Logo AND Algorithmique Numérique Distribuée

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