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