2 This file is part of GNUnet.
3 (C) 2011 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
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_normalization.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__)
37 struct PreferenceClient
42 struct PreferenceClient *prev;
48 struct PreferenceClient *next;
56 * Total preference for this peer
58 double f_abs_sum[GNUNET_ATS_PreferenceCount];
61 * List of peer preferences for this client
67 struct PreferencePeer *p_head;
72 struct PreferencePeer *p_tail;
83 struct PreferencePeer *next;
88 struct PreferencePeer *prev;
93 struct PreferenceClient *client;
98 struct GNUNET_PeerIdentity id;
101 * Absolute preference values
103 double f_abs[GNUNET_ATS_PreferenceCount];
106 * Relative preference values
108 double f_rel[GNUNET_ATS_PreferenceCount];
110 struct GNUNET_TIME_Absolute next_aging[GNUNET_ATS_PreferenceCount];
114 * Relative preferences for a peer
119 * Relative preference values
121 double f_rel[GNUNET_ATS_PreferenceCount];
126 struct GNUNET_PeerIdentity id;
130 * Callback to call on changing preference values
132 static GAS_Normalization_preference_changed_cb pref_changed_cb;
135 * Closure for callback to call on changing preference values
137 static void *pref_changed_cb_cls;
140 * Callback to call on changing property values
142 GAS_Normalization_property_changed_cb prop_ch_cb;
145 * Closure for callback to call on changing property values
147 void *prop_ch_cb_cls;
150 * Hashmap to store peer information for preference normalization
152 static struct GNUNET_CONTAINER_MultiHashMap *preference_peers;
155 * Hashmap to store peer information for property normalization
157 static struct GNUNET_CONTAINER_MultiHashMap *property_peers;
160 * Clients in DLL: head
162 static struct PreferenceClient *pc_head;
165 * Clients in DLL: tail
167 static struct PreferenceClient *pc_tail;
172 static struct PeerRelative defvalues;
174 static GNUNET_SCHEDULER_TaskIdentifier aging_task;
177 * Application Preference Normalization
183 * @param kind the kind
184 * @return the new relative preference
187 update_peers (struct GNUNET_PeerIdentity *id,
188 enum GNUNET_ATS_PreferenceKind kind)
190 struct PreferenceClient *c_cur;
191 struct PreferencePeer *p_cur;
192 struct PeerRelative *rp;
200 /* For all clients */
201 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
203 /* Find peer with id */
204 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
206 if (0 == memcmp (id, &p_cur->id, sizeof(struct GNUNET_PeerIdentity)))
211 /* Found peer with id */
212 f_rel_total += p_cur->f_rel[kind];
218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
219 "%u clients have a total relative preference for peer `%s''s `%s' of %.3f\n",
220 count, GNUNET_i2s (id), GNUNET_ATS_print_preference_type (kind),
223 != (rp = GNUNET_CONTAINER_multihashmap_get (preference_peers,
226 backup = rp->f_rel[kind];
229 rp->f_rel[kind] = f_rel_total / count;
233 rp->f_rel[kind] = DEFAULT_REL_PREFERENCE;
238 return DEFAULT_REL_PREFERENCE;
241 if ((backup != rp->f_rel[kind]) && (NULL != pref_changed_cb))
243 pref_changed_cb (pref_changed_cb_cls, &rp->id, kind, rp->f_rel[kind]);
246 return rp->f_rel[kind];
250 * Recalculate preference for a specific ATS property
252 * @param c the preference client
254 * @param kind the preference kind
258 recalculate_rel_preferences (struct PreferenceClient *c,
259 struct PreferencePeer *p, enum GNUNET_ATS_PreferenceKind kind)
261 struct PreferencePeer *p_cur;
262 struct PeerRelative *rp;
267 /* For this client: sum preferences to total preference */
268 c->f_abs_sum[kind] = 0;
269 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
270 c->f_abs_sum[kind] += p_cur->f_abs[kind];
271 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
272 "Client %p has total preference for %s of %.3f\n", c->client,
273 GNUNET_ATS_print_preference_type (kind), c->f_abs_sum[kind]);
275 ret = DEFAULT_REL_PREFERENCE;
276 /* For all peers: calculate relative preference */
277 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
279 /* Calculate relative preference for specific kind */
280 backup = p_cur->f_rel[kind];
281 if (DEFAULT_ABS_PREFERENCE == c->f_abs_sum[kind])
282 /* No peer has a preference for this property,
283 * so set default preference */
284 p_cur->f_rel[kind] = DEFAULT_REL_PREFERENCE;
286 p_cur->f_rel[kind] = (c->f_abs_sum[kind] + p_cur->f_abs[kind])
287 / c->f_abs_sum[kind];
289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
290 "Client %p: peer `%s' has relative preference for %s of %.3f\n",
291 c->client, GNUNET_i2s (&p_cur->id),
292 GNUNET_ATS_print_preference_type (kind), p_cur->f_rel[kind]);
294 if (p_cur->f_rel[kind] != backup)
296 /* Value changed, recalculate */
297 res = update_peers (&p_cur->id, kind);
298 if (0 == memcmp (&p->id, &p_cur->id, sizeof(struct GNUNET_PeerIdentity)))
303 /* Value did not chang, return old value*/
305 NULL != (rp = GNUNET_CONTAINER_multihashmap_get (preference_peers, &p->id.hashPubKey)));
306 ret = rp->f_rel[kind];
313 * Update the absolute preference value for a peer
314 * @param c the client
316 * @param kind the preference kind
317 * @param score_abs the absolute value
318 * @return the new relative preference value
321 update_preference (struct PreferenceClient *c, struct PreferencePeer *p,
322 enum GNUNET_ATS_PreferenceKind kind, float score_abs)
324 double score = score_abs;
326 /* Update preference value according to type */
329 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
330 case GNUNET_ATS_PREFERENCE_LATENCY:
331 p->f_abs[kind] = (p->f_abs[kind] + score) / 2;
332 p->next_aging[kind] = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
333 PREF_AGING_INTERVAL);
335 case GNUNET_ATS_PREFERENCE_END:
340 return recalculate_rel_preferences (c, p, kind);
344 * Reduce absolute preferences since they got old
346 * @param cls the PreferencePeer
350 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 int values_to_update;
355 struct PreferencePeer *p;
356 struct PreferenceClient *pc;
357 aging_task = GNUNET_SCHEDULER_NO_TASK;
359 values_to_update = 0;
360 for (pc = pc_head; NULL != pc; pc =pc->next);
363 for (p = pc->p_head; NULL != p; p = p->next)
366 /* Aging absolute values: */
367 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
370 if (0 == GNUNET_TIME_absolute_get_remaining(p->next_aging[i]).rel_value_us)
372 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Aging preferences for peer `%s'\n",
373 GNUNET_i2s (&p->id));
376 backup = p->f_abs[i];
377 if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE)
378 p->f_abs[i] *= PREF_AGING_FACTOR;
379 if (p->f_abs[i] <= DEFAULT_ABS_PREFERENCE + PREF_EPSILON)
380 p->f_abs[i] = DEFAULT_ABS_PREFERENCE;
381 if ((p->f_abs[i] != DEFAULT_ABS_PREFERENCE) && (backup != p->f_abs[i]))
383 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
384 "Aged preference for peer `%s' from %.3f to %.3f\n",
385 GNUNET_i2s (&p->id), backup, p->f_abs[i]);
386 recalculate_rel_preferences (p->client, p, i);
387 p->next_aging[i] = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
388 PREF_AGING_INTERVAL);
394 if (values_to_update > 0)
395 aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
396 &preference_aging, NULL);
401 * Normalize an updated preference value
403 * @param src the client with this preference
404 * @param peer the peer to change the preference for
405 * @param kind the kind to change the preference
406 * @param score_abs the normalized score
409 GAS_normalization_normalize_preference (void *src,
410 const struct GNUNET_PeerIdentity *peer,
411 enum GNUNET_ATS_PreferenceKind kind,
414 struct PreferenceClient *c_cur;
415 struct PreferencePeer *p_cur;
416 struct PeerRelative *r_cur;
419 GNUNET_assert(NULL != src);
420 GNUNET_assert(NULL != peer);
422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
423 "Client %p changes preference for peer `%s' for `%s' to %.2f\n", src,
424 GNUNET_i2s (peer), GNUNET_ATS_print_preference_type (kind), score_abs);
426 if (kind >= GNUNET_ATS_PreferenceCount)
432 /* Find preference client */
433 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
435 if (src == c_cur->client)
438 /* Not found: create new preference client */
441 c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
443 GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, c_cur);
446 /* Find entry for peer */
447 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
448 if (0 == memcmp (&p_cur->id, peer, sizeof(p_cur->id)))
451 /* Not found: create new peer entry */
454 p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
455 p_cur->client = c_cur;
457 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
459 /* Default value per peer absolut preference for a quality:
460 * No value set, so absolute preference 0 */
461 p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
462 /* Default value per peer relative preference for a quality: 1.0 */
463 p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
465 GNUNET_CONTAINER_DLL_insert(c_cur->p_head, c_cur->p_tail, p_cur);
468 if (NULL == GNUNET_CONTAINER_multihashmap_get (preference_peers,
471 r_cur = GNUNET_malloc (sizeof (struct PeerRelative));
473 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
474 r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
475 GNUNET_CONTAINER_multihashmap_put (preference_peers, &r_cur->id.hashPubKey,
476 r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
479 if (GNUNET_SCHEDULER_NO_TASK == aging_task)
480 aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
481 &preference_aging, NULL);
483 update_preference (c_cur, p_cur, kind, score_abs);
487 * Get the normalized preference values for a specific peer or
488 * the default values if
491 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind,
492 * default preferences if peer does not exist
495 GAS_normalization_get_preferences (const struct GNUNET_PeerIdentity *id)
497 GNUNET_assert(NULL != preference_peers);
498 GNUNET_assert(NULL != id);
500 struct PeerRelative *rp;
502 == (rp = GNUNET_CONTAINER_multihashmap_get (preference_peers,
505 return defvalues.f_rel;
511 * Get the normalized properties values for a specific peer or
512 * the default values if
514 * @param address the address
515 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind,
516 * default preferences if peer does not exist
519 GAS_normalization_get_properties (struct ATS_Address *address)
521 static double norm_values[GNUNET_ATS_QualityPropertiesCount];
524 GNUNET_assert(NULL != address);
526 for (i = 0; i < GNUNET_ATS_QualityPropertiesCount; i++)
528 if ((address->atsin[i].norm >= 1.0) && (address->atsin[i].norm <= 2.0))
529 norm_values[i] = address->atsin[i].norm;
531 norm_values[i] = DEFAULT_REL_QUALITY;
537 * Quality Normalization
548 struct Property properties[GNUNET_ATS_QualityPropertiesCount];
551 * Normalize a specific ATS type with the values in queue
552 * @param address the address
553 * @param atsi the ats information
554 * @return the new average or GNUNET_ATS_VALUE_UNDEFINED
558 property_average (struct ATS_Address *address,
559 const struct GNUNET_ATS_Information *atsi)
561 struct GAS_NormalizationInfo *ni;
562 uint32_t current_type;
563 uint32_t current_val;
569 unsigned int props[] = GNUNET_ATS_QualityProperties;
571 /* Average the values of this property */
572 current_type = ntohl (atsi->type);
573 current_val = ntohl (atsi->value);
575 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
577 if (current_type == props[c1])
580 if (c1 == GNUNET_ATS_QualityPropertiesCount)
583 return GNUNET_ATS_VALUE_UNDEFINED;
587 ni = &address->atsin[index];
588 ni->atsi_abs[ni->avg_queue_index] = current_val;
589 ni->avg_queue_index++;
590 if (GAS_normalization_queue_length == ni->avg_queue_index)
591 ni->avg_queue_index = 0;
595 for (c1 = 0; c1 < GAS_normalization_queue_length; c1++)
597 if (GNUNET_ATS_VALUE_UNDEFINED != ni->atsi_abs[c1])
600 if (GNUNET_ATS_VALUE_UNDEFINED > (sum + ni->atsi_abs[c1]))
601 sum += ni->atsi_abs[c1];
604 sum = GNUNET_ATS_VALUE_UNDEFINED - 1;
609 GNUNET_assert(0 != count);
611 LOG(GNUNET_ERROR_TYPE_DEBUG,
612 "New average of `%s' created by adding %u from %u elements: %u\n",
613 GNUNET_ATS_print_property_type (current_type), current_val, count, res,
627 find_min_max_it (void *cls, const struct GNUNET_HashCode *h, void *k)
629 struct ATS_Address *a = (struct ATS_Address *) k;
630 struct FindMinMaxCtx *find_res = cls;
632 if (a->atsin[find_res->p->prop_type].avg > find_res->max)
633 find_res->max = a->atsin[find_res->p->prop_type].avg;
635 if (a->atsin[find_res->p->prop_type].avg < find_res->min)
636 find_res->min = a->atsin[find_res->p->prop_type].avg;
642 normalize_address (void *cls, const struct GNUNET_HashCode *h, void *k)
644 struct Property *p = cls;
645 struct ATS_Address *address = (struct ATS_Address *) k;
648 uint32_t avg_value = address->atsin[p->prop_type].avg;
650 delta = p->max - p->min;
651 address->atsin[p->prop_type].norm = (delta + (avg_value - p->min)) / (delta);
653 LOG(GNUNET_ERROR_TYPE_DEBUG,
654 "Normalize `%s' address %p's '%s' with value %u to range [%u..%u] = %.3f\n",
655 GNUNET_i2s (&address->peer), address,
656 GNUNET_ATS_print_property_type (p->atsi_type),
657 address->atsin[p->prop_type].avg, p->min, p->max,
658 address->atsin[p->prop_type].norm);
660 if (NULL != prop_ch_cb)
661 prop_ch_cb (prop_ch_cb_cls, address, p->atsi_type,
662 address->atsin[p->prop_type].norm);
668 * Normalize avg_value to a range of values between [1.0, 2.0]
669 * based on min max values currently known.
671 * @param addresses the address hashmap
672 * @param p the property
673 * @param address the address
674 * @param avg_value the value to normalize
678 property_normalize (struct GNUNET_CONTAINER_MultiHashMap *addresses,
679 struct Property *p, struct ATS_Address *address, uint32_t avg_value)
681 struct FindMinMaxCtx find_ctx;
687 find_ctx.min = UINT32_MAX;
688 addr_count = GNUNET_CONTAINER_multihashmap_iterate (addresses,
689 &find_min_max_it, &find_ctx);
696 limits_changed = GNUNET_NO;
697 if (find_ctx.max != p->max)
699 LOG(GNUNET_ERROR_TYPE_DEBUG,
700 "Normalizing %s: new maximum %u -> recalculate all values\n",
701 GNUNET_ATS_print_property_type (p->atsi_type), find_ctx.max);
702 p->max = find_ctx.max;
703 limits_changed = GNUNET_YES;
706 if ((find_ctx.min != p->min) && (find_ctx.min < p->max))
708 LOG(GNUNET_ERROR_TYPE_DEBUG,
709 "Normalizing %s: new minimum %u -> recalculate all values\n",
710 GNUNET_ATS_print_property_type (p->atsi_type), find_ctx.min,
712 p->min = find_ctx.min;
713 limits_changed = GNUNET_YES;
715 else if (find_ctx.min == p->max)
717 /* Only one value, so minimum has to be 0 */
721 /* Normalize the values of this property */
722 if (GNUNET_NO == limits_changed)
724 /* normalize just this address */
725 normalize_address (p, &address->peer.hashPubKey, address);
730 /* limits changed, normalize all addresses */
731 GNUNET_CONTAINER_multihashmap_iterate (addresses, &normalize_address, p);
737 * Update and normalize atsi performance information
739 * @param addresses hashmap containing all addresses
740 * @param address the address to update
741 * @param atsi the array of performance information
742 * @param atsi_count the number of atsi information in the array
745 GAS_normalization_normalize_property (
746 struct GNUNET_CONTAINER_MultiHashMap *addresses,
747 struct ATS_Address *address, const struct GNUNET_ATS_Information *atsi,
750 struct Property *cur_prop;
753 uint32_t current_type;
754 uint32_t current_val;
755 unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
757 GNUNET_assert(NULL != address);
758 GNUNET_assert(NULL != atsi);
760 LOG(GNUNET_ERROR_TYPE_DEBUG, "Updating %u elements for peer `%s'\n",
761 atsi_count, GNUNET_i2s (&address->peer));
763 for (c1 = 0; c1 < atsi_count; c1++)
765 current_type = ntohl (atsi[c1].type);
767 for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
769 /* Check if type is valid */
770 if (current_type == existing_properties[c2])
773 if (GNUNET_ATS_QualityPropertiesCount == c2)
775 /* Invalid property, continue with next element */
779 current_val = property_average (address, &atsi[c1]);
780 if (GNUNET_ATS_VALUE_UNDEFINED == current_val)
788 cur_prop = &properties[c2];
789 property_normalize (addresses, cur_prop, address, current_val);
794 * Start the normalization component
796 * @param pref_ch_cb callback to call on relative preference changing
797 * @param pref_ch_cb_cls cls for the preference callback
798 * @param property_ch_cb callback to call on relative property changing
799 * @param property_ch_cb_cls cls for the property callback
802 GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb,
803 void *pref_ch_cb_cls, GAS_Normalization_property_changed_cb property_ch_cb,
804 void *property_ch_cb_cls)
808 preference_peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
809 property_peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
810 unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
812 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
814 properties[c1].prop_type = c1;
815 properties[c1].atsi_type = existing_properties[c1];
816 properties[c1].min = 0;
817 properties[c1].max = 0;
820 pref_changed_cb = pref_ch_cb;
821 pref_changed_cb_cls = pref_ch_cb_cls;
822 prop_ch_cb = property_ch_cb;
823 prop_ch_cb_cls = pref_ch_cb_cls;
825 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
826 defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE;
827 aging_task = GNUNET_SCHEDULER_NO_TASK;
836 * @param value RelativePeer
837 * @return GNUNET_OK to continue
840 free_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
842 struct PeerRelative *rp = value;
844 == GNUNET_CONTAINER_multihashmap_remove (preference_peers, key, value))
852 * Stop the normalization component and free all items
855 GAS_normalization_stop ()
857 struct PreferenceClient *pc;
858 struct PreferenceClient *next_pc;
859 struct PreferencePeer *p;
860 struct PreferencePeer *next_p;
862 if (GNUNET_SCHEDULER_NO_TASK != aging_task)
864 GNUNET_SCHEDULER_cancel (aging_task);
865 aging_task = GNUNET_SCHEDULER_NO_TASK;
869 while (NULL != (pc = next_pc))
872 GNUNET_CONTAINER_DLL_remove(pc_head, pc_tail, pc);
874 while (NULL != (p = next_p))
877 GNUNET_CONTAINER_DLL_remove(pc->p_head, pc->p_tail, p);
883 GNUNET_CONTAINER_multihashmap_iterate (preference_peers, &free_peer, NULL );
884 GNUNET_CONTAINER_multihashmap_destroy (preference_peers);
885 GNUNET_CONTAINER_multihashmap_destroy (property_peers);
889 /* end of gnunet-service-ats_normalization.c */