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 void* SIMIX_pre_file_get_data(smx_simcall_t simcall,smx_file_t fd){
38 return SIMIX_file_get_data(fd);
41 void* SIMIX_file_get_data(smx_file_t fd){
42 xbt_assert((fd != NULL), "Invalid parameters (simix file is NULL)");
47 void SIMIX_pre_file_set_data(smx_simcall_t simcall, smx_file_t fd, void *data) {
48 SIMIX_file_set_data(fd, data);
51 void SIMIX_file_set_data(smx_file_t fd, void *data){
52 xbt_assert((fd != NULL), "Invalid parameter");
58 void SIMIX_pre_file_read(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
60 smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size);
61 xbt_fifo_push(action->simcalls, simcall);
62 simcall->issuer->waiting_action = action;
65 smx_action_t SIMIX_file_read(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
68 smx_host_t host = process->smx_host;
70 /* check if the host is active */
71 if (surf_workstation_model->extension.
72 workstation.get_state(host) != SURF_RESOURCE_ON) {
73 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
77 action = xbt_mallocator_get(simix_global->action_mallocator);
78 action->type = SIMIX_ACTION_IO;
81 action->category = NULL;
84 action->io.host = host;
86 surf_workstation_model->extension.workstation.read(host, fd->surf_file, size);
88 surf_workstation_model->action_data_set(action->io.surf_io, action);
89 XBT_DEBUG("Create io action %p", action);
95 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_storage_size_t size)
97 smx_action_t action = SIMIX_file_write(simcall->issuer, fd, size);
98 xbt_fifo_push(action->simcalls, simcall);
99 simcall->issuer->waiting_action = action;
102 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_storage_size_t size)
105 smx_host_t host = process->smx_host;
107 /* check if the host is active */
108 if (surf_workstation_model->extension.
109 workstation.get_state(host) != SURF_RESOURCE_ON) {
110 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
114 action = xbt_mallocator_get(simix_global->action_mallocator);
115 action->type = SIMIX_ACTION_IO;
118 action->category = NULL;
121 action->io.host = host;
123 surf_workstation_model->extension.workstation.write(host, fd->surf_file, size);
125 surf_workstation_model->action_data_set(action->io.surf_io, action);
126 XBT_DEBUG("Create io action %p", action);
132 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
135 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
136 xbt_fifo_push(action->simcalls, simcall);
137 simcall->issuer->waiting_action = action;
140 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
144 smx_host_t host = process->smx_host;
146 /* check if the host is active */
147 if (surf_workstation_model->extension.
148 workstation.get_state(host) != SURF_RESOURCE_ON) {
149 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
153 action = xbt_mallocator_get(simix_global->action_mallocator);
154 action->type = SIMIX_ACTION_IO;
157 action->category = NULL;
160 action->io.host = host;
162 surf_workstation_model->extension.workstation.open(host, mount, path);
164 surf_workstation_model->action_data_set(action->io.surf_io, action);
165 XBT_DEBUG("Create io action %p", action);
171 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
173 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
174 xbt_fifo_push(action->simcalls, simcall);
175 simcall->issuer->waiting_action = action;
178 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
181 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 action = xbt_mallocator_get(simix_global->action_mallocator);
191 action->type = SIMIX_ACTION_IO;
194 action->category = NULL;
197 action->io.host = host;
198 action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
200 surf_workstation_model->action_data_set(action->io.surf_io, action);
201 XBT_DEBUG("Create io action %p", action);
208 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
210 return SIMIX_file_unlink(simcall->issuer, fd);
213 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
215 smx_host_t host = process->smx_host;
216 /* check if the host is active */
217 if (surf_workstation_model->extension.
218 workstation.get_state(host) != SURF_RESOURCE_ON) {
219 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
223 if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
231 void SIMIX_pre_file_ls(smx_simcall_t simcall,
232 const char* mount, const char* path)
234 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
235 xbt_fifo_push(action->simcalls, simcall);
236 simcall->issuer->waiting_action = action;
238 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
241 smx_host_t host = process->smx_host;
242 /* check if the host is active */
243 if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
244 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
248 action = xbt_mallocator_get(simix_global->action_mallocator);
249 action->type = SIMIX_ACTION_IO;
252 action->category = NULL;
255 action->io.host = host;
256 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
258 surf_workstation_model->action_data_set(action->io.surf_io, action);
259 XBT_DEBUG("Create io action %p", action);
263 sg_storage_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
265 return SIMIX_file_get_size(simcall->issuer, fd);
268 sg_storage_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
270 smx_host_t host = process->smx_host;
271 return surf_workstation_model->extension.workstation.get_size(host,
275 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
277 return SIMIX_file_get_info(simcall->issuer, fd);
280 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
282 smx_host_t host = process->smx_host;
283 return surf_workstation_model->extension.workstation.get_info(host,
287 sg_storage_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
289 return SIMIX_storage_get_free_size(simcall->issuer, name);
292 sg_storage_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
294 smx_host_t host = process->smx_host;
295 return surf_workstation_model->extension.workstation.get_free_size(host,name);
298 sg_storage_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
300 return SIMIX_storage_get_used_size(simcall->issuer, name);
303 sg_storage_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
305 smx_host_t host = process->smx_host;
306 return surf_workstation_model->extension.workstation.get_used_size(host,name);
309 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
310 return SIMIX_storage_get_properties(storage);
312 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
313 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
314 return surf_storage_model->extension.storage.get_properties(storage);
317 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
318 return SIMIX_storage_get_name(storage);
321 const char* SIMIX_storage_get_name(smx_storage_t storage){
322 xbt_assert((storage != NULL), "Invalid parameters");
323 return sg_storage_name(storage);
326 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
327 SIMIX_storage_set_data(storage, data);
329 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
330 xbt_assert((storage != NULL), "Invalid parameters");
331 xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
333 SIMIX_storage_priv(storage)->data = data;
336 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
337 return SIMIX_storage_get_data(storage);
340 void* SIMIX_storage_get_data(smx_storage_t storage){
341 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
343 return SIMIX_storage_priv(storage)->data;
346 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
347 return SIMIX_storage_get_content(storage);
350 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
351 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
352 return surf_storage_model->extension.storage.get_content(storage);
355 sg_storage_size_t SIMIX_storage_get_size(smx_storage_t storage){
356 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
357 return surf_storage_model->extension.storage.get_size(storage);
360 void SIMIX_post_io(smx_action_t action)
363 smx_simcall_t simcall;
365 // xbt_dict_cursor_t cursor = NULL;
366 // s_file_stat_t *dst = NULL;
367 // s_file_stat_t *src = NULL;
369 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
370 switch (simcall->call) {
371 case SIMCALL_FILE_OPEN:;
372 smx_file_t tmp = xbt_new(s_smx_file_t,1);
373 tmp->surf_file = (action->io.surf_io)->file;
374 simcall_file_open__set__result(simcall, tmp);
377 case SIMCALL_FILE_CLOSE:
378 xbt_free(simcall_file_close__get__fd(simcall));
379 simcall_file_close__set__result(simcall, 0);
382 case SIMCALL_FILE_WRITE:
383 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
386 case SIMCALL_FILE_READ:
387 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
390 case SIMCALL_FILE_LS:
391 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
392 // // if there is a stat we have to duplicate it
394 // dst = xbt_new0(s_file_stat_t,1);
395 // file_stat_copy(src, dst);
396 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
399 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
406 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
408 case SURF_ACTION_FAILED:
409 action->state = SIMIX_FAILED;
412 case SURF_ACTION_DONE:
413 action->state = SIMIX_DONE;
421 SIMIX_io_finish(action);
424 void SIMIX_io_destroy(smx_action_t action)
426 XBT_DEBUG("Destroy action %p", action);
427 if (action->io.surf_io)
428 action->io.surf_io->model_type->action_unref(action->io.surf_io);
429 xbt_mallocator_release(simix_global->action_mallocator, action);
432 void SIMIX_io_finish(smx_action_t action)
434 xbt_fifo_item_t item;
435 smx_simcall_t simcall;
437 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
439 switch (action->state) {
442 /* do nothing, action done */
446 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
450 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
454 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
458 if (surf_workstation_model->extension.
459 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
460 simcall->issuer->context->iwannadie = 1;
463 simcall->issuer->waiting_action = NULL;
464 SIMIX_simcall_answer(simcall);
467 /* We no longer need it */
468 SIMIX_io_destroy(action);