Logo AND Algorithmique Numérique Distribuée

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