Logo AND Algorithmique Numérique Distribuée

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