Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
shutdown all hosted VMs when an host is turned off
[simgrid.git] / src / simix / smx_host.cpp
1 /* Copyright (c) 2007-2016. 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.h"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/mc/mc_replay.h"
10 #include "src/plugins/vm/VirtualMachineImpl.hpp"
11 #include "src/surf/surf_interface.hpp"
12 #include "xbt/ex.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
15
16 namespace simgrid {
17   namespace simix {
18     simgrid::xbt::Extension<simgrid::s4u::Host, Host> Host::EXTENSION_ID;
19
20     Host::Host()
21     {
22       if (not Host::EXTENSION_ID.valid())
23         Host::EXTENSION_ID = s4u::Host::extension_create<simix::Host>();
24
25       simgrid::simix::ActorImpl act;
26       process_list = xbt_swag_new(xbt_swag_offset(act, host_proc_hookup));
27     }
28
29     Host::~Host()
30     {
31       /* All processes should be gone when the host is turned off (by the end of the simulation). */
32       if (xbt_swag_size(process_list) != 0) {
33         std::string msg     = std::string("Shutting down host, but it's not empty:");
34         smx_actor_t process = nullptr;
35
36         xbt_swag_foreach(process, process_list) msg = msg + "\n\t" + process->name.c_str();
37
38         SIMIX_display_process_status();
39         THROWF(arg_error, 0, "%s", msg.c_str());
40       }
41       for (auto const& arg : auto_restart_processes)
42         delete arg;
43       auto_restart_processes.clear();
44       for (auto const& arg : boot_processes)
45         delete arg;
46       boot_processes.clear();
47       xbt_swag_free(process_list);
48     }
49
50     /** Re-starts all the actors that are marked as restartable.
51      *
52      * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
53      */
54     void Host::turnOn()
55     {
56       for (auto const& arg : boot_processes) {
57         XBT_DEBUG("Booting Process %s(%s) right now", arg->name.c_str(), arg->host->getCname());
58         smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
59                                                                   arg->properties.get(), nullptr);
60         if (arg->kill_time >= 0)
61           simcall_process_set_kill_time(actor, arg->kill_time);
62         if (arg->auto_restart)
63           actor->auto_restart = arg->auto_restart;
64       }
65     }
66
67 }} // namespaces
68
69 /** @brief Stop the host if it is on */
70 void SIMIX_host_off(sg_host_t h, smx_actor_t issuer)
71 {
72   simgrid::simix::Host* host = h->extension<simgrid::simix::Host>();
73
74   xbt_assert((host != nullptr), "Invalid parameters");
75
76   if (h->isOn()) {
77     h->pimpl_cpu->turnOff();
78
79     /* Clean Simulator data */
80     if (xbt_swag_size(host->process_list) != 0) {
81       smx_actor_t process = nullptr;
82       xbt_swag_foreach(process, host->process_list) {
83         SIMIX_process_kill(process, issuer);
84         XBT_DEBUG("Killing %s@%s on behalf of %s which turned off that host.", process->cname(),
85                   process->host->getCname(), issuer->cname());
86       }
87     }
88   } else {
89     XBT_INFO("Host %s is already off", h->getCname());
90   }
91 }
92
93 sg_host_t sg_host_self()
94 {
95   smx_actor_t process = SIMIX_process_self();
96   return (process == nullptr) ? nullptr : process->host;
97 }
98
99 /* needs to be public and without simcall for exceptions and logging events */
100 const char* sg_host_self_get_name()
101 {
102   sg_host_t host = sg_host_self();
103   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
104     return "";
105
106   return host->getCname();
107 }
108
109 /**
110  * \brief Add a process to the list of the processes that the host will restart when it comes back
111  * This function add a process to the list of the processes that will be restarted when the host comes
112  * back. It is expected that this function is called when the host is down.
113  * The processes will only be restarted once, meaning that you will have to register the process
114  * again to restart the process again.
115  */
116 void SIMIX_host_add_auto_restart_process(sg_host_t host, const char* name, std::function<void()> code, void* data,
117                                          double kill_time, std::map<std::string, std::string>* properties,
118                                          int auto_restart)
119 {
120   smx_process_arg_t arg = new simgrid::simix::ProcessArg();
121   arg->name = name;
122   arg->code = std::move(code);
123   arg->data = data;
124   arg->host = host;
125   arg->kill_time = kill_time;
126   arg->properties.reset(properties, [](decltype(properties)) {});
127   arg->auto_restart = auto_restart;
128
129   if (host->isOff() && watched_hosts.find(host->getCname()) == watched_hosts.end()) {
130     watched_hosts.insert(host->getCname());
131     XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->getCname());
132   }
133   host->extension<simgrid::simix::Host>()->auto_restart_processes.push_back(arg);
134 }
135 /** @brief Restart the list of processes that have been registered to the host */
136 void SIMIX_host_autorestart(sg_host_t host)
137 {
138   std::vector<simgrid::simix::ProcessArg*> process_list =
139       host->extension<simgrid::simix::Host>()->auto_restart_processes;
140
141   for (auto const& arg : process_list) {
142     XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->getCname());
143     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
144                                                               arg->properties.get(), nullptr);
145     if (arg->kill_time >= 0)
146       simcall_process_set_kill_time(actor, arg->kill_time);
147     if (arg->auto_restart)
148       actor->auto_restart = arg->auto_restart;
149   }
150   process_list.clear();
151 }
152
153 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl> simcall_HANDLER_execution_start(smx_simcall_t simcall,
154                                                                                           const char* name,
155                                                                                           double flops_amount,
156                                                                                           double priority, double bound)
157 {
158   return SIMIX_execution_start(simcall->issuer, name,flops_amount,priority,bound);
159 }
160
161 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
162 SIMIX_execution_start(smx_actor_t issuer, const char* name, double flops_amount, double priority, double bound)
163 {
164
165   /* alloc structures and initialize */
166   simgrid::kernel::activity::ExecImplPtr exec =
167       simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(name, issuer->host));
168
169   /* set surf's action */
170   if (not MC_is_active() && not MC_record_replay_is_active()) {
171
172     exec->surf_exec = issuer->host->pimpl_cpu->execution_start(flops_amount);
173     exec->surf_exec->setData(exec.get());
174     exec->surf_exec->setSharingWeight(priority);
175
176     if (bound > 0)
177       static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
178   }
179
180   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name.c_str());
181
182   return exec;
183 }
184
185 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
186 SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
187                                double* bytes_amount, double rate, double timeout)
188 {
189
190   /* alloc structures and initialize */
191   simgrid::kernel::activity::ExecImplPtr exec =
192       simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(name, nullptr));
193
194   /* set surf's synchro */
195   sg_host_t *host_list_cpy = xbt_new0(sg_host_t, host_nb);
196   for (int i = 0; i < host_nb; i++)
197     host_list_cpy[i] = host_list[i];
198
199   /* Check that we are not mixing VMs and PMs in the parallel task */
200   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
201   for (int i = 1; i < host_nb; i++) {
202     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
203     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
204   }
205
206   /* set surf's synchro */
207   if (not MC_is_active() && not MC_record_replay_is_active()) {
208     exec->surf_exec = surf_host_model->executeParallelTask(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
209     exec->surf_exec->setData(exec.get());
210     if (timeout > 0) {
211       exec->timeoutDetector = host_list[0]->pimpl_cpu->sleep(timeout);
212       exec->timeoutDetector->setData(exec.get());
213     }
214   }
215   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
216
217   return exec;
218 }
219
220 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
221 {
222   simgrid::kernel::activity::ExecImplPtr exec =
223       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
224   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state);
225
226   /* Associate this simcall to the synchro */
227   synchro->simcalls.push_back(simcall);
228   simcall->issuer->waiting_synchro = synchro;
229
230   /* set surf's synchro */
231   if (MC_is_active() || MC_record_replay_is_active()) {
232     synchro->state = SIMIX_DONE;
233     SIMIX_execution_finish(exec);
234     return;
235   }
236
237   /* If the synchro is already finished then perform the error handling */
238   if (synchro->state != SIMIX_RUNNING)
239     SIMIX_execution_finish(exec);
240 }
241
242 void SIMIX_execution_finish(simgrid::kernel::activity::ExecImplPtr exec)
243 {
244   for (smx_simcall_t const& simcall : exec->simcalls) {
245     switch (exec->state) {
246
247       case SIMIX_DONE:
248         /* do nothing, synchro done */
249         XBT_DEBUG("SIMIX_execution_finished: execution successful");
250         break;
251
252       case SIMIX_FAILED:
253         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host->getCname());
254         simcall->issuer->context->iwannadie = 1;
255         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
256         break;
257
258       case SIMIX_CANCELED:
259         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
260         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
261         break;
262
263       case SIMIX_TIMEOUT:
264         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
265         SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
266         break;
267
268       default:
269         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
270             (int)exec->state);
271     }
272     /* Fail the process if the host is down */
273     if (simcall->issuer->host->isOff())
274       simcall->issuer->context->iwannadie = 1;
275
276     simcall->issuer->waiting_synchro = nullptr;
277     simcall_execution_wait__set__result(simcall, exec->state);
278     SIMIX_simcall_answer(simcall);
279   }
280 }
281
282 void SIMIX_set_category(smx_activity_t synchro, const char *category)
283 {
284   if (synchro->state != SIMIX_RUNNING)
285     return;
286
287   simgrid::kernel::activity::ExecImplPtr exec =
288       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
289   if (exec != nullptr) {
290     exec->surf_exec->setCategory(category);
291     return;
292   }
293
294   simgrid::kernel::activity::CommImplPtr comm =
295       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
296   if (comm != nullptr) {
297     comm->surf_comm->setCategory(category);
298   }
299 }