1 /* Copyright (c) 2007-2019. 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 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state_);
22 /* Associate this simcall to the synchro */
23 synchro->register_simcall(simcall);
25 if (MC_is_active() || MC_record_replay_is_active()) {
26 int idx = SIMCALL_GET_MC_VALUE(*simcall);
28 synchro->state_ = simgrid::kernel::activity::State::DONE;
30 /* If we reached this point, the wait simcall must have a timeout */
31 /* Otherwise it shouldn't be enabled and executed by the MC */
34 synchro->state_ = simgrid::kernel::activity::State::TIMEOUT;
39 /* If the synchro is already finished then perform the error handling */
40 if (synchro->state_ != simgrid::kernel::activity::State::RUNNING)
43 /* we need a sleep action (even when there is no timeout) to be notified of host failures */
44 if (synchro->get_disk() != nullptr)
45 synchro->timeout_detector_ = synchro->get_disk()->get_host()->pimpl_cpu->sleep(timeout);
47 synchro->timeout_detector_ =
48 simgrid::s4u::Host::by_name(synchro->get_storage()->get_host())->pimpl_cpu->sleep(timeout);
49 synchro->timeout_detector_->set_activity(synchro);
57 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
63 IoImpl& IoImpl::set_size(sg_size_t size)
69 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
75 IoImpl& IoImpl::set_storage(resource::StorageImpl* storage)
81 IoImpl* IoImpl::start()
83 state_ = State::RUNNING;
85 surf_action_ = storage_->io_start(size_, type_);
87 surf_action_ = disk_->io_start(size_, type_);
88 surf_action_->set_activity(this);
90 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
91 IoImpl::on_start(*this);
98 performed_ioops_ = surf_action_->get_cost();
99 if (surf_action_->get_state() == resource::Action::State::FAILED) {
100 if ((storage_ && not storage_->is_on()) || (disk_ && not disk_->is_on()))
101 state_ = State::FAILED;
103 state_ = State::CANCELED;
104 } else if (surf_action_->get_state() == resource::Action::State::FINISHED) {
105 state_ = State::DONE;
106 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
107 state_ = State::TIMEOUT;
110 if (timeout_detector_) {
111 timeout_detector_->unref();
112 timeout_detector_ = nullptr;
115 on_completion(*this);
117 /* Answer all simcalls associated with the synchro */
121 void IoImpl::finish()
123 while (not simcalls_.empty()) {
124 smx_simcall_t simcall = simcalls_.front();
125 simcalls_.pop_front();
128 /* do nothing, synchro done */
131 simcall->issuer_->context_->iwannadie = true;
132 simcall->issuer_->exception_ =
133 std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
135 case State::CANCELED:
136 simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
139 XBT_DEBUG("IoImpl::finish(): execution timeouted");
140 simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
143 xbt_die("Internal error in IoImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
146 simcall->issuer_->waiting_synchro = nullptr;
147 simcall->issuer_->simcall_answer();
154 xbt::signal<void(IoImpl const&)> IoImpl::on_start;
155 xbt::signal<void(IoImpl const&)> IoImpl::on_completion;
157 } // namespace activity
158 } // namespace kernel
159 } // namespace simgrid