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, smx_host_t host)
55 smx_action_t action = SIMIX_file_read(simcall->issuer, fd, size, host);
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, smx_host_t host)
64 /* check if the host is active */
65 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
66 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
70 action = xbt_mallocator_get(simix_global->action_mallocator);
71 action->type = SIMIX_ACTION_IO;
74 action->category = NULL;
77 action->io.host = host;
78 action->io.surf_io = surf_workstation_read(host, fd->surf_file, size);
80 surf_action_set_data(action->io.surf_io, action);
81 XBT_DEBUG("Create io action %p", action);
87 void SIMIX_pre_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, smx_host_t host)
89 smx_action_t action = SIMIX_file_write(simcall->issuer, fd, size, host);
90 xbt_fifo_push(action->simcalls, simcall);
91 simcall->issuer->waiting_action = action;
94 smx_action_t SIMIX_file_write(smx_process_t process, smx_file_t fd, sg_size_t size, smx_host_t host)
98 /* check if the host is active */
99 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
100 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
104 action = xbt_mallocator_get(simix_global->action_mallocator);
105 action->type = SIMIX_ACTION_IO;
108 action->category = NULL;
111 action->io.host = host;
112 action->io.surf_io = surf_workstation_write(host, fd->surf_file, size);
114 surf_action_set_data(action->io.surf_io, action);
115 XBT_DEBUG("Create io action %p", action);
121 void SIMIX_pre_file_open(smx_simcall_t simcall, const char* fullpath)
123 smx_action_t action = SIMIX_file_open(simcall->issuer, fullpath);
124 xbt_fifo_push(action->simcalls, simcall);
125 simcall->issuer->waiting_action = action;
128 smx_action_t SIMIX_file_open(smx_process_t process, const char* fullpath)
131 smx_host_t host = process->smx_host;
133 /* check if the host is active */
134 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
135 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
139 action = xbt_mallocator_get(simix_global->action_mallocator);
140 action->type = SIMIX_ACTION_IO;
143 action->category = NULL;
146 action->io.host = host;
147 action->io.surf_io = surf_workstation_open(host, fullpath);
149 surf_action_set_data(action->io.surf_io, action);
150 XBT_DEBUG("Create io action %p", action);
156 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd)
158 smx_action_t action = SIMIX_file_close(simcall->issuer, fd);
159 xbt_fifo_push(action->simcalls, simcall);
160 simcall->issuer->waiting_action = action;
163 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd)
166 smx_host_t host = process->smx_host;
168 /* check if the host is active */
169 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
170 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
174 action = xbt_mallocator_get(simix_global->action_mallocator);
175 action->type = SIMIX_ACTION_IO;
178 action->category = NULL;
181 action->io.host = host;
182 action->io.surf_io = surf_workstation_close(host, fd->surf_file);
184 surf_action_set_data(action->io.surf_io, action);
185 XBT_DEBUG("Create io action %p", action);
192 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
194 return SIMIX_file_unlink(simcall->issuer, fd);
197 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
199 smx_host_t host = process->smx_host;
200 /* check if the host is active */
201 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
202 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
206 if (surf_workstation_unlink(host, fd->surf_file)){
213 sg_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
215 return SIMIX_file_get_size(simcall->issuer, fd);
218 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
220 smx_host_t host = process->smx_host;
221 return surf_workstation_get_size(host, fd->surf_file);
224 sg_size_t SIMIX_pre_file_tell(smx_simcall_t simcall, smx_file_t fd)
226 return SIMIX_file_tell(simcall->issuer, fd);
229 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
231 smx_host_t host = process->smx_host;
232 return surf_workstation_file_tell(host, fd->surf_file);
236 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
238 return SIMIX_file_get_info(simcall->issuer, fd);
241 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
243 smx_host_t host = process->smx_host;
244 return surf_workstation_get_info(host, fd->surf_file);
247 int SIMIX_pre_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_size_t offset, int origin)
249 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
252 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_size_t offset, int origin)
254 smx_host_t host = process->smx_host;
255 return surf_workstation_file_seek(host, fd->surf_file, offset, origin);
258 int SIMIX_pre_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
260 return SIMIX_file_move(simcall->issuer, file, fullpath);
263 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
265 smx_host_t host = process->smx_host;
266 return surf_workstation_file_move(host, file->surf_file, fullpath);
269 int SIMIX_pre_file_rcopy(smx_simcall_t simcall, smx_file_t fd, smx_host_t host, const char* fullpath)
271 return SIMIX_file_rcopy(simcall->issuer, fd, host, fullpath);
274 int SIMIX_file_rcopy(smx_process_t process, smx_file_t file, smx_host_t host_dest, const char* fullpath)
276 smx_host_t host = process->smx_host;
277 return surf_workstation_file_rcopy(host, file->surf_file, host_dest, fullpath);
280 sg_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
282 return SIMIX_storage_get_free_size(simcall->issuer, name);
285 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
287 smx_host_t host = process->smx_host;
288 return surf_workstation_get_free_size(host, name);
291 sg_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
293 return SIMIX_storage_get_used_size(simcall->issuer, name);
296 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
298 smx_host_t host = process->smx_host;
299 return surf_workstation_get_used_size(host, name);
302 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
303 return SIMIX_storage_get_properties(storage);
305 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
306 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
307 return surf_resource_get_properties(surf_storage_resource_priv(storage));
310 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
311 return SIMIX_storage_get_name(storage);
314 const char* SIMIX_storage_get_name(smx_storage_t storage){
315 xbt_assert((storage != NULL), "Invalid parameters");
316 return sg_storage_name(storage);
319 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
320 return SIMIX_storage_get_content(storage);
323 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
324 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
325 return surf_storage_get_content(storage);
328 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
329 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
330 return surf_storage_get_size(storage);
333 const char* SIMIX_pre_storage_get_host(smx_simcall_t simcall, smx_storage_t storage){
334 return SIMIX_storage_get_host(storage);
337 const char* SIMIX_storage_get_host(smx_storage_t storage){
338 xbt_assert((storage != NULL), "Invalid parameters");
339 return surf_storage_get_host(storage);
342 void SIMIX_post_io(smx_action_t action)
345 smx_simcall_t simcall;
347 // xbt_dict_cursor_t cursor = NULL;
348 // s_file_stat_t *dst = NULL;
349 // s_file_stat_t *src = NULL;
351 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
352 switch (simcall->call) {
353 case SIMCALL_FILE_OPEN: {
354 smx_file_t tmp = xbt_new(s_smx_file_t,1);
355 tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
356 simcall_file_open__set__result(simcall, tmp);
359 case SIMCALL_FILE_CLOSE:
360 xbt_free(simcall_file_close__get__fd(simcall));
361 simcall_file_close__set__result(simcall, 0);
363 case SIMCALL_FILE_WRITE:
364 simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
367 case SIMCALL_FILE_READ:
368 simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
376 switch (surf_action_get_state(action->io.surf_io)) {
378 case SURF_ACTION_FAILED:
379 action->state = SIMIX_FAILED;
382 case SURF_ACTION_DONE:
383 action->state = SIMIX_DONE;
391 SIMIX_io_finish(action);
394 void SIMIX_io_destroy(smx_action_t action)
396 XBT_DEBUG("Destroy action %p", action);
397 if (action->io.surf_io)
398 surf_action_unref(action->io.surf_io);
399 xbt_mallocator_release(simix_global->action_mallocator, action);
402 void SIMIX_io_finish(smx_action_t action)
404 xbt_fifo_item_t item;
405 smx_simcall_t simcall;
407 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
409 switch (action->state) {
412 /* do nothing, action done */
416 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
420 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
424 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
428 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
429 simcall->issuer->context->iwannadie = 1;
432 simcall->issuer->waiting_action = NULL;
433 SIMIX_simcall_answer(simcall);
436 /* We no longer need it */
437 SIMIX_io_destroy(action);