Logo AND Algorithmique Numérique Distribuée

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