Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move all network models to the kernel::resource namespace
[simgrid.git] / src / s4u / s4u_link.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 <algorithm>
7
8 #include "simgrid/s4u/Link.hpp"
9 #include "simgrid/sg_config.hpp"
10 #include "simgrid/simix.hpp"
11 #include "src/kernel/lmm/maxmin.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "xbt/log.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_link, s4u, "Logging specific to the S4U links");
16
17 /*********
18  * C API *
19  *********/
20
21 const char* sg_link_name(sg_link_t link)
22 {
23   return link->get_cname();
24 }
25 sg_link_t sg_link_by_name(const char* name)
26 {
27   return simgrid::s4u::Link::byName(name);
28 }
29
30 int sg_link_is_shared(sg_link_t link)
31 {
32   return link->sharingPolicy();
33 }
34 double sg_link_bandwidth(sg_link_t link)
35 {
36   return link->bandwidth();
37 }
38 double sg_link_latency(sg_link_t link)
39 {
40   return link->latency();
41 }
42 void* sg_link_data(sg_link_t link)
43 {
44   return link->getData();
45 }
46 void sg_link_data_set(sg_link_t link, void* data)
47 {
48   link->setData(data);
49 }
50 int sg_link_count()
51 {
52   return simgrid::kernel::resource::LinkImpl::linksCount();
53 }
54 sg_link_t* sg_link_list()
55 {
56   simgrid::kernel::resource::LinkImpl** list = simgrid::kernel::resource::LinkImpl::linksList();
57   sg_link_t* res                 = (sg_link_t*)list; // Use the same memory area
58
59   int size = sg_link_count();
60   for (int i = 0; i < size; i++)
61     res[i] = &(list[i]->piface_); // Convert each entry into its interface
62
63   return res;
64 }
65 void sg_link_exit()
66 {
67   simgrid::kernel::resource::LinkImpl::linksExit();
68 }
69
70 /***********
71  * C++ API *
72  ***********/
73
74 namespace simgrid {
75 namespace s4u {
76 Link* Link::byName(const char* name)
77 {
78   kernel::resource::LinkImpl* res = kernel::resource::LinkImpl::byName(name);
79   if (res == nullptr)
80     return nullptr;
81   return &res->piface_;
82 }
83 const std::string& Link::get_name() const
84 {
85   return this->pimpl_->get_name();
86 }
87 const char* Link::get_cname() const
88 {
89   return this->pimpl_->get_cname();
90 }
91 const char* Link::name()
92 {
93   return get_cname();
94 }
95 bool Link::isUsed()
96 {
97   return this->pimpl_->is_used();
98 }
99
100 double Link::latency()
101 {
102   return this->pimpl_->latency();
103 }
104
105 double Link::bandwidth()
106 {
107   return this->pimpl_->bandwidth();
108 }
109
110 int Link::sharingPolicy()
111 {
112   return this->pimpl_->sharingPolicy();
113 }
114
115 double Link::getUsage()
116 {
117   return this->pimpl_->get_constraint()->get_usage();
118 }
119
120 void Link::turnOn()
121 {
122   simgrid::simix::kernelImmediate([this]() { this->pimpl_->turn_on(); });
123 }
124 void Link::turnOff()
125 {
126   simgrid::simix::kernelImmediate([this]() { this->pimpl_->turn_off(); });
127 }
128
129 void* Link::getData()
130 {
131   return this->pimpl_->getData();
132 }
133 void Link::setData(void* d)
134 {
135   simgrid::simix::kernelImmediate([this, d]() {
136     this->pimpl_->setData(d);
137   });
138 }
139
140 void Link::setStateTrace(tmgr_trace_t trace)
141 {
142   simgrid::simix::kernelImmediate([this, trace]() {
143     this->pimpl_->setStateTrace(trace);
144   });
145 }
146 void Link::setBandwidthTrace(tmgr_trace_t trace)
147 {
148   simgrid::simix::kernelImmediate([this, trace]() {
149     this->pimpl_->setBandwidthTrace(trace);
150   });
151 }
152 void Link::setLatencyTrace(tmgr_trace_t trace)
153 {
154   simgrid::simix::kernelImmediate([this, trace]() {
155     this->pimpl_->setLatencyTrace(trace);
156   });
157 }
158
159 const char* Link::getProperty(const char* key)
160 {
161   return this->pimpl_->getProperty(key);
162 }
163 void Link::setProperty(std::string key, std::string value)
164 {
165   simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
166 }
167
168 /*************
169  * Callbacks *
170  *************/
171 simgrid::xbt::signal<void(s4u::Link&)> Link::onCreation;
172 simgrid::xbt::signal<void(s4u::Link&)> Link::onDestruction;
173 simgrid::xbt::signal<void(s4u::Link&)> Link::onStateChange;
174 simgrid::xbt::signal<void(kernel::resource::NetworkAction*, s4u::Host* src, s4u::Host* dst)> Link::onCommunicate;
175 simgrid::xbt::signal<void(kernel::resource::NetworkAction*)> Link::onCommunicationStateChange;
176 }
177 }