1 /* Copyright (c) 2007-2016. The SimGrid Team. All rights reserved. */
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. */
7 #include "smx_private.h"
8 #include "src/mc/mc_replay.h"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
12 #include "src/kernel/activity/SynchroComm.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
18 simgrid::xbt::Extension<simgrid::s4u::Host, Host> Host::EXTENSION_ID;
22 if (!Host::EXTENSION_ID.valid())
23 Host::EXTENSION_ID = s4u::Host::extension_create<simix::Host>();
25 simgrid::simix::ActorImpl act;
26 process_list = xbt_swag_new(xbt_swag_offset(act, host_proc_hookup));
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:");
35 smx_actor_t process = nullptr;
37 xbt_swag_foreach(process, process_list) {
38 tmp = bprintf("%s\n\t%s", msg, process->name.c_str());
42 SIMIX_display_process_status();
43 THROWF(arg_error, 0, "%s", msg);
45 for (auto arg : auto_restart_processes)
47 auto_restart_processes.clear();
48 for (auto arg : boot_processes)
50 boot_processes.clear();
51 xbt_swag_free(process_list);
54 /** Re-starts all the actors that are marked as restartable.
56 * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
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, nullptr);
64 if (arg->kill_time >= 0)
65 simcall_process_set_kill_time(actor, arg->kill_time);
66 if (arg->auto_restart)
67 simcall_process_auto_restart_set(actor, arg->auto_restart);
73 /** @brief Stop the host if it is on */
74 void SIMIX_host_off(sg_host_t h, smx_actor_t issuer)
76 simgrid::simix::Host* host = h->extension<simgrid::simix::Host>();
78 xbt_assert((host != nullptr), "Invalid parameters");
81 h->pimpl_cpu->turnOff();
83 /* Clean Simulator data */
84 if (xbt_swag_size(host->process_list) != 0) {
85 smx_actor_t process = nullptr;
86 xbt_swag_foreach(process, host->process_list) {
87 SIMIX_process_kill(process, issuer);
88 XBT_DEBUG("Killing %s@%s on behalf of %s", process->cname(), process->host->cname(), issuer->cname());
92 XBT_INFO("Host %s is already off", h->cname());
96 sg_host_t SIMIX_host_self()
98 smx_actor_t process = SIMIX_process_self();
99 return (process == nullptr) ? nullptr : process->host;
102 /* needs to be public and without simcall for exceptions and logging events */
103 const char* SIMIX_host_self_get_name()
105 sg_host_t host = SIMIX_host_self();
106 if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
109 return host->cname();
113 * \brief Add a process to the list of the processes that the host will restart when it comes back
114 * This function add a process to the list of the processes that will be restarted when the host comes
115 * back. It is expected that this function is called when the host is down.
116 * The processes will only be restarted once, meaning that you will have to register the process
117 * again to restart the process again.
119 void SIMIX_host_add_auto_restart_process(
120 sg_host_t host, const char *name, std::function<void()> code,
121 void* data, double kill_time, xbt_dict_t properties, int auto_restart)
123 smx_process_arg_t arg = new simgrid::simix::ProcessArg();
125 arg->code = std::move(code);
128 arg->kill_time = kill_time;
129 arg->properties = properties;
130 arg->auto_restart = auto_restart;
132 if (host->isOff() && !xbt_dict_get_or_null(watched_hosts_lib, host->cname())) {
133 xbt_dict_set(watched_hosts_lib, host->cname(), host, nullptr);
134 XBT_DEBUG("Push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF", host->cname());
136 host->extension<simgrid::simix::Host>()->auto_restart_processes.push_back(arg);
138 /** @brief Restart the list of processes that have been registered to the host */
139 void SIMIX_host_autorestart(sg_host_t host)
141 std::vector<simgrid::simix::ProcessArg*> process_list =
142 host->extension<simgrid::simix::Host>()->auto_restart_processes;
144 for (auto arg : process_list) {
145 XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->cname());
146 smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
147 arg->properties, nullptr);
148 if (arg->kill_time >= 0)
149 simcall_process_set_kill_time(actor, arg->kill_time);
150 if (arg->auto_restart)
151 simcall_process_auto_restart_set(actor, arg->auto_restart);
153 process_list.clear();
156 smx_activity_t simcall_HANDLER_execution_start(smx_simcall_t simcall, const char* name, double flops_amount,
157 double priority, double bound) {
158 return SIMIX_execution_start(simcall->issuer, name,flops_amount,priority,bound);
161 smx_activity_t SIMIX_execution_start(smx_actor_t issuer, const char *name, double flops_amount, double priority,
164 /* alloc structures and initialize */
165 simgrid::kernel::activity::Exec *exec = new simgrid::kernel::activity::Exec(name, issuer->host);
167 /* set surf's action */
168 if (!MC_is_active() && !MC_record_replay_is_active()) {
170 exec->surf_exec = issuer->host->pimpl_cpu->execution_start(flops_amount);
171 exec->surf_exec->setData(exec);
172 exec->surf_exec->setPriority(priority);
175 static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
178 XBT_DEBUG("Create execute synchro %p: %s", exec, exec->name.c_str());
183 smx_activity_t SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
184 double* bytes_amount, double amount, double rate, double timeout)
187 /* alloc structures and initialize */
188 simgrid::kernel::activity::Exec *exec = new simgrid::kernel::activity::Exec(name, nullptr);
190 /* set surf's synchro */
191 sg_host_t *host_list_cpy = xbt_new0(sg_host_t, host_nb);
192 for (int i = 0; i < host_nb; i++)
193 host_list_cpy[i] = host_list[i];
195 /* Check that we are not mixing VMs and PMs in the parallel task */
196 bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
197 for (int i = 1; i < host_nb; i++) {
198 bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
199 xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
202 /* set surf's synchro */
203 if (!MC_is_active() && !MC_record_replay_is_active()) {
204 exec->surf_exec = surf_host_model->executeParallelTask(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
205 exec->surf_exec->setData(exec);
207 exec->timeoutDetector = host_list[0]->pimpl_cpu->sleep(timeout);
208 exec->timeoutDetector->setData(exec);
211 XBT_DEBUG("Create parallel execute synchro %p", exec);
216 void SIMIX_execution_cancel(smx_activity_t synchro)
218 XBT_DEBUG("Cancel synchro %p", synchro);
219 simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
222 exec->surf_exec->cancel();
225 void SIMIX_execution_set_priority(smx_activity_t synchro, double priority)
227 simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
229 exec->surf_exec->setPriority(priority);
232 void SIMIX_execution_set_bound(smx_activity_t synchro, double bound)
234 simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
236 static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
239 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
241 simgrid::kernel::activity::Exec *exec = static_cast<simgrid::kernel::activity::Exec *>(synchro);
242 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro, (int)synchro->state);
244 /* Associate this simcall to the synchro */
245 synchro->simcalls.push_back(simcall);
246 simcall->issuer->waiting_synchro = synchro;
248 /* set surf's synchro */
249 if (MC_is_active() || MC_record_replay_is_active()) {
250 synchro->state = SIMIX_DONE;
251 SIMIX_execution_finish(exec);
255 /* If the synchro is already finished then perform the error handling */
256 if (synchro->state != SIMIX_RUNNING)
257 SIMIX_execution_finish(exec);
260 void SIMIX_execution_finish(simgrid::kernel::activity::Exec *exec)
262 for (smx_simcall_t simcall : exec->simcalls) {
263 switch (exec->state) {
266 /* do nothing, synchro done */
267 XBT_DEBUG("SIMIX_execution_finished: execution successful");
271 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host->cname());
272 simcall->issuer->context->iwannadie = 1;
273 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
277 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
278 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
282 XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
283 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
287 xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
290 /* Fail the process if the host is down */
291 if (simcall->issuer->host->isOff())
292 simcall->issuer->context->iwannadie = 1;
294 simcall->issuer->waiting_synchro = nullptr;
295 simcall_execution_wait__set__result(simcall, exec->state);
296 SIMIX_simcall_answer(simcall);
299 /* We no longer need it */
303 void SIMIX_set_category(smx_activity_t synchro, const char *category)
305 if (synchro->state != SIMIX_RUNNING)
308 simgrid::kernel::activity::Exec *exec = dynamic_cast<simgrid::kernel::activity::Exec *>(synchro);
309 if (exec != nullptr) {
310 exec->surf_exec->setCategory(category);
314 simgrid::kernel::activity::Comm *comm = dynamic_cast<simgrid::kernel::activity::Comm *>(synchro);
315 if (comm != nullptr) {
316 comm->surf_comm->setCategory(category);