Logo AND Algorithmique Numérique Distribuée

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