Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/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 HostImpl::~HostImpl()
106 {
107   /* All processes should be gone when the host is turned off (by the end of the simulation). */
108   if (not process_list_.empty()) {
109     std::string msg = std::string("Shutting down host, but it's not empty:");
110     for (auto const& process : process_list_)
111       msg += "\n\t" + std::string(process.get_name());
112
113     SIMIX_display_process_status();
114     THROWF(arg_error, 0, "%s", msg.c_str());
115   }
116   for (auto const& arg : auto_restart_processes_)
117     delete arg;
118   auto_restart_processes_.clear();
119   for (auto const& arg : boot_processes_)
120     delete arg;
121   boot_processes_.clear();
122 }
123
124 /** Re-starts all the actors that are marked as restartable.
125  *
126  * Weird things will happen if you turn on an host that is already on. S4U is fool-proof, not this.
127  */
128 void HostImpl::turn_on()
129 {
130   for (auto const& arg : boot_processes_) {
131     XBT_DEBUG("Booting Process %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
132     smx_actor_t actor = simix_global->create_process_function(arg->name.c_str(), arg->code, nullptr, arg->host,
133                                                               arg->properties.get(), nullptr);
134     if (arg->kill_time >= 0)
135       simcall_process_set_kill_time(actor, arg->kill_time);
136     if (arg->auto_restart)
137       actor->auto_restart_ = arg->auto_restart;
138   }
139 }
140 /** Kill all actors hosted here */
141 void HostImpl::turn_off()
142 {
143   if (not process_list_.empty()) {
144     for (auto& actor : process_list_) {
145       SIMIX_process_kill(&actor, SIMIX_process_self());
146       XBT_DEBUG("Killing %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     }
149   }
150 }
151
152 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
153 {
154   std::vector<s4u::ActorPtr> res;
155   for (auto& actor : process_list_)
156     res.push_back(actor.ciface());
157   return res;
158 }
159 int HostImpl::get_actor_count()
160 {
161   return process_list_.size();
162 }
163 std::vector<const char*> HostImpl::get_attached_storages()
164 {
165   std::vector<const char*> storages;
166   for (auto const& s : storage_)
167     if (s.second->getHost() == piface_->get_cname())
168       storages.push_back(s.second->piface_.get_cname());
169   return storages;
170 }
171
172 }
173 }