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);
21 ExecImpl::ExecImpl(std::string name, std::string tracing_category, resource::Action* timeout_detector, s4u::Host* host)
22 : ActivityImpl(std::move(name)), host_(host), timeout_detector_(timeout_detector)
24 this->state_ = SIMIX_RUNNING;
25 this->set_category(std::move(tracing_category));
27 if (timeout_detector != nullptr)
28 timeout_detector_->set_data(this);
30 XBT_DEBUG("Create exec %p", this);
36 surf_action_->unref();
37 if (timeout_detector_)
38 timeout_detector_->unref();
39 XBT_DEBUG("Destroy exec %p", this);
42 ExecImpl* ExecImpl::start(double flops_amount, double priority, double bound)
44 if (not MC_is_active() && not MC_record_replay_is_active()) {
45 surf_action_ = host_->pimpl_cpu->execution_start(flops_amount);
46 surf_action_->set_data(this);
47 surf_action_->set_priority(priority);
49 surf_action_->set_bound(bound);
52 XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
53 ExecImpl::on_creation(this);
57 void ExecImpl::cancel()
59 XBT_VERB("This exec %p is canceled", this);
60 if (surf_action_ != nullptr)
61 surf_action_->cancel();
64 double ExecImpl::get_remaining()
66 xbt_assert(host_ != nullptr, "Calling remains() on a parallel execution is not allowed. "
67 "We would need to return a vector instead of a scalar. "
68 "Did you mean remainingRatio() instead?");
69 return surf_action_ ? surf_action_->get_remains() : 0;
72 double ExecImpl::get_remaining_ratio()
75 nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::get_remaining())
76 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
77 else // Actually compute the ratio for sequential tasks
78 return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
81 void ExecImpl::set_bound(double bound)
84 surf_action_->set_bound(bound);
86 void ExecImpl::set_priority(double priority)
89 surf_action_->set_priority(priority);
94 if (host_ && not host_->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
95 /* If the host running the synchro failed, notice it. This way, the asking
96 * process can be killed if it runs on that host itself */
97 state_ = SIMIX_FAILED;
98 } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
99 /* If the host running the synchro didn't fail, then the synchro was canceled */
100 state_ = SIMIX_CANCELED;
101 } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
102 state_ = SIMIX_TIMEOUT;
110 surf_action_->unref();
111 surf_action_ = nullptr;
113 if (timeout_detector_) {
114 timeout_detector_->unref();
115 timeout_detector_ = nullptr;
118 /* If there are simcalls associated with the synchro, then answer them */
119 if (not simcalls_.empty())
120 SIMIX_execution_finish(this);
123 ActivityImpl* ExecImpl::migrate(simgrid::s4u::Host* to)
125 if (not MC_is_active() && not MC_record_replay_is_active()) {
126 resource::Action* old_action = this->surf_action_;
127 resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
128 new_action->set_remains(old_action->get_remains());
129 new_action->set_data(this);
130 new_action->set_priority(old_action->get_priority());
132 // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
133 // I hope that the user did not provide any.
135 old_action->set_data(nullptr);
136 old_action->cancel();
138 this->surf_action_ = new_action;
141 on_migration(this, to);
148 xbt::signal<void(ExecImplPtr)> ExecImpl::on_creation;
149 xbt::signal<void(ExecImplPtr)> ExecImpl::on_completion;
150 xbt::signal<void(ExecImplPtr, s4u::Host*)> ExecImpl::on_migration;
152 } // namespace activity
153 } // namespace kernel
154 } // namespace simgrid