Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics: rename 2 fields
[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_ == nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::remains())
77     return surf_action_->get_remains();
78   else // Actually compute the ratio for sequential tasks
79     return surf_action_->get_remains() / surf_action_->get_cost();
80 }
81
82 void simgrid::kernel::activity::ExecImpl::set_bound(double bound)
83 {
84   if (surf_action_)
85     surf_action_->set_bound(bound);
86 }
87 void simgrid::kernel::activity::ExecImpl::set_priority(double priority)
88 {
89   if (surf_action_)
90     surf_action_->set_priority(priority);
91 }
92
93 void simgrid::kernel::activity::ExecImpl::post()
94 {
95   if (host_ && host_->isOff()) { /* FIXME: handle resource failure for parallel tasks too */
96                                  /* If the host running the synchro failed, notice it. This way, the asking
97                                   * process can be killed if it runs on that host itself */
98     state = SIMIX_FAILED;
99   } else if (surf_action_ && surf_action_->get_state() == simgrid::kernel::resource::Action::State::failed) {
100     /* If the host running the synchro didn't fail, then the synchro was canceled */
101     state = SIMIX_CANCELED;
102   } else if (timeout_detector_ && timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::done) {
103     state = SIMIX_TIMEOUT;
104   } else {
105     state = SIMIX_DONE;
106   }
107
108   if (surf_action_) {
109     surf_action_->unref();
110     surf_action_ = nullptr;
111   }
112   if (timeout_detector_) {
113     timeout_detector_->unref();
114     timeout_detector_ = nullptr;
115   }
116
117   onCompletion(this);
118   /* If there are simcalls associated with the synchro, then answer them */
119   if (not simcalls.empty())
120     SIMIX_execution_finish(this);
121 }
122
123 simgrid::kernel::activity::ActivityImpl*
124 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
125 {
126
127   if (not MC_is_active() && not MC_record_replay_is_active()) {
128     simgrid::kernel::resource::Action* oldAction = this->surf_action_;
129     simgrid::kernel::resource::Action* newAction = to->pimpl_cpu->execution_start(oldAction->get_cost());
130     newAction->set_remains(oldAction->get_remains());
131     newAction->set_data(this);
132     newAction->set_priority(oldAction->get_priority());
133
134     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
135     // I hope that the user did not provide any.
136
137     oldAction->set_data(nullptr);
138     oldAction->cancel();
139     oldAction->unref();
140     this->surf_action_ = newAction;
141   }
142
143   onMigration(this, to);
144   return this;
145 }
146
147 /*************
148  * Callbacks *
149  *************/
150 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCreation;
151 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCompletion;
152 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)> simgrid::kernel::activity::ExecImpl::onMigration;