diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-02-05 19:48:15 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-02-05 19:48:15 +0000 |
commit | 21264a59434a5b18b4f5e85e735a2d3ec5c3c6c6 (patch) | |
tree | db7c1187ddbba17641e0c0fcd631331803d19983 /src/ats/gnunet-service-ats_normalization.c | |
parent | 88031e0e8188e96d7ae4ce3659e6ed0d90a64f26 (diff) | |
download | gnunet-21264a59434a5b18b4f5e85e735a2d3ec5c3c6c6.tar.gz gnunet-21264a59434a5b18b4f5e85e735a2d3ec5c3c6c6.zip |
cleaning up gnunet-service-ats_normalization.c
Diffstat (limited to 'src/ats/gnunet-service-ats_normalization.c')
-rw-r--r-- | src/ats/gnunet-service-ats_normalization.c | 143 |
1 files changed, 95 insertions, 48 deletions
diff --git a/src/ats/gnunet-service-ats_normalization.c b/src/ats/gnunet-service-ats_normalization.c index 7003f69e4..244813108 100644 --- a/src/ats/gnunet-service-ats_normalization.c +++ b/src/ats/gnunet-service-ats_normalization.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2011 Christian Grothoff (and other contributing authors) | 3 | (C) 2011-2015 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -24,7 +24,7 @@ | |||
24 | * @author Matthias Wachs | 24 | * @author Matthias Wachs |
25 | * @author Christian Grothoff | 25 | * @author Christian Grothoff |
26 | * | 26 | * |
27 | * FIXME: rename to 'properties'! | 27 | * FIXME: rename to 'properties'!? |
28 | */ | 28 | */ |
29 | #include "platform.h" | 29 | #include "platform.h" |
30 | #include "gnunet_ats_service.h" | 30 | #include "gnunet_ats_service.h" |
@@ -35,32 +35,42 @@ | |||
35 | #define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__) | 35 | #define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__) |
36 | 36 | ||
37 | 37 | ||
38 | |||
39 | /** | 38 | /** |
40 | * Quality Normalization | 39 | * Quality Normalization |
41 | */ | 40 | */ |
42 | struct Property | 41 | struct Property |
43 | { | 42 | { |
43 | /** | ||
44 | * Index into the properties array. | ||
45 | */ | ||
44 | uint32_t prop_type; | 46 | uint32_t prop_type; |
47 | |||
48 | /** | ||
49 | * Corresponding enum value. FIXME: type? | ||
50 | */ | ||
45 | uint32_t atsi_type; | 51 | uint32_t atsi_type; |
52 | |||
53 | /** | ||
54 | * Minimum value we see for this property across all addresses. | ||
55 | */ | ||
46 | uint32_t min; | 56 | uint32_t min; |
57 | |||
58 | /** | ||
59 | * Maximum value we see for this property across all addresses. | ||
60 | */ | ||
47 | uint32_t max; | 61 | uint32_t max; |
48 | }; | 62 | }; |
49 | 63 | ||
50 | static struct Property properties[GNUNET_ATS_QualityPropertiesCount]; | ||
51 | 64 | ||
52 | /** | 65 | /** |
53 | * Hashmap to store peer information for property normalization | 66 | * Range information for all properties we see. |
54 | * FIXME: this map is not used! | ||
55 | */ | 67 | */ |
56 | static struct GNUNET_CONTAINER_MultiPeerMap *property_peers; | 68 | static struct Property properties[GNUNET_ATS_QualityPropertiesCount]; |
57 | |||
58 | |||
59 | 69 | ||
60 | 70 | ||
61 | /** | 71 | /** |
62 | * Get the normalized properties values for a specific peer or | 72 | * Get the normalized properties values for a specific peer or |
63 | * the default values if | 73 | * the default values if no normalized values are available. |
64 | * | 74 | * |
65 | * @param cls ignored | 75 | * @param cls ignored |
66 | * @param address the address | 76 | * @param address the address |
@@ -72,9 +82,7 @@ GAS_normalization_get_properties (void *cls, | |||
72 | const struct ATS_Address *address) | 82 | const struct ATS_Address *address) |
73 | { | 83 | { |
74 | static double norm_values[GNUNET_ATS_QualityPropertiesCount]; | 84 | static double norm_values[GNUNET_ATS_QualityPropertiesCount]; |
75 | int i; | 85 | unsigned int i; |
76 | |||
77 | GNUNET_assert(NULL != address); | ||
78 | 86 | ||
79 | for (i = 0; i < GNUNET_ATS_QualityPropertiesCount; i++) | 87 | for (i = 0; i < GNUNET_ATS_QualityPropertiesCount; i++) |
80 | { | 88 | { |
@@ -88,7 +96,8 @@ GAS_normalization_get_properties (void *cls, | |||
88 | 96 | ||
89 | 97 | ||
90 | /** | 98 | /** |
91 | * Normalize a specific ATS type with the values in queue | 99 | * Normalize a specific ATS type with the values in queue. |
100 | * | ||
92 | * @param address the address | 101 | * @param address the address |
93 | * @param atsi the ats information | 102 | * @param atsi the ats information |
94 | * @return the new average or GNUNET_ATS_VALUE_UNDEFINED | 103 | * @return the new average or GNUNET_ATS_VALUE_UNDEFINED |
@@ -149,40 +158,78 @@ property_average (struct ATS_Address *address, | |||
149 | res = sum / count; | 158 | res = sum / count; |
150 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 159 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
151 | "New average of `%s' created by adding %u from %u elements: %u\n", | 160 | "New average of `%s' created by adding %u from %u elements: %u\n", |
152 | GNUNET_ATS_print_property_type (current_type), current_val, count, res, | 161 | GNUNET_ATS_print_property_type (current_type), |
162 | current_val, | ||
163 | count, | ||
164 | res, | ||
153 | sum); | 165 | sum); |
154 | ni->avg = res; | 166 | ni->avg = res; |
155 | return res; | 167 | return res; |
156 | } | 168 | } |
157 | 169 | ||
158 | 170 | ||
171 | /** | ||
172 | * Closure for #find_min_max_it(). | ||
173 | */ | ||
159 | struct FindMinMaxCtx | 174 | struct FindMinMaxCtx |
160 | { | 175 | { |
176 | /** | ||
177 | * Property we are looking for. | ||
178 | */ | ||
161 | struct Property *p; | 179 | struct Property *p; |
180 | |||
181 | /** | ||
182 | * Set to mimimum value observed. | ||
183 | */ | ||
162 | uint32_t min; | 184 | uint32_t min; |
185 | |||
186 | /** | ||
187 | * Set to maximum value observed. | ||
188 | */ | ||
163 | uint32_t max; | 189 | uint32_t max; |
164 | }; | 190 | }; |
165 | 191 | ||
166 | 192 | ||
193 | /** | ||
194 | * Function called on X to find the minimum and maximum | ||
195 | * values for a given property. | ||
196 | * | ||
197 | * @param cls the `struct FindMinMaxCtx` | ||
198 | * @param h which peer are we looking at (ignored) | ||
199 | * @param k the address for that peer | ||
200 | * @return #GNUNET_OK (continue to iterate) | ||
201 | */ | ||
167 | static int | 202 | static int |
168 | find_min_max_it (void *cls, const struct GNUNET_PeerIdentity *h, void *k) | 203 | find_min_max_it (void *cls, |
204 | const struct GNUNET_PeerIdentity *h, | ||
205 | void *k) | ||
169 | { | 206 | { |
170 | struct FindMinMaxCtx *find_res = cls; | 207 | struct FindMinMaxCtx *find_res = cls; |
171 | struct ATS_Address *a = k; | 208 | const struct ATS_Address *a = k; |
172 | |||
173 | if (a->atsin[find_res->p->prop_type].avg > find_res->max) | ||
174 | find_res->max = a->atsin[find_res->p->prop_type].avg; | ||
175 | |||
176 | if (a->atsin[find_res->p->prop_type].avg < find_res->min) | ||
177 | find_res->min = a->atsin[find_res->p->prop_type].avg; | ||
178 | 209 | ||
210 | find_res->max = GNUNET_MAX (find_res->max, | ||
211 | a->atsin[find_res->p->prop_type].avg); | ||
212 | find_res->min = GNUNET_MIN (find_res->min, | ||
213 | a->atsin[find_res->p->prop_type].avg); | ||
179 | return GNUNET_OK; | 214 | return GNUNET_OK; |
180 | } | 215 | } |
181 | 216 | ||
182 | 217 | ||
218 | /** | ||
219 | * Normalize the property value for a given address based | ||
220 | * on the range we know that property value has globally. | ||
221 | * | ||
222 | * @param cls the `struct Property` with details on the | ||
223 | * property and its global range | ||
224 | * @param h which peer are we looking at (ignored) | ||
225 | * @param k the address for that peer, from where we get | ||
226 | * the original value and where we write the | ||
227 | * normalized value | ||
228 | * @return #GNUNET_OK (continue to iterate) | ||
229 | */ | ||
183 | static int | 230 | static int |
184 | normalize_address (void *cls, | 231 | normalize_address (void *cls, |
185 | const struct GNUNET_PeerIdentity *h, | 232 | const struct GNUNET_PeerIdentity *h, |
186 | void *k) | 233 | void *k) |
187 | { | 234 | { |
188 | struct Property *p = cls; | 235 | struct Property *p = cls; |
@@ -203,32 +250,30 @@ normalize_address (void *cls, | |||
203 | if (backup == address->atsin[p->prop_type].norm) | 250 | if (backup == address->atsin[p->prop_type].norm) |
204 | return GNUNET_OK; | 251 | return GNUNET_OK; |
205 | 252 | ||
206 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 253 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
207 | "Normalize `%s' address %p's '%s' with value %u to range [%u..%u] = %.3f\n", | 254 | "Normalize `%s' address %p's '%s' with value %u to range [%u..%u] = %.3f\n", |
208 | GNUNET_i2s (&address->peer), address, | 255 | GNUNET_i2s (&address->peer), address, |
209 | GNUNET_ATS_print_property_type (p->atsi_type), | 256 | GNUNET_ATS_print_property_type (p->atsi_type), |
210 | address->atsin[p->prop_type].avg, p->min, p->max, | 257 | address->atsin[p->prop_type].avg, p->min, p->max, |
211 | address->atsin[p->prop_type].norm); | 258 | address->atsin[p->prop_type].norm); |
212 | 259 | GAS_normalized_property_changed (address, | |
213 | GAS_normalized_property_changed (address, | ||
214 | p->atsi_type, | 260 | p->atsi_type, |
215 | address->atsin[p->prop_type].norm); | 261 | address->atsin[p->prop_type].norm); |
216 | |||
217 | return GNUNET_OK; | 262 | return GNUNET_OK; |
218 | } | 263 | } |
219 | 264 | ||
220 | 265 | ||
221 | /** | 266 | /** |
222 | * Normalize avg_value to a range of values between [1.0, 2.0] | 267 | * Normalize @a avg_value to a range of values between [1.0, 2.0] |
223 | * based on min max values currently known. | 268 | * based on min/max values currently known. |
224 | * | 269 | * |
225 | * @param p the property | 270 | * @param p the property |
226 | * @param address the address | 271 | * @param address the address |
227 | * @param avg_value the value to normalize | 272 | * @param avg_value the value to normalize |
228 | */ | 273 | */ |
229 | static void | 274 | static void |
230 | property_normalize (struct Property *p, | 275 | property_normalize (struct Property *p, |
231 | struct ATS_Address *address, | 276 | struct ATS_Address *address, |
232 | uint32_t avg_value) | 277 | uint32_t avg_value) |
233 | { | 278 | { |
234 | struct FindMinMaxCtx find_ctx; | 279 | struct FindMinMaxCtx find_ctx; |
@@ -239,7 +284,7 @@ property_normalize (struct Property *p, | |||
239 | find_ctx.max = 0; | 284 | find_ctx.max = 0; |
240 | find_ctx.min = UINT32_MAX; | 285 | find_ctx.min = UINT32_MAX; |
241 | addr_count = GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, | 286 | addr_count = GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, |
242 | &find_min_max_it, | 287 | &find_min_max_it, |
243 | &find_ctx); | 288 | &find_ctx); |
244 | if (0 == addr_count) | 289 | if (0 == addr_count) |
245 | { | 290 | { |
@@ -252,7 +297,7 @@ property_normalize (struct Property *p, | |||
252 | { | 297 | { |
253 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 298 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
254 | "Normalizing %s: new maximum %u -> recalculate all values\n", | 299 | "Normalizing %s: new maximum %u -> recalculate all values\n", |
255 | GNUNET_ATS_print_property_type (p->atsi_type), | 300 | GNUNET_ATS_print_property_type (p->atsi_type), |
256 | find_ctx.max); | 301 | find_ctx.max); |
257 | p->max = find_ctx.max; | 302 | p->max = find_ctx.max; |
258 | limits_changed = GNUNET_YES; | 303 | limits_changed = GNUNET_YES; |
@@ -278,13 +323,15 @@ property_normalize (struct Property *p, | |||
278 | if (GNUNET_NO == limits_changed) | 323 | if (GNUNET_NO == limits_changed) |
279 | { | 324 | { |
280 | /* normalize just this address */ | 325 | /* normalize just this address */ |
281 | normalize_address (p, &address->peer, address); | 326 | normalize_address (p, |
327 | &address->peer, | ||
328 | address); | ||
282 | } | 329 | } |
283 | else | 330 | else |
284 | { | 331 | { |
285 | /* limits changed, normalize all addresses */ | 332 | /* limits changed, normalize all addresses */ |
286 | GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, | 333 | GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses, |
287 | &normalize_address, | 334 | &normalize_address, |
288 | p); | 335 | p); |
289 | } | 336 | } |
290 | } | 337 | } |
@@ -340,40 +387,40 @@ GAS_normalization_normalize_property (struct ATS_Address *address, | |||
340 | /* Normalizing */ | 387 | /* Normalizing */ |
341 | /* Check min, max */ | 388 | /* Check min, max */ |
342 | cur_prop = &properties[c2]; | 389 | cur_prop = &properties[c2]; |
343 | property_normalize (cur_prop, address, current_val); | 390 | property_normalize (cur_prop, |
391 | address, | ||
392 | current_val); | ||
344 | } | 393 | } |
345 | } | 394 | } |
346 | 395 | ||
347 | 396 | ||
348 | |||
349 | /** | 397 | /** |
350 | * Start the normalization component | 398 | * Start the normalization component |
351 | */ | 399 | */ |
352 | void | 400 | void |
353 | GAS_normalization_start () | 401 | GAS_normalization_start () |
354 | { | 402 | { |
355 | int c1; | 403 | unsigned int c1; |
356 | unsigned int existing_properties[] = GNUNET_ATS_QualityProperties; | 404 | unsigned int existing_properties[] = GNUNET_ATS_QualityProperties; |
357 | 405 | ||
358 | property_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO); | ||
359 | for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++) | 406 | for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++) |
360 | { | 407 | { |
361 | properties[c1].prop_type = c1; | 408 | properties[c1].prop_type = c1; |
362 | properties[c1].atsi_type = existing_properties[c1]; | 409 | properties[c1].atsi_type = existing_properties[c1]; |
363 | properties[c1].min = 0; | 410 | properties[c1].min = UINT32_MAX; |
364 | properties[c1].max = 0; | 411 | properties[c1].max = 0; |
365 | } | 412 | } |
366 | } | 413 | } |
367 | 414 | ||
368 | 415 | ||
369 | |||
370 | /** | 416 | /** |
371 | * Stop the normalization component and free all items | 417 | * Stop the normalization component and free all items |
372 | */ | 418 | */ |
373 | void | 419 | void |
374 | GAS_normalization_stop () | 420 | GAS_normalization_stop () |
375 | { | 421 | { |
376 | GNUNET_CONTAINER_multipeermap_destroy (property_peers); | 422 | /* nothing to do */ |
377 | } | 423 | } |
378 | 424 | ||
425 | |||
379 | /* end of gnunet-service-ats_normalization.c */ | 426 | /* end of gnunet-service-ats_normalization.c */ |