Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dfecdf01206b50904a81fa8930e497e4eb373473
[simgrid.git] / src / simix / smx_io.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14                                 "Logging specific to SIMIX (io)");
15
16
17 //SIMIX FILE READ
18 void SIMIX_pre_file_read(smx_simcall_t simcall, void *ptr, size_t size,
19                          size_t nmemb, smx_file_t stream)
20 {
21   smx_action_t action = SIMIX_file_read(simcall->issuer, ptr, size, nmemb, stream);
22   xbt_fifo_push(action->simcalls, simcall);
23   simcall->issuer->waiting_action = action;
24 }
25
26 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream)
27 {
28   smx_action_t action;
29   smx_host_t host = process->smx_host;
30
31   /* check if the host is active */
32   if (surf_workstation_model->extension.
33       workstation.get_state(host) != SURF_RESOURCE_ON) {
34     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
35            sg_host_name(host));
36   }
37
38   action = xbt_mallocator_get(simix_global->action_mallocator);
39   action->type = SIMIX_ACTION_IO;
40   action->name = NULL;
41 #ifdef HAVE_TRACING
42   action->category = NULL;
43 #endif
44
45   action->io.host = host;
46   action->io.surf_io = surf_workstation_model->extension.workstation.read(host, ptr, size, nmemb, stream->surf_file);
47
48   surf_workstation_model->action_data_set(action->io.surf_io, action);
49   XBT_DEBUG("Create io action %p", action);
50
51   return action;
52 }
53
54 //SIMIX FILE WRITE
55 void SIMIX_pre_file_write(smx_simcall_t simcall, const void *ptr, size_t size,
56                           size_t nmemb, smx_file_t stream)
57 {
58   smx_action_t action = SIMIX_file_write(simcall->issuer, ptr, size, nmemb, stream);
59   xbt_fifo_push(action->simcalls, simcall);
60   simcall->issuer->waiting_action = action;
61 }
62
63 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
64 {
65   smx_action_t action;
66   smx_host_t host = process->smx_host;
67
68   /* check if the host is active */
69   if (surf_workstation_model->extension.
70       workstation.get_state(host) != SURF_RESOURCE_ON) {
71     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
72            sg_host_name(host));
73   }
74
75   action = xbt_mallocator_get(simix_global->action_mallocator);
76   action->type = SIMIX_ACTION_IO;
77   action->name = NULL;
78 #ifdef HAVE_TRACING
79   action->category = NULL;
80 #endif
81
82   action->io.host = host;
83   action->io.surf_io = surf_workstation_model->extension.workstation.write(host, ptr, size, nmemb, stream->surf_file);
84
85   surf_workstation_model->action_data_set(action->io.surf_io, action);
86   XBT_DEBUG("Create io action %p", action);
87
88   return action;
89 }
90
91 //SIMIX FILE OPEN
92 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
93                          const char* path, const char* mode)
94 {
95   smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path, mode);
96   xbt_fifo_push(action->simcalls, simcall);
97   simcall->issuer->waiting_action = action;
98 }
99
100 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount, const char* path, const char* mode)
101 {
102   smx_action_t action;
103   smx_host_t host = process->smx_host;
104
105   /* check if the host is active */
106   if (surf_workstation_model->extension.
107       workstation.get_state(host) != SURF_RESOURCE_ON) {
108     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
109            sg_host_name(host));
110   }
111
112   action = xbt_mallocator_get(simix_global->action_mallocator);
113   action->type = SIMIX_ACTION_IO;
114   action->name = NULL;
115 #ifdef HAVE_TRACING
116   action->category = NULL;
117 #endif
118
119   action->io.host = host;
120   action->io.surf_io = surf_workstation_model->extension.workstation.open(host, mount, path, mode);
121
122   surf_workstation_model->action_data_set(action->io.surf_io, action);
123   XBT_DEBUG("Create io action %p", action);
124
125   return action;
126 }
127
128 //SIMIX FILE CLOSE
129 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
130 {
131   smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
132   xbt_fifo_push(action->simcalls, simcall);
133   simcall->issuer->waiting_action = action;
134 }
135
136 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
137 {
138   smx_action_t action;
139   smx_host_t host = process->smx_host;
140
141   /* check if the host is active */
142   if (surf_workstation_model->extension.
143       workstation.get_state(host) != SURF_RESOURCE_ON) {
144     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
145            sg_host_name(host));
146   }
147
148   action = xbt_mallocator_get(simix_global->action_mallocator);
149   action->type = SIMIX_ACTION_IO;
150   action->name = NULL;
151 #ifdef HAVE_TRACING
152   action->category = NULL;
153 #endif
154
155   action->io.host = host;
156   action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
157
158   surf_workstation_model->action_data_set(action->io.surf_io, action);
159   XBT_DEBUG("Create io action %p", action);
160
161   return action;
162 }
163
164
165 //SIMIX FILE UNLINK
166 void SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
167 {
168   smx_action_t action = SIMIX_file_unlink(simcall->issuer, fd);
169   xbt_fifo_push(action->simcalls, simcall);
170   simcall->issuer->waiting_action = action;
171 }
172
173 smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
174 {
175   smx_action_t action;
176   smx_host_t host = process->smx_host;
177   /* check if the host is active */
178   if (surf_workstation_model->extension.
179       workstation.get_state(host) != SURF_RESOURCE_ON) {
180     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
181            sg_host_name(host));
182   }
183
184   action = xbt_mallocator_get(simix_global->action_mallocator);
185   action->type = SIMIX_ACTION_IO;
186   action->name = NULL;
187 #ifdef HAVE_TRACING
188   action->category = NULL;
189 #endif
190
191   action->io.host = host;
192   action->io.surf_io = surf_workstation_model->extension.workstation.unlink(host, fd->surf_file);
193
194   surf_workstation_model->action_data_set(action->io.surf_io, action);
195   XBT_DEBUG("Create io action %p", action);
196
197   return action;
198 }
199
200 //SIMIX FILE LS
201 void SIMIX_pre_file_ls(smx_simcall_t simcall,
202                        const char* mount, const char* path)
203 {
204   smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
205   xbt_fifo_push(action->simcalls, simcall);
206   simcall->issuer->waiting_action = action;
207 }
208 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
209 {
210   smx_action_t action;
211   smx_host_t host = process->smx_host;
212   /* check if the host is active */
213   if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
214     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
215            sg_host_name(host));
216   }
217
218   action = xbt_mallocator_get(simix_global->action_mallocator);
219   action->type = SIMIX_ACTION_IO;
220   action->name = NULL;
221 #ifdef HAVE_TRACING
222   action->category = NULL;
223 #endif
224
225   action->io.host = host;
226   action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
227
228   surf_workstation_model->action_data_set(action->io.surf_io, action);
229   XBT_DEBUG("Create io action %p", action);
230   return action;
231 }
232
233 void SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
234 {
235   smx_action_t action = SIMIX_file_get_size(simcall->issuer, fd);
236   xbt_fifo_push(action->simcalls, simcall);
237   simcall->issuer->waiting_action = action;
238 }
239
240 smx_action_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
241 {
242   smx_action_t action;
243   smx_host_t host = process->smx_host;
244
245   /* check if the host is active */
246   if (surf_workstation_model->extension.
247       workstation.get_state(host) != SURF_RESOURCE_ON) {
248     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
249            sg_host_name(host));
250   }
251
252   action = xbt_mallocator_get(simix_global->action_mallocator);
253   action->type = SIMIX_ACTION_IO;
254   action->name = NULL;
255 #ifdef HAVE_TRACING
256   action->category = NULL;
257 #endif
258
259   action->io.host = host;
260   action->io.surf_io = surf_workstation_model->extension.workstation.get_size(host, fd->surf_file);
261
262   surf_workstation_model->action_data_set(action->io.surf_io, action);
263   XBT_DEBUG("Create io action %p", action);
264
265   return action;
266 }
267
268
269 void SIMIX_post_io(smx_action_t action)
270 {
271   xbt_fifo_item_t i;
272   smx_simcall_t simcall;
273 //  char* key;
274 //  xbt_dict_cursor_t cursor = NULL;
275 //  s_file_stat_t *dst = NULL;
276 //  s_file_stat_t *src = NULL;
277
278   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
279     switch (simcall->call) {
280     case SIMCALL_FILE_OPEN:;
281       smx_file_t tmp = xbt_new(s_smx_file_t,1);
282       tmp->surf_file = (action->io.surf_io)->file;
283       simcall_file_open__set__result(simcall, tmp);
284       break;
285
286     case SIMCALL_FILE_CLOSE:
287       xbt_free(simcall_file_close__get__fd(simcall));
288       simcall_file_close__set__result(simcall, 0);
289       break;
290
291     case SIMCALL_FILE_WRITE:
292       simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
293       break;
294
295     case SIMCALL_FILE_READ:
296       simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
297       break;
298
299     case SIMCALL_FILE_UNLINK:
300       xbt_free(simcall_file_unlink__get__fd(simcall));
301       simcall_file_unlink__set__result(simcall, 0);
302       break;
303
304     case SIMCALL_FILE_LS:
305 //      xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
306 //        // if there is a stat we have to duplicate it
307 //        if(src){
308 //          dst = xbt_new0(s_file_stat_t,1);
309 //          file_stat_copy(src, dst);
310 //          xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
311 //        }
312 //      }
313       simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
314       break;
315     case SIMCALL_FILE_GET_SIZE:
316       xbt_free(simcall_file_get_size__get__fd(simcall));
317       simcall_file_get_size__set__result(simcall, 0);
318       break;
319
320     default:
321       break;
322     }
323   }
324
325   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
326
327     case SURF_ACTION_FAILED:
328       action->state = SIMIX_FAILED;
329       break;
330
331     case SURF_ACTION_DONE:
332       action->state = SIMIX_DONE;
333       break;
334
335     default:
336       THROW_IMPOSSIBLE;
337       break;
338   }
339
340   SIMIX_io_finish(action);
341 }
342
343 void SIMIX_io_destroy(smx_action_t action)
344 {
345   XBT_DEBUG("Destroy action %p", action);
346   if (action->io.surf_io)
347     action->io.surf_io->model_type->action_unref(action->io.surf_io);
348   xbt_mallocator_release(simix_global->action_mallocator, action);
349 }
350
351 void SIMIX_io_finish(smx_action_t action)
352 {
353   xbt_fifo_item_t item;
354   smx_simcall_t simcall;
355
356   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
357
358     switch (action->state) {
359
360       case SIMIX_DONE:
361         /* do nothing, action done */
362         break;
363
364       case SIMIX_FAILED:
365         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
366         break;
367
368       case SIMIX_CANCELED:
369         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
370         break;
371
372       default:
373         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
374             (int)action->state);
375     }
376
377     if (surf_workstation_model->extension.
378         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
379       simcall->issuer->context->iwannadie = 1;
380     }
381
382     simcall->issuer->waiting_action = NULL;
383     SIMIX_simcall_answer(simcall);
384   }
385
386   /* We no longer need it */
387   SIMIX_io_destroy(action);
388 }