Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4b66dd21bbd64c14b2b74c3fb4f5f4196a043af4
[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(const std::string& name, const std::string& tracing_category) : ActivityImpl(name)
55 {
56   this->state_ = SIMIX_RUNNING;
57   this->set_category(tracing_category);
58
59   XBT_DEBUG("Create exec %p", this);
60 }
61
62 ExecImpl::~ExecImpl()
63 {
64   if (surf_action_)
65     surf_action_->unref();
66   if (timeout_detector_)
67     timeout_detector_->unref();
68   XBT_DEBUG("Destroy exec %p", this);
69 }
70
71 ExecImpl* ExecImpl::set_host(s4u::Host* host)
72 {
73   host_ = host;
74   return this;
75 }
76
77 ExecImpl* ExecImpl::set_timeout(double timeout)
78 {
79   if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
80     timeout_detector_ = host_->pimpl_cpu->sleep(timeout);
81     timeout_detector_->set_data(this);
82   }
83   return this;
84 }
85
86 ExecImpl* ExecImpl::start(double flops_amount, double priority, double bound)
87 {
88   if (not MC_is_active() && not MC_record_replay_is_active()) {
89     surf_action_ = host_->pimpl_cpu->execution_start(flops_amount);
90     surf_action_->set_data(this);
91     surf_action_->set_priority(priority);
92     if (bound > 0)
93       surf_action_->set_bound(bound);
94   }
95
96   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
97   ExecImpl::on_creation(this);
98   return this;
99 }
100
101 ExecImpl* ExecImpl::start(const std::vector<s4u::Host*>& hosts, const std::vector<double>& flops_amounts,
102                           const std::vector<double>& bytes_amounts)
103 {
104   /* set surf's synchro */
105   if (not MC_is_active() && not MC_record_replay_is_active()) {
106     surf_action_ = surf_host_model->execute_parallel(hosts, flops_amounts.data(), bytes_amounts.data(), -1);
107     if (surf_action_ != nullptr) {
108       surf_action_->set_data(this);
109     }
110   }
111   XBT_DEBUG("Create parallel execute synchro %p", this);
112   ExecImpl::on_creation(this);
113   return this;
114 }
115 void ExecImpl::cancel()
116 {
117   XBT_VERB("This exec %p is canceled", this);
118   if (surf_action_ != nullptr)
119     surf_action_->cancel();
120 }
121
122 double ExecImpl::get_remaining()
123 {
124   return surf_action_ ? surf_action_->get_remains() : 0;
125 }
126
127 double ExecImpl::get_seq_remaining_ratio()
128 {
129   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
130 }
131
132 double ExecImpl::get_par_remaining_ratio()
133 {
134   // parallel task: their remain is already between 0 and 1
135   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
136 }
137
138 void ExecImpl::set_bound(double bound)
139 {
140   if (surf_action_)
141     surf_action_->set_bound(bound);
142 }
143 void ExecImpl::set_priority(double priority)
144 {
145   if (surf_action_)
146     surf_action_->set_priority(priority);
147 }
148
149 void ExecImpl::post()
150 {
151   if (host_ && not host_->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   if (surf_action_) {
167     surf_action_->unref();
168     surf_action_ = nullptr;
169   }
170   if (timeout_detector_) {
171     timeout_detector_->unref();
172     timeout_detector_ = nullptr;
173   }
174
175   /* If there are simcalls associated with the synchro, then answer them */
176   if (not simcalls_.empty())
177     finish();
178 }
179
180 void ExecImpl::finish()
181 {
182   while (not simcalls_.empty()) {
183     smx_simcall_t simcall = simcalls_.front();
184     simcalls_.pop_front();
185     switch (state_) {
186
187       case SIMIX_DONE:
188         /* do nothing, synchro done */
189         XBT_DEBUG("ExecImpl::finish(): execution successful");
190         break;
191
192       case SIMIX_FAILED:
193         XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer->get_host()->get_cname());
194         simcall->issuer->context_->iwannadie = true;
195         if (simcall->issuer->get_host()->is_on())
196           simcall->issuer->exception_ =
197               std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
198         /* else, the actor will be killed with no possibility to survive */
199         break;
200
201       case SIMIX_CANCELED:
202         XBT_DEBUG("ExecImpl::finish(): execution canceled");
203         simcall->issuer->exception_ =
204             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
205         break;
206
207       case SIMIX_TIMEOUT:
208         XBT_DEBUG("ExecImpl::finish(): execution timeouted");
209         simcall->issuer->exception_ = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
210         break;
211
212       default:
213         xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
214     }
215
216     simcall->issuer->waiting_synchro = nullptr;
217     simcall_execution_wait__set__result(simcall, state_);
218
219     /* Fail the process if the host is down */
220     if (simcall->issuer->get_host()->is_on())
221       SIMIX_simcall_answer(simcall);
222     else
223       simcall->issuer->context_->iwannadie = true;
224   }
225 }
226
227 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
228 {
229   if (not MC_is_active() && not MC_record_replay_is_active()) {
230     resource::Action* old_action = this->surf_action_;
231     resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
232     new_action->set_remains(old_action->get_remains());
233     new_action->set_data(this);
234     new_action->set_priority(old_action->get_priority());
235
236     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
237     // I hope that the user did not provide any.
238
239     old_action->set_data(nullptr);
240     old_action->cancel();
241     old_action->unref();
242     this->surf_action_ = new_action;
243   }
244
245   on_migration(this, to);
246   return this;
247 }
248
249 /*************
250  * Callbacks *
251  *************/
252 xbt::signal<void(ExecImplPtr)> ExecImpl::on_creation;
253 xbt::signal<void(ExecImplPtr)> ExecImpl::on_completion;
254 xbt::signal<void(ExecImplPtr, s4u::Host*)> ExecImpl::on_migration;
255
256 } // namespace activity
257 } // namespace kernel
258 } // namespace simgrid