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)");
18 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
19 return SIMIX_file_get_data(fd);
22 void* SIMIX_file_get_data(smx_file_t fd){
23 xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
28 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
29 SIMIX_file_set_data(fd, data);
32 void SIMIX_file_set_data(smx_file_t fd, void *data){
33 xbt_assert((fd != NULL), "Invalid parameter");
39 void SIMIX_pre_file_read(smx_simcall_t simcall, size_t size,
42 smx_action_t action = SIMIX_file_read(simcall->issuer, size, fd);
43 xbt_fifo_push(action->simcalls, simcall);
44 simcall->issuer->waiting_action = action;
47 smx_action_t SIMIX_file_read(smx_process_t process, size_t size,
51 smx_host_t host = process->smx_host;
53 /* check if the host is active */
54 if (surf_workstation_model->extension.
55 workstation.get_state(host) != SURF_RESOURCE_ON) {
56 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
60 action = xbt_mallocator_get(simix_global->action_mallocator);
61 action->type = SIMIX_ACTION_IO;
64 action->category = NULL;
67 action->io.host = host;
69 surf_workstation_model->extension.workstation.read(host, size,
72 surf_workstation_model->action_data_set(action->io.surf_io, action);
73 XBT_DEBUG("Create io action %p", action);
79 void SIMIX_pre_file_write(smx_simcall_t simcall, size_t size,
82 smx_action_t action = SIMIX_file_write(simcall->issuer, size, fd);
83 xbt_fifo_push(action->simcalls, simcall);
84 simcall->issuer->waiting_action = action;
87 smx_action_t SIMIX_file_write(smx_process_t process,
88 size_t size, smx_file_t fd)
91 smx_host_t host = process->smx_host;
93 /* check if the host is active */
94 if (surf_workstation_model->extension.
95 workstation.get_state(host) != SURF_RESOURCE_ON) {
96 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
100 action = xbt_mallocator_get(simix_global->action_mallocator);
101 action->type = SIMIX_ACTION_IO;
104 action->category = NULL;
107 action->io.host = host;
109 surf_workstation_model->extension.workstation.write(host, size,
112 surf_workstation_model->action_data_set(action->io.surf_io, action);
113 XBT_DEBUG("Create io action %p", action);
119 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
122 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
123 xbt_fifo_push(action->simcalls, simcall);
124 simcall->issuer->waiting_action = action;
127 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
131 smx_host_t host = process->smx_host;
133 /* check if the host is active */
134 if (surf_workstation_model->extension.
135 workstation.get_state(host) != SURF_RESOURCE_ON) {
136 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
140 action = xbt_mallocator_get(simix_global->action_mallocator);
141 action->type = SIMIX_ACTION_IO;
144 action->category = NULL;
147 action->io.host = host;
149 surf_workstation_model->extension.workstation.open(host, mount, path);
151 surf_workstation_model->action_data_set(action->io.surf_io, action);
152 XBT_DEBUG("Create io action %p", action);
158 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
160 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
161 xbt_fifo_push(action->simcalls, simcall);
162 simcall->issuer->waiting_action = action;
165 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
168 smx_host_t host = process->smx_host;
170 /* check if the host is active */
171 if (surf_workstation_model->extension.
172 workstation.get_state(host) != SURF_RESOURCE_ON) {
173 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
177 action = xbt_mallocator_get(simix_global->action_mallocator);
178 action->type = SIMIX_ACTION_IO;
181 action->category = NULL;
184 action->io.host = host;
185 action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
187 surf_workstation_model->action_data_set(action->io.surf_io, action);
188 XBT_DEBUG("Create io action %p", action);
195 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
197 return SIMIX_file_unlink(simcall->issuer, fd);
200 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
202 smx_host_t host = process->smx_host;
203 /* check if the host is active */
204 if (surf_workstation_model->extension.
205 workstation.get_state(host) != SURF_RESOURCE_ON) {
206 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
210 if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
211 fd->surf_file = NULL;
218 void SIMIX_pre_file_ls(smx_simcall_t simcall,
219 const char* mount, const char* path)
221 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
222 xbt_fifo_push(action->simcalls, simcall);
223 simcall->issuer->waiting_action = action;
225 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
228 smx_host_t host = process->smx_host;
229 /* check if the host is active */
230 if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
231 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
235 action = xbt_mallocator_get(simix_global->action_mallocator);
236 action->type = SIMIX_ACTION_IO;
239 action->category = NULL;
242 action->io.host = host;
243 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
245 surf_workstation_model->action_data_set(action->io.surf_io, action);
246 XBT_DEBUG("Create io action %p", action);
250 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
252 return SIMIX_file_get_size(simcall->issuer, fd);
255 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
257 smx_host_t host = process->smx_host;
258 return surf_workstation_model->extension.workstation.get_size(host,
262 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
264 return SIMIX_file_get_info(simcall->issuer, fd);
267 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
269 smx_host_t host = process->smx_host;
270 return surf_workstation_model->extension.workstation.get_info(host,
274 size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
276 return SIMIX_storage_get_free_size(simcall->issuer, name);
279 size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
281 smx_host_t host = process->smx_host;
282 return surf_workstation_model->extension.workstation.get_free_size(host,name);
285 size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
287 return SIMIX_storage_get_used_size(simcall->issuer, name);
290 size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
292 smx_host_t host = process->smx_host;
293 return surf_workstation_model->extension.workstation.get_used_size(host,name);
296 void SIMIX_post_io(smx_action_t action)
299 smx_simcall_t simcall;
301 // xbt_dict_cursor_t cursor = NULL;
302 // s_file_stat_t *dst = NULL;
303 // s_file_stat_t *src = NULL;
305 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
306 switch (simcall->call) {
307 case SIMCALL_FILE_OPEN:;
308 smx_file_t tmp = xbt_new(s_smx_file_t,1);
309 tmp->surf_file = (action->io.surf_io)->file;
310 simcall_file_open__set__result(simcall, tmp);
313 case SIMCALL_FILE_CLOSE:
314 xbt_free(simcall_file_close__get__fd(simcall));
315 simcall_file_close__set__result(simcall, 0);
318 case SIMCALL_FILE_WRITE:
319 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
322 case SIMCALL_FILE_READ:
323 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
326 case SIMCALL_FILE_LS:
327 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
328 // // if there is a stat we have to duplicate it
330 // dst = xbt_new0(s_file_stat_t,1);
331 // file_stat_copy(src, dst);
332 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
335 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
342 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
344 case SURF_ACTION_FAILED:
345 action->state = SIMIX_FAILED;
348 case SURF_ACTION_DONE:
349 action->state = SIMIX_DONE;
357 SIMIX_io_finish(action);
360 void SIMIX_io_destroy(smx_action_t action)
362 XBT_DEBUG("Destroy action %p", action);
363 if (action->io.surf_io)
364 action->io.surf_io->model_type->action_unref(action->io.surf_io);
365 xbt_mallocator_release(simix_global->action_mallocator, action);
368 void SIMIX_io_finish(smx_action_t action)
370 xbt_fifo_item_t item;
371 smx_simcall_t simcall;
373 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
375 switch (action->state) {
378 /* do nothing, action done */
382 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
386 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
390 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
394 if (surf_workstation_model->extension.
395 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
396 simcall->issuer->context->iwannadie = 1;
399 simcall->issuer->waiting_action = NULL;
400 SIMIX_simcall_answer(simcall);
403 /* We no longer need it */
404 SIMIX_io_destroy(action);