Logo AND Algorithmique Numérique Distribuée

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