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.
21 * @file ats/gnunet-service-ats_preferences.c
22 * @brief manage preferences expressed by clients
23 * @author Matthias Wachs
24 * @author Christian Grothoff
27 #include "gnunet-service-ats.h"
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_performance.h"
30 #include "gnunet-service-ats_plugins.h"
31 #include "gnunet-service-ats_preferences.h"
32 #include "gnunet-service-ats_reservations.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "ats-preferences",__VA_ARGS__)
38 * How frequently do we age preference values?
40 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
43 * By which factor do we age preferences expressed during
44 * each #PREF_AGING_INTERVAL?
46 #define PREF_AGING_FACTOR 0.95
49 * What is the lowest threshold up to which prefernce values
50 * are aged, and below which we consider them zero and thus
51 * no longer subject to aging?
53 #define PREF_EPSILON 0.01
57 * Relative preferences for a peer
62 * Relative preference values
64 double f_rel[GNUNET_ATS_PreferenceCount];
67 * Peer identity for which we have these preferences.
69 struct GNUNET_PeerIdentity id;
76 static struct PeerRelative defvalues;
80 * Preference client, information we keep track of per client.
82 struct PreferenceClient
87 struct PreferenceClient *prev;
92 struct PreferenceClient *next;
97 struct PreferencePeer *p_head;
102 struct PreferencePeer *p_tail;
107 struct GNUNET_SERVER_Client *client;
110 * Array of sum of absolute preferences for this client
112 double f_abs_sum[GNUNET_ATS_PreferenceCount];
115 * Array of sum of relative preferences for this client
117 double f_rel_sum[GNUNET_ATS_PreferenceCount];
122 * Preference information per peer and client.
124 struct PreferencePeer
127 * Next in DLL of preference entries for the same client.
129 struct PreferencePeer *next;
132 * Previous in DLL of preference entries for the same client.
134 struct PreferencePeer *prev;
137 * Client that expressed this preferences.
139 struct PreferenceClient *client;
142 * Peer identity for which the preference was expressed.
144 struct GNUNET_PeerIdentity id;
147 * Absolute preference values for all preference types
149 double f_abs[GNUNET_ATS_PreferenceCount];
152 * Relative preference values for all preference types
154 double f_rel[GNUNET_ATS_PreferenceCount];
157 * Absolute point of time of next aging process
159 struct GNUNET_TIME_Absolute next_aging[GNUNET_ATS_PreferenceCount];
164 * Hashmap to store peer information for preference normalization.
165 * Maps the identity of a peer to a `struct PeerRelative` containing
166 * the current relative preference values for that peer.
168 static struct GNUNET_CONTAINER_MultiPeerMap *preference_peers;
171 * Clients in DLL: head
173 static struct PreferenceClient *pc_head;
176 * Clients in DLL: tail
178 static struct PreferenceClient *pc_tail;
181 * Handle for task we run periodically to age preferences over time.
183 static struct GNUNET_SCHEDULER_Task *aging_task;
190 * @param kind the kind
191 * @param rp the relative peer struct
192 * @return the new relative preference
195 update_relative_values_for_peer (const struct GNUNET_PeerIdentity *id,
196 enum GNUNET_ATS_PreferenceKind kind,
197 struct PeerRelative *rp)
199 struct PreferenceClient *c_cur;
200 struct PreferencePeer *p_cur;
204 unsigned int peer_count;
210 /* For all clients */
211 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
213 /* For peer entries with this id */
214 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
216 f_rel_sum += p_cur->f_rel[kind];
217 if (0 == memcmp (id, &p_cur->id, sizeof(struct GNUNET_PeerIdentity)))
220 f_rel_total += p_cur->f_rel[kind];
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "%u clients have a total relative preference for peer `%s' `%s' of %.3f and for %s in total %.3f\n",
229 GNUNET_ATS_print_preference_type (kind),
231 GNUNET_ATS_print_preference_type (kind),
234 /* Find entry for the peer containing relative values in the hashmap */
237 backup = rp->f_rel[kind];
239 rp->f_rel[kind] = f_rel_total / f_rel_sum;
242 /* No client had any preferences for this type and any peer */
243 rp->f_rel[kind] = DEFAULT_REL_PREFERENCE;
245 if (backup != rp->f_rel[kind])
246 GAS_plugin_notify_preference_changed (&rp->id, kind,
253 * Recalculate preference for a specific ATS property
255 * @param c the preference client
256 * @param kind the preference kind
260 recalculate_relative_preferences (struct PreferenceClient *c,
261 enum GNUNET_ATS_PreferenceKind kind)
263 struct PreferencePeer *p_cur;
265 /* For this client: sum of absolute preference values for this preference */
266 c->f_abs_sum[kind] = 0.0;
267 /* For this client: sum of relative preference values for this preference
269 * Note: this value should also be 1.0, but:
270 * if no preferences exist due to aging, this value can be 0.0
271 * and the client can be removed */
272 c->f_rel_sum[kind] = 0.0;
274 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
275 c->f_abs_sum[kind] += p_cur->f_abs[kind];
276 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Client %p has sum of total preferences for %s of %.3f\n",
279 GNUNET_ATS_print_preference_type (kind),
282 /* For all peers: calculate relative preference */
283 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
285 /* Calculate relative preference for specific kind */
287 /* Every application has a preference for each peer between
288 * [0 .. 1] in relative values
289 * and [0 .. inf] in absolute values */
290 p_cur->f_rel[kind] = p_cur->f_abs[kind] / c->f_abs_sum[kind];
291 c->f_rel_sum[kind] += p_cur->f_rel[kind];
293 LOG (GNUNET_ERROR_TYPE_DEBUG,
294 "Client %p has relative preference for %s for peer `%s' of %.3f\n",
296 GNUNET_ATS_print_preference_type (kind),
297 GNUNET_i2s (&p_cur->id),
307 update_iterator (void *cls,
308 const struct GNUNET_PeerIdentity *key,
311 enum GNUNET_ATS_PreferenceKind *kind = cls;
312 struct PeerRelative *pr = value;
314 update_relative_values_for_peer (key,
326 run_preference_update (struct PreferenceClient *c_cur,
327 struct PreferencePeer *p_cur,
328 enum GNUNET_ATS_PreferenceKind kind,
333 /* Update relative value */
334 old_value = p_cur->f_rel[kind];
335 recalculate_relative_preferences (c_cur, kind);
336 if (p_cur->f_rel[kind] == old_value)
339 /* Relative preference value changed, recalculate for all peers */
340 GNUNET_CONTAINER_multipeermap_iterate (preference_peers,
347 * Reduce absolute preferences since they got old
349 * @param cls the PreferencePeer
353 preference_aging (void *cls,
354 const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct PreferencePeer *p;
357 struct PreferenceClient *cur_client;
359 unsigned int values_to_update;
363 values_to_update = 0;
366 for (cur_client = pc_head; NULL != cur_client; cur_client = cur_client->next)
368 for (p = cur_client->p_head; NULL != p; p = p->next)
370 /* Aging absolute values: */
371 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
374 == GNUNET_TIME_absolute_get_remaining (p->next_aging[i]).rel_value_us)
376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
377 "Aging preference for peer `%s'\n", GNUNET_i2s (&p->id));
378 backup = p->f_abs[i];
379 if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE)
380 p->f_abs[i] *= PREF_AGING_FACTOR;
382 if (p->f_abs[i] <= DEFAULT_ABS_PREFERENCE + PREF_EPSILON)
383 p->f_abs[i] = DEFAULT_ABS_PREFERENCE;
385 if ( (p->f_abs[i] != DEFAULT_ABS_PREFERENCE) &&
386 (backup != p->f_abs[i]) )
388 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
389 "Aged preference for peer `%s' from %.3f to %.3f\n",
390 GNUNET_i2s (&p->id), backup, p->f_abs[i]);
392 run_preference_update(cur_client, p, i, p->f_abs[i]);
394 p->next_aging[i] = GNUNET_TIME_absolute_add (
395 GNUNET_TIME_absolute_get (), PREF_AGING_INTERVAL);
403 if (values_to_update > 0)
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Rescheduling aging task due to %u elements to age\n",
408 aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "No values to age left, not rescheduling aging task\n");
421 * Update the absolute preference value for a peer
422 * @param c the client
424 * @param kind the preference kind
425 * @param score_abs the absolute value
426 * @return the new relative preference value
429 update_abs_preference (struct PreferenceClient *c,
430 struct PreferencePeer *p,
431 enum GNUNET_ATS_PreferenceKind kind,
434 double score = score_abs;
436 /* Update preference value according to type */
439 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
440 case GNUNET_ATS_PREFERENCE_LATENCY:
441 p->f_abs[kind] = score;
442 /* p->f_abs[kind] = (p->f_abs[kind] + score) / 2; */
443 p->next_aging[kind] = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
444 PREF_AGING_INTERVAL);
446 case GNUNET_ATS_PREFERENCE_END:
455 * Normalize an updated preference value
457 * @param client the client with this preference
458 * @param peer the peer to change the preference for
459 * @param kind the kind to change the preference
460 * @param score_abs the normalized score
463 normalize_preference (struct GNUNET_SERVER_Client *client,
464 const struct GNUNET_PeerIdentity *peer,
465 enum GNUNET_ATS_PreferenceKind kind,
468 struct PreferenceClient *c_cur;
469 struct PreferencePeer *p_cur;
470 struct PeerRelative *r_cur;
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 "Client changes preference for peer `%s' for `%s' to %.2f\n",
477 GNUNET_ATS_print_preference_type (kind),
480 if (kind >= GNUNET_ATS_PreferenceCount)
486 /* Find preference client */
487 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
488 if (client == c_cur->client)
490 /* Not found: create new preference client */
493 c_cur = GNUNET_new (struct PreferenceClient);
494 c_cur->client = client;
495 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
497 c_cur->f_abs_sum[i] = DEFAULT_ABS_PREFERENCE;
498 c_cur->f_rel_sum[i] = DEFAULT_REL_PREFERENCE;
500 GNUNET_CONTAINER_DLL_insert (pc_head,
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "Adding new client %p\n",
508 /* Find entry for peer */
509 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
510 if (0 == memcmp (&p_cur->id,
515 /* Not found: create new peer entry */
518 p_cur = GNUNET_new (struct PreferencePeer);
519 p_cur->client = c_cur;
521 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
523 /* Default value per peer absolute preference for a preference: 0 */
524 p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
525 /* Default value per peer relative preference for a quality: 1.0 */
526 p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
527 p_cur->next_aging[i] = GNUNET_TIME_UNIT_FOREVER_ABS;
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Adding new peer %p for client %p\n",
533 GNUNET_CONTAINER_DLL_insert (c_cur->p_head,
538 /* Create struct for peer */
540 GNUNET_CONTAINER_multipeermap_get (preference_peers,
543 r_cur = GNUNET_new (struct PeerRelative);
545 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
546 r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
547 GNUNET_assert (GNUNET_OK ==
548 GNUNET_CONTAINER_multipeermap_put (preference_peers,
551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
554 /* Update absolute value */
555 old_value = p_cur->f_abs[kind];
556 update_abs_preference (c_cur, p_cur, kind, score_abs);
557 if (p_cur->f_abs[kind] == old_value)
560 GAS_plugin_solver_lock ();
561 run_preference_update (c_cur,
565 GAS_plugin_solver_unlock ();
567 if (NULL == aging_task)
568 aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
575 * Handle 'preference change' messages from clients.
577 * @param cls unused, NULL
578 * @param client client that sent the request
579 * @param message the request message
582 GAS_handle_preference_change (void *cls,
583 struct GNUNET_SERVER_Client *client,
584 const struct GNUNET_MessageHeader *message)
586 const struct ChangePreferenceMessage *msg;
587 const struct PreferenceInformation *pi;
592 msize = ntohs (message->size);
593 if (msize < sizeof (struct ChangePreferenceMessage))
596 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
599 msg = (const struct ChangePreferenceMessage *) message;
600 nump = ntohl (msg->num_preferences);
602 sizeof (struct ChangePreferenceMessage) +
603 nump * sizeof (struct PreferenceInformation)) ||
604 (UINT16_MAX / sizeof (struct PreferenceInformation) < nump) )
607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Received PREFERENCE_CHANGE message for peer `%s'\n",
612 GNUNET_i2s (&msg->peer));
613 GNUNET_STATISTICS_update (GSA_stats,
614 "# preference change requests processed",
616 pi = (const struct PreferenceInformation *) &msg[1];
617 for (i = 0; i < nump; i++)
618 normalize_preference (client,
620 (enum GNUNET_ATS_PreferenceKind)
621 ntohl (pi[i].preference_kind),
622 pi[i].preference_value);
623 GNUNET_SERVER_receive_done (client, GNUNET_OK);
628 * Initialize preferences subsystem.
631 GAS_preference_init ()
635 preference_peers = GNUNET_CONTAINER_multipeermap_create (16,
637 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
638 defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE;
643 * Free a peer's `struct PeerRelative`.
647 * @param value the `struct PeerRelative` to free.
648 * @return #GNUNET_OK to continue
651 free_peer (void *cls,
652 const struct GNUNET_PeerIdentity *key,
655 struct PeerRelative *rp = value;
657 GNUNET_assert (GNUNET_YES ==
658 GNUNET_CONTAINER_multipeermap_remove (preference_peers,
667 free_client (struct PreferenceClient *pc)
669 struct PreferencePeer *next_p;
670 struct PreferencePeer *p;
673 while (NULL != (p = next_p))
676 GNUNET_CONTAINER_DLL_remove (pc->p_head,
686 * Shutdown preferences subsystem.
689 GAS_preference_done ()
691 struct PreferenceClient *pc;
692 struct PreferenceClient *next_pc;
694 if (NULL != aging_task)
696 GNUNET_SCHEDULER_cancel (aging_task);
700 while (NULL != (pc = next_pc))
703 GNUNET_CONTAINER_DLL_remove (pc_head,
708 GNUNET_CONTAINER_multipeermap_iterate (preference_peers,
711 GNUNET_CONTAINER_multipeermap_destroy (preference_peers);
717 * Get the normalized preference values for a specific peer or
718 * the default values if
722 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind,
723 * default preferences if peer does not exist
726 GAS_preference_get_by_peer (void *cls,
727 const struct GNUNET_PeerIdentity *id)
729 struct PeerRelative *rp;
732 (rp = GNUNET_CONTAINER_multipeermap_get (preference_peers,
735 return defvalues.f_rel;
742 * A performance client disconnected
744 * @param client the client
747 GAS_preference_client_disconnect (struct GNUNET_SERVER_Client *client)
749 struct PreferenceClient *c_cur;
751 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
752 if (client == c_cur->client)
756 GNUNET_CONTAINER_DLL_remove (pc_head,
763 /* end of gnunet-service-ats_preferences.c */