Logo AND Algorithmique Numérique Distribuée

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