Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2020. 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(surf_host, surf, "Logging specific to the SURF host module");
12
13 simgrid::surf::HostModel *surf_host_model = nullptr;
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(s4u::Host* host) : piface_(host)
29 {
30   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
31   delete piface_->pimpl_;
32   piface_->pimpl_ = this;
33 }
34
35 HostImpl::~HostImpl()
36 {
37   /* All actors should be gone when the host is turned off (by the end of the simulation). */
38   if (not actor_list_.empty()) {
39     std::string msg = std::string("Shutting down host, but it's not empty:");
40     for (auto const& actor : actor_list_)
41       msg += "\n\t" + std::string(actor.get_name());
42
43     simix_global->display_all_actor_status();
44     xbt_die("%s", msg.c_str());
45   }
46   for (auto const& arg : actors_at_boot_)
47     delete arg;
48   actors_at_boot_.clear();
49
50   for (auto const& d : disks_)
51     d->destroy();
52 }
53
54 /** Re-starts all the actors that are marked as restartable.
55  *
56  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
57  */
58 void HostImpl::turn_on()
59 {
60   for (auto const& arg : actors_at_boot_) {
61     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
62     simgrid::kernel::actor::ActorImplPtr actor = simgrid::kernel::actor::ActorImpl::create(
63         arg->name, arg->code, nullptr, arg->host, arg->properties.get(), nullptr);
64     if (arg->on_exit)
65       *actor->on_exit = *arg->on_exit;
66     if (arg->kill_time >= 0)
67       actor->set_kill_time(arg->kill_time);
68     if (arg->auto_restart)
69       actor->set_auto_restart(arg->auto_restart);
70     if (arg->daemon_)
71       actor->daemonize();
72   }
73 }
74
75 /** Kill all actors hosted here */
76 void HostImpl::turn_off(kernel::actor::ActorImpl* issuer)
77 {
78   for (auto& actor : actor_list_) {
79     XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
80               actor.get_host()->get_cname(), issuer->get_cname());
81     issuer->kill(&actor);
82   }
83   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
84   // Then get rid of the others.
85   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](const kernel::actor::ProcessArg* arg) {
86     if (arg->auto_restart)
87       return false;
88     delete arg;
89     return true;
90   });
91   actors_at_boot_.erase(elm, end(actors_at_boot_));
92 }
93
94 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
95 {
96   std::vector<s4u::ActorPtr> res;
97   for (auto& actor : actor_list_)
98     res.push_back(actor.ciface());
99   return res;
100 }
101 size_t HostImpl::get_actor_count()
102 {
103   return actor_list_.size();
104 }
105
106 std::vector<s4u::Disk*> HostImpl::get_disks()
107 {
108   std::vector<s4u::Disk*> disks;
109   for (auto const& d : disks_)
110     disks.push_back(d->get_iface());
111   return disks;
112 }
113
114 void HostImpl::add_disk(const s4u::Disk* disk)
115 {
116   disks_.push_back(disk->get_impl());
117 }
118
119 void HostImpl::remove_disk(const std::string& disk_name)
120 {
121   auto position = disks_.begin();
122   for (auto const& d : disks_) {
123     if (d->get_name() == disk_name) {
124       disks_.erase(position);
125       break;
126     }
127     position++;
128   }
129 }
130
131 std::vector<const char*> HostImpl::get_attached_storages()
132 {
133   std::vector<const char*> storages;
134   for (auto const& s : storage_)
135     if (s.second->get_host() == piface_->get_cname())
136       storages.push_back(s.second->get_iface()->get_cname());
137   return storages;
138 }
139
140 } // namespace surf
141 } // namespace simgrid