Logo AND Algorithmique Numérique Distribuée

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