Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle simcall result through mc::SimcallObserver.
[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/mc/checker/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   }
95
96   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
97   return this;
98 }
99
100 double ExecImpl::get_seq_remaining_ratio()
101 {
102   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
103 }
104
105 double ExecImpl::get_par_remaining_ratio()
106 {
107   // parallel task: their remain is already between 0 and 1
108   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
109 }
110
111 ExecImpl& ExecImpl::set_bound(double bound)
112 {
113   bound_ = bound;
114   return *this;
115 }
116
117 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
118 {
119   sharing_penalty_ = sharing_penalty;
120   return *this;
121 }
122
123 void ExecImpl::post()
124 {
125   xbt_assert(surf_action_ != nullptr);
126   if (std::any_of(hosts_.begin(), hosts_.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
127     /* If one of the hosts running the synchro failed, notice it. This way, the asking
128      * process can be killed if it runs on that host itself */
129     state_ = State::FAILED;
130   } else if (surf_action_->get_state() == resource::Action::State::FAILED) {
131     /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
132     state_ = State::CANCELED;
133   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
134     if (surf_action_->get_remains() > 0.0) {
135       surf_action_->set_state(resource::Action::State::FAILED);
136       state_ = State::TIMEOUT;
137     } else {
138       state_ = State::DONE;
139     }
140   } else {
141     state_ = State::DONE;
142   }
143
144   get_iface()->set_finish_time(surf_action_->get_finish_time());
145
146   clean_action();
147   timeout_detector_.reset();
148   if (actor_) {
149     actor_->activities_.remove(this);
150     actor_ = nullptr;
151   }
152   /* Answer all simcalls associated with the synchro */
153   finish();
154 }
155
156 void ExecImpl::finish()
157 {
158   while (not simcalls_.empty()) {
159     smx_simcall_t simcall = simcalls_.front();
160     simcalls_.pop_front();
161
162     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
163      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
164      * simcall */
165
166     if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
167       continue;                                 // if process handling comm is killed
168     if (auto* observer = dynamic_cast<mc::ExecutionWaitanySimcall*>(simcall->observer_)) { // simcall is a wait_any?
169       const auto* execs = observer->get_execs();
170
171       for (auto* exec : *execs) {
172         exec->unregister_simcall(simcall);
173
174         if (simcall->timeout_cb_) {
175           simcall->timeout_cb_->remove();
176           simcall->timeout_cb_ = nullptr;
177         }
178       }
179
180       if (not MC_is_active() && not MC_record_replay_is_active()) {
181         auto element = std::find(execs->begin(), execs->end(), this);
182         int rank     = element != execs->end() ? static_cast<int>(std::distance(execs->begin(), element)) : -1;
183         observer->set_result(rank);
184       }
185     }
186     switch (state_) {
187       case State::DONE:
188         /* do nothing, synchro done */
189         XBT_DEBUG("ExecImpl::finish(): execution successful");
190         break;
191
192       case State::FAILED:
193         XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer_->get_host()->get_cname());
194         simcall->issuer_->context_->set_wannadie();
195         if (simcall->issuer_->get_host()->is_on())
196           simcall->issuer_->exception_ =
197               std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
198         /* else, the actor will be killed with no possibility to survive */
199         break;
200
201       case State::CANCELED:
202         XBT_DEBUG("ExecImpl::finish(): execution canceled");
203         simcall->issuer_->exception_ =
204             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
205         break;
206
207       case State::TIMEOUT:
208         XBT_DEBUG("ExecImpl::finish(): execution timeouted");
209         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
210         break;
211
212       default:
213         xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
214     }
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_->context_->set_wannadie();
222   }
223 }
224
225 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
226 {
227   if (not MC_is_active() && not MC_record_replay_is_active()) {
228     resource::Action* old_action = this->surf_action_;
229     resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
230     new_action->set_remains(old_action->get_remains());
231     new_action->set_activity(this);
232     new_action->set_sharing_penalty(old_action->get_sharing_penalty());
233     new_action->set_user_bound(old_action->get_user_bound());
234
235     old_action->set_activity(nullptr);
236     old_action->cancel();
237     old_action->unref();
238     this->surf_action_ = new_action;
239   }
240
241   on_migration(*this, to);
242   return this;
243 }
244
245 void ExecImpl::wait_any_for(actor::ActorImpl* issuer, const std::vector<ExecImpl*>* execs, double timeout)
246 {
247   if (timeout < 0.0) {
248     issuer->simcall_.timeout_cb_ = nullptr;
249   } else {
250     issuer->simcall_.timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [issuer, execs]() {
251       issuer->simcall_.timeout_cb_ = nullptr;
252       for (auto* exec : *execs)
253         exec->unregister_simcall(&issuer->simcall_);
254       // default result (-1) is set in mc::ExecutionWaitanySimcall
255       issuer->simcall_answer();
256     });
257   }
258
259   for (auto* exec : *execs) {
260     /* associate this simcall to the the synchro */
261     exec->simcalls_.push_back(&issuer->simcall_);
262
263     /* see if the synchro is already finished */
264     if (exec->state_ != simgrid::kernel::activity::State::WAITING &&
265         exec->state_ != simgrid::kernel::activity::State::RUNNING) {
266       exec->finish();
267       break;
268     }
269   }
270 }
271
272 /*************
273  * Callbacks *
274  *************/
275 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
276
277 } // namespace activity
278 } // namespace kernel
279 } // namespace simgrid