Logo AND Algorithmique Numérique Distribuée

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