1 /* Copyright (c) 2007-2010, 2012-2015. 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 "src/surf/surf_interface.hpp"
8 #include "smx_private.h"
10 #include "xbt/sysdep.h"
15 #include "src/simix/SynchroIo.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
21 * \brief Internal function to create a SIMIX storage.
22 * \param name name of the storage to create
23 * \param storage the SURF storage to encapsulate
24 * \param data some user data (may be nullptr)
26 smx_storage_t SIMIX_storage_create(const char *name, void *storage, void *data)
28 smx_storage_priv_t smx_storage = xbt_new0(s_smx_storage_priv_t, 1);
30 smx_storage->data = data;
32 /* Update global variables */
33 xbt_lib_set(storage_lib,name,SIMIX_STORAGE_LEVEL,smx_storage);
34 return xbt_lib_get_elm_or_null(storage_lib, name);
38 * \brief Internal function to destroy a SIMIX storage.
40 * \param s the host to destroy (a smx_storage_t)
42 void SIMIX_storage_destroy(void *s)
44 smx_storage_priv_t storage = (smx_storage_priv_t) s;
46 xbt_assert((storage != nullptr), "Invalid parameters");
50 /* Clean storage structure */
55 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
57 smx_synchro_t synchro = SIMIX_file_read(fd, size, host);
58 synchro->simcalls.push_back(simcall);
59 simcall->issuer->waiting_synchro = synchro;
62 smx_synchro_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
64 /* check if the host is active */
66 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
69 simgrid::simix::Io *synchro = new simgrid::simix::Io();
71 synchro->surf_io = surf_host_read(host, fd->surf_file, size);
73 synchro->surf_io->setData(synchro);
74 XBT_DEBUG("Create io synchro %p", synchro);
80 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
82 smx_synchro_t synchro = SIMIX_file_write(fd, size, host);
83 synchro->simcalls.push_back(simcall);
84 simcall->issuer->waiting_synchro = synchro;
87 smx_synchro_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
90 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
92 simgrid::simix::Io *synchro = new simgrid::simix::Io();
94 synchro->surf_io = surf_host_write(host, fd->surf_file, size);
95 synchro->surf_io->setData(synchro);
96 XBT_DEBUG("Create io synchro %p", synchro);
102 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
104 smx_synchro_t synchro = SIMIX_file_open(fullpath, host);
105 synchro->simcalls.push_back(simcall);
106 simcall->issuer->waiting_synchro = synchro;
109 smx_synchro_t SIMIX_file_open(const char* fullpath, sg_host_t host)
112 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
114 simgrid::simix::Io *synchro = new simgrid::simix::Io();
115 synchro->host = host;
116 synchro->surf_io = surf_host_open(host, fullpath);
117 synchro->surf_io->setData(synchro);
118 XBT_DEBUG("Create io synchro %p", synchro);
124 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
126 smx_synchro_t synchro = SIMIX_file_close(fd, host);
127 synchro->simcalls.push_back(simcall);
128 simcall->issuer->waiting_synchro = synchro;
131 smx_synchro_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
134 THROWF(host_error, 0, "Host %s failed, you cannot call this function", sg_host_get_name(host));
136 simgrid::simix::Io *synchro = new simgrid::simix::Io();
137 synchro->host = host;
138 synchro->surf_io = surf_host_close(host, fd->surf_file);
139 synchro->surf_io->setData(synchro);
140 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", sg_host_get_name(host));
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_process_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_process_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_process_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_process_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_process_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 sg_size_t SIMIX_storage_get_size(smx_storage_t storage){
214 return surf_storage_get_size(storage);
217 sg_size_t simcall_HANDLER_storage_get_free_size(smx_simcall_t simcall, smx_storage_t storage)
219 return SIMIX_storage_get_free_size(simcall->issuer, storage);
222 sg_size_t SIMIX_storage_get_free_size(smx_process_t process, smx_storage_t storage)
224 return surf_storage_get_free_size(storage);
227 sg_size_t simcall_HANDLER_storage_get_used_size(smx_simcall_t simcall, smx_storage_t storage)
229 return SIMIX_storage_get_used_size(simcall->issuer, storage);
232 sg_size_t SIMIX_storage_get_used_size(smx_process_t process, smx_storage_t storage)
234 return surf_storage_get_used_size(storage);
237 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
238 return surf_storage_get_properties(storage);
241 const char* SIMIX_storage_get_name(smx_storage_t storage){
242 return sg_storage_name(storage);
245 xbt_dict_t SIMIX_storage_get_content(smx_storage_t storage){
246 return surf_storage_get_content(storage);
249 const char* SIMIX_storage_get_host(smx_storage_t storage){
250 return surf_storage_get_host(storage);
253 void SIMIX_post_io(smx_synchro_t synchro)
257 void SIMIX_io_destroy(smx_synchro_t synchro)
259 simgrid::simix::Io *io = static_cast<simgrid::simix::Io*>(synchro);
260 XBT_DEBUG("Destroy synchro %p", synchro);
262 io->surf_io->unref();
266 void SIMIX_io_finish(smx_synchro_t synchro)
268 for (smx_simcall_t simcall : synchro->simcalls) {
270 switch (synchro->state) {
273 /* do nothing, synchro done */
277 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
281 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
285 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d",
286 (int)synchro->state);
289 if (simcall->issuer->host->isOff()) {
290 simcall->issuer->context->iwannadie = 1;
293 simcall->issuer->waiting_synchro = nullptr;
294 SIMIX_simcall_answer(simcall);
297 /* We no longer need it */
298 SIMIX_io_destroy(synchro);