Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Activity refactoring
[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   actor::ActorImpl* self = actor::ActorImpl::self();
30   if (self) {
31     set_actor(self);
32     self->activities_.emplace_back(this);
33   }
34 }
35
36 IoImpl& IoImpl::set_sharing_penalty(double sharing_penalty)
37 {
38   sharing_penalty_ = sharing_penalty;
39   return *this;
40 }
41
42 IoImpl& IoImpl::update_sharing_penalty(double sharing_penalty)
43 {
44   sharing_penalty_ = sharing_penalty;
45   surf_action_->set_sharing_penalty(sharing_penalty);
46   return *this;
47 }
48
49 IoImpl& IoImpl::set_timeout(double timeout)
50 {
51   const s4u::Host* host = get_disk()->get_host();
52   timeout_detector_     = host->get_cpu()->sleep(timeout);
53   timeout_detector_->set_activity(this);
54   return *this;
55 }
56
57 IoImpl& IoImpl::set_type(s4u::Io::OpType type)
58 {
59   type_ = type;
60   return *this;
61 }
62
63 IoImpl& IoImpl::set_size(sg_size_t size)
64 {
65   size_ = size;
66   return *this;
67 }
68
69 IoImpl& IoImpl::set_disk(resource::DiskImpl* disk)
70 {
71   disk_ = disk;
72   return *this;
73 }
74
75 IoImpl* IoImpl::start()
76 {
77   state_ = State::RUNNING;
78   surf_action_ =
79       disk_->get_host()->get_netpoint()->get_englobing_zone()->get_disk_model()->io_start(disk_, size_, type_);
80   surf_action_->set_sharing_penalty(sharing_penalty_);
81   surf_action_->set_activity(this);
82
83   XBT_DEBUG("Create IO synchro %p %s", this, get_cname());
84
85   return this;
86 }
87
88 void IoImpl::post()
89 {
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       state_ = State::FAILED;
94     else
95       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       state_ = State::TIMEOUT;
100     } else {
101       state_ = State::DONE;
102     }
103   } else {
104     state_ = State::DONE;
105   }
106
107   clean_action();
108   if (timeout_detector_) {
109     timeout_detector_->unref();
110     timeout_detector_ = nullptr;
111   }
112
113   /* Answer all simcalls associated with the synchro */
114   finish();
115 }
116 void IoImpl::set_exception(actor::ActorImpl* issuer)
117 {
118   switch (state_) {
119     case State::FAILED:
120       issuer->context_->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"));
123       break;
124     case State::CANCELED:
125       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "I/O Canceled"));
126       break;
127     case State::TIMEOUT:
128       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
129       break;
130     default:
131       xbt_assert(state_ == State::DONE, "Internal error in IoImpl::finish(): unexpected synchro state %s",
132                  to_c_str(state_));
133   }
134 }
135
136 void IoImpl::finish()
137 {
138   XBT_DEBUG("IoImpl::finish() in state %s", to_c_str(state_));
139   while (not simcalls_.empty()) {
140     smx_simcall_t simcall = simcalls_.front();
141     simcalls_.pop_front();
142
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
145      * simcall */
146
147     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
148       continue;                                 // if process handling comm is killed
149     if (auto* observer = dynamic_cast<kernel::actor::IoWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
150       const auto& ios = observer->get_ios();
151
152       for (auto* io : ios) {
153         io->unregister_simcall(simcall);
154
155         if (simcall->timeout_cb_) {
156           simcall->timeout_cb_->remove();
157           simcall->timeout_cb_ = nullptr;
158         }
159       }
160
161       if (not MC_is_active() && not MC_record_replay_is_active()) {
162         auto element = std::find(ios.begin(), ios.end(), this);
163         int rank     = element != ios.end() ? static_cast<int>(std::distance(ios.begin(), element)) : -1;
164         observer->set_result(rank);
165       }
166     }
167
168     set_exception(simcall->issuer_);
169
170     simcall->issuer_->waiting_synchro_ = nullptr;
171     simcall->issuer_->simcall_answer();
172   }
173 }
174
175 void IoImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<IoImpl*>& ios, double timeout)
176 {
177   if (timeout < 0.0) {
178     issuer->simcall_.timeout_cb_ = nullptr;
179   } else {
180     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &ios]() {
181       issuer->simcall_.timeout_cb_ = nullptr;
182       for (auto* io : ios)
183         io->unregister_simcall(&issuer->simcall_);
184       // default result (-1) is set in actor::IoWaitanySimcall
185       issuer->simcall_answer();
186     });
187   }
188
189   for (auto* io : ios) {
190     /* associate this simcall to the the synchro */
191     io->simcalls_.push_back(&issuer->simcall_);
192
193     /* see if the synchro is already finished */
194     if (io->state_ != State::WAITING && io->state_ != State::RUNNING) {
195       io->finish();
196       break;
197     }
198   }
199 }
200
201 } // namespace activity
202 } // namespace kernel
203 } // namespace simgrid