Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
052929c027722093c314ce2c57563d4ce7850675
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2021. 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 <simgrid/Exception.hpp>
7 #include <simgrid/kernel/routing/NetPoint.hpp>
8 #include <simgrid/modelchecker.h>
9 #include <simgrid/s4u/Engine.hpp>
10
11 #include "src/kernel/activity/ExecImpl.hpp"
12 #include "src/kernel/actor/ActorImpl.hpp"
13 #include "src/kernel/actor/SimcallObserver.hpp"
14 #include "src/mc/mc_replay.hpp"
15 #include "src/surf/HostImpl.hpp"
16 #include "src/surf/cpu_interface.hpp"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
19
20 namespace simgrid {
21 namespace kernel {
22 namespace activity {
23
24 ExecImpl::ExecImpl()
25 {
26   piface_                = new s4u::Exec(this);
27   actor::ActorImpl* self = actor::ActorImpl::self();
28   if (self) {
29     actor_ = self;
30     self->activities_.emplace_back(this);
31   }
32 }
33
34 ExecImpl& ExecImpl::set_host(s4u::Host* host)
35 {
36   hosts_.assign(1, host);
37   return *this;
38 }
39
40 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
41 {
42   hosts_ = hosts;
43   return *this;
44 }
45
46 ExecImpl& ExecImpl::set_timeout(double timeout)
47 {
48   if (timeout >= 0 && not MC_is_active() && not MC_record_replay_is_active()) {
49     timeout_detector_.reset(hosts_.front()->get_cpu()->sleep(timeout));
50     timeout_detector_->set_activity(this);
51   }
52   return *this;
53 }
54
55 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
56 {
57   flops_amounts_.assign(1, flops_amount);
58   return *this;
59 }
60
61 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
62 {
63   flops_amounts_ = flops_amounts;
64   return *this;
65 }
66
67 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
68 {
69   bytes_amounts_ = bytes_amounts;
70
71   return *this;
72 }
73
74 ExecImpl* ExecImpl::start()
75 {
76   state_ = State::RUNNING;
77   if (not MC_is_active() && not MC_record_replay_is_active()) {
78     if (hosts_.size() == 1) {
79       surf_action_ = hosts_.front()->get_cpu()->execution_start(flops_amounts_.front(), bound_);
80       surf_action_->set_sharing_penalty(sharing_penalty_);
81       surf_action_->set_category(get_tracing_category());
82     } else {
83       // get the model from first host since we have only 1 by now
84       auto host_model = hosts_.front()->get_netpoint()->get_englobing_zone()->get_host_model();
85       surf_action_    = host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
86     }
87     surf_action_->set_activity(this);
88     start_time_ = surf_action_->get_start_time();
89   }
90
91   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
92   return this;
93 }
94
95 double ExecImpl::get_seq_remaining_ratio()
96 {
97   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
98 }
99
100 double ExecImpl::get_par_remaining_ratio()
101 {
102   // parallel task: their remain is already between 0 and 1
103   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
104 }
105
106 ExecImpl& ExecImpl::set_bound(double bound)
107 {
108   bound_ = bound;
109   return *this;
110 }
111
112 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
113 {
114   sharing_penalty_ = sharing_penalty;
115   return *this;
116 }
117
118 void ExecImpl::post()
119 {
120   xbt_assert(surf_action_ != nullptr);
121   if (std::any_of(hosts_.begin(), hosts_.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
122     /* If one of the hosts running the synchro failed, notice it. This way, the asking
123      * process can be killed if it runs on that host itself */
124     state_ = State::FAILED;
125   } else if (surf_action_->get_state() == resource::Action::State::FAILED) {
126     /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
127     state_ = State::CANCELED;
128   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
129     if (surf_action_->get_remains() > 0.0) {
130       surf_action_->set_state(resource::Action::State::FAILED);
131       state_ = State::TIMEOUT;
132     } else {
133       state_ = State::DONE;
134     }
135   } else {
136     state_ = State::DONE;
137   }
138
139   finish_time_ = surf_action_->get_finish_time();
140
141   clean_action();
142   timeout_detector_.reset();
143   if (actor_) {
144     actor_->activities_.remove(this);
145     actor_ = nullptr;
146   }
147   if (state_ != State::FAILED && cb_id_ >= 0)
148     s4u::Host::on_state_change.disconnect(cb_id_);
149   /* Answer all simcalls associated with the synchro */
150   finish();
151 }
152
153 void ExecImpl::finish()
154 {
155   XBT_DEBUG("ExecImpl::finish() in state %s", to_c_str(state_));
156   while (not simcalls_.empty()) {
157     smx_simcall_t simcall = simcalls_.front();
158     simcalls_.pop_front();
159
160     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
161      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
162      * simcall */
163
164     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
165       continue;                                 // if process handling comm is killed
166     if (auto* observer =
167             dynamic_cast<kernel::actor::ExecutionWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
168       const auto& execs = observer->get_execs();
169
170       for (auto* exec : execs) {
171         exec->unregister_simcall(simcall);
172
173         if (simcall->timeout_cb_) {
174           simcall->timeout_cb_->remove();
175           simcall->timeout_cb_ = nullptr;
176         }
177       }
178
179       if (not MC_is_active() && not MC_record_replay_is_active()) {
180         auto element = std::find(execs.begin(), execs.end(), this);
181         int rank     = element != execs.end() ? static_cast<int>(std::distance(execs.begin(), element)) : -1;
182         observer->set_result(rank);
183       }
184     }
185     switch (state_) {
186       case State::FAILED:
187         piface_->complete(s4u::Activity::State::FAILED);
188         if (simcall->issuer_->get_host()->is_on())
189           simcall->issuer_->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
190         else /* else, the actor will be killed with no possibility to survive */
191           simcall->issuer_->context_->set_wannadie();
192         break;
193
194       case State::CANCELED:
195         simcall->issuer_->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Execution Canceled"));
196         break;
197
198       case State::TIMEOUT:
199         simcall->issuer_->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
200         break;
201
202       default:
203         xbt_assert(state_ == State::DONE, "Internal error in ExecImpl::finish(): unexpected synchro state %s",
204                    to_c_str(state_));
205     }
206
207     simcall->issuer_->waiting_synchro_ = nullptr;
208     /* Fail the process if the host is down */
209     if (simcall->issuer_->get_host()->is_on())
210       simcall->issuer_->simcall_answer();
211     else
212       simcall->issuer_->context_->set_wannadie();
213   }
214 }
215
216 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
217 {
218   if (not MC_is_active() && not MC_record_replay_is_active()) {
219     resource::Action* old_action = this->surf_action_;
220     resource::Action* new_action = to->get_cpu()->execution_start(old_action->get_cost(), old_action->get_user_bound());
221     new_action->set_remains(old_action->get_remains());
222     new_action->set_activity(this);
223     new_action->set_sharing_penalty(old_action->get_sharing_penalty());
224     new_action->set_user_bound(old_action->get_user_bound());
225
226     old_action->set_activity(nullptr);
227     old_action->cancel();
228     old_action->unref();
229     this->surf_action_ = new_action;
230   }
231
232   on_migration(*this, to);
233   return this;
234 }
235
236 void ExecImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ExecImpl*>& execs, double timeout)
237 {
238   if (timeout < 0.0) {
239     issuer->simcall_.timeout_cb_ = nullptr;
240   } else {
241     issuer->simcall_.timeout_cb_ = timer::Timer::set(s4u::Engine::get_clock() + timeout, [issuer, &execs]() {
242       issuer->simcall_.timeout_cb_ = nullptr;
243       for (auto* exec : execs)
244         exec->unregister_simcall(&issuer->simcall_);
245       // default result (-1) is set in actor::ExecutionWaitanySimcall
246       issuer->simcall_answer();
247     });
248   }
249
250   for (auto* exec : execs) {
251     /* associate this simcall to the the synchro */
252     exec->simcalls_.push_back(&issuer->simcall_);
253     /* see if the synchro is already finished */
254     if (exec->state_ != State::WAITING && exec->state_ != State::RUNNING) {
255       exec->finish();
256       break;
257     }
258   }
259 }
260
261 /*************
262  * Callbacks *
263  *************/
264 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
265
266 } // namespace activity
267 } // namespace kernel
268 } // namespace simgrid