1 /* Copyright (c) 2013-2019. 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;
25 /* Helper function for executeParallelTask */
26 static inline double has_cost(const double* array, size_t pos)
34 kernel::resource::Action* HostModel::execute_parallel(const std::vector<s4u::Host*>& host_list,
35 const double* flops_amount, const double* bytes_amount,
38 kernel::resource::Action* action = nullptr;
39 if ((host_list.size() == 1) && (has_cost(bytes_amount, 0) <= 0) && (has_cost(flops_amount, 0) > 0)) {
40 action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
41 } else if ((host_list.size() == 1) && (has_cost(flops_amount, 0) <= 0)) {
42 action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
43 } else if ((host_list.size() == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
47 for (size_t i = 0; i < host_list.size() * host_list.size(); i++) {
48 if (has_cost(bytes_amount, i) > 0.0) {
50 value = has_cost(bytes_amount, i);
54 action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
56 xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
59 xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
60 "using the ptask model");
64 "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
65 " - execution with one host only and no communication\n"
66 " - Self-comms with one host only\n"
67 " - Communications with two hosts and no computation");
75 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
77 /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
78 delete piface_->pimpl_;
79 piface_->pimpl_ = this;
84 /* All processes should be gone when the host is turned off (by the end of the simulation). */
85 if (not process_list_.empty()) {
86 std::string msg = std::string("Shutting down host, but it's not empty:");
87 for (auto const& process : process_list_)
88 msg += "\n\t" + std::string(process.get_name());
90 SIMIX_display_process_status();
91 THROWF(arg_error, 0, "%s", msg.c_str());
93 for (auto const& arg : actors_at_boot_)
95 actors_at_boot_.clear();
98 /** Re-starts all the actors that are marked as restartable.
100 * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
102 void HostImpl::turn_on()
104 for (auto const& arg : actors_at_boot_) {
105 XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
106 simgrid::kernel::actor::ActorImplPtr actor = simgrid::kernel::actor::ActorImpl::create(
107 arg->name.c_str(), arg->code, nullptr, arg->host, arg->properties.get(), nullptr);
108 if (arg->kill_time >= 0)
109 actor->set_kill_time(arg->kill_time);
110 if (arg->auto_restart)
111 actor->set_auto_restart(arg->auto_restart);
117 /** Kill all actors hosted here */
118 void HostImpl::turn_off()
120 if (not process_list_.empty()) {
121 for (auto& actor : process_list_) {
122 XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
123 actor.get_host()->get_cname(), SIMIX_process_self()->get_cname());
124 SIMIX_process_self()->kill(&actor);
127 // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
128 // Then get rid of the others.
129 auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](kernel::actor::ProcessArg* arg) {
130 if (arg->auto_restart)
135 actors_at_boot_.erase(elm, end(actors_at_boot_));
138 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
140 std::vector<s4u::ActorPtr> res;
141 for (auto& actor : process_list_)
142 res.push_back(actor.ciface());
145 size_t HostImpl::get_actor_count()
147 return process_list_.size();
149 std::vector<const char*> HostImpl::get_attached_storages()
151 std::vector<const char*> storages;
152 for (auto const& s : storage_)
153 if (s.second->getHost() == piface_->get_cname())
154 storages.push_back(s.second->piface_.get_cname());