Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
get rid od auto_restart_processes_
[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/kernel/activity/CommImpl.hpp"
9 #include "src/kernel/activity/ExecImpl.hpp"
10 #include "src/mc/mc_replay.hpp"
11 #include "src/plugins/vm/VirtualMachineImpl.hpp"
12 #include "src/simix/smx_host_private.hpp"
13 #include "xbt/ex.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_host, simix, "SIMIX hosts");
16
17 /* needs to be public and without simcall for exceptions and logging events */
18 const char* sg_host_self_get_name()
19 {
20   sg_host_t host = sg_host_self();
21   if (host == nullptr || SIMIX_process_self() == simix_global->maestro_process)
22     return "";
23
24   return host->get_cname();
25 }
26
27 /**
28  * @brief Add a process to the list of the processes that the host will restart when it comes back
29  * This function add a process to the list of the processes that will be restarted when the host comes
30  * back. It is expected that this function is called when the host is down.
31  * The processes will only be restarted once, meaning that you will have to register the process
32  * again to restart the process again.
33  */
34 void SIMIX_host_add_auto_restart_process(sg_host_t host, simgrid::kernel::actor::ActorImpl* actor)
35 {
36   simgrid::kernel::actor::ProcessArg* arg = new simgrid::kernel::actor::ProcessArg(host, actor);
37
38   if (host->is_off() && watched_hosts.find(host->get_cname()) == watched_hosts.end()) {
39     watched_hosts.insert(host->get_cname());
40     XBT_DEBUG("Push host %s to watched_hosts because state == SURF_RESOURCE_OFF", host->get_cname());
41   }
42   if (host->pimpl_->actors_at_boot_.find(actor->get_name()) == host->pimpl_->actors_at_boot_.end()) {
43     XBT_DEBUG("Adding Process %s to the actors_at_boot_ list of Host %s", arg->name.c_str(), arg->host->get_cname());
44     host->pimpl_->actors_at_boot_.insert({arg->name, arg});
45   }
46 }
47
48 /** @brief Restart the list of processes that have been registered to the host */
49 void SIMIX_host_autorestart(sg_host_t host)
50 {
51   std::map<std::string, simgrid::kernel::actor::ProcessArg*> process_list = host->pimpl_->actors_at_boot_;
52
53   for (auto const& elm : process_list) {
54     simgrid::kernel::actor::ProcessArg* arg = elm.second;
55     XBT_DEBUG("Restarting Process %s@%s right now", arg->name.c_str(), arg->host->get_cname());
56     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
57                                                               arg->properties.get(), nullptr);
58     if (arg->kill_time >= 0)
59       simcall_process_set_kill_time(actor, arg->kill_time);
60     if (arg->auto_restart)
61       actor->auto_restart_ = arg->auto_restart;
62   }
63   process_list.clear();
64 }
65
66 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_start(std::string name, std::string category,
67                                                              double flops_amount, double priority, double bound,
68                                                              sg_host_t host)
69 {
70   /* set surf's action */
71   simgrid::kernel::resource::Action* surf_action = nullptr;
72   if (not MC_is_active() && not MC_record_replay_is_active()) {
73     surf_action = host->pimpl_cpu->execution_start(flops_amount);
74     surf_action->set_priority(priority);
75     if (bound > 0)
76       surf_action->set_bound(bound);
77   }
78
79   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
80       new simgrid::kernel::activity::ExecImpl(name, surf_action, /*timeout_detector*/ nullptr, host));
81
82   exec->set_category(category);
83   XBT_DEBUG("Create execute synchro %p: %s", exec.get(), exec->name_.c_str());
84   simgrid::kernel::activity::ExecImpl::on_creation(exec);
85
86   return exec;
87 }
88
89 simgrid::kernel::activity::ExecImplPtr SIMIX_execution_parallel_start(std::string name, int host_nb,
90                                                                       sg_host_t* host_list, double* flops_amount,
91                                                                       double* bytes_amount, double rate, double timeout)
92 {
93
94   /* Check that we are not mixing VMs and PMs in the parallel task */
95   bool is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[0]));
96   for (int i = 1; i < host_nb; i++) {
97     bool tmp_is_a_vm = (nullptr != dynamic_cast<simgrid::s4u::VirtualMachine*>(host_list[i]));
98     xbt_assert(is_a_vm == tmp_is_a_vm, "parallel_execute: mixing VMs and PMs is not supported (yet).");
99   }
100
101   /* set surf's synchro */
102   simgrid::kernel::resource::Action* surf_action      = nullptr;
103   simgrid::kernel::resource::Action* timeout_detector = nullptr;
104   if (not MC_is_active() && not MC_record_replay_is_active()) {
105     sg_host_t* host_list_cpy = new sg_host_t[host_nb];
106     std::copy_n(host_list, host_nb, host_list_cpy);
107     surf_action = surf_host_model->execute_parallel(host_nb, host_list_cpy, flops_amount, bytes_amount, rate);
108     if (timeout > 0) {
109       timeout_detector = host_list[0]->pimpl_cpu->sleep(timeout);
110     }
111   }
112
113   simgrid::kernel::activity::ExecImplPtr exec = simgrid::kernel::activity::ExecImplPtr(
114       new simgrid::kernel::activity::ExecImpl(name, surf_action, timeout_detector, nullptr));
115
116   XBT_DEBUG("Create parallel execute synchro %p", exec.get());
117
118   return exec;
119 }
120
121 void simcall_HANDLER_execution_wait(smx_simcall_t simcall, smx_activity_t synchro)
122 {
123   XBT_DEBUG("Wait for execution of synchro %p, state %d", synchro.get(), (int)synchro->state_);
124
125   /* Associate this simcall to the synchro */
126   synchro->simcalls_.push_back(simcall);
127   simcall->issuer->waiting_synchro = synchro;
128
129   /* set surf's synchro */
130   if (MC_is_active() || MC_record_replay_is_active()) {
131     synchro->state_ = SIMIX_DONE;
132     SIMIX_execution_finish(synchro);
133     return;
134   }
135
136   /* If the synchro is already finished then perform the error handling */
137   if (synchro->state_ != SIMIX_RUNNING)
138     SIMIX_execution_finish(synchro);
139 }
140
141 void simcall_HANDLER_execution_test(smx_simcall_t simcall, smx_activity_t synchro)
142 {
143   int res = (synchro->state_ != SIMIX_WAITING && synchro->state_ != SIMIX_RUNNING);
144   if (res) {
145     synchro->simcalls_.push_back(simcall);
146     SIMIX_execution_finish(synchro);
147   } else {
148     SIMIX_simcall_answer(simcall);
149   }
150   simcall_execution_test__set__result(simcall, res);
151 }
152
153 void SIMIX_execution_finish(smx_activity_t synchro)
154 {
155   simgrid::kernel::activity::ExecImplPtr exec =
156       boost::static_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
157
158   while (not synchro->simcalls_.empty()) {
159     smx_simcall_t simcall = synchro->simcalls_.front();
160     synchro->simcalls_.pop_front();
161     switch (exec->state_) {
162
163       case SIMIX_DONE:
164         /* do nothing, synchro done */
165         XBT_DEBUG("SIMIX_execution_finished: execution successful");
166         break;
167
168       case SIMIX_FAILED:
169         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", simcall->issuer->host_->get_cname());
170         simcall->issuer->context_->iwannadie = 1;
171         SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
172         break;
173
174       case SIMIX_CANCELED:
175         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
176         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
177         break;
178
179       case SIMIX_TIMEOUT:
180         XBT_DEBUG("SIMIX_execution_finished: execution timeouted");
181         SMX_EXCEPTION(simcall->issuer, timeout_error, 0, "Timeouted");
182         break;
183
184       default:
185         xbt_die("Internal error in SIMIX_execution_finish: unexpected synchro state %d", (int)exec->state_);
186     }
187     /* Fail the process if the host is down */
188     if (simcall->issuer->host_->is_off())
189       simcall->issuer->context_->iwannadie = 1;
190
191     simcall->issuer->waiting_synchro = nullptr;
192     simcall_execution_wait__set__result(simcall, exec->state_);
193     SIMIX_simcall_answer(simcall);
194   }
195 }
196
197 void SIMIX_set_category(smx_activity_t synchro, std::string category)
198 {
199   if (synchro->state_ != SIMIX_RUNNING)
200     return;
201
202   simgrid::kernel::activity::ExecImplPtr exec =
203       boost::dynamic_pointer_cast<simgrid::kernel::activity::ExecImpl>(synchro);
204   if (exec != nullptr) {
205     exec->surf_action_->set_category(category);
206     return;
207   }
208
209   simgrid::kernel::activity::CommImplPtr comm =
210       boost::dynamic_pointer_cast<simgrid::kernel::activity::CommImpl>(synchro);
211   if (comm != nullptr) {
212     comm->surfAction_->set_category(category);
213   }
214 }