Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
unlink can be a remote operation too.
[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, smx_host_t host)
54 {
55   smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size, host);
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, smx_host_t host)
61 {
62   smx_action_t action;
63
64   /* check if the host is active */
65   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
66     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
67            sg_host_name(host));
68   }
69
70   action = xbt_mallocator_get(simix_global->action_mallocator);
71   action->type = SIMIX_ACTION_IO;
72   action->name = NULL;
73 #ifdef HAVE_TRACING
74   action->category = NULL;
75 #endif
76
77   action->io.host = host;
78   action->io.surf_io = surf_workstation_read(host, fd->surf_file, size);
79
80   surf_action_set_data(action->io.surf_io, action);
81   XBT_DEBUG("Create io action %p", action);
82
83   return action;
84 }
85
86 //SIMIX FILE WRITE
87 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, smx_host_t host)
88 {
89   smx_action_t action = SIMIX_file_write(simcall->issuer, fd,  size, host);
90   xbt_fifo_push(action->simcalls, simcall);
91   simcall->issuer->waiting_action = action;
92 }
93
94 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_size_t size, smx_host_t host)
95 {
96   smx_action_t action;
97
98   /* check if the host is active */
99   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
100     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
101            sg_host_name(host));
102   }
103
104   action = xbt_mallocator_get(simix_global->action_mallocator);
105   action->type = SIMIX_ACTION_IO;
106   action->name = NULL;
107 #ifdef HAVE_TRACING
108   action->category = NULL;
109 #endif
110
111   action->io.host = host;
112   action->io.surf_io = surf_workstation_write(host, fd->surf_file, size);
113
114   surf_action_set_data(action->io.surf_io, action);
115   XBT_DEBUG("Create io action %p", action);
116
117   return action;
118 }
119
120 //SIMIX FILE OPEN
121 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* fullpath, smx_host_t host)
122 {
123   smx_action_t action = SIMIX_file_open(simcall->issuer, fullpath, host);
124   xbt_fifo_push(action->simcalls, simcall);
125   simcall->issuer->waiting_action = action;
126 }
127
128 smx_action_t SIMIX_file_open(smx_process_t process, const char* fullpath, smx_host_t host)
129 {
130   smx_action_t action;
131
132   /* check if the host is active */
133   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
134     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
135            sg_host_name(host));
136   }
137
138   action = xbt_mallocator_get(simix_global->action_mallocator);
139   action->type = SIMIX_ACTION_IO;
140   action->name = NULL;
141 #ifdef HAVE_TRACING
142   action->category = NULL;
143 #endif
144
145   action->io.host = host;
146   action->io.surf_io = surf_workstation_open(host, fullpath);
147
148   surf_action_set_data(action->io.surf_io, action);
149   XBT_DEBUG("Create io action %p", action);
150
151   return action;
152 }
153
154 //SIMIX FILE CLOSE
155 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd, smx_host_t host)
156 {
157   smx_action_t action = SIMIX_file_close(simcall->issuer, fd, host);
158   xbt_fifo_push(action->simcalls, simcall);
159   simcall->issuer->waiting_action = action;
160 }
161
162 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd, smx_host_t host)
163 {
164   smx_action_t action;
165
166   /* check if the host is active */
167   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
168     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
169            sg_host_name(host));
170   }
171
172   action = xbt_mallocator_get(simix_global->action_mallocator);
173   action->type = SIMIX_ACTION_IO;
174   action->name = NULL;
175 #ifdef HAVE_TRACING
176   action->category = NULL;
177 #endif
178
179   action->io.host = host;
180   action->io.surf_io = surf_workstation_close(host, fd->surf_file);
181
182   surf_action_set_data(action->io.surf_io, action);
183   XBT_DEBUG("Create io action %p", action);
184
185   return action;
186 }
187
188
189 //SIMIX FILE UNLINK
190 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd, smx_host_t host)
191 {
192   return SIMIX_file_unlink(simcall->issuer, fd, host);
193 }
194
195 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd, smx_host_t host)
196 {
197   /* check if the host is active */
198   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
199     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
200            sg_host_name(host));
201   }
202
203   int res = surf_workstation_unlink(host, fd->surf_file);
204   xbt_free(fd);
205   return !!res;
206 }
207
208 sg_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
209 {
210   return SIMIX_file_get_size(simcall->issuer, fd);
211 }
212
213 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
214 {
215   smx_host_t host = process->smx_host;
216   return  surf_workstation_get_size(host, fd->surf_file);
217 }
218
219 sg_size_t SIMIX_pre_file_tell(smx_simcall_t simcall, smx_file_t fd)
220 {
221   return SIMIX_file_tell(simcall->issuer, fd);
222 }
223
224 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
225 {
226   smx_host_t host = process->smx_host;
227   return  surf_workstation_file_tell(host, fd->surf_file);
228 }
229
230
231 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
232 {
233   return SIMIX_file_get_info(simcall->issuer, fd);
234 }
235
236 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
237 {
238   smx_host_t host = process->smx_host;
239   return  surf_workstation_get_info(host, fd->surf_file);
240 }
241
242 int SIMIX_pre_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
243 {
244   return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
245 }
246
247 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_offset_t offset, int origin)
248 {
249   smx_host_t host = process->smx_host;
250   return  surf_workstation_file_seek(host, fd->surf_file, offset, origin);
251 }
252
253 int SIMIX_pre_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
254 {
255   return SIMIX_file_move(simcall->issuer, file, fullpath);
256 }
257
258 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
259 {
260   smx_host_t host = process->smx_host;
261   return  surf_workstation_file_move(host, file->surf_file, fullpath);
262 }
263
264 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
265   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
266   return surf_storage_get_size(storage);
267 }
268
269 sg_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
270 {
271   return SIMIX_storage_get_free_size(simcall->issuer, storage);
272 }
273
274 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
275 {
276   return  surf_storage_get_free_size(storage);
277 }
278
279 sg_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
280 {
281   return SIMIX_storage_get_used_size(simcall->issuer, storage);
282 }
283
284 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
285 {
286   return  surf_storage_get_used_size(storage);
287 }
288
289 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
290   return SIMIX_storage_get_properties(storage);
291 }
292 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
293   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
294   return surf_resource_get_properties(surf_storage_resource_priv(storage));
295 }
296
297 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
298    return SIMIX_storage_get_name(storage);
299 }
300
301 const char* SIMIX_storage_get_name(smx_storage_t storage){
302   xbt_assert((storage != NULL), "Invalid parameters");
303   return sg_storage_name(storage);
304 }
305
306 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
307   return SIMIX_storage_get_content(storage);
308 }
309
310 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
311   xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
312   return surf_storage_get_content(storage);
313 }
314
315 const char* SIMIX_pre_storage_get_host(smx_simcall_t simcall, smx_storage_t storage){
316    return SIMIX_storage_get_host(storage);
317 }
318
319 const char* SIMIX_storage_get_host(smx_storage_t storage){
320   xbt_assert((storage != NULL), "Invalid parameters");
321   return surf_storage_get_host(storage);
322 }
323
324 void SIMIX_post_io(smx_action_t action)
325 {
326   xbt_fifo_item_t i;
327   smx_simcall_t simcall;
328 //  char* key;
329 //  xbt_dict_cursor_t cursor = NULL;
330 //  s_file_stat_t *dst = NULL;
331 //  s_file_stat_t *src = NULL;
332
333   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
334     switch (simcall->call) {
335     case SIMCALL_FILE_OPEN: {
336       smx_file_t tmp = xbt_new(s_smx_file_t,1);
337       tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
338       simcall_file_open__set__result(simcall, tmp);
339       break;
340     }
341     case SIMCALL_FILE_CLOSE:
342       xbt_free(simcall_file_close__get__fd(simcall));
343       simcall_file_close__set__result(simcall, 0);
344       break;
345     case SIMCALL_FILE_WRITE:
346       simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
347       break;
348
349     case SIMCALL_FILE_READ:
350       simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
351       break;
352
353     default:
354       break;
355     }
356   }
357
358   switch (surf_action_get_state(action->io.surf_io)) {
359
360     case SURF_ACTION_FAILED:
361       action->state = SIMIX_FAILED;
362       break;
363
364     case SURF_ACTION_DONE:
365       action->state = SIMIX_DONE;
366       break;
367
368     default:
369       THROW_IMPOSSIBLE;
370       break;
371   }
372
373   SIMIX_io_finish(action);
374 }
375
376 void SIMIX_io_destroy(smx_action_t action)
377 {
378   XBT_DEBUG("Destroy action %p", action);
379   if (action->io.surf_io)
380     surf_action_unref(action->io.surf_io);
381   xbt_mallocator_release(simix_global->action_mallocator, action);
382 }
383
384 void SIMIX_io_finish(smx_action_t action)
385 {
386   xbt_fifo_item_t item;
387   smx_simcall_t simcall;
388
389   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
390
391     switch (action->state) {
392
393       case SIMIX_DONE:
394         /* do nothing, action done */
395         break;
396
397       case SIMIX_FAILED:
398         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
399         break;
400
401       case SIMIX_CANCELED:
402         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
403         break;
404
405       default:
406         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
407             (int)action->state);
408     }
409
410     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
411       simcall->issuer->context->iwannadie = 1;
412     }
413
414     simcall->issuer->waiting_action = NULL;
415     SIMIX_simcall_answer(simcall);
416   }
417
418   /* We no longer need it */
419   SIMIX_io_destroy(action);
420 }