Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5e7d0da0e6943473f9e48c7430a6ca89731907d6
[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::kernel::resource {
22
23 /*********
24  * Model *
25  *********/
26 /************
27  * Resource *
28  ************/
29 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
30 {
31   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
32 }
33
34 HostImpl::~HostImpl()
35 {
36   /* All actors should be gone when the host is turned off (by the end of the simulation). */
37   if (not actor_list_.empty()) {
38     const char* msg = "Shutting down host, but it's not empty";
39     try {
40       std::string actors;
41       for (auto const& actor : actor_list_)
42         actors += "\n\t" + std::string(actor.get_name());
43
44       EngineImpl::get_instance()->display_all_actor_status();
45       xbt_die("%s:%s", msg, actors.c_str());
46     } catch (const std::bad_alloc& ba) {
47       xbt_die("%s (cannot print actor list: %s)", msg, ba.what());
48     }
49   }
50   for (auto const& arg : actors_at_boot_)
51     delete arg;
52   actors_at_boot_.clear();
53
54   for (auto const& [_, d] : disks_)
55     d->destroy();
56
57   for (auto const& [_, vm] : vms_)
58     vm->vm_destroy();
59 }
60
61 /** @brief Fire the required callbacks and destroy the object
62  *
63  * Don't delete directly a Host, call h->destroy() instead.
64  */
65 void HostImpl::destroy()
66 {
67   s4u::Host::on_destruction(*this->get_iface());
68   delete this;
69 }
70
71 /** Re-starts all the actors that are marked as restartable.
72  *
73  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
74  */
75 void HostImpl::turn_on() const
76 {
77   for (auto const& arg : actors_at_boot_) {
78     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
79     actor::ActorImplPtr actor = actor::ActorImpl::create(arg);
80   }
81 }
82
83 /** Kill all actors hosted here */
84 void HostImpl::turn_off(const actor::ActorImpl* issuer)
85 {
86   /* turn_off VMs running on host */
87   for (const auto& [_, vm] : vms_) {
88     // call s4u functions to generate the good on_state_change signal, maybe one day this wont be necessary
89     vm->get_iface()->shutdown();
90     vm->get_iface()->turn_off();
91   }
92   for (auto& actor : actor_list_) {
93     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
94               actor.get_host()->get_cname(), issuer->get_cname());
95     issuer->kill(&actor);
96   }
97   for (const auto& activity : EngineImpl::get_instance()->get_maestro()->activities_) {
98     auto* exec = dynamic_cast<activity::ExecImpl*>(activity.get());
99     if (exec != nullptr) {
100       auto hosts = exec->get_hosts();
101       if (std::find(hosts.begin(), hosts.end(), &piface_) != hosts.end()) {
102         exec->cancel();
103         exec->set_state(activity::State::FAILED);
104       }
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   if (get_iface()->get_pstate() != 0)
165     cpu->set_pstate(get_iface()->get_pstate());
166
167   cpu->seal();
168
169   /* Currently, a VM uses the network resource of its physical host */
170   vm->set_netpoint(get_iface()->get_netpoint());
171
172   vm->seal();
173
174   return vm;
175 }
176
177 void HostImpl::move_vm(VirtualMachineImpl* vm, HostImpl* destination)
178 {
179   xbt_assert(vm && destination);
180
181   vms_.erase(vm->get_name());
182   destination->vms_[vm->get_name()] = vm;
183 }
184
185 void HostImpl::destroy_vm(const std::string& name)
186 {
187   auto* vm = vms_[name];
188   vms_.erase(name);
189   vm->vm_destroy();
190 }
191
192 VirtualMachineImpl* HostImpl::get_vm_by_name_or_null(const std::string& name) const
193 {
194   auto vm_it = vms_.find(name);
195   return vm_it == vms_.end() ? nullptr : vm_it->second;
196 }
197
198 std::vector<s4u::VirtualMachine*> HostImpl::get_vms() const
199 {
200   std::vector<s4u::VirtualMachine*> vms;
201   for (const auto& [_, vm] : vms_) {
202     vms.push_back(vm->get_iface());
203   }
204   return vms;
205 }
206
207 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
208 {
209   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
210                                                                                           write_bandwidth);
211   return disk->set_host(&piface_)->get_iface();
212 }
213
214 void HostImpl::add_disk(const s4u::Disk* disk)
215 {
216   disks_[disk->get_name()] = disk->get_impl();
217 }
218
219 void HostImpl::remove_disk(const std::string& name)
220 {
221   disks_.erase(name);
222 }
223
224 void HostImpl::seal()
225 {
226   if (sealed_) {
227     return;
228   }
229   // seals host's CPU
230   get_iface()->get_cpu()->seal();
231   sealed_ = true;
232
233   /* seal its disks */
234   for (auto const& [_, disk] : disks_)
235     disk->seal();
236
237   /* seal its VMs */
238   for (auto const& [_, vm] : vms_)
239     vm->seal();
240 }
241 } // namespace simgrid::kernel::resource