Logo AND Algorithmique Numérique Distribuée

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