1 /* Copyright (c) 2007-2018. 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/mc/mc_replay.hpp"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include "src/simix/smx_host_private.hpp"
11 #include "src/surf/surf_interface.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
16 /* needs to be public and without simcall for exceptions and logging events */
17 const char* sg_host_self_get_name()
19 sg_host_t host = sg_host_self();
20 if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
23 return host->get_cname();
27 * \brief Add a process to the list of the processes that the host will restart when it comes back
28 * This function add a process to the list of the processes that will be restarted when the host comes
29 * back. It is expected that this function is called when the host is down.
30 * The processes will only be restarted once, meaning that you will have to register the process
31 * again to restart the process again.
33 void SIMIX_host_add_auto_restart_process(sg_host_t host, simgrid::kernel::actor::ActorImpl* actor)
35 simgrid::kernel::actor::ProcessArg* arg = new simgrid::kernel::actor::ProcessArg(host, actor);
37 if (host->is_off() && watched_hosts.find(host->get_cname()) == watched_hosts.end()) {
38 watched_hosts.insert(host->get_cname());
39 XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->get_cname());
41 XBT_DEBUG("Adding Process %s to the auto-restart list of Host %s", arg->name.c_str(), arg->host->get_cname());
42 host->pimpl_->auto_restart_processes.push_back(arg);
45 /** @brief Restart the list of processes that have been registered to the host */
46 void SIMIX_host_autorestart(sg_host_t host)
48 std::vector<simgrid::kernel::actor::ProcessArg*> process_list = host->pimpl_->auto_restart_processes;
50 for (auto const& arg : process_list) {
51 XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->get_cname());
52 smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
53 arg->properties.get(), nullptr);
54 if (arg->kill_time >= 0)
55 simcall_process_set_kill_time(actor, arg->kill_time);
56 if (arg->auto_restart)
57 actor->auto_restart_ = arg->auto_restart;
62 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
63 SIMIX_execution_start(const char* name, double flops_amount, double priority, double bound, sg_host_t host)
65 /* set surf's action */
66 simgrid::kernel::resource::Action* surf_action = nullptr;
67 if (not MC_is_active() && not MC_record_replay_is_active()) {
68 surf_action = host->pimpl_cpu->execution_start(flops_amount);
69 surf_action->set_priority(priority);
71 static_cast<simgrid::surf::CpuAction*>(surf_action)->set_bound(bound);
74 simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
75 new simgrid::kernel::activity::ExecImpl(name, surf_action, /*timeout_detector*/ nullptr, host));
77 XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name_.c_str());
78 simgrid::kernel::activity::ExecImpl::onCreation(exec);
83 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
84 SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
85 double* bytes_amount, double rate, double timeout)
88 /* Check that we are not mixing VMs and PMs in the parallel task */
89 bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
90 for (int i = 1; i < host_nb; i++) {
91 bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
92 xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
95 /* set surf's synchro */
96 simgrid::kernel::resource::Action* surf_action = nullptr;
97 simgrid::kernel::resource::Action* timeout_detector = nullptr;
98 if (not MC_is_active() && not MC_record_replay_is_active()) {
99 sg_host_t* host_list_cpy = new sg_host_t[host_nb];
100 std::copy_n(host_list, host_nb, host_list_cpy);
101 surf_action = surf_host_model->execute_parallel(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
103 timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
107 simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
108 new simgrid::kernel::activity::ExecImpl(name, surf_action, timeout_detector, nullptr));
110 XBT_DEBUG("Create parallel execute synchro %p", exec.get());
115 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
117 XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
119 /* Associate this simcall to the synchro */
120 synchro->simcalls_.push_back(simcall);
121 simcall->issuer->waiting_synchro = synchro;
123 /* set surf's synchro */
124 if (MC_is_active() || MC_record_replay_is_active()) {
125 synchro->state_ = SIMIX_DONE;
126 SIMIX_execution_finish(synchro);
130 /* If the synchro is already finished then perform the error handling */
131 if (synchro->state_ != SIMIX_RUNNING)
132 SIMIX_execution_finish(synchro);
135 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
137 int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
139 synchro->simcalls_.push_back(simcall);
140 SIMIX_execution_finish(synchro);
142 SIMIX_simcall_answer(simcall);
144 simcall_execution_test__set__result(simcall, res);
147 void SIMIX_execution_finish(smx_activity_t synchro)
149 simgrid::kernel::activity::ExecImplPtr exec =
150 boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
152 while (not synchro->simcalls_.empty()) {
153 smx_simcall_t simcall = synchro->simcalls_.front();
154 synchro->simcalls_.pop_front();
155 switch (exec->state_) {
158 /* do nothing, synchro done */
159 XBT_DEBUG("SIMIX_execution_finished: execution successful");
163 XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host_->get_cname());
164 simcall->issuer->context_->iwannadie = 1;
165 SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
169 XBT_DEBUG("SIMIX_execution_finished: execution canceled");
170 SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
174 XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
175 SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
179 xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
181 /* Fail the process if the host is down */
182 if (simcall->issuer->host_->is_off())
183 simcall->issuer->context_->iwannadie = 1;
185 simcall->issuer->waiting_synchro = nullptr;
186 simcall_execution_wait__set__result(simcall, exec->state_);
187 SIMIX_simcall_answer(simcall);
191 void SIMIX_set_category(smx_activity_t synchro, const char *category)
193 if (synchro->state_ != SIMIX_RUNNING)
196 simgrid::kernel::activity::ExecImplPtr exec =
197 boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
198 if (exec != nullptr) {
199 exec->surf_action_->set_category(category);
203 simgrid::kernel::activity::CommImplPtr comm =
204 boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
205 if (comm != nullptr) {
206 comm->surfAction_->set_category(category);