Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6c7868c6146ce481c1bdc96a89733215cef88122
[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 "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14                                 "Logging specific to SIMIX (io)");
15
16
17 //SIMIX FILE READ
18 void SIMIX_pre_file_read(smx_simcall_t simcall)
19 {
20   smx_action_t action = SIMIX_file_read(simcall->issuer,
21       simcall->file_read.storage,
22       simcall->file_read.ptr,
23       simcall->file_read.size,
24       simcall->file_read.nmemb,
25       simcall->file_read.stream);
26   xbt_fifo_push(action->simcalls, simcall);
27   simcall->issuer->waiting_action = action;
28 }
29
30 smx_action_t SIMIX_file_read(smx_process_t process, const char* storage, void* ptr, size_t size, size_t nmemb, smx_file_t stream)
31 {
32   smx_action_t action;
33   smx_host_t host = process->smx_host;
34
35   /* check if the host is active */
36   if (surf_workstation_model->extension.
37       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
38     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
39            host->name);
40   }
41
42   action = xbt_mallocator_get(simix_global->action_mallocator);
43   action->type = SIMIX_ACTION_IO;
44   action->name = NULL;
45 #ifdef HAVE_TRACING
46   action->category = NULL;
47 #endif
48
49   action->io.host = host;
50   action->io.surf_io = surf_workstation_model->extension.workstation.read(host->host, storage, ptr, size, nmemb, stream->surf_file),
51
52   surf_workstation_model->action_data_set(action->io.surf_io, action);
53   XBT_DEBUG("Create io action %p", action);
54
55   return action;
56 }
57
58 //SIMIX FILE WRITE
59 void SIMIX_pre_file_write(smx_simcall_t simcall)
60 {
61   smx_action_t action = SIMIX_file_write(simcall->issuer,
62       simcall->file_write.storage,
63       simcall->file_write.ptr,
64       simcall->file_write.size,
65       simcall->file_write.nmemb,
66       simcall->file_write.stream);
67   xbt_fifo_push(action->simcalls, simcall);
68   simcall->issuer->waiting_action = action;
69 }
70
71 smx_action_t SIMIX_file_write(smx_process_t process, const char* storage, const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
72 {
73   smx_action_t action;
74   smx_host_t host = process->smx_host;
75
76   /* check if the host is active */
77   if (surf_workstation_model->extension.
78       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
79     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
80            host->name);
81   }
82
83   action = xbt_mallocator_get(simix_global->action_mallocator);
84   action->type = SIMIX_ACTION_IO;
85   action->name = NULL;
86 #ifdef HAVE_TRACING
87   action->category = NULL;
88 #endif
89
90   action->io.host = host;
91   action->io.surf_io = surf_workstation_model->extension.workstation.write(host->host, storage, ptr, size, nmemb, stream->surf_file);
92
93   surf_workstation_model->action_data_set(action->io.surf_io, action);
94   XBT_DEBUG("Create io action %p", action);
95
96   return action;
97 }
98
99 //SIMIX FILE OPEN
100 void SIMIX_pre_file_open(smx_simcall_t simcall)
101 {
102   smx_action_t action = SIMIX_file_open(simcall->issuer,
103       simcall->file_open.storage,
104       simcall->file_open.path,
105       simcall->file_open.mode);
106   xbt_fifo_push(action->simcalls, simcall);
107   simcall->issuer->waiting_action = action;
108 }
109
110 smx_action_t SIMIX_file_open(smx_process_t process ,const char* storage, const char* path, const char* mode)
111 {
112   smx_action_t action;
113   smx_host_t host = process->smx_host;
114
115   /* check if the host is active */
116   if (surf_workstation_model->extension.
117       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
118     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
119            host->name);
120   }
121
122   action = xbt_mallocator_get(simix_global->action_mallocator);
123   action->type = SIMIX_ACTION_IO;
124   action->name = NULL;
125 #ifdef HAVE_TRACING
126   action->category = NULL;
127 #endif
128
129   action->io.host = host;
130   action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, storage, path, mode);
131
132   surf_workstation_model->action_data_set(action->io.surf_io, action);
133   XBT_DEBUG("Create io action %p", action);
134
135   return action;
136 }
137
138 //SIMIX FILE CLOSE
139 void SIMIX_pre_file_close(smx_simcall_t simcall)
140 {
141   smx_action_t action = SIMIX_file_close(simcall->issuer,
142       simcall->file_close.storage,
143       simcall->file_close.fp);
144   xbt_fifo_push(action->simcalls, simcall);
145   simcall->issuer->waiting_action = action;
146 }
147
148 smx_action_t SIMIX_file_close(smx_process_t process ,const char* storage, smx_file_t fp)
149 {
150   smx_action_t action;
151   smx_host_t host = process->smx_host;
152
153   /* check if the host is active */
154   if (surf_workstation_model->extension.
155       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
156     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
157            host->name);
158   }
159
160   action = xbt_mallocator_get(simix_global->action_mallocator);
161   action->type = SIMIX_ACTION_IO;
162   action->name = NULL;
163 #ifdef HAVE_TRACING
164   action->category = NULL;
165 #endif
166
167   action->io.host = host;
168   action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, storage, fp->surf_file);
169
170   surf_workstation_model->action_data_set(action->io.surf_io, action);
171   XBT_DEBUG("Create io action %p", action);
172
173   return action;
174 }
175
176 //SIMIX FILE STAT
177 void SIMIX_pre_file_stat(smx_simcall_t simcall)
178 {
179   smx_action_t action = SIMIX_file_stat(simcall->issuer,
180       simcall->file_stat.storage,
181       simcall->file_stat.fd,
182       simcall->file_stat.buf);
183   xbt_fifo_push(action->simcalls, simcall);
184   simcall->issuer->waiting_action = action;
185 }
186
187 smx_action_t SIMIX_file_stat(smx_process_t process ,const char* storage, smx_file_t fd, s_file_stat_t buf)
188 {
189   smx_action_t action;
190   smx_host_t host = process->smx_host;
191   /* check if the host is active */
192   if (surf_workstation_model->extension.
193       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
194     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
195            host->name);
196   }
197
198   action = xbt_mallocator_get(simix_global->action_mallocator);
199   action->type = SIMIX_ACTION_IO;
200   action->name = NULL;
201 #ifdef HAVE_TRACING
202   action->category = NULL;
203 #endif
204
205   action->io.host = host;
206   action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, storage, fd->surf_file);
207
208   surf_workstation_model->action_data_set(action->io.surf_io, action);
209   XBT_DEBUG("Create io action %p", action);
210
211   return action;
212 }
213
214 void SIMIX_post_io(smx_action_t action)
215 {
216   xbt_fifo_item_t i;
217   smx_simcall_t simcall;
218
219   xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
220     switch (simcall->call) {
221     case SIMCALL_FILE_OPEN:
222       simcall->file_open.result = xbt_new(s_smx_file_t,1);
223       simcall->file_open.result->surf_file = (action->io.surf_io)->file;
224       break;
225     case SIMCALL_FILE_CLOSE:
226       simcall->file_read.result = 0;
227       break;
228     case SIMCALL_FILE_WRITE:
229       simcall->file_write.result = (action->io.surf_io)->cost;
230       break;
231     case SIMCALL_FILE_READ:
232       simcall->file_read.result = (action->io.surf_io)->cost;
233       break;
234     case SIMCALL_FILE_STAT:
235       simcall->file_stat.result = 0;
236       s_file_stat_t *dst = &(simcall->file_stat.buf);
237       s_file_stat_t *src = &((action->io.surf_io)->stat);
238       file_stat_copy(src,dst);
239       break;
240     default:
241       break;
242     }
243   }
244
245   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
246
247     case SURF_ACTION_FAILED:
248       action->state = SIMIX_FAILED;
249       break;
250
251     case SURF_ACTION_DONE:
252       action->state = SIMIX_DONE;
253       break;
254
255     default:
256       THROW_IMPOSSIBLE;
257       break;
258   }
259
260   SIMIX_io_finish(action);
261 }
262
263 void SIMIX_io_destroy(smx_action_t action)
264 {
265   XBT_DEBUG("Destroy action %p", action);
266   if (action->io.surf_io)
267     action->io.surf_io->model_type->action_unref(action->io.surf_io);
268   xbt_mallocator_release(simix_global->action_mallocator, action);
269 }
270
271 void SIMIX_io_finish(smx_action_t action)
272 {
273   xbt_fifo_item_t item;
274   smx_simcall_t simcall;
275
276   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
277
278     switch (action->state) {
279
280       case SIMIX_DONE:
281         /* do nothing, action done */
282         break;
283
284       case SIMIX_FAILED:
285         SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
286         break;
287
288       case SIMIX_CANCELED:
289         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
290         break;
291
292       default:
293         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
294             (int)action->state);
295     }
296     simcall->issuer->waiting_action = NULL;
297     SIMIX_simcall_answer(simcall);
298   }
299
300   /* We no longer need it */
301   SIMIX_io_destroy(action);
302 }