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");
21 namespace simgrid::kernel::activity {
25 piface_ = new s4u::Io(this);
26 actor::ActorImpl* self = actor::ActorImpl::self();
29 self->activities_.emplace_back(this);
33 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
35 sharing_penalty_ = sharing_penalty;
39 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
41 sharing_penalty_ = sharing_penalty;
42 surf_action_->set_sharing_penalty(sharing_penalty);
46 IoImpl& IoImpl::set_timeout(double timeout)
48 const s4u::Host* host = get_disk()->get_host();
49 timeout_detector_ = host->get_cpu()->sleep(timeout);
50 timeout_detector_->set_activity(this);
54 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
60 IoImpl& IoImpl::set_size(sg_size_t size)
66 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
72 IoImpl* IoImpl::start()
74 set_state(State::RUNNING);
75 surf_action_ = disk_->io_start(size_, type_);
76 surf_action_->set_sharing_penalty(sharing_penalty_);
77 surf_action_->set_activity(this);
78 set_start_time(surf_action_->get_start_time());
80 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
87 performed_ioops_ = surf_action_->get_cost();
88 if (surf_action_->get_state() == resource::Action::State::FAILED) {
89 if (disk_ && not disk_->is_on())
90 set_state(State::FAILED);
92 set_state(State::CANCELED);
93 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
94 if (surf_action_->get_remains() > 0.0) {
95 surf_action_->set_state(resource::Action::State::FAILED);
96 set_state(State::TIMEOUT);
98 set_state(State::DONE);
101 set_state(State::DONE);
105 if (timeout_detector_) {
106 timeout_detector_->unref();
107 timeout_detector_ = nullptr;
110 /* Answer all simcalls associated with the synchro */
113 void IoImpl::set_exception(actor::ActorImpl* issuer)
115 switch (get_state()) {
117 issuer->set_wannadie();
118 static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
119 issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
121 case State::CANCELED:
122 issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
125 issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
128 xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
133 void IoImpl::finish()
135 XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
136 while (not simcalls_.empty()) {
137 actor::Simcall* simcall = simcalls_.front();
138 simcalls_.pop_front();
140 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
141 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
144 if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
145 continue; // if process handling comm is killed
147 handle_activity_waitany(simcall);
149 set_exception(simcall->issuer_);
151 simcall->issuer_->waiting_synchro_ = nullptr;
152 simcall->issuer_->simcall_answer();
156 } // namespace simgrid::kernel::activity