Logo AND Algorithmique Numérique Distribuée

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