1 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include <xbt/sysdep.h>
11 #include "simgrid/s4u/Host.hpp"
15 #include "src/surf/surf_interface.hpp"
16 #include "smx_private.h"
18 #include "src/kernel/activity/SynchroIo.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
23 * \brief Internal function to create a SIMIX storage.
24 * \param name name of the storage to create
25 * \param storage the SURF storage to encapsulate
26 * \param data some user data (may be nullptr)
28 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
30 smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
32 smx_storage->data = data;
34 /* Update global variables */
35 xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
36 return xbt_lib_get_elm_or_null(storage_lib, name);
40 * \brief Internal function to destroy a SIMIX storage.
42 * \param s the host to destroy (a smx_storage_t)
44 void SIMIX_storage_destroy(void *s)
46 smx_storage_priv_t storage = static_cast<smx_storage_priv_t>(s);
48 xbt_assert((storage != nullptr), "Invalid parameters");
52 /* Clean storage structure */
57 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
59 smx_activity_t synchro = SIMIX_file_read(fd, size, host);
60 synchro->simcalls.push_back(simcall);
61 simcall->issuer->waiting_synchro = synchro;
64 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
66 /* check if the host is active */
68 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
70 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
72 synchro->surf_io = surf_host_read(host, fd->surf_file, size);
74 synchro->surf_io->setData(synchro);
75 XBT_DEBUG("Create io synchro %p", synchro);
81 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
83 smx_activity_t synchro = SIMIX_file_write(fd, size, host);
84 synchro->simcalls.push_back(simcall);
85 simcall->issuer->waiting_synchro = synchro;
88 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
91 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
93 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
95 synchro->surf_io = surf_host_write(host, fd->surf_file, size);
96 synchro->surf_io->setData(synchro);
97 XBT_DEBUG("Create io synchro %p", synchro);
103 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
105 smx_activity_t synchro = SIMIX_file_open(fullpath, host);
106 synchro->simcalls.push_back(simcall);
107 simcall->issuer->waiting_synchro = synchro;
110 smx_activity_t SIMIX_file_open(const char* fullpath, sg_host_t host)
113 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
115 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
116 synchro->host = host;
117 synchro->surf_io = surf_host_open(host, fullpath);
118 synchro->surf_io->setData(synchro);
119 XBT_DEBUG("Create io synchro %p", synchro);
125 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
127 smx_activity_t synchro = SIMIX_file_close(fd, host);
128 synchro->simcalls.push_back(simcall);
129 simcall->issuer->waiting_synchro = synchro;
132 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
135 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
137 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
138 synchro->host = host;
139 synchro->surf_io = surf_host_close(host, fd->surf_file);
140 synchro->surf_io->setData(synchro);
141 XBT_DEBUG("Create io synchro %p", synchro);
147 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
150 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
152 int res = surf_host_unlink(host, fd->surf_file);
157 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
159 return SIMIX_file_get_size(simcall->issuer, fd);
162 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
164 sg_host_t host = process->host;
165 return surf_host_get_size(host, fd->surf_file);
168 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
170 return SIMIX_file_tell(simcall->issuer, fd);
173 sg_size_t SIMIX_file_tell(smx_actor_t process, smx_file_t fd)
175 sg_host_t host = process->host;
176 return surf_host_file_tell(host, fd->surf_file);
180 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
182 return SIMIX_file_get_info(simcall->issuer, fd);
185 xbt_dynar_t SIMIX_file_get_info(smx_actor_t process, smx_file_t fd)
187 sg_host_t host = process->host;
188 return surf_host_get_info(host, fd->surf_file);
191 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
193 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
196 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
198 sg_host_t host = process->host;
199 return surf_host_file_seek(host, fd->surf_file, offset, origin);
202 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
204 return SIMIX_file_move(simcall->issuer, file, fullpath);
207 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
209 sg_host_t host = process->host;
210 return surf_host_file_move(host, file->surf_file, fullpath);
213 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
214 return surf_storage_get_properties(storage);
217 const char* SIMIX_storage_get_name(smx_storage_t storage){
218 return sg_storage_name(storage);
221 void SIMIX_io_destroy(smx_activity_t synchro)
223 simgrid::kernel::activity::Io *io = static_cast<simgrid::kernel::activity::Io*>(synchro);
224 XBT_DEBUG("Destroy synchro %p", synchro);
226 io->surf_io->unref();
230 void SIMIX_io_finish(smx_activity_t synchro)
232 for (smx_simcall_t simcall : synchro->simcalls) {
233 switch (synchro->state) {
235 /* do nothing, synchro done */
238 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
241 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
244 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
247 if (simcall->issuer->host->isOff()) {
248 simcall->issuer->context->iwannadie = 1;
251 simcall->issuer->waiting_synchro = nullptr;
252 SIMIX_simcall_answer(simcall);
255 /* We no longer need it */
256 SIMIX_io_destroy(synchro);