Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix wrench test, use s4u method to generate the good signal.
[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 {
22 namespace kernel {
23 namespace resource {
24
25 /*********
26  * Model *
27  *********/
28 /************
29  * Resource *
30  ************/
31 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
32 {
33   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", 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     std::string msg = "Shutting down host, but it's not empty:";
41     for (auto const& actor : actor_list_)
42       msg += "\n\t" + std::string(actor.get_name());
43
44     EngineImpl::get_instance()->display_all_actor_status();
45     xbt_die("%s", msg.c_str());
46   }
47   for (auto const& arg : actors_at_boot_)
48     delete arg;
49   actors_at_boot_.clear();
50
51   for (auto const& d : disks_)
52     d.second->destroy();
53
54   for (auto const& vm : vms_)
55     vm.second->vm_destroy();
56 }
57
58 /** @brief Fire the required callbacks and destroy the object
59  *
60  * Don't delete directly a Host, call h->destroy() instead.
61  */
62 void HostImpl::destroy()
63 {
64   s4u::Host::on_destruction(*this->get_iface());
65   delete this;
66 }
67
68 /** Re-starts all the actors that are marked as restartable.
69  *
70  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
71  */
72 void HostImpl::turn_on() const
73 {
74   for (auto const& arg : actors_at_boot_) {
75     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
76     actor::ActorImplPtr actor = actor::ActorImpl::create(arg);
77   }
78 }
79
80 /** Kill all actors hosted here */
81 void HostImpl::turn_off(const actor::ActorImpl* issuer)
82 {
83   /* turn_off VMs running on host */
84   for (const auto& kv : vms_) {
85     // call s4u functions to generate the good on_state_change signal, maybe one day this wont be necessary
86     kv.second->get_iface()->shutdown();
87     kv.second->get_iface()->turn_off();
88   }
89   for (auto& actor : actor_list_) {
90     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
91               actor.get_host()->get_cname(), issuer->get_cname());
92     issuer->kill(&actor);
93   }
94   for (const auto& activity : EngineImpl::get_instance()->get_maestro()->activities_) {
95     auto* exec = dynamic_cast<activity::ExecImpl*>(activity.get());
96     if (exec != nullptr) {
97       auto hosts = exec->get_hosts();
98       if (std::find(hosts.begin(), hosts.end(), &piface_) != hosts.end()) {
99         exec->cancel();
100         exec->set_state(activity::State::FAILED);
101       }
102     }
103   }
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)
108       return false;
109     delete arg;
110     return true;
111   });
112   actors_at_boot_.erase(elm, end(actors_at_boot_));
113 }
114
115 HostImpl* HostImpl::set_englobing_zone(routing::NetZoneImpl* englobing_zone)
116 {
117   englobing_zone_ = englobing_zone;
118   return this;
119 }
120
121 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
122 {
123   std::vector<s4u::ActorPtr> res;
124   for (auto& actor : actor_list_)
125     res.emplace_back(actor.get_ciface());
126   return res;
127 }
128 size_t HostImpl::get_actor_count() const
129 {
130   return actor_list_.size();
131 }
132
133 std::vector<s4u::Disk*> HostImpl::get_disks() const
134 {
135   std::vector<s4u::Disk*> disks;
136   for (auto const& d : disks_)
137     disks.push_back(d.second->get_iface());
138   return disks;
139 }
140
141 s4u::VirtualMachine* HostImpl::create_vm(const std::string& name, int core_amount, size_t ramsize)
142 {
143   auto* vm   = new s4u::VirtualMachine(name, get_iface(), core_amount, ramsize);
144   vms_[name] = vm->get_vm_impl();
145
146   // Create a VCPU for this VM
147   std::vector<double> speeds;
148   for (unsigned long i = 0; i < get_iface()->get_pstate_count(); i++)
149     speeds.push_back(get_iface()->get_pstate_speed(i));
150
151   auto* cpu = englobing_zone_->get_cpu_vm_model()->create_cpu(vm, speeds)->set_core_count(core_amount);
152
153   if (get_iface()->get_pstate() != 0)
154     cpu->set_pstate(get_iface()->get_pstate());
155
156   cpu->seal();
157
158   /* Currently, a VM uses the network resource of its physical host */
159   vm->set_netpoint(get_iface()->get_netpoint());
160
161   vm->seal();
162
163   return vm;
164 }
165
166 void HostImpl::move_vm(VirtualMachineImpl* vm, HostImpl* destination)
167 {
168   xbt_assert(vm && destination);
169
170   vms_.erase(vm->get_name());
171   destination->vms_[vm->get_name()] = vm;
172 }
173
174 void HostImpl::destroy_vm(const std::string& name)
175 {
176   auto* vm = vms_[name];
177   vms_.erase(name);
178   vm->vm_destroy();
179 }
180
181 VirtualMachineImpl* HostImpl::get_vm_by_name_or_null(const std::string& name) const
182 {
183   auto vm_it = vms_.find(name);
184   if (vm_it != vms_.end())
185     return vm_it->second;
186   return nullptr;
187 }
188
189 std::vector<s4u::VirtualMachine*> HostImpl::get_vms() const
190 {
191   std::vector<s4u::VirtualMachine*> vms;
192   for (const auto& kv : vms_) {
193     vms.push_back(kv.second->get_iface());
194   }
195   return vms;
196 }
197
198 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
199 {
200   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
201                                                                                           write_bandwidth);
202   return disk->set_host(&piface_)->get_iface();
203 }
204
205 void HostImpl::add_disk(const s4u::Disk* disk)
206 {
207   disks_[disk->get_name()] = disk->get_impl();
208 }
209
210 void HostImpl::remove_disk(const std::string& name)
211 {
212   disks_.erase(name);
213 }
214
215 void HostImpl::seal()
216 {
217   if (sealed_) {
218     return;
219   }
220   // seals host's CPU
221   get_iface()->get_cpu()->seal();
222   sealed_ = true;
223
224   /* seal its disks */
225   for (auto const& disk : disks_)
226     disk.second->seal();
227
228   /* seal its VMs */
229   for (auto const& vm : vms_)
230     vm.second->seal();
231 }
232 } // namespace resource
233 } // namespace kernel
234 } // namespace simgrid