Logo AND Algorithmique Numérique Distribuée

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