2 This file is part of GNUnet.
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 rps/gnunet-service-rps_sampler.c
23 * @brief sampler implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
30 #include "gnunet-service-rps_sampler.h"
35 #define LOG(kind, ...) GNUNET_log(kind, __VA_ARGS__)
37 // multiple 'clients'?
39 // TODO check for overflows
41 // TODO align message structs
43 // hist_size_init, hist_size_max
45 /***********************************************************************
46 * WARNING: This section needs to be reviewed regarding the use of
47 * functions providing (pseudo)randomness!
48 ***********************************************************************/
50 // TODO care about invalid input of the caller (size 0 or less...)
59 * A sampler element sampling one PeerID at a time.
61 struct RPS_SamplerElement
64 * Min-wise linear permutation used by this sampler.
66 * This is an key later used by a hmac.
68 struct GNUNET_CRYPTO_AuthKey auth_key;
71 * The PeerID this sampler currently samples.
73 struct GNUNET_PeerIdentity peer_id;
76 * The according hash value of this PeerID.
78 struct GNUNET_HashCode peer_id_hash;
81 * Time of last request.
83 struct GNUNET_TIME_Absolute last_request;
86 * Flag that indicates that we are not holding a valid PeerID right now.
88 enum RPS_SamplerEmpty is_empty;
92 * Sampler with its own array of SamplerElements
97 * Number of sampler elements we hold.
99 unsigned int sampler_size;
103 * All Samplers in one array.
105 struct RPS_SamplerElement **sampler_elements;
108 * Index to a sampler element.
110 * Gets cycled on every hist_request.
112 uint64_t sampler_elem_index;
115 * Callback to be called when a peer gets inserted into a sampler.
117 RPS_sampler_insert_cb insert_cb;
120 * Closure to the insert_cb.
125 * Callback to be called when a peer gets inserted into a sampler.
127 RPS_sampler_remove_cb remove_cb;
130 * Closure to the remove_cb.
136 * Global sampler variable.
138 struct RPS_Sampler *sampler;
142 * The minimal size for the extended sampler elements.
144 static size_t min_size;
147 * The maximal size the extended sampler elements should grow to.
149 static size_t max_size;
152 * The size the extended sampler elements currently have.
154 static size_t extra_size;
157 * Inedex to the sampler element that is the next to be returned
159 static struct RPS_SamplerElement **extended_samplers_index;
164 * Only needed in the beginning to check how many of the 64 deltas
167 static unsigned int req_counter;
170 * Time of the last request we received.
172 * Used to compute the expected request rate.
174 static struct GNUNET_TIME_Absolute last_request;
177 * Last 64 deltas between requests
179 static struct GNUNET_TIME_Relative request_deltas[64];
182 * The prediction of the rate of requests
184 static struct GNUNET_TIME_Relative request_rate;
188 * Sum all time relatives of an array.
190 struct GNUNET_TIME_Relative
191 T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
193 struct GNUNET_TIME_Relative sum;
196 sum = GNUNET_TIME_UNIT_ZERO;
197 for ( i = 0 ; i < arr_size ; i++ )
199 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
205 * Compute the average of given time relatives.
207 struct GNUNET_TIME_Relative
208 T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
210 return T_relative_sum (rel_array, arr_size); // FIXME find a way to devide that by arr_size
216 * Reinitialise a previously initialised sampler element.
218 * @param sampler pointer to the memory that keeps the value.
221 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
223 sampler_el->is_empty = EMPTY;
225 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
226 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
227 &(sampler_el->auth_key.key),
228 GNUNET_CRYPTO_HASH_LENGTH);
230 sampler_el->last_request = GNUNET_TIME_UNIT_FOREVER_ABS;
232 /* We might want to keep the previous peer */
234 //GNUNET_CRYPTO_hmac(&sampler_el->auth_key, sampler_el->peer_id,
235 // sizeof(struct GNUNET_PeerIdentity),
236 // &sampler_el->peer_id_hash);
241 * (Re)Initialise given Sampler with random min-wise independent function.
243 * In this implementation this means choosing an auth_key for later use in
246 * @return a newly created RPS_SamplerElement which currently holds no id.
248 struct RPS_SamplerElement *
249 RPS_sampler_elem_create (void)
251 struct RPS_SamplerElement *s;
253 s = GNUNET_new (struct RPS_SamplerElement);
255 RPS_sampler_elem_reinit (s);
256 LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: initialised with empty PeerID\n");
263 * Input an PeerID into the given sampler.
266 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, const struct GNUNET_PeerIdentity *other,
267 RPS_sampler_insert_cb insert_cb, void *insert_cls,
268 RPS_sampler_remove_cb remove_cb, void *remove_cls)
270 struct GNUNET_HashCode other_hash;
272 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(other, &(s_elem->peer_id)) )
274 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s_elem\n",
276 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Have already PeerID %s_elem\n",
277 GNUNET_i2s(&(s_elem->peer_id)));
281 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
283 sizeof(struct GNUNET_PeerIdentity),
286 if ( EMPTY == s_elem->is_empty )
287 { // Or whatever is a valid way to say
288 // "we have no PeerID at the moment"
289 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s_elem; Simply accepting (was empty previously).\n",
291 s_elem->peer_id = *other;
292 //s_elem->peer_id = other;
293 s_elem->peer_id_hash = other_hash;
294 if (NULL != sampler->insert_cb)
296 sampler->insert_cb(sampler->insert_cls, &(s_elem->peer_id));
299 else if ( 0 > GNUNET_CRYPTO_hash_cmp(&other_hash, &s_elem->peer_id_hash) )
301 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s_elem\n",
303 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Discarding old PeerID %s_elem\n",
304 GNUNET_i2s(&s_elem->peer_id));
306 if ( NULL != sampler->remove_cb )
308 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing old PeerID %s_elem with the remove callback.\n",
309 GNUNET_i2s(&s_elem->peer_id));
310 sampler->remove_cb(sampler->remove_cls, &s_elem->peer_id);
313 memcpy(&s_elem->peer_id, other, sizeof(struct GNUNET_PeerIdentity));
314 //s_elem->peer_id = other;
315 s_elem->peer_id_hash = other_hash;
317 if ( NULL != sampler->insert_cb )
319 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Inserting new PeerID %s_elem with the insert callback.\n",
320 GNUNET_i2s(&s_elem->peer_id));
321 sampler->insert_cb(sampler->insert_cls, &s_elem->peer_id);
326 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Got PeerID %s_elem\n",
328 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Keeping old PeerID %s_elem\n",
329 GNUNET_i2s(&s_elem->peer_id));
332 s_elem->is_empty = NOT_EMPTY;
337 * Grow or shrink the size of the sampler.
339 * @param new_size the new size of the sampler
342 RPS_sampler_resize (unsigned int new_size)
344 unsigned int old_size;
346 struct RPS_SamplerElement **rem_list;
348 // TODO check min and max size
350 old_size = sampler->sampler_size;
352 if (old_size > new_size*4 &&
353 extra_size > new_size*4)
358 /* Temporary store those to properly call the removeCB on those later */
359 rem_list = GNUNET_malloc ((old_size - new_size) * sizeof (struct RPS_SamplerElement *));
361 &sampler->sampler_elements[new_size],
362 (old_size - new_size) * sizeof(struct RPS_SamplerElement *));
364 LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Shrinking sampler %d -> %d\n", old_size, new_size);
365 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size);
366 LOG (GNUNET_ERROR_TYPE_DEBUG,
367 "SAMPLER: sampler->sampler_elements now points to %p\n",
368 sampler->sampler_elements);
370 // TODO move extended_samplers_index
371 for (i = new_size ; i < old_size ; i++)
372 {/* Remove unneeded rest */
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing %" PRIX64 ". sampler\n", i);
374 if (NULL != sampler->remove_cb)
375 sampler->remove_cb (sampler->remove_cls, &rem_list[i]->peer_id);
376 GNUNET_free (rem_list[i]);
379 else if (old_size < new_size)// ||
380 //extra_size < new_size) // needed?
382 new_size *= 2; // TODO check overflow
384 LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Growing sampler %d -> %d\n", old_size, new_size);
385 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size);
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "SAMPLER: sampler->sampler_elements now points to %p\n",
388 sampler->sampler_elements);
390 // TODO move extended_samplers_index
392 for ( i = old_size ; i < new_size ; i++ )
393 { /* Add new sampler elements */
394 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
395 if (NULL != sampler->insert_cb)
396 sampler->insert_cb (sampler->insert_cls, &sampler->sampler_elements[i]->peer_id);
397 LOG (GNUNET_ERROR_TYPE_DEBUG,
398 "SAMPLER: Added %" PRIX64 ". sampler, now pointing to %p, contains %s\n",
399 i, &sampler->sampler_elements[i], GNUNET_i2s (&sampler->sampler_elements[i]->peer_id));
404 LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Size remains the same -- nothing to do\n");
408 GNUNET_assert(sampler->sampler_size == new_size);
409 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Finished growing/shrinking.\n"); // remove
414 * Initialise a tuple of sampler elements.
416 * @param init_size the size the sampler is initialised with
417 * @param id with which all newly created sampler elements are initialised
418 * @param ins_cb the callback that will be called on every PeerID that is
419 * newly inserted into a sampler element
420 * @param ins_cls the closure given to #ins_cb
421 * @param rem_cb the callback that will be called on every PeerID that is
422 * removed from a sampler element
423 * @param rem_cls the closure given to #rem_cb
426 RPS_sampler_init (size_t init_size, const struct GNUNET_PeerIdentity *id,
427 RPS_sampler_insert_cb ins_cb, void *ins_cls,
428 RPS_sampler_remove_cb rem_cb, void *rem_cls)
430 //struct RPS_Sampler *sampler;
433 /* Initialise context around extended sampler */
434 min_size = 10; // TODO make input to _samplers_init()
435 max_size = 1000; // TODO make input to _samplers_init()
436 GNUNET_new_array (64, struct GNUNET_TIME_Relative);
438 sampler = GNUNET_new (struct RPS_Sampler);
439 sampler->sampler_size = 0;
440 sampler->sampler_elements = NULL;
441 sampler->insert_cb = ins_cb;
442 sampler->insert_cls = ins_cls;
443 sampler->remove_cb = rem_cb;
444 sampler->remove_cls = rem_cls;
445 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
446 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
447 RPS_sampler_resize (init_size);
448 RPS_sampler_update_list (id); // no super nice desing but ok for the moment
450 extended_samplers_index = sampler->sampler_elements;
452 //GNUNET_assert (init_size == sampler->sampler_size);
457 * A fuction to update every sampler in the given list
459 * @param id the PeerID that is put in the sampler
462 RPS_sampler_update_list (const struct GNUNET_PeerIdentity *id)
466 for ( i = 0 ; i < sampler->sampler_size ; i++ )
467 RPS_sampler_elem_next (sampler->sampler_elements[i], id,
468 sampler->insert_cb, sampler->insert_cls,
469 sampler->remove_cb, sampler->remove_cls);
474 * Reinitialise all previously initialised sampler elements with the given value.
476 * Used to get rid of a PeerID.
478 * @param id the id of the sampler elements to update.
481 RPS_sampler_reinitialise_by_value (const struct GNUNET_PeerIdentity *id)
485 for ( i = 0 ; i < sampler->sampler_size ; i++ )
487 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
489 LOG(GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Reinitialising sampler\n");
490 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
497 * Get one random peer out of the sampled peers.
499 * We might want to reinitialise this sampler after giving the
500 * corrsponding peer to the client.
501 * Only used internally
503 const struct GNUNET_PeerIdentity *
504 RPS_sampler_get_rand_peer_ ()
507 const struct GNUNET_PeerIdentity *peer; // do we have to malloc that?
509 // TODO implement extra logic
512 * Choose the r_index of the peer we want to return
513 * at random from the interval of the gossip list
515 r_index = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG,
516 sampler->sampler_size);
518 //if ( EMPTY == sampler->sampler_elements[r_index]->is_empty )
519 // // TODO schedule for later
522 peer = &(sampler->sampler_elements[r_index]->peer_id);
523 sampler->sampler_elements[r_index]->last_request = GNUNET_TIME_absolute_get();
524 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sgrp: Returning PeerID %s\n", GNUNET_i2s(peer));
531 * Get n random peers out of the sampled peers.
533 * We might want to reinitialise this sampler after giving the
534 * corrsponding peer to the client.
535 * Random with or without consumption?
536 * Only used internally
538 const struct GNUNET_PeerIdentity *
539 RPS_sampler_get_n_rand_peers_ (uint64_t n)
541 if ( 0 == sampler->sampler_size )
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 "Sgrp: List empty - Returning NULL\n");
549 // TODO check if we have too much (distinct) sampled peers
550 // If we are not ready yet maybe schedule for later
551 struct GNUNET_PeerIdentity *peers;
554 peers = GNUNET_malloc (n * sizeof(struct GNUNET_PeerIdentity));
556 for ( i = 0 ; i < n ; i++ ) {
557 //peers[i] = RPS_sampler_get_rand_peer_(sampler->sampler_elements);
558 memcpy (&peers[i], RPS_sampler_get_rand_peer_ (), sizeof (struct GNUNET_PeerIdentity));
566 * Get one random peer out of the sampled peers.
568 * We might want to reinitialise this sampler after giving the
569 * corrsponding peer to the client.
571 * @return a random PeerID of the PeerIDs previously put into the sampler.
573 const struct GNUNET_PeerIdentity *
574 RPS_sampler_get_rand_peer ()
576 struct GNUNET_PeerIdentity *peer;
578 if (64 > req_counter)
582 memcpy (&request_deltas[1],
584 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
585 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
586 GNUNET_TIME_absolute_get ());
587 request_rate = T_relative_avg (request_deltas, req_counter);
589 last_request = GNUNET_TIME_absolute_get();
590 // TODO resize the size of the extended_samplers
592 // use _get_rand_peer_ ?
593 peer = GNUNET_new (struct GNUNET_PeerIdentity);
594 *peer = (*extended_samplers_index)->peer_id;
595 RPS_sampler_elem_reinit (*extended_samplers_index);
596 if ( extended_samplers_index == &sampler->sampler_elements[sampler->sampler_size -1] )
597 extended_samplers_index = &sampler->sampler_elements[0];
599 extended_samplers_index++;
606 * Get n random peers out of the sampled peers.
608 * We might want to reinitialise this sampler after giving the
609 * corrsponding peer to the client.
610 * Random with or without consumption?
612 * @return n random PeerIDs of the PeerIDs previously put into the sampler.
614 const struct GNUNET_PeerIdentity *
615 RPS_sampler_get_n_rand_peers (uint64_t n)
617 // use _get_rand_peers_ ?
618 if ( 0 == sampler->sampler_size )
620 LOG (GNUNET_ERROR_TYPE_DEBUG,
621 "Sgrp: List empty - Returning NULL\n");
626 // TODO check if we have too much (distinct) sampled peers
627 // If we are not ready yet maybe schedule for later
628 struct GNUNET_PeerIdentity *peers;
631 peers = GNUNET_malloc (n * sizeof(struct GNUNET_PeerIdentity));
633 for ( i = 0 ; i < n ; i++ ) {
634 //peers[i] = RPS_sampler_get_rand_peer_(sampler->sampler_elements);
635 memcpy (&peers[i], RPS_sampler_get_rand_peer (), sizeof (struct GNUNET_PeerIdentity));
643 * Counts how many Samplers currently hold a given PeerID.
645 * @param id the PeerID to count.
647 * @return the number of occurrences of id.
650 RPS_sampler_count_id (const struct GNUNET_PeerIdentity *id)
656 for ( i = 0 ; i < sampler->sampler_size ; i++ )
658 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
659 && EMPTY != sampler->sampler_elements[i]->is_empty)
667 * Cleans the sampler.
670 RPS_sampler_destroy ()
672 RPS_sampler_resize (0);
673 GNUNET_free (request_deltas); // _array_grow()?
674 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, 0);
677 /* end of gnunet-service-rps.c */