aboutsummaryrefslogtreecommitdiff
path: root/src/rps
diff options
context:
space:
mode:
authorJulius Bünger <buenger@mytum.de>2015-01-09 11:13:56 +0000
committerJulius Bünger <buenger@mytum.de>2015-01-09 11:13:56 +0000
commit8215376b2d1b4a3d95a0cf1ba474cf4be437c1b0 (patch)
treebfbc2d5958af13ee533ed3ab2e53f5716cffdb18 /src/rps
parentb191726bb6653b5fd08305b87951b7d3c8cdf0fb (diff)
downloadgnunet-8215376b2d1b4a3d95a0cf1ba474cf4be437c1b0.tar.gz
gnunet-8215376b2d1b4a3d95a0cf1ba474cf4be437c1b0.zip
moved computation of request rate out of sampler
Diffstat (limited to 'src/rps')
-rw-r--r--src/rps/gnunet-service-rps.c116
-rw-r--r--src/rps/gnunet-service-rps_sampler.c83
2 files changed, 112 insertions, 87 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index 51336ed2a..cf3bd080e 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -153,6 +153,10 @@ struct peer_context
153 * /Housekeeping with peers 153 * /Housekeeping with peers
154***********************************************************************/ 154***********************************************************************/
155 155
156/***********************************************************************
157 * Globals
158***********************************************************************/
159
156/** 160/**
157 * Set of all peers to keep track of them. 161 * Set of all peers to keep track of them.
158 */ 162 */
@@ -201,8 +205,6 @@ static float beta;
201 */ 205 */
202 206
203 207
204
205
206/** 208/**
207 * Identifier for the main task that runs periodically. 209 * Identifier for the main task that runs periodically.
208 */ 210 */
@@ -258,6 +260,43 @@ static struct GNUNET_CADET_Handle *cadet_handle;
258uint64_t g_i = 0; 260uint64_t g_i = 0;
259 261
260 262
263/**
264 * Request counter.
265 *
266 * Only needed in the beginning to check how many of the 64 deltas
267 * we already have
268 */
269static unsigned int req_counter;
270
271/**
272 * Time of the last request we received.
273 *
274 * Used to compute the expected request rate.
275 */
276static struct GNUNET_TIME_Absolute last_request;
277
278/**
279 * Size of #request_deltas.
280 */
281#define REQUEST_DELTAS_SIZE 64
282static unsigned int request_deltas_size = REQUEST_DELTAS_SIZE;
283
284/**
285 * Last 64 deltas between requests
286 */
287static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
288
289/**
290 * The prediction of the rate of requests
291 */
292static struct GNUNET_TIME_Relative request_rate;
293
294
295/***********************************************************************
296 * /Globals
297***********************************************************************/
298
299
261/*********************************************************************** 300/***********************************************************************
262 * Util functions 301 * Util functions
263***********************************************************************/ 302***********************************************************************/
@@ -392,6 +431,34 @@ get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, const struct GNUNET_Peer
392} 431}
393 432
394 433
434/**
435 * Sum all time relatives of an array.
436 */
437 struct GNUNET_TIME_Relative
438T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
439{
440 struct GNUNET_TIME_Relative sum;
441 uint64_t i;
442
443 sum = GNUNET_TIME_UNIT_ZERO;
444 for ( i = 0 ; i < arr_size ; i++ )
445 {
446 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
447 }
448 return sum;
449}
450
451
452/**
453 * Compute the average of given time relatives.
454 */
455 struct GNUNET_TIME_Relative
456T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
457{
458 return GNUNET_TIME_relative_divide (T_relative_sum (rel_array, arr_size), arr_size); // FIXME find a way to devide that by arr_size
459}
460
461
395/*********************************************************************** 462/***********************************************************************
396 * /Util functions 463 * /Util functions
397***********************************************************************/ 464***********************************************************************/
@@ -467,6 +534,25 @@ handle_cs_request (void *cls,
467 const struct GNUNET_PeerIdentity *peers; 534 const struct GNUNET_PeerIdentity *peers;
468 //uint64_t i; 535 //uint64_t i;
469 536
537
538 /* Estimate request rate */
539 if (request_deltas_size > req_counter)
540 req_counter++;
541 if ( 1 < req_counter)
542 {
543 /* Shift last request deltas to the right */
544 memcpy (&request_deltas[1],
545 request_deltas,
546 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
547 /* Add current delta to beginning */
548 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
549 GNUNET_TIME_absolute_get ());
550 request_rate = T_relative_avg (request_deltas, req_counter);
551 }
552 last_request = GNUNET_TIME_absolute_get();
553 // TODO resize the size of the extended_samplers
554
555
470 // TODO check message size 556 // TODO check message size
471 msg = (struct GNUNET_RPS_CS_RequestMessage *) message; 557 msg = (struct GNUNET_RPS_CS_RequestMessage *) message;
472 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx); 558 cli_ctx = GNUNET_SERVER_client_get_user_context (client, struct client_ctx);
@@ -781,15 +867,20 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
781 struct GNUNET_TIME_Relative time_next_round; 867 struct GNUNET_TIME_Relative time_next_round;
782 struct GNUNET_TIME_Relative half_round_interval; 868 struct GNUNET_TIME_Relative half_round_interval;
783 unsigned int rand_delay; 869 unsigned int rand_delay;
784 870
871 /* Compute random time value between .5 * round_interval and 1.5 *round_interval */
785 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2); 872 half_round_interval = GNUNET_TIME_relative_divide (round_interval, 2);
786 do 873 do
787 { 874 {
875 /*
876 * Compute random value between (0 and 1) * round_interval
877 * via multiplying round_interval with a 'fraction' (0 to value)/value
878 */
788 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10); 879 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT_MAX/10);
789 time_next_round = GNUNET_TIME_relative_multiply (time_next_round, rand_delay); 880 time_next_round = GNUNET_TIME_relative_multiply (round_interval, rand_delay);
790 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10); 881 time_next_round = GNUNET_TIME_relative_divide (time_next_round, UINT_MAX/10);
791 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval); 882 time_next_round = GNUNET_TIME_relative_add (time_next_round, half_round_interval);
792 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != time_next_round.rel_value_us); 883 } while (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == time_next_round.rel_value_us);
793 884
794 /* Schedule next round */ 885 /* Schedule next round */
795 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL); 886 do_round_task = GNUNET_SCHEDULER_add_delayed (round_interval, &do_round, NULL);
@@ -907,13 +998,14 @@ shutdown_task (void *cls,
907 do_round_task = NULL; 998 do_round_task = NULL;
908 } 999 }
909 1000
910 GNUNET_NSE_disconnect(nse); 1001 GNUNET_NSE_disconnect (nse);
911 GNUNET_CADET_disconnect(cadet_handle); 1002 GNUNET_CADET_disconnect (cadet_handle);
912 GNUNET_free(own_identity); 1003 GNUNET_free (own_identity);
913 RPS_sampler_destroy(); 1004 RPS_sampler_destroy ();
914 GNUNET_array_grow(gossip_list, gossip_list_size, 0); 1005 GNUNET_array_grow (request_deltas, request_deltas_size, 0);
915 GNUNET_array_grow(push_list, push_list_size, 0); 1006 GNUNET_array_grow (gossip_list, gossip_list_size, 0);
916 GNUNET_array_grow(pull_list, pull_list_size, 0); 1007 GNUNET_array_grow (push_list, push_list_size, 0);
1008 GNUNET_array_grow (pull_list, pull_list_size, 0);
917} 1009}
918 1010
919 1011
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c
index f1496f53a..d182894c0 100644
--- a/src/rps/gnunet-service-rps_sampler.c
+++ b/src/rps/gnunet-service-rps_sampler.c
@@ -156,60 +156,7 @@ static size_t max_size;
156/** 156/**
157 * Inedex to the sampler element that is the next to be returned 157 * Inedex to the sampler element that is the next to be returned
158 */ 158 */
159static uint64_t extended_samplers_index; 159static uint64_t client_get_index;
160
161/**
162 * Request counter.
163 *
164 * Only needed in the beginning to check how many of the 64 deltas
165 * we already have
166 */
167static unsigned int req_counter;
168
169/**
170 * Time of the last request we received.
171 *
172 * Used to compute the expected request rate.
173 */
174static struct GNUNET_TIME_Absolute last_request;
175
176/**
177 * Last 64 deltas between requests
178 */
179static struct GNUNET_TIME_Relative request_deltas[64];
180
181/**
182 * The prediction of the rate of requests
183 */
184static struct GNUNET_TIME_Relative request_rate;
185
186
187/**
188 * Sum all time relatives of an array.
189 */
190 struct GNUNET_TIME_Relative
191T_relative_sum (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
192{
193 struct GNUNET_TIME_Relative sum;
194 uint64_t i;
195
196 sum = GNUNET_TIME_UNIT_ZERO;
197 for ( i = 0 ; i < arr_size ; i++ )
198 {
199 sum = GNUNET_TIME_relative_add (sum, rel_array[i]);
200 }
201 return sum;
202}
203
204/**
205 * Compute the average of given time relatives.
206 */
207 struct GNUNET_TIME_Relative
208T_relative_avg (const struct GNUNET_TIME_Relative *rel_array, uint64_t arr_size)
209{
210 return T_relative_sum (rel_array, arr_size); // FIXME find a way to devide that by arr_size
211}
212
213 160
214 161
215/** 162/**
@@ -437,7 +384,7 @@ RPS_sampler_init (size_t init_size, const struct GNUNET_PeerIdentity *id,
437 RPS_sampler_resize (init_size); 384 RPS_sampler_resize (init_size);
438 RPS_sampler_update_list (id); // no super nice desing but ok for the moment 385 RPS_sampler_update_list (id); // no super nice desing but ok for the moment
439 386
440 extended_samplers_index = 0; 387 client_get_index = 0;
441 388
442 //GNUNET_assert (init_size == sampler->sampler_size); 389 //GNUNET_assert (init_size == sampler->sampler_size);
443} 390}
@@ -516,6 +463,7 @@ RPS_sampler_get_rand_peer_ ()
516 return peer; 463 return peer;
517} 464}
518 465
466
519/** 467/**
520 * Get n random peers out of the sampled peers. 468 * Get n random peers out of the sampled peers.
521 * 469 *
@@ -564,28 +512,14 @@ RPS_sampler_get_rand_peer ()
564{ 512{
565 struct GNUNET_PeerIdentity *peer; 513 struct GNUNET_PeerIdentity *peer;
566 514
567 if (64 > req_counter)
568 req_counter++;
569 if (1 < req_counter)
570 {
571 memcpy (&request_deltas[1],
572 request_deltas,
573 (req_counter - 1) * sizeof (struct GNUNET_TIME_Relative));
574 request_deltas[0] = GNUNET_TIME_absolute_get_difference (last_request,
575 GNUNET_TIME_absolute_get ());
576 request_rate = T_relative_avg (request_deltas, req_counter);
577 }
578 last_request = GNUNET_TIME_absolute_get();
579 // TODO resize the size of the extended_samplers
580
581 // use _get_rand_peer_ ? 515 // use _get_rand_peer_ ?
582 peer = GNUNET_new (struct GNUNET_PeerIdentity); 516 peer = GNUNET_new (struct GNUNET_PeerIdentity);
583 *peer = sampler->sampler_elements[extended_samplers_index]->peer_id; 517 *peer = sampler->sampler_elements[client_get_index]->peer_id;
584 RPS_sampler_elem_reinit (sampler->sampler_elements[extended_samplers_index]); 518 RPS_sampler_elem_reinit (sampler->sampler_elements[client_get_index]);
585 if ( extended_samplers_index == sampler->sampler_size ) 519 if ( client_get_index == sampler->sampler_size )
586 extended_samplers_index = 0; 520 client_get_index = 0;
587 else 521 else
588 extended_samplers_index++; 522 client_get_index++;
589 return peer; 523 return peer;
590} 524}
591 525
@@ -661,7 +595,6 @@ RPS_sampler_count_id (const struct GNUNET_PeerIdentity *id)
661RPS_sampler_destroy () 595RPS_sampler_destroy ()
662{ 596{
663 RPS_sampler_resize (0); 597 RPS_sampler_resize (0);
664 GNUNET_free (request_deltas); // _array_grow()?
665 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, 0); 598 GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, 0);
666} 599}
667 600