Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / simix / smx_io.c
1 /* Copyright (c) 2007-2010, 2012-2013. 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 /**
19  * \brief Internal function to create a SIMIX storage.
20  * \param name name of the storage to create
21  * \param storage the SURF storage to encapsulate
22  * \param data some user data (may be NULL)
23  */
24 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
25 {
26   smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
27
28   smx_storage->data = data;
29
30   /* Update global variables */
31   xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
32
33   return xbt_lib_get_or_null(storage_lib, name, SIMIX_STORAGE_LEVEL);
34 }
35
36
37 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
38   return SIMIX_file_get_data(fd);
39 }
40
41 void* SIMIX_file_get_data(smx_file_t fd){
42   xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
43
44   return fd->data;
45 }
46
47 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
48   SIMIX_file_set_data(fd, data);
49 }
50
51 void SIMIX_file_set_data(smx_file_t fd, void *data){
52   xbt_assert((fd != NULL), "Invalid parameter");
53
54   fd->data = data;
55 }
56
57 //SIMIX FILE READ
58 void SIMIX_pre_file_read(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
59 {
60   smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size);
61   xbt_fifo_push(action->simcalls, simcall);
62   simcall->issuer->waiting_action = action;
63 }
64
65 smx_action_t SIMIX_file_read(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
66 {
67   smx_action_t action;
68   smx_host_t host = process->smx_host;
69
70   /* check if the host is active */
71   if (surf_workstation_model->extension.
72       workstation.get_state(host) != SURF_RESOURCE_ON) {
73     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
74            sg_host_name(host));
75   }
76
77   action = xbt_mallocator_get(simix_global->action_mallocator);
78   action->type = SIMIX_ACTION_IO;
79   action->name = NULL;
80 #ifdef HAVE_TRACING
81   action->category = NULL;
82 #endif
83
84   action->io.host = host;
85   action->io.surf_io =
86       surf_workstation_model->extension.workstation.read(host, fd->surf_file, size);
87
88   surf_workstation_model->action_data_set(action->io.surf_io, action);
89   XBT_DEBUG("Create io action %p", action);
90
91   return action;
92 }
93
94 //SIMIX FILE WRITE
95 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
96 {
97   smx_action_t action = SIMIX_file_write(simcall->issuer, fd,  size);
98   xbt_fifo_push(action->simcalls, simcall);
99   simcall->issuer->waiting_action = action;
100 }
101
102 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
103 {
104   smx_action_t action;
105   smx_host_t host = process->smx_host;
106
107   /* check if the host is active */
108   if (surf_workstation_model->extension.
109       workstation.get_state(host) != SURF_RESOURCE_ON) {
110     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
111            sg_host_name(host));
112   }
113
114   action = xbt_mallocator_get(simix_global->action_mallocator);
115   action->type = SIMIX_ACTION_IO;
116   action->name = NULL;
117 #ifdef HAVE_TRACING
118   action->category = NULL;
119 #endif
120
121   action->io.host = host;
122   action->io.surf_io =
123       surf_workstation_model->extension.workstation.write(host, fd->surf_file, size);
124
125   surf_workstation_model->action_data_set(action->io.surf_io, action);
126   XBT_DEBUG("Create io action %p", action);
127
128   return action;
129 }
130
131 //SIMIX FILE OPEN
132 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
133                          const char* path)
134 {
135   smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
136   xbt_fifo_push(action->simcalls, simcall);
137   simcall->issuer->waiting_action = action;
138 }
139
140 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
141                              const char* path)
142 {
143   smx_action_t action;
144   smx_host_t host = process->smx_host;
145
146   /* check if the host is active */
147   if (surf_workstation_model->extension.
148       workstation.get_state(host) != SURF_RESOURCE_ON) {
149     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
150            sg_host_name(host));
151   }
152
153   action = xbt_mallocator_get(simix_global->action_mallocator);
154   action->type = SIMIX_ACTION_IO;
155   action->name = NULL;
156 #ifdef HAVE_TRACING
157   action->category = NULL;
158 #endif
159
160   action->io.host = host;
161   action->io.surf_io =
162       surf_workstation_model->extension.workstation.open(host, mount, path);
163
164   surf_workstation_model->action_data_set(action->io.surf_io, action);
165   XBT_DEBUG("Create io action %p", action);
166
167   return action;
168 }
169
170 //SIMIX FILE CLOSE
171 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
172 {
173   smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
174   xbt_fifo_push(action->simcalls, simcall);
175   simcall->issuer->waiting_action = action;
176 }
177
178 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
179 {
180   smx_action_t action;
181   smx_host_t host = process->smx_host;
182
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   action = xbt_mallocator_get(simix_global->action_mallocator);
191   action->type = SIMIX_ACTION_IO;
192   action->name = NULL;
193 #ifdef HAVE_TRACING
194   action->category = NULL;
195 #endif
196
197   action->io.host = host;
198   action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
199
200   surf_workstation_model->action_data_set(action->io.surf_io, action);
201   XBT_DEBUG("Create io action %p", action);
202
203   return action;
204 }
205
206
207 //SIMIX FILE UNLINK
208 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
209 {
210   return SIMIX_file_unlink(simcall->issuer, fd);
211 }
212
213 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
214 {
215   smx_host_t host = process->smx_host;
216   /* check if the host is active */
217   if (surf_workstation_model->extension.
218       workstation.get_state(host) != SURF_RESOURCE_ON) {
219     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
220            sg_host_name(host));
221   }
222
223   if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
224     fd->surf_file = NULL;
225     return 1;
226   } else
227     return 0;
228 }
229
230 //SIMIX FILE LS
231 void SIMIX_pre_file_ls(smx_simcall_t simcall,
232                        const char* mount, const char* path)
233 {
234   smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
235   xbt_fifo_push(action->simcalls, simcall);
236   simcall->issuer->waiting_action = action;
237 }
238 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
239 {
240   smx_action_t action;
241   smx_host_t host = process->smx_host;
242   /* check if the host is active */
243   if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
244     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
245            sg_host_name(host));
246   }
247
248   action = xbt_mallocator_get(simix_global->action_mallocator);
249   action->type = SIMIX_ACTION_IO;
250   action->name = NULL;
251 #ifdef HAVE_TRACING
252   action->category = NULL;
253 #endif
254
255   action->io.host = host;
256   action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
257
258   surf_workstation_model->action_data_set(action->io.surf_io, action);
259   XBT_DEBUG("Create io action %p", action);
260   return action;
261 }
262
263 sg_storage_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
264 {
265   return SIMIX_file_get_size(simcall->issuer, fd);
266 }
267
268 sg_storage_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
269 {
270   smx_host_t host = process->smx_host;
271   return  surf_workstation_model->extension.workstation.get_size(host,
272       fd->surf_file);
273 }
274
275 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
276 {
277   return SIMIX_file_get_info(simcall->issuer, fd);
278 }
279
280 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
281 {
282   smx_host_t host = process->smx_host;
283   return  surf_workstation_model->extension.workstation.get_info(host,
284       fd->surf_file);
285 }
286
287 sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
288 {
289   return SIMIX_storage_get_free_size(simcall->issuer, name);
290 }
291
292 sg_storage_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
293 {
294   smx_host_t host = process->smx_host;
295   return  surf_workstation_model->extension.workstation.get_free_size(host,name);
296 }
297
298 sg_storage_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
299 {
300   return SIMIX_storage_get_used_size(simcall->issuer, name);
301 }
302
303 sg_storage_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
304 {
305   smx_host_t host = process->smx_host;
306   return  surf_workstation_model->extension.workstation.get_used_size(host,name);
307 }
308
309 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
310   return SIMIX_storage_get_properties(storage);
311 }
312 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
313   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
314   return surf_storage_model->extension.storage.get_properties(storage);
315 }
316
317 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
318    return SIMIX_storage_get_name(storage);
319 }
320
321 const char* SIMIX_storage_get_name(smx_storage_t storage){
322   xbt_assert((storage != NULL), "Invalid parameters");
323   return sg_storage_name(storage);
324 }
325
326 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
327   SIMIX_storage_set_data(storage, data);
328 }
329 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
330   xbt_assert((storage != NULL), "Invalid parameters");
331   xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
332
333   SIMIX_storage_priv(storage)->data = data;
334 }
335
336 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
337   return SIMIX_storage_get_data(storage);
338 }
339
340 void* SIMIX_storage_get_data(smx_storage_t storage){
341   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
342
343   return SIMIX_storage_priv(storage)->data;
344 }
345
346 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
347   return SIMIX_storage_get_content(storage);
348 }
349
350 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
351   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
352   return surf_storage_model->extension.storage.get_content(storage);
353 }
354
355 sg_storage_size_t SIMIX_storage_get_size(smx_storage_t storage){
356   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
357   return surf_storage_model->extension.storage.get_size(storage);
358 }
359
360 void SIMIX_post_io(smx_action_t action)
361 {
362   xbt_fifo_item_t i;
363   smx_simcall_t simcall;
364 //  char* key;
365 //  xbt_dict_cursor_t cursor = NULL;
366 //  s_file_stat_t *dst = NULL;
367 //  s_file_stat_t *src = NULL;
368
369   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
370     switch (simcall->call) {
371     case SIMCALL_FILE_OPEN:;
372       smx_file_t tmp = xbt_new(s_smx_file_t,1);
373       tmp->surf_file = (action->io.surf_io)->file;
374       simcall_file_open__set__result(simcall, tmp);
375       break;
376
377     case SIMCALL_FILE_CLOSE:
378       xbt_free(simcall_file_close__get__fd(simcall));
379       simcall_file_close__set__result(simcall, 0);
380       break;
381
382     case SIMCALL_FILE_WRITE:
383       simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
384       break;
385
386     case SIMCALL_FILE_READ:
387       simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
388       break;
389
390     case SIMCALL_FILE_LS:
391 //      xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
392 //        // if there is a stat we have to duplicate it
393 //        if(src){
394 //          dst = xbt_new0(s_file_stat_t,1);
395 //          file_stat_copy(src, dst);
396 //          xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
397 //        }
398 //      }
399       simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
400       break;
401     default:
402       break;
403     }
404   }
405
406   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
407
408     case SURF_ACTION_FAILED:
409       action->state = SIMIX_FAILED;
410       break;
411
412     case SURF_ACTION_DONE:
413       action->state = SIMIX_DONE;
414       break;
415
416     default:
417       THROW_IMPOSSIBLE;
418       break;
419   }
420
421   SIMIX_io_finish(action);
422 }
423
424 void SIMIX_io_destroy(smx_action_t action)
425 {
426   XBT_DEBUG("Destroy action %p", action);
427   if (action->io.surf_io)
428     action->io.surf_io->model_type->action_unref(action->io.surf_io);
429   xbt_mallocator_release(simix_global->action_mallocator, action);
430 }
431
432 void SIMIX_io_finish(smx_action_t action)
433 {
434   xbt_fifo_item_t item;
435   smx_simcall_t simcall;
436
437   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
438
439     switch (action->state) {
440
441       case SIMIX_DONE:
442         /* do nothing, action done */
443         break;
444
445       case SIMIX_FAILED:
446         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
447         break;
448
449       case SIMIX_CANCELED:
450         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
451         break;
452
453       default:
454         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
455             (int)action->state);
456     }
457
458     if (surf_workstation_model->extension.
459         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
460       simcall->issuer->context->iwannadie = 1;
461     }
462
463     simcall->issuer->waiting_action = NULL;
464     SIMIX_simcall_answer(simcall);
465   }
466
467   /* We no longer need it */
468   SIMIX_io_destroy(action);
469 }