Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0edc38596b2c1d6c69dca7bc2c95af47d61dcd1d
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2019. 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, surf_action), host_(host), 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   on_suspended(this);
46 }
47
48 void simgrid::kernel::activity::ExecImpl::resume()
49 {
50   XBT_VERB("This exec is resumed (remain: %f)", surf_action_->get_remains());
51   if (surf_action_ != nullptr)
52     surf_action_->resume();
53   on_resumed(this);
54 }
55 void simgrid::kernel::activity::ExecImpl::cancel()
56 {
57   XBT_VERB("This exec %p is canceled", this);
58   if (surf_action_ != nullptr)
59     surf_action_->cancel();
60 }
61
62 double simgrid::kernel::activity::ExecImpl::get_remaining()
63 {
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
68   return surf_action_ ? surf_action_->get_remains() : 0;
69 }
70
71 double simgrid::kernel::activity::ExecImpl::get_remaining_ratio()
72 {
73   if (host_ ==
74       nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::get_remaining())
75     return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
76   else // Actually compute the ratio for sequential tasks
77     return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
78 }
79
80 void simgrid::kernel::activity::ExecImpl::set_bound(double bound)
81 {
82   if (surf_action_)
83     surf_action_->set_bound(bound);
84 }
85 void simgrid::kernel::activity::ExecImpl::set_priority(double priority)
86 {
87   if (surf_action_)
88     surf_action_->set_priority(priority);
89 }
90
91 void simgrid::kernel::activity::ExecImpl::set_category(std::string category)
92 {
93   if (surf_action_)
94     surf_action_->set_category(category);
95 }
96
97 void simgrid::kernel::activity::ExecImpl::post()
98 {
99   if (host_ && host_->is_off()) { /* FIXME: handle resource failure for parallel tasks too */
100     /* If the host running the synchro failed, notice it. This way, the asking
101      * process can be killed if it runs on that host itself */
102     state_ = SIMIX_FAILED;
103   } else if (surf_action_ && surf_action_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
104     /* If the host running the synchro didn't fail, then the synchro was canceled */
105     state_ = SIMIX_CANCELED;
106   } else if (timeout_detector_ &&
107              timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::FINISHED) {
108     state_ = SIMIX_TIMEOUT;
109   } else {
110     state_ = SIMIX_DONE;
111   }
112
113   on_completion(this);
114
115   if (surf_action_) {
116     surf_action_->unref();
117     surf_action_ = nullptr;
118   }
119   if (timeout_detector_) {
120     timeout_detector_->unref();
121     timeout_detector_ = nullptr;
122   }
123
124   /* If there are simcalls associated with the synchro, then answer them */
125   if (not simcalls_.empty())
126     SIMIX_execution_finish(this);
127 }
128
129 simgrid::kernel::activity::ActivityImpl*
130 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
131 {
132
133   if (not MC_is_active() && not MC_record_replay_is_active()) {
134     simgrid::kernel::resource::Action* old_action = this->surf_action_;
135     simgrid::kernel::resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
136     new_action->set_remains(old_action->get_remains());
137     new_action->set_data(this);
138     new_action->set_priority(old_action->get_priority());
139
140     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
141     // I hope that the user did not provide any.
142
143     old_action->set_data(nullptr);
144     old_action->cancel();
145     old_action->unref();
146     this->surf_action_ = new_action;
147   }
148
149   on_migration(this, to);
150   return this;
151 }
152
153 /*************
154  * Callbacks *
155  *************/
156 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_creation;
157 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_completion;
158 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_resumed;
159 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_suspended;
160 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)>
161     simgrid::kernel::activity::ExecImpl::on_migration;