Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add jarfile generation script for github, using actions.
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2020. 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 "src/kernel/activity/ExecImpl.hpp"
7 #include "simgrid/Exception.hpp"
8 #include "simgrid/modelchecker.h"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/surf/HostImpl.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 #include "simgrid/s4u/Host.hpp"
15
16 #include <boost/range/algorithm.hpp>
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
19
20 void simcall_HANDLER_execution_waitany_for(smx_simcall_t simcall, simgrid::kernel::activity::ExecImpl* execs[],
21                                            size_t count, double timeout)
22 {
23   if (timeout < 0.0) {
24     simcall->timeout_cb_ = nullptr;
25   } else {
26     simcall->timeout_cb_ = simgrid::simix::Timer::set(SIMIX_get_clock() + timeout, [simcall, execs, count]() {
27       simcall->timeout_cb_ = nullptr;
28       for (size_t i = 0; i < count; i++) {
29         // Remove the first occurrence of simcall:
30         auto* exec = execs[i];
31         auto j     = boost::range::find(exec->simcalls_, simcall);
32         if (j != exec->simcalls_.end())
33           exec->simcalls_.erase(j);
34       }
35       simcall_execution_waitany_for__set__result(simcall, -1);
36       simcall->issuer_->simcall_answer();
37     });
38   }
39
40   for (size_t i = 0; i < count; i++) {
41     /* associate this simcall to the the synchro */
42     auto* exec = execs[i];
43     exec->simcalls_.push_back(simcall);
44
45     /* see if the synchro is already finished */
46     if (exec->state_ != simgrid::kernel::activity::State::WAITING &&
47         exec->state_ != simgrid::kernel::activity::State::RUNNING) {
48       exec->finish();
49       break;
50     }
51   }
52 }
53
54 namespace simgrid {
55 namespace kernel {
56 namespace activity {
57
58 ExecImpl::ExecImpl()
59 {
60   actor::ActorImpl* self = actor::ActorImpl::self();
61   if (self) {
62     actor_ = self;
63     self->activities_.push_back(this);
64   }
65 }
66
67 ExecImpl& ExecImpl::set_host(s4u::Host* host)
68 {
69   hosts_.assign(1, host);
70   return *this;
71 }
72
73 ExecImpl& ExecImpl::set_hosts(const std::vector<s4u::Host*>& hosts)
74 {
75   hosts_ = hosts;
76   return *this;
77 }
78
79 ExecImpl& ExecImpl::set_timeout(double timeout)
80 {
81   if (timeout > 0 && not MC_is_active() && not MC_record_replay_is_active()) {
82     timeout_detector_.reset(hosts_.front()->pimpl_cpu->sleep(timeout));
83     timeout_detector_->set_activity(this);
84   }
85   return *this;
86 }
87
88 ExecImpl& ExecImpl::set_flops_amount(double flops_amount)
89 {
90   flops_amounts_.assign(1, flops_amount);
91   return *this;
92 }
93
94 ExecImpl& ExecImpl::set_flops_amounts(const std::vector<double>& flops_amounts)
95 {
96   flops_amounts_ = flops_amounts;
97   return *this;
98 }
99
100 ExecImpl& ExecImpl::set_bytes_amounts(const std::vector<double>& bytes_amounts)
101 {
102   bytes_amounts_ = bytes_amounts;
103
104   return *this;
105 }
106
107 ExecImpl* ExecImpl::start()
108 {
109   state_ = State::RUNNING;
110   if (not MC_is_active() && not MC_record_replay_is_active()) {
111     if (hosts_.size() == 1) {
112       surf_action_ = hosts_.front()->pimpl_cpu->execution_start(flops_amounts_.front());
113       surf_action_->set_sharing_penalty(sharing_penalty_);
114       surf_action_->set_category(get_tracing_category());
115
116       if (bound_ > 0)
117         surf_action_->set_bound(bound_);
118     } else {
119       surf_action_ = surf_host_model->execute_parallel(hosts_, flops_amounts_.data(), bytes_amounts_.data(), -1);
120     }
121     surf_action_->set_activity(this);
122   }
123
124   XBT_DEBUG("Create execute synchro %p: %s", this, get_cname());
125   return this;
126 }
127
128 double ExecImpl::get_seq_remaining_ratio()
129 {
130   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains() / surf_action_->get_cost();
131 }
132
133 double ExecImpl::get_par_remaining_ratio()
134 {
135   // parallel task: their remain is already between 0 and 1
136   return (surf_action_ == nullptr) ? 0 : surf_action_->get_remains();
137 }
138
139 ExecImpl& ExecImpl::set_bound(double bound)
140 {
141   bound_ = bound;
142   return *this;
143 }
144
145 ExecImpl& ExecImpl::set_sharing_penalty(double sharing_penalty)
146 {
147   sharing_penalty_ = sharing_penalty;
148   return *this;
149 }
150
151 void ExecImpl::post()
152 {
153   if (hosts_.size() == 1 && not hosts_.front()->is_on()) { /* FIXME: handle resource failure for parallel tasks too */
154     /* If the host running the synchro failed, notice it. This way, the asking
155      * process can be killed if it runs on that host itself */
156     state_ = State::FAILED;
157   } else if (surf_action_ && surf_action_->get_state() == resource::Action::State::FAILED) {
158     /* If the host running the synchro didn't fail, then the synchro was canceled */
159     state_ = State::CANCELED;
160   } else if (timeout_detector_ && timeout_detector_->get_state() == resource::Action::State::FINISHED) {
161     state_ = State::TIMEOUT;
162   } else {
163     state_ = State::DONE;
164   }
165
166   clean_action();
167   timeout_detector_.reset();
168   if (actor_) {
169     actor_->activities_.remove(this);
170     actor_ = nullptr;
171   }
172   /* Answer all simcalls associated with the synchro */
173   finish();
174 }
175
176 void ExecImpl::finish()
177 {
178   while (not simcalls_.empty()) {
179     smx_simcall_t simcall = simcalls_.front();
180     simcalls_.pop_front();
181
182     /* If a waitany simcall is waiting for this synchro to finish, then remove it from the other synchros in the waitany
183      * list. Afterwards, get the position of the actual synchro in the waitany list and return it as the result of the
184      * simcall */
185
186     if (simcall->call_ == SIMCALL_NONE) // FIXME: maybe a better way to handle this case
187       continue;                        // if process handling comm is killed
188     if (simcall->call_ == SIMCALL_EXECUTION_WAITANY_FOR) {
189       simgrid::kernel::activity::ExecImpl** execs = simcall_execution_waitany_for__get__execs(simcall);
190       size_t count                                = simcall_execution_waitany_for__get__count(simcall);
191
192       for (size_t i = 0; i < count; i++) {
193         // Remove the first occurrence of simcall:
194         auto* exec = execs[i];
195         auto j     = boost::range::find(exec->simcalls_, simcall);
196         if (j != exec->simcalls_.end())
197           exec->simcalls_.erase(j);
198
199         if (simcall->timeout_cb_) {
200           simcall->timeout_cb_->remove();
201           simcall->timeout_cb_ = nullptr;
202         }
203       }
204
205       if (not MC_is_active() && not MC_record_replay_is_active()) {
206         ExecImpl** element = std::find(execs, execs + count, this);
207         int rank           = (element != execs + count) ? element - execs : -1;
208         simcall_execution_waitany_for__set__result(simcall, rank);
209       }
210     }
211
212     switch (state_) {
213       case State::DONE:
214         /* do nothing, synchro done */
215         XBT_DEBUG("ExecImpl::finish(): execution successful");
216         break;
217
218       case State::FAILED:
219         XBT_DEBUG("ExecImpl::finish(): host '%s' failed", simcall->issuer_->get_host()->get_cname());
220         simcall->issuer_->context_->set_wannadie();
221         if (simcall->issuer_->get_host()->is_on())
222           simcall->issuer_->exception_ =
223               std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
224         /* else, the actor will be killed with no possibility to survive */
225         break;
226
227       case State::CANCELED:
228         XBT_DEBUG("ExecImpl::finish(): execution canceled");
229         simcall->issuer_->exception_ =
230             std::make_exception_ptr(simgrid::CancelException(XBT_THROW_POINT, "Execution Canceled"));
231         break;
232
233       case State::TIMEOUT:
234         XBT_DEBUG("ExecImpl::finish(): execution timeouted");
235         simcall->issuer_->exception_ = std::make_exception_ptr(simgrid::TimeoutException(XBT_THROW_POINT, "Timeouted"));
236         break;
237
238       default:
239         xbt_die("Internal error in ExecImpl::finish(): unexpected synchro state %d", static_cast<int>(state_));
240     }
241
242     simcall->issuer_->waiting_synchro_ = nullptr;
243     /* Fail the process if the host is down */
244     if (simcall->issuer_->get_host()->is_on())
245       simcall->issuer_->simcall_answer();
246     else
247       simcall->issuer_->context_->set_wannadie();
248   }
249 }
250
251 ActivityImpl* ExecImpl::migrate(s4u::Host* to)
252 {
253   if (not MC_is_active() && not MC_record_replay_is_active()) {
254     resource::Action* old_action = this->surf_action_;
255     resource::Action* new_action = to->pimpl_cpu->execution_start(old_action->get_cost());
256     new_action->set_remains(old_action->get_remains());
257     new_action->set_activity(this);
258     new_action->set_sharing_penalty(old_action->get_sharing_penalty());
259
260     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
261     // I hope that the user did not provide any.
262
263     old_action->set_activity(nullptr);
264     old_action->cancel();
265     old_action->unref();
266     this->surf_action_ = new_action;
267   }
268
269   on_migration(*this, to);
270   return this;
271 }
272
273 /*************
274  * Callbacks *
275  *************/
276 xbt::signal<void(ExecImpl const&, s4u::Host*)> ExecImpl::on_migration;
277
278 } // namespace activity
279 } // namespace kernel
280 } // namespace simgrid