Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make expression clear.
[simgrid.git] / src / s4u / s4u_Link.cpp
1 /* Copyright (c) 2013-2020. 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 <algorithm>
7
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Link.hpp"
10 #include "simgrid/sg_config.hpp"
11 #include "simgrid/simix.hpp"
12 #include "src/kernel/lmm/maxmin.hpp"
13 #include "src/surf/network_interface.hpp"
14 #include "src/surf/network_wifi.hpp"
15 #include "xbt/log.h"
16
17 namespace simgrid {
18
19 template class xbt::Extendable<s4u::Link>;
20
21 namespace s4u {
22
23 xbt::signal<void(Link&)> Link::on_creation;
24 xbt::signal<void(Link const&)> Link::on_destruction;
25 xbt::signal<void(Link const&)> Link::on_state_change;
26 xbt::signal<void(Link const&)> Link::on_bandwidth_change;
27 xbt::signal<void(kernel::resource::NetworkAction&)> Link::on_communicate;
28 xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
29     Link::on_communication_state_change;
30
31 Link* Link::by_name(const std::string& name)
32 {
33   return Engine::get_instance()->link_by_name(name);
34 }
35
36 Link* Link::by_name_or_null(const std::string& name)
37 {
38   return Engine::get_instance()->link_by_name_or_null(name);
39 }
40
41 const std::string& Link::get_name() const
42 {
43   return this->pimpl_->get_name();
44 }
45 const char* Link::get_cname() const
46 {
47   return this->pimpl_->get_cname();
48 }
49 bool Link::is_used() const
50 {
51   return this->pimpl_->is_used();
52 }
53
54 double Link::get_latency() const
55 {
56   return this->pimpl_->get_latency();
57 }
58
59 void Link::set_latency(double value)
60 {
61   kernel::actor::simcall([this, value] { pimpl_->set_latency(value); });
62 }
63
64 double Link::get_bandwidth() const
65 {
66   return this->pimpl_->get_bandwidth();
67 }
68
69 void Link::set_bandwidth(double value)
70 {
71   kernel::actor::simcall([this, value] { pimpl_->set_bandwidth(value); });
72 }
73
74 Link::SharingPolicy Link::get_sharing_policy() const
75 {
76   return this->pimpl_->get_sharing_policy();
77 }
78
79 void Link::set_host_wifi_rate(const s4u::Host* host, int level) const
80 {
81   xbt_assert(pimpl_->get_sharing_policy() == Link::SharingPolicy::WIFI, "Link %s does not seem to be a wifi link.",
82              get_cname());
83   auto* wlink = dynamic_cast<kernel::resource::NetworkWifiLink*>(pimpl_);
84   xbt_assert(wlink != nullptr, "Cannot convert link %s into a wifi link.", get_cname());
85   wlink->set_host_rate(host, level);
86 }
87
88 double Link::get_usage() const
89 {
90   return this->pimpl_->get_constraint()->get_usage();
91 }
92
93 void Link::turn_on()
94 {
95   simgrid::kernel::actor::simcall([this]() { this->pimpl_->turn_on(); });
96 }
97 void Link::turn_off()
98 {
99   simgrid::kernel::actor::simcall([this]() { this->pimpl_->turn_off(); });
100 }
101
102 bool Link::is_on() const
103 {
104   return this->pimpl_->is_on();
105 }
106
107 void Link::set_state_profile(kernel::profile::Profile* profile)
108 {
109   simgrid::kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
110 }
111 void Link::set_bandwidth_profile(kernel::profile::Profile* profile)
112 {
113   simgrid::kernel::actor::simcall([this, profile]() { this->pimpl_->set_bandwidth_profile(profile); });
114 }
115 void Link::set_latency_profile(kernel::profile::Profile* trace)
116 {
117   simgrid::kernel::actor::simcall([this, trace]() { this->pimpl_->set_latency_profile(trace); });
118 }
119
120 const char* Link::get_property(const std::string& key) const
121 {
122   return this->pimpl_->get_property(key);
123 }
124 void Link::set_property(const std::string& key, const std::string& value)
125 {
126   simgrid::kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
127 }
128 } // namespace s4u
129 } // namespace simgrid
130
131 /* **************************** Public C interface *************************** */
132
133 const char* sg_link_name(const_sg_link_t link)
134 {
135   return link->get_cname();
136 }
137 sg_link_t sg_link_by_name(const char* name)
138 {
139   return simgrid::s4u::Link::by_name(name);
140 }
141
142 int sg_link_is_shared(const_sg_link_t link)
143 {
144   return link->get_sharing_policy() != simgrid::s4u::Link::SharingPolicy::FATPIPE;
145 }
146 double sg_link_bandwidth(const_sg_link_t link)
147 {
148   return link->get_bandwidth();
149 }
150
151 void sg_link_bandwidth_set(sg_link_t link, double value)
152 {
153   return link->set_bandwidth(value);
154 }
155
156 double sg_link_latency(const_sg_link_t link)
157 {
158   return link->get_latency();
159 }
160 void sg_link_latency_set(sg_link_t link, double value)
161 {
162   return link->set_latency(value);
163 }
164 void* sg_link_data(const_sg_link_t link)
165 {
166   return link->get_data();
167 }
168 void sg_link_data_set(sg_link_t link, void* data)
169 {
170   link->set_data(data);
171 }
172 int sg_link_count()
173 {
174   return simgrid::s4u::Engine::get_instance()->get_link_count();
175 }
176
177 sg_link_t* sg_link_list()
178 {
179   std::vector<simgrid::s4u::Link*> links = simgrid::s4u::Engine::get_instance()->get_all_links();
180
181   sg_link_t* res = xbt_new(sg_link_t, links.size());
182   memcpy(res, links.data(), sizeof(sg_link_t) * links.size());
183
184   return res;
185 }