Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1c16b3b73fb978cd0fdaeadd050efdacb9ebaa38
[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_host_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_storage_get_name(smx_storage_t storage){
323   xbt_assert((storage != NULL), "Invalid parameters");
324
325   return sg_storage_name(storage);
326 }
327
328 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
329   SIMIX_storage_set_data(storage, data);
330 }
331 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
332   xbt_assert((storage != NULL), "Invalid parameters");
333   xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
334
335   SIMIX_storage_priv(storage)->data = data;
336 }
337
338 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
339   return SIMIX_storage_get_data(storage);
340 }
341
342 void* SIMIX_storage_get_data(smx_storage_t storage){
343   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
344
345   return SIMIX_storage_priv(storage)->data;
346 }
347
348 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
349   return SIMIX_storage_get_content(storage);
350 }
351
352 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
353   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
354   return surf_storage_model->extension.storage.get_content(storage);
355 }
356
357 void SIMIX_post_io(smx_action_t action)
358 {
359   xbt_fifo_item_t i;
360   smx_simcall_t simcall;
361 //  char* key;
362 //  xbt_dict_cursor_t cursor = NULL;
363 //  s_file_stat_t *dst = NULL;
364 //  s_file_stat_t *src = NULL;
365
366   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
367     switch (simcall->call) {
368     case SIMCALL_FILE_OPEN:;
369       smx_file_t tmp = xbt_new(s_smx_file_t,1);
370       tmp->surf_file = (action->io.surf_io)->file;
371       simcall_file_open__set__result(simcall, tmp);
372       break;
373
374     case SIMCALL_FILE_CLOSE:
375       xbt_free(simcall_file_close__get__fd(simcall));
376       simcall_file_close__set__result(simcall, 0);
377       break;
378
379     case SIMCALL_FILE_WRITE:
380       simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
381       break;
382
383     case SIMCALL_FILE_READ:
384       simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
385       break;
386
387     case SIMCALL_FILE_LS:
388 //      xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
389 //        // if there is a stat we have to duplicate it
390 //        if(src){
391 //          dst = xbt_new0(s_file_stat_t,1);
392 //          file_stat_copy(src, dst);
393 //          xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
394 //        }
395 //      }
396       simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
397       break;
398     default:
399       break;
400     }
401   }
402
403   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
404
405     case SURF_ACTION_FAILED:
406       action->state = SIMIX_FAILED;
407       break;
408
409     case SURF_ACTION_DONE:
410       action->state = SIMIX_DONE;
411       break;
412
413     default:
414       THROW_IMPOSSIBLE;
415       break;
416   }
417
418   SIMIX_io_finish(action);
419 }
420
421 void SIMIX_io_destroy(smx_action_t action)
422 {
423   XBT_DEBUG("Destroy action %p", action);
424   if (action->io.surf_io)
425     action->io.surf_io->model_type->action_unref(action->io.surf_io);
426   xbt_mallocator_release(simix_global->action_mallocator, action);
427 }
428
429 void SIMIX_io_finish(smx_action_t action)
430 {
431   xbt_fifo_item_t item;
432   smx_simcall_t simcall;
433
434   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
435
436     switch (action->state) {
437
438       case SIMIX_DONE:
439         /* do nothing, action done */
440         break;
441
442       case SIMIX_FAILED:
443         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
444         break;
445
446       case SIMIX_CANCELED:
447         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
448         break;
449
450       default:
451         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
452             (int)action->state);
453     }
454
455     if (surf_workstation_model->extension.
456         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
457       simcall->issuer->context->iwannadie = 1;
458     }
459
460     simcall->issuer->waiting_action = NULL;
461     SIMIX_simcall_answer(simcall);
462   }
463
464   /* We no longer need it */
465   SIMIX_io_destroy(action);
466 }