Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
928c9599093b49d2c9d1271a3eda313496a6dafe
[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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
17
18 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro)
19 {
20   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state_);
21
22   /* Associate this simcall to the synchro */
23   synchro->simcalls_.push_back(simcall);
24   simcall->issuer->waiting_synchro = synchro;
25
26   /* set surf's synchro */
27   if (MC_is_active() || MC_record_replay_is_active()) {
28     synchro->state_ = SIMIX_DONE;
29     synchro->finish();
30     return;
31   }
32
33   /* If the synchro is already finished then perform the error handling */
34   if (synchro->state_ != SIMIX_RUNNING)
35     synchro->finish();
36 }
37
38 void simcall_HANDLER_execution_test(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro)
39 {
40   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
41   if (res) {
42     synchro->simcalls_.push_back(simcall);
43     synchro->finish();
44   } else {
45     SIMIX_simcall_answer(simcall);
46   }
47   simcall_execution_test__set__result(simcall, res);
48 }
49
50 namespace simgrid {
51 namespace kernel {
52 namespace activity {
53
54 ExecImpl::~ExecImpl()
55 {
56   if (timeout_detector_)
57     timeout_detector_->unref();
58   XBT_DEBUG("Destroy exec %p", this);
59 }
60
61 ExecImpl& ExecImpl::set_host(s4u::Host* host)
62 {
63   if (not hosts_.empty())
64     hosts_.clear();
65   hosts_.push_back(host);
66   return *this;
67 }
68
69 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
70 {
71   hosts_ = hosts;
72   return *this;
73 }
74
75 ExecImpl& ExecImpl::set_timeout(double timeout)
76 {
77   if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
78     timeout_detector_ = hosts_.front()->pimpl_cpu->sleep(timeout);
79     timeout_detector_->set_data(this);
80   }
81   return *this;
82 }
83
84 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
85 {
86   if (not flops_amounts_.empty())
87     flops_amounts_.clear();
88   flops_amounts_.push_back(flops_amount);
89   return *this;
90 }
91
92 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
93 {
94   flops_amounts_ = flops_amounts;
95   return *this;
96 }
97
98 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
99 {
100   bytes_amounts_ = bytes_amounts;
101
102   return *this;
103 }
104
105 ExecImpl* ExecImpl::start()
106 {
107   state_ = SIMIX_RUNNING;
108   if (not MC_is_active() && not MC_record_replay_is_active()) {
109     if (hosts_.size() == 1) {
110       surf_action_ = hosts_.front()->pimpl_cpu->execution_start(flops_amounts_.front());
111       surf_action_->set_priority(priority_);
112       surf_action_->set_category(get_tracing_category());
113
114       if (bound_ > 0)
115         surf_action_->set_bound(bound_);
116     } else {
117       surf_action_ = surf_host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
118     }
119     surf_action_->set_data(this);
120   }
121
122   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
123   ExecImpl::on_creation(*this);
124   return this;
125 }
126
127 void ExecImpl::cancel()
128 {
129   XBT_VERB("This exec %p is canceled", this);
130   if (surf_action_ != nullptr)
131     surf_action_->cancel();
132 }
133
134 double ExecImpl::get_remaining() const
135 {
136   return surf_action_ ? surf_action_->get_remains() : 0;
137 }
138
139 double ExecImpl::get_seq_remaining_ratio()
140 {
141   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
142 }
143
144 double ExecImpl::get_par_remaining_ratio()
145 {
146   // parallel task: their remain is already between 0 and 1
147   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
148 }
149
150 ExecImpl& ExecImpl::set_bound(double bound)
151 {
152   bound_ = bound;
153   return *this;
154 }
155
156 ExecImpl& ExecImpl::set_priority(double priority)
157 {
158   priority_ = priority;
159   return *this;
160 }
161
162 void ExecImpl::post()
163 {
164   if (hosts_.size() == 1 && not hosts_.front()->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
165     /* If the host running the synchro failed, notice it. This way, the asking
166      * process can be killed if it runs on that host itself */
167     state_ = SIMIX_FAILED;
168   } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
169     /* If the host running the synchro didn't fail, then the synchro was canceled */
170     state_ = SIMIX_CANCELED;
171   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
172     state_ = SIMIX_TIMEOUT;
173   } else {
174     state_ = SIMIX_DONE;
175   }
176
177   on_completion(*this);
178
179   if (surf_action_) {
180     surf_action_->unref();
181     surf_action_ = nullptr;
182   }
183   if (timeout_detector_) {
184     timeout_detector_->unref();
185     timeout_detector_ = nullptr;
186   }
187
188   /* If there are simcalls associated with the synchro, then answer them */
189   if (not simcalls_.empty())
190     finish();
191 }
192
193 void ExecImpl::finish()
194 {
195   while (not simcalls_.empty()) {
196     smx_simcall_t simcall = simcalls_.front();
197     simcalls_.pop_front();
198     switch (state_) {
199
200       case SIMIX_DONE:
201         /* do nothing, synchro done */
202         XBT_DEBUG("ExecImpl::finish(): execution successful");
203         break;
204
205       case SIMIX_FAILED:
206         XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer->get_host()->get_cname());
207         simcall->issuer->context_->iwannadie = true;
208         if (simcall->issuer->get_host()->is_on())
209           simcall->issuer->exception_ =
210               std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
211         /* else, the actor will be killed with no possibility to survive */
212         break;
213
214       case SIMIX_CANCELED:
215         XBT_DEBUG("ExecImpl::finish(): execution canceled");
216         simcall->issuer->exception_ =
217             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
218         break;
219
220       case SIMIX_TIMEOUT:
221         XBT_DEBUG("ExecImpl::finish(): execution timeouted");
222         simcall->issuer->exception_ = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
223         break;
224
225       default:
226         xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
227     }
228
229     simcall->issuer->waiting_synchro = nullptr;
230     simcall_execution_wait__set__result(simcall, state_);
231
232     /* Fail the process if the host is down */
233     if (simcall->issuer->get_host()->is_on())
234       SIMIX_simcall_answer(simcall);
235     else
236       simcall->issuer->context_->iwannadie = true;
237   }
238 }
239
240 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
241 {
242   if (not MC_is_active() && not MC_record_replay_is_active()) {
243     resource::Action* old_action = this->surf_action_;
244     resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
245     new_action->set_remains(old_action->get_remains());
246     new_action->set_data(this);
247     new_action->set_priority(old_action->get_priority());
248
249     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
250     // I hope that the user did not provide any.
251
252     old_action->set_data(nullptr);
253     old_action->cancel();
254     old_action->unref();
255     this->surf_action_ = new_action;
256   }
257
258   on_migration(*this, to);
259   return this;
260 }
261
262 /*************
263  * Callbacks *
264  *************/
265 xbt::signal<void(ExecImpl&)> ExecImpl::on_creation;
266 xbt::signal<void(ExecImpl const&)> ExecImpl::on_completion;
267 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
268
269 } // namespace activity
270 } // namespace kernel
271 } // namespace simgrid