Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to register functions that are void (*code)(std::vector<std::string>)
[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 "smx_private.hpp"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "src/mc/mc_replay.hpp"
11 #include "src/plugins/vm/VirtualMachineImpl.hpp"
12 #include "src/simix/smx_host_private.hpp"
13 #include "xbt/ex.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 /**
28  * @brief Add a process to the list of the processes that the host will restart when it comes back
29  * This function add a process to the list of the processes that will be restarted when the host comes
30  * back. It is expected that this function is called when the host is down.
31  * The processes will only be restarted once, meaning that you will have to register the process
32  * again to restart the process again.
33  */
34 void SIMIX_host_add_auto_restart_process(sg_host_t host, simgrid::kernel::actor::ActorImpl* actor)
35 {
36   simgrid::kernel::actor::ProcessArg* arg = new simgrid::kernel::actor::ProcessArg(host, actor);
37
38   if (host->is_off() && watched_hosts.find(host->get_cname()) == watched_hosts.end()) {
39     watched_hosts.insert(host->get_cname());
40     XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->get_cname());
41   }
42   if (host->pimpl_->actors_at_boot_.find(actor->get_name()) == host->pimpl_->actors_at_boot_.end()) {
43     XBT_DEBUG("Adding Process %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
44     host->pimpl_->actors_at_boot_.insert({arg->name, arg});
45   }
46 }
47
48 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_start(std::string name, std::string category,
49                                                              double flops_amount, double priority, double bound,
50                                                              sg_host_t host)
51 {
52   /* set surf's action */
53   simgrid::kernel::resource::Action* surf_action = nullptr;
54   if (not MC_is_active() && not MC_record_replay_is_active()) {
55     surf_action = host->pimpl_cpu->execution_start(flops_amount);
56     surf_action->set_priority(priority);
57     if (bound > 0)
58       surf_action->set_bound(bound);
59   }
60
61   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
62       new simgrid::kernel::activity::ExecImpl(name, surf_action, /*timeout_detector*/ nullptr, host));
63
64   exec->set_category(category);
65   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name_.c_str());
66   simgrid::kernel::activity::ExecImpl::on_creation(exec);
67
68   return exec;
69 }
70
71 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_parallel_start(std::string name, int host_nb,
72                                                                       sg_host_t* host_list, double* flops_amount,
73                                                                       double* bytes_amount, double rate, double timeout)
74 {
75
76   /* Check that we are not mixing VMs and PMs in the parallel task */
77   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
78   for (int i = 1; i < host_nb; i++) {
79     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
80     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
81   }
82
83   /* set surf's synchro */
84   simgrid::kernel::resource::Action* surf_action      = nullptr;
85   simgrid::kernel::resource::Action* timeout_detector = nullptr;
86   if (not MC_is_active() && not MC_record_replay_is_active()) {
87     sg_host_t* host_list_cpy = new sg_host_t[host_nb];
88     std::copy_n(host_list, host_nb, host_list_cpy);
89     surf_action = surf_host_model->execute_parallel(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
90     if (timeout > 0) {
91       timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
92     }
93   }
94
95   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
96       new simgrid::kernel::activity::ExecImpl(name, surf_action, timeout_detector, nullptr));
97
98   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
99
100   return exec;
101 }
102
103 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
104 {
105   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
106
107   /* Associate this simcall to the synchro */
108   synchro->simcalls_.push_back(simcall);
109   simcall->issuer->waiting_synchro = synchro;
110
111   /* set surf's synchro */
112   if (MC_is_active() || MC_record_replay_is_active()) {
113     synchro->state_ = SIMIX_DONE;
114     SIMIX_execution_finish(synchro);
115     return;
116   }
117
118   /* If the synchro is already finished then perform the error handling */
119   if (synchro->state_ != SIMIX_RUNNING)
120     SIMIX_execution_finish(synchro);
121 }
122
123 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
124 {
125   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
126   if (res) {
127     synchro->simcalls_.push_back(simcall);
128     SIMIX_execution_finish(synchro);
129   } else {
130     SIMIX_simcall_answer(simcall);
131   }
132   simcall_execution_test__set__result(simcall, res);
133 }
134
135 void SIMIX_execution_finish(smx_activity_t synchro)
136 {
137   simgrid::kernel::activity::ExecImplPtr exec =
138       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
139
140   while (not synchro->simcalls_.empty()) {
141     smx_simcall_t simcall = synchro->simcalls_.front();
142     synchro->simcalls_.pop_front();
143     switch (exec->state_) {
144
145       case SIMIX_DONE:
146         /* do nothing, synchro done */
147         XBT_DEBUG("SIMIX_execution_finished: execution successful");
148         break;
149
150       case SIMIX_FAILED:
151         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host_->get_cname());
152         simcall->issuer->context_->iwannadie = 1;
153         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
154         break;
155
156       case SIMIX_CANCELED:
157         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
158         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
159         break;
160
161       case SIMIX_TIMEOUT:
162         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
163         SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
164         break;
165
166       default:
167         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
168     }
169     /* Fail the process if the host is down */
170     if (simcall->issuer->host_->is_off())
171       simcall->issuer->context_->iwannadie = 1;
172
173     simcall->issuer->waiting_synchro = nullptr;
174     simcall_execution_wait__set__result(simcall, exec->state_);
175     SIMIX_simcall_answer(simcall);
176   }
177 }
178
179 void SIMIX_set_category(smx_activity_t synchro, std::string category)
180 {
181   if (synchro->state_ != SIMIX_RUNNING)
182     return;
183
184   simgrid::kernel::activity::ExecImplPtr exec =
185       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
186   if (exec != nullptr) {
187     exec->surf_action_->set_category(category);
188     return;
189   }
190
191   simgrid::kernel::activity::CommImplPtr comm =
192       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
193   if (comm != nullptr) {
194     comm->surfAction_->set_category(category);
195   }
196 }