1 /* Copyright (c) 2007-2019. 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, std::string tracing_category,
19 resource::Action* timeout_detector, s4u::Host* host)
20 : ActivityImpl(name), host_(host), timeout_detector_(timeout_detector)
22 this->state_ = SIMIX_RUNNING;
23 this->set_category(tracing_category);
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::start(double flops_amount, double priority, double bound)
42 if (not MC_is_active() && not MC_record_replay_is_active()) {
43 surf_action_ = host_->pimpl_cpu->execution_start(flops_amount);
44 surf_action_->set_data(this);
45 surf_action_->set_priority(priority);
47 surf_action_->set_bound(bound);
50 XBT_DEBUG("Create execute synchro %p: %s", this, name_.c_str());
51 simgrid::kernel::activity::ExecImpl::on_creation(this);
55 void simgrid::kernel::activity::ExecImpl::cancel()
57 XBT_VERB("This exec %p is canceled", this);
58 if (surf_action_ != nullptr)
59 surf_action_->cancel();
62 double simgrid::kernel::activity::ExecImpl::get_remaining()
64 xbt_assert(host_ != nullptr, "Calling remains() on a parallel execution is not allowed. "
65 "We would need to return a vector instead of a scalar. "
66 "Did you mean remainingRatio() instead?");
67 return surf_action_ ? surf_action_->get_remains() : 0;
70 double simgrid::kernel::activity::ExecImpl::get_remaining_ratio()
73 nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::get_remaining())
74 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
75 else // Actually compute the ratio for sequential tasks
76 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
79 void simgrid::kernel::activity::ExecImpl::set_bound(double bound)
82 surf_action_->set_bound(bound);
84 void simgrid::kernel::activity::ExecImpl::set_priority(double priority)
87 surf_action_->set_priority(priority);
90 void simgrid::kernel::activity::ExecImpl::post()
92 if (host_ && host_->is_off()) { /* FIXME: handle resource failure for parallel tasks too */
93 /* If the host running the synchro failed, notice it. This way, the asking
94 * process can be killed if it runs on that host itself */
95 state_ = SIMIX_FAILED;
96 } else if (surf_action_ && surf_action_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
97 /* If the host running the synchro didn't fail, then the synchro was canceled */
98 state_ = SIMIX_CANCELED;
99 } else if (timeout_detector_ &&
100 timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::FINISHED) {
101 state_ = SIMIX_TIMEOUT;
109 surf_action_->unref();
110 surf_action_ = nullptr;
112 if (timeout_detector_) {
113 timeout_detector_->unref();
114 timeout_detector_ = nullptr;
117 /* If there are simcalls associated with the synchro, then answer them */
118 if (not simcalls_.empty())
119 SIMIX_execution_finish(this);
122 simgrid::kernel::activity::ActivityImpl*
123 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
126 if (not MC_is_active() && not MC_record_replay_is_active()) {
127 simgrid::kernel::resource::Action* old_action = this->surf_action_;
128 simgrid::kernel::resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
129 new_action->set_remains(old_action->get_remains());
130 new_action->set_data(this);
131 new_action->set_priority(old_action->get_priority());
133 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
134 // I hope that the user did not provide any.
136 old_action->set_data(nullptr);
137 old_action->cancel();
139 this->surf_action_ = new_action;
142 on_migration(this, to);
149 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_creation;
150 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_completion;
151 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)>
152 simgrid::kernel::activity::ExecImpl::on_migration;