Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't throw exception in destructor (sonar).
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2022. 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) : 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 }
35
36 HostImpl::~HostImpl()
37 {
38   /* All actors should be gone when the host is turned off (by the end of the simulation). */
39   if (not actor_list_.empty()) {
40     const char* msg = "Shutting down host, but it's not empty";
41     try {
42       std::string actors;
43       for (auto const& actor : actor_list_)
44         actors += "\n\t" + std::string(actor.get_name());
45
46       EngineImpl::get_instance()->display_all_actor_status();
47       xbt_die("%s:%s", msg, actors.c_str());
48     } catch (std::bad_alloc& ba) {
49       xbt_die("%s (cannot print actor list: %s)", msg, ba.what());
50     }
51   }
52   for (auto const& arg : actors_at_boot_)
53     delete arg;
54   actors_at_boot_.clear();
55
56   for (auto const& d : disks_)
57     d.second->destroy();
58
59   for (auto const& vm : vms_)
60     vm.second->vm_destroy();
61 }
62
63 /** @brief Fire the required callbacks and destroy the object
64  *
65  * Don't delete directly a Host, call h->destroy() instead.
66  */
67 void HostImpl::destroy()
68 {
69   s4u::Host::on_destruction(*this->get_iface());
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);
82   }
83 }
84
85 /** Kill all actors hosted here */
86 void HostImpl::turn_off(const actor::ActorImpl* issuer)
87 {
88   /* turn_off VMs running on host */
89   for (const auto& kv : vms_) {
90     // call s4u functions to generate the good on_state_change signal, maybe one day this wont be necessary
91     kv.second->get_iface()->shutdown();
92     kv.second->get_iface()->turn_off();
93   }
94   for (auto& actor : actor_list_) {
95     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
96               actor.get_host()->get_cname(), issuer->get_cname());
97     issuer->kill(&actor);
98   }
99   for (const auto& activity : EngineImpl::get_instance()->get_maestro()->activities_) {
100     auto* exec = dynamic_cast<activity::ExecImpl*>(activity.get());
101     if (exec != nullptr) {
102       auto hosts = exec->get_hosts();
103       if (std::find(hosts.begin(), hosts.end(), &piface_) != hosts.end()) {
104         exec->cancel();
105         exec->set_state(activity::State::FAILED);
106       }
107     }
108   }
109   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
110   // Then get rid of the others.
111   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const actor::ProcessArg* arg) {
112     if (arg->auto_restart)
113       return false;
114     delete arg;
115     return true;
116   });
117   actors_at_boot_.erase(elm, end(actors_at_boot_));
118 }
119
120 HostImpl* HostImpl::set_englobing_zone(routing::NetZoneImpl* englobing_zone)
121 {
122   englobing_zone_ = englobing_zone;
123   return this;
124 }
125
126 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
127 {
128   std::vector<s4u::ActorPtr> res;
129   for (auto& actor : actor_list_)
130     res.emplace_back(actor.get_ciface());
131   return res;
132 }
133 size_t HostImpl::get_actor_count() const
134 {
135   return actor_list_.size();
136 }
137
138 std::vector<s4u::Disk*> HostImpl::get_disks() const
139 {
140   std::vector<s4u::Disk*> disks;
141   for (auto const& d : disks_)
142     disks.push_back(d.second->get_iface());
143   return disks;
144 }
145
146 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, int core_amount, size_t ramsize)
147 {
148   auto* host_vm = new kernel::resource::VirtualMachineImpl(name, get_iface(), core_amount, ramsize);
149   auto* vm      = new s4u::VirtualMachine(host_vm);
150   host_vm->set_piface(vm);
151   return create_vm(name, vm);
152 }
153
154 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, s4u::VirtualMachine* vm)
155 {
156   vms_[name] = vm->get_vm_impl();
157
158   // Create a VCPU for this VM
159   std::vector<double> speeds;
160   for (unsigned long i = 0; i < get_iface()->get_pstate_count(); i++)
161     speeds.push_back(get_iface()->get_pstate_speed(i));
162
163   auto* cpu =
164       englobing_zone_->get_cpu_vm_model()->create_cpu(vm, speeds)->set_core_count(vm->get_vm_impl()->get_core_amount());
165
166   if (get_iface()->get_pstate() != 0)
167     cpu->set_pstate(get_iface()->get_pstate());
168
169   cpu->seal();
170
171   /* Currently, a VM uses the network resource of its physical host */
172   vm->set_netpoint(get_iface()->get_netpoint());
173
174   vm->seal();
175
176   return vm;
177 }
178
179 void HostImpl::move_vm(VirtualMachineImpl* vm, HostImpl* destination)
180 {
181   xbt_assert(vm && destination);
182
183   vms_.erase(vm->get_name());
184   destination->vms_[vm->get_name()] = vm;
185 }
186
187 void HostImpl::destroy_vm(const std::string& name)
188 {
189   auto* vm = vms_[name];
190   vms_.erase(name);
191   vm->vm_destroy();
192 }
193
194 VirtualMachineImpl* HostImpl::get_vm_by_name_or_null(const std::string& name) const
195 {
196   auto vm_it = vms_.find(name);
197   if (vm_it != vms_.end())
198     return vm_it->second;
199   return nullptr;
200 }
201
202 std::vector<s4u::VirtualMachine*> HostImpl::get_vms() const
203 {
204   std::vector<s4u::VirtualMachine*> vms;
205   for (const auto& kv : vms_) {
206     vms.push_back(kv.second->get_iface());
207   }
208   return vms;
209 }
210
211 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
212 {
213   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
214                                                                                           write_bandwidth);
215   return disk->set_host(&piface_)->get_iface();
216 }
217
218 void HostImpl::add_disk(const s4u::Disk* disk)
219 {
220   disks_[disk->get_name()] = disk->get_impl();
221 }
222
223 void HostImpl::remove_disk(const std::string& name)
224 {
225   disks_.erase(name);
226 }
227
228 void HostImpl::seal()
229 {
230   if (sealed_) {
231     return;
232   }
233   // seals host's CPU
234   get_iface()->get_cpu()->seal();
235   sealed_ = true;
236
237   /* seal its disks */
238   for (auto const& disk : disks_)
239     disk.second->seal();
240
241   /* seal its VMs */
242   for (auto const& vm : vms_)
243     vm.second->seal();
244 }
245 } // namespace resource
246 } // namespace kernel
247 } // namespace simgrid