Logo AND Algorithmique Numérique Distribuée

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