2 This file is part of GNUnet.
3 (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-preferencesx",__VA_ARGS__)
37 #define PREF_AGING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39 #define PREF_AGING_FACTOR 0.95
41 #define PREF_EPSILON 0.01
45 * Relative preferences for a peer
50 * Relative preference values
52 double f_rel[GNUNET_ATS_PreferenceCount];
55 * Peer identity for which we have these preferences.
57 struct GNUNET_PeerIdentity id;
64 static struct PeerRelative defvalues;
71 struct PreferenceClient
76 struct PreferenceClient *prev;
81 struct PreferenceClient *next;
89 * Array of sum of absolute preferences for this client
91 double f_abs_sum[GNUNET_ATS_PreferenceCount];
94 * Array of sum of relative preferences for this client
96 double f_rel_sum[GNUNET_ATS_PreferenceCount];
101 struct PreferencePeer *p_head;
106 struct PreferencePeer *p_tail;
113 struct PreferencePeer
118 struct PreferencePeer *next;
123 struct PreferencePeer *prev;
128 struct PreferenceClient *client;
133 struct GNUNET_PeerIdentity id;
136 * Absolute preference values for all preference types
138 double f_abs[GNUNET_ATS_PreferenceCount];
141 * Relative preference values for all preference types
143 double f_rel[GNUNET_ATS_PreferenceCount];
146 * Absolute point of time of next aging process
148 struct GNUNET_TIME_Absolute next_aging[GNUNET_ATS_PreferenceCount];
153 * Hashmap to store peer information for preference normalization
155 static struct GNUNET_CONTAINER_MultiPeerMap *preference_peers;
158 * Clients in DLL: head
160 static struct PreferenceClient *pc_head;
163 * Clients in DLL: tail
165 static struct PreferenceClient *pc_tail;
169 static struct GNUNET_SCHEDULER_Task * aging_task;
176 * @param kind the kind
177 * @param rp the relative peer struct
178 * @return the new relative preference
181 update_relative_values_for_peer (const struct GNUNET_PeerIdentity *id,
182 enum GNUNET_ATS_PreferenceKind kind,
183 struct PeerRelative *rp)
185 struct PreferenceClient *c_cur;
186 struct PreferencePeer *p_cur;
190 unsigned int peer_count;
196 /* For all clients */
197 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
199 /* For peer entries with this id */
200 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
202 f_rel_sum += p_cur->f_rel[kind];
203 if (0 == memcmp (id, &p_cur->id, sizeof(struct GNUNET_PeerIdentity)))
206 f_rel_total += p_cur->f_rel[kind];
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "%u clients have a total relative preference for peer `%s' `%s' of %.3f and for %s in total %.3f\n",
214 peer_count, GNUNET_i2s (id),
215 GNUNET_ATS_print_preference_type (kind),
217 GNUNET_ATS_print_preference_type (kind),
220 /* Find entry for the peer containing relative values in the hashmap */
223 backup = rp->f_rel[kind];
225 rp->f_rel[kind] = f_rel_total / f_rel_sum;
228 /* No client had any preferences for this type and any peer */
229 rp->f_rel[kind] = DEFAULT_REL_PREFERENCE;
231 if (backup != rp->f_rel[kind])
232 GAS_normalized_preference_changed (&rp->id, kind, rp->f_rel[kind]);
238 update_iterator (void *cls,
239 const struct GNUNET_PeerIdentity *key,
242 enum GNUNET_ATS_PreferenceKind *kind = cls;
243 struct PeerRelative *pr = value;
245 update_relative_values_for_peer (key,
254 * Recalculate preference for a specific ATS property
256 * @param c the preference client
257 * @param kind the preference kind
261 recalculate_relative_preferences (struct PreferenceClient *c,
262 enum GNUNET_ATS_PreferenceKind kind)
264 struct PreferencePeer *p_cur;
266 /* For this client: sum of absolute preference values for this preference */
267 c->f_abs_sum[kind] = 0.0;
268 /* For this client: sum of relative preference values for this preference
270 * Note: this value should also be 1.0, but:
271 * if no preferences exist due to aging, this value can be 0.0
272 * and the client can be removed */
273 c->f_rel_sum[kind] = 0.0;
275 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
276 c->f_abs_sum[kind] += p_cur->f_abs[kind];
277 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Client %p has sum of total preferences for %s of %.3f\n",
279 c->client, GNUNET_ATS_print_preference_type (kind), c->f_abs_sum[kind]);
281 /* For all peers: calculate relative preference */
282 for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next)
284 /* Calculate relative preference for specific kind */
286 /* Every application has a preference for each peer between
287 * [0 .. 1] in relative values
288 * and [0 .. inf] in absolute values */
289 p_cur->f_rel[kind] = p_cur->f_abs[kind] / c->f_abs_sum[kind];
290 c->f_rel_sum[kind] += p_cur->f_rel[kind];
292 LOG (GNUNET_ERROR_TYPE_DEBUG,
293 "Client %p has relative preference for %s for peer `%s' of %.3f\n",
295 GNUNET_ATS_print_preference_type (kind),
296 GNUNET_i2s (&p_cur->id),
305 run_preference_update (struct PreferenceClient *c_cur,
306 struct PreferencePeer *p_cur,
307 enum GNUNET_ATS_PreferenceKind kind,
312 /* Update relative value */
313 old_value = p_cur->f_rel[kind];
314 recalculate_relative_preferences (c_cur, kind);
315 if (p_cur->f_rel[kind] == old_value)
318 /* Relative preference value changed, recalculate for all peers */
319 GNUNET_CONTAINER_multipeermap_iterate (preference_peers,
328 * Reduce absolute preferences since they got old
330 * @param cls the PreferencePeer
334 preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
336 struct PreferencePeer *p;
337 struct PreferenceClient *cur_client;
339 int values_to_update;
343 values_to_update = 0;
346 for (cur_client = pc_head; NULL != cur_client; cur_client = cur_client->next)
348 for (p = cur_client->p_head; NULL != p; p = p->next)
350 /* Aging absolute values: */
351 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
354 == GNUNET_TIME_absolute_get_remaining (p->next_aging[i]).rel_value_us)
356 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
357 "Aging preference for peer `%s'\n", GNUNET_i2s (&p->id));
358 backup = p->f_abs[i];
359 if (p->f_abs[i] > DEFAULT_ABS_PREFERENCE)
360 p->f_abs[i] *= PREF_AGING_FACTOR;
362 if (p->f_abs[i] <= DEFAULT_ABS_PREFERENCE + PREF_EPSILON)
363 p->f_abs[i] = DEFAULT_ABS_PREFERENCE;
365 if ( (p->f_abs[i] != DEFAULT_ABS_PREFERENCE) &&
366 (backup != p->f_abs[i]) )
368 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
369 "Aged preference for peer `%s' from %.3f to %.3f\n",
370 GNUNET_i2s (&p->id), backup, p->f_abs[i]);
372 run_preference_update(cur_client, p, i, p->f_abs[i]);
374 p->next_aging[i] = GNUNET_TIME_absolute_add (
375 GNUNET_TIME_absolute_get (), PREF_AGING_INTERVAL);
383 if (values_to_update > 0)
385 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
386 "Rescheduling aging task due to %u elements to age\n",
388 aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
389 &preference_aging, NULL );
392 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
393 "No values to age left, not rescheduling aging task\n");
399 * Update the absolute preference value for a peer
400 * @param c the client
402 * @param kind the preference kind
403 * @param score_abs the absolute value
404 * @return the new relative preference value
407 update_abs_preference (struct PreferenceClient *c,
408 struct PreferencePeer *p,
409 enum GNUNET_ATS_PreferenceKind kind,
412 double score = score_abs;
414 /* Update preference value according to type */
417 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
418 case GNUNET_ATS_PREFERENCE_LATENCY:
419 p->f_abs[kind] = score;
420 /* p->f_abs[kind] = (p->f_abs[kind] + score) / 2; */
421 p->next_aging[kind] = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
422 PREF_AGING_INTERVAL);
424 case GNUNET_ATS_PREFERENCE_END:
434 * Change the preference for a peer
436 * @param client the client sending this request
437 * @param peer the peer id
438 * @param kind the preference kind to change
439 * @param score_abs the new preference score
442 preference_change (struct GNUNET_SERVER_Client *client,
443 const struct GNUNET_PeerIdentity *peer,
444 enum GNUNET_ATS_PreferenceKind kind,
448 GNUNET_CONTAINER_multipeermap_contains (GSA_addresses,
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Received CHANGE_PREFERENCE for unknown peer `%s'\n",
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Received CHANGE_PREFERENCE for peer `%s'\n",
459 GAS_plugin_update_preferences (client,
467 * Handle 'preference change' messages from clients.
469 * @param cls unused, NULL
470 * @param client client that sent the request
471 * @param message the request message
474 GAS_handle_preference_change (void *cls,
475 struct GNUNET_SERVER_Client *client,
476 const struct GNUNET_MessageHeader *message)
478 const struct ChangePreferenceMessage *msg;
479 const struct PreferenceInformation *pi;
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Received `%s' message\n",
486 "PREFERENCE_CHANGE");
487 msize = ntohs (message->size);
488 if (msize < sizeof (struct ChangePreferenceMessage))
491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
494 msg = (const struct ChangePreferenceMessage *) message;
495 nump = ntohl (msg->num_preferences);
497 sizeof (struct ChangePreferenceMessage) +
498 nump * sizeof (struct PreferenceInformation))
501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
504 GNUNET_STATISTICS_update (GSA_stats,
505 "# preference change requests processed",
507 pi = (const struct PreferenceInformation *) &msg[1];
508 for (i = 0; i < nump; i++)
509 preference_change (client,
511 (enum GNUNET_ATS_PreferenceKind)
512 ntohl (pi[i].preference_kind),
513 pi[i].preference_value);
514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
519 * Initialize preferences subsystem.
522 GAS_preference_init ()
526 preference_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
527 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
528 defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE;
537 * @param value RelativePeer
538 * @return #GNUNET_OK to continue
541 free_peer (void *cls,
542 const struct GNUNET_PeerIdentity *key,
545 struct PeerRelative *rp = value;
548 GNUNET_CONTAINER_multipeermap_remove (preference_peers,
559 free_client (struct PreferenceClient *pc)
561 struct PreferencePeer *next_p;
562 struct PreferencePeer *p;
565 while (NULL != (p = next_p))
568 GNUNET_CONTAINER_DLL_remove(pc->p_head, pc->p_tail, p);
576 * Shutdown preferences subsystem.
579 GAS_preference_done ()
581 struct PreferenceClient *pc;
582 struct PreferenceClient *next_pc;
584 if (NULL != aging_task)
586 GNUNET_SCHEDULER_cancel (aging_task);
590 while (NULL != (pc = next_pc))
593 GNUNET_CONTAINER_DLL_remove(pc_head, pc_tail, pc);
596 GNUNET_CONTAINER_multipeermap_iterate (preference_peers,
599 GNUNET_CONTAINER_multipeermap_destroy (preference_peers);
605 * Normalize an updated preference value
607 * @param client the client with this preference
608 * @param peer the peer to change the preference for
609 * @param kind the kind to change the preference
610 * @param score_abs the normalized score
613 GAS_normalization_normalize_preference (struct GNUNET_SERVER_Client *client,
614 const struct GNUNET_PeerIdentity *peer,
615 enum GNUNET_ATS_PreferenceKind kind,
618 struct PreferenceClient *c_cur;
619 struct PreferencePeer *p_cur;
620 struct PeerRelative *r_cur;
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
625 "Client changes preference for peer `%s' for `%s' to %.2f\n",
627 GNUNET_ATS_print_preference_type (kind),
630 if (kind >= GNUNET_ATS_PreferenceCount)
636 /* Find preference client */
637 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
639 if (client == c_cur->client)
642 /* Not found: create new preference client */
645 c_cur = GNUNET_new (struct PreferenceClient);
646 c_cur->client = client;
647 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
649 c_cur->f_abs_sum[i] = DEFAULT_ABS_PREFERENCE;
650 c_cur->f_rel_sum[i] = DEFAULT_REL_PREFERENCE;
653 GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, c_cur);
654 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding new client %p \n", c_cur);
657 /* Find entry for peer */
658 for (p_cur = c_cur->p_head; NULL != p_cur; p_cur = p_cur->next)
659 if (0 == memcmp (&p_cur->id, peer, sizeof(p_cur->id)))
662 /* Not found: create new peer entry */
665 p_cur = GNUNET_new (struct PreferencePeer);
666 p_cur->client = c_cur;
668 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
670 /* Default value per peer absolute preference for a preference: 0 */
671 p_cur->f_abs[i] = DEFAULT_ABS_PREFERENCE;
672 /* Default value per peer relative preference for a quality: 1.0 */
673 p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
674 p_cur->next_aging[i] = GNUNET_TIME_UNIT_FOREVER_ABS;
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding new peer %p for client %p \n",
678 GNUNET_CONTAINER_DLL_insert(c_cur->p_head, c_cur->p_tail, p_cur);
681 /* Create struct for peer */
682 if (NULL == GNUNET_CONTAINER_multipeermap_get (preference_peers, peer))
684 r_cur = GNUNET_new (struct PeerRelative);
686 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
687 r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE;
689 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (preference_peers,
690 &r_cur->id, r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
693 /* Update absolute value */
694 old_value = p_cur->f_abs[kind];
695 update_abs_preference (c_cur, p_cur, kind, score_abs);
696 if (p_cur->f_abs[kind] == old_value)
699 run_preference_update (c_cur, p_cur, kind, score_abs);
701 /* Start aging task */
702 if (NULL == aging_task)
703 aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL,
711 * Get the normalized preference values for a specific peer or
712 * the default values if
716 * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind,
717 * default preferences if peer does not exist
720 GAS_normalization_get_preferences_by_peer (void *cls,
721 const struct GNUNET_PeerIdentity *id)
723 GNUNET_assert(NULL != preference_peers);
724 GNUNET_assert(NULL != id);
726 struct PeerRelative *rp;
727 if (NULL == (rp = GNUNET_CONTAINER_multipeermap_get (preference_peers, id)))
729 return defvalues.f_rel;
736 * A performance client disconnected
738 * @param client the client
741 GAS_normalization_preference_client_disconnect (struct GNUNET_SERVER_Client *client)
743 struct PreferenceClient *c_cur;
744 /* Find preference client */
746 for (c_cur = pc_head; NULL != c_cur; c_cur = c_cur->next)
748 if (client == c_cur->client)
754 GNUNET_CONTAINER_DLL_remove(pc_head, pc_tail, c_cur);