Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix unset variables issues
[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 //SIMIX FILE READ
19 void SIMIX_pre_file_read(smx_simcall_t simcall, void *ptr, size_t size,
20                         smx_file_t fd)
21 {
22   smx_action_t action = SIMIX_file_read(simcall->issuer, ptr, size, fd);
23   xbt_fifo_push(action->simcalls, simcall);
24   simcall->issuer->waiting_action = action;
25 }
26
27 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size,
28                              smx_file_t fd)
29 {
30   smx_action_t action;
31   smx_host_t host = process->smx_host;
32
33   /* check if the host is active */
34   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
35     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
36            sg_host_name(host));
37   }
38
39   action = xbt_mallocator_get(simix_global->action_mallocator);
40   action->type = SIMIX_ACTION_IO;
41   action->name = NULL;
42 #ifdef HAVE_TRACING
43   action->category = NULL;
44 #endif
45
46   action->io.host = host;
47   action->io.surf_io = surf_workstation_read(host, ptr, size, fd->surf_file);
48
49   surf_action_set_data(action->io.surf_io, action);
50   XBT_DEBUG("Create io action %p", action);
51
52   return action;
53 }
54
55 //SIMIX FILE WRITE
56 void SIMIX_pre_file_write(smx_simcall_t simcall, const void *ptr, size_t size,
57                           smx_file_t fd)
58 {
59   smx_action_t action = SIMIX_file_write(simcall->issuer, ptr, size, fd);
60   xbt_fifo_push(action->simcalls, simcall);
61   simcall->issuer->waiting_action = action;
62 }
63
64 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr,
65                               size_t size, smx_file_t fd)
66 {
67   smx_action_t action;
68   smx_host_t host = process->smx_host;
69
70   /* check if the host is active */
71   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
72     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
73            sg_host_name(host));
74   }
75
76   action = xbt_mallocator_get(simix_global->action_mallocator);
77   action->type = SIMIX_ACTION_IO;
78   action->name = NULL;
79 #ifdef HAVE_TRACING
80   action->category = NULL;
81 #endif
82
83   action->io.host = host;
84   action->io.surf_io = surf_workstation_write(host, ptr, size, fd->surf_file);
85
86   surf_action_set_data(action->io.surf_io, action);
87   XBT_DEBUG("Create io action %p", action);
88
89   return action;
90 }
91
92 //SIMIX FILE OPEN
93 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
94                          const char* path)
95 {
96   smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
97   xbt_fifo_push(action->simcalls, simcall);
98   simcall->issuer->waiting_action = action;
99 }
100
101 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
102                              const char* path)
103 {
104   smx_action_t action;
105   smx_host_t host = process->smx_host;
106
107   /* check if the host is active */
108   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
109     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
110            sg_host_name(host));
111   }
112
113   action = xbt_mallocator_get(simix_global->action_mallocator);
114   action->type = SIMIX_ACTION_IO;
115   action->name = NULL;
116 #ifdef HAVE_TRACING
117   action->category = NULL;
118 #endif
119
120   action->io.host = host;
121   action->io.surf_io = surf_workstation_open(host, mount, path);
122
123   surf_action_set_data(action->io.surf_io, action);
124   XBT_DEBUG("Create io action %p", action);
125
126   return action;
127 }
128
129 //SIMIX FILE CLOSE
130 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
131 {
132   smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
133   xbt_fifo_push(action->simcalls, simcall);
134   simcall->issuer->waiting_action = action;
135 }
136
137 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
138 {
139   smx_action_t action;
140   smx_host_t host = process->smx_host;
141
142   /* check if the host is active */
143   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
144     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
145            sg_host_name(host));
146   }
147
148   action = xbt_mallocator_get(simix_global->action_mallocator);
149   action->type = SIMIX_ACTION_IO;
150   action->name = NULL;
151 #ifdef HAVE_TRACING
152   action->category = NULL;
153 #endif
154
155   action->io.host = host;
156   action->io.surf_io = surf_workstation_close(host, fd->surf_file);
157
158   surf_action_set_data(action->io.surf_io, action);
159   XBT_DEBUG("Create io action %p", action);
160
161   return action;
162 }
163
164
165 //SIMIX FILE UNLINK
166 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
167 {
168   return SIMIX_file_unlink(simcall->issuer, fd);
169 }
170
171 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
172 {
173   smx_host_t host = process->smx_host;
174   /* check if the host is active */
175   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
176     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
177            sg_host_name(host));
178   }
179
180   if (surf_workstation_unlink(host, fd->surf_file)){
181     fd->surf_file = NULL;
182     return 1;
183   } else
184     return 0;
185 }
186
187 //SIMIX FILE LS
188 void SIMIX_pre_file_ls(smx_simcall_t simcall,
189                        const char* mount, const char* path)
190 {
191   smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
192   xbt_fifo_push(action->simcalls, simcall);
193   simcall->issuer->waiting_action = action;
194 }
195 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
196 {
197   smx_action_t action;
198   smx_host_t host = process->smx_host;
199   /* check if the host is active */
200   if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
201     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
202            sg_host_name(host));
203   }
204
205   action = xbt_mallocator_get(simix_global->action_mallocator);
206   action->type = SIMIX_ACTION_IO;
207   action->name = NULL;
208 #ifdef HAVE_TRACING
209   action->category = NULL;
210 #endif
211
212   action->io.host = host;
213   action->io.surf_io = surf_workstation_ls(host,mount,path);
214
215   surf_action_set_data(action->io.surf_io, action);
216   XBT_DEBUG("Create io action %p", action);
217   return action;
218 }
219
220 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
221 {
222   return SIMIX_file_get_size(simcall->issuer, fd);
223 }
224
225 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
226 {
227   smx_host_t host = process->smx_host;
228   return  surf_workstation_get_size(host, fd->surf_file);
229 }
230
231
232 void SIMIX_post_io(smx_action_t action)
233 {
234   xbt_fifo_item_t i;
235   smx_simcall_t simcall;
236 //  char* key;
237 //  xbt_dict_cursor_t cursor = NULL;
238 //  s_file_stat_t *dst = NULL;
239 //  s_file_stat_t *src = NULL;
240
241   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
242     switch (simcall->call) {
243     case SIMCALL_FILE_OPEN:;
244       smx_file_t tmp = xbt_new(s_smx_file_t,1);
245       tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
246       simcall_file_open__set__result(simcall, tmp);
247       break;
248
249     case SIMCALL_FILE_CLOSE:
250       xbt_free(simcall_file_close__get__fd(simcall));
251       simcall_file_close__set__result(simcall, 0);
252       break;
253     case SIMCALL_FILE_WRITE:
254       simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
255       break;
256
257     case SIMCALL_FILE_READ:
258       simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
259       break;
260
261     case SIMCALL_FILE_LS:
262 //      xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
263 //        // if there is a stat we have to duplicate it
264 //        if(src){
265 //          dst = xbt_new0(s_file_stat_t,1);
266 //          file_stat_copy(src, dst);
267 //          xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
268 //        }
269 //      }
270       simcall_file_ls__set__result(simcall, surf_storage_action_get_ls_dict(action->io.surf_io));
271       break;
272     default:
273       break;
274     }
275   }
276
277   switch (surf_action_get_state(action->io.surf_io)) {
278
279     case SURF_ACTION_FAILED:
280       action->state = SIMIX_FAILED;
281       break;
282
283     case SURF_ACTION_DONE:
284       action->state = SIMIX_DONE;
285       break;
286
287     default:
288       THROW_IMPOSSIBLE;
289       break;
290   }
291
292   SIMIX_io_finish(action);
293 }
294
295 void SIMIX_io_destroy(smx_action_t action)
296 {
297   XBT_DEBUG("Destroy action %p", action);
298   if (action->io.surf_io)
299     surf_action_unref(action->io.surf_io);
300   xbt_mallocator_release(simix_global->action_mallocator, action);
301 }
302
303 void SIMIX_io_finish(smx_action_t action)
304 {
305   xbt_fifo_item_t item;
306   smx_simcall_t simcall;
307
308   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
309
310     switch (action->state) {
311
312       case SIMIX_DONE:
313         /* do nothing, action done */
314         break;
315
316       case SIMIX_FAILED:
317         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
318         break;
319
320       case SIMIX_CANCELED:
321         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
322         break;
323
324       default:
325         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
326             (int)action->state);
327     }
328
329     if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
330       simcall->issuer->context->iwannadie = 1;
331     }
332
333     simcall->issuer->waiting_action = NULL;
334     SIMIX_simcall_answer(simcall);
335   }
336
337   /* We no longer need it */
338   SIMIX_io_destroy(action);
339 }