Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a0e3cc2c9d29328be6a764c475eec093d9c102e3
[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_ &&
104              timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::FINISHED) {
105     state_ = SIMIX_TIMEOUT;
106   } else {
107     state_ = SIMIX_DONE;
108   }
109
110   if (surf_action_) {
111     surf_action_->unref();
112     surf_action_ = nullptr;
113   }
114   if (timeout_detector_) {
115     timeout_detector_->unref();
116     timeout_detector_ = nullptr;
117   }
118
119   onCompletion(this);
120   /* If there are simcalls associated with the synchro, then answer them */
121   if (not simcalls_.empty())
122     SIMIX_execution_finish(this);
123 }
124
125 simgrid::kernel::activity::ActivityImpl*
126 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
127 {
128
129   if (not MC_is_active() && not MC_record_replay_is_active()) {
130     simgrid::kernel::resource::Action* old_action = this->surf_action_;
131     simgrid::kernel::resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
132     new_action->set_remains(old_action->get_remains());
133     new_action->set_data(this);
134     new_action->set_priority(old_action->get_priority());
135
136     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
137     // I hope that the user did not provide any.
138
139     old_action->set_data(nullptr);
140     old_action->cancel();
141     old_action->unref();
142     this->surf_action_ = new_action;
143   }
144
145   onMigration(this, to);
146   return this;
147 }
148
149 /*************
150  * Callbacks *
151  *************/
152 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCreation;
153 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCompletion;
154 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)> simgrid::kernel::activity::ExecImpl::onMigration;