Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics to please codefactor.io.
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2022. 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/modelchecker.h>
9 #include <simgrid/s4u/Engine.hpp>
10
11 #include "src/kernel/activity/ExecImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/kernel/resource/CpuImpl.hpp"
15 #include "src/mc/mc_replay.hpp"
16 #include "src/surf/HostImpl.hpp"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(ker_actor);
19
20 namespace simgrid {
21 namespace kernel {
22 namespace activity {
23
24 ExecImpl::ExecImpl()
25 {
26   piface_                = new s4u::Exec(this);
27   actor::ActorImpl* self = actor::ActorImpl::self();
28   if (self) {
29     set_actor(self);
30     self->activities_.emplace_back(this);
31   }
32 }
33
34 ExecImpl& ExecImpl::set_host(s4u::Host* host)
35 {
36   hosts_.assign(1, host);
37   return *this;
38 }
39
40 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
41 {
42   hosts_ = hosts;
43   return *this;
44 }
45
46 ExecImpl& ExecImpl::set_timeout(double timeout)
47 {
48   if (timeout >= 0 && not MC_is_active() && not MC_record_replay_is_active()) {
49     timeout_detector_.reset(hosts_.front()->get_cpu()->sleep(timeout));
50     timeout_detector_->set_activity(this);
51   }
52   return *this;
53 }
54
55 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
56 {
57   flops_amounts_.assign(1, flops_amount);
58   return *this;
59 }
60
61 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
62 {
63   flops_amounts_ = flops_amounts;
64   return *this;
65 }
66
67 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
68 {
69   bytes_amounts_ = bytes_amounts;
70
71   return *this;
72 }
73
74 ExecImpl* ExecImpl::start()
75 {
76   state_ = State::RUNNING;
77   if (not MC_is_active() && not MC_record_replay_is_active()) {
78     if (hosts_.size() == 1) {
79       surf_action_ = hosts_.front()->get_cpu()->execution_start(flops_amounts_.front(), bound_);
80       surf_action_->set_sharing_penalty(sharing_penalty_);
81       surf_action_->set_category(get_tracing_category());
82     } else {
83       // get the model from first host since we have only 1 by now
84       auto host_model = hosts_.front()->get_netpoint()->get_englobing_zone()->get_host_model();
85       surf_action_    = host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
86     }
87     surf_action_->set_activity(this);
88     start_time_ = surf_action_->get_start_time();
89   }
90
91   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
92   return this;
93 }
94
95 double ExecImpl::get_remaining() const
96 {
97   if (state_ == State::WAITING || state_ == State::FAILED)
98     return flops_amounts_.front();
99   return ActivityImpl::get_remaining();
100 }
101
102 double ExecImpl::get_seq_remaining_ratio()
103 {
104   if (state_ == State::WAITING)
105     return 1;
106   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
107 }
108
109 double ExecImpl::get_par_remaining_ratio()
110 {
111   // parallel task: their remain is already between 0 and 1
112   if (state_ == State::WAITING)
113     return 1;
114   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
115 }
116
117 ExecImpl& ExecImpl::set_bound(double bound)
118 {
119   bound_ = bound;
120   return *this;
121 }
122
123 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
124 {
125   sharing_penalty_ = sharing_penalty;
126   return *this;
127 }
128
129 ExecImpl& ExecImpl::update_sharing_penalty(double sharing_penalty)
130 {
131   sharing_penalty_ = sharing_penalty;
132   surf_action_->set_sharing_penalty(sharing_penalty);
133   return *this;
134 }
135
136 void ExecImpl::post()
137 {
138   xbt_assert(surf_action_ != nullptr);
139   if (std::any_of(hosts_.begin(), hosts_.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
140     /* If one of the hosts running the synchro failed, notice it. This way, the asking
141      * process can be killed if it runs on that host itself */
142     state_ = State::FAILED;
143   } else if (surf_action_->get_state() == resource::Action::State::FAILED) {
144     /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
145     state_ = State::CANCELED;
146   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
147     if (surf_action_->get_remains() > 0.0) {
148       surf_action_->set_state(resource::Action::State::FAILED);
149       state_ = State::TIMEOUT;
150     } else {
151       state_ = State::DONE;
152     }
153   } else {
154     state_ = State::DONE;
155   }
156
157   clean_action();
158   timeout_detector_.reset();
159   if (get_actor() != nullptr) {
160     get_actor()->activities_.remove(this);
161   }
162   if (state_ != State::FAILED && cb_id_ >= 0)
163     s4u::Host::on_state_change.disconnect(cb_id_);
164   /* Answer all simcalls associated with the synchro */
165   finish();
166 }
167
168 void ExecImpl::set_exception(actor::ActorImpl* issuer)
169 {
170   switch (state_) {
171     case State::FAILED:
172       static_cast<s4u::Exec*>(get_iface())->complete(s4u::Activity::State::FAILED);
173       if (issuer->get_host()->is_on())
174         issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
175       else /* else, the actor will be killed with no possibility to survive */
176         issuer->context_->set_wannadie();
177       break;
178
179     case State::CANCELED:
180       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Execution Canceled"));
181       break;
182
183     case State::TIMEOUT:
184       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
185       break;
186
187     default:
188       xbt_assert(state_ == State::DONE, "Internal error in ExecImpl::finish(): unexpected synchro state %s",
189                  to_c_str(state_));
190   }
191 }
192 void ExecImpl::finish()
193 {
194   XBT_DEBUG("ExecImpl::finish() in state %s", to_c_str(state_));
195   while (not simcalls_.empty()) {
196     smx_simcall_t simcall = simcalls_.front();
197     simcalls_.pop_front();
198
199     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
200       continue;                                 // if process handling comm is killed
201
202     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
203      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
204      * simcall */
205     if (auto* observer = dynamic_cast<actor::ExecutionWaitanySimcall*>(simcall->observer_)) {
206       const auto& execs = observer->get_execs();
207
208       for (auto* exec : execs)
209         exec->unregister_simcall(simcall);
210
211       if (simcall->timeout_cb_) {
212         simcall->timeout_cb_->remove();
213         simcall->timeout_cb_ = nullptr;
214       }
215
216       if (not MC_is_active() && not MC_record_replay_is_active()) {
217         auto element = std::find(execs.begin(), execs.end(), this);
218         int rank     = element != execs.end() ? static_cast<int>(std::distance(execs.begin(), element)) : -1;
219         observer->set_result(rank);
220       }
221     }
222
223     set_exception(simcall->issuer_);
224
225     simcall->issuer_->waiting_synchro_ = nullptr;
226     /* Fail the process if the host is down */
227     if (simcall->issuer_->get_host()->is_on())
228       simcall->issuer_->simcall_answer();
229     else
230       simcall->issuer_->context_->set_wannadie();
231   }
232 }
233
234 void ExecImpl::reset()
235 {
236   hosts_.clear();
237   bytes_amounts_.clear();
238   flops_amounts_.clear();
239   start_time_ = -1.0;
240 }
241
242 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
243 {
244   if (not MC_is_active() && not MC_record_replay_is_active()) {
245     resource::Action* old_action = this->surf_action_;
246     resource::Action* new_action = to->get_cpu()->execution_start(old_action->get_cost(), old_action->get_user_bound());
247     new_action->set_remains(old_action->get_remains());
248     new_action->set_activity(this);
249     new_action->set_sharing_penalty(old_action->get_sharing_penalty());
250     new_action->set_user_bound(old_action->get_user_bound());
251
252     old_action->set_activity(nullptr);
253     old_action->cancel();
254     old_action->unref();
255     this->surf_action_ = new_action;
256   }
257
258   on_migration(*this, to);
259   return this;
260 }
261
262 void ExecImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ExecImpl*>& execs, double timeout)
263 {
264   if (timeout < 0.0) {
265     issuer->simcall_.timeout_cb_ = nullptr;
266   } else {
267     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &execs]() {
268       issuer->simcall_.timeout_cb_ = nullptr;
269       for (auto* exec : execs)
270         exec->unregister_simcall(&issuer->simcall_);
271       // default result (-1) is set in actor::ExecutionWaitanySimcall
272       issuer->simcall_answer();
273     });
274   }
275
276   for (auto* exec : execs) {
277     /* associate this simcall to the the synchro */
278     exec->simcalls_.push_back(&issuer->simcall_);
279     /* see if the synchro is already finished */
280     if (exec->state_ != State::WAITING && exec->state_ != State::RUNNING) {
281       exec->finish();
282       break;
283     }
284   }
285 }
286
287 /*************
288  * Callbacks *
289  *************/
290 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
291
292 } // namespace activity
293 } // namespace kernel
294 } // namespace simgrid