Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'fix-multi-smpi-buffer-double-free' into 'master'
[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) && (has_cost(flops_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   return action;
91 }
92
93 /************
94  * Resource *
95  ************/
96 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
97 {
98   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
99   delete piface_->pimpl_;
100   piface_->pimpl_ = this;
101 }
102
103 HostImpl::~HostImpl()
104 {
105   /* All processes should be gone when the host is turned off (by the end of the simulation). */
106   if (not process_list_.empty()) {
107     std::string msg = std::string("Shutting down host, but it's not empty:");
108     for (auto const& process : process_list_)
109       msg += "\n\t" + std::string(process.get_name());
110
111     SIMIX_display_process_status();
112     THROWF(arg_error, 0, "%s", msg.c_str());
113   }
114   for (auto const& arg : actors_at_boot_)
115     delete arg;
116   actors_at_boot_.clear();
117 }
118
119 /** Re-starts all the actors that are marked as restartable.
120  *
121  * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
122  */
123 void HostImpl::turn_on()
124 {
125   for (auto const& arg : actors_at_boot_) {
126     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
127     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
128                                                               arg->properties.get(), nullptr);
129     if (arg->kill_time >= 0)
130       simcall_process_set_kill_time(actor, arg->kill_time);
131     if (arg->auto_restart)
132       actor->auto_restart_ = arg->auto_restart;
133     if (arg->daemon_)
134       actor->daemonize();
135   }
136 }
137
138 /** Kill all actors hosted here */
139 void HostImpl::turn_off()
140 {
141   if (not process_list_.empty()) {
142     for (auto& actor : process_list_) {
143       XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
144                 actor.host_->get_cname(), SIMIX_process_self()->get_cname());
145       SIMIX_process_kill(&actor, SIMIX_process_self());
146     }
147   }
148   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
149   // Then get rid of the others.
150   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](kernel::actor::ProcessArg* arg) {
151     if (arg->auto_restart)
152       return false;
153     delete arg;
154     return true;
155   });
156   actors_at_boot_.erase(elm, end(actors_at_boot_));
157 }
158
159 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
160 {
161   std::vector<s4u::ActorPtr> res;
162   for (auto& actor : process_list_)
163     res.push_back(actor.ciface());
164   return res;
165 }
166 int HostImpl::get_actor_count()
167 {
168   return process_list_.size();
169 }
170 std::vector<const char*> HostImpl::get_attached_storages()
171 {
172   std::vector<const char*> storages;
173   for (auto const& s : storage_)
174     if (s.second->getHost() == piface_->get_cname())
175       storages.push_back(s.second->piface_.get_cname());
176   return storages;
177 }
178
179 }
180 }