1 /* Copyright (c) 2007-2022. 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. */
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
12 #include "src/kernel/activity/IoImpl.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14 #include "src/kernel/actor/SimcallObserver.hpp"
15 #include "src/kernel/resource/CpuImpl.hpp"
16 #include "src/kernel/resource/DiskImpl.hpp"
17 #include "src/mc/mc_replay.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_io, kernel, "Kernel io-related synchronization");
27 piface_ = new s4u::Io(this);
28 actor::ActorImpl* self = actor::ActorImpl::self();
31 self->activities_.emplace_back(this);
35 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
37 sharing_penalty_ = sharing_penalty;
41 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
43 sharing_penalty_ = sharing_penalty;
44 surf_action_->set_sharing_penalty(sharing_penalty);
48 IoImpl& IoImpl::set_timeout(double timeout)
50 const s4u::Host* host = get_disk()->get_host();
51 timeout_detector_ = host->get_cpu()->sleep(timeout);
52 timeout_detector_->set_activity(this);
56 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
62 IoImpl& IoImpl::set_size(sg_size_t size)
68 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
74 IoImpl* IoImpl::start()
76 set_state(State::RUNNING);
78 disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
79 surf_action_->set_sharing_penalty(sharing_penalty_);
80 surf_action_->set_activity(this);
81 set_start_time(surf_action_->get_start_time());
83 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
90 performed_ioops_ = surf_action_->get_cost();
91 if (surf_action_->get_state() == resource::Action::State::FAILED) {
92 if (disk_ && not disk_->is_on())
93 set_state(State::FAILED);
95 set_state(State::CANCELED);
96 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
97 if (surf_action_->get_remains() > 0.0) {
98 surf_action_->set_state(resource::Action::State::FAILED);
99 set_state(State::TIMEOUT);
101 set_state(State::DONE);
104 set_state(State::DONE);
108 if (timeout_detector_) {
109 timeout_detector_->unref();
110 timeout_detector_ = nullptr;
113 /* Answer all simcalls associated with the synchro */
116 void IoImpl::set_exception(actor::ActorImpl* issuer)
118 switch (get_state()) {
120 issuer->set_wannadie();
121 static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
122 issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
124 case State::CANCELED:
125 issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
128 issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
131 xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
136 void IoImpl::finish()
138 XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
139 while (not simcalls_.empty()) {
140 actor::Simcall* simcall = simcalls_.front();
141 simcalls_.pop_front();
143 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
144 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
147 if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
148 continue; // if process handling comm is killed
150 handle_activity_waitany(simcall);
152 set_exception(simcall->issuer_);
154 simcall->issuer_->waiting_synchro_ = nullptr;
155 simcall->issuer_->simcall_answer();
159 } // namespace activity
160 } // namespace kernel
161 } // namespace simgrid