Logo AND Algorithmique Numérique Distribuée

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