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 void simcall_HANDLER_file_read(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
25 smx_activity_t synchro = SIMIX_file_read(fd, size, host);
26 synchro->simcalls.push_back(simcall);
27 simcall->issuer->waiting_synchro = synchro;
30 smx_activity_t SIMIX_file_read(smx_file_t fd, sg_size_t size, sg_host_t host)
32 /* check if the host is active */
34 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
36 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
38 synchro->surf_io = surf_host_read(host, fd->surf_file, size);
40 synchro->surf_io->setData(synchro);
41 XBT_DEBUG("Create io synchro %p", synchro);
47 void simcall_HANDLER_file_write(smx_simcall_t simcall, smx_file_t fd, sg_size_t size, sg_host_t host)
49 smx_activity_t synchro = SIMIX_file_write(fd, size, host);
50 synchro->simcalls.push_back(simcall);
51 simcall->issuer->waiting_synchro = synchro;
54 smx_activity_t SIMIX_file_write(smx_file_t fd, sg_size_t size, sg_host_t host)
57 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
59 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
61 synchro->surf_io = surf_host_write(host, fd->surf_file, size);
62 synchro->surf_io->setData(synchro);
63 XBT_DEBUG("Create io synchro %p", synchro);
69 void simcall_HANDLER_file_open(smx_simcall_t simcall, const char* fullpath, sg_host_t host)
71 smx_activity_t synchro = SIMIX_file_open(fullpath, host);
72 synchro->simcalls.push_back(simcall);
73 simcall->issuer->waiting_synchro = synchro;
76 smx_activity_t SIMIX_file_open(const char* fullpath, sg_host_t host)
79 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
81 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
83 synchro->surf_io = surf_host_open(host, fullpath);
84 synchro->surf_io->setData(synchro);
85 XBT_DEBUG("Create io synchro %p", synchro);
91 void simcall_HANDLER_file_close(smx_simcall_t simcall, smx_file_t fd, sg_host_t host)
93 smx_activity_t synchro = SIMIX_file_close(fd, host);
94 synchro->simcalls.push_back(simcall);
95 simcall->issuer->waiting_synchro = synchro;
98 smx_activity_t SIMIX_file_close(smx_file_t fd, sg_host_t host)
101 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
103 simgrid::kernel::activity::Io *synchro = new simgrid::kernel::activity::Io();
104 synchro->host = host;
105 synchro->surf_io = surf_host_close(host, fd->surf_file);
106 synchro->surf_io->setData(synchro);
107 XBT_DEBUG("Create io synchro %p", synchro);
113 int SIMIX_file_unlink(smx_file_t fd, sg_host_t host)
116 THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->cname());
118 int res = surf_host_unlink(host, fd->surf_file);
123 sg_size_t simcall_HANDLER_file_get_size(smx_simcall_t simcall, smx_file_t fd)
125 return SIMIX_file_get_size(simcall->issuer, fd);
128 sg_size_t SIMIX_file_get_size(smx_actor_t process, smx_file_t fd)
130 sg_host_t host = process->host;
131 return surf_host_get_size(host, fd->surf_file);
134 sg_size_t simcall_HANDLER_file_tell(smx_simcall_t simcall, smx_file_t fd)
136 return SIMIX_file_tell(simcall->issuer, fd);
139 sg_size_t SIMIX_file_tell(smx_actor_t process, smx_file_t fd)
141 sg_host_t host = process->host;
142 return surf_host_file_tell(host, fd->surf_file);
146 xbt_dynar_t simcall_HANDLER_file_get_info(smx_simcall_t simcall, smx_file_t fd)
148 return SIMIX_file_get_info(simcall->issuer, fd);
151 xbt_dynar_t SIMIX_file_get_info(smx_actor_t process, smx_file_t fd)
153 sg_host_t host = process->host;
154 return surf_host_get_info(host, fd->surf_file);
157 int simcall_HANDLER_file_seek(smx_simcall_t simcall, smx_file_t fd, sg_offset_t offset, int origin)
159 return SIMIX_file_seek(simcall->issuer, fd, offset, origin);
162 int SIMIX_file_seek(smx_actor_t process, smx_file_t fd, sg_offset_t offset, int origin)
164 sg_host_t host = process->host;
165 return surf_host_file_seek(host, fd->surf_file, offset, origin);
168 int simcall_HANDLER_file_move(smx_simcall_t simcall, smx_file_t file, const char* fullpath)
170 return SIMIX_file_move(simcall->issuer, file, fullpath);
173 int SIMIX_file_move(smx_actor_t process, smx_file_t file, const char* fullpath)
175 sg_host_t host = process->host;
176 return surf_host_file_move(host, file->surf_file, fullpath);
179 xbt_dict_t SIMIX_storage_get_properties(smx_storage_t storage){
180 return surf_storage_get_properties(storage);
183 void SIMIX_io_destroy(smx_activity_t synchro)
185 simgrid::kernel::activity::Io *io = static_cast<simgrid::kernel::activity::Io*>(synchro);
186 XBT_DEBUG("Destroy synchro %p", synchro);
188 io->surf_io->unref();
192 void SIMIX_io_finish(smx_activity_t synchro)
194 for (smx_simcall_t simcall : synchro->simcalls) {
195 switch (synchro->state) {
197 /* do nothing, synchro done */
200 SMX_EXCEPTION(simcall->issuer, io_error, 0, "IO failed");
203 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
206 xbt_die("Internal error in SIMIX_io_finish: unexpected synchro state %d", static_cast<int>(synchro->state));
209 if (simcall->issuer->host->isOff()) {
210 simcall->issuer->context->iwannadie = 1;
213 simcall->issuer->waiting_synchro = nullptr;
214 SIMIX_simcall_answer(simcall);
217 /* We no longer need it */
218 SIMIX_io_destroy(synchro);