Logo AND Algorithmique Numérique Distribuée

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