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__)
38 struct PreferenceClient
43 struct PreferenceClient *prev;
49 struct PreferenceClient *next;
57 * Total preference for this peer
59 double f_abs_sum[GNUNET_ATS_PreferenceCount];
62 * List of peer preferences for this client
68 struct PreferencePeer *p_head;
73 struct PreferencePeer *p_tail;
85 struct PreferencePeer *next;
90 struct PreferencePeer *prev;
95 struct PreferenceClient *client;
100 struct GNUNET_PeerIdentity id;
103 * Absolute preference values
105 double f_abs[GNUNET_ATS_PreferenceCount];
108 * Relative preference values
110 double f_rel[GNUNET_ATS_PreferenceCount];
115 GNUNET_SCHEDULER_TaskIdentifier aging_task;
119 * Relative preferences for a peer
124 * Relative preference values
126 double f_rel[GNUNET_ATS_PreferenceCount];
131 struct GNUNET_PeerIdentity id;
136 * Callback to call on changing preference values
138 static GAS_Normalization_preference_changed_cb pref_changed_cb;
142 * Closure for callback to call on changing preference values
144 static void *pref_changed_cb_cls;
148 * Callback to call on changing property values
150 GAS_Normalization_property_changed_cb prop_ch_cb;
153 * Closure for callback to call on changing property values
155 void *prop_ch_cb_cls;
159 * Hashmap to store peer information for preference normalization
161 static struct GNUNET_CONTAINER_MultiHashMap *preference_peers;
166 * Hashmap to store peer information for property normalization
168 static struct GNUNET_CONTAINER_MultiHashMap *property_peers;
173 * Clients in DLL: head
175 static struct PreferenceClient *pc_head;
179 * Clients in DLL: tail
181 static struct PreferenceClient *pc_tail;
187 static struct PeerRelative defvalues;
190 * Application Preference Normalization
197 * @param kind the kind
198 * @return the new relative preference
201 update_peers (struct GNUNET_PeerIdentity *id,
202 enum GNUNET_ATS_PreferenceKind kind)
204 struct PreferenceClient *c_cur;
205 struct PreferencePeer *p_cur;
206 struct PeerRelative *rp;
214 /* For all clients */
215 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
217 /* Find peer with id */
218 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
220 if (0 == memcmp (id, &p_cur->id, sizeof (struct GNUNET_PeerIdentity)))
225 /* Found peer with id */
226 f_rel_total += p_cur->f_rel[kind];
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "%u clients have a total relative preference for peer `%s''s `%s' of %.3f\n",
236 GNUNET_ATS_print_preference_type (kind),
238 if (NULL != (rp = GNUNET_CONTAINER_multihashmap_get (preference_peers, &id->hashPubKey)))
240 backup = rp->f_rel[kind];
243 rp->f_rel[kind] = f_rel_total / count;
247 rp->f_rel[kind] = DEFAULT_REL_PREFERENCE;
252 return DEFAULT_REL_PREFERENCE;
255 if ((backup != rp->f_rel[kind]) && (NULL != pref_changed_cb))
257 pref_changed_cb (pref_changed_cb_cls, &rp->id, kind, rp->f_rel[kind]);
260 return rp->f_rel[kind];
265 * Recalculate preference for a specific ATS property
267 * @param c the preference client
269 * @param kind the preference kind
273 recalculate_rel_preferences (struct PreferenceClient *c,
274 struct PreferencePeer *p,
275 enum GNUNET_ATS_PreferenceKind kind)
277 struct PreferencePeer *p_cur;
278 struct PeerRelative *rp;
283 /* For this client: sum preferences to total preference */
284 c->f_abs_sum[kind] = 0;
285 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
286 c->f_abs_sum[kind] += p_cur->f_abs[kind];
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Client %p has total preference for %s of %.3f\n",
290 GNUNET_ATS_print_preference_type (kind),
293 ret = DEFAULT_REL_PREFERENCE;
294 /* For all peers: calculate relative preference */
295 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
297 /* Calculate relative preference for specific kind */
298 backup = p_cur->f_rel[kind];
299 if (DEFAULT_ABS_PREFERENCE == c->f_abs_sum[kind])
300 /* No peer has a preference for this property,
301 * so set default preference */
302 p_cur->f_rel[kind] = DEFAULT_REL_PREFERENCE;
304 p_cur->f_rel[kind] = (c->f_abs_sum[kind] + p_cur->f_abs[kind]) /
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Client %p: peer `%s' has relative preference for %s of %.3f\n",
310 GNUNET_i2s (&p_cur->id),
311 GNUNET_ATS_print_preference_type (kind),
314 if (p_cur->f_rel[kind] != backup)
316 /* Value changed, recalculate */
317 res = update_peers (&p_cur->id,kind);
318 if (0 == memcmp (&p->id, &p_cur->id, sizeof (struct GNUNET_PeerIdentity)))
323 /* Value did not chang, return old value*/
324 GNUNET_assert (NULL != (rp = GNUNET_CONTAINER_multihashmap_get (preference_peers,
325 &p->id.hashPubKey)));
326 ret = rp->f_rel[kind];
334 * Update the absolute preference value for a peer
335 * @param c the client
337 * @param kind the preference kind
338 * @param score_abs the absolute value
339 * @return the new relative preference value
342 update_preference (struct PreferenceClient *c,
343 struct PreferencePeer *p,
344 enum GNUNET_ATS_PreferenceKind kind,
347 double score = score_abs;
349 /* Update preference value according to type */
351 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
352 case GNUNET_ATS_PREFERENCE_LATENCY:
353 p->f_abs[kind] = (p->f_abs[kind] + score) / 2;
355 case GNUNET_ATS_PREFERENCE_END:
360 return recalculate_rel_preferences (c, p, kind);
365 * Reduce absolute preferences since they got old
367 * @param cls the PreferencePeer
371 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 struct PreferencePeer *p = cls;
376 GNUNET_assert (NULL != p);
378 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n",
381 GNUNET_i2s (&p->id));
383 /* Aging absolute values: */
384 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
386 backup = p->f_abs[i];
387 if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE)
388 p->f_abs[i] *= PREF_AGING_FACTOR;
389 if (backup != p->f_abs[i])
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Aged preference for peer `%s' from %.3f to %.3f\n",
392 GNUNET_i2s (&p->id), backup, p->f_abs[i]);
393 recalculate_rel_preferences (p->client, p, i);
396 p->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
397 &preference_aging, p);
402 * Normalize an updated preference value
404 * @param src the client with this preference
405 * @param peer the peer to change the preference for
406 * @param kind the kind to change the preference
407 * @param score_abs the normalized score
410 GAS_normalization_normalize_preference (void *src,
411 const struct GNUNET_PeerIdentity *peer,
412 enum GNUNET_ATS_PreferenceKind kind,
415 struct PreferenceClient *c_cur;
416 struct PreferencePeer *p_cur;
417 struct PeerRelative *r_cur;
421 GNUNET_assert (NULL != src);
422 GNUNET_assert (NULL != peer);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Client %p changes preference for peer `%s' for `%s' to %.2f\n",
428 GNUNET_ATS_print_preference_type (kind),
431 if (kind >= GNUNET_ATS_PreferenceCount)
437 /* Find preference client */
438 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
440 if (src == c_cur->client)
443 /* Not found: create new preference client */
446 c_cur = GNUNET_malloc (sizeof (struct PreferenceClient));
448 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, c_cur);
451 /* Find entry for peer */
452 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
453 if (0 == memcmp (&p_cur->id, peer, sizeof (p_cur->id)))
456 /* Not found: create new peer entry */
459 p_cur = GNUNET_malloc (sizeof (struct PreferencePeer));
460 p_cur->client = c_cur;
462 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
464 /* Default value per peer absolut preference for a quality:
465 * No value set, so absolute preference 0 */
466 p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
467 /* Default value per peer relative preference for a quality: 1.0 */
468 p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
470 p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
471 &preference_aging, p_cur);
472 GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur);
475 if (NULL == GNUNET_CONTAINER_multihashmap_get (preference_peers,
478 r_cur = GNUNET_malloc (sizeof (struct PeerRelative));
480 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
481 r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
482 GNUNET_CONTAINER_multihashmap_put (preference_peers, &r_cur->id.hashPubKey,
483 r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
485 update_preference (c_cur, p_cur, kind, score_abs);
490 * Get the normalized preference values for a specific peer or
491 * the default values if
494 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind,
495 * default preferences if peer does not exist
498 GAS_normalization_get_preferences (const struct GNUNET_PeerIdentity *id)
500 GNUNET_assert (NULL != preference_peers);
501 GNUNET_assert (NULL != id);
503 struct PeerRelative *rp;
504 if (NULL == (rp = GNUNET_CONTAINER_multihashmap_get (preference_peers, &id->hashPubKey)))
506 return defvalues.f_rel;
513 * Get the normalized properties values for a specific peer or
514 * the default values if
516 * @param address the address
517 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind,
518 * default preferences if peer does not exist
521 GAS_normalization_get_properties (struct ATS_Address *address)
523 static double norm_values[GNUNET_ATS_QualityPropertiesCount];
526 GNUNET_assert (NULL != address);
528 for (i = 0; i < GNUNET_ATS_QualityPropertiesCount; i++)
530 if ((address->atsin[i].norm >= 1.0) &&
531 (address->atsin[i].norm <= 2.0))
532 norm_values[i] = address->atsin[i].norm;
534 norm_values[i] = DEFAULT_REL_QUALITY;
541 * Quality Normalization
552 struct Property properties[GNUNET_ATS_QualityPropertiesCount];
555 * Normalize a specific ATS type with the values in queue
556 * @param address the address
557 * @param atsi the ats information
558 * @return the new average or GNUNET_ATS_VALUE_UNDEFINED
562 property_average (struct ATS_Address *address,
563 const struct GNUNET_ATS_Information *atsi)
565 struct GAS_NormalizationInfo *ni;
566 uint32_t current_type;
567 uint32_t current_val;
573 unsigned int props[] = GNUNET_ATS_QualityProperties;
575 /* Average the values of this property */
576 current_type = ntohl (atsi->type);
577 current_val = ntohl (atsi->value);
579 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
581 if (current_type == props[c1])
584 if (c1 == GNUNET_ATS_QualityPropertiesCount)
587 return GNUNET_ATS_VALUE_UNDEFINED;
591 ni = &address->atsin[index];
592 ni->atsi_abs[ni->avg_queue_index] = current_val;
593 ni->avg_queue_index ++;
594 if (GAS_normalization_queue_length == ni->avg_queue_index)
595 ni->avg_queue_index = 0;
599 for (c1 = 0; c1 < GAS_normalization_queue_length; c1++)
601 if (GNUNET_ATS_VALUE_UNDEFINED != ni->atsi_abs[c1])
604 if (GNUNET_ATS_VALUE_UNDEFINED > (sum + ni->atsi_abs[c1]))
605 sum += ni->atsi_abs[c1];
608 sum = GNUNET_ATS_VALUE_UNDEFINED - 1;
613 GNUNET_assert (0 != count);
615 LOG (GNUNET_ERROR_TYPE_DEBUG, "New average of `%s' created by adding %u from %u elements: %u\n",
616 GNUNET_ATS_print_property_type(current_type),
617 current_val, count, res , sum);
631 find_min_max_it (void *cls, const struct GNUNET_HashCode *h, void *k)
633 struct ATS_Address *a = (struct ATS_Address *) k;
634 struct FindMinMaxCtx *find_res = cls;
636 if (a->atsin[find_res->p->prop_type].avg > find_res->max)
637 find_res->max = a->atsin[find_res->p->prop_type].avg;
639 if (a->atsin[find_res->p->prop_type].avg < find_res->min)
640 find_res->min = a->atsin[find_res->p->prop_type].avg;
647 normalize_address (void *cls, const struct GNUNET_HashCode *h, void *k)
649 struct Property *p = cls;
650 struct ATS_Address *address = (struct ATS_Address *) k;
653 uint32_t avg_value = address->atsin[p->prop_type].avg;
655 delta = p->max - p->min;
656 address->atsin[p->prop_type].norm = (delta + (avg_value - p->min)) / (delta);
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "Normalize `%s' address %p's '%s' with value %u to range [%u..%u] = %.3f\n",
659 GNUNET_i2s (&address->peer),
661 GNUNET_ATS_print_property_type (p->atsi_type),
662 address->atsin[p->prop_type].avg,
664 address->atsin[p->prop_type].norm );
666 if (NULL != prop_ch_cb)
667 prop_ch_cb (prop_ch_cb_cls, address, p->atsi_type,
668 address->atsin[p->prop_type].norm);
676 * Normalize avg_value to a range of values between [1.0, 2.0]
677 * based on min max values currently known.
679 * @param addresses the address hashmap
680 * @param p the property
681 * @param address the address
682 * @param avg_value the value to normalize
686 property_normalize (struct GNUNET_CONTAINER_MultiHashMap *addresses,
688 struct ATS_Address *address,
691 struct FindMinMaxCtx find_ctx;
697 find_ctx.min = UINT32_MAX;
698 addr_count = GNUNET_CONTAINER_multihashmap_iterate(addresses, &find_min_max_it, &find_ctx);
706 limits_changed = GNUNET_NO;
707 if (find_ctx.max != p->max)
709 LOG (GNUNET_ERROR_TYPE_DEBUG, "Normalizing %s: new maximum %u -> recalculate all values\n",
710 GNUNET_ATS_print_property_type (p->atsi_type),
712 p->max = find_ctx.max;
713 limits_changed = GNUNET_YES;
716 if ((find_ctx.min != p->min) && (find_ctx.min < p->max))
718 LOG (GNUNET_ERROR_TYPE_DEBUG, "Normalizing %s: new minimum %u -> recalculate all values\n",
719 GNUNET_ATS_print_property_type (p->atsi_type),
720 find_ctx.min, find_ctx.max);
721 p->min = find_ctx.min;
722 limits_changed = GNUNET_YES;
724 else if (find_ctx.min == p->max)
726 /* Only one value, so minimum has to be 0 */
730 /* Normalize the values of this property */
731 if (GNUNET_NO == limits_changed)
733 /* normalize just this address */
734 normalize_address (p, &address->peer.hashPubKey, address);
739 /* limits changed, normalize all addresses */
740 GNUNET_CONTAINER_multihashmap_iterate(addresses, &normalize_address, p);
747 * Update and normalize atsi performance information
749 * @param addresses hashmap containing all addresses
750 * @param address the address to update
751 * @param atsi the array of performance information
752 * @param atsi_count the number of atsi information in the array
755 GAS_normalization_normalize_property (struct GNUNET_CONTAINER_MultiHashMap *addresses,
756 struct ATS_Address *address,
757 const struct GNUNET_ATS_Information *atsi,
760 struct Property *cur_prop;
763 uint32_t current_type;
764 uint32_t current_val;
765 unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
767 GNUNET_assert (NULL != address);
768 GNUNET_assert (NULL != atsi);
770 LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating %u elements for peer `%s'\n",
771 atsi_count, GNUNET_i2s (&address->peer));
773 for (c1 = 0; c1 < atsi_count; c1++)
775 current_type = ntohl (atsi[c1].type);
777 for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
779 /* Check if type is valid */
780 if (current_type == existing_properties[c2])
783 if (GNUNET_ATS_QualityPropertiesCount == c2)
785 /* Invalid property, continue with next element */
789 current_val = property_average (address, &atsi[c1]);
790 if (GNUNET_ATS_VALUE_UNDEFINED == current_val)
798 cur_prop = &properties[c2];
799 property_normalize (addresses, cur_prop, address, current_val);
805 * Start the normalization component
807 * @param pref_ch_cb callback to call on relative preference changing
808 * @param pref_ch_cb_cls cls for the preference callback
809 * @param property_ch_cb callback to call on relative property changing
810 * @param property_ch_cb_cls cls for the property callback
813 GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb,
814 void *pref_ch_cb_cls,
815 GAS_Normalization_property_changed_cb property_ch_cb,
816 void *property_ch_cb_cls)
820 preference_peers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
821 property_peers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
822 unsigned int existing_properties[] = GNUNET_ATS_QualityProperties;
824 for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++)
826 properties[c1].prop_type = c1;
827 properties[c1].atsi_type = existing_properties[c1];
828 properties[c1].min = 0;
829 properties[c1].max = 0;
832 pref_changed_cb = pref_ch_cb;
833 pref_changed_cb_cls = pref_ch_cb_cls;
834 prop_ch_cb = property_ch_cb;
835 prop_ch_cb_cls = pref_ch_cb_cls;
837 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
838 defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE;
848 * @param value RelativePeer
849 * @return GNUNET_OK to continue
852 free_peer (void *cls,
853 const struct GNUNET_HashCode * key,
856 struct PeerRelative *rp = value;
857 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (preference_peers, key, value))
866 * Stop the normalization component and free all items
869 GAS_normalization_stop ()
871 struct PreferenceClient *pc;
872 struct PreferenceClient *next_pc;
873 struct PreferencePeer *p;
874 struct PreferencePeer *next_p;
877 while (NULL != (pc = next_pc))
880 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
882 while (NULL != (p = next_p))
885 if (GNUNET_SCHEDULER_NO_TASK != p->aging_task)
887 GNUNET_SCHEDULER_cancel(p->aging_task);
888 p->aging_task = GNUNET_SCHEDULER_NO_TASK;
890 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
895 GNUNET_CONTAINER_multihashmap_iterate (preference_peers, &free_peer, NULL);
896 GNUNET_CONTAINER_multihashmap_destroy (preference_peers);
897 GNUNET_CONTAINER_multihashmap_destroy (property_peers);
901 /* end of gnunet-service-ats_normalization.c */