1 /* Copyright (c) 2007-2020. 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/modelchecker.h"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/surf/HostImpl.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/surf_interface.hpp"
14 #include "simgrid/s4u/Host.hpp"
16 #include <boost/range/algorithm.hpp>
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
20 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro, double timeout)
22 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state_);
23 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
25 /* Associate this simcall to the synchro */
26 synchro->register_simcall(simcall);
28 /* set surf's synchro */
29 if (MC_is_active() || MC_record_replay_is_active()) {
30 int idx = SIMCALL_GET_MC_VALUE(*simcall);
32 synchro->state_ = simgrid::kernel::activity::State::DONE;
34 /* If we reached this point, the wait simcall must have a timeout */
35 /* Otherwise it shouldn't be enabled and executed by the MC */
38 synchro->state_ = simgrid::kernel::activity::State::TIMEOUT;
44 /* If the synchro is already finished then perform the error handling */
45 if (synchro->state_ != simgrid::kernel::activity::State::RUNNING) {
47 } else { /* we need a sleep action (even when there is no timeout) to be notified of host failures */
48 synchro->set_timeout(timeout);
52 void simcall_HANDLER_execution_test(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* synchro)
54 bool res = (synchro->state_ != simgrid::kernel::activity::State::WAITING &&
55 synchro->state_ != simgrid::kernel::activity::State::RUNNING);
57 synchro->simcalls_.push_back(simcall);
60 simcall->issuer_->simcall_answer();
62 simcall_execution_test__set__result(simcall, res);
65 void simcall_HANDLER_execution_waitany_for(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* execs[],
66 size_t count, double timeout)
69 simcall->timeout_cb_ = nullptr;
71 simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall, execs, count]() {
72 for (size_t i = 0; i < count; i++) {
73 // Remove the first occurrence of simcall:
74 auto* exec = execs[i];
75 auto j = boost::range::find(exec->simcalls_, simcall);
76 if (j != exec->simcalls_.end())
77 exec->simcalls_.erase(j);
79 simcall_execution_waitany_for__set__result(simcall, -1);
80 simcall->issuer_->simcall_answer();
84 for (size_t i = 0; i < count; i++) {
85 /* associate this simcall to the the synchro */
86 auto* exec = execs[i];
87 exec->simcalls_.push_back(simcall);
89 /* see if the synchro is already finished */
90 if (exec->state_ != simgrid::kernel::activity::State::WAITING &&
91 exec->state_ != simgrid::kernel::activity::State::RUNNING) {
102 ExecImpl& ExecImpl::set_host(s4u::Host* host)
104 if (not hosts_.empty())
106 hosts_.push_back(host);
110 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
116 ExecImpl& ExecImpl::set_timeout(double timeout)
118 if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
119 timeout_detector_.reset(hosts_.front()->pimpl_cpu->sleep(timeout));
120 timeout_detector_->set_activity(this);
125 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
127 if (not flops_amounts_.empty())
128 flops_amounts_.clear();
129 flops_amounts_.push_back(flops_amount);
133 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
135 flops_amounts_ = flops_amounts;
139 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
141 bytes_amounts_ = bytes_amounts;
146 ExecImpl* ExecImpl::start()
148 state_ = State::RUNNING;
149 if (not MC_is_active() && not MC_record_replay_is_active()) {
150 if (hosts_.size() == 1) {
151 surf_action_ = hosts_.front()->pimpl_cpu->execution_start(flops_amounts_.front());
152 surf_action_->set_sharing_penalty(sharing_penalty_);
153 surf_action_->set_category(get_tracing_category());
156 surf_action_->set_bound(bound_);
158 surf_action_ = surf_host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
160 surf_action_->set_activity(this);
163 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
167 double ExecImpl::get_seq_remaining_ratio()
169 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
172 double ExecImpl::get_par_remaining_ratio()
174 // parallel task: their remain is already between 0 and 1
175 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
178 ExecImpl& ExecImpl::set_bound(double bound)
184 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
186 sharing_penalty_ = sharing_penalty;
190 void ExecImpl::post()
192 if (hosts_.size() == 1 && not hosts_.front()->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
193 /* If the host running the synchro failed, notice it. This way, the asking
194 * process can be killed if it runs on that host itself */
195 state_ = State::FAILED;
196 } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
197 /* If the host running the synchro didn't fail, then the synchro was canceled */
198 state_ = State::CANCELED;
199 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
200 state_ = State::TIMEOUT;
202 state_ = State::DONE;
206 timeout_detector_.reset();
207 /* Answer all simcalls associated with the synchro */
211 void ExecImpl::finish()
213 while (not simcalls_.empty()) {
214 smx_simcall_t simcall = simcalls_.front();
215 simcalls_.pop_front();
217 /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
218 * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
221 if (simcall->call_ == SIMCALL_NONE) // FIXME: maybe a better way to handle this case
222 continue; // if process handling comm is killed
223 if (simcall->call_ == SIMCALL_EXECUTION_WAITANY_FOR) {
224 simgrid::kernel::activity::ExecImpl** execs = simcall_execution_waitany_for__get__execs(simcall);
225 size_t count = simcall_execution_waitany_for__get__count(simcall);
227 for (size_t i = 0; i < count; i++) {
228 // Remove the first occurrence of simcall:
229 auto* exec = execs[i];
230 auto j = boost::range::find(exec->simcalls_, simcall);
231 if (j != exec->simcalls_.end())
232 exec->simcalls_.erase(j);
234 if (simcall->timeout_cb_) {
235 simcall->timeout_cb_->remove();
236 simcall->timeout_cb_ = nullptr;
240 if (not MC_is_active() && not MC_record_replay_is_active()) {
241 ExecImpl** element = std::find(execs, execs + count, this);
242 int rank = (element != execs + count) ? element - execs : -1;
243 simcall_execution_waitany_for__set__result(simcall, rank);
249 /* do nothing, synchro done */
250 XBT_DEBUG("ExecImpl::finish(): execution successful");
254 XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer_->get_host()->get_cname());
255 simcall->issuer_->context_->set_wannadie();
256 if (simcall->issuer_->get_host()->is_on())
257 simcall->issuer_->exception_ =
258 std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
259 /* else, the actor will be killed with no possibility to survive */
262 case State::CANCELED:
263 XBT_DEBUG("ExecImpl::finish(): execution canceled");
264 simcall->issuer_->exception_ =
265 std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
269 XBT_DEBUG("ExecImpl::finish(): execution timeouted");
270 simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
274 xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
277 simcall->issuer_->waiting_synchro = nullptr;
278 /* Fail the process if the host is down */
279 if (simcall->issuer_->get_host()->is_on())
280 simcall->issuer_->simcall_answer();
282 simcall->issuer_->context_->set_wannadie();
286 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
288 if (not MC_is_active() && not MC_record_replay_is_active()) {
289 resource::Action* old_action = this->surf_action_;
290 resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
291 new_action->set_remains(old_action->get_remains());
292 new_action->set_activity(this);
293 new_action->set_sharing_penalty(old_action->get_sharing_penalty());
295 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
296 // I hope that the user did not provide any.
298 old_action->set_activity(nullptr);
299 old_action->cancel();
301 this->surf_action_ = new_action;
304 on_migration(*this, to);
311 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
313 } // namespace activity
314 } // namespace kernel
315 } // namespace simgrid