Logo AND Algorithmique Numérique Distribuée

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