Logo AND Algorithmique Numérique Distribuée

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