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"
15 #include "surf/surf.h"
19 #include "src/surf/surf_interface.hpp"
20 #include "smx_private.h"
22 #include "src/kernel/activity/SynchroIo.hpp"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
27 void simcall_HANDLER_file_read(smx_simcall_t simcall, surf_file_t fd, sg_size_t size, sg_host_t host)
29 smx_activity_t synchro = SIMIX_file_read(fd, size, host);
30 synchro->simcalls.push_back(simcall);
31 simcall->issuer->waiting_synchro = synchro;
34 smx_activity_t SIMIX_file_read(surf_file_t file, sg_size_t size, sg_host_t host)
36 /* check if the host is active */
38 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
40 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
42 synchro->surf_io = surf_host_read(host, file, size);
44 synchro->surf_io->setData(synchro);
45 XBT_DEBUG("Create io synchro %p", synchro);
51 void simcall_HANDLER_file_write(smx_simcall_t simcall, surf_file_t fd, sg_size_t size, sg_host_t host)
53 smx_activity_t synchro = SIMIX_file_write(fd, size, host);
54 synchro->simcalls.push_back(simcall);
55 simcall->issuer->waiting_synchro = synchro;
58 smx_activity_t SIMIX_file_write(surf_file_t file, sg_size_t size, sg_host_t host)
61 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
63 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
65 synchro->surf_io = surf_host_write(host, file, size);
66 synchro->surf_io->setData(synchro);
67 XBT_DEBUG("Create io synchro %p", synchro);
73 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* mount, const char* path, sg_storage_t st)
75 smx_activity_t synchro = SIMIX_file_open(mount, path, st);
76 synchro->simcalls.push_back(simcall);
77 simcall->issuer->waiting_synchro = synchro;
80 smx_activity_t SIMIX_file_open(const char* mount, const char* path, sg_storage_t st)
82 if (st->getHost()->isOff())
83 THROWF(host_error, 0, "Host %s failed, you cannot call this function", st->getHost()->getCname());
85 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
86 synchro->host = st->getHost();
87 synchro->surf_io = st->pimpl_->open(mount, path);
88 synchro->surf_io->setData(synchro);
89 XBT_DEBUG("Create io synchro %p", synchro);
95 void simcall_HANDLER_file_close(smx_simcall_t simcall, surf_file_t fd, sg_host_t host)
97 smx_activity_t synchro = SIMIX_file_close(fd, host);
98 synchro->simcalls.push_back(simcall);
99 simcall->issuer->waiting_synchro = synchro;
102 smx_activity_t SIMIX_file_close(surf_file_t file, sg_host_t host)
105 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
107 simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
108 synchro->host = host;
109 synchro->surf_io = surf_host_close(host, file);
110 synchro->surf_io->setData(synchro);
111 XBT_DEBUG("Create io synchro %p", synchro);
116 void SIMIX_io_destroy(smx_activity_t synchro)
118 simgrid::kernel::activity::IoImplPtr io = boost::static_pointer_cast<simgrid::kernel::activity::IoImpl>(synchro);
119 XBT_DEBUG("Destroy synchro %p", synchro.get());
121 io->surf_io->unref();
124 void SIMIX_io_finish(smx_activity_t synchro)
126 for (smx_simcall_t simcall : synchro->simcalls) {
127 switch (synchro->state) {
129 /* do nothing, synchro done */
132 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
135 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
138 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
141 if (simcall->issuer->host->isOff()) {
142 simcall->issuer->context->iwannadie = 1;
145 simcall->issuer->waiting_synchro = nullptr;
146 SIMIX_simcall_answer(simcall);
149 /* We no longer need it */
150 SIMIX_io_destroy(synchro);