1 /* Copyright (c) 2013-2018. 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;
26 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the constraint (capacity) of the VM in the
27 * PM layer. If the VM does not have any active task, the dummy CPU action must be deactivated, so that the VM does not
28 * get any CPU share in the PM layer. */
29 void HostModel::ignore_empty_vm_in_pm_LMM()
31 /* iterate for all virtual machines */
32 for (s4u::VirtualMachine* const& ws_vm : vm::VirtualMachineImpl::allVms_) {
33 Cpu* cpu = ws_vm->pimpl_cpu;
34 int active_tasks = cpu->get_constraint()->get_variable_amount();
36 /* The impact of the VM over its PM is the min between its vCPU amount and the amount of tasks it contains */
37 int impact = std::min(active_tasks, ws_vm->get_impl()->get_core_amount());
39 XBT_DEBUG("set the weight of the dummy CPU action of VM%p on PM to %d (#tasks: %d)", ws_vm, impact, active_tasks);
41 ws_vm->get_impl()->action_->set_priority(1. / impact);
43 ws_vm->get_impl()->action_->set_priority(0.);
47 /* Helper function for executeParallelTask */
48 static inline double has_cost(double* array, int pos)
56 kernel::resource::Action* HostModel::execute_parallel(int host_nb, s4u::Host** host_list, double* flops_amount,
57 double* bytes_amount, double rate)
59 kernel::resource::Action* action = nullptr;
60 if ((host_nb == 1) && (has_cost(bytes_amount, 0) <= 0)) {
61 action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
62 } else if ((host_nb == 1) && (has_cost(flops_amount, 0) <= 0)) {
63 action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
64 } else if ((host_nb == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
68 for (int i = 0; i < host_nb * host_nb; i++) {
69 if (has_cost(bytes_amount, i) > 0.0) {
71 value = has_cost(bytes_amount, i);
75 action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
77 xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
80 xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
81 "using the ptask model");
85 "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
86 " - execution with one host only and no communication\n"
87 " - Self-comms with one host only\n"
88 " - Communications with two hosts and no computation");
91 delete[] flops_amount;
92 delete[] bytes_amount;
99 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
101 /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
102 delete piface_->pimpl_;
103 piface_->pimpl_ = this;
106 HostImpl::~HostImpl()
108 /* All processes should be gone when the host is turned off (by the end of the simulation). */
109 if (not process_list_.empty()) {
110 std::string msg = std::string("Shutting down host, but it's not empty:");
111 for (auto const& process : process_list_)
112 msg += "\n\t" + std::string(process.get_name());
114 SIMIX_display_process_status();
115 THROWF(arg_error, 0, "%s", msg.c_str());
117 for (auto const& arg : actors_at_boot_)
119 actors_at_boot_.clear();
122 /** Re-starts all the actors that are marked as restartable.
124 * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
126 void HostImpl::turn_on()
128 for (auto const& arg : actors_at_boot_) {
129 XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
130 smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
131 arg->properties.get(), nullptr);
132 if (arg->kill_time >= 0)
133 simcall_process_set_kill_time(actor, arg->kill_time);
134 if (arg->auto_restart)
135 actor->auto_restart_ = arg->auto_restart;
141 /** Kill all actors hosted here */
142 void HostImpl::turn_off()
144 if (not process_list_.empty()) {
145 for (auto& actor : process_list_) {
146 XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
147 actor.host_->get_cname(), SIMIX_process_self()->get_cname());
148 SIMIX_process_kill(&actor, SIMIX_process_self());
151 // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
152 // Then get rid of the others.
153 auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](kernel::actor::ProcessArg* arg) {
154 if (arg->auto_restart)
159 actors_at_boot_.erase(elm, end(actors_at_boot_));
162 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
164 std::vector<s4u::ActorPtr> res;
165 for (auto& actor : process_list_)
166 res.push_back(actor.ciface());
169 int HostImpl::get_actor_count()
171 return process_list_.size();
173 std::vector<const char*> HostImpl::get_attached_storages()
175 std::vector<const char*> storages;
176 for (auto const& s : storage_)
177 if (s.second->getHost() == piface_->get_cname())
178 storages.push_back(s.second->piface_.get_cname());