Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a7d6c2a085602e9d025d328e601a4ee6ee2229d6
[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::surf::LinkImpl::linksCount();
53 }
54 sg_link_t* sg_link_list()
55 {
56   simgrid::surf::LinkImpl** list = simgrid::surf::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::surf::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   surf::LinkImpl* res = surf::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_->constraint()->get_usage();
118 }
119
120 void Link::turnOn()
121 {
122   simgrid::simix::kernelImmediate([this]() {
123     this->pimpl_->turnOn();
124   });
125 }
126 void Link::turnOff()
127 {
128   simgrid::simix::kernelImmediate([this]() {
129     this->pimpl_->turnOff();
130   });
131 }
132
133 void* Link::getData()
134 {
135   return this->pimpl_->getData();
136 }
137 void Link::setData(void* d)
138 {
139   simgrid::simix::kernelImmediate([this, d]() {
140     this->pimpl_->setData(d);
141   });
142 }
143
144 void Link::setStateTrace(tmgr_trace_t trace)
145 {
146   simgrid::simix::kernelImmediate([this, trace]() {
147     this->pimpl_->setStateTrace(trace);
148   });
149 }
150 void Link::setBandwidthTrace(tmgr_trace_t trace)
151 {
152   simgrid::simix::kernelImmediate([this, trace]() {
153     this->pimpl_->setBandwidthTrace(trace);
154   });
155 }
156 void Link::setLatencyTrace(tmgr_trace_t trace)
157 {
158   simgrid::simix::kernelImmediate([this, trace]() {
159     this->pimpl_->setLatencyTrace(trace);
160   });
161 }
162
163 const char* Link::getProperty(const char* key)
164 {
165   return this->pimpl_->getProperty(key);
166 }
167 void Link::setProperty(std::string key, std::string value)
168 {
169   simgrid::simix::kernelImmediate([this, key, value] { this->pimpl_->setProperty(key, value); });
170 }
171
172 /*************
173  * Callbacks *
174  *************/
175 simgrid::xbt::signal<void(s4u::Link&)> Link::onCreation;
176 simgrid::xbt::signal<void(s4u::Link&)> Link::onDestruction;
177 simgrid::xbt::signal<void(s4u::Link&)> Link::onStateChange;
178 simgrid::xbt::signal<void(surf::NetworkAction*, s4u::Host* src, s4u::Host* dst)> Link::onCommunicate;
179 simgrid::xbt::signal<void(surf::NetworkAction*)> Link::onCommunicationStateChange;
180 }
181 }