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, m_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),
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, m_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);
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 = (action->io.surf_io)->file;
113 smx_action_t SIMIX_file_open(smx_process_t process ,const char* storage, const char* path, const char* mode)
116 smx_host_t host = process->smx_host;
118 /* check if the host is active */
119 if (surf_workstation_model->extension.
120 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
121 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
125 action = xbt_mallocator_get(simix_global->action_mallocator);
126 action->type = SIMIX_ACTION_IO;
129 action->category = NULL;
132 action->io.host = host;
133 action->io.surf_io = surf_workstation_model->extension.workstation.open(host->host, storage, path, mode);
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.storage,
146 simcall->file_close.fp);
147 xbt_fifo_push(action->simcalls, simcall);
148 simcall->issuer->waiting_action = action;
149 simcall->file_close.result = 0;
152 smx_action_t SIMIX_file_close(smx_process_t process ,const char* storage, m_file_t fp)
155 smx_host_t host = process->smx_host;
157 /* check if the host is active */
158 if (surf_workstation_model->extension.
159 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
160 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
164 action = xbt_mallocator_get(simix_global->action_mallocator);
165 action->type = SIMIX_ACTION_IO;
168 action->category = NULL;
171 action->io.host = host;
172 action->io.surf_io = surf_workstation_model->extension.workstation.close(host->host, storage, fp);
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.storage,
185 simcall->file_stat.fd,
186 simcall->file_stat.buf);
187 xbt_fifo_push(action->simcalls, simcall);
188 simcall->issuer->waiting_action = action;
189 simcall->file_stat.result = 0;
192 smx_action_t SIMIX_file_stat(smx_process_t process ,const char* storage, int fd, void* buf)
195 smx_host_t host = process->smx_host;
197 /* check if the host is active */
198 if (surf_workstation_model->extension.
199 workstation.get_state(host->host) != SURF_RESOURCE_ON) {
200 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
204 action = xbt_mallocator_get(simix_global->action_mallocator);
205 action->type = SIMIX_ACTION_IO;
208 action->category = NULL;
211 action->io.host = host;
212 action->io.surf_io = surf_workstation_model->extension.workstation.stat(host->host, storage, fd, buf);
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 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 */
262 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
266 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
270 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
273 simcall->issuer->waiting_action = NULL;
274 SIMIX_simcall_answer(simcall);
277 /* We no longer need it */
278 SIMIX_io_destroy(action);