Logo AND Algorithmique Numérique Distribuée

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