1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
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/mc_replay.hpp"
12 #include "src/surf/HostImpl.hpp"
13 #include "src/surf/cpu_interface.hpp"
14 #include "src/surf/surf_interface.hpp"
16 #include "simgrid/s4u/Host.hpp"
18 #include <boost/range/algorithm.hpp>
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
22 void simcall_HANDLER_execution_waitany_for(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* execs[],
23 size_t count, double timeout)
26 simcall->timeout_cb_ = nullptr;
28 simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall, execs, count]() {
29 simcall->timeout_cb_ = nullptr;
30 for (size_t i = 0; i < count; i++) {
31 // Remove the first occurrence of simcall:
32 auto* exec = execs[i];
33 auto j = boost::range::find(exec->simcalls_, simcall);
34 if (j != exec->simcalls_.end())
35 exec->simcalls_.erase(j);
37 simcall_execution_waitany_for__set__result(simcall, -1);
38 simcall->issuer_->simcall_answer();
42 for (size_t i = 0; i < count; i++) {
43 /* associate this simcall to the the synchro */
44 auto* exec = execs[i];
45 exec->simcalls_.push_back(simcall);
47 /* see if the synchro is already finished */
48 if (exec->state_ != simgrid::kernel::activity::State::WAITING &&
49 exec->state_ != simgrid::kernel::activity::State::RUNNING) {
62 piface_ = new s4u::Exec(this);
63 actor::ActorImpl* self = actor::ActorImpl::self();
66 self->activities_.emplace_back(this);
70 ExecImpl& ExecImpl::set_host(s4u::Host* host)
72 hosts_.assign(1, host);
76 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
82 ExecImpl& ExecImpl::set_timeout(double timeout)
84 if (timeout >= 0 && not MC_is_active() && not MC_record_replay_is_active()) {
85 timeout_detector_.reset(hosts_.front()->pimpl_cpu->sleep(timeout));
86 timeout_detector_->set_activity(this);
91 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
93 flops_amounts_.assign(1, flops_amount);
97 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
99 flops_amounts_ = flops_amounts;
103 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
105 bytes_amounts_ = bytes_amounts;
110 ExecImpl* ExecImpl::start()
112 state_ = State::RUNNING;
113 if (not MC_is_active() && not MC_record_replay_is_active()) {
114 if (hosts_.size() == 1) {
115 surf_action_ = hosts_.front()->pimpl_cpu->execution_start(flops_amounts_.front());
116 surf_action_->set_sharing_penalty(sharing_penalty_);
117 surf_action_->set_category(get_tracing_category());
120 surf_action_->set_bound(bound_);
121 surf_action_->set_user_bound(bound_);
124 // FIXME[donassolo]: verify if all hosts belongs to the same netZone?
125 auto host_model = hosts_.front()->get_netpoint()->get_englobing_zone()->get_host_model();
126 surf_action_ = host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
128 surf_action_->set_activity(this);
131 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
135 double ExecImpl::get_seq_remaining_ratio()
137 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
140 double ExecImpl::get_par_remaining_ratio()
142 // parallel task: their remain is already between 0 and 1
143 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
146 ExecImpl& ExecImpl::set_bound(double bound)
152 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
154 sharing_penalty_ = sharing_penalty;
158 void ExecImpl::post()
160 xbt_assert(surf_action_ != nullptr);
161 if (std::any_of(hosts_.begin(), hosts_.end(), [](const s4u::Host* host) { return not host->is_on(); })) {
162 /* If one of the hosts running the synchro failed, notice it. This way, the asking
163 * process can be killed if it runs on that host itself */
164 state_ = State::FAILED;
165 } else if (surf_action_->get_state() == resource::Action::State::FAILED) {
166 /* If all the hosts are running the synchro didn't fail, then the synchro was canceled */
167 state_ = State::CANCELED;
168 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
169 if (surf_action_->get_remains() > 0.0) {
170 surf_action_->set_state(resource::Action::State::FAILED);
171 state_ = State::TIMEOUT;
173 state_ = State::DONE;
176 state_ = State::DONE;
179 get_iface()->set_finish_time(surf_action_->get_finish_time());
182 timeout_detector_.reset();
184 actor_->activities_.remove(this);
187 /* Answer all simcalls associated with the synchro */
191 void ExecImpl::finish()
193 while (not simcalls_.empty()) {
194 smx_simcall_t simcall = simcalls_.front();
195 simcalls_.pop_front();
197 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
198 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
201 if (simcall->call_ == simix::Simcall::NONE) // FIXME: maybe a better way to handle this case
202 continue; // if process handling comm is killed
203 if (simcall->call_ == simix::Simcall::EXECUTION_WAITANY_FOR) {
204 simgrid::kernel::activity::ExecImpl** execs = simcall_execution_waitany_for__get__execs(simcall);
205 size_t count = simcall_execution_waitany_for__get__count(simcall);
207 for (size_t i = 0; i < count; i++) {
208 // Remove the first occurrence of simcall:
209 auto* exec = execs[i];
210 auto j = boost::range::find(exec->simcalls_, simcall);
211 if (j != exec->simcalls_.end())
212 exec->simcalls_.erase(j);
214 if (simcall->timeout_cb_) {
215 simcall->timeout_cb_->remove();
216 simcall->timeout_cb_ = nullptr;
220 if (not MC_is_active() && not MC_record_replay_is_active()) {
221 ExecImpl** element = std::find(execs, execs + count, this);
222 int rank = (element != execs + count) ? element - execs : -1;
223 simcall_execution_waitany_for__set__result(simcall, rank);
228 /* do nothing, synchro done */
229 XBT_DEBUG("ExecImpl::finish(): execution successful");
233 XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer_->get_host()->get_cname());
234 simcall->issuer_->context_->set_wannadie();
235 if (simcall->issuer_->get_host()->is_on())
236 simcall->issuer_->exception_ =
237 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
238 /* else, the actor will be killed with no possibility to survive */
241 case State::CANCELED:
242 XBT_DEBUG("ExecImpl::finish(): execution canceled");
243 simcall->issuer_->exception_ =
244 std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
248 XBT_DEBUG("ExecImpl::finish(): execution timeouted");
249 simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
253 xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
256 simcall->issuer_->waiting_synchro_ = nullptr;
257 /* Fail the process if the host is down */
258 if (simcall->issuer_->get_host()->is_on())
259 simcall->issuer_->simcall_answer();
261 simcall->issuer_->context_->set_wannadie();
265 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
267 if (not MC_is_active() && not MC_record_replay_is_active()) {
268 resource::Action* old_action = this->surf_action_;
269 resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
270 new_action->set_remains(old_action->get_remains());
271 new_action->set_activity(this);
272 new_action->set_sharing_penalty(old_action->get_sharing_penalty());
274 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
275 // I hope that the user did not provide any.
277 old_action->set_activity(nullptr);
278 old_action->cancel();
280 this->surf_action_ = new_action;
283 on_migration(*this, to);
290 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
292 } // namespace activity
293 } // namespace kernel
294 } // namespace simgrid