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"
12 #include "simgrid/s4u/Storage.hpp"
13 #include "src/surf/StorageImpl.hpp"
17 #include "src/surf/surf_interface.hpp"
18 #include "smx_private.h"
20 #include "src/kernel/activity/SynchroIo.hpp"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
25 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
27 smx_activity_t synchro = SIMIX_file_read(fd, size, host);
28 synchro->simcalls.push_back(simcall);
29 simcall->issuer->waiting_synchro = synchro;
32 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
34 /* check if the host is active */
36 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
38 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
40 synchro->surf_io = surf_host_read(host, fd->surf_file, size);
42 synchro->surf_io->setData(synchro);
43 XBT_DEBUG("Create io synchro %p", synchro);
49 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
51 smx_activity_t synchro = SIMIX_file_write(fd, size, host);
52 synchro->simcalls.push_back(simcall);
53 simcall->issuer->waiting_synchro = synchro;
56 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
59 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
61 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
63 synchro->surf_io = surf_host_write(host, fd->surf_file, size);
64 synchro->surf_io->setData(synchro);
65 XBT_DEBUG("Create io synchro %p", synchro);
71 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* mount, const char* path, sg_storage_t st)
73 smx_activity_t synchro = SIMIX_file_open(mount, path, st);
74 synchro->simcalls.push_back(simcall);
75 simcall->issuer->waiting_synchro = synchro;
78 smx_activity_t SIMIX_file_open(const char* mount, const char* path, sg_storage_t st)
80 if (st->host()->isOff())
81 THROWF(host_error, 0, "Host %s failed, you cannot call this function", st->host()->cname());
83 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
84 synchro->host = st->host();
85 synchro->surf_io = st->pimpl_->open(mount, path);
86 synchro->surf_io->setData(synchro);
87 XBT_DEBUG("Create io synchro %p", synchro);
93 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
95 smx_activity_t synchro = SIMIX_file_close(fd, host);
96 synchro->simcalls.push_back(simcall);
97 simcall->issuer->waiting_synchro = synchro;
100 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
103 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
105 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
106 synchro->host = host;
107 synchro->surf_io = surf_host_close(host, fd->surf_file);
108 synchro->surf_io->setData(synchro);
109 XBT_DEBUG("Create io synchro %p", synchro);
115 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
118 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
120 int res = surf_host_unlink(host, fd->surf_file);
125 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
127 return SIMIX_file_get_size(simcall->issuer, fd);
130 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
132 sg_host_t host = process->host;
133 return surf_host_get_size(host, fd->surf_file);
136 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
138 return SIMIX_file_tell(simcall->issuer, fd);
141 sg_size_t SIMIX_file_tell(smx_actor_t process, smx_file_t fd)
143 sg_host_t host = process->host;
144 return surf_host_file_tell(host, fd->surf_file);
147 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
149 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
152 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
154 sg_host_t host = process->host;
155 return surf_host_file_seek(host, fd->surf_file, offset, origin);
158 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
160 return SIMIX_file_move(simcall->issuer, file, fullpath);
163 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
165 sg_host_t host = process->host;
166 return surf_host_file_move(host, file->surf_file, fullpath);
169 void SIMIX_io_destroy(smx_activity_t synchro)
171 simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
172 XBT_DEBUG("Destroy synchro %p", synchro.get());
174 io->surf_io->unref();
177 void SIMIX_io_finish(smx_activity_t synchro)
179 for (smx_simcall_t simcall : synchro->simcalls) {
180 switch (synchro->state) {
182 /* do nothing, synchro done */
185 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
188 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
191 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
194 if (simcall->issuer->host->isOff()) {
195 simcall->issuer->context->iwannadie = 1;
198 simcall->issuer->waiting_synchro = nullptr;
199 SIMIX_simcall_answer(simcall);
202 /* We no longer need it */
203 SIMIX_io_destroy(synchro);