1 /* Copyright (c) 2007-2010, 2012-2013. 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_storage_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);
33 return xbt_lib_get_or_null(storage_lib, name, SIMIX_STORAGE_LEVEL);
37 * \brief Internal function to destroy a SIMIX storage.
39 * \param s the host to destroy (a smx_storage_t)
41 void SIMIX_storage_destroy(void *s)
43 smx_storage_priv_t storage = (smx_storage_priv_t) s;
45 xbt_assert((storage != NULL), "Invalid parameters");
49 /* Clean storage structure */
53 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
54 return SIMIX_file_get_data(fd);
57 void* SIMIX_file_get_data(smx_file_t fd){
58 xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
63 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
64 SIMIX_file_set_data(fd, data);
67 void SIMIX_file_set_data(smx_file_t fd, void *data){
68 xbt_assert((fd != NULL), "Invalid parameter");
74 void SIMIX_pre_file_read(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
76 smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size);
77 xbt_fifo_push(action->simcalls, simcall);
78 simcall->issuer->waiting_action = action;
81 smx_action_t SIMIX_file_read(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
84 smx_host_t host = process->smx_host;
86 /* check if the host is active */
87 if (surf_workstation_model->extension.
88 workstation.get_state(host) != SURF_RESOURCE_ON) {
89 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
93 action = xbt_mallocator_get(simix_global->action_mallocator);
94 action->type = SIMIX_ACTION_IO;
97 action->category = NULL;
100 action->io.host = host;
102 surf_workstation_model->extension.workstation.read(host, fd->surf_file, size);
104 surf_workstation_model->action_data_set(action->io.surf_io, action);
105 XBT_DEBUG("Create io action %p", action);
111 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
113 smx_action_t action = SIMIX_file_write(simcall->issuer, fd, size);
114 xbt_fifo_push(action->simcalls, simcall);
115 simcall->issuer->waiting_action = action;
118 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
121 smx_host_t host = process->smx_host;
123 /* check if the host is active */
124 if (surf_workstation_model->extension.
125 workstation.get_state(host) != SURF_RESOURCE_ON) {
126 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
130 action = xbt_mallocator_get(simix_global->action_mallocator);
131 action->type = SIMIX_ACTION_IO;
134 action->category = NULL;
137 action->io.host = host;
139 surf_workstation_model->extension.workstation.write(host, fd->surf_file, size);
141 surf_workstation_model->action_data_set(action->io.surf_io, action);
142 XBT_DEBUG("Create io action %p", action);
148 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
151 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
152 xbt_fifo_push(action->simcalls, simcall);
153 simcall->issuer->waiting_action = action;
156 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
160 smx_host_t host = process->smx_host;
162 /* check if the host is active */
163 if (surf_workstation_model->extension.
164 workstation.get_state(host) != SURF_RESOURCE_ON) {
165 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
169 action = xbt_mallocator_get(simix_global->action_mallocator);
170 action->type = SIMIX_ACTION_IO;
173 action->category = NULL;
176 action->io.host = host;
178 surf_workstation_model->extension.workstation.open(host, mount, path);
180 surf_workstation_model->action_data_set(action->io.surf_io, action);
181 XBT_DEBUG("Create io action %p", action);
187 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
189 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
190 xbt_fifo_push(action->simcalls, simcall);
191 simcall->issuer->waiting_action = action;
194 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
197 smx_host_t host = process->smx_host;
199 /* check if the host is active */
200 if (surf_workstation_model->extension.
201 workstation.get_state(host) != SURF_RESOURCE_ON) {
202 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
206 action = xbt_mallocator_get(simix_global->action_mallocator);
207 action->type = SIMIX_ACTION_IO;
210 action->category = NULL;
213 action->io.host = host;
214 action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
216 surf_workstation_model->action_data_set(action->io.surf_io, action);
217 XBT_DEBUG("Create io action %p", action);
224 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
226 return SIMIX_file_unlink(simcall->issuer, fd);
229 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
231 smx_host_t host = process->smx_host;
232 /* check if the host is active */
233 if (surf_workstation_model->extension.
234 workstation.get_state(host) != SURF_RESOURCE_ON) {
235 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
239 if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
247 void SIMIX_pre_file_ls(smx_simcall_t simcall,
248 const char* mount, const char* path)
250 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
251 xbt_fifo_push(action->simcalls, simcall);
252 simcall->issuer->waiting_action = action;
254 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
257 smx_host_t host = process->smx_host;
258 /* check if the host is active */
259 if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
260 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
264 action = xbt_mallocator_get(simix_global->action_mallocator);
265 action->type = SIMIX_ACTION_IO;
268 action->category = NULL;
271 action->io.host = host;
272 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
274 surf_workstation_model->action_data_set(action->io.surf_io, action);
275 XBT_DEBUG("Create io action %p", action);
279 sg_storage_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
281 return SIMIX_file_get_size(simcall->issuer, fd);
284 sg_storage_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
286 smx_host_t host = process->smx_host;
287 return surf_workstation_model->extension.workstation.get_size(host,
291 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
293 return SIMIX_file_get_info(simcall->issuer, fd);
296 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
298 smx_host_t host = process->smx_host;
299 return surf_workstation_model->extension.workstation.get_info(host,
303 void SIMIX_pre_file_rename(smx_simcall_t simcall, smx_file_t fd, const char* new_name)
305 return SIMIX_file_rename(simcall->issuer, fd, new_name);
308 void SIMIX_file_rename(smx_process_t process, smx_file_t fd, const char* new_name)
310 smx_host_t host = process->smx_host;
311 return surf_workstation_model->extension.workstation.rename(host,
312 fd->surf_file, new_name);
316 sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
318 return SIMIX_storage_get_free_size(simcall->issuer, name);
321 sg_storage_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
323 smx_host_t host = process->smx_host;
324 return surf_workstation_model->extension.workstation.get_free_size(host,name);
327 sg_storage_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
329 return SIMIX_storage_get_used_size(simcall->issuer, name);
332 sg_storage_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
334 smx_host_t host = process->smx_host;
335 return surf_workstation_model->extension.workstation.get_used_size(host,name);
338 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
339 return SIMIX_storage_get_properties(storage);
341 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
342 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
343 return surf_storage_model->extension.storage.get_properties(storage);
346 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
347 return SIMIX_storage_get_name(storage);
350 const char* SIMIX_storage_get_name(smx_storage_t storage){
351 xbt_assert((storage != NULL), "Invalid parameters");
352 return sg_storage_name(storage);
355 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
356 SIMIX_storage_set_data(storage, data);
358 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
359 xbt_assert((storage != NULL), "Invalid parameters");
360 xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
362 SIMIX_storage_priv(storage)->data = data;
365 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
366 return SIMIX_storage_get_data(storage);
369 void* SIMIX_storage_get_data(smx_storage_t storage){
370 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
372 return SIMIX_storage_priv(storage)->data;
375 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
376 return SIMIX_storage_get_content(storage);
379 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
380 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
381 return surf_storage_model->extension.storage.get_content(storage);
384 sg_storage_size_t SIMIX_storage_get_size(smx_storage_t storage){
385 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
386 return surf_storage_model->extension.storage.get_size(storage);
389 void SIMIX_post_io(smx_action_t action)
392 smx_simcall_t simcall;
394 // xbt_dict_cursor_t cursor = NULL;
395 // s_file_stat_t *dst = NULL;
396 // s_file_stat_t *src = NULL;
398 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
399 switch (simcall->call) {
400 case SIMCALL_FILE_OPEN:;
401 smx_file_t tmp = xbt_new(s_smx_file_t,1);
402 tmp->surf_file = (action->io.surf_io)->file;
403 simcall_file_open__set__result(simcall, tmp);
406 case SIMCALL_FILE_CLOSE:
407 xbt_free(simcall_file_close__get__fd(simcall));
408 simcall_file_close__set__result(simcall, 0);
411 case SIMCALL_FILE_WRITE:
412 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
415 case SIMCALL_FILE_READ:
416 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
419 case SIMCALL_FILE_LS:
420 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
421 // // if there is a stat we have to duplicate it
423 // dst = xbt_new0(s_file_stat_t,1);
424 // file_stat_copy(src, dst);
425 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
428 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
435 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
437 case SURF_ACTION_FAILED:
438 action->state = SIMIX_FAILED;
441 case SURF_ACTION_DONE:
442 action->state = SIMIX_DONE;
450 SIMIX_io_finish(action);
453 void SIMIX_io_destroy(smx_action_t action)
455 XBT_DEBUG("Destroy action %p", action);
456 if (action->io.surf_io)
457 action->io.surf_io->model_type->action_unref(action->io.surf_io);
458 xbt_mallocator_release(simix_global->action_mallocator, action);
461 void SIMIX_io_finish(smx_action_t action)
463 xbt_fifo_item_t item;
464 smx_simcall_t simcall;
466 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
468 switch (action->state) {
471 /* do nothing, action done */
475 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
479 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
483 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
487 if (surf_workstation_model->extension.
488 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
489 simcall->issuer->context->iwannadie = 1;
492 simcall->issuer->waiting_action = NULL;
493 SIMIX_simcall_answer(simcall);
496 /* We no longer need it */
497 SIMIX_io_destroy(action);