Logo AND Algorithmique Numérique Distribuée

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