1 /* Copyright (c) 2013-2020. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/plugins/vm/VirtualMachineImpl.hpp"
7 #include "src/simix/smx_private.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
13 simgrid::surf::HostModel *surf_host_model = nullptr;
28 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
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;
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());
43 simix_global->display_all_actor_status();
44 xbt_die("%s", msg.c_str());
46 for (auto const& arg : actors_at_boot_)
48 actors_at_boot_.clear();
50 for (auto const& d : disks_)
54 /** Re-starts all the actors that are marked as restartable.
56 * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
58 void HostImpl::turn_on() const
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);
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);
75 /** Kill all actors hosted here */
76 void HostImpl::turn_off(const kernel::actor::ActorImpl* issuer)
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());
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)
91 actors_at_boot_.erase(elm, end(actors_at_boot_));
94 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
96 std::vector<s4u::ActorPtr> res;
97 for (auto& actor : actor_list_)
98 res.push_back(actor.ciface());
101 size_t HostImpl::get_actor_count() const
103 return actor_list_.size();
106 std::vector<s4u::Disk*> HostImpl::get_disks() const
108 std::vector<s4u::Disk*> disks;
109 for (auto const& d : disks_)
110 disks.push_back(d->get_iface());
114 void HostImpl::set_disks(const std::vector<kernel::resource::DiskImpl*>& disks, s4u::Host* host)
116 disks_ = std::move(disks);
117 for (auto d : disks_)
121 void HostImpl::add_disk(const s4u::Disk* disk)
123 disks_.push_back(disk->get_impl());
126 void HostImpl::remove_disk(const std::string& disk_name)
128 auto position = disks_.begin();
129 for (auto const& d : disks_) {
130 if (d->get_name() == disk_name) {
131 disks_.erase(position);
138 std::vector<const char*> HostImpl::get_attached_storages()
140 std::vector<const char*> storages;
141 for (auto const& s : storage_)
142 if (s.second->get_host() == piface_->get_cname())
143 storages.push_back(s.second->get_iface()->get_cname());
146 std::unordered_map<std::string, s4u::Storage*>* HostImpl::get_mounted_storages()
148 std::unordered_map<std::string, s4u::Storage*>* mounts = new std::unordered_map<std::string, s4u::Storage*>();
149 for (auto const& m : storage_) {
150 mounts->insert({m.first, m.second->get_iface()});
155 } // namespace simgrid