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.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;
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)
34 smx_host_t host = process->smx_host;
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",
43 action = xbt_mallocator_get(simix_global->action_mallocator);
44 action->type = SIMIX_ACTION_IO;
47 action->category = NULL;
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),
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.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;
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)
76 smx_host_t host = process->smx_host;
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",
85 action = xbt_mallocator_get(simix_global->action_mallocator);
86 action->type = SIMIX_ACTION_IO;
89 action->category = NULL;
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);
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.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;
114 smx_action_t SIMIX_file_open(smx_process_t process ,const char* storage, const char* path, const char* mode)
117 smx_host_t host = process->smx_host;
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",
126 action = xbt_mallocator_get(simix_global->action_mallocator);
127 action->type = SIMIX_ACTION_IO;
130 action->category = NULL;
133 action->io.host = host;
134 action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, storage, path, mode);
136 surf_workstation_model->action_data_set(action->io.surf_io, action);
137 XBT_DEBUG("Create io action %p", action);
143 void SIMIX_pre_file_close(smx_simcall_t simcall)
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;
153 smx_action_t SIMIX_file_close(smx_process_t process ,const char* storage, smx_file_t fp)
156 smx_host_t host = process->smx_host;
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",
165 action = xbt_mallocator_get(simix_global->action_mallocator);
166 action->type = SIMIX_ACTION_IO;
169 action->category = NULL;
172 action->io.host = host;
173 action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, storage, fp->surf_file);
175 surf_workstation_model->action_data_set(action->io.surf_io, action);
176 XBT_DEBUG("Create io action %p", action);
182 void SIMIX_pre_file_stat(smx_simcall_t simcall)
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;
193 smx_action_t SIMIX_file_stat(smx_process_t process ,const char* storage, int fd, void* buf)
196 smx_host_t host = process->smx_host;
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",
205 action = xbt_mallocator_get(simix_global->action_mallocator);
206 action->type = SIMIX_ACTION_IO;
209 action->category = NULL;
212 action->io.host = host;
213 action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, storage, fd, buf);
215 surf_workstation_model->action_data_set(action->io.surf_io, action);
216 XBT_DEBUG("Create io action %p", action);
221 void SIMIX_post_io(smx_action_t action)
223 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
225 case SURF_ACTION_FAILED:
226 action->state = SIMIX_FAILED;
229 case SURF_ACTION_DONE:
230 action->state = SIMIX_DONE;
238 SIMIX_io_finish(action);
241 void SIMIX_io_destroy(smx_action_t action)
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);
249 void SIMIX_io_finish(smx_action_t action)
251 xbt_fifo_item_t item;
252 smx_simcall_t simcall;
254 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
256 switch (action->state) {
259 /* do nothing, action done */
263 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
267 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
271 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
274 simcall->issuer->waiting_action = NULL;
275 SIMIX_simcall_answer(simcall);
278 /* We no longer need it */
279 SIMIX_io_destroy(action);