Logo AND Algorithmique Numérique Distribuée

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