Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use m_file_t instead of m_file_t*
[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.ptr,
22       simcall->file_read.size,
23       simcall->file_read.nmemb,
24       simcall->file_read.stream);
25   xbt_fifo_push(action->simcalls, simcall);
26   simcall->issuer->waiting_action = action;
27 }
28
29 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream)
30 {
31   smx_action_t action;
32   smx_host_t host = process->smx_host;
33
34   /* check if the host is active */
35   if (surf_workstation_model->extension.
36       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
37     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
38            host->name);
39   }
40
41   action = xbt_mallocator_get(simix_global->action_mallocator);
42   action->type = SIMIX_ACTION_IO;
43   action->name = NULL;
44 #ifdef HAVE_TRACING
45   action->category = NULL;
46 #endif
47
48   action->io.host = host;
49   //  TODO in surf model disk???
50   //  action->io.surf_io = surf_workstation_model->extension.storage.read(host->host, name),
51   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 1.0);
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.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 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   //  TODO in surf model disk???
92   //  action->io.surf_io = surf_workstation_model->extension.storage.write(host->host, name),
93   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 2.0);
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.path,
106       simcall->file_open.mode);
107   xbt_fifo_push(action->simcalls, simcall);
108   simcall->issuer->waiting_action = action;
109 }
110
111 smx_action_t SIMIX_file_open(smx_process_t process, const char* path, const char* mode)
112 {
113   smx_action_t action;
114   smx_host_t host = process->smx_host;
115
116   /* check if the host is active */
117   if (surf_workstation_model->extension.
118       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
119     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
120            host->name);
121   }
122
123   action = xbt_mallocator_get(simix_global->action_mallocator);
124   action->type = SIMIX_ACTION_IO;
125   action->name = NULL;
126 #ifdef HAVE_TRACING
127   action->category = NULL;
128 #endif
129
130   action->io.host = host;
131   //  TODO in surf model disk???
132   //  action->io.surf_io = surf_workstation_model->extension.storage.open(host->host, name),
133   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 3.0);
134
135   surf_workstation_model->action_data_set(action->io.surf_io, action);
136   XBT_DEBUG("Create io action %p", action);
137
138   return action;
139 }
140
141 //SIMIX FILE CLOSE
142 void SIMIX_pre_file_close(smx_simcall_t simcall)
143 {
144   smx_action_t action = SIMIX_file_close(simcall->issuer,
145       simcall->file_close.fp);
146   xbt_fifo_push(action->simcalls, simcall);
147   simcall->issuer->waiting_action = action;
148 }
149
150 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp)
151 {
152   smx_action_t action;
153   smx_host_t host = process->smx_host;
154
155   /* check if the host is active */
156   if (surf_workstation_model->extension.
157       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
158     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
159            host->name);
160   }
161
162   action = xbt_mallocator_get(simix_global->action_mallocator);
163   action->type = SIMIX_ACTION_IO;
164   action->name = NULL;
165 #ifdef HAVE_TRACING
166   action->category = NULL;
167 #endif
168
169   action->io.host = host;
170   //  TODO in surf model disk???
171   //  action->io.surf_io = surf_workstation_model->extension.storage.close(host->host, name),
172   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 4.0);
173
174   surf_workstation_model->action_data_set(action->io.surf_io, action);
175   XBT_DEBUG("Create io action %p", action);
176
177   return action;
178 }
179
180 //SIMIX FILE STAT
181 void SIMIX_pre_file_stat(smx_simcall_t simcall)
182 {
183   smx_action_t action = SIMIX_file_stat(simcall->issuer,
184       simcall->file_stat.fd,
185       simcall->file_stat.buf);
186   xbt_fifo_push(action->simcalls, simcall);
187   simcall->issuer->waiting_action = action;
188 }
189
190 smx_action_t SIMIX_file_stat(smx_process_t process, int fd, void* buf)
191 {
192   smx_action_t action;
193   smx_host_t host = process->smx_host;
194
195   /* check if the host is active */
196   if (surf_workstation_model->extension.
197       workstation.get_state(host->host) != SURF_RESOURCE_ON) {
198     THROWF(host_error, 0, "Host %s failed, you cannot call this function",
199            host->name);
200   }
201
202   action = xbt_mallocator_get(simix_global->action_mallocator);
203   action->type = SIMIX_ACTION_IO;
204   action->name = NULL;
205 #ifdef HAVE_TRACING
206   action->category = NULL;
207 #endif
208
209   action->io.host = host;
210   //  TODO in surf model disk???
211   //  action->io.surf_io = surf_workstation_model->extension.storage.stat(host->host, name),
212   action->io.surf_io = surf_workstation_model->extension.workstation.sleep(host->host, 5.0);
213
214   surf_workstation_model->action_data_set(action->io.surf_io, action);
215   XBT_DEBUG("Create io action %p", action);
216
217   return action;
218 }
219
220 void SIMIX_post_io(smx_action_t action)
221 {
222   switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
223
224     case SURF_ACTION_FAILED:
225       action->state = SIMIX_FAILED;
226       break;
227
228     case SURF_ACTION_DONE:
229       action->state = SIMIX_DONE;
230       break;
231
232     default:
233       THROW_IMPOSSIBLE;
234       break;
235   }
236
237   SIMIX_io_finish(action);
238 }
239
240 void SIMIX_io_destroy(smx_action_t action)
241 {
242   XBT_DEBUG("Destroy action %p", action);
243   if (action->io.surf_io)
244     action->io.surf_io->model_type->action_unref(action->io.surf_io);
245   xbt_mallocator_release(simix_global->action_mallocator, action);
246 }
247
248 void SIMIX_io_finish(smx_action_t action)
249 {
250   volatile xbt_fifo_item_t item;
251   smx_simcall_t simcall;
252
253   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
254
255     switch (action->state) {
256
257       case SIMIX_DONE:
258         /* do nothing, action done */
259         break;
260
261       case SIMIX_FAILED:
262         TRY {
263           THROWF(io_error, 0, "IO failed");
264         }
265         CATCH(simcall->issuer->running_ctx->exception) {
266           simcall->issuer->doexception = 1;
267         }
268       break;
269
270       case SIMIX_CANCELED:
271         TRY {
272           THROWF(cancel_error, 0, "Canceled");
273         }
274         CATCH(simcall->issuer->running_ctx->exception) {
275           simcall->issuer->doexception = 1;
276         }
277         break;
278
279       default:
280         xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
281             action->state);
282     }
283     simcall->issuer->waiting_action = NULL;
284     SIMIX_simcall_answer(simcall);
285   }
286
287   /* We no longer need it */
288   SIMIX_io_destroy(action);
289 }