Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] fix some bugs and smells related to disk addition
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2019. 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 *t
17  *************/
18
19 namespace simgrid {
20 namespace surf {
21
22 /*********
23  * Model *
24  *********/
25 /* Helper function for executeParallelTask */
26 static inline double has_cost(const double* array, size_t pos)
27 {
28   if (array)
29     return array[pos];
30   return -1.0;
31 }
32
33 kernel::resource::Action* HostModel::execute_parallel(const std::vector<s4u::Host*>& host_list,
34                                                       const double* flops_amount, const double* bytes_amount,
35                                                       double rate)
36 {
37   kernel::resource::Action* action = nullptr;
38   if ((host_list.size() == 1) && (has_cost(bytes_amount, 0) <= 0) && (has_cost(flops_amount, 0) > 0)) {
39     action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
40   } else if ((host_list.size() == 1) && (has_cost(flops_amount, 0) <= 0)) {
41     action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
42   } else if ((host_list.size() == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
43     int nb = 0;
44     double value = 0.0;
45
46     for (size_t i = 0; i < host_list.size() * host_list.size(); i++) {
47       if (has_cost(bytes_amount, i) > 0.0) {
48         nb++;
49         value = has_cost(bytes_amount, i);
50       }
51     }
52     if (nb == 1) {
53       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
54     } else if (nb == 0) {
55       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
56           "ptask model");
57     } else {
58       xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
59           "using the ptask model");
60     }
61   } else {
62     xbt_die(
63         "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
64         " - execution with one host only and no communication\n"
65         " - Self-comms with one host only\n"
66         " - Communications with two hosts and no computation");
67   }
68   return action;
69 }
70
71 /************
72  * Resource *
73  ************/
74 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
75 {
76   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
77   delete piface_->pimpl_;
78   piface_->pimpl_ = this;
79 }
80
81 HostImpl::~HostImpl()
82 {
83   /* All processes should be gone when the host is turned off (by the end of the simulation). */
84   if (not process_list_.empty()) {
85     std::string msg = std::string("Shutting down host, but it's not empty:");
86     for (auto const& process : process_list_)
87       msg += "\n\t" + std::string(process.get_name());
88
89     SIMIX_display_process_status();
90     xbt_die("%s", msg.c_str());
91   }
92   for (auto const& arg : actors_at_boot_)
93     delete arg;
94   actors_at_boot_.clear();
95
96   for (auto const& d : disks_)
97     d->destroy();
98 }
99
100 /** Re-starts all the actors that are marked as restartable.
101  *
102  * Weird things will happen if you turn on a host that is already on. S4U is fool-proof, not this.
103  */
104 void HostImpl::turn_on()
105 {
106   for (auto const& arg : actors_at_boot_) {
107     XBT_DEBUG("Booting Actor %s(%s) right now", arg->name.c_str(), arg->host->get_cname());
108     simgrid::kernel::actor::ActorImplPtr actor = simgrid::kernel::actor::ActorImpl::create(
109         arg->name, arg->code, nullptr, arg->host, arg->properties.get(), nullptr);
110     if (arg->on_exit)
111       *actor->on_exit = *arg->on_exit;
112     if (arg->kill_time >= 0)
113       actor->set_kill_time(arg->kill_time);
114     if (arg->auto_restart)
115       actor->set_auto_restart(arg->auto_restart);
116     if (arg->daemon_)
117       actor->daemonize();
118   }
119 }
120
121 /** Kill all actors hosted here */
122 void HostImpl::turn_off()
123 {
124   if (not process_list_.empty()) {
125     for (auto& actor : process_list_) {
126       XBT_DEBUG("Killing Actor %s@%s on behalf of %s which turned off that host.", actor.get_cname(),
127                 actor.get_host()->get_cname(), SIMIX_process_self()->get_cname());
128       SIMIX_process_self()->kill(&actor);
129     }
130   }
131   // When a host is turned off, we want to keep only the actors that should restart for when it will boot again.
132   // Then get rid of the others.
133   auto elm = remove_if(begin(actors_at_boot_), end(actors_at_boot_), [](kernel::actor::ProcessArg* arg) {
134     if (arg->auto_restart)
135       return false;
136     delete arg;
137     return true;
138   });
139   actors_at_boot_.erase(elm, end(actors_at_boot_));
140 }
141
142 std::vector<s4u::ActorPtr> HostImpl::get_all_actors()
143 {
144   std::vector<s4u::ActorPtr> res;
145   for (auto& actor : process_list_)
146     res.push_back(actor.ciface());
147   return res;
148 }
149 size_t HostImpl::get_actor_count()
150 {
151   return process_list_.size();
152 }
153
154 std::vector<s4u::Disk*> HostImpl::get_disks()
155 {
156   std::vector<s4u::Disk*> disks;
157   for (auto const& d : disks_)
158     disks.push_back(&d->piface_);
159   return disks;
160 }
161
162 void HostImpl::add_disk(s4u::Disk* disk)
163 {
164   disks_.push_back(disk->get_impl());
165 }
166
167 void HostImpl::remove_disk(const std::string& disk_name)
168 {
169   auto position = disks_.begin();
170   for (auto const& d : disks_) {
171     if (d->get_name() == disk_name) {
172       disks_.erase(position);
173       break;
174     }
175     position++;
176   }
177 }
178
179 std::vector<const char*> HostImpl::get_attached_storages()
180 {
181   std::vector<const char*> storages;
182   for (auto const& s : storage_)
183     if (s.second->get_host() == piface_->get_cname())
184       storages.push_back(s.second->piface_.get_cname());
185   return storages;
186 }
187
188 } // namespace surf
189 } // namespace simgrid