Logo AND Algorithmique Numérique Distribuée

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