Logo AND Algorithmique Numérique Distribuée

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