Logo AND Algorithmique Numérique Distribuée

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