Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[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_test(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro)
53 {
54   bool res = (synchro->state_ != simgrid::kernel::activity::State::WAITING &&
55               synchro->state_ != simgrid::kernel::activity::State::RUNNING);
56   if (res) {
57     synchro->simcalls_.push_back(simcall);
58     synchro->finish();
59   } else {
60     simcall->issuer_->simcall_answer();
61   }
62   simcall_execution_test__set__result(simcall, res);
63 }
64
65 void simcall_HANDLER_execution_waitany_for(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* execs[],
66                                            size_t count, double timeout)
67 {
68   if (timeout < 0.0) {
69     simcall->timeout_cb_ = nullptr;
70   } else {
71     simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall, execs, count]() {
72       for (size_t i = 0; i < count; i++) {
73         // Remove the first occurrence of simcall:
74         auto* exec = execs[i];
75         auto j     = boost::range::find(exec->simcalls_, simcall);
76         if (j != exec->simcalls_.end())
77           exec->simcalls_.erase(j);
78       }
79       simcall_execution_waitany_for__set__result(simcall, -1);
80       simcall->issuer_->simcall_answer();
81     });
82   }
83
84   for (size_t i = 0; i < count; i++) {
85     /* associate this simcall to the the synchro */
86     auto* exec = execs[i];
87     exec->simcalls_.push_back(simcall);
88
89     /* see if the synchro is already finished */
90     if (exec->state_ != simgrid::kernel::activity::State::WAITING &&
91         exec->state_ != simgrid::kernel::activity::State::RUNNING) {
92       exec->finish();
93       break;
94     }
95   }
96 }
97
98 namespace simgrid {
99 namespace kernel {
100 namespace activity {
101
102 ExecImpl::~ExecImpl()
103 {
104   if (timeout_detector_)
105     timeout_detector_->unref();
106   XBT_DEBUG("Destroy exec %p", this);
107 }
108
109 ExecImpl& ExecImpl::set_host(s4u::Host* host)
110 {
111   if (not hosts_.empty())
112     hosts_.clear();
113   hosts_.push_back(host);
114   return *this;
115 }
116
117 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
118 {
119   hosts_ = hosts;
120   return *this;
121 }
122
123 ExecImpl& ExecImpl::set_timeout(double timeout)
124 {
125   if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
126     timeout_detector_ = hosts_.front()->pimpl_cpu->sleep(timeout);
127     timeout_detector_->set_activity(this);
128   }
129   return *this;
130 }
131
132 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
133 {
134   if (not flops_amounts_.empty())
135     flops_amounts_.clear();
136   flops_amounts_.push_back(flops_amount);
137   return *this;
138 }
139
140 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
141 {
142   flops_amounts_ = flops_amounts;
143   return *this;
144 }
145
146 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
147 {
148   bytes_amounts_ = bytes_amounts;
149
150   return *this;
151 }
152
153 ExecImpl* ExecImpl::start()
154 {
155   state_ = State::RUNNING;
156   if (not MC_is_active() && not MC_record_replay_is_active()) {
157     if (hosts_.size() == 1) {
158       surf_action_ = hosts_.front()->pimpl_cpu->execution_start(flops_amounts_.front());
159       surf_action_->set_sharing_penalty(sharing_penalty_);
160       surf_action_->set_category(get_tracing_category());
161
162       if (bound_ > 0)
163         surf_action_->set_bound(bound_);
164     } else {
165       surf_action_ = surf_host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
166     }
167     surf_action_->set_activity(this);
168   }
169
170   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
171   return this;
172 }
173
174 double ExecImpl::get_seq_remaining_ratio()
175 {
176   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
177 }
178
179 double ExecImpl::get_par_remaining_ratio()
180 {
181   // parallel task: their remain is already between 0 and 1
182   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
183 }
184
185 ExecImpl& ExecImpl::set_bound(double bound)
186 {
187   bound_ = bound;
188   return *this;
189 }
190
191 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
192 {
193   sharing_penalty_ = sharing_penalty;
194   return *this;
195 }
196
197 void ExecImpl::post()
198 {
199   if (hosts_.size() == 1 && not hosts_.front()->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
200     /* If the host running the synchro failed, notice it. This way, the asking
201      * process can be killed if it runs on that host itself */
202     state_ = State::FAILED;
203   } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
204     /* If the host running the synchro didn't fail, then the synchro was canceled */
205     state_ = State::CANCELED;
206   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
207     state_ = State::TIMEOUT;
208   } else {
209     state_ = State::DONE;
210   }
211
212   clean_action();
213
214   if (timeout_detector_) {
215     timeout_detector_->unref();
216     timeout_detector_ = nullptr;
217   }
218
219   /* Answer all simcalls associated with the synchro */
220   finish();
221 }
222
223 void ExecImpl::finish()
224 {
225   while (not simcalls_.empty()) {
226     smx_simcall_t simcall = simcalls_.front();
227     simcalls_.pop_front();
228
229     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
230      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
231      * simcall */
232
233     if (simcall->call_ == SIMCALL_NONE) // FIXME: maybe a better way to handle this case
234       continue;                        // if process handling comm is killed
235     if (simcall->call_ == SIMCALL_EXECUTION_WAITANY_FOR) {
236       simgrid::kernel::activity::ExecImpl** execs = simcall_execution_waitany_for__get__execs(simcall);
237       size_t count                                = simcall_execution_waitany_for__get__count(simcall);
238
239       for (size_t i = 0; i < count; i++) {
240         // Remove the first occurrence of simcall:
241         auto* exec = execs[i];
242         auto j     = boost::range::find(exec->simcalls_, simcall);
243         if (j != exec->simcalls_.end())
244           exec->simcalls_.erase(j);
245
246         if (simcall->timeout_cb_) {
247           simcall->timeout_cb_->remove();
248           simcall->timeout_cb_ = nullptr;
249         }
250       }
251
252       if (not MC_is_active() && not MC_record_replay_is_active()) {
253         ExecImpl** element = std::find(execs, execs + count, this);
254         int rank           = (element != execs + count) ? element - execs : -1;
255         simcall_execution_waitany_for__set__result(simcall, rank);
256       }
257     }
258
259     switch (state_) {
260       case State::DONE:
261         /* do nothing, synchro done */
262         XBT_DEBUG("ExecImpl::finish(): execution successful");
263         break;
264
265       case State::FAILED:
266         XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer_->get_host()->get_cname());
267         simcall->issuer_->context_->iwannadie = true;
268         if (simcall->issuer_->get_host()->is_on())
269           simcall->issuer_->exception_ =
270               std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
271         /* else, the actor will be killed with no possibility to survive */
272         break;
273
274       case State::CANCELED:
275         XBT_DEBUG("ExecImpl::finish(): execution canceled");
276         simcall->issuer_->exception_ =
277             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
278         break;
279
280       case State::TIMEOUT:
281         XBT_DEBUG("ExecImpl::finish(): execution timeouted");
282         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
283         break;
284
285       default:
286         xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
287     }
288
289     simcall->issuer_->waiting_synchro = nullptr;
290     /* Fail the process if the host is down */
291     if (simcall->issuer_->get_host()->is_on())
292       simcall->issuer_->simcall_answer();
293     else
294       simcall->issuer_->context_->iwannadie = true;
295   }
296 }
297
298 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
299 {
300   if (not MC_is_active() && not MC_record_replay_is_active()) {
301     resource::Action* old_action = this->surf_action_;
302     resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
303     new_action->set_remains(old_action->get_remains());
304     new_action->set_activity(this);
305     new_action->set_sharing_penalty(old_action->get_sharing_penalty());
306
307     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
308     // I hope that the user did not provide any.
309
310     old_action->set_activity(nullptr);
311     old_action->cancel();
312     old_action->unref();
313     this->surf_action_ = new_action;
314   }
315
316   on_migration(*this, to);
317   return this;
318 }
319
320 /*************
321  * Callbacks *
322  *************/
323 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
324
325 } // namespace activity
326 } // namespace kernel
327 } // namespace simgrid