Logo AND Algorithmique Numérique Distribuée

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