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, 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 /**
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   return xbt_lib_get_elm_or_null(storage_lib, name);
33 }
34
35
36 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
37   return SIMIX_file_get_data(fd);
38 }
39
40 void* SIMIX_file_get_data(smx_file_t fd){
41   xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
42
43   return fd->data;
44 }
45
46 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
47   SIMIX_file_set_data(fd, data);
48 }
49
50 void SIMIX_file_set_data(smx_file_t fd, void *data){
51   xbt_assert((fd != NULL), "Invalid parameter");
52
53   fd->data = data;
54 }
55
56 //SIMIX FILE READ
57 void SIMIX_pre_file_read(smx_simcall_t simcall, size_t size,
58                         smx_file_t fd)
59 {
60   smx_action_t action = SIMIX_file_read(simcall->issuer, size, fd);
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, size_t size,
66                              smx_file_t fd)
67 {
68   smx_action_t action;
69   smx_host_t host = process->smx_host;
70
71   /* check if the host is active */
72   if (surf_workstation_model->extension.
73       workstation.get_state(host) != SURF_RESOURCE_ON) {
74     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
75            sg_host_name(host));
76   }
77
78   action = xbt_mallocator_get(simix_global->action_mallocator);
79   action->type = SIMIX_ACTION_IO;
80   action->name = NULL;
81 #ifdef HAVE_TRACING
82   action->category = NULL;
83 #endif
84
85   action->io.host = host;
86   action->io.surf_io =
87       surf_workstation_model->extension.workstation.read(host, size,
88                                                          fd->surf_file);
89
90   surf_workstation_model->action_data_set(action->io.surf_io, action);
91   XBT_DEBUG("Create io action %p", action);
92
93   return action;
94 }
95
96 //SIMIX FILE WRITE
97 void SIMIX_pre_file_write(smx_simcall_t simcall, size_t size,
98                           smx_file_t fd)
99 {
100   smx_action_t action = SIMIX_file_write(simcall->issuer, size, fd);
101   xbt_fifo_push(action->simcalls, simcall);
102   simcall->issuer->waiting_action = action;
103 }
104
105 smx_action_t SIMIX_file_write(smx_process_t process,
106                               size_t size, smx_file_t fd)
107 {
108   smx_action_t action;
109   smx_host_t host = process->smx_host;
110
111   /* check if the host is active */
112   if (surf_workstation_model->extension.
113       workstation.get_state(host) != SURF_RESOURCE_ON) {
114     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
115            sg_host_name(host));
116   }
117
118   action = xbt_mallocator_get(simix_global->action_mallocator);
119   action->type = SIMIX_ACTION_IO;
120   action->name = NULL;
121 #ifdef HAVE_TRACING
122   action->category = NULL;
123 #endif
124
125   action->io.host = host;
126   action->io.surf_io =
127       surf_workstation_model->extension.workstation.write(host, size,
128                                                           fd->surf_file);
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 OPEN
137 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
138                          const char* path)
139 {
140   smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
141   xbt_fifo_push(action->simcalls, simcall);
142   simcall->issuer->waiting_action = action;
143 }
144
145 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
146                              const char* path)
147 {
148   smx_action_t action;
149   smx_host_t host = process->smx_host;
150
151   /* check if the host is active */
152   if (surf_workstation_model->extension.
153       workstation.get_state(host) != SURF_RESOURCE_ON) {
154     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
155            sg_host_name(host));
156   }
157
158   action = xbt_mallocator_get(simix_global->action_mallocator);
159   action->type = SIMIX_ACTION_IO;
160   action->name = NULL;
161 #ifdef HAVE_TRACING
162   action->category = NULL;
163 #endif
164
165   action->io.host = host;
166   action->io.surf_io =
167       surf_workstation_model->extension.workstation.open(host, mount, path);
168
169   surf_workstation_model->action_data_set(action->io.surf_io, action);
170   XBT_DEBUG("Create io action %p", action);
171
172   return action;
173 }
174
175 //SIMIX FILE CLOSE
176 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
177 {
178   smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
179   xbt_fifo_push(action->simcalls, simcall);
180   simcall->issuer->waiting_action = action;
181 }
182
183 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
184 {
185   smx_action_t action;
186   smx_host_t host = process->smx_host;
187
188   /* check if the host is active */
189   if (surf_workstation_model->extension.
190       workstation.get_state(host) != SURF_RESOURCE_ON) {
191     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
192            sg_host_name(host));
193   }
194
195   action = xbt_mallocator_get(simix_global->action_mallocator);
196   action->type = SIMIX_ACTION_IO;
197   action->name = NULL;
198 #ifdef HAVE_TRACING
199   action->category = NULL;
200 #endif
201
202   action->io.host = host;
203   action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
204
205   surf_workstation_model->action_data_set(action->io.surf_io, action);
206   XBT_DEBUG("Create io action %p", action);
207
208   return action;
209 }
210
211
212 //SIMIX FILE UNLINK
213 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
214 {
215   return SIMIX_file_unlink(simcall->issuer, fd);
216 }
217
218 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
219 {
220   smx_host_t host = process->smx_host;
221   /* check if the host is active */
222   if (surf_workstation_model->extension.
223       workstation.get_state(host) != SURF_RESOURCE_ON) {
224     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
225            sg_host_name(host));
226   }
227
228   if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
229     fd->surf_file = NULL;
230     return 1;
231   } else
232     return 0;
233 }
234
235 //SIMIX FILE LS
236 void SIMIX_pre_file_ls(smx_simcall_t simcall,
237                        const char* mount, const char* path)
238 {
239   smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, 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) != SURF_RESOURCE_ON) {
249     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
250            sg_host_name(host));
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,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 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
269 {
270   return SIMIX_file_get_size(simcall->issuer, fd);
271 }
272
273 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
274 {
275   smx_host_t host = process->smx_host;
276   return  surf_workstation_model->extension.workstation.get_size(host,
277       fd->surf_file);
278 }
279
280 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
281 {
282   return SIMIX_file_get_info(simcall->issuer, fd);
283 }
284
285 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
286 {
287   smx_host_t host = process->smx_host;
288   return  surf_workstation_model->extension.workstation.get_info(host,
289       fd->surf_file);
290 }
291
292 size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
293 {
294   return SIMIX_storage_get_free_size(simcall->issuer, name);
295 }
296
297 size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
298 {
299   smx_host_t host = process->smx_host;
300   return  surf_workstation_model->extension.workstation.get_free_size(host,name);
301 }
302
303 size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
304 {
305   return SIMIX_storage_get_used_size(simcall->issuer, name);
306 }
307
308 size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
309 {
310   smx_host_t host = process->smx_host;
311   return  surf_workstation_model->extension.workstation.get_used_size(host,name);
312 }
313
314 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
315   return SIMIX_storage_get_properties(storage);
316 }
317 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
318   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
319   return surf_storage_model->extension.storage.get_properties(storage);
320 }
321
322 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
323    return SIMIX_storage_get_name(storage);
324 }
325
326 const char* SIMIX_storage_get_name(smx_storage_t storage){
327   xbt_assert((storage != NULL), "Invalid parameters");
328   return sg_storage_name(storage);
329 }
330
331 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
332   SIMIX_storage_set_data(storage, data);
333 }
334 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
335   xbt_assert((storage != NULL), "Invalid parameters");
336   xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
337
338   SIMIX_storage_priv(storage)->data = data;
339 }
340
341 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
342   return SIMIX_storage_get_data(storage);
343 }
344
345 void* SIMIX_storage_get_data(smx_storage_t storage){
346   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
347
348   return SIMIX_storage_priv(storage)->data;
349 }
350
351 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
352   return SIMIX_storage_get_content(storage);
353 }
354
355 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
356   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
357   return surf_storage_model->extension.storage.get_content(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 }