Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2023. 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/kernel/resource/HostImpl.hpp"
16 #include "src/mc/mc_replay.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_.insert(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_flops_amount(double flops_amount)
45 {
46   flops_amounts_.assign(1, flops_amount);
47   return *this;
48 }
49
50 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
51 {
52   flops_amounts_ = flops_amounts;
53   return *this;
54 }
55
56 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
57 {
58   bytes_amounts_ = bytes_amounts;
59
60   return *this;
61 }
62 ExecImpl& ExecImpl::set_thread_count(int thread_count)
63 {
64   thread_count_ = thread_count;
65
66   return *this;
67 }
68
69 ExecImpl* ExecImpl::start()
70 {
71   set_state(State::RUNNING);
72   if (not MC_is_active() && not MC_record_replay_is_active()) {
73     if (get_hosts().size() == 1) {
74       if (thread_count_ == 1) {
75         model_action_ = get_host()->get_cpu()->execution_start(flops_amounts_.front(), bound_);
76         model_action_->set_sharing_penalty(sharing_penalty_);
77       } else {
78         auto host_model = get_host()->get_netpoint()->get_englobing_zone()->get_host_model();
79         model_action_   = host_model->execute_thread(get_host(), flops_amounts_.front(), thread_count_);
80       }
81       model_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 = get_host()->get_netpoint()->get_englobing_zone()->get_host_model();
85       model_action_   = host_model->execute_parallel(get_hosts(), flops_amounts_.data(), bytes_amounts_.data(), -1);
86     }
87     model_action_->set_activity(this);
88     set_start_time(model_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_remaining() const
96 {
97   if (get_state() == State::WAITING || get_state() == State::FAILED)
98     return flops_amounts_.front();
99   return ActivityImpl::get_remaining();
100 }
101
102 double ExecImpl::get_seq_remaining_ratio()
103 {
104   if (get_state() == State::WAITING)
105     return 1;
106   return (model_action_ == nullptr) ? 0 : model_action_->get_remains() / model_action_->get_cost();
107 }
108
109 double ExecImpl::get_par_remaining_ratio()
110 {
111   // parallel task: their remain is already between 0 and 1
112   if (get_state() == State::WAITING)
113     return 1;
114   return (model_action_ == nullptr) ? 0 : model_action_->get_remains();
115 }
116
117 ExecImpl& ExecImpl::set_bound(double bound)
118 {
119   bound_ = bound;
120   return *this;
121 }
122
123 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
124 {
125   sharing_penalty_ = sharing_penalty;
126   return *this;
127 }
128
129 ExecImpl& ExecImpl::update_sharing_penalty(double sharing_penalty)
130 {
131   sharing_penalty_ = sharing_penalty;
132   model_action_->set_sharing_penalty(sharing_penalty);
133   return *this;
134 }
135
136 void ExecImpl::set_exception(actor::ActorImpl* issuer)
137 {
138   switch (get_state()) {
139     case State::FAILED:
140       static_cast<s4u::Exec*>(get_iface())->complete(s4u::Activity::State::FAILED);
141       if (issuer->get_host()->is_on())
142         issuer->exception_ = std::make_exception_ptr(HostFailureException(XBT_THROW_POINT, "Host failed"));
143       else /* else, the actor will be killed with no possibility to survive */
144         issuer->set_wannadie();
145       break;
146
147     case State::CANCELED:
148       issuer->exception_ = std::make_exception_ptr(CancelException(XBT_THROW_POINT, "Execution Canceled"));
149       break;
150
151     case State::TIMEOUT:
152       issuer->exception_ = std::make_exception_ptr(TimeoutException(XBT_THROW_POINT, "Timeouted"));
153       break;
154
155     default:
156       xbt_assert(get_state() == State::DONE, "Internal error in ExecImpl::finish(): unexpected synchro state %s",
157                  get_state_str());
158   }
159 }
160 void ExecImpl::finish()
161 {
162   XBT_DEBUG("ExecImpl::finish() in state %s", get_state_str());
163   if (model_action_ != nullptr) {
164     if (auto const& hosts = get_hosts();
165         std::any_of(hosts.begin(), hosts.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
166       /* If one of the hosts running the synchro failed, notice it. This way, the asking
167        * process can be killed if it runs on that host itself */
168       set_state(State::FAILED);
169     } else if (model_action_->get_state() == resource::Action::State::FAILED) {
170       /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
171       set_state(State::CANCELED);
172     } else {
173       set_state(State::DONE);
174     }
175
176     clean_action();
177   }
178
179   if (get_actor() != nullptr)
180     get_actor()->activities_.erase(this);
181
182   if (get_state() != State::FAILED && cb_id_ >= 0)
183     s4u::Host::on_state_change.disconnect(cb_id_);
184
185   while (not simcalls_.empty()) {
186     actor::Simcall* simcall = simcalls_.front();
187     simcalls_.pop_front();
188
189     if (simcall->call_ == actor::Simcall::Type::NONE) // FIXME: maybe a better way to handle this case
190       continue;                                       // if process handling comm is killed
191
192     handle_activity_waitany(simcall);
193
194     set_exception(simcall->issuer_);
195
196     simcall->issuer_->waiting_synchro_ = nullptr;
197     /* Fail the process if the host is down */
198     if (simcall->issuer_->get_host()->is_on())
199       simcall->issuer_->simcall_answer();
200     else
201       simcall->issuer_->set_wannadie();
202   }
203 }
204
205 void ExecImpl::reset()
206 {
207   clear_hosts();
208   bytes_amounts_.clear();
209   flops_amounts_.clear();
210   set_start_time(-1.0);
211 }
212
213 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
214 {
215   if (not MC_is_active() && not MC_record_replay_is_active()) {
216     resource::Action* old_action = this->model_action_;
217     resource::Action* new_action = to->get_cpu()->execution_start(old_action->get_cost(), old_action->get_user_bound());
218     new_action->set_remains(old_action->get_remains());
219     new_action->set_activity(this);
220     new_action->set_sharing_penalty(old_action->get_sharing_penalty());
221     new_action->set_user_bound(old_action->get_user_bound());
222
223     old_action->set_activity(nullptr);
224     old_action->cancel();
225     old_action->unref();
226     this->model_action_ = new_action;
227   }
228
229   on_migration(*this, to);
230   return this;
231 }
232
233 /*************
234  * Callbacks *
235  *************/
236 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
237
238 } // namespace simgrid::kernel::activity