1 /* Copyright (c) 2007-2017. 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.hpp"
8 #include "src/kernel/activity/CommImpl.hpp"
9 #include "src/mc/mc_replay.hpp"
10 #include "src/plugins/vm/VirtualMachineImpl.hpp"
11 #include "src/surf/surf_interface.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
19 simgrid::xbt::Extension<simgrid::s4u::Host, Host> Host::EXTENSION_ID;
23 if (not Host::EXTENSION_ID.valid())
24 Host::EXTENSION_ID = s4u::Host::extension_create<simix::Host>();
29 /* All processes should be gone when the host is turned off (by the end of the simulation). */
30 if (not process_list.empty()) {
31 std::string msg = std::string("Shutting down host, but it's not empty:");
32 for (auto const& process : process_list)
33 msg += "\n\t" + std::string(process.getName());
35 SIMIX_display_process_status();
36 THROWF(arg_error, 0, "%s", msg.c_str());
38 for (auto const& arg : auto_restart_processes)
40 auto_restart_processes.clear();
41 for (auto const& arg : boot_processes)
43 boot_processes.clear();
46 /** Re-starts all the actors that are marked as restartable.
48 * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
52 for (auto const& arg : boot_processes) {
53 XBT_DEBUG("Booting Process %s(%s) right now", arg->name.c_str(), arg->host->getCname());
54 smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
55 arg->properties.get(), nullptr);
56 if (arg->kill_time >= 0)
57 simcall_process_set_kill_time(actor, arg->kill_time);
58 if (arg->auto_restart)
59 actor->auto_restart = arg->auto_restart;
65 /** @brief Stop the host if it is on */
66 void SIMIX_host_off(sg_host_t h, smx_actor_t issuer)
68 simgrid::simix::Host* host = h->extension<simgrid::simix::Host>();
70 xbt_assert((host != nullptr), "Invalid parameters");
73 h->pimpl_cpu->turnOff();
75 /* Clean Simulator data */
76 if (not host->process_list.empty()) {
77 for (auto& process : host->process_list) {
78 SIMIX_process_kill(&process, issuer);
79 XBT_DEBUG("Killing %s@%s on behalf of %s which turned off that host.", process.getCname(),
80 process.host->getCname(), issuer->getCname());
84 XBT_INFO("Host %s is already off", h->getCname());
88 sg_host_t sg_host_self()
90 smx_actor_t process = SIMIX_process_self();
91 return (process == nullptr) ? nullptr : process->host;
94 /* needs to be public and without simcall for exceptions and logging events */
95 const char* sg_host_self_get_name()
97 sg_host_t host = sg_host_self();
98 if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
101 return host->getCname();
105 * \brief Add a process to the list of the processes that the host will restart when it comes back
106 * This function add a process to the list of the processes that will be restarted when the host comes
107 * back. It is expected that this function is called when the host is down.
108 * The processes will only be restarted once, meaning that you will have to register the process
109 * again to restart the process again.
111 void SIMIX_host_add_auto_restart_process(sg_host_t host, const char* name, std::function<void()> code, void* data,
112 double kill_time, std::map<std::string, std::string>* properties,
115 smx_process_arg_t arg = new simgrid::simix::ProcessArg();
117 arg->code = std::move(code);
120 arg->kill_time = kill_time;
121 arg->properties.reset(properties, [](decltype(properties)) {});
122 arg->auto_restart = auto_restart;
124 if (host->isOff() && watched_hosts.find(host->getCname()) == watched_hosts.end()) {
125 watched_hosts.insert(host->getCname());
126 XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->getCname());
128 host->extension<simgrid::simix::Host>()->auto_restart_processes.push_back(arg);
130 /** @brief Restart the list of processes that have been registered to the host */
131 void SIMIX_host_autorestart(sg_host_t host)
133 std::vector<simgrid::simix::ProcessArg*> process_list =
134 host->extension<simgrid::simix::Host>()->auto_restart_processes;
136 for (auto const& arg : process_list) {
137 XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->getCname());
138 smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
139 arg->properties.get(), nullptr);
140 if (arg->kill_time >= 0)
141 simcall_process_set_kill_time(actor, arg->kill_time);
142 if (arg->auto_restart)
143 actor->auto_restart = arg->auto_restart;
145 process_list.clear();
148 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl> simcall_HANDLER_execution_start(smx_simcall_t simcall,
151 double priority, double bound)
153 return SIMIX_execution_start(simcall->issuer, name,flops_amount,priority,bound);
156 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
157 SIMIX_execution_start(smx_actor_t issuer, const char* name, double flops_amount, double priority, double bound)
160 /* alloc structures and initialize */
161 simgrid::kernel::activity::ExecImplPtr exec =
162 simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(name, issuer->host));
164 /* set surf's action */
165 if (not MC_is_active() && not MC_record_replay_is_active()) {
167 exec->surf_exec = issuer->host->pimpl_cpu->execution_start(flops_amount);
168 exec->surf_exec->setData(exec.get());
169 exec->surf_exec->setSharingWeight(priority);
172 static_cast<simgrid::surf::CpuAction*>(exec->surf_exec)->setBound(bound);
175 XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name.c_str());
180 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
181 SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
182 double* bytes_amount, double rate, double timeout)
185 /* alloc structures and initialize */
186 simgrid::kernel::activity::ExecImplPtr exec =
187 simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(name, nullptr));
189 /* Check that we are not mixing VMs and PMs in the parallel task */
190 bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
191 for (int i = 1; i < host_nb; i++) {
192 bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
193 xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
196 /* set surf's synchro */
197 if (not MC_is_active() && not MC_record_replay_is_active()) {
198 /* set surf's synchro */
199 sg_host_t* host_list_cpy = new sg_host_t[host_nb];
200 std::copy_n(host_list, host_nb, host_list_cpy);
201 exec->surf_exec = surf_host_model->executeParallelTask(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
202 exec->surf_exec->setData(exec.get());
204 exec->timeoutDetector = host_list[0]->pimpl_cpu->sleep(timeout);
205 exec->timeoutDetector->setData(exec.get());
208 XBT_DEBUG("Create parallel execute synchro %p", exec.get());
213 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
215 simgrid::kernel::activity::ExecImplPtr exec =
216 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
217 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state);
219 /* Associate this simcall to the synchro */
220 synchro->simcalls.push_back(simcall);
221 simcall->issuer->waiting_synchro = synchro;
223 /* set surf's synchro */
224 if (MC_is_active() || MC_record_replay_is_active()) {
225 synchro->state = SIMIX_DONE;
226 SIMIX_execution_finish(exec);
230 /* If the synchro is already finished then perform the error handling */
231 if (synchro->state != SIMIX_RUNNING)
232 SIMIX_execution_finish(exec);
235 void SIMIX_execution_finish(simgrid::kernel::activity::ExecImplPtr exec)
237 for (smx_simcall_t const& simcall : exec->simcalls) {
238 switch (exec->state) {
241 /* do nothing, synchro done */
242 XBT_DEBUG("SIMIX_execution_finished: execution successful");
246 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host->getCname());
247 simcall->issuer->context->iwannadie = 1;
248 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
252 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
253 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
257 XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
258 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
262 xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
265 /* Fail the process if the host is down */
266 if (simcall->issuer->host->isOff())
267 simcall->issuer->context->iwannadie = 1;
269 simcall->issuer->waiting_synchro = nullptr;
270 simcall_execution_wait__set__result(simcall, exec->state);
271 SIMIX_simcall_answer(simcall);
275 void SIMIX_set_category(smx_activity_t synchro, const char *category)
277 if (synchro->state != SIMIX_RUNNING)
280 simgrid::kernel::activity::ExecImplPtr exec =
281 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
282 if (exec != nullptr) {
283 exec->surf_exec->setCategory(category);
287 simgrid::kernel::activity::CommImplPtr comm =
288 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
289 if (comm != nullptr) {
290 comm->surf_comm->setCategory(category);