2 This file is part of GNUnet.
3 Copyright (C) 2011-2015 GNUnet e.V.
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
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
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/>.
20 * @file ats/gnunet-service-ats_normalization.c
21 * @brief ats service address: management of ATS properties and preferences normalization
22 * @author Matthias Wachs
23 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_normalization.h"
30 #include "gnunet-service-ats_plugins.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__)
36 * Range information for normalization of quality properties.
41 * Minimum value we see for this property across all addresses.
43 struct GNUNET_ATS_Properties min;
46 * Maximum value we see for this property across all addresses.
48 struct GNUNET_ATS_Properties max;
53 * Range information for all quality properties we see.
55 static struct PropertyRange property_range;
59 * Add the value from @a atsi to the running average of the
60 * given @a ni quality property.
62 * @param current_val the updated value
63 * @param ni normalization information to update
66 update_avg (uint64_t current_val,
67 struct GAS_NormalizationInfo *ni)
73 ni->atsi_abs[ni->avg_queue_index++] = current_val;
74 if (GAS_normalization_queue_length == ni->avg_queue_index)
75 ni->avg_queue_index = 0;
78 for (c1 = 0; c1 < GAS_normalization_queue_length; c1++)
80 if (UINT64_MAX != ni->atsi_abs[c1])
83 sum += (double) ni->atsi_abs[c1];
87 ni->avg = current_val; /* must be UINT64_MAX */
89 ni->avg = sum / count;
94 * Function called for all addresses and peers to find the minimum and
95 * maximum (averaged) values for a given quality property. Given
96 * those, we can then calculate the normalized score.
98 * @param cls the `struct PropertyRange`
99 * @param h which peer are we looking at (ignored)
100 * @param k the address for that peer
101 * @return #GNUNET_OK (continue to iterate)
104 find_min_max_it (void *cls,
105 const struct GNUNET_PeerIdentity *h,
108 struct PropertyRange *pr = cls;
109 const struct ATS_Address *a = k;
111 pr->max.utilization_out = GNUNET_MAX (pr->max.utilization_out,
112 a->properties.utilization_out);
113 pr->max.utilization_in = GNUNET_MAX (pr->max.utilization_in,
114 a->properties.utilization_in);
115 pr->max.distance = GNUNET_MAX (pr->max.distance,
116 a->properties.distance);
117 pr->max.delay = GNUNET_TIME_relative_max (pr->max.delay,
118 a->properties.delay);
119 pr->min.utilization_out = GNUNET_MIN (pr->min.utilization_out,
120 a->properties.utilization_out);
121 pr->min.utilization_in = GNUNET_MIN (pr->min.utilization_in,
122 a->properties.utilization_in);
123 pr->min.distance = GNUNET_MIN (pr->min.distance,
124 a->properties.distance);
125 pr->min.delay = GNUNET_TIME_relative_min (pr->min.delay,
126 a->properties.delay);
132 * Compute the normalized value from the given @a ni range
133 * data and the average value.
135 * @param min minimum value
136 * @param max maximum value
137 * @param ni normalization information to update
140 update_norm (uint64_t min,
142 struct GAS_NormalizationInfo *ni)
144 /* max - 2 * min + avg_value / (max - min) */
146 ni->norm = DEFAULT_REL_QUALITY + (ni->avg - min) / (double) (max - min);
148 ni->norm = DEFAULT_REL_QUALITY;
153 * Normalize the property value for a given address based
154 * on the range we know that property values have globally.
157 * @param key which peer are we looking at (ignored)
158 * @param value the address for that peer, from where we get
159 * the original value and where we write the
161 * @return #GNUNET_OK (continue to iterate)
164 normalize_address (void *cls,
165 const struct GNUNET_PeerIdentity *key,
168 struct ATS_Address *address = value;
170 update_norm (property_range.min.delay.rel_value_us,
171 property_range.max.delay.rel_value_us,
172 &address->norm_delay);
173 update_norm (property_range.min.distance,
174 property_range.max.distance,
175 &address->norm_distance);
176 update_norm (property_range.min.utilization_in,
177 property_range.max.utilization_in,
178 &address->norm_utilization_in);
179 update_norm (property_range.min.utilization_out,
180 property_range.max.utilization_out,
181 &address->norm_utilization_out);
187 * Notify about change in normalized property.
190 * @param key which peer are we looking at (ignored)
191 * @param value the address for that peer
192 * @return #GNUNET_OK (continue to iterate)
195 notify_change (void *cls,
196 const struct GNUNET_PeerIdentity *key,
199 struct ATS_Address *address = value;
201 GAS_plugin_notify_property_changed (address);
207 * Initialize property range to the values corresponding
210 * @param pr range to initialize
213 init_range (struct PropertyRange *pr)
215 memset (pr, 0, sizeof (struct PropertyRange));
216 pr->min.utilization_out = UINT32_MAX;
217 pr->min.utilization_in = UINT32_MAX;
218 pr->min.distance = UINT32_MAX;
219 pr->min.delay = GNUNET_TIME_UNIT_FOREVER_REL;
224 * Update and normalize atsi performance information
226 * @param address the address to update
229 GAS_normalization_update_property (struct ATS_Address *address)
231 const struct GNUNET_ATS_Properties *prop = &address->properties;
232 struct PropertyRange range;
234 LOG (GNUNET_ERROR_TYPE_DEBUG,
235 "Updating properties for peer `%s'\n",
236 GNUNET_i2s (&address->peer));
237 GAS_plugin_solver_lock ();
238 update_avg (prop->delay.rel_value_us,
239 &address->norm_delay);
240 update_avg (prop->distance,
241 &address->norm_distance);
242 update_avg (prop->utilization_in,
243 &address->norm_utilization_in);
244 update_avg (prop->utilization_in,
245 &address->norm_utilization_out);
248 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
251 if (0 != memcmp (&range,
253 sizeof (struct PropertyRange)))
255 /* limits changed, (re)normalize all addresses */
256 property_range = range;
257 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
260 GNUNET_CONTAINER_multipeermap_iterate (GSA_addresses,
266 /* renormalize just this one address */
267 normalize_address (NULL,
274 GAS_plugin_solver_unlock ();
279 * Start the normalization component
282 GAS_normalization_start ()
284 init_range (&property_range);
289 * Stop the normalization component and free all items
292 GAS_normalization_stop ()
298 /* end of gnunet-service-ats_normalization.c */