Logo AND Algorithmique Numérique Distribuée

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