Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SIMIX] Remove SMPI-specific segment_index notion from the Actor class
[simgrid.git] / src / simix / smx_host.cpp
1 /* Copyright (c) 2007-2017. 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/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"
12 #include "xbt/ex.hpp"
13 #include <algorithm>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
16
17 namespace simgrid {
18   namespace simix {
19     simgrid::xbt::Extension<simgrid::s4u::Host, Host> Host::EXTENSION_ID;
20
21     Host::Host()
22     {
23       if (not Host::EXTENSION_ID.valid())
24         Host::EXTENSION_ID = s4u::Host::extension_create<simix::Host>();
25     }
26
27     Host::~Host()
28     {
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());
34
35         SIMIX_display_process_status();
36         THROWF(arg_error, 0, "%s", msg.c_str());
37       }
38       for (auto const& arg : auto_restart_processes)
39         delete arg;
40       auto_restart_processes.clear();
41       for (auto const& arg : boot_processes)
42         delete arg;
43       boot_processes.clear();
44     }
45
46     /** Re-starts all the actors that are marked as restartable.
47      *
48      * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
49      */
50     void Host::turnOn()
51     {
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;
60       }
61     }
62
63 }} // namespaces
64
65 /** @brief Stop the host if it is on */
66 void SIMIX_host_off(sg_host_t h, smx_actor_t issuer)
67 {
68   simgrid::simix::Host* host = h->extension<simgrid::simix::Host>();
69
70   xbt_assert((host != nullptr), "Invalid parameters");
71
72   if (h->isOn()) {
73     h->pimpl_cpu->turnOff();
74
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());
81       }
82     }
83   } else {
84     XBT_INFO("Host %s is already off", h->getCname());
85   }
86 }
87
88 sg_host_t sg_host_self()
89 {
90   smx_actor_t process = SIMIX_process_self();
91   return (process == nullptr) ? nullptr : process->host;
92 }
93
94 /* needs to be public and without simcall for exceptions and logging events */
95 const char* sg_host_self_get_name()
96 {
97   sg_host_t host = sg_host_self();
98   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
99     return "";
100
101   return host->getCname();
102 }
103
104 /**
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.
110  */
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,
113                                          int auto_restart)
114 {
115   simgrid::simix::ProcessArg* arg =
116       new simgrid::simix::ProcessArg(name, code, data, host, kill_time, nullptr, auto_restart);
117   arg->properties.reset(properties, [](decltype(properties)) {});
118
119   if (host->isOff() && watched_hosts.find(host->getCname()) == watched_hosts.end()) {
120     watched_hosts.insert(host->getCname());
121     XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->getCname());
122   }
123   host->extension<simgrid::simix::Host>()->auto_restart_processes.push_back(arg);
124 }
125
126 /** @brief Restart the list of processes that have been registered to the host */
127 void SIMIX_host_autorestart(sg_host_t host)
128 {
129   std::vector<simgrid::simix::ProcessArg*> process_list =
130       host->extension<simgrid::simix::Host>()->auto_restart_processes;
131
132   for (auto const& arg : process_list) {
133     XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->getCname());
134     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
135                                                               arg->properties.get(), nullptr);
136     if (arg->kill_time >= 0)
137       simcall_process_set_kill_time(actor, arg->kill_time);
138     if (arg->auto_restart)
139       actor->auto_restart = arg->auto_restart;
140   }
141   process_list.clear();
142 }
143
144 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
145 SIMIX_execution_start(const char* name, double flops_amount, double priority, double bound, sg_host_t host)
146 {
147   /* alloc structures and initialize */
148   simgrid::kernel::activity::ExecImplPtr exec =
149       simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(name, host));
150
151   /* set surf's action */
152   if (not MC_is_active() && not MC_record_replay_is_active()) {
153
154     exec->surfAction_ = host->pimpl_cpu->execution_start(flops_amount);
155     exec->surfAction_->setData(exec.get());
156     exec->surfAction_->setSharingWeight(priority);
157
158     if (bound > 0)
159       static_cast<simgrid::surf::CpuAction*>(exec->surfAction_)->setBound(bound);
160   }
161
162   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name.c_str());
163   simgrid::kernel::activity::ExecImpl::onCreation(exec);
164
165   return exec;
166 }
167
168 boost::intrusive_ptr<simgrid::kernel::activity::ExecImpl>
169 SIMIX_execution_parallel_start(const char* name, int host_nb, sg_host_t* host_list, double* flops_amount,
170                                double* bytes_amount, double rate, double timeout)
171 {
172
173   /* alloc structures and initialize */
174   simgrid::kernel::activity::ExecImplPtr exec =
175       simgrid::kernel::activity::ExecImplPtr(new simgrid::kernel::activity::ExecImpl(name, nullptr));
176
177   /* Check that we are not mixing VMs and PMs in the parallel task */
178   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
179   for (int i = 1; i < host_nb; i++) {
180     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
181     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
182   }
183
184   /* set surf's synchro */
185   if (not MC_is_active() && not MC_record_replay_is_active()) {
186     /* set surf's synchro */
187     sg_host_t* host_list_cpy = new sg_host_t[host_nb];
188     std::copy_n(host_list, host_nb, host_list_cpy);
189     exec->surfAction_ = surf_host_model->executeParallelTask(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
190     exec->surfAction_->setData(exec.get());
191     if (timeout > 0) {
192       exec->timeoutDetector = host_list[0]->pimpl_cpu->sleep(timeout);
193       exec->timeoutDetector->setData(exec.get());
194     }
195   }
196   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
197
198   return exec;
199 }
200
201 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
202 {
203   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state);
204
205   /* Associate this simcall to the synchro */
206   synchro->simcalls.push_back(simcall);
207   simcall->issuer->waiting_synchro = synchro;
208
209   /* set surf's synchro */
210   if (MC_is_active() || MC_record_replay_is_active()) {
211     synchro->state = SIMIX_DONE;
212     SIMIX_execution_finish(synchro);
213     return;
214   }
215
216   /* If the synchro is already finished then perform the error handling */
217   if (synchro->state != SIMIX_RUNNING)
218     SIMIX_execution_finish(synchro);
219 }
220
221 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
222 {
223   simcall_execution_test__set__result(simcall, (synchro->state != SIMIX_WAITING && synchro->state != SIMIX_RUNNING));
224   if (simcall_execution_test__get__result(simcall)) {
225     synchro->simcalls.push_back(simcall);
226     SIMIX_execution_finish(synchro);
227   } else {
228     SIMIX_simcall_answer(simcall);
229   }
230   /* If the synchro is already finished then perform the error handling */
231   if (synchro->state != SIMIX_RUNNING)
232     SIMIX_execution_finish(synchro);
233 }
234
235 void SIMIX_execution_finish(smx_activity_t synchro)
236 {
237   simgrid::kernel::activity::ExecImplPtr exec =
238       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
239
240   while (not synchro->simcalls.empty()) {
241     smx_simcall_t simcall = synchro->simcalls.front();
242     synchro->simcalls.pop_front();
243     switch (exec->state) {
244
245       case SIMIX_DONE:
246         /* do nothing, synchro done */
247         XBT_DEBUG("SIMIX_execution_finished: execution successful");
248         break;
249
250       case SIMIX_FAILED:
251         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host->getCname());
252         simcall->issuer->context->iwannadie = 1;
253         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
254         break;
255
256       case SIMIX_CANCELED:
257         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
258         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
259         break;
260
261       case SIMIX_TIMEOUT:
262         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
263         SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
264         break;
265
266       default:
267         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d",
268             (int)exec->state);
269     }
270     /* Fail the process if the host is down */
271     if (simcall->issuer->host->isOff())
272       simcall->issuer->context->iwannadie = 1;
273
274     simcall->issuer->waiting_synchro = nullptr;
275     simcall_execution_wait__set__result(simcall, exec->state);
276     SIMIX_simcall_answer(simcall);
277   }
278 }
279
280 void SIMIX_set_category(smx_activity_t synchro, const char *category)
281 {
282   if (synchro->state != SIMIX_RUNNING)
283     return;
284
285   simgrid::kernel::activity::ExecImplPtr exec =
286       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
287   if (exec != nullptr) {
288     exec->surfAction_->setCategory(category);
289     return;
290   }
291
292   simgrid::kernel::activity::CommImplPtr comm =
293       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
294   if (comm != nullptr) {
295     comm->surfAction_->setCategory(category);
296   }
297 }