Logo AND Algorithmique Numérique Distribuée

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