aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-sampler_client.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/rps/rps-sampler_client.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/rps/rps-sampler_client.c')
-rw-r--r--src/rps/rps-sampler_client.c190
1 files changed, 97 insertions, 93 deletions
diff --git a/src/rps/rps-sampler_client.c b/src/rps/rps-sampler_client.c
index b4ac5f6f8..5ab854e01 100644
--- a/src/rps/rps-sampler_client.c
+++ b/src/rps/rps-sampler_client.c
@@ -37,7 +37,7 @@
37 37
38#include "rps-test_util.h" 38#include "rps-test_util.h"
39 39
40#define LOG(kind, ...) GNUNET_log_from(kind, "rps-sampler", __VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler", __VA_ARGS__)
41 41
42 42
43// multiple 'clients'? 43// multiple 'clients'?
@@ -68,7 +68,8 @@ typedef void
68 * 68 *
69 * Meant to be an entry in an DLL. 69 * Meant to be an entry in an DLL.
70 */ 70 */
71struct SamplerNotifyUpdateCTX { 71struct SamplerNotifyUpdateCTX
72{
72 /** 73 /**
73 * @brief The Callback to call on updates 74 * @brief The Callback to call on updates
74 */ 75 */
@@ -106,13 +107,14 @@ typedef void
106 * corrsponding peer to the client. 107 * corrsponding peer to the client.
107 */ 108 */
108static void 109static void
109sampler_mod_get_rand_peer(void *cls); 110sampler_mod_get_rand_peer (void *cls);
110 111
111 112
112/** 113/**
113 * Closure to _get_n_rand_peers_ready_cb() 114 * Closure to _get_n_rand_peers_ready_cb()
114 */ 115 */
115struct RPS_SamplerRequestHandle { 116struct RPS_SamplerRequestHandle
117{
116 /** 118 /**
117 * DLL 119 * DLL
118 */ 120 */
@@ -160,7 +162,8 @@ struct RPS_SamplerRequestHandle {
160/** 162/**
161 * Closure to _get_rand_peer_info() 163 * Closure to _get_rand_peer_info()
162 */ 164 */
163struct RPS_SamplerRequestHandleSingleInfo { 165struct RPS_SamplerRequestHandleSingleInfo
166{
164 /** 167 /**
165 * DLL 168 * DLL
166 */ 169 */
@@ -198,7 +201,7 @@ struct RPS_SamplerRequestHandleSingleInfo {
198///** 201///**
199// * Global sampler variable. 202// * Global sampler variable.
200// */ 203// */
201//struct RPS_Sampler *sampler; 204// struct RPS_Sampler *sampler;
202 205
203 206
204/** 207/**
@@ -214,7 +217,7 @@ static size_t max_size;
214/** 217/**
215 * The size the extended sampler elements currently have. 218 * The size the extended sampler elements currently have.
216 */ 219 */
217//static size_t extra_size; 220// static size_t extra_size;
218 221
219/** 222/**
220 * Inedex to the sampler element that is the next to be returned 223 * Inedex to the sampler element that is the next to be returned
@@ -230,8 +233,8 @@ static uint32_t client_get_index;
230 * @return a handle to a sampler that consists of sampler elements. 233 * @return a handle to a sampler that consists of sampler elements.
231 */ 234 */
232struct RPS_Sampler * 235struct RPS_Sampler *
233RPS_sampler_mod_init(size_t init_size, 236RPS_sampler_mod_init (size_t init_size,
234 struct GNUNET_TIME_Relative max_round_interval) 237 struct GNUNET_TIME_Relative max_round_interval)
235{ 238{
236 struct RPS_Sampler *sampler; 239 struct RPS_Sampler *sampler;
237 240
@@ -239,17 +242,17 @@ RPS_sampler_mod_init(size_t init_size,
239 min_size = 10; // TODO make input to _samplers_init() 242 min_size = 10; // TODO make input to _samplers_init()
240 max_size = 1000; // TODO make input to _samplers_init() 243 max_size = 1000; // TODO make input to _samplers_init()
241 244
242 sampler = GNUNET_new(struct RPS_Sampler); 245 sampler = GNUNET_new (struct RPS_Sampler);
243 sampler->max_round_interval = max_round_interval; 246 sampler->max_round_interval = max_round_interval;
244 sampler->get_peers = sampler_mod_get_rand_peer; 247 sampler->get_peers = sampler_mod_get_rand_peer;
245 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity); 248 // sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
246 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size); 249 // GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
247 250
248 client_get_index = 0; 251 client_get_index = 0;
249 252
250 //GNUNET_assert (init_size == sampler->sampler_size); 253 // GNUNET_assert (init_size == sampler->sampler_size);
251 254
252 RPS_sampler_resize(sampler, init_size); 255 RPS_sampler_resize (sampler, init_size);
253 256
254 return sampler; 257 return sampler;
255} 258}
@@ -271,22 +274,22 @@ RPS_sampler_mod_init(size_t init_size,
271 * @return The estimated probability 274 * @return The estimated probability
272 */ 275 */
273static double 276static double
274prob_observed_n_peers(uint32_t num_peers_estim, 277prob_observed_n_peers (uint32_t num_peers_estim,
275 uint32_t num_peers_observed, 278 uint32_t num_peers_observed,
276 double deficiency_factor) 279 double deficiency_factor)
277{ 280{
278 uint32_t num_peers = num_peers_estim * (1 / deficiency_factor); 281 uint32_t num_peers = num_peers_estim * (1 / deficiency_factor);
279 uint64_t sum = 0; 282 uint64_t sum = 0;
280 283
281 for (uint32_t i = 0; i < num_peers; i++) 284 for (uint32_t i = 0; i < num_peers; i++)
282 { 285 {
283 uint64_t a = pow(-1, num_peers - i); 286 uint64_t a = pow (-1, num_peers - i);
284 uint64_t b = binom(num_peers, i); 287 uint64_t b = binom (num_peers, i);
285 uint64_t c = pow(i, num_peers_observed); 288 uint64_t c = pow (i, num_peers_observed);
286 sum += a * b * c; 289 sum += a * b * c;
287 } 290 }
288 291
289 return sum / (double)pow(num_peers, num_peers_observed); 292 return sum / (double) pow (num_peers, num_peers_observed);
290} 293}
291 294
292 295
@@ -296,7 +299,7 @@ prob_observed_n_peers(uint32_t num_peers_estim,
296 * This reinitialises the queried sampler element. 299 * This reinitialises the queried sampler element.
297 */ 300 */
298static void 301static void
299sampler_mod_get_rand_peer(void *cls) 302sampler_mod_get_rand_peer (void *cls)
300{ 303{
301 struct GetPeerCls *gpc = cls; 304 struct GetPeerCls *gpc = cls;
302 struct RPS_SamplerElement *s_elem; 305 struct RPS_SamplerElement *s_elem;
@@ -307,86 +310,88 @@ sampler_mod_get_rand_peer(void *cls)
307 310
308 gpc->get_peer_task = NULL; 311 gpc->get_peer_task = NULL;
309 gpc->notify_ctx = NULL; 312 gpc->notify_ctx = NULL;
310 GNUNET_assert((NULL != gpc->req_handle) || 313 GNUNET_assert ((NULL != gpc->req_handle) ||
311 (NULL != gpc->req_single_info_handle)); 314 (NULL != gpc->req_single_info_handle));
312 if (NULL != gpc->req_handle) 315 if (NULL != gpc->req_handle)
313 sampler = gpc->req_handle->sampler; 316 sampler = gpc->req_handle->sampler;
314 else 317 else
315 sampler = gpc->req_single_info_handle->sampler; 318 sampler = gpc->req_single_info_handle->sampler;
316 319
317 LOG(GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n"); 320 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
318 321
319 /* Cycle the #client_get_index one step further */ 322 /* Cycle the #client_get_index one step further */
320 client_get_index = (client_get_index + 1) % sampler->sampler_size; 323 client_get_index = (client_get_index + 1) % sampler->sampler_size;
321 324
322 s_elem = sampler->sampler_elements[client_get_index]; 325 s_elem = sampler->sampler_elements[client_get_index];
323 *gpc->id = s_elem->peer_id; 326 *gpc->id = s_elem->peer_id;
324 GNUNET_assert(NULL != s_elem); 327 GNUNET_assert (NULL != s_elem);
325 328
326 if (EMPTY == s_elem->is_empty) 329 if (EMPTY == s_elem->is_empty)
327 { 330 {
328 LOG(GNUNET_ERROR_TYPE_DEBUG, 331 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "Sampler_mod element empty, rescheduling.\n"); 332 "Sampler_mod element empty, rescheduling.\n");
330 GNUNET_assert(NULL == gpc->notify_ctx); 333 GNUNET_assert (NULL == gpc->notify_ctx);
331 gpc->notify_ctx = 334 gpc->notify_ctx =
332 sampler_notify_on_update(sampler, 335 sampler_notify_on_update (sampler,
333 &sampler_mod_get_rand_peer, 336 &sampler_mod_get_rand_peer,
334 gpc); 337 gpc);
335 return; 338 return;
336 } 339 }
337 340
338 /* Check whether we may use this sampler to give it back to the client */ 341 /* Check whether we may use this sampler to give it back to the client */
339 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us) 342 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
340 { 343 s_elem->last_client_request.abs_value_us)
341 // TODO remove this condition at least for the client sampler 344 {
342 last_request_diff = 345 // TODO remove this condition at least for the client sampler
343 GNUNET_TIME_absolute_get_difference(s_elem->last_client_request, 346 last_request_diff =
344 GNUNET_TIME_absolute_get()); 347 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
345 /* We're not going to give it back now if it was 348 GNUNET_TIME_absolute_get ());
346 * already requested by a client this round */ 349 /* We're not going to give it back now if it was
347 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us) 350 * already requested by a client this round */
348 { 351 if (last_request_diff.rel_value_us <
349 LOG(GNUNET_ERROR_TYPE_DEBUG, 352 sampler->max_round_interval.rel_value_us)
350 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
351 ///* How many time remains untile the next round has started? */
352 //inv_last_request_diff =
353 // GNUNET_TIME_absolute_get_difference (last_request_diff,
354 // sampler->max_round_interval);
355 // add a little delay
356 /* Schedule it one round later */
357 GNUNET_assert(NULL == gpc->notify_ctx);
358 gpc->notify_ctx =
359 sampler_notify_on_update(sampler,
360 &sampler_mod_get_rand_peer,
361 gpc);
362 return;
363 }
364 }
365 if (2 > s_elem->num_peers)
366 { 353 {
367 LOG(GNUNET_ERROR_TYPE_DEBUG, 354 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 "This s_elem saw less than two peers -- scheduling for later\n"); 355 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
369 GNUNET_assert(NULL == gpc->notify_ctx); 356 ///* How many time remains untile the next round has started? */
357 // inv_last_request_diff =
358 // GNUNET_TIME_absolute_get_difference (last_request_diff,
359 // sampler->max_round_interval);
360 // add a little delay
361 /* Schedule it one round later */
362 GNUNET_assert (NULL == gpc->notify_ctx);
370 gpc->notify_ctx = 363 gpc->notify_ctx =
371 sampler_notify_on_update(sampler, 364 sampler_notify_on_update (sampler,
372 &sampler_mod_get_rand_peer, 365 &sampler_mod_get_rand_peer,
373 gpc); 366 gpc);
374 return; 367 return;
375 } 368 }
369 }
370 if (2 > s_elem->num_peers)
371 {
372 LOG (GNUNET_ERROR_TYPE_DEBUG,
373 "This s_elem saw less than two peers -- scheduling for later\n");
374 GNUNET_assert (NULL == gpc->notify_ctx);
375 gpc->notify_ctx =
376 sampler_notify_on_update (sampler,
377 &sampler_mod_get_rand_peer,
378 gpc);
379 return;
380 }
376 /* compute probability */ 381 /* compute probability */
377 /* Currently disabled due to numerical limitations */ 382 /* Currently disabled due to numerical limitations */
378 //prob_observed_n = prob_observed_n_peers (sampler->num_peers_estim, 383 // prob_observed_n = prob_observed_n_peers (sampler->num_peers_estim,
379 // s_elem->num_peers, 384 // s_elem->num_peers,
380 // sampler->deficiency_factor); 385 // sampler->deficiency_factor);
381 //LOG (GNUNET_ERROR_TYPE_DEBUG, 386 // LOG (GNUNET_ERROR_TYPE_DEBUG,
382 // "Computed sample - prob %f, %" PRIu32 " peers, n: %" PRIu32 ", roh: %f\n", 387 // "Computed sample - prob %f, %" PRIu32 " peers, n: %" PRIu32 ", roh: %f\n",
383 // prob_observed_n, 388 // prob_observed_n,
384 // s_elem->num_peers, 389 // s_elem->num_peers,
385 // sampler->num_peers_estim, 390 // sampler->num_peers_estim,
386 // sampler->deficiency_factor); 391 // sampler->deficiency_factor);
387 ///* check if probability is above desired */ 392 ///* check if probability is above desired */
388 //if (prob_observed_n < sampler->desired_probability) 393 // if (prob_observed_n < sampler->desired_probability)
389 //{ 394 // {
390 // LOG (GNUNET_ERROR_TYPE_DEBUG, 395 // LOG (GNUNET_ERROR_TYPE_DEBUG,
391 // "Probability of having observed all peers (%f) too small ( < %f).\n", 396 // "Probability of having observed all peers (%f) too small ( < %f).\n",
392 // prob_observed_n, 397 // prob_observed_n,
@@ -397,7 +402,7 @@ sampler_mod_get_rand_peer(void *cls)
397 // &sampler_mod_get_rand_peer, 402 // &sampler_mod_get_rand_peer,
398 // gpc); 403 // gpc);
399 // return; 404 // return;
400 //} 405 // }
401 /* More reasons to wait could be added here */ 406 /* More reasons to wait could be added here */
402 407
403// GNUNET_STATISTICS_set (stats, 408// GNUNET_STATISTICS_set (stats,
@@ -410,25 +415,24 @@ sampler_mod_get_rand_peer(void *cls)
410// GNUNET_NO); 415// GNUNET_NO);
411 416
412 num_observed = s_elem->num_peers; 417 num_observed = s_elem->num_peers;
413 RPS_sampler_elem_reinit(s_elem); 418 RPS_sampler_elem_reinit (s_elem);
414 s_elem->last_client_request = GNUNET_TIME_absolute_get(); 419 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
415 420
416 if (NULL != gpc->req_handle) 421 if (NULL != gpc->req_handle)
417 { 422 {
418 GNUNET_CONTAINER_DLL_remove(gpc->req_handle->gpc_head, 423 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
419 gpc->req_handle->gpc_tail, 424 gpc->req_handle->gpc_tail,
420 gpc); 425 gpc);
421 } 426 }
422 else 427 else
423 { 428 {
424 GNUNET_CONTAINER_DLL_remove(gpc->req_single_info_handle->gpc_head, 429 GNUNET_CONTAINER_DLL_remove (gpc->req_single_info_handle->gpc_head,
425 gpc->req_single_info_handle->gpc_tail, 430 gpc->req_single_info_handle->gpc_tail,
426 gpc); 431 gpc);
427 } 432 }
428 gpc->cont(gpc->cont_cls, gpc->id, prob_observed_n, num_observed); 433 gpc->cont (gpc->cont_cls, gpc->id, prob_observed_n, num_observed);
429 GNUNET_free(gpc); 434 GNUNET_free (gpc);
430} 435}
431 436
432 437
433/* end of gnunet-service-rps.c */ 438/* end of gnunet-service-rps.c */
434