Logo AND Algorithmique Numérique Distribuée

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