Logo AND Algorithmique Numérique Distribuée

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