1 /* Copyright (c) 2007-2021. 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/kernel/routing/NetPoint.hpp"
10 #include "simgrid/s4u/Engine.hpp"
11 #include "simgrid/s4u/Host.hpp"
12 #include "simgrid/s4u/Io.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/resource/DiskImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "src/simix/smx_private.hpp"
17 #include "src/surf/cpu_interface.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
27 piface_ = new s4u::Io(this);
30 IoImpl& IoImpl::set_timeout(double timeout)
32 const s4u::Host* host = get_disk()->get_host();
33 timeout_detector_ = host->get_cpu()->sleep(timeout);
34 timeout_detector_->set_activity(this);
38 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
44 IoImpl& IoImpl::set_size(sg_size_t size)
50 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
56 IoImpl* IoImpl::start()
58 state_ = State::RUNNING;
60 disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
61 surf_action_->set_activity(this);
63 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
70 performed_ioops_ = surf_action_->get_cost();
71 if (surf_action_->get_state() == resource::Action::State::FAILED) {
72 if (disk_ && not disk_->is_on())
73 state_ = State::FAILED;
75 state_ = State::CANCELED;
76 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
77 if (surf_action_->get_remains() > 0.0) {
78 surf_action_->set_state(resource::Action::State::FAILED);
79 state_ = State::TIMEOUT;
88 if (timeout_detector_) {
89 timeout_detector_->unref();
90 timeout_detector_ = nullptr;
93 /* Answer all simcalls associated with the synchro */
99 XBT_DEBUG("IoImpl::finish() in state %s", to_c_str(state_));
100 while (not simcalls_.empty()) {
101 smx_simcall_t simcall = simcalls_.front();
102 simcalls_.pop_front();
104 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
105 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
108 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
109 continue; // if process handling comm is killed
110 if (auto* observer = dynamic_cast<kernel::actor::IoWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
111 const auto& ios = observer->get_ios();
113 for (auto* io : ios) {
114 io->unregister_simcall(simcall);
116 if (simcall->timeout_cb_) {
117 simcall->timeout_cb_->remove();
118 simcall->timeout_cb_ = nullptr;
122 if (not MC_is_active() && not MC_record_replay_is_active()) {
123 auto element = std::find(ios.begin(), ios.end(), this);
124 int rank = element != ios.end() ? static_cast<int>(std::distance(ios.begin(), element)) : -1;
125 observer->set_result(rank);
131 simcall->issuer_->context_->set_wannadie();
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 simcall->issuer_->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
142 xbt_assert(state_ == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
146 simcall->issuer_->waiting_synchro_ = nullptr;
147 simcall->issuer_->simcall_answer();
151 void IoImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<IoImpl*>& ios, double timeout)
154 issuer->simcall_.timeout_cb_ = nullptr;
156 issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &ios]() {
157 issuer->simcall_.timeout_cb_ = nullptr;
159 io->unregister_simcall(&issuer->simcall_);
160 // default result (-1) is set in actor::IoWaitanySimcall
161 issuer->simcall_answer();
165 for (auto* io : ios) {
166 /* associate this simcall to the the synchro */
167 io->simcalls_.push_back(&issuer->simcall_);
169 /* see if the synchro is already finished */
170 if (io->state_ != State::WAITING && io->state_ != State::RUNNING) {
177 } // namespace activity
178 } // namespace kernel
179 } // namespace simgrid