1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
14 "Logging specific to SIMIX (io)");
18 void SIMIX_pre_file_read(smx_simcall_t simcall)
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;
29 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size, size_t nmemb, smx_file_t stream)
32 smx_host_t host = process->smx_host;
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",
41 action = xbt_mallocator_get(simix_global->action_mallocator);
42 action->type = SIMIX_ACTION_IO;
45 action->category = NULL;
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);
53 surf_workstation_model->action_data_set(action->io.surf_io, action);
54 XBT_DEBUG("Create io action %p", action);
60 void SIMIX_pre_file_write(smx_simcall_t simcall)
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;
71 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
74 smx_host_t host = process->smx_host;
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",
83 action = xbt_mallocator_get(simix_global->action_mallocator);
84 action->type = SIMIX_ACTION_IO;
87 action->category = NULL;
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);
95 surf_workstation_model->action_data_set(action->io.surf_io, action);
96 XBT_DEBUG("Create io action %p", action);
102 void SIMIX_pre_file_open(smx_simcall_t simcall)
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;
111 smx_action_t SIMIX_file_open(smx_process_t process, const char* path, const char* mode)
114 smx_host_t host = process->smx_host;
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",
123 action = xbt_mallocator_get(simix_global->action_mallocator);
124 action->type = SIMIX_ACTION_IO;
127 action->category = NULL;
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);
135 surf_workstation_model->action_data_set(action->io.surf_io, action);
136 XBT_DEBUG("Create io action %p", action);
142 void SIMIX_pre_file_close(smx_simcall_t simcall)
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;
150 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp)
153 smx_host_t host = process->smx_host;
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",
162 action = xbt_mallocator_get(simix_global->action_mallocator);
163 action->type = SIMIX_ACTION_IO;
166 action->category = NULL;
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);
174 surf_workstation_model->action_data_set(action->io.surf_io, action);
175 XBT_DEBUG("Create io action %p", action);
181 void SIMIX_pre_file_stat(smx_simcall_t simcall)
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;
190 smx_action_t SIMIX_file_stat(smx_process_t process, int fd, void* buf)
193 smx_host_t host = process->smx_host;
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",
202 action = xbt_mallocator_get(simix_global->action_mallocator);
203 action->type = SIMIX_ACTION_IO;
206 action->category = NULL;
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);
214 surf_workstation_model->action_data_set(action->io.surf_io, action);
215 XBT_DEBUG("Create io action %p", action);
220 void SIMIX_post_io(smx_action_t action)
222 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
224 case SURF_ACTION_FAILED:
225 action->state = SIMIX_FAILED;
228 case SURF_ACTION_DONE:
229 action->state = SIMIX_DONE;
237 SIMIX_io_finish(action);
240 void SIMIX_io_destroy(smx_action_t action)
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);
248 void SIMIX_io_finish(smx_action_t action)
250 volatile xbt_fifo_item_t item;
251 smx_simcall_t simcall;
253 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
255 switch (action->state) {
258 /* do nothing, action done */
263 THROWF(io_error, 0, "IO failed");
265 CATCH(simcall->issuer->running_ctx->exception) {
266 simcall->issuer->doexception = 1;
272 THROWF(cancel_error, 0, "Canceled");
274 CATCH(simcall->issuer->running_ctx->exception) {
275 simcall->issuer->doexception = 1;
280 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
283 simcall->issuer->waiting_action = NULL;
284 SIMIX_simcall_answer(simcall);
287 /* We no longer need it */
288 SIMIX_io_destroy(action);