Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : init result of simcalls for snapshot comparison done during simcall
[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)
19 {
20   smx_action_t action = SIMIX_file_read(simcall->issuer,
21       simcall->file_read.ptr,
22       simcall->file_read.size,
23       simcall->file_read.nmemb,
24       simcall->file_read.stream);
25   xbt_fifo_push(action->simcalls, simcall);
26   simcall->issuer->waiting_action = action;
27 }
28
29 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream)
30 {
31   smx_action_t action;
32   smx_host_t host = process->smx_host;
33
34   /* check if the host is active */
35   if (surf_workstation_model->extension.
36       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
37     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
38            host->name);
39   }
40
41   action = xbt_mallocator_get(simix_global->action_mallocator);
42   action->type = SIMIX_ACTION_IO;
43   action->name = NULL;
44 #ifdef HAVE_TRACING
45   action->category = NULL;
46 #endif
47
48   action->io.host = host;
49   action->io.surf_io = surf_workstation_model->extension.workstation.read(host->host, ptr, size, nmemb, stream->surf_file);
50
51   surf_workstation_model->action_data_set(action->io.surf_io, action);
52   XBT_DEBUG("Create io action %p", action);
53
54   return action;
55 }
56
57 //SIMIX FILE WRITE
58 void SIMIX_pre_file_write(smx_simcall_t simcall)
59 {
60   smx_action_t action = SIMIX_file_write(simcall->issuer,
61       simcall->file_write.ptr,
62       simcall->file_write.size,
63       simcall->file_write.nmemb,
64       simcall->file_write.stream);
65   xbt_fifo_push(action->simcalls, simcall);
66   simcall->issuer->waiting_action = action;
67 }
68
69 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
70 {
71   smx_action_t action;
72   smx_host_t host = process->smx_host;
73
74   /* check if the host is active */
75   if (surf_workstation_model->extension.
76       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
77     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
78            host->name);
79   }
80
81   action = xbt_mallocator_get(simix_global->action_mallocator);
82   action->type = SIMIX_ACTION_IO;
83   action->name = NULL;
84 #ifdef HAVE_TRACING
85   action->category = NULL;
86 #endif
87
88   action->io.host = host;
89   action->io.surf_io = surf_workstation_model->extension.workstation.write(host->host, ptr, size, nmemb, stream->surf_file);
90
91   surf_workstation_model->action_data_set(action->io.surf_io, action);
92   XBT_DEBUG("Create io action %p", action);
93
94   return action;
95 }
96
97 //SIMIX FILE OPEN
98 void SIMIX_pre_file_open(smx_simcall_t simcall)
99 {
100   smx_action_t action = SIMIX_file_open(simcall->issuer,
101       simcall->file_open.mount,
102       simcall->file_open.path,
103       simcall->file_open.mode);
104   xbt_fifo_push(action->simcalls, simcall);
105   simcall->issuer->waiting_action = action;
106 }
107
108 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount, const char* path, const char* mode)
109 {
110   smx_action_t action;
111   smx_host_t host = process->smx_host;
112
113   /* check if the host is active */
114   if (surf_workstation_model->extension.
115       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
116     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
117            host->name);
118   }
119
120   action = xbt_mallocator_get(simix_global->action_mallocator);
121   action->type = SIMIX_ACTION_IO;
122   action->name = NULL;
123 #ifdef HAVE_TRACING
124   action->category = NULL;
125 #endif
126
127   action->io.host = host;
128   action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, mount, path, mode);
129
130   surf_workstation_model->action_data_set(action->io.surf_io, action);
131   XBT_DEBUG("Create io action %p", action);
132
133   return action;
134 }
135
136 //SIMIX FILE CLOSE
137 void SIMIX_pre_file_close(smx_simcall_t simcall)
138 {
139   smx_action_t action = SIMIX_file_close(simcall->issuer,
140       simcall->file_close.fp);
141   xbt_fifo_push(action->simcalls, simcall);
142   simcall->issuer->waiting_action = action;
143 }
144
145 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp)
146 {
147   smx_action_t action;
148   smx_host_t host = process->smx_host;
149
150   /* check if the host is active */
151   if (surf_workstation_model->extension.
152       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
153     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
154            host->name);
155   }
156
157   action = xbt_mallocator_get(simix_global->action_mallocator);
158   action->type = SIMIX_ACTION_IO;
159   action->name = NULL;
160 #ifdef HAVE_TRACING
161   action->category = NULL;
162 #endif
163
164   action->io.host = host;
165   action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, fp->surf_file);
166
167   surf_workstation_model->action_data_set(action->io.surf_io, action);
168   XBT_DEBUG("Create io action %p", action);
169
170   return action;
171 }
172
173 //SIMIX FILE STAT
174 void SIMIX_pre_file_stat(smx_simcall_t simcall)
175 {
176   smx_action_t action = SIMIX_file_stat(simcall->issuer,
177       simcall->file_stat.fd,
178       simcall->file_stat.buf);
179   xbt_fifo_push(action->simcalls, simcall);
180   simcall->issuer->waiting_action = action;
181 }
182
183 smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t buf)
184 {
185   smx_action_t action;
186   smx_host_t host = process->smx_host;
187   /* check if the host is active */
188   if (surf_workstation_model->extension.
189       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
190     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
191            host->name);
192   }
193
194   action = xbt_mallocator_get(simix_global->action_mallocator);
195   action->type = SIMIX_ACTION_IO;
196   action->name = NULL;
197 #ifdef HAVE_TRACING
198   action->category = NULL;
199 #endif
200
201   action->io.host = host;
202   action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, fd->surf_file);
203
204   surf_workstation_model->action_data_set(action->io.surf_io, action);
205   XBT_DEBUG("Create io action %p", action);
206
207   return action;
208 }
209
210 //SIMIX FILE UNLINK
211 void SIMIX_pre_file_unlink(smx_simcall_t simcall)
212 {
213   smx_action_t action = SIMIX_file_unlink(simcall->issuer,
214       simcall->file_unlink.fd);
215   xbt_fifo_push(action->simcalls, simcall);
216   simcall->issuer->waiting_action = action;
217 }
218
219 smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
220 {
221   smx_action_t action;
222   smx_host_t host = process->smx_host;
223   /* check if the host is active */
224   if (surf_workstation_model->extension.
225       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
226     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
227            host->name);
228   }
229
230   action = xbt_mallocator_get(simix_global->action_mallocator);
231   action->type = SIMIX_ACTION_IO;
232   action->name = NULL;
233 #ifdef HAVE_TRACING
234   action->category = NULL;
235 #endif
236
237   action->io.host = host;
238   action->io.surf_io = surf_workstation_model->extension.workstation.unlink(host->host, fd->surf_file);
239
240   surf_workstation_model->action_data_set(action->io.surf_io, action);
241   XBT_DEBUG("Create io action %p", action);
242
243   return action;
244 }
245
246 //SIMIX FILE LS
247 void SIMIX_pre_file_ls(smx_simcall_t simcall)
248 {
249   smx_action_t action = SIMIX_file_ls(simcall->issuer,
250       simcall->file_ls.mount, simcall->file_ls.path);
251   xbt_fifo_push(action->simcalls, simcall);
252   simcall->issuer->waiting_action = action;
253 }
254 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
255 {
256   smx_action_t action;
257   smx_host_t host = process->smx_host;
258   /* check if the host is active */
259   if (surf_workstation_model->extension.workstation.get_state(host->host) != SURF_RESOURCE_ON) {
260     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
261            host->name);
262   }
263
264   action = xbt_mallocator_get(simix_global->action_mallocator);
265   action->type = SIMIX_ACTION_IO;
266   action->name = NULL;
267 #ifdef HAVE_TRACING
268   action->category = NULL;
269 #endif
270
271   action->io.host = host;
272   action->io.surf_io = surf_workstation_model->extension.workstation.ls(host->host,mount,path);
273
274   surf_workstation_model->action_data_set(action->io.surf_io, action);
275   XBT_DEBUG("Create io action %p", action);
276   return action;
277 }
278
279 static void free_file_stat(void *p)
280 {
281   file_stat_t fs = p;
282   xbt_free(fs->date);
283   xbt_free(fs->group);
284   xbt_free(fs->time);
285   xbt_free(fs->user);
286   xbt_free(fs->user_rights);
287   xbt_free(fs);
288 }
289
290 void SIMIX_post_io(smx_action_t action)
291 {
292   xbt_fifo_item_t i;
293   smx_simcall_t simcall;
294   char* key;
295   xbt_dict_cursor_t cursor = NULL;
296   s_file_stat_t *dst = NULL;
297   s_file_stat_t *src = NULL;
298
299   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
300     switch (simcall->call) {
301     case SIMCALL_FILE_OPEN:
302       simcall->file_open.result = xbt_new(s_smx_file_t,1);
303       simcall->file_open.result->surf_file = (action->io.surf_io)->file;
304       break;
305
306     case SIMCALL_FILE_CLOSE:
307       xbt_free(simcall->file_close.fp);
308       simcall->file_close.result = 0;
309       break;
310
311     case SIMCALL_FILE_WRITE:
312       simcall->file_write.result = (action->io.surf_io)->cost;
313       break;
314
315     case SIMCALL_FILE_READ:
316       simcall->file_read.result = (action->io.surf_io)->cost;
317       break;
318
319     case SIMCALL_FILE_STAT:
320       simcall->file_stat.result = 0;
321       dst = &(simcall->file_stat.buf);
322       src = &((action->io.surf_io)->stat);
323       file_stat_copy(src,dst);
324       break;
325
326     case SIMCALL_FILE_UNLINK:
327       xbt_free(simcall->file_unlink.fd);
328       simcall->file_unlink.result = 0;
329       break;
330
331     case SIMCALL_FILE_LS:
332       xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
333         // if there is a stat we have to duplicate it
334         if(src){
335           dst = xbt_new0(s_file_stat_t,1);
336           file_stat_copy(src, dst);
337           xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,free_file_stat);
338         }
339       }
340       simcall->file_ls.result = (action->io.surf_io)->ls_dict;
341       break;
342
343     default:
344       break;
345     }
346   }
347
348   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
349
350     case SURF_ACTION_FAILED:
351       action->state = SIMIX_FAILED;
352       break;
353
354     case SURF_ACTION_DONE:
355       action->state = SIMIX_DONE;
356       break;
357
358     default:
359       THROW_IMPOSSIBLE;
360       break;
361   }
362
363   SIMIX_io_finish(action);
364 }
365
366 void SIMIX_io_destroy(smx_action_t action)
367 {
368   XBT_DEBUG("Destroy action %p", action);
369   if (action->io.surf_io)
370     action->io.surf_io->model_type->action_unref(action->io.surf_io);
371   xbt_mallocator_release(simix_global->action_mallocator, action);
372 }
373
374 void SIMIX_io_finish(smx_action_t action)
375 {
376   xbt_fifo_item_t item;
377   smx_simcall_t simcall;
378
379   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
380
381     switch (action->state) {
382
383       case SIMIX_DONE:
384         /* do nothing, action done */
385         break;
386
387       case SIMIX_FAILED:
388         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
389         break;
390
391       case SIMIX_CANCELED:
392         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
393         break;
394
395       default:
396         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
397             (int)action->state);
398     }
399
400     if (surf_workstation_model->extension.
401         workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
402       simcall->issuer->context->iwannadie = 1;
403     }
404
405     simcall->issuer->waiting_action = NULL;
406     SIMIX_simcall_answer(simcall);
407   }
408
409   /* We no longer need it */
410   SIMIX_io_destroy(action);
411 }