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, smx_host_t host)
123 smx_action_t action = SIMIX_file_open(simcall->issuer, fullpath, host);
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, smx_host_t host)
132 /* check if the host is active */
133 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
134 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
138 action = xbt_mallocator_get(simix_global->action_mallocator);
139 action->type = SIMIX_ACTION_IO;
142 action->category = NULL;
145 action->io.host = host;
146 action->io.surf_io = surf_workstation_open(host, fullpath);
148 surf_action_set_data(action->io.surf_io, action);
149 XBT_DEBUG("Create io action %p", action);
155 void SIMIX_pre_file_close(smx_simcall_t simcall, smx_file_t fd, smx_host_t host)
157 smx_action_t action = SIMIX_file_close(simcall->issuer, fd, host);
158 xbt_fifo_push(action->simcalls, simcall);
159 simcall->issuer->waiting_action = action;
162 smx_action_t SIMIX_file_close(smx_process_t process, smx_file_t fd, smx_host_t host)
166 /* check if the host is active */
167 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
168 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
172 action = xbt_mallocator_get(simix_global->action_mallocator);
173 action->type = SIMIX_ACTION_IO;
176 action->category = NULL;
179 action->io.host = host;
180 action->io.surf_io = surf_workstation_close(host, fd->surf_file);
182 surf_action_set_data(action->io.surf_io, action);
183 XBT_DEBUG("Create io action %p", action);
190 int SIMIX_pre_file_unlink(smx_simcall_t simcall, smx_file_t fd)
192 return SIMIX_file_unlink(simcall->issuer, fd);
195 int SIMIX_file_unlink(smx_process_t process, smx_file_t fd)
197 smx_host_t host = process->smx_host;
198 /* check if the host is active */
199 if (surf_resource_get_state(surf_workstation_resource_priv(host)) != SURF_RESOURCE_ON) {
200 THROWF(host_error, 0, "Host %s failed, you cannot call this function",
204 int res = surf_workstation_unlink(host, fd->surf_file);
209 sg_size_t SIMIX_pre_file_get_size(smx_simcall_t simcall, smx_file_t fd)
211 return SIMIX_file_get_size(simcall->issuer, fd);
214 sg_size_t SIMIX_file_get_size(smx_process_t process, smx_file_t fd)
216 smx_host_t host = process->smx_host;
217 return surf_workstation_get_size(host, fd->surf_file);
220 sg_size_t SIMIX_pre_file_tell(smx_simcall_t simcall, smx_file_t fd)
222 return SIMIX_file_tell(simcall->issuer, fd);
225 sg_size_t SIMIX_file_tell(smx_process_t process, smx_file_t fd)
227 smx_host_t host = process->smx_host;
228 return surf_workstation_file_tell(host, fd->surf_file);
232 xbt_dynar_t SIMIX_pre_file_get_info(smx_simcall_t simcall, smx_file_t fd)
234 return SIMIX_file_get_info(simcall->issuer, fd);
237 xbt_dynar_t SIMIX_file_get_info(smx_process_t process, smx_file_t fd)
239 smx_host_t host = process->smx_host;
240 return surf_workstation_get_info(host, fd->surf_file);
243 int SIMIX_pre_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_size_t offset, int origin)
245 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
248 int SIMIX_file_seek(smx_process_t process, smx_file_t fd, sg_size_t offset, int origin)
250 smx_host_t host = process->smx_host;
251 return surf_workstation_file_seek(host, fd->surf_file, offset, origin);
254 int SIMIX_pre_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
256 return SIMIX_file_move(simcall->issuer, file, fullpath);
259 int SIMIX_file_move(smx_process_t process, smx_file_t file, const char* fullpath)
261 smx_host_t host = process->smx_host;
262 return surf_workstation_file_move(host, file->surf_file, fullpath);
265 sg_size_t SIMIX_pre_storage_get_free_size(smx_simcall_t simcall, const char* name)
267 return SIMIX_storage_get_free_size(simcall->issuer, name);
270 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, const char* name)
272 smx_host_t host = process->smx_host;
273 return surf_workstation_get_free_size(host, name);
276 sg_size_t SIMIX_pre_storage_get_used_size(smx_simcall_t simcall, const char* name)
278 return SIMIX_storage_get_used_size(simcall->issuer, name);
281 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, const char* name)
283 smx_host_t host = process->smx_host;
284 return surf_workstation_get_used_size(host, name);
287 xbt_dict_t SIMIX_pre_storage_get_properties(smx_simcall_t simcall, smx_storage_t storage){
288 return SIMIX_storage_get_properties(storage);
290 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
291 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
292 return surf_resource_get_properties(surf_storage_resource_priv(storage));
295 const char* SIMIX_pre_storage_get_name(smx_simcall_t simcall, smx_storage_t storage){
296 return SIMIX_storage_get_name(storage);
299 const char* SIMIX_storage_get_name(smx_storage_t storage){
300 xbt_assert((storage != NULL), "Invalid parameters");
301 return sg_storage_name(storage);
304 xbt_dict_t SIMIX_pre_storage_get_content(smx_simcall_t simcall, smx_storage_t storage){
305 return SIMIX_storage_get_content(storage);
308 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
309 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
310 return surf_storage_get_content(storage);
313 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
314 xbt_assert((storage != NULL), "Invalid parameters (simix storage is NULL)");
315 return surf_storage_get_size(storage);
318 const char* SIMIX_pre_storage_get_host(smx_simcall_t simcall, smx_storage_t storage){
319 return SIMIX_storage_get_host(storage);
322 const char* SIMIX_storage_get_host(smx_storage_t storage){
323 xbt_assert((storage != NULL), "Invalid parameters");
324 return surf_storage_get_host(storage);
327 void SIMIX_post_io(smx_action_t action)
330 smx_simcall_t simcall;
332 // xbt_dict_cursor_t cursor = NULL;
333 // s_file_stat_t *dst = NULL;
334 // s_file_stat_t *src = NULL;
336 xbt_fifo_foreach(action->simcalls,i,simcall,smx_simcall_t) {
337 switch (simcall->call) {
338 case SIMCALL_FILE_OPEN: {
339 smx_file_t tmp = xbt_new(s_smx_file_t,1);
340 tmp->surf_file = surf_storage_action_get_file(action->io.surf_io);
341 simcall_file_open__set__result(simcall, tmp);
344 case SIMCALL_FILE_CLOSE:
345 xbt_free(simcall_file_close__get__fd(simcall));
346 simcall_file_close__set__result(simcall, 0);
348 case SIMCALL_FILE_WRITE:
349 simcall_file_write__set__result(simcall, surf_action_get_cost(action->io.surf_io));
352 case SIMCALL_FILE_READ:
353 simcall_file_read__set__result(simcall, surf_action_get_cost(action->io.surf_io));
361 switch (surf_action_get_state(action->io.surf_io)) {
363 case SURF_ACTION_FAILED:
364 action->state = SIMIX_FAILED;
367 case SURF_ACTION_DONE:
368 action->state = SIMIX_DONE;
376 SIMIX_io_finish(action);
379 void SIMIX_io_destroy(smx_action_t action)
381 XBT_DEBUG("Destroy action %p", action);
382 if (action->io.surf_io)
383 surf_action_unref(action->io.surf_io);
384 xbt_mallocator_release(simix_global->action_mallocator, action);
387 void SIMIX_io_finish(smx_action_t action)
389 xbt_fifo_item_t item;
390 smx_simcall_t simcall;
392 xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
394 switch (action->state) {
397 /* do nothing, action done */
401 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
405 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
409 xbt_die("Internal error in SIMIX_io_finish: unexpected action state %d",
413 if (surf_resource_get_state(surf_workstation_resource_priv(simcall->issuer->smx_host)) != SURF_RESOURCE_ON) {
414 simcall->issuer->context->iwannadie = 1;
417 simcall->issuer->waiting_action = NULL;
418 SIMIX_simcall_answer(simcall);
421 /* We no longer need it */
422 SIMIX_io_destroy(action);