Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reorganize VM code
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2021. 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 surf {
23
24 /*********
25  * Model *
26  *********/
27 /************
28  * Resource *
29  ************/
30 HostImpl::HostImpl(const std::string& name, s4u::Host* piface) : piface_(this), name_(name)
31 {
32   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
33   s4u::Engine::get_instance()->host_register(name_, piface);
34 }
35
36 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
37 {
38   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
39   s4u::Engine::get_instance()->host_register(name_, &piface_);
40 }
41
42 HostImpl::~HostImpl()
43 {
44   /* All actors should be gone when the host is turned off (by the end of the simulation). */
45   if (not actor_list_.empty()) {
46     std::string msg = "Shutting down host, but it's not empty:";
47     for (auto const& actor : actor_list_)
48       msg += "\n\t" + std::string(actor.get_name());
49
50     kernel::EngineImpl::get_instance()->display_all_actor_status();
51     xbt_die("%s", msg.c_str());
52   }
53   for (auto const& arg : actors_at_boot_)
54     delete arg;
55   actors_at_boot_.clear();
56
57   for (auto const& d : disks_)
58     d.second->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   s4u::Engine::get_instance()->host_unregister(std::string(name_));
69   delete this;
70 }
71
72 /** Re-starts all the actors that are marked as restartable.
73  *
74  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
75  */
76 void HostImpl::turn_on() const
77 {
78   for (auto const& arg : actors_at_boot_) {
79     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
80     simgrid::kernel::actor::ActorImplPtr actor =
81         simgrid::kernel::actor::ActorImpl::create(arg->name, arg->code, nullptr, arg->host, nullptr);
82     actor->set_properties(arg->properties);
83     if (arg->on_exit)
84       *actor->on_exit = *arg->on_exit;
85     if (arg->kill_time >= 0)
86       actor->set_kill_time(arg->kill_time);
87     if (arg->auto_restart)
88       actor->set_auto_restart(arg->auto_restart);
89     if (arg->daemon_)
90       actor->daemonize();
91   }
92 }
93
94 /** Kill all actors hosted here */
95 void HostImpl::turn_off(const kernel::actor::ActorImpl* issuer)
96 {
97   for (auto& actor : actor_list_) {
98     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
99               actor.get_host()->get_cname(), issuer->get_cname());
100     issuer->kill(&actor);
101   }
102   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
103   // Then get rid of the others.
104   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const kernel::actor::ProcessArg* arg) {
105     if (arg->auto_restart)
106       return false;
107     delete arg;
108     return true;
109   });
110   actors_at_boot_.erase(elm, end(actors_at_boot_));
111 }
112
113 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
114 {
115   std::vector<s4u::ActorPtr> res;
116   for (auto& actor : actor_list_)
117     res.emplace_back(actor.get_ciface());
118   return res;
119 }
120 size_t HostImpl::get_actor_count() const
121 {
122   return actor_list_.size();
123 }
124
125 std::vector<s4u::Disk*> HostImpl::get_disks() const
126 {
127   std::vector<s4u::Disk*> disks;
128   for (auto const& d : disks_)
129     disks.push_back(d.second->get_iface());
130   return disks;
131 }
132
133 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
134 {
135   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
136                                                                                           write_bandwidth);
137   return disk->set_host(&piface_)->get_iface();
138 }
139
140 void HostImpl::add_disk(const s4u::Disk* disk)
141 {
142   disks_[disk->get_name()] = disk->get_impl();
143 }
144
145 void HostImpl::remove_disk(const std::string& name)
146 {
147   disks_.erase(name);
148 }
149
150 void HostImpl::seal()
151 {
152   if (sealed_) {
153     return;
154   }
155   // seals host's CPU
156   get_iface()->get_cpu()->seal();
157   sealed_ = true;
158
159   /* seal its disks */
160   for (auto const& disk : disks_)
161     disk.second->seal();
162 }
163 } // namespace surf
164 } // namespace simgrid