Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] bunch of const
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2021. 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 <simgrid/kernel/routing/NetPoint.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <simgrid/s4u/Host.hpp>
9
10 #include "src/kernel/EngineImpl.hpp"
11 #include "src/kernel/resource/VirtualMachineImpl.hpp"
12
13 #include <string>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_host, ker_resource, "Host resources agregate CPU, networking and I/O features");
16
17 /*************
18  * Callbacks *t
19  *************/
20
21 namespace simgrid {
22 namespace kernel {
23 namespace resource {
24
25 /*********
26  * Model *
27  *********/
28 /************
29  * Resource *
30  ************/
31 HostImpl::HostImpl(const std::string& name, s4u::Host* piface) : piface_(this), name_(name)
32 {
33   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
34   s4u::Engine::get_instance()->host_register(name_, piface);
35 }
36
37 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
38 {
39   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
40   s4u::Engine::get_instance()->host_register(name_, &piface_);
41 }
42
43 HostImpl::~HostImpl()
44 {
45   /* All actors should be gone when the host is turned off (by the end of the simulation). */
46   if (not actor_list_.empty()) {
47     std::string msg = "Shutting down host, but it's not empty:";
48     for (auto const& actor : actor_list_)
49       msg += "\n\t" + std::string(actor.get_name());
50
51     EngineImpl::get_instance()->display_all_actor_status();
52     xbt_die("%s", msg.c_str());
53   }
54   for (auto const& arg : actors_at_boot_)
55     delete arg;
56   actors_at_boot_.clear();
57
58   for (auto const& d : disks_)
59     d.second->destroy();
60 }
61
62 /** @brief Fire the required callbacks and destroy the object
63  *
64  * Don't delete directly a Host, call h->destroy() instead.
65  */
66 void HostImpl::destroy()
67 {
68   s4u::Host::on_destruction(*this->get_iface());
69   s4u::Engine::get_instance()->host_unregister(std::string(name_));
70   delete this;
71 }
72
73 /** Re-starts all the actors that are marked as restartable.
74  *
75  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
76  */
77 void HostImpl::turn_on() const
78 {
79   for (auto const& arg : actors_at_boot_) {
80     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
81     actor::ActorImplPtr actor = actor::ActorImpl::create(arg->name, arg->code, nullptr, arg->host, nullptr);
82     actor->set_properties(arg->properties);
83     if (arg->on_exit)
84       *actor->on_exit = *arg->on_exit;
85     if (arg->kill_time >= 0)
86       actor->set_kill_time(arg->kill_time);
87     if (arg->auto_restart)
88       actor->set_auto_restart(arg->auto_restart);
89     if (arg->daemon_)
90       actor->daemonize();
91   }
92 }
93
94 /** Kill all actors hosted here */
95 void HostImpl::turn_off(const actor::ActorImpl* issuer)
96 {
97   for (auto& actor : actor_list_) {
98     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
99               actor.get_host()->get_cname(), issuer->get_cname());
100     issuer->kill(&actor);
101   }
102   for (const auto& activity : EngineImpl::get_instance()->get_maestro()->activities_) {
103     auto* exec = dynamic_cast<activity::ExecImpl*>(activity.get());
104     if (exec != nullptr) {
105       auto hosts = exec->get_hosts();
106       if (std::find(hosts.begin(), hosts.end(), &piface_) != hosts.end()) {
107         exec->cancel();
108         exec->state_ = activity::State::FAILED;
109       }
110     }
111   }
112   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
113   // Then get rid of the others.
114   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const actor::ProcessArg* arg) {
115     if (arg->auto_restart)
116       return false;
117     delete arg;
118     return true;
119   });
120   actors_at_boot_.erase(elm, end(actors_at_boot_));
121 }
122
123 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
124 {
125   std::vector<s4u::ActorPtr> res;
126   for (auto& actor : actor_list_)
127     res.emplace_back(actor.get_ciface());
128   return res;
129 }
130 size_t HostImpl::get_actor_count() const
131 {
132   return actor_list_.size();
133 }
134
135 std::vector<s4u::Disk*> HostImpl::get_disks() const
136 {
137   std::vector<s4u::Disk*> disks;
138   for (auto const& d : disks_)
139     disks.push_back(d.second->get_iface());
140   return disks;
141 }
142
143 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
144 {
145   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
146                                                                                           write_bandwidth);
147   return disk->set_host(&piface_)->get_iface();
148 }
149
150 void HostImpl::add_disk(const s4u::Disk* disk)
151 {
152   disks_[disk->get_name()] = disk->get_impl();
153 }
154
155 void HostImpl::remove_disk(const std::string& name)
156 {
157   disks_.erase(name);
158 }
159
160 void HostImpl::seal()
161 {
162   if (sealed_) {
163     return;
164   }
165   // seals host's CPU
166   get_iface()->get_cpu()->seal();
167   sealed_ = true;
168
169   /* seal its disks */
170   for (auto const& disk : disks_)
171     disk.second->seal();
172 }
173 } // namespace resource
174 } // namespace kernel
175 } // namespace simgrid