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;
30 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)
33 smx_host_t host = process->smx_host;
35 /* check if the host is active */
36 if (surf_workstation_model->extension.
37 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
38 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
42 action = xbt_mallocator_get(simix_global->action_mallocator);
43 action->type = SIMIX_ACTION_IO;
46 action->category = NULL;
49 action->io.host = host;
50 action->io.surf_io = surf_workstation_model->extension.workstation.read(host->host, storage, ptr, size, nmemb, stream->surf_file),
52 surf_workstation_model->action_data_set(action->io.surf_io, action);
53 XBT_DEBUG("Create io action %p", action);
59 void SIMIX_pre_file_write(smx_simcall_t simcall)
61 smx_action_t action = SIMIX_file_write(simcall->issuer,
62 simcall->file_write.storage,
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 char* storage, 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 action->io.surf_io = surf_workstation_model->extension.workstation.write(host->host, storage, ptr, size, nmemb, stream->surf_file);
93 surf_workstation_model->action_data_set(action->io.surf_io, action);
94 XBT_DEBUG("Create io action %p", action);
100 void SIMIX_pre_file_open(smx_simcall_t simcall)
102 smx_action_t action = SIMIX_file_open(simcall->issuer,
103 simcall->file_open.storage,
104 simcall->file_open.path,
105 simcall->file_open.mode);
106 xbt_fifo_push(action->simcalls, simcall);
107 simcall->issuer->waiting_action = action;
110 smx_action_t SIMIX_file_open(smx_process_t process ,const char* storage, const char* path, const char* mode)
113 smx_host_t host = process->smx_host;
115 /* check if the host is active */
116 if (surf_workstation_model->extension.
117 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
118 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
122 action = xbt_mallocator_get(simix_global->action_mallocator);
123 action->type = SIMIX_ACTION_IO;
126 action->category = NULL;
129 action->io.host = host;
130 action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, storage, path, mode);
132 surf_workstation_model->action_data_set(action->io.surf_io, action);
133 XBT_DEBUG("Create io action %p", action);
139 void SIMIX_pre_file_close(smx_simcall_t simcall)
141 smx_action_t action = SIMIX_file_close(simcall->issuer,
142 simcall->file_close.storage,
143 simcall->file_close.fp);
144 xbt_fifo_push(action->simcalls, simcall);
145 simcall->issuer->waiting_action = action;
148 smx_action_t SIMIX_file_close(smx_process_t process ,const char* storage, smx_file_t fp)
151 smx_host_t host = process->smx_host;
153 /* check if the host is active */
154 if (surf_workstation_model->extension.
155 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
156 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
160 action = xbt_mallocator_get(simix_global->action_mallocator);
161 action->type = SIMIX_ACTION_IO;
164 action->category = NULL;
167 action->io.host = host;
168 action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, storage, fp->surf_file);
170 surf_workstation_model->action_data_set(action->io.surf_io, action);
171 XBT_DEBUG("Create io action %p", action);
177 void SIMIX_pre_file_stat(smx_simcall_t simcall)
179 smx_action_t action = SIMIX_file_stat(simcall->issuer,
180 simcall->file_stat.storage,
181 simcall->file_stat.fd,
182 simcall->file_stat.buf);
183 xbt_fifo_push(action->simcalls, simcall);
184 simcall->issuer->waiting_action = action;
187 smx_action_t SIMIX_file_stat(smx_process_t process ,const char* storage, smx_file_t fd, s_file_stat_t buf)
190 smx_host_t host = process->smx_host;
191 /* check if the host is active */
192 if (surf_workstation_model->extension.
193 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
194 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
198 action = xbt_mallocator_get(simix_global->action_mallocator);
199 action->type = SIMIX_ACTION_IO;
202 action->category = NULL;
205 action->io.host = host;
206 action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, storage, fd->surf_file);
208 surf_workstation_model->action_data_set(action->io.surf_io, action);
209 XBT_DEBUG("Create io action %p", action);
214 void SIMIX_post_io(smx_action_t action)
217 smx_simcall_t simcall;
219 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
220 switch (simcall->call) {
221 case SIMCALL_FILE_OPEN:
222 simcall->file_open.result = xbt_new(s_smx_file_t,1);
223 simcall->file_open.result->surf_file = (action->io.surf_io)->file;
225 case SIMCALL_FILE_CLOSE:
226 simcall->file_read.result = 0;
228 case SIMCALL_FILE_WRITE:
229 simcall->file_write.result = (action->io.surf_io)->cost;
231 case SIMCALL_FILE_READ:
232 simcall->file_read.result = (action->io.surf_io)->cost;
234 case SIMCALL_FILE_STAT:
235 simcall->file_stat.result = 0;
236 s_file_stat_t *dst = &(simcall->file_stat.buf);
237 s_file_stat_t *src = &((action->io.surf_io)->stat);
238 file_stat_copy(src,dst);
245 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
247 case SURF_ACTION_FAILED:
248 action->state = SIMIX_FAILED;
251 case SURF_ACTION_DONE:
252 action->state = SIMIX_DONE;
260 SIMIX_io_finish(action);
263 void SIMIX_io_destroy(smx_action_t action)
265 XBT_DEBUG("Destroy action %p", action);
266 if (action->io.surf_io)
267 action->io.surf_io->model_type->action_unref(action->io.surf_io);
268 xbt_mallocator_release(simix_global->action_mallocator, action);
271 void SIMIX_io_finish(smx_action_t action)
273 xbt_fifo_item_t item;
274 smx_simcall_t simcall;
276 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
278 switch (action->state) {
281 /* do nothing, action done */
285 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
289 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
293 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
296 simcall->issuer->waiting_action = NULL;
297 SIMIX_simcall_answer(simcall);
300 /* We no longer need it */
301 SIMIX_io_destroy(action);