Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics: snake_case some random local variables, fields and non-s4u methods
[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(const char* name, resource::Action* surf_action,
19                                               resource::Action* timeout_detector, s4u::Host* host)
20     : host_(host)
21 {
22   if (name)
23     this->name_ = name;
24   this->state_ = SIMIX_RUNNING;
25
26   surf_action_ = surf_action;
27   surf_action_->set_data(this);
28   if (timeout_detector != nullptr) {
29     timeout_detector->set_data(this);
30     timeout_detector_ = timeout_detector;
31   }
32
33   XBT_DEBUG("Create exec %p", this);
34 }
35
36 simgrid::kernel::activity::ExecImpl::~ExecImpl()
37 {
38   if (surf_action_)
39     surf_action_->unref();
40   if (timeout_detector_)
41     timeout_detector_->unref();
42   XBT_DEBUG("Destroy exec %p", this);
43 }
44
45 void simgrid::kernel::activity::ExecImpl::suspend()
46 {
47   XBT_VERB("This exec is suspended (remain: %f)", surf_action_->get_remains());
48   if (surf_action_ != nullptr)
49     surf_action_->suspend();
50 }
51
52 void simgrid::kernel::activity::ExecImpl::resume()
53 {
54   XBT_VERB("This exec is resumed (remain: %f)", surf_action_->get_remains());
55   if (surf_action_ != nullptr)
56     surf_action_->resume();
57 }
58 void simgrid::kernel::activity::ExecImpl::cancel()
59 {
60   XBT_VERB("This exec %p is canceled", this);
61   if (surf_action_ != nullptr)
62     surf_action_->cancel();
63 }
64
65 double simgrid::kernel::activity::ExecImpl::get_remaining()
66 {
67   xbt_assert(host_ != nullptr, "Calling remains() on a parallel execution is not allowed. "
68                                "We would need to return a vector instead of a scalar. "
69                                "Did you mean remainingRatio() instead?");
70
71   return surf_action_ ? surf_action_->get_remains() : 0;
72 }
73
74 double simgrid::kernel::activity::ExecImpl::get_remaining_ratio()
75 {
76   if (host_ ==
77       nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::get_remaining())
78     return surf_action_->get_remains();
79   else // Actually compute the ratio for sequential tasks
80     return surf_action_->get_remains() / surf_action_->get_cost();
81 }
82
83 void simgrid::kernel::activity::ExecImpl::set_bound(double bound)
84 {
85   if (surf_action_)
86     surf_action_->set_bound(bound);
87 }
88 void simgrid::kernel::activity::ExecImpl::set_priority(double priority)
89 {
90   if (surf_action_)
91     surf_action_->set_priority(priority);
92 }
93
94 void simgrid::kernel::activity::ExecImpl::post()
95 {
96   if (host_ && host_->isOff()) { /* FIXME: handle resource failure for parallel tasks too */
97                                  /* If the host running the synchro failed, notice it. This way, the asking
98                                   * process can be killed if it runs on that host itself */
99                                  state_ = SIMIX_FAILED;
100   } else if (surf_action_ && surf_action_->get_state() == simgrid::kernel::resource::Action::State::failed) {
101     /* If the host running the synchro didn't fail, then the synchro was canceled */
102     state_ = SIMIX_CANCELED;
103   } else if (timeout_detector_ && timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::done) {
104     state_ = SIMIX_TIMEOUT;
105   } else {
106     state_ = SIMIX_DONE;
107   }
108
109   if (surf_action_) {
110     surf_action_->unref();
111     surf_action_ = nullptr;
112   }
113   if (timeout_detector_) {
114     timeout_detector_->unref();
115     timeout_detector_ = nullptr;
116   }
117
118   onCompletion(this);
119   /* If there are simcalls associated with the synchro, then answer them */
120   if (not simcalls_.empty())
121     SIMIX_execution_finish(this);
122 }
123
124 simgrid::kernel::activity::ActivityImpl*
125 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
126 {
127
128   if (not MC_is_active() && not MC_record_replay_is_active()) {
129     simgrid::kernel::resource::Action* old_action = this->surf_action_;
130     simgrid::kernel::resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
131     new_action->set_remains(old_action->get_remains());
132     new_action->set_data(this);
133     new_action->set_priority(old_action->get_priority());
134
135     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
136     // I hope that the user did not provide any.
137
138     old_action->set_data(nullptr);
139     old_action->cancel();
140     old_action->unref();
141     this->surf_action_ = new_action;
142   }
143
144   onMigration(this, to);
145   return this;
146 }
147
148 /*************
149  * Callbacks *
150  *************/
151 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCreation;
152 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCompletion;
153 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)> simgrid::kernel::activity::ExecImpl::onMigration;