Logo AND Algorithmique Numérique Distribuée

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