Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change simcall functions
[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->host) != SURF_RESOURCE_ON) {
34     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
35            host->name);
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->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->host) != SURF_RESOURCE_ON) {
71     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
72            host->name);
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->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->host) != SURF_RESOURCE_ON) {
108     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
109            host->name);
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->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 fp)
130 {
131   smx_action_t action = SIMIX_file_close(simcall->issuer, fp);
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 fp)
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->host) != SURF_RESOURCE_ON) {
144     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
145            host->name);
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->host, fp->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 //SIMIX FILE STAT
165 void SIMIX_pre_file_stat(smx_simcall_t simcall, smx_file_t fd, s_file_stat_t buf)
166 {
167   smx_action_t action = SIMIX_file_stat(simcall->issuer, fd, buf);
168   xbt_fifo_push(action->simcalls, simcall);
169   simcall->issuer->waiting_action = action;
170 }
171
172 smx_action_t SIMIX_file_stat(smx_process_t process, smx_file_t fd, s_file_stat_t buf)
173 {
174   smx_action_t action;
175   smx_host_t host = process->smx_host;
176   /* check if the host is active */
177   if (surf_workstation_model->extension.
178       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
179     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
180            host->name);
181   }
182
183   action = xbt_mallocator_get(simix_global->action_mallocator);
184   action->type = SIMIX_ACTION_IO;
185   action->name = NULL;
186 #ifdef HAVE_TRACING
187   action->category = NULL;
188 #endif
189
190   action->io.host = host;
191   action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, fd->surf_file);
192
193   surf_workstation_model->action_data_set(action->io.surf_io, action);
194   XBT_DEBUG("Create io action %p", action);
195
196   return action;
197 }
198
199 //SIMIX FILE UNLINK
200 void SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
201 {
202   smx_action_t action = SIMIX_file_unlink(simcall->issuer, fd);
203   xbt_fifo_push(action->simcalls, simcall);
204   simcall->issuer->waiting_action = action;
205 }
206
207 smx_action_t SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
208 {
209   smx_action_t action;
210   smx_host_t host = process->smx_host;
211   /* check if the host is active */
212   if (surf_workstation_model->extension.
213       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
214     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
215            host->name);
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.unlink(host->host, fd->surf_file);
227
228   surf_workstation_model->action_data_set(action->io.surf_io, action);
229   XBT_DEBUG("Create io action %p", action);
230
231   return action;
232 }
233
234 //SIMIX FILE LS
235 void SIMIX_pre_file_ls(smx_simcall_t simcall,
236                        const char* mount, const char* path)
237 {
238   smx_action_t action = SIMIX_file_ls(simcall->issuer,
239       simcall->file_ls.mount, simcall->file_ls.path);
240   xbt_fifo_push(action->simcalls, simcall);
241   simcall->issuer->waiting_action = action;
242 }
243 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
244 {
245   smx_action_t action;
246   smx_host_t host = process->smx_host;
247   /* check if the host is active */
248   if (surf_workstation_model->extension.workstation.get_state(host->host) != SURF_RESOURCE_ON) {
249     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
250            host->name);
251   }
252
253   action = xbt_mallocator_get(simix_global->action_mallocator);
254   action->type = SIMIX_ACTION_IO;
255   action->name = NULL;
256 #ifdef HAVE_TRACING
257   action->category = NULL;
258 #endif
259
260   action->io.host = host;
261   action->io.surf_io = surf_workstation_model->extension.workstation.ls(host->host,mount,path);
262
263   surf_workstation_model->action_data_set(action->io.surf_io, action);
264   XBT_DEBUG("Create io action %p", action);
265   return action;
266 }
267
268 static void free_file_stat(void *p)
269 {
270   file_stat_t fs = p;
271   xbt_free(fs->date);
272   xbt_free(fs->group);
273   xbt_free(fs->time);
274   xbt_free(fs->user);
275   xbt_free(fs->user_rights);
276   xbt_free(fs);
277 }
278
279 void SIMIX_post_io(smx_action_t action)
280 {
281   xbt_fifo_item_t i;
282   smx_simcall_t simcall;
283   char* key;
284   xbt_dict_cursor_t cursor = NULL;
285   s_file_stat_t *dst = NULL;
286   s_file_stat_t *src = NULL;
287
288   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
289     switch (simcall->call) {
290     case SIMCALL_FILE_OPEN:;
291       smx_file_t tmp = xbt_new(s_smx_file_t,1);
292       tmp->surf_file = (action->io.surf_io)->file;
293       simcall->result.p = tmp;
294       break;
295
296     case SIMCALL_FILE_CLOSE:
297       xbt_free(simcall->file_close.fp);
298       simcall->result.i = 0;
299       break;
300
301     case SIMCALL_FILE_WRITE:
302       simcall->result.si = (action->io.surf_io)->cost;
303       break;
304
305     case SIMCALL_FILE_READ:
306       simcall->result.d = (action->io.surf_io)->cost;
307       break;
308
309     case SIMCALL_FILE_STAT:
310       simcall->result.i = 0;
311       dst = &(simcall->file_stat.buf);
312       src = &((action->io.surf_io)->stat);
313       file_stat_copy(src,dst);
314       break;
315
316     case SIMCALL_FILE_UNLINK:
317       xbt_free(simcall->file_unlink.fd);
318       simcall->result.i = 0;
319       break;
320
321     case SIMCALL_FILE_LS:
322       xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
323         // if there is a stat we have to duplicate it
324         if(src){
325           dst = xbt_new0(s_file_stat_t,1);
326           file_stat_copy(src, dst);
327           xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,free_file_stat);
328         }
329       }
330       simcall->file_ls.result = (action->io.surf_io)->ls_dict;
331       break;
332
333     default:
334       break;
335     }
336   }
337
338   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
339
340     case SURF_ACTION_FAILED:
341       action->state = SIMIX_FAILED;
342       break;
343
344     case SURF_ACTION_DONE:
345       action->state = SIMIX_DONE;
346       break;
347
348     default:
349       THROW_IMPOSSIBLE;
350       break;
351   }
352
353   SIMIX_io_finish(action);
354 }
355
356 void SIMIX_io_destroy(smx_action_t action)
357 {
358   XBT_DEBUG("Destroy action %p", action);
359   if (action->io.surf_io)
360     action->io.surf_io->model_type->action_unref(action->io.surf_io);
361   xbt_mallocator_release(simix_global->action_mallocator, action);
362 }
363
364 void SIMIX_io_finish(smx_action_t action)
365 {
366   xbt_fifo_item_t item;
367   smx_simcall_t simcall;
368
369   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
370
371     switch (action->state) {
372
373       case SIMIX_DONE:
374         /* do nothing, action done */
375         break;
376
377       case SIMIX_FAILED:
378         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
379         break;
380
381       case SIMIX_CANCELED:
382         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
383         break;
384
385       default:
386         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
387             (int)action->state);
388     }
389
390     if (surf_workstation_model->extension.
391         workstation.get_state(simcall->issuer->smx_host->host) != SURF_RESOURCE_ON) {
392       simcall->issuer->context->iwannadie = 1;
393     }
394
395     simcall->issuer->waiting_action = NULL;
396     SIMIX_simcall_answer(simcall);
397   }
398
399   /* We no longer need it */
400   SIMIX_io_destroy(action);
401 }