Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2018. 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 *
17  *************/
18
19 namespace simgrid {
20 namespace surf {
21
22 /*********
23  * Model *
24  *********/
25
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()
30 {
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();
35
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());
38
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);
40     if (impact > 0)
41       ws_vm->get_impl()->action_->set_priority(1. / impact);
42     else
43       ws_vm->get_impl()->action_->set_priority(0.);
44   }
45 }
46
47 /* Helper function for executeParallelTask */
48 static inline double has_cost(double* array, int pos)
49 {
50   if (array)
51     return array[pos];
52   else
53     return -1.0;
54 }
55
56 kernel::resource::Action* HostModel::execute_parallel(int host_nb, s4u::Host** host_list, double* flops_amount,
57                                                       double* bytes_amount, double rate)
58 {
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)) {
65     int nb = 0;
66     double value = 0.0;
67
68     for (int i = 0; i < host_nb * host_nb; i++) {
69       if (has_cost(bytes_amount, i) > 0.0) {
70         nb++;
71         value = has_cost(bytes_amount, i);
72       }
73     }
74     if (nb == 1) {
75       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
76     } else if (nb == 0) {
77       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
78           "ptask model");
79     } else {
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");
82     }
83   } else {
84     xbt_die(
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");
89   }
90   delete[] host_list;
91   delete[] flops_amount;
92   delete[] bytes_amount;
93   return action;
94 }
95
96 /************
97  * Resource *
98  ************/
99 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
100 {
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;
104 }
105
106 HostImpl::~HostImpl()
107 {
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());
113
114     SIMIX_display_process_status();
115     THROWF(arg_error, 0, "%s", msg.c_str());
116   }
117   for (auto const& arg : actors_at_boot_)
118     delete arg.second;
119   actors_at_boot_.clear();
120 }
121
122 /** Re-starts all the actors that are marked as restartable.
123  *
124  * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
125  */
126 void HostImpl::turn_on()
127 {
128   for (auto const& elm : actors_at_boot_) {
129     kernel::actor::ProcessArg* arg = elm.second;
130     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
131     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
132                                                               arg->properties.get(), nullptr);
133     if (arg->kill_time >= 0)
134       simcall_process_set_kill_time(actor, arg->kill_time);
135     if (arg->auto_restart)
136       actor->auto_restart_ = arg->auto_restart;
137   }
138 }
139 /** Kill all actors hosted here */
140 void HostImpl::turn_off()
141 {
142   if (not process_list_.empty()) {
143     for (auto& actor : process_list_) {
144       XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
145                 actor.host_->get_cname(), SIMIX_process_self()->get_cname());
146       SIMIX_process_kill(&actor, SIMIX_process_self());
147     }
148   }
149   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
150   // Then get rid of the others.
151   auto elm = actors_at_boot_.begin();
152   while (elm != actors_at_boot_.end()) {
153     if (not elm->second->auto_restart) {
154       delete elm->second;
155       actors_at_boot_.erase(elm);
156     } else
157       ++elm;
158   }
159 }
160
161 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
162 {
163   std::vector<s4u::ActorPtr> res;
164   for (auto& actor : process_list_)
165     res.push_back(actor.ciface());
166   return res;
167 }
168 int HostImpl::get_actor_count()
169 {
170   return process_list_.size();
171 }
172 std::vector<const char*> HostImpl::get_attached_storages()
173 {
174   std::vector<const char*> storages;
175   for (auto const& s : storage_)
176     if (s.second->getHost() == piface_->get_cname())
177       storages.push_back(s.second->piface_.get_cname());
178   return storages;
179 }
180
181 }
182 }