Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not allocate/free properties
[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 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include "src/simix/smx_private.hpp"
11
12 #include <string>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_host, ker_resource, "Host resources agregate CPU, networking and I/O features");
15
16 /*************
17  * Callbacks *t
18  *************/
19
20 namespace simgrid {
21 namespace surf {
22
23 /*********
24  * Model *
25  *********/
26 /************
27  * Resource *
28  ************/
29 HostImpl::HostImpl(const std::string& name, s4u::Host* piface) : 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   s4u::Engine::get_instance()->host_register(name_, piface);
33 }
34
35 HostImpl::HostImpl(const std::string& name) : piface_(this), name_(name)
36 {
37   xbt_assert(s4u::Host::by_name_or_null(name_) == nullptr, "Refusing to create a second host named '%s'.", get_cname());
38   s4u::Engine::get_instance()->host_register(name_, &piface_);
39 }
40
41 HostImpl::~HostImpl()
42 {
43   /* All actors should be gone when the host is turned off (by the end of the simulation). */
44   if (not actor_list_.empty()) {
45     std::string msg = "Shutting down host, but it's not empty:";
46     for (auto const& actor : actor_list_)
47       msg += "\n\t" + std::string(actor.get_name());
48
49     simix_global->display_all_actor_status();
50     xbt_die("%s", msg.c_str());
51   }
52   for (auto const& arg : actors_at_boot_)
53     delete arg;
54   actors_at_boot_.clear();
55
56   for (auto const& d : disks_)
57     d->destroy();
58 }
59
60 /** @brief Fire the required callbacks and destroy the object
61  *
62  * Don't delete directly a Host, call h->destroy() instead.
63  */
64 void HostImpl::destroy()
65 {
66   s4u::Host::on_destruction(*this->get_iface());
67   s4u::Engine::get_instance()->host_unregister(std::string(name_));
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     simgrid::kernel::actor::ActorImplPtr actor =
80         simgrid::kernel::actor::ActorImpl::create(arg->name, arg->code, nullptr, arg->host, arg->properties, nullptr);
81     if (arg->on_exit)
82       *actor->on_exit = *arg->on_exit;
83     if (arg->kill_time >= 0)
84       actor->set_kill_time(arg->kill_time);
85     if (arg->auto_restart)
86       actor->set_auto_restart(arg->auto_restart);
87     if (arg->daemon_)
88       actor->daemonize();
89   }
90 }
91
92 /** Kill all actors hosted here */
93 void HostImpl::turn_off(const kernel::actor::ActorImpl* issuer)
94 {
95   for (auto& actor : actor_list_) {
96     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
97               actor.get_host()->get_cname(), issuer->get_cname());
98     issuer->kill(&actor);
99   }
100   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
101   // Then get rid of the others.
102   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const kernel::actor::ProcessArg* arg) {
103     if (arg->auto_restart)
104       return false;
105     delete arg;
106     return true;
107   });
108   actors_at_boot_.erase(elm, end(actors_at_boot_));
109 }
110
111 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
112 {
113   std::vector<s4u::ActorPtr> res;
114   for (auto& actor : actor_list_)
115     res.emplace_back(actor.get_ciface());
116   return res;
117 }
118 size_t HostImpl::get_actor_count() const
119 {
120   return actor_list_.size();
121 }
122
123 std::vector<s4u::Disk*> HostImpl::get_disks() const
124 {
125   std::vector<s4u::Disk*> disks;
126   for (auto const& d : disks_)
127     disks.push_back(d->get_iface());
128   return disks;
129 }
130
131 HostImpl* HostImpl::set_disks(const std::vector<kernel::resource::DiskImpl*>& disks)
132 {
133   disks_ = disks;
134   for (auto d : disks_)
135     d->set_host(&piface_);
136   return this;
137 }
138
139 s4u::Disk* HostImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
140 {
141   auto disk = piface_.get_netpoint()->get_englobing_zone()->get_disk_model()->create_disk(name, read_bandwidth,
142                                                                                           write_bandwidth);
143   return disk->set_host(&piface_)->get_iface();
144 }
145
146 void HostImpl::add_disk(const s4u::Disk* disk)
147 {
148   disks_.push_back(disk->get_impl());
149 }
150
151 void HostImpl::remove_disk(const std::string& disk_name)
152 {
153   auto position = disks_.begin();
154   for (auto const& d : disks_) {
155     if (d->get_name() == disk_name) {
156       disks_.erase(position);
157       break;
158     }
159     position++;
160   }
161 }
162
163 void HostImpl::seal()
164 {
165   if (sealed_) {
166     return;
167   }
168   // seals host's CPU
169   get_iface()->pimpl_cpu->seal();
170   sealed_ = true;
171 }
172 } // namespace surf
173 } // namespace simgrid