1 /* Copyright (c) 2007-2010, 2012-2014. 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);
32 return xbt_lib_get_elm_or_null(storage_lib, name);
36 * \brief Internal function to destroy a SIMIX storage.
38 * \param s the host to destroy (a smx_storage_t)
40 void SIMIX_storage_destroy(void *s)
42 smx_storage_priv_t storage = (smx_storage_priv_t) s;
44 xbt_assert((storage != NULL), "Invalid parameters");
48 /* Clean storage structure */
53 void SIMIX_pre_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size)
55 smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size);
56 xbt_fifo_push(action->simcalls, simcall);
57 simcall->issuer->waiting_action = action;
60 smx_action_t SIMIX_file_read(smx_process_t process, smx_file_t fd, sg_size_t size)
63 smx_host_t host = process->smx_host;
65 /* check if the host is active */
66 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
67 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
71 action = xbt_mallocator_get(simix_global->action_mallocator);
72 action->type = SIMIX_ACTION_IO;
75 action->category = NULL;
78 action->io.host = host;
79 action->io.surf_io = surf_workstation_read(host, fd->surf_file, size);
81 surf_action_set_data(action->io.surf_io, action);
82 XBT_DEBUG("Create io action %p", action);
88 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size)
90 smx_action_t action = SIMIX_file_write(simcall->issuer, fd, size);
91 xbt_fifo_push(action->simcalls, simcall);
92 simcall->issuer->waiting_action = action;
95 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_size_t size)
98 smx_host_t host = process->smx_host;
100 /* check if the host is active */
101 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
102 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
106 action = xbt_mallocator_get(simix_global->action_mallocator);
107 action->type = SIMIX_ACTION_IO;
110 action->category = NULL;
113 action->io.host = host;
114 action->io.surf_io = surf_workstation_write(host, fd->surf_file, size);
116 surf_action_set_data(action->io.surf_io, action);
117 XBT_DEBUG("Create io action %p", action);
123 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* fullpath)
125 smx_action_t action = SIMIX_file_open(simcall->issuer, fullpath);
126 xbt_fifo_push(action->simcalls, simcall);
127 simcall->issuer->waiting_action = action;
130 smx_action_t SIMIX_file_open(smx_process_t process, const char* fullpath)
133 smx_host_t host = process->smx_host;
135 /* check if the host is active */
136 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
137 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
141 action = xbt_mallocator_get(simix_global->action_mallocator);
142 action->type = SIMIX_ACTION_IO;
145 action->category = NULL;
148 action->io.host = host;
149 action->io.surf_io = surf_workstation_open(host, fullpath);
151 surf_action_set_data(action->io.surf_io, action);
152 XBT_DEBUG("Create io action %p", action);
158 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
160 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
161 xbt_fifo_push(action->simcalls, simcall);
162 simcall->issuer->waiting_action = action;
165 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
168 smx_host_t host = process->smx_host;
170 /* check if the host is active */
171 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
172 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
176 action = xbt_mallocator_get(simix_global->action_mallocator);
177 action->type = SIMIX_ACTION_IO;
180 action->category = NULL;
183 action->io.host = host;
184 action->io.surf_io = surf_workstation_close(host, fd->surf_file);
186 surf_action_set_data(action->io.surf_io, action);
187 XBT_DEBUG("Create io action %p", action);
194 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
196 return SIMIX_file_unlink(simcall->issuer, fd);
199 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
201 smx_host_t host = process->smx_host;
202 /* check if the host is active */
203 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
204 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
208 if (surf_workstation_unlink(host, fd->surf_file)){
216 void SIMIX_pre_file_ls(smx_simcall_t simcall,
217 const char* mount, const char* path)
219 smx_action_t action = SIMIX_file_ls(simcall->issuer, mount, path);
220 xbt_fifo_push(action->simcalls, simcall);
221 simcall->issuer->waiting_action = action;
223 smx_action_t SIMIX_file_ls(smx_process_t process, const char* mount, const char *path)
226 smx_host_t host = process->smx_host;
227 /* check if the host is active */
228 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
229 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
233 action = xbt_mallocator_get(simix_global->action_mallocator);
234 action->type = SIMIX_ACTION_IO;
237 action->category = NULL;
240 action->io.host = host;
241 action->io.surf_io = surf_workstation_ls(host,mount,path);
243 surf_action_set_data(action->io.surf_io, action);
244 XBT_DEBUG("Create io action %p", action);
248 sg_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
250 return SIMIX_file_get_size(simcall->issuer, fd);
253 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
255 smx_host_t host = process->smx_host;
256 return surf_workstation_get_size(host, fd->surf_file);
259 sg_size_t SIMIX_pre_file_tell(smx_simcall_t simcall, smx_file_t fd)
261 return SIMIX_file_tell(simcall->issuer, fd);
264 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
266 smx_host_t host = process->smx_host;
267 return surf_workstation_file_tell(host, fd->surf_file);
271 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
273 return SIMIX_file_get_info(simcall->issuer, fd);
276 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
278 smx_host_t host = process->smx_host;
279 return surf_workstation_get_info(host, fd->surf_file);
282 int SIMIX_pre_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_size_t offset, int origin)
284 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
287 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_size_t offset, int origin)
289 smx_host_t host = process->smx_host;
290 return surf_workstation_file_seek(host, fd->surf_file, offset, origin);
293 void SIMIX_pre_storage_file_rename(smx_simcall_t simcall, smx_storage_t storage, const char* src, const char* dest)
295 return SIMIX_storage_file_rename(simcall->issuer, storage, src, dest);
298 void SIMIX_storage_file_rename(smx_process_t process, smx_storage_t storage, const char* src, const char* dest)
300 return surf_storage_rename(storage, src, dest);
303 sg_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
305 return SIMIX_storage_get_free_size(simcall->issuer, name);
308 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
310 smx_host_t host = process->smx_host;
311 return surf_workstation_get_free_size(host, name);
314 sg_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
316 return SIMIX_storage_get_used_size(simcall->issuer, name);
319 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
321 smx_host_t host = process->smx_host;
322 return surf_workstation_get_used_size(host, name);
325 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
326 return SIMIX_storage_get_properties(storage);
328 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
329 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
330 return surf_resource_get_properties(surf_storage_resource_priv(storage));
333 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
334 return SIMIX_storage_get_name(storage);
337 const char* SIMIX_storage_get_name(smx_storage_t storage){
338 xbt_assert((storage != NULL), "Invalid parameters");
339 return sg_storage_name(storage);
342 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
343 return SIMIX_storage_get_content(storage);
346 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
347 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
348 return surf_storage_get_content(storage);
351 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
352 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
353 return surf_storage_get_size(storage);
356 const char* SIMIX_pre_storage_get_host(smx_simcall_t simcall, smx_storage_t storage){
357 return SIMIX_storage_get_host(storage);
360 const char* SIMIX_storage_get_host(smx_storage_t storage){
361 xbt_assert((storage != NULL), "Invalid parameters");
362 return surf_storage_get_host(storage);
365 void SIMIX_post_io(smx_action_t action)
368 smx_simcall_t simcall;
370 // xbt_dict_cursor_t cursor = NULL;
371 // s_file_stat_t *dst = NULL;
372 // s_file_stat_t *src = NULL;
374 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
375 switch (simcall->call) {
376 case SIMCALL_FILE_OPEN: {
377 smx_file_t tmp = xbt_new(s_smx_file_t,1);
378 tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
379 simcall_file_open__set__result(simcall, tmp);
382 case SIMCALL_FILE_CLOSE:
383 xbt_free(simcall_file_close__get__fd(simcall));
384 simcall_file_close__set__result(simcall, 0);
386 case SIMCALL_FILE_WRITE:
387 simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
390 case SIMCALL_FILE_READ:
391 simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
394 case SIMCALL_FILE_LS:
395 // xbt_dict_foreach((action->io.surf_io)->ls_dict,cursor,key, src){
396 // // if there is a stat we have to duplicate it
398 // dst = xbt_new0(s_file_stat_t,1);
399 // file_stat_copy(src, dst);
400 // xbt_dict_set((action->io.surf_io)->ls_dict,key,dst,xbt_free);
403 simcall_file_ls__set__result(simcall, surf_storage_action_get_ls_dict(action->io.surf_io));
410 switch (surf_action_get_state(action->io.surf_io)) {
412 case SURF_ACTION_FAILED:
413 action->state = SIMIX_FAILED;
416 case SURF_ACTION_DONE:
417 action->state = SIMIX_DONE;
425 SIMIX_io_finish(action);
428 void SIMIX_io_destroy(smx_action_t action)
430 XBT_DEBUG("Destroy action %p", action);
431 if (action->io.surf_io)
432 surf_action_unref(action->io.surf_io);
433 xbt_mallocator_release(simix_global->action_mallocator, action);
436 void SIMIX_io_finish(smx_action_t action)
438 xbt_fifo_item_t item;
439 smx_simcall_t simcall;
441 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
443 switch (action->state) {
446 /* do nothing, action done */
450 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
454 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
458 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
462 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
463 simcall->issuer->context->iwannadie = 1;
466 simcall->issuer->waiting_action = NULL;
467 SIMIX_simcall_answer(simcall);
470 /* We no longer need it */
471 SIMIX_io_destroy(action);