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, size_t size,
61 smx_action_t action = SIMIX_file_read(simcall->issuer, size, fd);
62 xbt_fifo_push(action->simcalls, simcall);
63 simcall->issuer->waiting_action = action;
66 smx_action_t SIMIX_file_read(smx_process_t process, size_t size,
70 smx_host_t host = process->smx_host;
72 /* check if the host is active */
73 if (surf_workstation_model->extension.
74 workstation.get_state(host) != SURF_RESOURCE_ON) {
75 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
79 action = xbt_mallocator_get(simix_global->action_mallocator);
80 action->type = SIMIX_ACTION_IO;
83 action->category = NULL;
86 action->io.host = host;
88 surf_workstation_model->extension.workstation.read(host, size,
91 surf_workstation_model->action_data_set(action->io.surf_io, action);
92 XBT_DEBUG("Create io action %p", action);
98 void SIMIX_pre_file_write(smx_simcall_t simcall, size_t size,
101 smx_action_t action = SIMIX_file_write(simcall->issuer, size, fd);
102 xbt_fifo_push(action->simcalls, simcall);
103 simcall->issuer->waiting_action = action;
106 smx_action_t SIMIX_file_write(smx_process_t process,
107 size_t size, smx_file_t fd)
110 smx_host_t host = process->smx_host;
112 /* check if the host is active */
113 if (surf_workstation_model->extension.
114 workstation.get_state(host) != SURF_RESOURCE_ON) {
115 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
119 action = xbt_mallocator_get(simix_global->action_mallocator);
120 action->type = SIMIX_ACTION_IO;
123 action->category = NULL;
126 action->io.host = host;
128 surf_workstation_model->extension.workstation.write(host, size,
131 surf_workstation_model->action_data_set(action->io.surf_io, action);
132 XBT_DEBUG("Create io action %p", action);
138 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
141 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
142 xbt_fifo_push(action->simcalls, simcall);
143 simcall->issuer->waiting_action = action;
146 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
150 smx_host_t host = process->smx_host;
152 /* check if the host is active */
153 if (surf_workstation_model->extension.
154 workstation.get_state(host) != SURF_RESOURCE_ON) {
155 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
159 action = xbt_mallocator_get(simix_global->action_mallocator);
160 action->type = SIMIX_ACTION_IO;
163 action->category = NULL;
166 action->io.host = host;
168 surf_workstation_model->extension.workstation.open(host, mount, path);
170 surf_workstation_model->action_data_set(action->io.surf_io, action);
171 XBT_DEBUG("Create io action %p", action);
177 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
179 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
180 xbt_fifo_push(action->simcalls, simcall);
181 simcall->issuer->waiting_action = action;
184 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
187 smx_host_t host = process->smx_host;
189 /* check if the host is active */
190 if (surf_workstation_model->extension.
191 workstation.get_state(host) != SURF_RESOURCE_ON) {
192 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
196 action = xbt_mallocator_get(simix_global->action_mallocator);
197 action->type = SIMIX_ACTION_IO;
200 action->category = NULL;
203 action->io.host = host;
204 action->io.surf_io = surf_workstation_model->extension.workstation.close(host, fd->surf_file);
206 surf_workstation_model->action_data_set(action->io.surf_io, action);
207 XBT_DEBUG("Create io action %p", action);
214 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
216 return SIMIX_file_unlink(simcall->issuer, fd);
219 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
221 smx_host_t host = process->smx_host;
222 /* check if the host is active */
223 if (surf_workstation_model->extension.
224 workstation.get_state(host) != SURF_RESOURCE_ON) {
225 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
229 if (surf_workstation_model->extension.workstation.unlink(host, fd->surf_file)){
230 fd->surf_file = NULL;
237 void SIMIX_pre_file_ls(smx_simcall_t simcall,
238 const char* mount, const char* path)
240 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
241 xbt_fifo_push(action->simcalls, simcall);
242 simcall->issuer->waiting_action = action;
244 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
247 smx_host_t host = process->smx_host;
248 /* check if the host is active */
249 if (surf_workstation_model->extension.workstation.get_state(host) != SURF_RESOURCE_ON) {
250 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
254 action = xbt_mallocator_get(simix_global->action_mallocator);
255 action->type = SIMIX_ACTION_IO;
258 action->category = NULL;
261 action->io.host = host;
262 action->io.surf_io = surf_workstation_model->extension.workstation.ls(host,mount,path);
264 surf_workstation_model->action_data_set(action->io.surf_io, action);
265 XBT_DEBUG("Create io action %p", action);
269 size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
271 return SIMIX_file_get_size(simcall->issuer, fd);
274 size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
276 smx_host_t host = process->smx_host;
277 return surf_workstation_model->extension.workstation.get_size(host,
281 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
283 return SIMIX_file_get_info(simcall->issuer, fd);
286 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
288 smx_host_t host = process->smx_host;
289 return surf_workstation_model->extension.workstation.get_info(host,
293 size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
295 return SIMIX_storage_get_free_size(simcall->issuer, name);
298 size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
300 smx_host_t host = process->smx_host;
301 return surf_workstation_model->extension.workstation.get_free_size(host,name);
304 size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
306 return SIMIX_storage_get_used_size(simcall->issuer, name);
309 size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
311 smx_host_t host = process->smx_host;
312 return surf_workstation_model->extension.workstation.get_used_size(host,name);
315 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
316 return SIMIX_storage_get_properties(storage);
318 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
319 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
320 return surf_storage_model->extension.storage.get_properties(storage);
323 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
324 return SIMIX_storage_get_name(storage);
327 const char* SIMIX_storage_get_name(smx_storage_t storage){
328 xbt_assert((storage != NULL), "Invalid parameters");
329 return sg_storage_name(storage);
332 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
333 SIMIX_storage_set_data(storage, data);
335 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
336 xbt_assert((storage != NULL), "Invalid parameters");
337 xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
339 SIMIX_storage_priv(storage)->data = data;
342 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
343 return SIMIX_storage_get_data(storage);
346 void* SIMIX_storage_get_data(smx_storage_t storage){
347 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
349 return SIMIX_storage_priv(storage)->data;
352 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
353 return SIMIX_storage_get_content(storage);
356 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
357 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
358 return surf_storage_model->extension.storage.get_content(storage);
361 void SIMIX_post_io(smx_action_t action)
364 smx_simcall_t simcall;
366 // xbt_dict_cursor_t cursor = NULL;
367 // s_file_stat_t *dst = NULL;
368 // s_file_stat_t *src = NULL;
370 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
371 switch (simcall->call) {
372 case SIMCALL_FILE_OPEN:;
373 smx_file_t tmp = xbt_new(s_smx_file_t,1);
374 tmp->surf_file = (action->io.surf_io)->file;
375 simcall_file_open__set__result(simcall, tmp);
378 case SIMCALL_FILE_CLOSE:
379 xbt_free(simcall_file_close__get__fd(simcall));
380 simcall_file_close__set__result(simcall, 0);
383 case SIMCALL_FILE_WRITE:
384 simcall_file_write__set__result(simcall, (action->io.surf_io)->cost);
387 case SIMCALL_FILE_READ:
388 simcall_file_read__set__result(simcall, (action->io.surf_io)->cost);
391 case SIMCALL_FILE_LS:
392 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
393 // // if there is a stat we have to duplicate it
395 // dst = xbt_new0(s_file_stat_t,1);
396 // file_stat_copy(src, dst);
397 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
400 simcall_file_ls__set__result(simcall, (action->io.surf_io)->ls_dict);
407 switch (surf_workstation_model->action_state_get(action->io.surf_io)) {
409 case SURF_ACTION_FAILED:
410 action->state = SIMIX_FAILED;
413 case SURF_ACTION_DONE:
414 action->state = SIMIX_DONE;
422 SIMIX_io_finish(action);
425 void SIMIX_io_destroy(smx_action_t action)
427 XBT_DEBUG("Destroy action %p", action);
428 if (action->io.surf_io)
429 action->io.surf_io->model_type->action_unref(action->io.surf_io);
430 xbt_mallocator_release(simix_global->action_mallocator, action);
433 void SIMIX_io_finish(smx_action_t action)
435 xbt_fifo_item_t item;
436 smx_simcall_t simcall;
438 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
440 switch (action->state) {
443 /* do nothing, action done */
447 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
451 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
455 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
459 if (surf_workstation_model->extension.
460 workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
461 simcall->issuer->context->iwannadie = 1;
464 simcall->issuer->waiting_action = NULL;
465 SIMIX_simcall_answer(simcall);
468 /* We no longer need it */
469 SIMIX_io_destroy(action);