1 /* Copyright (c) 2013-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include <simgrid/kernel/routing/NetPoint.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <simgrid/s4u/Host.hpp>
10 #include "src/kernel/EngineImpl.hpp"
11 #include "src/kernel/resource/VirtualMachineImpl.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_host, ker_resource, "Host resources agregate CPU, networking and I/O features");
21 namespace simgrid::kernel::resource {
29 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
31 xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
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";
41 for (auto const& actor : actor_list_)
42 actors += "\n\t" + std::string(actor.get_name());
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());
50 for (auto const& arg : actors_at_boot_)
52 actors_at_boot_.clear();
54 for (auto const& [_, d] : disks_)
57 for (auto const& [_, vm] : vms_)
61 /** @brief Fire the required callbacks and destroy the object
63 * Don't delete directly a Host, call h->destroy() instead.
65 void HostImpl::destroy()
67 s4u::Host::on_destruction(*this->get_iface());
71 /** Re-starts all the actors that are marked as restartable.
73 * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
75 void HostImpl::turn_on() const
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);
83 /** Kill all actors hosted here */
84 void HostImpl::turn_off(const actor::ActorImpl* issuer)
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();
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());
97 for (const auto& activity : EngineImpl::get_instance()->get_maestro()->activities_) {
98 auto hosts = activity->get_hosts();
99 if (std::find(hosts.begin(), hosts.end(), &piface_) != hosts.end()) {
101 activity->set_state(activity::State::FAILED);
104 // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
105 // Then get rid of the others.
106 auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const actor::ProcessArg* arg) {
107 if (arg->auto_restart)
112 actors_at_boot_.erase(elm, end(actors_at_boot_));
115 HostImpl* HostImpl::set_englobing_zone(routing::NetZoneImpl* englobing_zone)
117 englobing_zone_ = englobing_zone;
121 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
123 std::vector<s4u::ActorPtr> res;
124 for (auto& actor : actor_list_)
125 res.emplace_back(actor.get_ciface());
128 size_t HostImpl::get_actor_count() const
130 return actor_list_.size();
133 std::vector<s4u::Disk*> HostImpl::get_disks() const
135 std::vector<s4u::Disk*> disks;
136 for (auto const& [_, d] : disks_)
137 disks.push_back(d->get_iface());
141 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, int core_amount, size_t ramsize)
143 auto* host_vm = new kernel::resource::VirtualMachineImpl(name, get_iface(), core_amount, ramsize);
144 auto* vm = new s4u::VirtualMachine(host_vm);
145 host_vm->set_piface(vm);
146 return create_vm(name, vm);
149 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, s4u::VirtualMachine* vm)
151 vms_[name] = vm->get_vm_impl();
153 // Create a VCPU for this VM
154 std::vector<double> speeds;
155 for (unsigned long i = 0; i < get_iface()->get_pstate_count(); i++)
156 speeds.push_back(get_iface()->get_pstate_speed(i));
159 englobing_zone_->get_cpu_vm_model()->create_cpu(vm, speeds)->set_core_count(vm->get_vm_impl()->get_core_amount());
161 if (get_iface()->get_pstate() != 0)
162 cpu->set_pstate(get_iface()->get_pstate());
166 /* Currently, a VM uses the network resource of its physical host */
167 vm->set_netpoint(get_iface()->get_netpoint());
174 void HostImpl::move_vm(VirtualMachineImpl* vm, HostImpl* destination)
176 xbt_assert(vm && destination);
178 vms_.erase(vm->get_name());
179 destination->vms_[vm->get_name()] = vm;
182 void HostImpl::destroy_vm(const std::string& name)
184 auto* vm = vms_[name];
189 VirtualMachineImpl* HostImpl::get_vm_by_name_or_null(const std::string& name) const
191 auto vm_it = vms_.find(name);
192 return vm_it == vms_.end() ? nullptr : vm_it->second;
195 std::vector<s4u::VirtualMachine*> HostImpl::get_vms() const
197 std::vector<s4u::VirtualMachine*> vms;
198 for (const auto& [_, vm] : vms_) {
199 vms.push_back(vm->get_iface());
204 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
206 auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
208 return disk->set_host(&piface_)->get_iface();
211 void HostImpl::add_disk(const s4u::Disk* disk)
213 disks_[disk->get_name()] = disk->get_impl();
216 void HostImpl::remove_disk(const std::string& name)
221 void HostImpl::seal()
227 get_iface()->get_cpu()->seal();
231 for (auto const& [_, disk] : disks_)
235 for (auto const& [_, vm] : vms_)
238 } // namespace simgrid::kernel::resource