Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
start to untangle the MSG actor creation mess
[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/mc/mc_replay.h"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include <xbt/ex.hpp>
11
12 #include "src/kernel/activity/SynchroComm.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 (!Host::EXTENSION_ID.valid())
23         Host::EXTENSION_ID = s4u::Host::extension_create<simix::Host>();
24
25       simgrid::simix::ActorImpl act;
26       process_list = xbt_swag_new(xbt_swag_offset(act, host_proc_hookup));
27     }
28
29     Host::~Host()
30     {
31       /* Clean Simulator data */
32       if (xbt_swag_size(process_list) != 0) {
33         char *msg = xbt_strdup("Shutting down host, but it's not empty:");
34         char *tmp;
35         smx_actor_t process = nullptr;
36
37         xbt_swag_foreach(process, process_list) {
38           tmp = bprintf("%s\n\t%s", msg, process->name.c_str());
39           free(msg);
40           msg = tmp;
41         }
42         SIMIX_display_process_status();
43         THROWF(arg_error, 0, "%s", msg);
44       }
45       for (auto arg : auto_restart_processes)
46         delete arg;
47       auto_restart_processes.clear();
48       for (auto arg : boot_processes)
49         delete arg;
50       boot_processes.clear();
51       xbt_swag_free(process_list);
52     }
53
54     /** Re-starts all the actors that are marked as restartable.
55      *
56      * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
57      */
58     void Host::turnOn()
59     {
60       for (auto arg : boot_processes) {
61         XBT_DEBUG("Booting Process %s(%s) right now", arg->name.c_str(), arg->host->cname());
62         smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
63                                                                   arg->properties, arg->auto_restart, nullptr);
64         if (arg->kill_time >= 0)
65           simcall_process_set_kill_time(actor, arg->kill_time);
66       }
67     }
68
69 }} // namespaces
70
71 /** @brief Stop the host if it is on */
72 void SIMIX_host_off(sg_host_t h, smx_actor_t issuer)
73 {
74   simgrid::simix::Host* host = h->extension<simgrid::simix::Host>();
75
76   xbt_assert((host != nullptr), "Invalid parameters");
77
78   if (h->isOn()) {
79     h->pimpl_cpu->turnOff();
80
81     /* Clean Simulator data */
82     if (xbt_swag_size(host->process_list) != 0) {
83       smx_actor_t process = nullptr;
84       xbt_swag_foreach(process, host->process_list) {
85         SIMIX_process_kill(process, issuer);
86         XBT_DEBUG("Killing %s@%s on behalf of %s", process->cname(), process->host->cname(), issuer->cname());
87       }
88     }
89   } else {
90     XBT_INFO("Host %s is already off", h->cname());
91   }
92 }
93
94 sg_host_t SIMIX_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* SIMIX_host_self_get_name()
102 {
103   sg_host_t host = SIMIX_host_self();
104   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
105     return "";
106
107   return host->cname();
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(
118   sg_host_t host, const char *name, std::function<void()> code,
119   void* data, double kill_time, xbt_dict_t properties, 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 = properties;
128   arg->auto_restart = auto_restart;
129
130   if (host->isOff() && !xbt_dict_get_or_null(watched_hosts_lib, host->cname())) {
131     xbt_dict_set(watched_hosts_lib, host->cname(), host, nullptr);
132     XBT_DEBUG("Push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF", host->cname());
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 arg : process_list) {
143     XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->cname());
144     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
145                                                               arg->properties, arg->auto_restart, nullptr);
146     if (arg->kill_time >= 0)
147       simcall_process_set_kill_time(actor, arg->kill_time);
148   }
149   process_list.clear();
150 }
151
152 smx_activity_t simcall_HANDLER_execution_start(smx_simcall_t simcall, const char* name, double flops_amount,
153                                               double priority, double bound) {
154   return SIMIX_execution_start(simcall->issuer, name,flops_amount,priority,bound);
155 }
156
157 smx_activity_t SIMIX_execution_start(smx_actor_t issuer, const char *name, double flops_amount, double priority,
158                                     double bound){
159
160   /* alloc structures and initialize */
161   simgrid::kernel::activity::Exec *exec = new simgrid::kernel::activity::Exec(name, issuer->host);
162
163   /* set surf's action */
164   if (!MC_is_active() && !MC_record_replay_is_active()) {
165
166     exec->surf_exec = issuer->host->pimpl_cpu->execution_start(flops_amount);
167     exec->surf_exec->setData(exec);
168     exec->surf_exec->setPriority(priority);
169
170     if (bound > 0)
171       static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
172   }
173
174   XBT_DEBUG("Create execute synchro %p: %s", exec, exec->name.c_str());
175
176   return exec;
177 }
178
179 smx_activity_t SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
180                                               double* bytes_amount, double amount, double rate, double timeout)
181 {
182
183   /* alloc structures and initialize */
184   simgrid::kernel::activity::Exec *exec = new simgrid::kernel::activity::Exec(name, nullptr);
185
186   /* set surf's synchro */
187   sg_host_t *host_list_cpy = xbt_new0(sg_host_t, host_nb);
188   for (int i = 0; i < host_nb; i++)
189     host_list_cpy[i] = host_list[i];
190
191   /* Check that we are not mixing VMs and PMs in the parallel task */
192   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
193   for (int i = 1; i < host_nb; i++) {
194     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
195     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
196   }
197
198   /* set surf's synchro */
199   if (!MC_is_active() && !MC_record_replay_is_active()) {
200     exec->surf_exec = surf_host_model->executeParallelTask(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
201     exec->surf_exec->setData(exec);
202     if (timeout > 0) {
203       exec->timeoutDetector = host_list[0]->pimpl_cpu->sleep(timeout);
204       exec->timeoutDetector->setData(exec);
205     }
206   }
207   XBT_DEBUG("Create parallel execute synchro %p", exec);
208
209   return exec;
210 }
211
212 void SIMIX_execution_cancel(smx_activity_t synchro)
213 {
214   XBT_DEBUG("Cancel synchro %p", synchro);
215   simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
216
217   if (exec->surf_exec)
218     exec->surf_exec->cancel();
219 }
220
221 void SIMIX_execution_set_priority(smx_activity_t synchro, double priority)
222 {
223   simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
224   if(exec->surf_exec)
225     exec->surf_exec->setPriority(priority);
226 }
227
228 void SIMIX_execution_set_bound(smx_activity_t synchro, double bound)
229 {
230   simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
231   if(exec->surf_exec)
232     static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
233 }
234
235 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
236 {
237   simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
238   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state);
239
240   /* Associate this simcall to the synchro */
241   synchro->simcalls.push_back(simcall);
242   simcall->issuer->waiting_synchro = synchro;
243
244   /* set surf's synchro */
245   if (MC_is_active() || MC_record_replay_is_active()) {
246     synchro->state = SIMIX_DONE;
247     SIMIX_execution_finish(exec);
248     return;
249   }
250
251   /* If the synchro is already finished then perform the error handling */
252   if (synchro->state != SIMIX_RUNNING)
253     SIMIX_execution_finish(exec);
254 }
255
256 void SIMIX_execution_finish(simgrid::kernel::activity::Exec *exec)
257 {
258   for (smx_simcall_t simcall : exec->simcalls) {
259     switch (exec->state) {
260
261       case SIMIX_DONE:
262         /* do nothing, synchro done */
263         XBT_DEBUG("SIMIX_execution_finished: execution successful");
264         break;
265
266       case SIMIX_FAILED:
267         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host->cname());
268         simcall->issuer->context->iwannadie = 1;
269         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
270         break;
271
272       case SIMIX_CANCELED:
273         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
274         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
275         break;
276
277       case SIMIX_TIMEOUT:
278         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
279         SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
280         break;
281
282       default:
283         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
284             (int)exec->state);
285     }
286     /* Fail the process if the host is down */
287     if (simcall->issuer->host->isOff())
288       simcall->issuer->context->iwannadie = 1;
289
290     simcall->issuer->waiting_synchro = nullptr;
291     simcall_execution_wait__set__result(simcall, exec->state);
292     SIMIX_simcall_answer(simcall);
293   }
294
295   /* We no longer need it */
296   exec->unref();
297 }
298
299 void SIMIX_set_category(smx_activity_t synchro, const char *category)
300 {
301   if (synchro->state != SIMIX_RUNNING)
302     return;
303
304   simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec *>(synchro);
305   if (exec != nullptr) {
306     exec->surf_exec->setCategory(category);
307     return;
308   }
309
310   simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm *>(synchro);
311   if (comm != nullptr) {
312     comm->surf_comm->setCategory(category);
313   }
314 }