Logo AND Algorithmique Numérique Distribuée

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