1 /* Copyright (c) 2007-2020. 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. */
5 #include "src/kernel/activity/IoImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/kernel/resource/Action.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "src/kernel/resource/DiskImpl.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/simix/smx_private.hpp"
13 #include "src/surf/StorageImpl.hpp"
14 #include "src/surf/cpu_interface.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
18 void simcall_HANDLER_io_wait(smx_simcall_t simcall, simgrid::kernel::activity::IoImpl* synchro, double timeout)
20 synchro->wait_for(simcall->issuer_, timeout);
27 IoImpl& IoImpl::set_timeout(double timeout)
29 s4u::Host* host = get_disk() ? get_disk()->get_host() : s4u::Host::by_name(get_storage()->get_host());
30 timeout_detector_ = host->pimpl_cpu->sleep(timeout);
31 timeout_detector_->set_activity(this);
35 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
41 IoImpl& IoImpl::set_size(sg_size_t size)
47 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
53 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
59 IoImpl* IoImpl::start()
61 state_ = State::RUNNING;
63 surf_action_ = storage_->io_start(size_, type_);
65 surf_action_ = disk_->io_start(size_, type_);
66 surf_action_->set_activity(this);
68 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
69 IoImpl::on_start(*this);
74 void IoImpl::wait_for(actor::ActorImpl* issuer, double timeout)
76 XBT_DEBUG("Wait for execution of synchro %p, state %d", this, (int)state_);
78 /* Associate this simcall to the synchro */
79 register_simcall(&issuer->simcall);
81 if (MC_is_active() || MC_record_replay_is_active()) {
82 int idx = SIMCALL_GET_MC_VALUE(issuer->simcall);
84 state_ = simgrid::kernel::activity::State::DONE;
86 /* If we reached this point, the wait simcall must have a timeout */
87 /* Otherwise it shouldn't be enabled and executed by the MC */
90 state_ = simgrid::kernel::activity::State::TIMEOUT;
96 /* If the synchro is already finished then perform the error handling */
97 if (state_ != simgrid::kernel::activity::State::RUNNING)
100 /* we need a sleep action (even when there is no timeout) to be notified of host failures */
101 set_timeout(timeout);
107 performed_ioops_ = surf_action_->get_cost();
108 if (surf_action_->get_state() == resource::Action::State::FAILED) {
109 if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
110 state_ = State::FAILED;
112 state_ = State::CANCELED;
113 } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
114 state_ = State::DONE;
115 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
116 state_ = State::TIMEOUT;
119 if (timeout_detector_) {
120 timeout_detector_->unref();
121 timeout_detector_ = nullptr;
124 on_completion(*this);
126 /* Answer all simcalls associated with the synchro */
130 void IoImpl::finish()
132 while (not simcalls_.empty()) {
133 const s_smx_simcall* simcall = simcalls_.front();
134 simcalls_.pop_front();
137 /* do nothing, synchro done */
140 simcall->issuer_->context_->set_wannadie();
141 simcall->issuer_->exception_ =
142 std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
144 case State::CANCELED:
145 simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
148 XBT_DEBUG("IoImpl::finish(): execution timeouted");
149 simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
152 xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
155 simcall->issuer_->waiting_synchro = nullptr;
156 simcall->issuer_->simcall_answer();
163 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
164 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
166 } // namespace activity
167 } // namespace kernel
168 } // namespace simgrid