Logo AND Algorithmique Numérique Distribuée

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