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 action->io.surf_io = surf_workstation_model->extension.workstation.read(host->host, ptr, size, nmemb, (surf_file_t)stream),
51 surf_workstation_model->action_data_set(action->io.surf_io, action);
52 XBT_DEBUG("Create io action %p", action);
58 void SIMIX_pre_file_write(smx_simcall_t simcall)
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;
69 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr, size_t size, size_t nmemb, smx_file_t stream)
72 smx_host_t host = process->smx_host;
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",
81 action = xbt_mallocator_get(simix_global->action_mallocator);
82 action->type = SIMIX_ACTION_IO;
85 action->category = NULL;
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);
91 surf_workstation_model->action_data_set(action->io.surf_io, action);
92 XBT_DEBUG("Create io action %p", action);
98 void SIMIX_pre_file_open(smx_simcall_t simcall)
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;
107 smx_action_t SIMIX_file_open(smx_process_t process, const char* path, const char* mode)
110 smx_host_t host = process->smx_host;
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",
119 action = xbt_mallocator_get(simix_global->action_mallocator);
120 action->type = SIMIX_ACTION_IO;
123 action->category = NULL;
126 action->io.host = host;
127 action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, path, mode);
129 surf_workstation_model->action_data_set(action->io.surf_io, action);
130 XBT_DEBUG("Create io action %p", action);
136 void SIMIX_pre_file_close(smx_simcall_t simcall)
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;
144 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fp)
147 smx_host_t host = process->smx_host;
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",
156 action = xbt_mallocator_get(simix_global->action_mallocator);
157 action->type = SIMIX_ACTION_IO;
160 action->category = NULL;
163 action->io.host = host;
164 action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, (surf_file_t)fp);
166 surf_workstation_model->action_data_set(action->io.surf_io, action);
167 XBT_DEBUG("Create io action %p", action);
173 void SIMIX_pre_file_stat(smx_simcall_t simcall)
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;
182 smx_action_t SIMIX_file_stat(smx_process_t process, int fd, void* buf)
185 smx_host_t host = process->smx_host;
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",
194 action = xbt_mallocator_get(simix_global->action_mallocator);
195 action->type = SIMIX_ACTION_IO;
198 action->category = NULL;
201 action->io.host = host;
202 action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, fd, buf);
204 surf_workstation_model->action_data_set(action->io.surf_io, action);
205 XBT_DEBUG("Create io action %p", action);
210 void SIMIX_post_io(smx_action_t action)
212 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
214 case SURF_ACTION_FAILED:
215 action->state = SIMIX_FAILED;
218 case SURF_ACTION_DONE:
219 action->state = SIMIX_DONE;
227 SIMIX_io_finish(action);
230 void SIMIX_io_destroy(smx_action_t action)
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);
238 void SIMIX_io_finish(smx_action_t action)
240 xbt_fifo_item_t item;
241 smx_simcall_t simcall;
243 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
245 switch (action->state) {
248 /* do nothing, action done */
252 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
256 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
260 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
263 simcall->issuer->waiting_action = NULL;
264 SIMIX_simcall_answer(simcall);
267 /* We no longer need it */
268 SIMIX_io_destroy(action);