1 /* Copyright (c) 2007-2018. 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 "simgrid/modelchecker.h"
7 #include "src/mc/mc_replay.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "src/simix/smx_host_private.hpp"
11 #include "src/surf/surf_interface.hpp"
12 #include "src/surf/cpu_interface.hpp"
14 #include "simgrid/s4u/Host.hpp"
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
18 simgrid::kernel::activity::ExecImpl::ExecImpl(std::string name, resource::Action* surf_action,
19 resource::Action* timeout_detector, s4u::Host* host)
20 : ActivityImpl(name), host_(host), surf_action_(surf_action), timeout_detector_(timeout_detector)
22 this->state_ = SIMIX_RUNNING;
24 surf_action_->set_data(this);
25 if (timeout_detector != nullptr)
26 timeout_detector->set_data(this);
28 XBT_DEBUG("Create exec %p", this);
31 simgrid::kernel::activity::ExecImpl::~ExecImpl()
34 surf_action_->unref();
35 if (timeout_detector_)
36 timeout_detector_->unref();
37 XBT_DEBUG("Destroy exec %p", this);
40 void simgrid::kernel::activity::ExecImpl::suspend()
42 XBT_VERB("This exec is suspended (remain: %f)", surf_action_->get_remains());
43 if (surf_action_ != nullptr)
44 surf_action_->suspend();
47 void simgrid::kernel::activity::ExecImpl::resume()
49 XBT_VERB("This exec is resumed (remain: %f)", surf_action_->get_remains());
50 if (surf_action_ != nullptr)
51 surf_action_->resume();
53 void simgrid::kernel::activity::ExecImpl::cancel()
55 XBT_VERB("This exec %p is canceled", this);
56 if (surf_action_ != nullptr)
57 surf_action_->cancel();
60 double simgrid::kernel::activity::ExecImpl::get_remaining()
62 xbt_assert(host_ != nullptr, "Calling remains() on a parallel execution is not allowed. "
63 "We would need to return a vector instead of a scalar. "
64 "Did you mean remainingRatio() instead?");
66 return surf_action_ ? surf_action_->get_remains() : 0;
69 double simgrid::kernel::activity::ExecImpl::get_remaining_ratio()
72 nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::get_remaining())
73 return surf_action_->get_remains();
74 else // Actually compute the ratio for sequential tasks
75 return surf_action_->get_remains() / surf_action_->get_cost();
78 void simgrid::kernel::activity::ExecImpl::set_bound(double bound)
81 surf_action_->set_bound(bound);
83 void simgrid::kernel::activity::ExecImpl::set_priority(double priority)
86 surf_action_->set_priority(priority);
89 void simgrid::kernel::activity::ExecImpl::post()
91 if (host_ && host_->is_off()) { /* FIXME: handle resource failure for parallel tasks too */
92 /* If the host running the synchro failed, notice it. This way, the asking
93 * process can be killed if it runs on that host itself */
94 state_ = SIMIX_FAILED;
95 } else if (surf_action_ && surf_action_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
96 /* If the host running the synchro didn't fail, then the synchro was canceled */
97 state_ = SIMIX_CANCELED;
98 } else if (timeout_detector_ &&
99 timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::FINISHED) {
100 state_ = SIMIX_TIMEOUT;
106 surf_action_->unref();
107 surf_action_ = nullptr;
109 if (timeout_detector_) {
110 timeout_detector_->unref();
111 timeout_detector_ = nullptr;
115 /* If there are simcalls associated with the synchro, then answer them */
116 if (not simcalls_.empty())
117 SIMIX_execution_finish(this);
120 simgrid::kernel::activity::ActivityImpl*
121 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
124 if (not MC_is_active() && not MC_record_replay_is_active()) {
125 simgrid::kernel::resource::Action* old_action = this->surf_action_;
126 simgrid::kernel::resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
127 new_action->set_remains(old_action->get_remains());
128 new_action->set_data(this);
129 new_action->set_priority(old_action->get_priority());
131 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
132 // I hope that the user did not provide any.
134 old_action->set_data(nullptr);
135 old_action->cancel();
137 this->surf_action_ = new_action;
140 on_migration(this, to);
147 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_creation;
148 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_completion;
149 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)>
150 simgrid::kernel::activity::ExecImpl::on_migration;