Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert all xbt_ex(timeout_error) throwing locations to simgrid::TimeoutError
[simgrid.git] / src / simix / smx_host.cpp
1 /* Copyright (c) 2007-2018. 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 "mc/mc.h"
7 #include "simgrid/Exception.hpp"
8 #include "smx_private.hpp"
9 #include "src/kernel/activity/CommImpl.hpp"
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/mc/mc_replay.hpp"
12 #include "src/plugins/vm/VirtualMachineImpl.hpp"
13 #include "src/simix/smx_host_private.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
16
17 /* needs to be public and without simcall for exceptions and logging events */
18 const char* sg_host_self_get_name()
19 {
20   sg_host_t host = sg_host_self();
21   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
22     return "";
23
24   return host->get_cname();
25 }
26
27 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_start(std::string name, std::string category,
28                                                              double flops_amount, double priority, double bound,
29                                                              sg_host_t host)
30 {
31   /* set surf's action */
32   simgrid::kernel::resource::Action* surf_action = nullptr;
33   if (not MC_is_active() && not MC_record_replay_is_active()) {
34     surf_action = host->pimpl_cpu->execution_start(flops_amount);
35     surf_action->set_priority(priority);
36     if (bound > 0)
37       surf_action->set_bound(bound);
38   }
39
40   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
41       new simgrid::kernel::activity::ExecImpl(name, surf_action, /*timeout_detector*/ nullptr, host));
42
43   exec->set_category(category);
44   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name_.c_str());
45   simgrid::kernel::activity::ExecImpl::on_creation(exec);
46
47   return exec;
48 }
49
50 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_parallel_start(std::string name, int host_nb,
51                                                                       sg_host_t* host_list, double* flops_amount,
52                                                                       double* bytes_amount, double rate, double timeout)
53 {
54
55   /* Check that we are not mixing VMs and PMs in the parallel task */
56   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
57   for (int i = 1; i < host_nb; i++) {
58     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
59     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
60   }
61
62   /* set surf's synchro */
63   simgrid::kernel::resource::Action* surf_action      = nullptr;
64   simgrid::kernel::resource::Action* timeout_detector = nullptr;
65   if (not MC_is_active() && not MC_record_replay_is_active()) {
66     sg_host_t* host_list_cpy = new sg_host_t[host_nb];
67     std::copy_n(host_list, host_nb, host_list_cpy);
68     surf_action = surf_host_model->execute_parallel(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
69     if (timeout > 0) {
70       timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
71     }
72   }
73
74   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
75       new simgrid::kernel::activity::ExecImpl(name, surf_action, timeout_detector, nullptr));
76
77   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
78
79   return exec;
80 }
81
82 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
83 {
84   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
85
86   /* Associate this simcall to the synchro */
87   synchro->simcalls_.push_back(simcall);
88   simcall->issuer->waiting_synchro = synchro;
89
90   /* set surf's synchro */
91   if (MC_is_active() || MC_record_replay_is_active()) {
92     synchro->state_ = SIMIX_DONE;
93     SIMIX_execution_finish(synchro);
94     return;
95   }
96
97   /* If the synchro is already finished then perform the error handling */
98   if (synchro->state_ != SIMIX_RUNNING)
99     SIMIX_execution_finish(synchro);
100 }
101
102 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
103 {
104   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
105   if (res) {
106     synchro->simcalls_.push_back(simcall);
107     SIMIX_execution_finish(synchro);
108   } else {
109     SIMIX_simcall_answer(simcall);
110   }
111   simcall_execution_test__set__result(simcall, res);
112 }
113
114 void SIMIX_execution_finish(smx_activity_t synchro)
115 {
116   simgrid::kernel::activity::ExecImplPtr exec =
117       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
118
119   while (not synchro->simcalls_.empty()) {
120     smx_simcall_t simcall = synchro->simcalls_.front();
121     synchro->simcalls_.pop_front();
122     switch (exec->state_) {
123
124       case SIMIX_DONE:
125         /* do nothing, synchro done */
126         XBT_DEBUG("SIMIX_execution_finished: execution successful");
127         break;
128
129       case SIMIX_FAILED:
130         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host_->get_cname());
131         simcall->issuer->context_->iwannadie = 1;
132         simcall->issuer->exception =
133             std::make_exception_ptr(simgrid::HostFailureException(XBT_THROW_POINT, "Host failed"));
134         break;
135
136       case SIMIX_CANCELED:
137         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
138         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
139         break;
140
141       case SIMIX_TIMEOUT:
142         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
143         simcall->issuer->exception = std::make_exception_ptr(simgrid::TimeoutError(XBT_THROW_POINT, "Timeouted"));
144         break;
145
146       default:
147         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
148     }
149     /* Fail the process if the host is down */
150     if (simcall->issuer->host_->is_off())
151       simcall->issuer->context_->iwannadie = 1;
152
153     simcall->issuer->waiting_synchro = nullptr;
154     simcall_execution_wait__set__result(simcall, exec->state_);
155     SIMIX_simcall_answer(simcall);
156   }
157 }
158
159 void SIMIX_set_category(smx_activity_t synchro, std::string category)
160 {
161   if (synchro->state_ != SIMIX_RUNNING)
162     return;
163
164   simgrid::kernel::activity::ExecImplPtr exec =
165       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
166   if (exec != nullptr) {
167     exec->surf_action_->set_category(category);
168     return;
169   }
170
171   simgrid::kernel::activity::CommImplPtr comm =
172       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
173   if (comm != nullptr) {
174     comm->surfAction_->set_category(category);
175   }
176 }