2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 Christian Grothoff (and other contributing authors)
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats_normalization.c
23 * @brief ats service address: management of ATS properties and preferences normalization
24 * @author Matthias Wachs
25 * @author Christian Grothoff
27 * FIXME: rename to 'properties'!? merge with addresses!?
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet-service-ats_plugins.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__)
39 * Quality Normalization
44 * Index into the properties array.
49 * Corresponding enum value. FIXME: type?
54 * Minimum value we see for this property across all addresses.
59 * Maximum value we see for this property across all addresses.
66 * Range information for all properties we see.
68 static struct Property properties[GNUNET_ATS_QualityPropertiesCount];
72 * Get the normalized properties values for a specific peer or
73 * the default values if no normalized values are available.
76 * @param address the address
77 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind
80 GAS_normalization_get_properties (void *cls,
81 const struct ATS_Address *address)
83 static double norm_values[GNUNET_ATS_QualityPropertiesCount];
86 for (i = 0; i < GNUNET_ATS_QualityPropertiesCount; i++)
88 if ((address->atsin[i].norm >= 1.0) && (address->atsin[i].norm <= 2.0))
89 norm_values[i] = address->atsin[i].norm;
91 norm_values[i] = DEFAULT_REL_QUALITY;
98 * Normalize a specific ATS type with the values in queue.
100 * @param address the address
101 * @param atsi the ats information
102 * @return the new average or #GNUNET_ATS_VALUE_UNDEFINED
105 property_average (struct ATS_Address *address,
106 const struct GNUNET_ATS_Information *atsi)
108 struct GAS_NormalizationInfo *ni;
109 uint32_t current_type;
110 uint32_t current_val;
116 unsigned int props[] = GNUNET_ATS_QualityProperties;
118 /* Average the values of this property */
119 current_type = ntohl (atsi->type);
120 current_val = ntohl (atsi->value);
122 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
124 if (current_type == props[c1])
127 if (c1 == GNUNET_ATS_QualityPropertiesCount)
130 return GNUNET_ATS_VALUE_UNDEFINED;
134 ni = &address->atsin[index];
135 ni->atsi_abs[ni->avg_queue_index] = current_val;
136 ni->avg_queue_index++;
137 if (GAS_normalization_queue_length == ni->avg_queue_index)
138 ni->avg_queue_index = 0;
142 for (c1 = 0; c1 < GAS_normalization_queue_length; c1++)
144 if (GNUNET_ATS_VALUE_UNDEFINED != ni->atsi_abs[c1])
147 if (GNUNET_ATS_VALUE_UNDEFINED > (sum + ni->atsi_abs[c1]))
148 sum += ni->atsi_abs[c1];
151 sum = GNUNET_ATS_VALUE_UNDEFINED - 1;
156 GNUNET_assert(0 != count);
158 LOG(GNUNET_ERROR_TYPE_DEBUG,
159 "New average of `%s' created by adding %u from %u elements: %u\n",
160 GNUNET_ATS_print_property_type (current_type),
171 * Closure for #find_min_max_it().
176 * Property we are looking for.
181 * Set to mimimum value observed.
186 * Set to maximum value observed.
193 * Function called on X to find the minimum and maximum
194 * values for a given property.
196 * @param cls the `struct FindMinMaxCtx`
197 * @param h which peer are we looking at (ignored)
198 * @param k the address for that peer
199 * @return #GNUNET_OK (continue to iterate)
202 find_min_max_it (void *cls,
203 const struct GNUNET_PeerIdentity *h,
206 struct FindMinMaxCtx *find_res = cls;
207 const struct ATS_Address *a = k;
209 find_res->max = GNUNET_MAX (find_res->max,
210 a->atsin[find_res->p->prop_type].avg);
211 find_res->min = GNUNET_MIN (find_res->min,
212 a->atsin[find_res->p->prop_type].avg);
218 * Normalize the property value for a given address based
219 * on the range we know that property value has globally.
221 * @param cls the `struct Property` with details on the
222 * property and its global range
223 * @param h which peer are we looking at (ignored)
224 * @param k the address for that peer, from where we get
225 * the original value and where we write the
227 * @return #GNUNET_OK (continue to iterate)
230 normalize_address (void *cls,
231 const struct GNUNET_PeerIdentity *h,
234 struct Property *p = cls;
235 struct ATS_Address *address = k;
240 backup = address->atsin[p->prop_type].norm;
241 avg_value = address->atsin[p->prop_type].avg;
242 delta = p->max - p->min;
243 /* max - 2 * min + avg_value / max - min */
245 address->atsin[p->prop_type].norm = (delta + (avg_value - p->min)) / (delta);
247 address->atsin[p->prop_type].norm = DEFAULT_REL_QUALITY;
249 if (backup == address->atsin[p->prop_type].norm)
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "Normalize `%s' address %p's '%s' with value %u to range [%u..%u] = %.3f\n",
254 GNUNET_i2s (&address->peer),
256 GNUNET_ATS_print_property_type (p->atsi_type),
257 address->atsin[p->prop_type].avg,
260 address->atsin[p->prop_type].norm);
261 GAS_normalized_property_changed (address,
263 address->atsin[p->prop_type].norm);
269 * Normalize @a avg_value to a range of values between [1.0, 2.0]
270 * based on min/max values currently known.
272 * @param p the property
273 * @param address the address
274 * @param avg_value the value to normalize
277 property_normalize (struct Property *p,
278 struct ATS_Address *address,
281 struct FindMinMaxCtx find_ctx;
287 find_ctx.min = UINT32_MAX;
288 addr_count = GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
297 limits_changed = GNUNET_NO;
298 if (find_ctx.max != p->max)
300 LOG (GNUNET_ERROR_TYPE_DEBUG,
301 "Normalizing %s: new maximum %u -> recalculate all values\n",
302 GNUNET_ATS_print_property_type (p->atsi_type),
304 p->max = find_ctx.max;
305 limits_changed = GNUNET_YES;
308 if ((find_ctx.min != p->min) && (find_ctx.min < p->max))
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "Normalizing %s: new minimum %u -> recalculate all values\n",
312 GNUNET_ATS_print_property_type (p->atsi_type),
315 p->min = find_ctx.min;
316 limits_changed = GNUNET_YES;
318 else if (find_ctx.min == p->max)
320 /* Only one value, so minimum has to be 0 */
324 /* Normalize the values of this property */
325 if (GNUNET_NO == limits_changed)
327 /* normalize just this address */
328 normalize_address (p,
334 /* limits changed, normalize all addresses */
335 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
343 * Update and normalize atsi performance information
345 * @param address the address to update
346 * @param atsi the array of performance information
347 * @param atsi_count the number of atsi information in the array
350 GAS_normalization_normalize_property (struct ATS_Address *address,
351 const struct GNUNET_ATS_Information *atsi,
354 struct Property *cur_prop;
357 uint32_t current_type;
358 uint32_t current_val;
359 unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "Updating %u elements for peer `%s'\n",
364 GNUNET_i2s (&address->peer));
365 GAS_plugin_solver_lock ();
366 for (c1 = 0; c1 < atsi_count; c1++)
368 current_type = ntohl (atsi[c1].type);
370 for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
372 /* Check if type is valid */
373 if (current_type == existing_properties[c2])
376 if (GNUNET_ATS_QualityPropertiesCount == c2)
378 /* Invalid property, continue with next element */
382 current_val = property_average (address, &atsi[c1]);
383 if (GNUNET_ATS_VALUE_UNDEFINED == current_val)
391 cur_prop = &properties[c2];
392 property_normalize (cur_prop,
396 GAS_plugin_solver_unlock ();
401 * Start the normalization component
404 GAS_normalization_start ()
407 unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
409 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
411 properties[c1].prop_type = c1;
412 properties[c1].atsi_type = existing_properties[c1];
413 properties[c1].min = UINT32_MAX;
414 properties[c1].max = 0;
420 * Stop the normalization component and free all items
423 GAS_normalization_stop ()
429 /* end of gnunet-service-ats_normalization.c */