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"
18 #include "src/surf/HostImpl.hpp"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_io, kernel, "Kernel io-related synchronization");
22 namespace simgrid::kernel::activity {
26 piface_ = new s4u::Io(this);
27 actor::ActorImpl* self = actor::ActorImpl::self();
30 self->activities_.emplace_back(this);
34 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
36 sharing_penalty_ = sharing_penalty;
40 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
42 sharing_penalty_ = sharing_penalty;
43 surf_action_->set_sharing_penalty(sharing_penalty);
47 IoImpl& IoImpl::set_timeout(double timeout)
49 const s4u::Host* host = get_disk()->get_host();
50 timeout_detector_ = host->get_cpu()->sleep(timeout);
51 timeout_detector_->set_activity(this);
55 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
61 IoImpl& IoImpl::set_size(sg_size_t size)
67 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
73 IoImpl& IoImpl::set_host(s4u::Host* host)
79 IoImpl& IoImpl::set_dst_disk(resource::DiskImpl* disk)
85 IoImpl& IoImpl::set_dst_host(s4u::Host* host)
91 IoImpl* IoImpl::start()
93 set_state(State::RUNNING);
94 if (dst_host_ == nullptr) {
95 XBT_DEBUG("Starting regular I/O");
96 surf_action_ = disk_->io_start(size_, type_);
97 surf_action_->set_sharing_penalty(sharing_penalty_);
99 XBT_DEBUG("Starting streaming I/O");
100 auto host_model = dst_host_->get_netpoint()->get_englobing_zone()->get_host_model();
101 surf_action_ = host_model->io_stream(host_, disk_, dst_host_, dst_disk_, size_);
104 surf_action_->set_activity(this);
105 set_start_time(surf_action_->get_start_time());
106 #include "src/surf/HostImpl.hpp"
108 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
115 performed_ioops_ = surf_action_->get_cost();
116 if (surf_action_->get_state() == resource::Action::State::FAILED) {
117 if (disk_ && not disk_->is_on())
118 set_state(State::FAILED);
120 set_state(State::CANCELED);
121 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
122 if (surf_action_->get_remains() > 0.0) {
123 surf_action_->set_state(resource::Action::State::FAILED);
124 set_state(State::TIMEOUT);
126 set_state(State::DONE);
129 set_state(State::DONE);
133 if (timeout_detector_) {
134 timeout_detector_->unref();
135 timeout_detector_ = nullptr;
138 /* Answer all simcalls associated with the synchro */
141 void IoImpl::set_exception(actor::ActorImpl* issuer)
143 switch (get_state()) {
145 issuer->set_wannadie();
146 static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
147 issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
149 case State::CANCELED:
150 issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
153 issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
156 xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
161 void IoImpl::finish()
163 XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
164 while (not simcalls_.empty()) {
165 actor::Simcall* simcall = simcalls_.front();
166 simcalls_.pop_front();
168 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
169 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
172 if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
173 continue; // if process handling comm is killed
175 handle_activity_waitany(simcall);
177 set_exception(simcall->issuer_);
179 simcall->issuer_->waiting_synchro_ = nullptr;
180 simcall->issuer_->simcall_answer();
184 } // namespace simgrid::kernel::activity