Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 's4u_enforce' into 'master'
[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 "xbt/asserts.hpp"
11 #include "src/kernel/EngineImpl.hpp"
12 #include "src/kernel/resource/VirtualMachineImpl.hpp"
13
14 #include <string>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_host, ker_resource, "Host resources agregate CPU, networking and I/O features");
17
18 /*************
19  * Callbacks *t
20  *************/
21
22 namespace simgrid::kernel::resource {
23
24 /*********
25  * Model *
26  *********/
27 /************
28  * Resource *
29  ************/
30 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
31 {
32   xbt_enforce(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
33 }
34
35 HostImpl::~HostImpl()
36 {
37   /* All actors should be gone when the host is turned off (by the end of the simulation). */
38   if (not actor_list_.empty()) {
39     const char* msg = "Shutting down host, but it's not empty";
40     try {
41       std::string actors;
42       for (auto const& actor : actor_list_)
43         actors += "\n\t" + std::string(actor.get_name());
44
45       EngineImpl::get_instance()->display_all_actor_status();
46       xbt_die("%s:%s", msg, actors.c_str());
47     } catch (const std::bad_alloc& ba) {
48       xbt_die("%s (cannot print actor list: %s)", msg, ba.what());
49     }
50   }
51   for (auto const& arg : actors_at_boot_)
52     delete arg;
53   actors_at_boot_.clear();
54
55   for (auto const& [_, d] : disks_)
56     d->destroy();
57
58   for (auto const& [_, vm] : vms_)
59     vm->vm_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   delete this;
70 }
71
72 /** Re-starts all the actors that are marked as restartable.
73  *
74  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
75  */
76 void HostImpl::turn_on() const
77 {
78   for (auto const& arg : actors_at_boot_) {
79     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
80     actor::ActorImplPtr actor = actor::ActorImpl::create(arg);
81   }
82 }
83
84 /** Kill all actors hosted here */
85 void HostImpl::turn_off(const actor::ActorImpl* issuer)
86 {
87   /* turn_off VMs running on host */
88   for (const auto& [_, vm] : vms_) {
89     // call s4u functions to generate the good on_state_change signal, maybe one day this wont be necessary
90     vm->get_iface()->shutdown();
91     vm->get_iface()->turn_off();
92   }
93   for (auto& actor : actor_list_) {
94     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
95               actor.get_host()->get_cname(), issuer->get_cname());
96     issuer->kill(&actor);
97   }
98   for (const auto& activity : EngineImpl::get_instance()->get_maestro()->activities_) {
99     auto const& hosts = activity->get_hosts();
100     if (std::find(hosts.begin(), hosts.end(), &piface_) != hosts.end()) {
101       activity->cancel();
102       activity->set_state(activity::State::FAILED);
103     }
104   }
105   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
106   // Then get rid of the others.
107   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const actor::ProcessArg* arg) {
108     if (arg->auto_restart)
109       return false;
110     delete arg;
111     return true;
112   });
113   actors_at_boot_.erase(elm, end(actors_at_boot_));
114 }
115
116 HostImpl* HostImpl::set_englobing_zone(routing::NetZoneImpl* englobing_zone)
117 {
118   englobing_zone_ = englobing_zone;
119   return this;
120 }
121
122 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
123 {
124   std::vector<s4u::ActorPtr> res;
125   for (auto& actor : actor_list_)
126     res.emplace_back(actor.get_ciface());
127   return res;
128 }
129 size_t HostImpl::get_actor_count() const
130 {
131   return actor_list_.size();
132 }
133
134 std::vector<s4u::Disk*> HostImpl::get_disks() const
135 {
136   std::vector<s4u::Disk*> disks;
137   for (auto const& [_, d] : disks_)
138     disks.push_back(d->get_iface());
139   return disks;
140 }
141
142 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, int core_amount, size_t ramsize)
143 {
144   auto* host_vm = new kernel::resource::VirtualMachineImpl(name, get_iface(), core_amount, ramsize);
145   auto* vm      = new s4u::VirtualMachine(host_vm);
146   host_vm->set_piface(vm);
147   return create_vm(name, vm);
148 }
149
150 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, s4u::VirtualMachine* vm)
151 {
152   vms_[name] = vm->get_vm_impl();
153
154   // Create a VCPU for this VM
155   std::vector<double> speeds;
156   for (unsigned long i = 0; i < get_iface()->get_pstate_count(); i++)
157     speeds.push_back(get_iface()->get_pstate_speed(i));
158
159   auto* cpu =
160       englobing_zone_->get_cpu_vm_model()->create_cpu(vm, speeds)->set_core_count(vm->get_vm_impl()->get_core_amount());
161
162   if (get_iface()->get_pstate() != 0)
163     cpu->set_pstate(get_iface()->get_pstate());
164
165   cpu->seal();
166
167   /* Currently, a VM uses the network resource of its physical host */
168   vm->set_netpoint(get_iface()->get_netpoint());
169
170   vm->seal();
171
172   return vm;
173 }
174
175 void HostImpl::move_vm(VirtualMachineImpl* vm, HostImpl* destination)
176 {
177   xbt_assert(vm && destination);
178
179   vms_.erase(vm->get_name());
180   destination->vms_[vm->get_name()] = vm;
181 }
182
183 void HostImpl::destroy_vm(const std::string& name)
184 {
185   auto* vm = vms_[name];
186   vms_.erase(name);
187   vm->vm_destroy();
188 }
189
190 VirtualMachineImpl* HostImpl::get_vm_by_name_or_null(const std::string& name) const
191 {
192   auto vm_it = vms_.find(name);
193   return vm_it == vms_.end() ? nullptr : vm_it->second;
194 }
195
196 std::vector<s4u::VirtualMachine*> HostImpl::get_vms() const
197 {
198   std::vector<s4u::VirtualMachine*> vms;
199   for (const auto& [_, vm] : vms_) {
200     vms.push_back(vm->get_iface());
201   }
202   return vms;
203 }
204
205 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
206 {
207   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
208                                                                                           write_bandwidth);
209   return disk->set_host(&piface_)->get_iface();
210 }
211
212 void HostImpl::add_disk(const s4u::Disk* disk)
213 {
214   disks_[disk->get_name()] = disk->get_impl();
215 }
216
217 void HostImpl::remove_disk(const std::string& name)
218 {
219   disks_.erase(name);
220 }
221
222 void HostImpl::seal()
223 {
224   if (sealed_) {
225     return;
226   }
227   // seals host's CPU
228   get_iface()->get_cpu()->seal();
229   sealed_ = true;
230
231   /* seal its disks */
232   for (auto const& [_, disk] : disks_)
233     disk->seal();
234
235   /* seal its VMs */
236   for (auto const& [_, vm] : vms_)
237     vm->seal();
238 }
239 } // namespace simgrid::kernel::resource