summaryrefslogtreecommitdiff
path: root/src/ats/gnunet-service-ats_normalization.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ats/gnunet-service-ats_normalization.c')
-rw-r--r--src/ats/gnunet-service-ats_normalization.c217
1 files changed, 108 insertions, 109 deletions
diff --git a/src/ats/gnunet-service-ats_normalization.c b/src/ats/gnunet-service-ats_normalization.c
index 4b58bf9b7..fa2b37425 100644
--- a/src/ats/gnunet-service-ats_normalization.c
+++ b/src/ats/gnunet-service-ats_normalization.c
@@ -1,19 +1,19 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V. 3 Copyright (C) 2011-2015 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
@@ -31,14 +31,13 @@
31#include "gnunet-service-ats_normalization.h" 31#include "gnunet-service-ats_normalization.h"
32#include "gnunet-service-ats_plugins.h" 32#include "gnunet-service-ats_plugins.h"
33 33
34#define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from(kind, "ats-normalization", __VA_ARGS__)
35 35
36 36
37/** 37/**
38 * Range information for normalization of quality properties. 38 * Range information for normalization of quality properties.
39 */ 39 */
40struct PropertyRange 40struct PropertyRange {
41{
42 /** 41 /**
43 * Minimum value we see for this property across all addresses. 42 * Minimum value we see for this property across all addresses.
44 */ 43 */
@@ -65,8 +64,8 @@ static struct PropertyRange property_range;
65 * @param ni normalization information to update 64 * @param ni normalization information to update
66 */ 65 */
67static void 66static void
68update_avg (uint64_t current_val, 67update_avg(uint64_t current_val,
69 struct GAS_NormalizationInfo *ni) 68 struct GAS_NormalizationInfo *ni)
70{ 69{
71 double sum; 70 double sum;
72 uint32_t count; 71 uint32_t count;
@@ -78,13 +77,13 @@ update_avg (uint64_t current_val,
78 count = 0; 77 count = 0;
79 sum = 0.0; 78 sum = 0.0;
80 for (c1 = 0; c1 < GAS_normalization_queue_length; c1++) 79 for (c1 = 0; c1 < GAS_normalization_queue_length; c1++)
81 {
82 if (UINT64_MAX != ni->atsi_abs[c1])
83 { 80 {
84 count++; 81 if (UINT64_MAX != ni->atsi_abs[c1])
85 sum += (double) ni->atsi_abs[c1]; 82 {
83 count++;
84 sum += (double)ni->atsi_abs[c1];
85 }
86 } 86 }
87 }
88 if (0 == count) 87 if (0 == count)
89 ni->avg = current_val; /* must be UINT64_MAX */ 88 ni->avg = current_val; /* must be UINT64_MAX */
90 else 89 else
@@ -103,29 +102,29 @@ update_avg (uint64_t current_val,
103 * @return #GNUNET_OK (continue to iterate) 102 * @return #GNUNET_OK (continue to iterate)
104 */ 103 */
105static int 104static int
106find_min_max_it (void *cls, 105find_min_max_it(void *cls,
107 const struct GNUNET_PeerIdentity *h, 106 const struct GNUNET_PeerIdentity *h,
108 void *k) 107 void *k)
109{ 108{
110 struct PropertyRange *pr = cls; 109 struct PropertyRange *pr = cls;
111 const struct ATS_Address *a = k; 110 const struct ATS_Address *a = k;
112 111
113 pr->max.utilization_out = GNUNET_MAX (pr->max.utilization_out, 112 pr->max.utilization_out = GNUNET_MAX(pr->max.utilization_out,
114 a->properties.utilization_out); 113 a->properties.utilization_out);
115 pr->max.utilization_in = GNUNET_MAX (pr->max.utilization_in, 114 pr->max.utilization_in = GNUNET_MAX(pr->max.utilization_in,
116 a->properties.utilization_in); 115 a->properties.utilization_in);
117 pr->max.distance = GNUNET_MAX (pr->max.distance, 116 pr->max.distance = GNUNET_MAX(pr->max.distance,
118 a->properties.distance); 117 a->properties.distance);
119 pr->max.delay = GNUNET_TIME_relative_max (pr->max.delay, 118 pr->max.delay = GNUNET_TIME_relative_max(pr->max.delay,
120 a->properties.delay); 119 a->properties.delay);
121 pr->min.utilization_out = GNUNET_MIN (pr->min.utilization_out, 120 pr->min.utilization_out = GNUNET_MIN(pr->min.utilization_out,
122 a->properties.utilization_out); 121 a->properties.utilization_out);
123 pr->min.utilization_in = GNUNET_MIN (pr->min.utilization_in, 122 pr->min.utilization_in = GNUNET_MIN(pr->min.utilization_in,
124 a->properties.utilization_in); 123 a->properties.utilization_in);
125 pr->min.distance = GNUNET_MIN (pr->min.distance, 124 pr->min.distance = GNUNET_MIN(pr->min.distance,
126 a->properties.distance); 125 a->properties.distance);
127 pr->min.delay = GNUNET_TIME_relative_min (pr->min.delay, 126 pr->min.delay = GNUNET_TIME_relative_min(pr->min.delay,
128 a->properties.delay); 127 a->properties.delay);
129 return GNUNET_OK; 128 return GNUNET_OK;
130} 129}
131 130
@@ -139,13 +138,13 @@ find_min_max_it (void *cls,
139 * @param ni normalization information to update 138 * @param ni normalization information to update
140 */ 139 */
141static void 140static void
142update_norm (uint64_t min, 141update_norm(uint64_t min,
143 uint64_t max, 142 uint64_t max,
144 struct GAS_NormalizationInfo *ni) 143 struct GAS_NormalizationInfo *ni)
145{ 144{
146 /* max - 2 * min + avg_value / (max - min) */ 145 /* max - 2 * min + avg_value / (max - min) */
147 if (min < max) 146 if (min < max)
148 ni->norm = DEFAULT_REL_QUALITY + (ni->avg - min) / (double) (max - min); 147 ni->norm = DEFAULT_REL_QUALITY + (ni->avg - min) / (double)(max - min);
149 else 148 else
150 ni->norm = DEFAULT_REL_QUALITY; 149 ni->norm = DEFAULT_REL_QUALITY;
151} 150}
@@ -163,24 +162,24 @@ update_norm (uint64_t min,
163 * @return #GNUNET_OK (continue to iterate) 162 * @return #GNUNET_OK (continue to iterate)
164 */ 163 */
165static int 164static int
166normalize_address (void *cls, 165normalize_address(void *cls,
167 const struct GNUNET_PeerIdentity *key, 166 const struct GNUNET_PeerIdentity *key,
168 void *value) 167 void *value)
169{ 168{
170 struct ATS_Address *address = value; 169 struct ATS_Address *address = value;
171 170
172 update_norm (property_range.min.delay.rel_value_us, 171 update_norm(property_range.min.delay.rel_value_us,
173 property_range.max.delay.rel_value_us, 172 property_range.max.delay.rel_value_us,
174 &address->norm_delay); 173 &address->norm_delay);
175 update_norm (property_range.min.distance, 174 update_norm(property_range.min.distance,
176 property_range.max.distance, 175 property_range.max.distance,
177 &address->norm_distance); 176 &address->norm_distance);
178 update_norm (property_range.min.utilization_in, 177 update_norm(property_range.min.utilization_in,
179 property_range.max.utilization_in, 178 property_range.max.utilization_in,
180 &address->norm_utilization_in); 179 &address->norm_utilization_in);
181 update_norm (property_range.min.utilization_out, 180 update_norm(property_range.min.utilization_out,
182 property_range.max.utilization_out, 181 property_range.max.utilization_out,
183 &address->norm_utilization_out); 182 &address->norm_utilization_out);
184 return GNUNET_OK; 183 return GNUNET_OK;
185} 184}
186 185
@@ -194,13 +193,13 @@ normalize_address (void *cls,
194 * @return #GNUNET_OK (continue to iterate) 193 * @return #GNUNET_OK (continue to iterate)
195 */ 194 */
196static int 195static int
197notify_change (void *cls, 196notify_change(void *cls,
198 const struct GNUNET_PeerIdentity *key, 197 const struct GNUNET_PeerIdentity *key,
199 void *value) 198 void *value)
200{ 199{
201 struct ATS_Address *address = value; 200 struct ATS_Address *address = value;
202 201
203 GAS_plugin_notify_property_changed (address); 202 GAS_plugin_notify_property_changed(address);
204 return GNUNET_OK; 203 return GNUNET_OK;
205} 204}
206 205
@@ -212,9 +211,9 @@ notify_change (void *cls,
212 * @param pr range to initialize 211 * @param pr range to initialize
213 */ 212 */
214static void 213static void
215init_range (struct PropertyRange *pr) 214init_range(struct PropertyRange *pr)
216{ 215{
217 memset (pr, 0, sizeof (struct PropertyRange)); 216 memset(pr, 0, sizeof(struct PropertyRange));
218 pr->min.utilization_out = UINT32_MAX; 217 pr->min.utilization_out = UINT32_MAX;
219 pr->min.utilization_in = UINT32_MAX; 218 pr->min.utilization_in = UINT32_MAX;
220 pr->min.distance = UINT32_MAX; 219 pr->min.distance = UINT32_MAX;
@@ -228,51 +227,51 @@ init_range (struct PropertyRange *pr)
228 * @param address the address to update 227 * @param address the address to update
229 */ 228 */
230void 229void
231GAS_normalization_update_property (struct ATS_Address *address) 230GAS_normalization_update_property(struct ATS_Address *address)
232{ 231{
233 const struct GNUNET_ATS_Properties *prop = &address->properties; 232 const struct GNUNET_ATS_Properties *prop = &address->properties;
234 struct PropertyRange range; 233 struct PropertyRange range;
235 234
236 LOG (GNUNET_ERROR_TYPE_DEBUG, 235 LOG(GNUNET_ERROR_TYPE_DEBUG,
237 "Updating properties for peer `%s'\n", 236 "Updating properties for peer `%s'\n",
238 GNUNET_i2s (&address->peer)); 237 GNUNET_i2s(&address->peer));
239 GAS_plugin_solver_lock (); 238 GAS_plugin_solver_lock();
240 update_avg (prop->delay.rel_value_us, 239 update_avg(prop->delay.rel_value_us,
241 &address->norm_delay); 240 &address->norm_delay);
242 update_avg (prop->distance, 241 update_avg(prop->distance,
243 &address->norm_distance); 242 &address->norm_distance);
244 update_avg (prop->utilization_in, 243 update_avg(prop->utilization_in,
245 &address->norm_utilization_in); 244 &address->norm_utilization_in);
246 update_avg (prop->utilization_in, 245 update_avg(prop->utilization_in,
247 &address->norm_utilization_out); 246 &address->norm_utilization_out);
248 247
249 init_range (&range); 248 init_range(&range);
250 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, 249 GNUNET_CONTAINER_multipeermap_iterate(GSA_addresses,
251 &find_min_max_it, 250 &find_min_max_it,
252 &range); 251 &range);
253 if (0 != GNUNET_memcmp (&range, 252 if (0 != GNUNET_memcmp(&range,
254 &property_range)) 253 &property_range))
255 { 254 {
256 /* limits changed, (re)normalize all addresses */ 255 /* limits changed, (re)normalize all addresses */
257 property_range = range; 256 property_range = range;
258 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, 257 GNUNET_CONTAINER_multipeermap_iterate(GSA_addresses,
259 &normalize_address, 258 &normalize_address,
260 NULL); 259 NULL);
261 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, 260 GNUNET_CONTAINER_multipeermap_iterate(GSA_addresses,
262 &notify_change, 261 &notify_change,
263 NULL); 262 NULL);
264 } 263 }
265 else 264 else
266 { 265 {
267 /* renormalize just this one address */ 266 /* renormalize just this one address */
268 normalize_address (NULL, 267 normalize_address(NULL,
269 &address->peer, 268 &address->peer,
270 address); 269 address);
271 notify_change (NULL, 270 notify_change(NULL,
272 &address->peer, 271 &address->peer,
273 address); 272 address);
274 } 273 }
275 GAS_plugin_solver_unlock (); 274 GAS_plugin_solver_unlock();
276} 275}
277 276
278 277
@@ -280,9 +279,9 @@ GAS_normalization_update_property (struct ATS_Address *address)
280 * Start the normalization component 279 * Start the normalization component
281 */ 280 */
282void 281void
283GAS_normalization_start () 282GAS_normalization_start()
284{ 283{
285 init_range (&property_range); 284 init_range(&property_range);
286} 285}
287 286
288 287
@@ -290,7 +289,7 @@ GAS_normalization_start ()
290 * Stop the normalization component and free all items 289 * Stop the normalization component and free all items
291 */ 290 */
292void 291void
293GAS_normalization_stop () 292GAS_normalization_stop()
294{ 293{
295 /* nothing to do */ 294 /* nothing to do */
296} 295}