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/FileImpl.hpp"
14 #include "src/surf/StorageImpl.hpp"
18 #include "src/surf/surf_interface.hpp"
19 #include "smx_private.h"
21 #include "src/kernel/activity/SynchroIo.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
26 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
28 smx_activity_t synchro = SIMIX_file_read(fd, size, host);
29 synchro->simcalls.push_back(simcall);
30 simcall->issuer->waiting_synchro = synchro;
33 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
35 /* check if the host is active */
37 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
39 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
41 synchro->surf_io = surf_host_read(host, fd->surf_file, size);
43 synchro->surf_io->setData(synchro);
44 XBT_DEBUG("Create io synchro %p", synchro);
50 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
52 smx_activity_t synchro = SIMIX_file_write(fd, size, host);
53 synchro->simcalls.push_back(simcall);
54 simcall->issuer->waiting_synchro = synchro;
57 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
60 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
62 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
64 synchro->surf_io = surf_host_write(host, fd->surf_file, size);
65 synchro->surf_io->setData(synchro);
66 XBT_DEBUG("Create io synchro %p", synchro);
72 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* mount, const char* path, sg_storage_t st)
74 smx_activity_t synchro = SIMIX_file_open(mount, path, st);
75 synchro->simcalls.push_back(simcall);
76 simcall->issuer->waiting_synchro = synchro;
79 smx_activity_t SIMIX_file_open(const char* mount, const char* path, sg_storage_t st)
81 if (st->host()->isOff())
82 THROWF(host_error, 0, "Host %s failed, you cannot call this function", st->host()->cname());
84 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
85 synchro->host = st->host();
86 synchro->surf_io = st->pimpl_->open(mount, path);
87 synchro->surf_io->setData(synchro);
88 XBT_DEBUG("Create io synchro %p", synchro);
94 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
96 smx_activity_t synchro = SIMIX_file_close(fd, host);
97 synchro->simcalls.push_back(simcall);
98 simcall->issuer->waiting_synchro = synchro;
101 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
104 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
106 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
107 synchro->host = host;
108 synchro->surf_io = surf_host_close(host, fd->surf_file);
109 synchro->surf_io->setData(synchro);
110 XBT_DEBUG("Create io synchro %p", synchro);
116 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
119 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
121 return surf_host_unlink(host, fd->surf_file);
124 sg_size_t SIMIX_file_get_size(smx_file_t fd)
126 return fd->surf_file->size();
129 sg_size_t SIMIX_file_tell(smx_file_t fd)
131 return fd->surf_file->tell();
134 int SIMIX_file_seek(smx_file_t fd, sg_offset_t offset, int origin)
136 return fd->surf_file->seek(offset, origin);
139 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
141 return SIMIX_file_move(simcall->issuer, file, fullpath);
144 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
146 sg_host_t host = process->host;
147 return surf_host_file_move(host, file->surf_file, fullpath);
150 void SIMIX_io_destroy(smx_activity_t synchro)
152 simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
153 XBT_DEBUG("Destroy synchro %p", synchro.get());
155 io->surf_io->unref();
158 void SIMIX_io_finish(smx_activity_t synchro)
160 for (smx_simcall_t simcall : synchro->simcalls) {
161 switch (synchro->state) {
163 /* do nothing, synchro done */
166 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
169 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
172 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
175 if (simcall->issuer->host->isOff()) {
176 simcall->issuer->context->iwannadie = 1;
179 simcall->issuer->waiting_synchro = nullptr;
180 SIMIX_simcall_answer(simcall);
183 /* We no longer need it */
184 SIMIX_io_destroy(synchro);