Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make cleanupSurf() private.
[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/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, smx_activity_t synchro)
19 {
20   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (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     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(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     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro)->finish();
36 }
37
38 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
39 {
40   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
41   if (res) {
42     synchro->simcalls_.push_back(simcall);
43     boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(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(std::string name, std::string tracing_category, resource::Action* timeout_detector, s4u::Host* host)
55     : ActivityImpl(std::move(name)), host_(host), timeout_detector_(timeout_detector)
56 {
57   this->state_ = SIMIX_RUNNING;
58   this->set_category(std::move(tracing_category));
59
60   if (timeout_detector != nullptr)
61     timeout_detector_->set_data(this);
62
63   XBT_DEBUG("Create exec %p", this);
64 }
65
66 ExecImpl::~ExecImpl()
67 {
68   if (surf_action_)
69     surf_action_->unref();
70   if (timeout_detector_)
71     timeout_detector_->unref();
72   XBT_DEBUG("Destroy exec %p", this);
73 }
74
75 ExecImpl* ExecImpl::start(double flops_amount, double priority, double bound)
76 {
77   if (not MC_is_active() && not MC_record_replay_is_active()) {
78     surf_action_ = host_->pimpl_cpu->execution_start(flops_amount);
79     surf_action_->set_data(this);
80     surf_action_->set_priority(priority);
81     if (bound > 0)
82       surf_action_->set_bound(bound);
83   }
84
85   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
86   ExecImpl::on_creation(this);
87   return this;
88 }
89
90 void ExecImpl::cancel()
91 {
92   XBT_VERB("This exec %p is canceled", this);
93   if (surf_action_ != nullptr)
94     surf_action_->cancel();
95 }
96
97 double ExecImpl::get_remaining()
98 {
99   xbt_assert(host_ != nullptr, "Calling remains() on a parallel execution is not allowed. "
100                                "We would need to return a vector instead of a scalar. "
101                                "Did you mean remainingRatio() instead?");
102   return surf_action_ ? surf_action_->get_remains() : 0;
103 }
104
105 double ExecImpl::get_remaining_ratio()
106 {
107   if (host_ ==
108       nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::get_remaining())
109     return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
110   else // Actually compute the ratio for sequential tasks
111     return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
112 }
113
114 void ExecImpl::set_bound(double bound)
115 {
116   if (surf_action_)
117     surf_action_->set_bound(bound);
118 }
119 void ExecImpl::set_priority(double priority)
120 {
121   if (surf_action_)
122     surf_action_->set_priority(priority);
123 }
124
125 void ExecImpl::post()
126 {
127   if (host_ && not host_->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
128     /* If the host running the synchro failed, notice it. This way, the asking
129      * process can be killed if it runs on that host itself */
130     state_ = SIMIX_FAILED;
131   } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
132     /* If the host running the synchro didn't fail, then the synchro was canceled */
133     state_ = SIMIX_CANCELED;
134   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
135     state_ = SIMIX_TIMEOUT;
136   } else {
137     state_ = SIMIX_DONE;
138   }
139
140   on_completion(this);
141
142   if (surf_action_) {
143     surf_action_->unref();
144     surf_action_ = nullptr;
145   }
146   if (timeout_detector_) {
147     timeout_detector_->unref();
148     timeout_detector_ = nullptr;
149   }
150
151   /* If there are simcalls associated with the synchro, then answer them */
152   if (not simcalls_.empty())
153     finish();
154 }
155
156 void ExecImpl::finish()
157 {
158   while (not simcalls_.empty()) {
159     smx_simcall_t simcall = simcalls_.front();
160     simcalls_.pop_front();
161     switch (state_) {
162
163       case SIMIX_DONE:
164         /* do nothing, synchro done */
165         XBT_DEBUG("SIMIX_execution_finished: execution successful");
166         break;
167
168       case SIMIX_FAILED:
169         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->get_host()->get_cname());
170         simcall->issuer->context_->iwannadie = true;
171         simcall->issuer->exception_ =
172             std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
173         break;
174
175       case SIMIX_CANCELED:
176         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
177         simcall->issuer->exception_ =
178             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
179         break;
180
181       case SIMIX_TIMEOUT:
182         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
183         simcall->issuer->exception_ = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
184         break;
185
186       default:
187         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", static_cast<int>(state_));
188     }
189
190     simcall->issuer->waiting_synchro = nullptr;
191     simcall_execution_wait__set__result(simcall, state_);
192
193     /* Fail the process if the host is down */
194     if (simcall->issuer->get_host()->is_on())
195       SIMIX_simcall_answer(simcall);
196     else
197       simcall->issuer->context_->iwannadie = true;
198   }
199 }
200
201 ActivityImpl* ExecImpl::migrate(simgrid::s4u::Host* to)
202 {
203   if (not MC_is_active() && not MC_record_replay_is_active()) {
204     resource::Action* old_action = this->surf_action_;
205     resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
206     new_action->set_remains(old_action->get_remains());
207     new_action->set_data(this);
208     new_action->set_priority(old_action->get_priority());
209
210     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
211     // I hope that the user did not provide any.
212
213     old_action->set_data(nullptr);
214     old_action->cancel();
215     old_action->unref();
216     this->surf_action_ = new_action;
217   }
218
219   on_migration(this, to);
220   return this;
221 }
222
223 /*************
224  * Callbacks *
225  *************/
226 xbt::signal<void(ExecImplPtr)> ExecImpl::on_creation;
227 xbt::signal<void(ExecImplPtr)> ExecImpl::on_completion;
228 xbt::signal<void(ExecImplPtr, s4u::Host*)> ExecImpl::on_migration;
229
230 } // namespace activity
231 } // namespace kernel
232 } // namespace simgrid