Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stringify, snake_case and cleanups in k::ExecImpl
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/modelchecker.h"
7 #include "src/mc/mc_replay.hpp"
8
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"
13
14 #include "simgrid/s4u/Host.hpp"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
17
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)
21 {
22   this->state_ = SIMIX_RUNNING;
23
24   surf_action_->set_data(this);
25   if (timeout_detector != nullptr)
26     timeout_detector->set_data(this);
27
28   XBT_DEBUG("Create exec %p", this);
29 }
30
31 simgrid::kernel::activity::ExecImpl::~ExecImpl()
32 {
33   if (surf_action_)
34     surf_action_->unref();
35   if (timeout_detector_)
36     timeout_detector_->unref();
37   XBT_DEBUG("Destroy exec %p", this);
38 }
39
40 void simgrid::kernel::activity::ExecImpl::suspend()
41 {
42   XBT_VERB("This exec is suspended (remain: %f)", surf_action_->get_remains());
43   if (surf_action_ != nullptr)
44     surf_action_->suspend();
45 }
46
47 void simgrid::kernel::activity::ExecImpl::resume()
48 {
49   XBT_VERB("This exec is resumed (remain: %f)", surf_action_->get_remains());
50   if (surf_action_ != nullptr)
51     surf_action_->resume();
52 }
53 void simgrid::kernel::activity::ExecImpl::cancel()
54 {
55   XBT_VERB("This exec %p is canceled", this);
56   if (surf_action_ != nullptr)
57     surf_action_->cancel();
58 }
59
60 double simgrid::kernel::activity::ExecImpl::get_remaining()
61 {
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?");
65
66   return surf_action_ ? surf_action_->get_remains() : 0;
67 }
68
69 double simgrid::kernel::activity::ExecImpl::get_remaining_ratio()
70 {
71   if (host_ ==
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();
76 }
77
78 void simgrid::kernel::activity::ExecImpl::set_bound(double bound)
79 {
80   if (surf_action_)
81     surf_action_->set_bound(bound);
82 }
83 void simgrid::kernel::activity::ExecImpl::set_priority(double priority)
84 {
85   if (surf_action_)
86     surf_action_->set_priority(priority);
87 }
88
89 void simgrid::kernel::activity::ExecImpl::post()
90 {
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;
101   } else {
102     state_ = SIMIX_DONE;
103   }
104
105   if (surf_action_) {
106     surf_action_->unref();
107     surf_action_ = nullptr;
108   }
109   if (timeout_detector_) {
110     timeout_detector_->unref();
111     timeout_detector_ = nullptr;
112   }
113
114   on_completion(this);
115   /* If there are simcalls associated with the synchro, then answer them */
116   if (not simcalls_.empty())
117     SIMIX_execution_finish(this);
118 }
119
120 simgrid::kernel::activity::ActivityImpl*
121 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
122 {
123
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());
130
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.
133
134     old_action->set_data(nullptr);
135     old_action->cancel();
136     old_action->unref();
137     this->surf_action_ = new_action;
138   }
139
140   on_migration(this, to);
141   return this;
142 }
143
144 /*************
145  * Callbacks *
146  *************/
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;