Logo AND Algorithmique Numérique Distribuée

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