Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
refactor exception handling for activities
[simgrid.git] / src / kernel / activity / IoImpl.cpp
1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
2
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
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
10
11 #include "mc/mc.h"
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/context/Context.hpp"
16 #include "src/kernel/resource/CpuImpl.hpp"
17 #include "src/kernel/resource/DiskImpl.hpp"
18 #include "src/mc/mc_replay.hpp"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_io, simix, "Logging specific to SIMIX (io)");
21
22 namespace simgrid {
23 namespace kernel {
24 namespace activity {
25
26 IoImpl::IoImpl()
27 {
28   piface_ = new s4u::Io(this);
29 }
30
31 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
32 {
33   sharing_penalty_ = sharing_penalty;
34   return *this;
35 }
36
37 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
38 {
39   sharing_penalty_ = sharing_penalty;
40   surf_action_->set_sharing_penalty(sharing_penalty);
41   return *this;
42 }
43
44 IoImpl& IoImpl::set_timeout(double timeout)
45 {
46   const s4u::Host* host = get_disk()->get_host();
47   timeout_detector_     = host->get_cpu()->sleep(timeout);
48   timeout_detector_->set_activity(this);
49   return *this;
50 }
51
52 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
53 {
54   type_ = type;
55   return *this;
56 }
57
58 IoImpl& IoImpl::set_size(sg_size_t size)
59 {
60   size_ = size;
61   return *this;
62 }
63
64 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
65 {
66   disk_ = disk;
67   return *this;
68 }
69
70 IoImpl* IoImpl::start()
71 {
72   state_ = State::RUNNING;
73   surf_action_ =
74       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
75   surf_action_->set_sharing_penalty(sharing_penalty_);
76   surf_action_->set_activity(this);
77
78   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
79
80   return this;
81 }
82
83 void IoImpl::post()
84 {
85   performed_ioops_ = surf_action_->get_cost();
86   if (surf_action_->get_state() == resource::Action::State::FAILED) {
87     if (disk_ && not disk_->is_on())
88       state_ = State::FAILED;
89     else
90       state_ = State::CANCELED;
91   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
92     if (surf_action_->get_remains() > 0.0) {
93       surf_action_->set_state(resource::Action::State::FAILED);
94       state_ = State::TIMEOUT;
95     } else {
96       state_ = State::DONE;
97     }
98   } else {
99     state_ = State::DONE;
100   }
101
102   clean_action();
103   if (timeout_detector_) {
104     timeout_detector_->unref();
105     timeout_detector_ = nullptr;
106   }
107
108   /* Answer all simcalls associated with the synchro */
109   finish();
110 }
111 void IoImpl::set_exception(actor::ActorImpl* issuer)
112 {
113   switch (state_) {
114     case State::FAILED:
115       issuer->context_->set_wannadie();
116       piface_->complete(s4u::Activity::State::FAILED);
117       issuer->exception_ = std::make_exception_ptr(StorageFailureException(XBT_THROW_POINT, "Storage failed"));
118       break;
119     case State::CANCELED:
120       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
121       break;
122     case State::TIMEOUT:
123       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
124       break;
125     default:
126       xbt_assert(state_ == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
127                  to_c_str(state_));
128   }
129 }
130
131 void IoImpl::finish()
132 {
133   XBT_DEBUG("IoImpl::finish() in state %s", to_c_str(state_));
134   while (not simcalls_.empty()) {
135     smx_simcall_t simcall = simcalls_.front();
136     simcalls_.pop_front();
137
138     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
139      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
140      * simcall */
141
142     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
143       continue;                                 // if process handling comm is killed
144     if (auto* observer = dynamic_cast<kernel::actor::IoWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
145       const auto& ios = observer->get_ios();
146
147       for (auto* io : ios) {
148         io->unregister_simcall(simcall);
149
150         if (simcall->timeout_cb_) {
151           simcall->timeout_cb_->remove();
152           simcall->timeout_cb_ = nullptr;
153         }
154       }
155
156       if (not MC_is_active() && not MC_record_replay_is_active()) {
157         auto element = std::find(ios.begin(), ios.end(), this);
158         int rank     = element != ios.end() ? static_cast<int>(std::distance(ios.begin(), element)) : -1;
159         observer->set_result(rank);
160       }
161     }
162
163     set_exception(simcall->issuer_);
164
165     simcall->issuer_->waiting_synchro_ = nullptr;
166     simcall->issuer_->simcall_answer();
167   }
168 }
169
170 void IoImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<IoImpl*>& ios, double timeout)
171 {
172   if (timeout < 0.0) {
173     issuer->simcall_.timeout_cb_ = nullptr;
174   } else {
175     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &ios]() {
176       issuer->simcall_.timeout_cb_ = nullptr;
177       for (auto* io : ios)
178         io->unregister_simcall(&issuer->simcall_);
179       // default result (-1) is set in actor::IoWaitanySimcall
180       issuer->simcall_answer();
181     });
182   }
183
184   for (auto* io : ios) {
185     /* associate this simcall to the the synchro */
186     io->simcalls_.push_back(&issuer->simcall_);
187
188     /* see if the synchro is already finished */
189     if (io->state_ != State::WAITING && io->state_ != State::RUNNING) {
190       io->finish();
191       break;
192     }
193   }
194 }
195
196 } // namespace activity
197 } // namespace kernel
198 } // namespace simgrid