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 "surf/storage_private.h"
9 #include "xbt/sysdep.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix,
15 "Logging specific to SIMIX (io)");
19 void SIMIX_pre_file_read(smx_simcall_t simcall, void *ptr, size_t size,
22 smx_action_t action = SIMIX_file_read(simcall->issuer, ptr, size, fd);
23 xbt_fifo_push(action->simcalls, simcall);
24 simcall->issuer->waiting_action = action;
27 smx_action_t SIMIX_file_read(smx_process_t process, void* ptr, size_t size,
31 smx_host_t host = process->smx_host;
33 /* check if the host is active */
34 if (surf_workstation_model->extension.
35 workstation.get_state(host) != SURF_RESOURCE_ON) {
36 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
40 action = xbt_mallocator_get(simix_global->action_mallocator);
41 action->type = SIMIX_ACTION_IO;
44 action->category = NULL;
47 action->io.host = host;
49 surf_workstation_model->extension.workstation.read(host, ptr, size,
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, const void *ptr, size_t size,
62 smx_action_t action = SIMIX_file_write(simcall->issuer, ptr, size, fd);
63 xbt_fifo_push(action->simcalls, simcall);
64 simcall->issuer->waiting_action = action;
67 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr,
68 size_t size, smx_file_t fd)
71 smx_host_t host = process->smx_host;
73 /* check if the host is active */
74 if (surf_workstation_model->extension.
75 workstation.get_state(host) != SURF_RESOURCE_ON) {
76 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
80 action = xbt_mallocator_get(simix_global->action_mallocator);
81 action->type = SIMIX_ACTION_IO;
84 action->category = NULL;
87 action->io.host = host;
89 surf_workstation_model->extension.workstation.write(host, ptr, size,
92 surf_workstation_model->action_data_set(action->io.surf_io, action);
93 XBT_DEBUG("Create io action %p", action);
99 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
102 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
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* mount,
111 smx_host_t host = process->smx_host;
113 /* check if the host is active */
114 if (surf_workstation_model->extension.
115 workstation.get_state(host) != SURF_RESOURCE_ON) {
116 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
120 action = xbt_mallocator_get(simix_global->action_mallocator);
121 action->type = SIMIX_ACTION_IO;
124 action->category = NULL;
127 action->io.host = host;
129 surf_workstation_model->extension.workstation.open(host, mount, path);
131 surf_workstation_model->action_data_set(action->io.surf_io, action);
132 XBT_DEBUG("Create io action %p", action);
138 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
140 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
141 xbt_fifo_push(action->simcalls, simcall);
142 simcall->issuer->waiting_action = action;
145 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
148 smx_host_t host = process->smx_host;
150 /* check if the host is active */
151 if (surf_workstation_model->extension.
152 workstation.get_state(host) != SURF_RESOURCE_ON) {
153 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
157 action = xbt_mallocator_get(simix_global->action_mallocator);
158 action->type = SIMIX_ACTION_IO;
161 action->category = NULL;
164 action->io.host = host;
165 action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
167 surf_workstation_model->action_data_set(action->io.surf_io, action);
168 XBT_DEBUG("Create io action %p", action);
175 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
177 return SIMIX_file_unlink(simcall->issuer, fd);
180 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
182 smx_host_t host = process->smx_host;
183 /* check if the host is active */
184 if (surf_workstation_model->extension.
185 workstation.get_state(host) != SURF_RESOURCE_ON) {
186 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
190 if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
191 fd->surf_file = NULL;
198 void SIMIX_pre_file_ls(smx_simcall_t simcall,
199 const char* mount, const char* path)
201 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
202 xbt_fifo_push(action->simcalls, simcall);
203 simcall->issuer->waiting_action = action;
205 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
208 smx_host_t host = process->smx_host;
209 /* check if the host is active */
210 if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
211 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
215 action = xbt_mallocator_get(simix_global->action_mallocator);
216 action->type = SIMIX_ACTION_IO;
219 action->category = NULL;
222 action->io.host = host;
223 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
225 surf_workstation_model->action_data_set(action->io.surf_io, action);
226 XBT_DEBUG("Create io action %p", action);
230 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
232 return SIMIX_file_get_size(simcall->issuer, fd);
235 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
237 smx_host_t host = process->smx_host;
238 return surf_workstation_model->extension.workstation.get_size(host,
243 void SIMIX_post_io(smx_action_t action)
246 smx_simcall_t simcall;
248 // xbt_dict_cursor_t cursor = NULL;
249 // s_file_stat_t *dst = NULL;
250 // s_file_stat_t *src = NULL;
252 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
253 switch (simcall->call) {
254 case SIMCALL_FILE_OPEN:;
255 smx_file_t tmp = xbt_new(s_smx_file_t,1);
256 tmp->surf_file = (action->io.surf_io)->file;
257 simcall_file_open__set__result(simcall, tmp);
260 case SIMCALL_FILE_CLOSE:
261 xbt_free(simcall_file_close__get__fd(simcall));
262 simcall_file_close__set__result(simcall, 0);
265 case SIMCALL_FILE_WRITE:
266 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
269 case SIMCALL_FILE_READ:
270 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
273 case SIMCALL_FILE_LS:
274 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
275 // // if there is a stat we have to duplicate it
277 // dst = xbt_new0(s_file_stat_t,1);
278 // file_stat_copy(src, dst);
279 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
282 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
289 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
291 case SURF_ACTION_FAILED:
292 action->state = SIMIX_FAILED;
295 case SURF_ACTION_DONE:
296 action->state = SIMIX_DONE;
304 SIMIX_io_finish(action);
307 void SIMIX_io_destroy(smx_action_t action)
309 XBT_DEBUG("Destroy action %p", action);
310 if (action->io.surf_io)
311 action->io.surf_io->model_obj->action_unref(action->io.surf_io);
312 xbt_mallocator_release(simix_global->action_mallocator, action);
315 void SIMIX_io_finish(smx_action_t action)
317 xbt_fifo_item_t item;
318 smx_simcall_t simcall;
320 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
322 switch (action->state) {
325 /* do nothing, action done */
329 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
333 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
337 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
341 if (surf_workstation_model->extension.
342 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
343 simcall->issuer->context->iwannadie = 1;
346 simcall->issuer->waiting_action = NULL;
347 SIMIX_simcall_answer(simcall);
350 /* We no longer need it */
351 SIMIX_io_destroy(action);