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 * \brief Internal function to create a SIMIX storage.
20 * \param name name of the storage to create
21 * \param storage the SURF storage to encapsulate
22 * \param data some user data (may be NULL)
24 smx_host_t SIMIX_storage_create(const char *name, void *storage, void *data)
26 smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
28 smx_storage->data = data;
30 /* Update global variables */
31 xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
32 return xbt_lib_get_elm_or_null(storage_lib, name);
36 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
37 return SIMIX_file_get_data(fd);
40 void* SIMIX_file_get_data(smx_file_t fd){
41 xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
46 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
47 SIMIX_file_set_data(fd, data);
50 void SIMIX_file_set_data(smx_file_t fd, void *data){
51 xbt_assert((fd != NULL), "Invalid parameter");
57 void SIMIX_pre_file_read(smx_simcall_t simcall, size_t size,
60 smx_action_t action = SIMIX_file_read(simcall->issuer, size, fd);
61 xbt_fifo_push(action->simcalls, simcall);
62 simcall->issuer->waiting_action = action;
65 smx_action_t SIMIX_file_read(smx_process_t process, size_t size,
69 smx_host_t host = process->smx_host;
71 /* check if the host is active */
72 if (surf_workstation_model->extension.
73 workstation.get_state(host) != SURF_RESOURCE_ON) {
74 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
78 action = xbt_mallocator_get(simix_global->action_mallocator);
79 action->type = SIMIX_ACTION_IO;
82 action->category = NULL;
85 action->io.host = host;
87 surf_workstation_model->extension.workstation.read(host, size,
90 surf_workstation_model->action_data_set(action->io.surf_io, action);
91 XBT_DEBUG("Create io action %p", action);
97 void SIMIX_pre_file_write(smx_simcall_t simcall, size_t size,
100 smx_action_t action = SIMIX_file_write(simcall->issuer, size, fd);
101 xbt_fifo_push(action->simcalls, simcall);
102 simcall->issuer->waiting_action = action;
105 smx_action_t SIMIX_file_write(smx_process_t process,
106 size_t size, smx_file_t fd)
109 smx_host_t host = process->smx_host;
111 /* check if the host is active */
112 if (surf_workstation_model->extension.
113 workstation.get_state(host) != SURF_RESOURCE_ON) {
114 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
118 action = xbt_mallocator_get(simix_global->action_mallocator);
119 action->type = SIMIX_ACTION_IO;
122 action->category = NULL;
125 action->io.host = host;
127 surf_workstation_model->extension.workstation.write(host, size,
130 surf_workstation_model->action_data_set(action->io.surf_io, action);
131 XBT_DEBUG("Create io action %p", action);
137 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
140 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
141 xbt_fifo_push(action->simcalls, simcall);
142 simcall->issuer->waiting_action = action;
145 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
149 smx_host_t host = process->smx_host;
151 /* check if the host is active */
152 if (surf_workstation_model->extension.
153 workstation.get_state(host) != SURF_RESOURCE_ON) {
154 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
158 action = xbt_mallocator_get(simix_global->action_mallocator);
159 action->type = SIMIX_ACTION_IO;
162 action->category = NULL;
165 action->io.host = host;
167 surf_workstation_model->extension.workstation.open(host, mount, path);
169 surf_workstation_model->action_data_set(action->io.surf_io, action);
170 XBT_DEBUG("Create io action %p", action);
176 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
178 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
179 xbt_fifo_push(action->simcalls, simcall);
180 simcall->issuer->waiting_action = action;
183 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
186 smx_host_t host = process->smx_host;
188 /* check if the host is active */
189 if (surf_workstation_model->extension.
190 workstation.get_state(host) != SURF_RESOURCE_ON) {
191 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
195 action = xbt_mallocator_get(simix_global->action_mallocator);
196 action->type = SIMIX_ACTION_IO;
199 action->category = NULL;
202 action->io.host = host;
203 action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
205 surf_workstation_model->action_data_set(action->io.surf_io, action);
206 XBT_DEBUG("Create io action %p", action);
213 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
215 return SIMIX_file_unlink(simcall->issuer, fd);
218 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
220 smx_host_t host = process->smx_host;
221 /* check if the host is active */
222 if (surf_workstation_model->extension.
223 workstation.get_state(host) != SURF_RESOURCE_ON) {
224 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
228 if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
229 fd->surf_file = NULL;
236 void SIMIX_pre_file_ls(smx_simcall_t simcall,
237 const char* mount, const char* path)
239 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
240 xbt_fifo_push(action->simcalls, simcall);
241 simcall->issuer->waiting_action = action;
243 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
246 smx_host_t host = process->smx_host;
247 /* check if the host is active */
248 if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
249 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
253 action = xbt_mallocator_get(simix_global->action_mallocator);
254 action->type = SIMIX_ACTION_IO;
257 action->category = NULL;
260 action->io.host = host;
261 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
263 surf_workstation_model->action_data_set(action->io.surf_io, action);
264 XBT_DEBUG("Create io action %p", action);
268 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
270 return SIMIX_file_get_size(simcall->issuer, fd);
273 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
275 smx_host_t host = process->smx_host;
276 return surf_workstation_model->extension.workstation.get_size(host,
280 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
282 return SIMIX_file_get_info(simcall->issuer, fd);
285 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
287 smx_host_t host = process->smx_host;
288 return surf_workstation_model->extension.workstation.get_info(host,
292 size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
294 return SIMIX_storage_get_free_size(simcall->issuer, name);
297 size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
299 smx_host_t host = process->smx_host;
300 return surf_workstation_model->extension.workstation.get_free_size(host,name);
303 size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
305 return SIMIX_storage_get_used_size(simcall->issuer, name);
308 size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
310 smx_host_t host = process->smx_host;
311 return surf_workstation_model->extension.workstation.get_used_size(host,name);
314 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
315 return SIMIX_storage_get_properties(storage);
317 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
318 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
319 return surf_storage_model->extension.storage.get_properties(storage);
322 const char* SIMIX_storage_get_name(smx_storage_t storage){
323 xbt_assert((storage != NULL), "Invalid parameters");
325 return sg_storage_name(storage);
328 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
329 SIMIX_storage_set_data(storage, data);
331 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
332 xbt_assert((storage != NULL), "Invalid parameters");
333 xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
335 SIMIX_storage_priv(storage)->data = data;
338 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
339 return SIMIX_storage_get_data(storage);
342 void* SIMIX_storage_get_data(smx_storage_t storage){
343 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
345 return SIMIX_storage_priv(storage)->data;
348 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
349 return SIMIX_storage_get_content(storage);
352 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
353 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
354 return surf_storage_model->extension.storage.get_content(storage);
357 void SIMIX_post_io(smx_action_t action)
360 smx_simcall_t simcall;
362 // xbt_dict_cursor_t cursor = NULL;
363 // s_file_stat_t *dst = NULL;
364 // s_file_stat_t *src = NULL;
366 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
367 switch (simcall->call) {
368 case SIMCALL_FILE_OPEN:;
369 smx_file_t tmp = xbt_new(s_smx_file_t,1);
370 tmp->surf_file = (action->io.surf_io)->file;
371 simcall_file_open__set__result(simcall, tmp);
374 case SIMCALL_FILE_CLOSE:
375 xbt_free(simcall_file_close__get__fd(simcall));
376 simcall_file_close__set__result(simcall, 0);
379 case SIMCALL_FILE_WRITE:
380 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
383 case SIMCALL_FILE_READ:
384 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
387 case SIMCALL_FILE_LS:
388 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
389 // // if there is a stat we have to duplicate it
391 // dst = xbt_new0(s_file_stat_t,1);
392 // file_stat_copy(src, dst);
393 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
396 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
403 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
405 case SURF_ACTION_FAILED:
406 action->state = SIMIX_FAILED;
409 case SURF_ACTION_DONE:
410 action->state = SIMIX_DONE;
418 SIMIX_io_finish(action);
421 void SIMIX_io_destroy(smx_action_t action)
423 XBT_DEBUG("Destroy action %p", action);
424 if (action->io.surf_io)
425 action->io.surf_io->model_type->action_unref(action->io.surf_io);
426 xbt_mallocator_release(simix_global->action_mallocator, action);
429 void SIMIX_io_finish(smx_action_t action)
431 xbt_fifo_item_t item;
432 smx_simcall_t simcall;
434 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
436 switch (action->state) {
439 /* do nothing, action done */
443 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
447 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
451 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
455 if (surf_workstation_model->extension.
456 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
457 simcall->issuer->context->iwannadie = 1;
460 simcall->issuer->waiting_action = NULL;
461 SIMIX_simcall_answer(simcall);
464 /* We no longer need it */
465 SIMIX_io_destroy(action);