Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Minor changes
[simgrid.git] / src / simix / smx_host.cpp
1 /* Copyright (c) 2007-2018. 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.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"
12 #include "xbt/ex.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 (not Host::EXTENSION_ID.valid())
23         Host::EXTENSION_ID = s4u::Host::extension_create<simix::Host>();
24     }
25
26     Host::~Host()
27     {
28       /* All processes should be gone when the host is turned off (by the end of the simulation). */
29       if (not process_list.empty()) {
30         std::string msg     = std::string("Shutting down host, but it's not empty:");
31         for (auto const& process : process_list)
32           msg += "\n\t" + std::string(process.get_name());
33
34         SIMIX_display_process_status();
35         THROWF(arg_error, 0, "%s", msg.c_str());
36       }
37       for (auto const& arg : auto_restart_processes)
38         delete arg;
39       auto_restart_processes.clear();
40       for (auto const& arg : boot_processes)
41         delete arg;
42       boot_processes.clear();
43     }
44
45     /** Re-starts all the actors that are marked as restartable.
46      *
47      * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
48      */
49     void Host::turnOn()
50     {
51       for (auto const& arg : boot_processes) {
52         XBT_DEBUG("Booting Process %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
53         smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
54                                                                   arg->properties.get(), nullptr);
55         if (arg->kill_time >= 0)
56           simcall_process_set_kill_time(actor, arg->kill_time);
57         if (arg->auto_restart)
58           actor->auto_restart = arg->auto_restart;
59       }
60     }
61
62 }} // namespaces
63
64 /* needs to be public and without simcall for exceptions and logging events */
65 const char* sg_host_self_get_name()
66 {
67   sg_host_t host = sg_host_self();
68   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
69     return "";
70
71   return host->get_cname();
72 }
73
74 /**
75  * \brief Add a process to the list of the processes that the host will restart when it comes back
76  * This function add a process to the list of the processes that will be restarted when the host comes
77  * back. It is expected that this function is called when the host is down.
78  * The processes will only be restarted once, meaning that you will have to register the process
79  * again to restart the process again.
80  */
81 void SIMIX_host_add_auto_restart_process(sg_host_t host, const char* name, std::function<void()> code, void* data,
82                                          double kill_time, std::unordered_map<std::string, std::string>* properties,
83                                          int auto_restart)
84 {
85   simgrid::kernel::actor::ProcessArg* arg =
86       new simgrid::kernel::actor::ProcessArg(name, code, data, host, kill_time, nullptr, auto_restart);
87   arg->properties.reset(properties, [](decltype(properties)) {});
88
89   if (host->is_off() && watched_hosts.find(host->get_cname()) == watched_hosts.end()) {
90     watched_hosts.insert(host->get_cname());
91     XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->get_cname());
92   }
93   XBT_DEBUG("Adding Process %s to the auto-restart list of Host %s", arg->name.c_str(), arg->host->get_cname());
94   host->extension<simgrid::simix::Host>()->auto_restart_processes.push_back(arg);
95 }
96
97 /** @brief Restart the list of processes that have been registered to the host */
98 void SIMIX_host_autorestart(sg_host_t host)
99 {
100   std::vector<simgrid::kernel::actor::ProcessArg*> process_list =
101       host->extension<simgrid::simix::Host>()->auto_restart_processes;
102
103   for (auto const& arg : process_list) {
104     XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->get_cname());
105     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
106                                                               arg->properties.get(), nullptr);
107     if (arg->kill_time >= 0)
108       simcall_process_set_kill_time(actor, arg->kill_time);
109     if (arg->auto_restart)
110       actor->auto_restart = arg->auto_restart;
111   }
112   process_list.clear();
113 }
114
115 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
116 SIMIX_execution_start(const char* name, double flops_amount, double priority, double bound, sg_host_t host)
117 {
118   /* set surf's action */
119   simgrid::kernel::resource::Action* surf_action = nullptr;
120   if (not MC_is_active() && not MC_record_replay_is_active()) {
121     surf_action = host->pimpl_cpu->execution_start(flops_amount);
122     surf_action->set_priority(priority);
123     if (bound > 0)
124       static_cast<simgrid::surf::CpuAction*>(surf_action)->set_bound(bound);
125   }
126
127   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
128       new simgrid::kernel::activity::ExecImpl(name, surf_action, /*timeout_detector*/ nullptr, host));
129
130   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name_.c_str());
131   simgrid::kernel::activity::ExecImpl::onCreation(exec);
132
133   return exec;
134 }
135
136 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
137 SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
138                                double* bytes_amount, double rate, double timeout)
139 {
140
141   /* Check that we are not mixing VMs and PMs in the parallel task */
142   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
143   for (int i = 1; i < host_nb; i++) {
144     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
145     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
146   }
147
148   /* set surf's synchro */
149   simgrid::kernel::resource::Action* surf_action      = nullptr;
150   simgrid::kernel::resource::Action* timeout_detector = nullptr;
151   if (not MC_is_active() && not MC_record_replay_is_active()) {
152     sg_host_t* host_list_cpy = new sg_host_t[host_nb];
153     std::copy_n(host_list, host_nb, host_list_cpy);
154     surf_action = surf_host_model->execute_parallel(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
155     if (timeout > 0) {
156       timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
157     }
158   }
159
160   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
161       new simgrid::kernel::activity::ExecImpl(name, surf_action, timeout_detector, nullptr));
162
163   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
164
165   return exec;
166 }
167
168 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
169 {
170   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
171
172   /* Associate this simcall to the synchro */
173   synchro->simcalls_.push_back(simcall);
174   simcall->issuer->waiting_synchro = synchro;
175
176   /* set surf's synchro */
177   if (MC_is_active() || MC_record_replay_is_active()) {
178     synchro->state_ = SIMIX_DONE;
179     SIMIX_execution_finish(synchro);
180     return;
181   }
182
183   /* If the synchro is already finished then perform the error handling */
184   if (synchro->state_ != SIMIX_RUNNING)
185     SIMIX_execution_finish(synchro);
186 }
187
188 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
189 {
190   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
191   if (res) {
192     synchro->simcalls_.push_back(simcall);
193     SIMIX_execution_finish(synchro);
194   } else {
195     SIMIX_simcall_answer(simcall);
196   }
197   simcall_execution_test__set__result(simcall, res);
198 }
199
200 void SIMIX_execution_finish(smx_activity_t synchro)
201 {
202   simgrid::kernel::activity::ExecImplPtr exec =
203       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
204
205   while (not synchro->simcalls_.empty()) {
206     smx_simcall_t simcall = synchro->simcalls_.front();
207     synchro->simcalls_.pop_front();
208     switch (exec->state_) {
209
210       case SIMIX_DONE:
211         /* do nothing, synchro done */
212         XBT_DEBUG("SIMIX_execution_finished: execution successful");
213         break;
214
215       case SIMIX_FAILED:
216         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host->get_cname());
217         simcall->issuer->context->iwannadie = 1;
218         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
219         break;
220
221       case SIMIX_CANCELED:
222         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
223         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
224         break;
225
226       case SIMIX_TIMEOUT:
227         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
228         SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
229         break;
230
231       default:
232         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
233     }
234     /* Fail the process if the host is down */
235     if (simcall->issuer->host->is_off())
236       simcall->issuer->context->iwannadie = 1;
237
238     simcall->issuer->waiting_synchro = nullptr;
239     simcall_execution_wait__set__result(simcall, exec->state_);
240     SIMIX_simcall_answer(simcall);
241   }
242 }
243
244 void SIMIX_set_category(smx_activity_t synchro, const char *category)
245 {
246   if (synchro->state_ != SIMIX_RUNNING)
247     return;
248
249   simgrid::kernel::activity::ExecImplPtr exec =
250       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
251   if (exec != nullptr) {
252     exec->surf_action_->set_category(category);
253     return;
254   }
255
256   simgrid::kernel::activity::CommImplPtr comm =
257       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
258   if (comm != nullptr) {
259     comm->surfAction_->set_category(category);
260   }
261 }