Logo AND Algorithmique Numérique Distribuée

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