1 /* Copyright (c) 2007-2023. 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>
9 #include "src/kernel/activity/IoImpl.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include "src/kernel/resource/DiskImpl.hpp"
12 #include "src/kernel/resource/HostImpl.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_io, kernel, "Kernel io-related synchronization");
16 namespace simgrid::kernel::activity {
20 piface_ = new s4u::Io(this);
21 actor::ActorImpl* self = actor::ActorImpl::self();
24 self->activities_.insert(this);
28 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
30 sharing_penalty_ = sharing_penalty;
34 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
36 sharing_penalty_ = sharing_penalty;
37 model_action_->set_sharing_penalty(sharing_penalty);
41 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
47 IoImpl& IoImpl::set_size(sg_size_t size)
53 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
59 IoImpl& IoImpl::set_host(s4u::Host* host)
65 IoImpl& IoImpl::set_dst_disk(resource::DiskImpl* disk)
71 IoImpl& IoImpl::set_dst_host(s4u::Host* host)
77 IoImpl* IoImpl::start()
79 set_state(State::RUNNING);
80 if (dst_host_ == nullptr) {
81 XBT_DEBUG("Starting regular I/O");
82 model_action_ = disk_->io_start(size_, type_);
83 model_action_->set_sharing_penalty(sharing_penalty_);
85 XBT_DEBUG("Starting streaming I/O");
86 auto host_model = dst_host_->get_netpoint()->get_englobing_zone()->get_host_model();
87 model_action_ = host_model->io_stream(host_, disk_, dst_host_, dst_disk_, size_);
90 model_action_->set_activity(this);
91 set_start_time(model_action_->get_start_time());
93 XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
98 void IoImpl::set_exception(actor::ActorImpl* issuer)
100 switch (get_state()) {
102 issuer->set_wannadie();
103 static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
104 issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
106 case State::CANCELED:
107 issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
109 case State::SRC_HOST_FAILURE:
110 case State::DST_HOST_FAILURE:
111 issuer->set_wannadie();
112 static_cast<s4u::Io*>(get_iface())->complete(s4u::Activity::State::FAILED);
113 issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Host failed"));
116 issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
119 xbt_assert(get_state() == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
124 void IoImpl::finish()
126 XBT_DEBUG("IoImpl::finish() in state %s", get_state_str());
127 if (model_action_ != nullptr) {
128 performed_ioops_ = model_action_->get_cost();
129 if (model_action_->get_state() == resource::Action::State::FAILED) {
130 if (host_ && dst_host_) { // this is an I/O stream
131 if (not host_->is_on())
132 set_state(State::SRC_HOST_FAILURE);
133 else if (not dst_host_->is_on())
134 set_state(State::DST_HOST_FAILURE);
135 } else if ((disk_ && not disk_->is_on()) || (dst_disk_ && not dst_disk_->is_on()))
136 set_state(State::FAILED);
138 set_state(State::CANCELED);
140 set_state(State::DONE);
146 while (not simcalls_.empty()) {
147 actor::Simcall* simcall = simcalls_.front();
148 simcalls_.pop_front();
150 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
151 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
154 if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
155 continue; // if process handling comm is killed
157 handle_activity_waitany(simcall);
159 if (not simcall->issuer_->get_host()->is_on()) {
160 simcall->issuer_->set_wannadie();
162 // Do not answer to dying actors
163 if (not simcall->issuer_->wannadie()) {
164 set_exception(simcall->issuer_);
165 simcall->issuer_->waiting_synchro_ = nullptr;
166 simcall->issuer_->simcall_answer();
172 } // namespace simgrid::kernel::activity