Logo AND Algorithmique Numérique Distribuée

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