Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dc7dad509e755fe6645d550085b5ea268947c4c8
[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::post()
90 {
91   if (host_ && host_->is_off()) { /* FIXME: handle resource failure for parallel tasks too */
92     /* If the host running the synchro failed, notice it. This way, the asking
93      * process can be killed if it runs on that host itself */
94     state_ = SIMIX_FAILED;
95   } else if (surf_action_ && surf_action_->get_state() == simgrid::kernel::resource::Action::State::FAILED) {
96     /* If the host running the synchro didn't fail, then the synchro was canceled */
97     state_ = SIMIX_CANCELED;
98   } else if (timeout_detector_ &&
99              timeout_detector_->get_state() == simgrid::kernel::resource::Action::State::FINISHED) {
100     state_ = SIMIX_TIMEOUT;
101   } else {
102     state_ = SIMIX_DONE;
103   }
104
105   on_completion(this);
106
107   if (surf_action_) {
108     surf_action_->unref();
109     surf_action_ = nullptr;
110   }
111   if (timeout_detector_) {
112     timeout_detector_->unref();
113     timeout_detector_ = nullptr;
114   }
115
116   /* If there are simcalls associated with the synchro, then answer them */
117   if (not simcalls_.empty())
118     SIMIX_execution_finish(this);
119 }
120
121 simgrid::kernel::activity::ActivityImpl*
122 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
123 {
124
125   if (not MC_is_active() && not MC_record_replay_is_active()) {
126     simgrid::kernel::resource::Action* old_action = this->surf_action_;
127     simgrid::kernel::resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
128     new_action->set_remains(old_action->get_remains());
129     new_action->set_data(this);
130     new_action->set_priority(old_action->get_priority());
131
132     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
133     // I hope that the user did not provide any.
134
135     old_action->set_data(nullptr);
136     old_action->cancel();
137     old_action->unref();
138     this->surf_action_ = new_action;
139   }
140
141   on_migration(this, to);
142   return this;
143 }
144
145 /*************
146  * Callbacks *
147  *************/
148 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_creation;
149 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::on_completion;
150 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)>
151     simgrid::kernel::activity::ExecImpl::on_migration;