Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge tag 'v3_9_90' into hypervisor
[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  * \brief Internal function to destroy a SIMIX storage.
38  *
39  * \param s the host to destroy (a smx_storage_t)
40  */
41 void SIMIX_storage_destroy(void *s)
42 {
43   smx_storage_priv_t storage = (smx_storage_priv_t) s;
44
45   xbt_assert((storage != NULL), "Invalid parameters");
46   if (storage->data)
47     free(storage->data);
48
49   /* Clean storage structure */
50   free(storage);
51 }
52
53 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
54   return SIMIX_file_get_data(fd);
55 }
56
57 void* SIMIX_file_get_data(smx_file_t fd){
58   xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
59
60   return fd->data;
61 }
62
63 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
64   SIMIX_file_set_data(fd, data);
65 }
66
67 void SIMIX_file_set_data(smx_file_t fd, void *data){
68   xbt_assert((fd != NULL), "Invalid parameter");
69
70   fd->data = data;
71 }
72
73 //SIMIX FILE READ
74 void SIMIX_pre_file_read(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
75 {
76   smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size);
77   xbt_fifo_push(action->simcalls, simcall);
78   simcall->issuer->waiting_action = action;
79 }
80
81 smx_action_t SIMIX_file_read(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
82 {
83   smx_action_t action;
84   smx_host_t host = process->smx_host;
85
86   /* check if the host is active */
87   if (surf_workstation_model->extension.
88       workstation.get_state(host) != SURF_RESOURCE_ON) {
89     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
90            sg_host_name(host));
91   }
92
93   action = xbt_mallocator_get(simix_global->action_mallocator);
94   action->type = SIMIX_ACTION_IO;
95   action->name = NULL;
96 #ifdef HAVE_TRACING
97   action->category = NULL;
98 #endif
99
100   action->io.host = host;
101   action->io.surf_io =
102       surf_workstation_model->extension.workstation.read(host, fd->surf_file, size);
103
104   surf_workstation_model->action_data_set(action->io.surf_io, action);
105   XBT_DEBUG("Create io action %p", action);
106
107   return action;
108 }
109
110 //SIMIX FILE WRITE
111 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
112 {
113   smx_action_t action = SIMIX_file_write(simcall->issuer, fd,  size);
114   xbt_fifo_push(action->simcalls, simcall);
115   simcall->issuer->waiting_action = action;
116 }
117
118 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
119 {
120   smx_action_t action;
121   smx_host_t host = process->smx_host;
122
123   /* check if the host is active */
124   if (surf_workstation_model->extension.
125       workstation.get_state(host) != SURF_RESOURCE_ON) {
126     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
127            sg_host_name(host));
128   }
129
130   action = xbt_mallocator_get(simix_global->action_mallocator);
131   action->type = SIMIX_ACTION_IO;
132   action->name = NULL;
133 #ifdef HAVE_TRACING
134   action->category = NULL;
135 #endif
136
137   action->io.host = host;
138   action->io.surf_io =
139       surf_workstation_model->extension.workstation.write(host, fd->surf_file, size);
140
141   surf_workstation_model->action_data_set(action->io.surf_io, action);
142   XBT_DEBUG("Create io action %p", action);
143
144   return action;
145 }
146
147 //SIMIX FILE OPEN
148 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
149                          const char* path)
150 {
151   smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
152   xbt_fifo_push(action->simcalls, simcall);
153   simcall->issuer->waiting_action = action;
154 }
155
156 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
157                              const char* path)
158 {
159   smx_action_t action;
160   smx_host_t host = process->smx_host;
161
162   /* check if the host is active */
163   if (surf_workstation_model->extension.
164       workstation.get_state(host) != SURF_RESOURCE_ON) {
165     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
166            sg_host_name(host));
167   }
168
169   action = xbt_mallocator_get(simix_global->action_mallocator);
170   action->type = SIMIX_ACTION_IO;
171   action->name = NULL;
172 #ifdef HAVE_TRACING
173   action->category = NULL;
174 #endif
175
176   action->io.host = host;
177   action->io.surf_io =
178       surf_workstation_model->extension.workstation.open(host, mount, path);
179
180   surf_workstation_model->action_data_set(action->io.surf_io, action);
181   XBT_DEBUG("Create io action %p", action);
182
183   return action;
184 }
185
186 //SIMIX FILE CLOSE
187 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
188 {
189   smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
190   xbt_fifo_push(action->simcalls, simcall);
191   simcall->issuer->waiting_action = action;
192 }
193
194 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
195 {
196   smx_action_t action;
197   smx_host_t host = process->smx_host;
198
199   /* check if the host is active */
200   if (surf_workstation_model->extension.
201       workstation.get_state(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   action = xbt_mallocator_get(simix_global->action_mallocator);
207   action->type = SIMIX_ACTION_IO;
208   action->name = NULL;
209 #ifdef HAVE_TRACING
210   action->category = NULL;
211 #endif
212
213   action->io.host = host;
214   action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
215
216   surf_workstation_model->action_data_set(action->io.surf_io, action);
217   XBT_DEBUG("Create io action %p", action);
218
219   return action;
220 }
221
222
223 //SIMIX FILE UNLINK
224 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
225 {
226   return SIMIX_file_unlink(simcall->issuer, fd);
227 }
228
229 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
230 {
231   smx_host_t host = process->smx_host;
232   /* check if the host is active */
233   if (surf_workstation_model->extension.
234       workstation.get_state(host) != SURF_RESOURCE_ON) {
235     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
236            sg_host_name(host));
237   }
238
239   if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
240     xbt_free(fd);
241     return 1;
242   } else
243     return 0;
244 }
245
246 //SIMIX FILE LS
247 void SIMIX_pre_file_ls(smx_simcall_t simcall,
248                        const char* mount, const char* path)
249 {
250   smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
251   xbt_fifo_push(action->simcalls, simcall);
252   simcall->issuer->waiting_action = action;
253 }
254 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
255 {
256   smx_action_t action;
257   smx_host_t host = process->smx_host;
258   /* check if the host is active */
259   if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
260     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
261            sg_host_name(host));
262   }
263
264   action = xbt_mallocator_get(simix_global->action_mallocator);
265   action->type = SIMIX_ACTION_IO;
266   action->name = NULL;
267 #ifdef HAVE_TRACING
268   action->category = NULL;
269 #endif
270
271   action->io.host = host;
272   action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
273
274   surf_workstation_model->action_data_set(action->io.surf_io, action);
275   XBT_DEBUG("Create io action %p", action);
276   return action;
277 }
278
279 sg_storage_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
280 {
281   return SIMIX_file_get_size(simcall->issuer, fd);
282 }
283
284 sg_storage_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
285 {
286   smx_host_t host = process->smx_host;
287   return  surf_workstation_model->extension.workstation.get_size(host,
288       fd->surf_file);
289 }
290
291 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
292 {
293   return SIMIX_file_get_info(simcall->issuer, fd);
294 }
295
296 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
297 {
298   smx_host_t host = process->smx_host;
299   return  surf_workstation_model->extension.workstation.get_info(host,
300       fd->surf_file);
301 }
302
303 sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
304 {
305   return SIMIX_storage_get_free_size(simcall->issuer, name);
306 }
307
308 sg_storage_size_t SIMIX_storage_get_free_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_free_size(host,name);
312 }
313
314 sg_storage_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
315 {
316   return SIMIX_storage_get_used_size(simcall->issuer, name);
317 }
318
319 sg_storage_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
320 {
321   smx_host_t host = process->smx_host;
322   return  surf_workstation_model->extension.workstation.get_used_size(host,name);
323 }
324
325 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
326   return SIMIX_storage_get_properties(storage);
327 }
328 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
329   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
330   return surf_storage_model->extension.storage.get_properties(storage);
331 }
332
333 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
334    return SIMIX_storage_get_name(storage);
335 }
336
337 const char* SIMIX_storage_get_name(smx_storage_t storage){
338   xbt_assert((storage != NULL), "Invalid parameters");
339   return sg_storage_name(storage);
340 }
341
342 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
343   SIMIX_storage_set_data(storage, data);
344 }
345 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
346   xbt_assert((storage != NULL), "Invalid parameters");
347   xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
348
349   SIMIX_storage_priv(storage)->data = data;
350 }
351
352 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
353   return SIMIX_storage_get_data(storage);
354 }
355
356 void* SIMIX_storage_get_data(smx_storage_t storage){
357   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
358
359   return SIMIX_storage_priv(storage)->data;
360 }
361
362 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
363   return SIMIX_storage_get_content(storage);
364 }
365
366 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
367   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
368   return surf_storage_model->extension.storage.get_content(storage);
369 }
370
371 sg_storage_size_t SIMIX_storage_get_size(smx_storage_t storage){
372   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
373   return surf_storage_model->extension.storage.get_size(storage);
374 }
375
376 void SIMIX_post_io(smx_action_t action)
377 {
378   xbt_fifo_item_t i;
379   smx_simcall_t simcall;
380 //  char* key;
381 //  xbt_dict_cursor_t cursor = NULL;
382 //  s_file_stat_t *dst = NULL;
383 //  s_file_stat_t *src = NULL;
384
385   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
386     switch (simcall->call) {
387     case SIMCALL_FILE_OPEN:;
388       smx_file_t tmp = xbt_new(s_smx_file_t,1);
389       tmp->surf_file = (action->io.surf_io)->file;
390       simcall_file_open__set__result(simcall, tmp);
391       break;
392
393     case SIMCALL_FILE_CLOSE:
394       xbt_free(simcall_file_close__get__fd(simcall));
395       simcall_file_close__set__result(simcall, 0);
396       break;
397
398     case SIMCALL_FILE_WRITE:
399       simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
400       break;
401
402     case SIMCALL_FILE_READ:
403       simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
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, (action->io.surf_io)->ls_dict);
416       break;
417     default:
418       break;
419     }
420   }
421
422   switch (surf_workstation_model->action_state_get(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     action->io.surf_io->model_obj->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_workstation_model->extension.
475         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
476       simcall->issuer->context->iwannadie = 1;
477     }
478
479     simcall->issuer->waiting_action = NULL;
480     SIMIX_simcall_answer(simcall);
481   }
482
483   /* We no longer need it */
484   SIMIX_io_destroy(action);
485 }