Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9dba149c180548c775ed493c51eee31c309a7216
[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/s4u/Host.hpp"
7
8 #include "src/kernel/activity/ExecImpl.hpp"
9 #include "src/simix/smx_host_private.hpp"
10 #include "src/surf/surf_interface.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_process);
13
14 simgrid::kernel::activity::ExecImpl::ExecImpl(const char* name, sg_host_t host) : host_(host)
15 {
16   if (name)
17     this->name = name;
18   this->state  = SIMIX_RUNNING;
19   XBT_DEBUG("Create exec %p", this);
20 }
21
22 simgrid::kernel::activity::ExecImpl::~ExecImpl()
23 {
24   if (surfAction_)
25     surfAction_->unref();
26   if (timeoutDetector)
27     timeoutDetector->unref();
28   XBT_DEBUG("Destroy exec %p", this);
29 }
30 void simgrid::kernel::activity::ExecImpl::suspend()
31 {
32   XBT_VERB("This exec is suspended (remain: %f)", surfAction_->getRemains());
33   if (surfAction_)
34     surfAction_->suspend();
35 }
36
37 void simgrid::kernel::activity::ExecImpl::resume()
38 {
39   XBT_VERB("This exec is resumed (remain: %f)", surfAction_->getRemains());
40   if (surfAction_)
41     surfAction_->resume();
42 }
43
44 double simgrid::kernel::activity::ExecImpl::remains()
45 {
46   static std::logic_error e(
47       "The remaining work on parallel tasks cannot be defined as a scalar amount of flops (it's a vector). "
48       "So parallel_task->remains() is not defined. "
49       "You are probably looking for parallel_task->remainingRatio().");
50
51   if (host_ == nullptr) // parallel task: their remain is not in flops (we'd need a vector for that, not a scalar)
52     throw &e;
53   else // sequential task: everything's fine
54     return surfAction_->getRemains();
55 }
56 double simgrid::kernel::activity::ExecImpl::remainingRatio()
57 {
58   if (host_ == nullptr) // parallel task: their remain is already between 0 and 1 (see comment in ExecImpl::remains())
59     return surfAction_->getRemains();
60   else // Actually compute the ratio for sequential tasks
61     return surfAction_->getRemains() / surfAction_->getCost();
62 }
63
64 void simgrid::kernel::activity::ExecImpl::post()
65 {
66   if (host_ && host_->isOff()) { /* FIXME: handle resource failure for parallel tasks too */
67                                  /* If the host running the synchro failed, notice it. This way, the asking
68                                   * process can be killed if it runs on that host itself */
69     state = SIMIX_FAILED;
70   } else if (surfAction_->getState() == simgrid::surf::Action::State::failed) {
71     /* If the host running the synchro didn't fail, then the synchro was canceled */
72     state = SIMIX_CANCELED;
73   } else if (timeoutDetector && timeoutDetector->getState() == simgrid::surf::Action::State::done) {
74     state = SIMIX_TIMEOUT;
75   } else {
76     state = SIMIX_DONE;
77   }
78
79   if (surfAction_) {
80     surfAction_->unref();
81     surfAction_ = nullptr;
82   }
83   if (timeoutDetector) {
84     timeoutDetector->unref();
85     timeoutDetector = nullptr;
86   }
87
88   /* If there are simcalls associated with the synchro, then answer them */
89   if (not simcalls.empty())
90     SIMIX_execution_finish(this);
91 }