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_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_size_t size)
84 smx_host_t host = process->smx_host;
86 /* check if the host is active */
87 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
88 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
92 action = xbt_mallocator_get(simix_global->action_mallocator);
93 action->type = SIMIX_ACTION_IO;
96 action->category = NULL;
99 action->io.host = host;
100 action->io.surf_io = surf_workstation_read(host, fd->surf_file, size);
102 surf_action_set_data(action->io.surf_io, action);
103 XBT_DEBUG("Create io action %p", action);
109 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size)
111 smx_action_t action = SIMIX_file_write(simcall->issuer, fd, size);
112 xbt_fifo_push(action->simcalls, simcall);
113 simcall->issuer->waiting_action = action;
116 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_size_t size)
119 smx_host_t host = process->smx_host;
121 /* check if the host is active */
122 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
123 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
127 action = xbt_mallocator_get(simix_global->action_mallocator);
128 action->type = SIMIX_ACTION_IO;
131 action->category = NULL;
134 action->io.host = host;
135 action->io.surf_io = surf_workstation_write(host, fd->surf_file, size);
137 surf_action_set_data(action->io.surf_io, action);
138 XBT_DEBUG("Create io action %p", action);
144 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* mount,
147 smx_action_t action = SIMIX_file_open(simcall->issuer, mount, path);
148 xbt_fifo_push(action->simcalls, simcall);
149 simcall->issuer->waiting_action = action;
152 smx_action_t SIMIX_file_open(smx_process_t process ,const char* mount,
156 smx_host_t host = process->smx_host;
158 /* check if the host is active */
159 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
160 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
164 action = xbt_mallocator_get(simix_global->action_mallocator);
165 action->type = SIMIX_ACTION_IO;
168 action->category = NULL;
171 action->io.host = host;
172 action->io.surf_io = surf_workstation_open(host, mount, path);
174 surf_action_set_data(action->io.surf_io, action);
175 XBT_DEBUG("Create io action %p", action);
181 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
183 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
184 xbt_fifo_push(action->simcalls, simcall);
185 simcall->issuer->waiting_action = action;
188 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
191 smx_host_t host = process->smx_host;
193 /* check if the host is active */
194 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
195 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
199 action = xbt_mallocator_get(simix_global->action_mallocator);
200 action->type = SIMIX_ACTION_IO;
203 action->category = NULL;
206 action->io.host = host;
207 action->io.surf_io = surf_workstation_close(host, fd->surf_file);
209 surf_action_set_data(action->io.surf_io, action);
210 XBT_DEBUG("Create io action %p", action);
217 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
219 return SIMIX_file_unlink(simcall->issuer, fd);
222 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
224 smx_host_t host = process->smx_host;
225 /* check if the host is active */
226 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
227 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
231 if (surf_workstation_unlink(host, fd->surf_file)){
239 void SIMIX_pre_file_ls(smx_simcall_t simcall,
240 const char* mount, const char* path)
242 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
243 xbt_fifo_push(action->simcalls, simcall);
244 simcall->issuer->waiting_action = action;
246 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
249 smx_host_t host = process->smx_host;
250 /* check if the host is active */
251 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
252 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
256 action = xbt_mallocator_get(simix_global->action_mallocator);
257 action->type = SIMIX_ACTION_IO;
260 action->category = NULL;
263 action->io.host = host;
264 action->io.surf_io = surf_workstation_ls(host,mount,path);
266 surf_action_set_data(action->io.surf_io, action);
267 XBT_DEBUG("Create io action %p", action);
271 sg_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
273 return SIMIX_file_get_size(simcall->issuer, fd);
276 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
278 smx_host_t host = process->smx_host;
279 return surf_workstation_get_size(host, fd->surf_file);
282 sg_size_t SIMIX_pre_file_tell(smx_simcall_t simcall, smx_file_t fd)
284 return SIMIX_file_tell(simcall->issuer, fd);
287 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
289 smx_host_t host = process->smx_host;
290 return surf_workstation_file_tell(host, fd->surf_file);
294 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
296 return SIMIX_file_get_info(simcall->issuer, fd);
299 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
301 smx_host_t host = process->smx_host;
302 return surf_workstation_get_info(host, fd->surf_file);
305 void SIMIX_pre_storage_file_rename(smx_simcall_t simcall, smx_storage_t storage, const char* src, const char* dest)
307 return SIMIX_storage_file_rename(simcall->issuer, storage, src, dest);
310 void SIMIX_storage_file_rename(smx_process_t process, smx_storage_t storage, const char* src, const char* dest)
312 return surf_storage_rename(storage, src, dest);
315 sg_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
317 return SIMIX_storage_get_free_size(simcall->issuer, name);
320 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
322 smx_host_t host = process->smx_host;
323 return surf_workstation_get_free_size(host, name);
326 sg_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
328 return SIMIX_storage_get_used_size(simcall->issuer, name);
331 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
333 smx_host_t host = process->smx_host;
334 return surf_workstation_get_used_size(host, name);
337 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
338 return SIMIX_storage_get_properties(storage);
340 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
341 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
342 return surf_resource_get_properties(surf_workstation_resource_priv(storage));
345 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
346 return SIMIX_storage_get_name(storage);
349 const char* SIMIX_storage_get_name(smx_storage_t storage){
350 xbt_assert((storage != NULL), "Invalid parameters");
351 return sg_storage_name(storage);
354 void SIMIX_pre_storage_set_data(smx_simcall_t simcall, smx_storage_t storage, void *data) {
355 SIMIX_storage_set_data(storage, data);
357 void SIMIX_storage_set_data(smx_storage_t storage, void *data){
358 xbt_assert((storage != NULL), "Invalid parameters");
359 xbt_assert((SIMIX_storage_priv(storage)->data == NULL), "Data already set");
361 SIMIX_storage_priv(storage)->data = data;
364 void* SIMIX_pre_storage_get_data(smx_simcall_t simcall,smx_storage_t storage){
365 return SIMIX_storage_get_data(storage);
368 void* SIMIX_storage_get_data(smx_storage_t storage){
369 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
371 return SIMIX_storage_priv(storage)->data;
374 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
375 return SIMIX_storage_get_content(storage);
378 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
379 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
380 return surf_storage_get_content(storage);
383 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
384 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
385 return surf_storage_get_size(storage);
388 void SIMIX_post_io(smx_action_t action)
391 smx_simcall_t simcall;
393 // xbt_dict_cursor_t cursor = NULL;
394 // s_file_stat_t *dst = NULL;
395 // s_file_stat_t *src = NULL;
397 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
398 switch (simcall->call) {
399 case SIMCALL_FILE_OPEN:;
400 smx_file_t tmp = xbt_new(s_smx_file_t,1);
401 tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
402 simcall_file_open__set__result(simcall, tmp);
405 case SIMCALL_FILE_CLOSE:
406 xbt_free(simcall_file_close__get__fd(simcall));
407 simcall_file_close__set__result(simcall, 0);
409 case SIMCALL_FILE_WRITE:
410 simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
413 case SIMCALL_FILE_READ:
414 simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
417 case SIMCALL_FILE_LS:
418 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
419 // // if there is a stat we have to duplicate it
421 // dst = xbt_new0(s_file_stat_t,1);
422 // file_stat_copy(src, dst);
423 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
426 simcall_file_ls__set__result(simcall, surf_storage_action_get_ls_dict(action->io.surf_io));
433 switch (surf_action_get_state(action->io.surf_io)) {
435 case SURF_ACTION_FAILED:
436 action->state = SIMIX_FAILED;
439 case SURF_ACTION_DONE:
440 action->state = SIMIX_DONE;
448 SIMIX_io_finish(action);
451 void SIMIX_io_destroy(smx_action_t action)
453 XBT_DEBUG("Destroy action %p", action);
454 if (action->io.surf_io)
455 surf_action_unref(action->io.surf_io);
456 xbt_mallocator_release(simix_global->action_mallocator, action);
459 void SIMIX_io_finish(smx_action_t action)
461 xbt_fifo_item_t item;
462 smx_simcall_t simcall;
464 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
466 switch (action->state) {
469 /* do nothing, action done */
473 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
477 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
481 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
485 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
486 simcall->issuer->context->iwannadie = 1;
489 simcall->issuer->waiting_action = NULL;
490 SIMIX_simcall_answer(simcall);
493 /* We no longer need it */
494 SIMIX_io_destroy(action);