Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d233924b568d460b1b30e4655f952a1cbd9359fb
[simgrid.git] / src / kernel / activity / ExecImpl.cpp
1 /* Copyright (c) 2007-2017. 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
15 #include "simgrid/s4u/Host.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
18
19 simgrid::kernel::activity::ExecImpl::ExecImpl(const char* name, sg_host_t host) : host_(host)
20 {
21   if (name)
22     this->name = name;
23   this->state  = SIMIX_RUNNING;
24   XBT_DEBUG("Create exec %p", this);
25 }
26
27 simgrid::kernel::activity::ExecImpl::~ExecImpl()
28 {
29   if (surfAction_)
30     surfAction_->unref();
31   if (timeoutDetector)
32     timeoutDetector->unref();
33   XBT_DEBUG("Destroy exec %p", this);
34 }
35 void simgrid::kernel::activity::ExecImpl::suspend()
36 {
37   XBT_VERB("This exec is suspended (remain: %f)", surfAction_->getRemains());
38   if (surfAction_)
39     surfAction_->suspend();
40 }
41
42 void simgrid::kernel::activity::ExecImpl::resume()
43 {
44   XBT_VERB("This exec is resumed (remain: %f)", surfAction_->getRemains());
45   if (surfAction_)
46     surfAction_->resume();
47 }
48
49 double simgrid::kernel::activity::ExecImpl::remains()
50 {
51   xbt_assert(host_ != nullptr, "Calling remains() on a parallel execution is not allowed. "
52                                "We would need to return a vector instead of a scalar. "
53                                "Did you meant remainingRatio() instead?");
54
55   return surfAction_ ? surfAction_->getRemains() : 0;
56 }
57 double simgrid::kernel::activity::ExecImpl::remainingRatio()
58 {
59   if (host_ == nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::remains())
60     return surfAction_->getRemains();
61   else // Actually compute the ratio for sequential tasks
62     return surfAction_->getRemains() / surfAction_->getCost();
63 }
64
65 void simgrid::kernel::activity::ExecImpl::post()
66 {
67   if (host_ && host_->isOff()) { /* FIXME: handle resource failure for parallel tasks too */
68                                  /* If the host running the synchro failed, notice it. This way, the asking
69                                   * process can be killed if it runs on that host itself */
70     state = SIMIX_FAILED;
71   } else if (surfAction_->getState() == simgrid::surf::Action::State::failed) {
72     /* If the host running the synchro didn't fail, then the synchro was canceled */
73     state = SIMIX_CANCELED;
74   } else if (timeoutDetector && timeoutDetector->getState() == simgrid::surf::Action::State::done) {
75     state = SIMIX_TIMEOUT;
76   } else {
77     state = SIMIX_DONE;
78   }
79
80   if (surfAction_) {
81     surfAction_->unref();
82     surfAction_ = nullptr;
83   }
84   if (timeoutDetector) {
85     timeoutDetector->unref();
86     timeoutDetector = nullptr;
87   }
88
89   onCompletion(this);
90   /* If there are simcalls associated with the synchro, then answer them */
91   if (not simcalls.empty())
92     SIMIX_execution_finish(this);
93 }
94
95 simgrid::kernel::activity::ActivityImpl*
96 simgrid::kernel::activity::ExecImpl::migrate(simgrid::s4u::Host* to)
97 {
98
99   if (not MC_is_active() && not MC_record_replay_is_active()) {
100     surf_action_t oldAction = this->surfAction_;
101     surf_action_t newAction = to->pimpl_cpu->execution_start(oldAction->getCost());
102     newAction->setRemains(oldAction->getRemains());
103     newAction->setData(this);
104     newAction->setSharingWeight(oldAction->getPriority());
105
106     // FIXME: the user-defined bound seem to not be kept by LMM, that seem to overwrite it for the multi-core modeling.
107     // I hope that the user did not provide any.
108
109     oldAction->setData(nullptr);
110     oldAction->cancel();
111     oldAction->unref();
112     this->surfAction_ = newAction;
113   }
114
115   onMigration(this, to);
116   return this;
117 }
118
119
120 /*************
121  * Callbacks *
122  *************/
123 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCreation;
124 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr)> simgrid::kernel::activity::ExecImpl::onCompletion;
125 simgrid::xbt::signal<void(simgrid::kernel::activity::ExecImplPtr, simgrid::s4u::Host*)> simgrid::kernel::activity::ExecImpl::onMigration;