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_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
35 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
39 action = xbt_mallocator_get(simix_global->action_mallocator);
40 action->type = SIMIX_ACTION_IO;
43 action->category = NULL;
46 action->io.host = host;
47 action->io.surf_io = surf_workstation_read(host, ptr, size, fd->surf_file);
49 surf_action_set_data(action->io.surf_io, action);
50 XBT_DEBUG("Create io action %p", action);
56 void SIMIX_pre_file_write(smx_simcall_t simcall, const void *ptr, size_t size,
59 smx_action_t action = SIMIX_file_write(simcall->issuer, ptr, size, fd);
60 xbt_fifo_push(action->simcalls, simcall);
61 simcall->issuer->waiting_action = action;
64 smx_action_t SIMIX_file_write(smx_process_t process, const void* ptr,
65 size_t size, smx_file_t fd)
68 smx_host_t host = process->smx_host;
70 /* check if the host is active */
71 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
72 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
76 action = xbt_mallocator_get(simix_global->action_mallocator);
77 action->type = SIMIX_ACTION_IO;
80 action->category = NULL;
83 action->io.host = host;
84 action->io.surf_io = surf_workstation_write(host, ptr, size, fd->surf_file);
86 surf_action_set_data(action->io.surf_io, action);
87 XBT_DEBUG("Create io action %p", action);
93 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
96 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
97 xbt_fifo_push(action->simcalls, simcall);
98 simcall->issuer->waiting_action = action;
101 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
105 smx_host_t host = process->smx_host;
107 /* check if the host is active */
108 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
109 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
113 action = xbt_mallocator_get(simix_global->action_mallocator);
114 action->type = SIMIX_ACTION_IO;
117 action->category = NULL;
120 action->io.host = host;
121 action->io.surf_io = surf_workstation_open(host, mount, path);
123 surf_action_set_data(action->io.surf_io, action);
124 XBT_DEBUG("Create io action %p", action);
130 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
132 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
133 xbt_fifo_push(action->simcalls, simcall);
134 simcall->issuer->waiting_action = action;
137 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
140 smx_host_t host = process->smx_host;
142 /* check if the host is active */
143 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
144 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
148 action = xbt_mallocator_get(simix_global->action_mallocator);
149 action->type = SIMIX_ACTION_IO;
152 action->category = NULL;
155 action->io.host = host;
156 action->io.surf_io = surf_workstation_close(host, fd->surf_file);
158 surf_action_set_data(action->io.surf_io, action);
159 XBT_DEBUG("Create io action %p", action);
166 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
168 return SIMIX_file_unlink(simcall->issuer, fd);
171 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
173 smx_host_t host = process->smx_host;
174 /* check if the host is active */
175 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
176 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
180 if (surf_workstation_unlink(host, fd->surf_file)){
181 fd->surf_file = NULL;
188 void SIMIX_pre_file_ls(smx_simcall_t simcall,
189 const char* mount, const char* path)
191 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
192 xbt_fifo_push(action->simcalls, simcall);
193 simcall->issuer->waiting_action = action;
195 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
198 smx_host_t host = process->smx_host;
199 /* check if the host is active */
200 if (surf_resource_get_state(surf_workstation_resource_priv(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_ls(host,mount,path);
215 surf_action_set_data(action->io.surf_io, action);
216 XBT_DEBUG("Create io action %p", action);
220 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
222 return SIMIX_file_get_size(simcall->issuer, fd);
225 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
227 smx_host_t host = process->smx_host;
228 return surf_workstation_get_size(host, fd->surf_file);
232 void SIMIX_post_io(smx_action_t action)
235 smx_simcall_t simcall;
237 // xbt_dict_cursor_t cursor = NULL;
238 // s_file_stat_t *dst = NULL;
239 // s_file_stat_t *src = NULL;
241 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
242 switch (simcall->call) {
243 case SIMCALL_FILE_OPEN:;
244 smx_file_t tmp = xbt_new(s_smx_file_t,1);
245 tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
246 simcall_file_open__set__result(simcall, tmp);
249 case SIMCALL_FILE_CLOSE:
250 xbt_free(simcall_file_close__get__fd(simcall));
251 simcall_file_close__set__result(simcall, 0);
253 case SIMCALL_FILE_WRITE:
254 simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
257 case SIMCALL_FILE_READ:
258 simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
261 case SIMCALL_FILE_LS:
262 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
263 // // if there is a stat we have to duplicate it
265 // dst = xbt_new0(s_file_stat_t,1);
266 // file_stat_copy(src, dst);
267 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
270 simcall_file_ls__set__result(simcall, surf_storage_action_get_ls_dict(action->io.surf_io));
277 switch (surf_action_get_state(action->io.surf_io)) {
279 case SURF_ACTION_FAILED:
280 action->state = SIMIX_FAILED;
283 case SURF_ACTION_DONE:
284 action->state = SIMIX_DONE;
292 SIMIX_io_finish(action);
295 void SIMIX_io_destroy(smx_action_t action)
297 XBT_DEBUG("Destroy action %p", action);
298 if (action->io.surf_io)
299 surf_action_unref(action->io.surf_io);
300 xbt_mallocator_release(simix_global->action_mallocator, action);
303 void SIMIX_io_finish(smx_action_t action)
305 xbt_fifo_item_t item;
306 smx_simcall_t simcall;
308 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
310 switch (action->state) {
313 /* do nothing, action done */
317 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
321 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
325 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
329 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
330 simcall->issuer->context->iwannadie = 1;
333 simcall->issuer->waiting_action = NULL;
334 SIMIX_simcall_answer(simcall);
337 /* We no longer need it */
338 SIMIX_io_destroy(action);