Logo AND Algorithmique Numérique Distribuée

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