summaryrefslogtreecommitdiff
path: root/src/statistics
diff options
context:
space:
mode:
Diffstat (limited to 'src/statistics')
-rw-r--r--src/statistics/gnunet-service-statistics.c853
-rw-r--r--src/statistics/gnunet-statistics.c815
-rw-r--r--src/statistics/statistics.h18
-rw-r--r--src/statistics/statistics_api.c1146
-rw-r--r--src/statistics/test_statistics_api.c297
-rw-r--r--src/statistics/test_statistics_api_loop.c101
-rw-r--r--src/statistics/test_statistics_api_watch.c139
-rw-r--r--src/statistics/test_statistics_api_watch_zero_value.c187
8 files changed, 1767 insertions, 1789 deletions
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c
index 18905f458..7d84f421e 100644
--- a/src/statistics/gnunet-service-statistics.c
+++ b/src/statistics/gnunet-service-statistics.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file statistics/gnunet-service-statistics.c 22 * @file statistics/gnunet-service-statistics.c
@@ -38,9 +38,7 @@
38/** 38/**
39 * Watch entry. 39 * Watch entry.
40 */ 40 */
41struct WatchEntry 41struct WatchEntry {
42{
43
44 /** 42 /**
45 * Watch entries are kept in a linked list. 43 * Watch entries are kept in a linked list.
46 */ 44 */
@@ -84,8 +82,7 @@ struct SubsystemEntry;
84/** 82/**
85 * Entry in the statistics list. 83 * Entry in the statistics list.
86 */ 84 */
87struct StatsEntry 85struct StatsEntry {
88{
89 /** 86 /**
90 * This is a linked list. 87 * This is a linked list.
91 */ 88 */
@@ -144,8 +141,7 @@ struct StatsEntry
144 * We keep the statistics organized by subsystem for faster 141 * We keep the statistics organized by subsystem for faster
145 * lookup during SET operations. 142 * lookup during SET operations.
146 */ 143 */
147struct SubsystemEntry 144struct SubsystemEntry {
148{
149 /** 145 /**
150 * Subsystems are kept in a DLL. 146 * Subsystems are kept in a DLL.
151 */ 147 */
@@ -177,8 +173,7 @@ struct SubsystemEntry
177/** 173/**
178 * Client entry. 174 * Client entry.
179 */ 175 */
180struct ClientEntry 176struct ClientEntry {
181{
182 /** 177 /**
183 * Corresponding server handle. 178 * Corresponding server handle.
184 */ 179 */
@@ -241,7 +236,7 @@ static int in_shutdown;
241 * Write persistent statistics to disk. 236 * Write persistent statistics to disk.
242 */ 237 */
243static void 238static void
244save () 239save()
245{ 240{
246 struct SubsystemEntry *se; 241 struct SubsystemEntry *se;
247 struct StatsEntry *pos; 242 struct StatsEntry *pos;
@@ -253,74 +248,74 @@ save ()
253 size_t slen; 248 size_t slen;
254 struct GNUNET_STATISTICS_SetMessage *msg; 249 struct GNUNET_STATISTICS_SetMessage *msg;
255 250
256 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, 251 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg,
257 "STATISTICS", 252 "STATISTICS",
258 "DATABASE", 253 "DATABASE",
259 &fn)) 254 &fn))
260 { 255 {
261 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 256 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
262 "STATISTICS", 257 "STATISTICS",
263 "DATABASE"); 258 "DATABASE");
264 return; 259 return;
265 } 260 }
266 (void) GNUNET_DISK_directory_create_for_file (fn); 261 (void)GNUNET_DISK_directory_create_for_file(fn);
267 wh = GNUNET_BIO_write_open (fn); 262 wh = GNUNET_BIO_write_open(fn);
268 total = 0; 263 total = 0;
269 while (NULL != (se = sub_head)) 264 while (NULL != (se = sub_head))
270 {
271 GNUNET_CONTAINER_DLL_remove (sub_head, sub_tail, se);
272 slen = strlen (se->service) + 1;
273 while (NULL != (pos = se->stat_head))
274 { 265 {
275 GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos); 266 GNUNET_CONTAINER_DLL_remove(sub_head, sub_tail, se);
276 if ((pos->persistent) && (NULL != wh)) 267 slen = strlen(se->service) + 1;
277 { 268 while (NULL != (pos = se->stat_head))
278 nlen = strlen (pos->name) + 1;
279 size = sizeof (struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
280 GNUNET_assert (size < UINT16_MAX);
281 msg = GNUNET_malloc (size);
282
283 msg->header.size = htons ((uint16_t) size);
284 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
285 GNUNET_assert (nlen + slen ==
286 GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
287 nlen + slen,
288 2,
289 se->service,
290 pos->name));
291 msg->flags =
292 htonl (pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
293 msg->value = GNUNET_htonll (pos->value);
294 if (GNUNET_OK != GNUNET_BIO_write (wh, msg, size))
295 { 269 {
296 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn); 270 GNUNET_CONTAINER_DLL_remove(se->stat_head, se->stat_tail, pos);
297 if (GNUNET_OK != GNUNET_BIO_write_close (wh)) 271 if ((pos->persistent) && (NULL != wh))
298 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn); 272 {
299 wh = NULL; 273 nlen = strlen(pos->name) + 1;
274 size = sizeof(struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
275 GNUNET_assert(size < UINT16_MAX);
276 msg = GNUNET_malloc(size);
277
278 msg->header.size = htons((uint16_t)size);
279 msg->header.type = htons(GNUNET_MESSAGE_TYPE_STATISTICS_SET);
280 GNUNET_assert(nlen + slen ==
281 GNUNET_STRINGS_buffer_fill((char *)&msg[1],
282 nlen + slen,
283 2,
284 se->service,
285 pos->name));
286 msg->flags =
287 htonl(pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
288 msg->value = GNUNET_htonll(pos->value);
289 if (GNUNET_OK != GNUNET_BIO_write(wh, msg, size))
290 {
291 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "write", fn);
292 if (GNUNET_OK != GNUNET_BIO_write_close(wh))
293 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "close", fn);
294 wh = NULL;
295 }
296 else
297 {
298 total += size;
299 }
300 GNUNET_free(msg);
301 }
302 GNUNET_free(pos);
300 } 303 }
301 else 304 GNUNET_free(se);
302 {
303 total += size;
304 }
305 GNUNET_free (msg);
306 }
307 GNUNET_free (pos);
308 } 305 }
309 GNUNET_free (se);
310 }
311 if (NULL != wh) 306 if (NULL != wh)
312 { 307 {
313 if (GNUNET_OK != GNUNET_BIO_write_close (wh)) 308 if (GNUNET_OK != GNUNET_BIO_write_close(wh))
314 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn); 309 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "close", fn);
315 if (0 == total) 310 if (0 == total)
316 GNUNET_break (0 == unlink (fn)); 311 GNUNET_break(0 == unlink(fn));
317 else 312 else
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 313 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
319 _ ("Wrote %llu bytes of statistics to `%s'\n"), 314 _("Wrote %llu bytes of statistics to `%s'\n"),
320 total, 315 total,
321 fn); 316 fn);
322 } 317 }
323 GNUNET_free_non_null (fn); 318 GNUNET_free_non_null(fn);
324} 319}
325 320
326 321
@@ -331,31 +326,31 @@ save ()
331 * @param e value to transmit 326 * @param e value to transmit
332 */ 327 */
333static void 328static void
334transmit (struct ClientEntry *ce, const struct StatsEntry *e) 329transmit(struct ClientEntry *ce, const struct StatsEntry *e)
335{ 330{
336 struct GNUNET_MQ_Envelope *env; 331 struct GNUNET_MQ_Envelope *env;
337 struct GNUNET_STATISTICS_ReplyMessage *m; 332 struct GNUNET_STATISTICS_ReplyMessage *m;
338 size_t size; 333 size_t size;
339 334
340 size = strlen (e->subsystem->service) + 1 + strlen (e->name) + 1; 335 size = strlen(e->subsystem->service) + 1 + strlen(e->name) + 1;
341 GNUNET_assert (size < GNUNET_MAX_MESSAGE_SIZE); 336 GNUNET_assert(size < GNUNET_MAX_MESSAGE_SIZE);
342 env = GNUNET_MQ_msg_extra (m, size, GNUNET_MESSAGE_TYPE_STATISTICS_VALUE); 337 env = GNUNET_MQ_msg_extra(m, size, GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
343 m->uid = htonl (e->uid); 338 m->uid = htonl(e->uid);
344 if (e->persistent) 339 if (e->persistent)
345 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT); 340 m->uid |= htonl(GNUNET_STATISTICS_PERSIST_BIT);
346 m->value = GNUNET_htonll (e->value); 341 m->value = GNUNET_htonll(e->value);
347 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1], 342 GNUNET_assert(size == GNUNET_STRINGS_buffer_fill((char *)&m[1],
348 size, 343 size,
349 2, 344 2,
350 e->subsystem->service, 345 e->subsystem->service,
351 e->name)); 346 e->name));
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 347 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
353 "Transmitting value for `%s:%s' (%d): %llu\n", 348 "Transmitting value for `%s:%s' (%d): %llu\n",
354 e->subsystem->service, 349 e->subsystem->service,
355 e->name, 350 e->name,
356 e->persistent, 351 e->persistent,
357 (unsigned long long) e->value); 352 (unsigned long long)e->value);
358 GNUNET_MQ_send (ce->mq, env); 353 GNUNET_MQ_send(ce->mq, env);
359} 354}
360 355
361 356
@@ -368,17 +363,17 @@ transmit (struct ClientEntry *ce, const struct StatsEntry *e)
368 * @return @a c 363 * @return @a c
369 */ 364 */
370static void * 365static void *
371client_connect_cb (void *cls, 366client_connect_cb(void *cls,
372 struct GNUNET_SERVICE_Client *c, 367 struct GNUNET_SERVICE_Client *c,
373 struct GNUNET_MQ_Handle *mq) 368 struct GNUNET_MQ_Handle *mq)
374{ 369{
375 struct ClientEntry *ce; 370 struct ClientEntry *ce;
376 371
377 ce = GNUNET_new (struct ClientEntry); 372 ce = GNUNET_new(struct ClientEntry);
378 ce->client = c; 373 ce->client = c;
379 ce->mq = mq; 374 ce->mq = mq;
380 client_count++; 375 client_count++;
381 GNUNET_notification_context_add (nc, mq); 376 GNUNET_notification_context_add(nc, mq);
382 return ce; 377 return ce;
383} 378}
384 379
@@ -391,22 +386,22 @@ client_connect_cb (void *cls,
391 * @return #GNUNET_OK if @a message is well-formed 386 * @return #GNUNET_OK if @a message is well-formed
392 */ 387 */
393static int 388static int
394check_get (void *cls, const struct GNUNET_MessageHeader *message) 389check_get(void *cls, const struct GNUNET_MessageHeader *message)
395{ 390{
396 const char *service; 391 const char *service;
397 const char *name; 392 const char *name;
398 size_t size; 393 size_t size;
399 394
400 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader); 395 size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
401 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], 396 if (size != GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
402 size, 397 size,
403 2, 398 2,
404 &service, 399 &service,
405 &name)) 400 &name))
406 { 401 {
407 GNUNET_break (0); 402 GNUNET_break(0);
408 return GNUNET_SYSERR; 403 return GNUNET_SYSERR;
409 } 404 }
410 return GNUNET_OK; 405 return GNUNET_OK;
411} 406}
412 407
@@ -418,7 +413,7 @@ check_get (void *cls, const struct GNUNET_MessageHeader *message)
418 * @param message the actual message 413 * @param message the actual message
419 */ 414 */
420static void 415static void
421handle_get (void *cls, const struct GNUNET_MessageHeader *message) 416handle_get(void *cls, const struct GNUNET_MessageHeader *message)
422{ 417{
423 struct ClientEntry *ce = cls; 418 struct ClientEntry *ce = cls;
424 struct GNUNET_MQ_Envelope *env; 419 struct GNUNET_MQ_Envelope *env;
@@ -431,33 +426,33 @@ handle_get (void *cls, const struct GNUNET_MessageHeader *message)
431 struct StatsEntry *pos; 426 struct StatsEntry *pos;
432 size_t size; 427 size_t size;
433 428
434 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader); 429 size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
435 GNUNET_assert (size == 430 GNUNET_assert(size ==
436 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], 431 GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
437 size, 432 size,
438 2, 433 2,
439 &service, 434 &service,
440 &name)); 435 &name));
441 slen = strlen (service); 436 slen = strlen(service);
442 nlen = strlen (name); 437 nlen = strlen(name);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 438 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
444 "Received request for statistics on `%s:%s'\n", 439 "Received request for statistics on `%s:%s'\n",
445 slen ? service : "*", 440 slen ? service : "*",
446 nlen ? name : "*"); 441 nlen ? name : "*");
447 for (se = sub_head; NULL != se; se = se->next) 442 for (se = sub_head; NULL != se; se = se->next)
448 {
449 if (! ((0 == slen) || (0 == strcmp (service, se->service))))
450 continue;
451 for (pos = se->stat_head; NULL != pos; pos = pos->next)
452 { 443 {
453 if (! ((0 == nlen) || (0 == strcmp (name, pos->name)))) 444 if (!((0 == slen) || (0 == strcmp(service, se->service))))
454 continue; 445 continue;
455 transmit (ce, pos); 446 for (pos = se->stat_head; NULL != pos; pos = pos->next)
447 {
448 if (!((0 == nlen) || (0 == strcmp(name, pos->name))))
449 continue;
450 transmit(ce, pos);
451 }
456 } 452 }
457 } 453 env = GNUNET_MQ_msg(end, GNUNET_MESSAGE_TYPE_STATISTICS_END);
458 env = GNUNET_MQ_msg (end, GNUNET_MESSAGE_TYPE_STATISTICS_END); 454 GNUNET_MQ_send(ce->mq, env);
459 GNUNET_MQ_send (ce->mq, env); 455 GNUNET_SERVICE_client_continue(ce->client);
460 GNUNET_SERVICE_client_continue (ce->client);
461} 456}
462 457
463 458
@@ -467,32 +462,32 @@ handle_get (void *cls, const struct GNUNET_MessageHeader *message)
467 * @param se value that changed 462 * @param se value that changed
468 */ 463 */
469static void 464static void
470notify_change (struct StatsEntry *se) 465notify_change(struct StatsEntry *se)
471{ 466{
472 struct GNUNET_MQ_Envelope *env; 467 struct GNUNET_MQ_Envelope *env;
473 struct GNUNET_STATISTICS_WatchValueMessage *wvm; 468 struct GNUNET_STATISTICS_WatchValueMessage *wvm;
474 struct WatchEntry *pos; 469 struct WatchEntry *pos;
475 470
476 for (pos = se->we_head; NULL != pos; pos = pos->next) 471 for (pos = se->we_head; NULL != pos; pos = pos->next)
477 {
478 if (GNUNET_YES == pos->last_value_set)
479 {
480 if (pos->last_value == se->value)
481 continue;
482 }
483 else
484 { 472 {
485 pos->last_value_set = GNUNET_YES; 473 if (GNUNET_YES == pos->last_value_set)
474 {
475 if (pos->last_value == se->value)
476 continue;
477 }
478 else
479 {
480 pos->last_value_set = GNUNET_YES;
481 }
482 env = GNUNET_MQ_msg(wvm, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
483 wvm->flags =
484 htonl(se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
485 wvm->wid = htonl(pos->wid);
486 wvm->reserved = htonl(0);
487 wvm->value = GNUNET_htonll(se->value);
488 GNUNET_MQ_send(pos->ce->mq, env);
489 pos->last_value = se->value;
486 } 490 }
487 env = GNUNET_MQ_msg (wvm, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
488 wvm->flags =
489 htonl (se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
490 wvm->wid = htonl (pos->wid);
491 wvm->reserved = htonl (0);
492 wvm->value = GNUNET_htonll (se->value);
493 GNUNET_MQ_send (pos->ce->mq, env);
494 pos->last_value = se->value;
495 }
496} 491}
497 492
498 493
@@ -505,7 +500,7 @@ notify_change (struct StatsEntry *se)
505 * @return subsystem entry, never NULL (subsystem entry is created if necessary) 500 * @return subsystem entry, never NULL (subsystem entry is created if necessary)
506 */ 501 */
507static struct SubsystemEntry * 502static struct SubsystemEntry *
508find_subsystem_entry (struct ClientEntry *ce, const char *service) 503find_subsystem_entry(struct ClientEntry *ce, const char *service)
509{ 504{
510 size_t slen; 505 size_t slen;
511 struct SubsystemEntry *se; 506 struct SubsystemEntry *se;
@@ -514,24 +509,24 @@ find_subsystem_entry (struct ClientEntry *ce, const char *service)
514 se = ce->subsystem; 509 se = ce->subsystem;
515 else 510 else
516 se = NULL; 511 se = NULL;
517 if ((NULL == se) || (0 != strcmp (service, se->service))) 512 if ((NULL == se) || (0 != strcmp(service, se->service)))
518 { 513 {
519 for (se = sub_head; NULL != se; se = se->next) 514 for (se = sub_head; NULL != se; se = se->next)
520 if (0 == strcmp (service, se->service)) 515 if (0 == strcmp(service, se->service))
521 break; 516 break;
522 if (NULL != ce) 517 if (NULL != ce)
523 ce->subsystem = se; 518 ce->subsystem = se;
524 } 519 }
525 if (NULL != se) 520 if (NULL != se)
526 return se; 521 return se;
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
528 "Allocating new subsystem entry `%s'\n", 523 "Allocating new subsystem entry `%s'\n",
529 service); 524 service);
530 slen = strlen (service) + 1; 525 slen = strlen(service) + 1;
531 se = GNUNET_malloc (sizeof (struct SubsystemEntry) + slen); 526 se = GNUNET_malloc(sizeof(struct SubsystemEntry) + slen);
532 GNUNET_memcpy (&se[1], service, slen); 527 GNUNET_memcpy(&se[1], service, slen);
533 se->service = (const char *) &se[1]; 528 se->service = (const char *)&se[1];
534 GNUNET_CONTAINER_DLL_insert (sub_head, sub_tail, se); 529 GNUNET_CONTAINER_DLL_insert(sub_head, sub_tail, se);
535 if (NULL != ce) 530 if (NULL != ce)
536 ce->subsystem = se; 531 ce->subsystem = se;
537 return se; 532 return se;
@@ -546,12 +541,12 @@ find_subsystem_entry (struct ClientEntry *ce, const char *service)
546 * @return statistis entry, or NULL if not found 541 * @return statistis entry, or NULL if not found
547 */ 542 */
548static struct StatsEntry * 543static struct StatsEntry *
549find_stat_entry (struct SubsystemEntry *se, const char *name) 544find_stat_entry(struct SubsystemEntry *se, const char *name)
550{ 545{
551 struct StatsEntry *pos; 546 struct StatsEntry *pos;
552 547
553 for (pos = se->stat_head; NULL != pos; pos = pos->next) 548 for (pos = se->stat_head; NULL != pos; pos = pos->next)
554 if (0 == strcmp (name, pos->name)) 549 if (0 == strcmp(name, pos->name))
555 return pos; 550 return pos;
556 return NULL; 551 return NULL;
557} 552}
@@ -565,22 +560,22 @@ find_stat_entry (struct SubsystemEntry *se, const char *name)
565 * @return #GNUNET_OK if message is well-formed 560 * @return #GNUNET_OK if message is well-formed
566 */ 561 */
567static int 562static int
568check_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg) 563check_set(void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
569{ 564{
570 const char *service; 565 const char *service;
571 const char *name; 566 const char *name;
572 size_t msize; 567 size_t msize;
573 568
574 msize = ntohs (msg->header.size) - sizeof (*msg); 569 msize = ntohs(msg->header.size) - sizeof(*msg);
575 if (msize != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], 570 if (msize != GNUNET_STRINGS_buffer_tokenize((const char *)&msg[1],
576 msize, 571 msize,
577 2, 572 2,
578 &service, 573 &service,
579 &name)) 574 &name))
580 { 575 {
581 GNUNET_break (0); 576 GNUNET_break(0);
582 return GNUNET_SYSERR; 577 return GNUNET_SYSERR;
583 } 578 }
584 return GNUNET_OK; 579 return GNUNET_OK;
585} 580}
586 581
@@ -592,7 +587,7 @@ check_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
592 * @param message the actual message 587 * @param message the actual message
593 */ 588 */
594static void 589static void
595handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg) 590handle_set(void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
596{ 591{
597 struct ClientEntry *ce = cls; 592 struct ClientEntry *ce = cls;
598 const char *service; 593 const char *service;
@@ -608,95 +603,95 @@ handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
608 int changed; 603 int changed;
609 int initial_set; 604 int initial_set;
610 605
611 msize = ntohs (msg->header.size); 606 msize = ntohs(msg->header.size);
612 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage); 607 size = msize - sizeof(struct GNUNET_STATISTICS_SetMessage);
613 GNUNET_assert (size == GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], 608 GNUNET_assert(size == GNUNET_STRINGS_buffer_tokenize((const char *)&msg[1],
614 size, 609 size,
615 2, 610 2,
616 &service, 611 &service,
617 &name)); 612 &name));
618 se = find_subsystem_entry (ce, service); 613 se = find_subsystem_entry(ce, service);
619 flags = ntohl (msg->flags); 614 flags = ntohl(msg->flags);
620 value = GNUNET_ntohll (msg->value); 615 value = GNUNET_ntohll(msg->value);
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 616 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
622 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n", 617 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
623 service, 618 service,
624 name, 619 name,
625 (unsigned int) flags, 620 (unsigned int)flags,
626 (unsigned long long) value); 621 (unsigned long long)value);
627 pos = find_stat_entry (se, name); 622 pos = find_stat_entry(se, name);
628 if (NULL != pos) 623 if (NULL != pos)
629 {
630 initial_set = 0;
631 if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
632 { 624 {
633 changed = (pos->value != value); 625 initial_set = 0;
634 pos->value = value; 626 if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
635 } 627 {
636 else 628 changed = (pos->value != value);
637 { 629 pos->value = value;
638 delta = (int64_t) value; 630 }
639 if ((delta < 0) && (pos->value < -delta))
640 {
641 changed = (0 != pos->value);
642 pos->value = 0;
643 }
644 else 631 else
645 { 632 {
646 changed = (0 != delta); 633 delta = (int64_t)value;
647 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value)); 634 if ((delta < 0) && (pos->value < -delta))
648 pos->value += delta; 635 {
649 } 636 changed = (0 != pos->value);
637 pos->value = 0;
638 }
639 else
640 {
641 changed = (0 != delta);
642 GNUNET_break((delta <= 0) || (pos->value + delta > pos->value));
643 pos->value += delta;
644 }
645 }
646 if (GNUNET_NO == pos->set)
647 {
648 pos->set = GNUNET_YES;
649 initial_set = 1;
650 }
651 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
652 if (pos != se->stat_head)
653 {
654 /* move to front for faster setting next time! */
655 GNUNET_CONTAINER_DLL_remove(se->stat_head, se->stat_tail, pos);
656 GNUNET_CONTAINER_DLL_insert(se->stat_head, se->stat_tail, pos);
657 }
658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
659 "Statistic `%s:%s' updated to value %llu (%d).\n",
660 service,
661 name,
662 (unsigned long long)pos->value,
663 pos->persistent);
664 if ((changed) || (1 == initial_set))
665 notify_change(pos);
666 GNUNET_SERVICE_client_continue(ce->client);
667 return;
650 } 668 }
651 if (GNUNET_NO == pos->set) 669 /* not found, create a new entry */
670 nlen = strlen(name) + 1;
671 pos = GNUNET_malloc(sizeof(struct StatsEntry) + nlen);
672 GNUNET_memcpy(&pos[1], name, nlen);
673 pos->name = (const char *)&pos[1];
674 pos->subsystem = se;
675 if ((0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
676 (0 < (int64_t)GNUNET_ntohll(msg->value)))
652 { 677 {
678 pos->value = GNUNET_ntohll(msg->value);
653 pos->set = GNUNET_YES; 679 pos->set = GNUNET_YES;
654 initial_set = 1;
655 } 680 }
656 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT)); 681 else
657 if (pos != se->stat_head)
658 { 682 {
659 /* move to front for faster setting next time! */ 683 pos->set = GNUNET_NO;
660 GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
661 GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
662 } 684 }
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Statistic `%s:%s' updated to value %llu (%d).\n",
665 service,
666 name,
667 (unsigned long long) pos->value,
668 pos->persistent);
669 if ((changed) || (1 == initial_set))
670 notify_change (pos);
671 GNUNET_SERVICE_client_continue (ce->client);
672 return;
673 }
674 /* not found, create a new entry */
675 nlen = strlen (name) + 1;
676 pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen);
677 GNUNET_memcpy (&pos[1], name, nlen);
678 pos->name = (const char *) &pos[1];
679 pos->subsystem = se;
680 if ((0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
681 (0 < (int64_t) GNUNET_ntohll (msg->value)))
682 {
683 pos->value = GNUNET_ntohll (msg->value);
684 pos->set = GNUNET_YES;
685 }
686 else
687 {
688 pos->set = GNUNET_NO;
689 }
690 pos->uid = uidgen++; 685 pos->uid = uidgen++;
691 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT)); 686 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
692 GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos); 687 GNUNET_CONTAINER_DLL_insert(se->stat_head, se->stat_tail, pos);
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 688 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
694 "New statistic on `%s:%s' with value %llu created.\n", 689 "New statistic on `%s:%s' with value %llu created.\n",
695 service, 690 service,
696 name, 691 name,
697 (unsigned long long) pos->value); 692 (unsigned long long)pos->value);
698 if (NULL != ce) 693 if (NULL != ce)
699 GNUNET_SERVICE_client_continue (ce->client); 694 GNUNET_SERVICE_client_continue(ce->client);
700} 695}
701 696
702 697
@@ -708,22 +703,22 @@ handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
708 * @return #GNUNET_OK if message is well-formed 703 * @return #GNUNET_OK if message is well-formed
709 */ 704 */
710static int 705static int
711check_watch (void *cls, const struct GNUNET_MessageHeader *message) 706check_watch(void *cls, const struct GNUNET_MessageHeader *message)
712{ 707{
713 size_t size; 708 size_t size;
714 const char *service; 709 const char *service;
715 const char *name; 710 const char *name;
716 711
717 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader); 712 size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
718 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], 713 if (size != GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
719 size, 714 size,
720 2, 715 2,
721 &service, 716 &service,
722 &name)) 717 &name))
723 { 718 {
724 GNUNET_break (0); 719 GNUNET_break(0);
725 return GNUNET_SYSERR; 720 return GNUNET_SYSERR;
726 } 721 }
727 return GNUNET_OK; 722 return GNUNET_OK;
728} 723}
729 724
@@ -735,7 +730,7 @@ check_watch (void *cls, const struct GNUNET_MessageHeader *message)
735 * @param message the actual message 730 * @param message the actual message
736 */ 731 */
737static void 732static void
738handle_watch (void *cls, const struct GNUNET_MessageHeader *message) 733handle_watch(void *cls, const struct GNUNET_MessageHeader *message)
739{ 734{
740 struct ClientEntry *ce = cls; 735 struct ClientEntry *ce = cls;
741 const char *service; 736 const char *service;
@@ -748,49 +743,49 @@ handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
748 size_t nlen; 743 size_t nlen;
749 744
750 if (NULL == nc) 745 if (NULL == nc)
751 { 746 {
752 GNUNET_SERVICE_client_drop (ce->client); 747 GNUNET_SERVICE_client_drop(ce->client);
753 return; 748 return;
754 } 749 }
755 GNUNET_SERVICE_client_mark_monitor (ce->client); 750 GNUNET_SERVICE_client_mark_monitor(ce->client);
756 msize = ntohs (message->size); 751 msize = ntohs(message->size);
757 size = msize - sizeof (struct GNUNET_MessageHeader); 752 size = msize - sizeof(struct GNUNET_MessageHeader);
758 GNUNET_assert (size == 753 GNUNET_assert(size ==
759 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], 754 GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
760 size, 755 size,
761 2, 756 2,
762 &service, 757 &service,
763 &name)); 758 &name));
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 759 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
765 "Received request to watch statistic on `%s:%s'\n", 760 "Received request to watch statistic on `%s:%s'\n",
766 service, 761 service,
767 name); 762 name);
768 se = find_subsystem_entry (ce, service); 763 se = find_subsystem_entry(ce, service);
769 pos = find_stat_entry (se, name); 764 pos = find_stat_entry(se, name);
770 if (NULL == pos) 765 if (NULL == pos)
771 { 766 {
772 nlen = strlen (name) + 1; 767 nlen = strlen(name) + 1;
773 pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen); 768 pos = GNUNET_malloc(sizeof(struct StatsEntry) + nlen);
774 GNUNET_memcpy (&pos[1], name, nlen); 769 GNUNET_memcpy(&pos[1], name, nlen);
775 pos->name = (const char *) &pos[1]; 770 pos->name = (const char *)&pos[1];
776 pos->subsystem = se; 771 pos->subsystem = se;
777 GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos); 772 GNUNET_CONTAINER_DLL_insert(se->stat_head, se->stat_tail, pos);
778 pos->uid = uidgen++; 773 pos->uid = uidgen++;
779 pos->set = GNUNET_NO; 774 pos->set = GNUNET_NO;
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 775 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
781 "New statistic on `%s:%s' with value %llu created.\n", 776 "New statistic on `%s:%s' with value %llu created.\n",
782 service, 777 service,
783 name, 778 name,
784 (unsigned long long) pos->value); 779 (unsigned long long)pos->value);
785 } 780 }
786 we = GNUNET_new (struct WatchEntry); 781 we = GNUNET_new(struct WatchEntry);
787 we->ce = ce; 782 we->ce = ce;
788 we->last_value_set = GNUNET_NO; 783 we->last_value_set = GNUNET_NO;
789 we->wid = ce->max_wid++; 784 we->wid = ce->max_wid++;
790 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we); 785 GNUNET_CONTAINER_DLL_insert(pos->we_head, pos->we_tail, we);
791 if (0 != pos->value) 786 if (0 != pos->value)
792 notify_change (pos); 787 notify_change(pos);
793 GNUNET_SERVICE_client_continue (ce->client); 788 GNUNET_SERVICE_client_continue(ce->client);
794} 789}
795 790
796 791
@@ -803,15 +798,15 @@ handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
803 * @param message the actual message 798 * @param message the actual message
804 */ 799 */
805static void 800static void
806handle_disconnect (void *cls, const struct GNUNET_MessageHeader *message) 801handle_disconnect(void *cls, const struct GNUNET_MessageHeader *message)
807{ 802{
808 struct ClientEntry *ce = cls; 803 struct ClientEntry *ce = cls;
809 struct GNUNET_MQ_Envelope *env; 804 struct GNUNET_MQ_Envelope *env;
810 struct GNUNET_MessageHeader *msg; 805 struct GNUNET_MessageHeader *msg;
811 806
812 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM); 807 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM);
813 GNUNET_MQ_send (ce->mq, env); 808 GNUNET_MQ_send(ce->mq, env);
814 GNUNET_SERVICE_client_continue (ce->client); 809 GNUNET_SERVICE_client_continue(ce->client);
815} 810}
816 811
817 812
@@ -819,7 +814,7 @@ handle_disconnect (void *cls, const struct GNUNET_MessageHeader *message)
819 * Actually perform the shutdown. 814 * Actually perform the shutdown.
820 */ 815 */
821static void 816static void
822do_shutdown () 817do_shutdown()
823{ 818{
824 struct WatchEntry *we; 819 struct WatchEntry *we;
825 struct StatsEntry *pos; 820 struct StatsEntry *pos;
@@ -827,26 +822,26 @@ do_shutdown ()
827 822
828 if (NULL == nc) 823 if (NULL == nc)
829 return; 824 return;
830 save (); 825 save();
831 GNUNET_notification_context_destroy (nc); 826 GNUNET_notification_context_destroy(nc);
832 nc = NULL; 827 nc = NULL;
833 GNUNET_assert (0 == client_count); 828 GNUNET_assert(0 == client_count);
834 while (NULL != (se = sub_head)) 829 while (NULL != (se = sub_head))
835 {
836 GNUNET_CONTAINER_DLL_remove (sub_head, sub_tail, se);
837 while (NULL != (pos = se->stat_head))
838 { 830 {
839 GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos); 831 GNUNET_CONTAINER_DLL_remove(sub_head, sub_tail, se);
840 while (NULL != (we = pos->we_head)) 832 while (NULL != (pos = se->stat_head))
841 { 833 {
842 GNUNET_break (0); 834 GNUNET_CONTAINER_DLL_remove(se->stat_head, se->stat_tail, pos);
843 GNUNET_CONTAINER_DLL_remove (pos->we_head, pos->we_tail, we); 835 while (NULL != (we = pos->we_head))
844 GNUNET_free (we); 836 {
845 } 837 GNUNET_break(0);
846 GNUNET_free (pos); 838 GNUNET_CONTAINER_DLL_remove(pos->we_head, pos->we_tail, we);
839 GNUNET_free(we);
840 }
841 GNUNET_free(pos);
842 }
843 GNUNET_free(se);
847 } 844 }
848 GNUNET_free (se);
849 }
850} 845}
851 846
852 847
@@ -856,12 +851,12 @@ do_shutdown ()
856 * @param cls unused 851 * @param cls unused
857 */ 852 */
858static void 853static void
859shutdown_task (void *cls) 854shutdown_task(void *cls)
860{ 855{
861 in_shutdown = GNUNET_YES; 856 in_shutdown = GNUNET_YES;
862 if (0 != client_count) 857 if (0 != client_count)
863 return; 858 return;
864 do_shutdown (); 859 do_shutdown();
865} 860}
866 861
867 862
@@ -873,9 +868,9 @@ shutdown_task (void *cls)
873 * @param app_cls the `struct ClientEntry *` 868 * @param app_cls the `struct ClientEntry *`
874 */ 869 */
875static void 870static void
876client_disconnect_cb (void *cls, 871client_disconnect_cb(void *cls,
877 struct GNUNET_SERVICE_Client *client, 872 struct GNUNET_SERVICE_Client *client,
878 void *app_cls) 873 void *app_cls)
879{ 874{
880 struct ClientEntry *ce = app_cls; 875 struct ClientEntry *ce = app_cls;
881 struct WatchEntry *we; 876 struct WatchEntry *we;
@@ -885,23 +880,23 @@ client_disconnect_cb (void *cls,
885 880
886 client_count--; 881 client_count--;
887 for (se = sub_head; NULL != se; se = se->next) 882 for (se = sub_head; NULL != se; se = se->next)
888 {
889 for (pos = se->stat_head; NULL != pos; pos = pos->next)
890 { 883 {
891 wen = pos->we_head; 884 for (pos = se->stat_head; NULL != pos; pos = pos->next)
892 while (NULL != (we = wen)) 885 {
893 { 886 wen = pos->we_head;
894 wen = we->next; 887 while (NULL != (we = wen))
895 if (we->ce != ce) 888 {
896 continue; 889 wen = we->next;
897 GNUNET_CONTAINER_DLL_remove (pos->we_head, pos->we_tail, we); 890 if (we->ce != ce)
898 GNUNET_free (we); 891 continue;
899 } 892 GNUNET_CONTAINER_DLL_remove(pos->we_head, pos->we_tail, we);
893 GNUNET_free(we);
894 }
895 }
900 } 896 }
901 } 897 GNUNET_free(ce);
902 GNUNET_free (ce);
903 if ((0 == client_count) && (GNUNET_YES == in_shutdown)) 898 if ((0 == client_count) && (GNUNET_YES == in_shutdown))
904 do_shutdown (); 899 do_shutdown();
905} 900}
906 901
907 902
@@ -917,19 +912,19 @@ client_disconnect_cb (void *cls,
917 * #GNUNET_SYSERR to stop further processing with error 912 * #GNUNET_SYSERR to stop further processing with error
918 */ 913 */
919static int 914static int
920inject_message (void *cls, const struct GNUNET_MessageHeader *message) 915inject_message(void *cls, const struct GNUNET_MessageHeader *message)
921{ 916{
922 uint16_t msize = ntohs (message->size); 917 uint16_t msize = ntohs(message->size);
923 const struct GNUNET_STATISTICS_SetMessage *sm; 918 const struct GNUNET_STATISTICS_SetMessage *sm;
924 919
925 sm = (const struct GNUNET_STATISTICS_SetMessage *) message; 920 sm = (const struct GNUNET_STATISTICS_SetMessage *)message;
926 if ((sizeof (struct GNUNET_STATISTICS_SetMessage) > msize) || 921 if ((sizeof(struct GNUNET_STATISTICS_SetMessage) > msize) ||
927 (GNUNET_OK != check_set (NULL, sm))) 922 (GNUNET_OK != check_set(NULL, sm)))
928 { 923 {
929 GNUNET_break (0); 924 GNUNET_break(0);
930 return GNUNET_SYSERR; 925 return GNUNET_SYSERR;
931 } 926 }
932 handle_set (NULL, sm); 927 handle_set(NULL, sm);
933 return GNUNET_OK; 928 return GNUNET_OK;
934} 929}
935 930
@@ -939,7 +934,7 @@ inject_message (void *cls, const struct GNUNET_MessageHeader *message)
939 * format that we also use for setting the values over the network. 934 * format that we also use for setting the values over the network.
940 */ 935 */
941static void 936static void
942load () 937load()
943{ 938{
944 char *fn; 939 char *fn;
945 struct GNUNET_BIO_ReadHandle *rh; 940 struct GNUNET_BIO_ReadHandle *rh;
@@ -947,51 +942,51 @@ load ()
947 char *buf; 942 char *buf;
948 struct GNUNET_MessageStreamTokenizer *mst; 943 struct GNUNET_MessageStreamTokenizer *mst;
949 944
950 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, 945 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg,
951 "STATISTICS", 946 "STATISTICS",
952 "DATABASE", 947 "DATABASE",
953 &fn)) 948 &fn))
954 { 949 {
955 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 950 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
956 "STATISTICS", 951 "STATISTICS",
957 "DATABASE"); 952 "DATABASE");
958 return; 953 return;
959 } 954 }
960 if ((GNUNET_OK != 955 if ((GNUNET_OK !=
961 GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || 956 GNUNET_DISK_file_size(fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
962 (0 == fsize)) 957 (0 == fsize))
963 { 958 {
964 GNUNET_free (fn); 959 GNUNET_free(fn);
965 return; 960 return;
966 } 961 }
967 buf = GNUNET_malloc (fsize); 962 buf = GNUNET_malloc(fsize);
968 rh = GNUNET_BIO_read_open (fn); 963 rh = GNUNET_BIO_read_open(fn);
969 if (! rh) 964 if (!rh)
970 { 965 {
971 GNUNET_free (buf); 966 GNUNET_free(buf);
972 GNUNET_free (fn); 967 GNUNET_free(fn);
973 return; 968 return;
974 } 969 }
975 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize)) 970 if (GNUNET_OK != GNUNET_BIO_read(rh, fn, buf, fsize))
976 { 971 {
977 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn); 972 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "read", fn);
978 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL)); 973 GNUNET_break(GNUNET_OK == GNUNET_BIO_read_close(rh, NULL));
979 GNUNET_free (buf); 974 GNUNET_free(buf);
980 GNUNET_free (fn); 975 GNUNET_free(fn);
981 return; 976 return;
982 } 977 }
983 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 978 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
984 _ ("Loading %llu bytes of statistics from `%s'\n"), 979 _("Loading %llu bytes of statistics from `%s'\n"),
985 (unsigned long long) fsize, 980 (unsigned long long)fsize,
986 fn); 981 fn);
987 mst = GNUNET_MST_create (&inject_message, NULL); 982 mst = GNUNET_MST_create(&inject_message, NULL);
988 GNUNET_break ( 983 GNUNET_break(
989 GNUNET_OK == 984 GNUNET_OK ==
990 GNUNET_MST_from_buffer (mst, buf, (size_t) fsize, GNUNET_YES, GNUNET_NO)); 985 GNUNET_MST_from_buffer(mst, buf, (size_t)fsize, GNUNET_YES, GNUNET_NO));
991 GNUNET_MST_destroy (mst); 986 GNUNET_MST_destroy(mst);
992 GNUNET_free (buf); 987 GNUNET_free(buf);
993 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL)); 988 GNUNET_break(GNUNET_OK == GNUNET_BIO_read_close(rh, NULL));
994 GNUNET_free (fn); 989 GNUNET_free(fn);
995} 990}
996 991
997 992
@@ -1003,44 +998,44 @@ load ()
1003 * @param service the initialized service 998 * @param service the initialized service
1004 */ 999 */
1005static void 1000static void
1006run (void *cls, 1001run(void *cls,
1007 const struct GNUNET_CONFIGURATION_Handle *c, 1002 const struct GNUNET_CONFIGURATION_Handle *c,
1008 struct GNUNET_SERVICE_Handle *service) 1003 struct GNUNET_SERVICE_Handle *service)
1009{ 1004{
1010 cfg = c; 1005 cfg = c;
1011 nc = GNUNET_notification_context_create (16); 1006 nc = GNUNET_notification_context_create(16);
1012 load (); 1007 load();
1013 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 1008 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
1014} 1009}
1015 1010
1016 1011
1017/** 1012/**
1018 * Define "main" method using service macro. 1013 * Define "main" method using service macro.
1019 */ 1014 */
1020GNUNET_SERVICE_MAIN ( 1015GNUNET_SERVICE_MAIN(
1021 "statistics", 1016 "statistics",
1022 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, 1017 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
1023 &run, 1018 &run,
1024 &client_connect_cb, 1019 &client_connect_cb,
1025 &client_disconnect_cb, 1020 &client_disconnect_cb,
1026 NULL, 1021 NULL,
1027 GNUNET_MQ_hd_var_size (set, 1022 GNUNET_MQ_hd_var_size(set,
1028 GNUNET_MESSAGE_TYPE_STATISTICS_SET, 1023 GNUNET_MESSAGE_TYPE_STATISTICS_SET,
1029 struct GNUNET_STATISTICS_SetMessage, 1024 struct GNUNET_STATISTICS_SetMessage,
1030 NULL), 1025 NULL),
1031 GNUNET_MQ_hd_var_size (get, 1026 GNUNET_MQ_hd_var_size(get,
1032 GNUNET_MESSAGE_TYPE_STATISTICS_GET, 1027 GNUNET_MESSAGE_TYPE_STATISTICS_GET,
1033 struct GNUNET_MessageHeader, 1028 struct GNUNET_MessageHeader,
1034 NULL), 1029 NULL),
1035 GNUNET_MQ_hd_var_size (watch, 1030 GNUNET_MQ_hd_var_size(watch,
1036 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 1031 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH,
1037 struct GNUNET_MessageHeader, 1032 struct GNUNET_MessageHeader,
1038 NULL), 1033 NULL),
1039 GNUNET_MQ_hd_fixed_size (disconnect, 1034 GNUNET_MQ_hd_fixed_size(disconnect,
1040 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT, 1035 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT,
1041 struct GNUNET_MessageHeader, 1036 struct GNUNET_MessageHeader,
1042 NULL), 1037 NULL),
1043 GNUNET_MQ_handler_end ()); 1038 GNUNET_MQ_handler_end());
1044 1039
1045 1040
1046#if defined(LINUX) && defined(__GLIBC__) 1041#if defined(LINUX) && defined(__GLIBC__)
@@ -1049,11 +1044,11 @@ GNUNET_SERVICE_MAIN (
1049/** 1044/**
1050 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 1045 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1051 */ 1046 */
1052void __attribute__ ((constructor)) GNUNET_STATISTICS_memory_init () 1047void __attribute__ ((constructor)) GNUNET_STATISTICS_memory_init()
1053{ 1048{
1054 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 1049 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
1055 mallopt (M_TOP_PAD, 1 * 1024); 1050 mallopt(M_TOP_PAD, 1 * 1024);
1056 malloc_trim (0); 1051 malloc_trim(0);
1057} 1052}
1058#endif 1053#endif
1059 1054
diff --git a/src/statistics/gnunet-statistics.c b/src/statistics/gnunet-statistics.c
index f1ea8cb19..470bb2ed8 100644
--- a/src/statistics/gnunet-statistics.c
+++ b/src/statistics/gnunet-statistics.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file statistics/gnunet-statistics.c 22 * @file statistics/gnunet-statistics.c
@@ -93,8 +93,7 @@ static int set_value;
93/** 93/**
94 * @brief Representation of all (testbed) nodes. 94 * @brief Representation of all (testbed) nodes.
95 */ 95 */
96static struct Node 96static struct Node {
97{
98 /** 97 /**
99 * @brief Index of the node in this array. 98 * @brief Index of the node in this array.
100 */ 99 */
@@ -128,8 +127,7 @@ static unsigned num_nodes;
128/** 127/**
129 * @brief Set of values for a combination of subsystem and name. 128 * @brief Set of values for a combination of subsystem and name.
130 */ 129 */
131struct ValueSet 130struct ValueSet {
132{
133 /** 131 /**
134 * @brief Subsystem of the valueset. 132 * @brief Subsystem of the valueset.
135 */ 133 */
@@ -177,17 +175,17 @@ static int num_nodes_ready_shutdown;
177 * @return Newly allocated #ValueSet. 175 * @return Newly allocated #ValueSet.
178 */ 176 */
179static struct ValueSet * 177static struct ValueSet *
180new_value_set (const char *subsystem, 178new_value_set(const char *subsystem,
181 const char *name, 179 const char *name,
182 unsigned num_values, 180 unsigned num_values,
183 int is_persistent) 181 int is_persistent)
184{ 182{
185 struct ValueSet *value_set; 183 struct ValueSet *value_set;
186 184
187 value_set = GNUNET_new (struct ValueSet); 185 value_set = GNUNET_new(struct ValueSet);
188 value_set->subsystem = GNUNET_strdup (subsystem); 186 value_set->subsystem = GNUNET_strdup(subsystem);
189 value_set->name = GNUNET_strdup (name); 187 value_set->name = GNUNET_strdup(name);
190 value_set->values = GNUNET_new_array (num_values, uint64_t); 188 value_set->values = GNUNET_new_array(num_values, uint64_t);
191 value_set->is_persistent = persistent; 189 value_set->is_persistent = persistent;
192 return value_set; 190 return value_set;
193} 191}
@@ -205,56 +203,56 @@ new_value_set (const char *subsystem,
205 * @return GNUNET_YES - continue iteration. 203 * @return GNUNET_YES - continue iteration.
206 */ 204 */
207static int 205static int
208printer (void *cls, const struct GNUNET_HashCode *key, void *value) 206printer(void *cls, const struct GNUNET_HashCode *key, void *value)
209{ 207{
210 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 208 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
211 const char *now_str; 209 const char *now_str;
212 struct ValueSet *value_set = value; 210 struct ValueSet *value_set = value;
213 211
214 if (quiet == GNUNET_NO) 212 if (quiet == GNUNET_NO)
215 {
216 if (GNUNET_YES == watch)
217 { 213 {
218 now_str = GNUNET_STRINGS_absolute_time_to_string (now); 214 if (GNUNET_YES == watch)
219 fprintf (stdout, 215 {
220 "%24s%s %s%s%12s%s %s%50s%s%s ", 216 now_str = GNUNET_STRINGS_absolute_time_to_string(now);
221 now_str, 217 fprintf(stdout,
222 csv_separator, 218 "%24s%s %s%s%12s%s %s%50s%s%s ",
223 value_set->is_persistent ? "!" : " ", 219 now_str,
224 csv_separator, 220 csv_separator,
225 value_set->subsystem, 221 value_set->is_persistent ? "!" : " ",
226 csv_separator, 222 csv_separator,
227 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */ 223 value_set->subsystem,
228 _ (value_set->name), 224 csv_separator,
229 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */ 225 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
230 (0 == strlen (csv_separator) ? ":" : csv_separator)); 226 _(value_set->name),
227 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
228 (0 == strlen(csv_separator) ? ":" : csv_separator));
229 }
230 else
231 {
232 fprintf(stdout,
233 "%s%s%12s%s %s%50s%s%s ",
234 value_set->is_persistent ? "!" : " ",
235 csv_separator,
236 value_set->subsystem,
237 csv_separator,
238 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
239 _(value_set->name),
240 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
241 (0 == strlen(csv_separator) ? ":" : csv_separator));
242 }
231 } 243 }
232 else 244 for (unsigned i = 0; i < num_nodes; i++)
233 { 245 {
234 fprintf (stdout, 246 fprintf(stdout,
235 "%s%s%12s%s %s%50s%s%s ", 247 "%16llu%s",
236 value_set->is_persistent ? "!" : " ", 248 (unsigned long long)value_set->values[i],
237 csv_separator, 249 csv_separator);
238 value_set->subsystem,
239 csv_separator,
240 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */
241 _ (value_set->name),
242 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */
243 (0 == strlen (csv_separator) ? ":" : csv_separator));
244 } 250 }
245 } 251 fprintf(stdout, "\n");
246 for (unsigned i = 0; i < num_nodes; i++) 252 GNUNET_free(value_set->subsystem);
247 { 253 GNUNET_free(value_set->name);
248 fprintf (stdout, 254 GNUNET_free(value_set->values);
249 "%16llu%s", 255 GNUNET_free(value_set);
250 (unsigned long long) value_set->values[i],
251 csv_separator);
252 }
253 fprintf (stdout, "\n");
254 GNUNET_free (value_set->subsystem);
255 GNUNET_free (value_set->name);
256 GNUNET_free (value_set->values);
257 GNUNET_free (value_set);
258 return GNUNET_YES; 256 return GNUNET_YES;
259} 257}
260 258
@@ -269,51 +267,51 @@ printer (void *cls, const struct GNUNET_HashCode *key, void *value)
269 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration 267 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
270 */ 268 */
271static int 269static int
272printer_watch (void *cls, 270printer_watch(void *cls,
273 const char *subsystem, 271 const char *subsystem,
274 const char *name, 272 const char *name,
275 uint64_t value, 273 uint64_t value,
276 int is_persistent) 274 int is_persistent)
277{ 275{
278 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 276 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
279 const char *now_str; 277 const char *now_str;
280 278
281 if (quiet == GNUNET_NO) 279 if (quiet == GNUNET_NO)
282 {
283 if (GNUNET_YES == watch)
284 { 280 {
285 now_str = GNUNET_STRINGS_absolute_time_to_string (now); 281 if (GNUNET_YES == watch)
286 fprintf (stdout, 282 {
287 "%24s%s %s%s%12s%s %s%50s%s%s %16llu\n", 283 now_str = GNUNET_STRINGS_absolute_time_to_string(now);
288 now_str, 284 fprintf(stdout,
289 csv_separator, 285 "%24s%s %s%s%12s%s %s%50s%s%s %16llu\n",
290 is_persistent ? "!" : " ", 286 now_str,
291 csv_separator, 287 csv_separator,
292 subsystem, 288 is_persistent ? "!" : " ",
293 csv_separator, 289 csv_separator,
294 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */ 290 subsystem,
295 _ (name), 291 csv_separator,
296 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */ 292 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
297 (0 == strlen (csv_separator) ? ":" : csv_separator), 293 _(name),
298 (unsigned long long) value); 294 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
295 (0 == strlen(csv_separator) ? ":" : csv_separator),
296 (unsigned long long)value);
297 }
298 else
299 {
300 fprintf(stdout,
301 "%s%s%12s%s %s%50s%s%s %16llu\n",
302 is_persistent ? "!" : " ",
303 csv_separator,
304 subsystem,
305 csv_separator,
306 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
307 _(name),
308 (0 == strlen(csv_separator) ? "" : "\""), /* quotes if csv */
309 (0 == strlen(csv_separator) ? ":" : csv_separator),
310 (unsigned long long)value);
311 }
299 } 312 }
300 else
301 {
302 fprintf (stdout,
303 "%s%s%12s%s %s%50s%s%s %16llu\n",
304 is_persistent ? "!" : " ",
305 csv_separator,
306 subsystem,
307 csv_separator,
308 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */
309 _ (name),
310 (0 == strlen (csv_separator) ? "" : "\""), /* quotes if csv */
311 (0 == strlen (csv_separator) ? ":" : csv_separator),
312 (unsigned long long) value);
313 }
314 }
315 else 313 else
316 fprintf (stdout, "%llu\n", (unsigned long long) value); 314 fprintf(stdout, "%llu\n", (unsigned long long)value);
317 315
318 return GNUNET_OK; 316 return GNUNET_OK;
319} 317}
@@ -326,50 +324,50 @@ printer_watch (void *cls,
326 * @param cls the index of the node 324 * @param cls the index of the node
327 */ 325 */
328static void 326static void
329clean_node (void *cls) 327clean_node(void *cls)
330{ 328{
331 const unsigned index_node = *(unsigned *) cls; 329 const unsigned index_node = *(unsigned *)cls;
332 struct GNUNET_STATISTICS_Handle *h; 330 struct GNUNET_STATISTICS_Handle *h;
333 struct GNUNET_STATISTICS_GetHandle *gh; 331 struct GNUNET_STATISTICS_GetHandle *gh;
334 332
335 if ((NULL != path_testbed) && /* were issued with -t <testbed-path> option */ 333 if ((NULL != path_testbed) && /* were issued with -t <testbed-path> option */
336 (NULL != nodes[index_node].conf)) 334 (NULL != nodes[index_node].conf))
337 { 335 {
338 GNUNET_CONFIGURATION_destroy (nodes[index_node].conf); 336 GNUNET_CONFIGURATION_destroy(nodes[index_node].conf);
339 nodes[index_node].conf = NULL; 337 nodes[index_node].conf = NULL;
340 } 338 }
341 339
342 h = nodes[index_node].handle; 340 h = nodes[index_node].handle;
343 gh = nodes[index_node].gh; 341 gh = nodes[index_node].gh;
344 342
345 if (NULL != gh) 343 if (NULL != gh)
346 { 344 {
347 GNUNET_STATISTICS_get_cancel (gh); 345 GNUNET_STATISTICS_get_cancel(gh);
348 gh = NULL; 346 gh = NULL;
349 } 347 }
350 if (GNUNET_YES == watch) 348 if (GNUNET_YES == watch)
351 { 349 {
352 GNUNET_assert ( 350 GNUNET_assert(
353 GNUNET_OK == 351 GNUNET_OK ==
354 GNUNET_STATISTICS_watch_cancel (h, 352 GNUNET_STATISTICS_watch_cancel(h,
355 subsystem, 353 subsystem,
356 name, 354 name,
357 &printer_watch, 355 &printer_watch,
358 &nodes[index_node].index_node)); 356 &nodes[index_node].index_node));
359 } 357 }
360 358
361 if (NULL != h) 359 if (NULL != h)
362 { 360 {
363 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 361 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
364 h = NULL; 362 h = NULL;
365 } 363 }
366 364
367 num_nodes_ready_shutdown++; 365 num_nodes_ready_shutdown++;
368 if (num_nodes == num_nodes_ready_shutdown) 366 if (num_nodes == num_nodes_ready_shutdown)
369 { 367 {
370 GNUNET_array_grow (nodes, num_nodes, 0); 368 GNUNET_array_grow(nodes, num_nodes, 0);
371 GNUNET_CONTAINER_multihashmap_destroy (values); 369 GNUNET_CONTAINER_multihashmap_destroy(values);
372 } 370 }
373} 371}
374 372
375/** 373/**
@@ -378,10 +376,10 @@ clean_node (void *cls)
378 * @param cls unused 376 * @param cls unused
379 */ 377 */
380static void 378static void
381print_finish (void *cls) 379print_finish(void *cls)
382{ 380{
383 GNUNET_CONTAINER_multihashmap_iterate (values, printer, NULL); 381 GNUNET_CONTAINER_multihashmap_iterate(values, printer, NULL);
384 GNUNET_SCHEDULER_shutdown (); 382 GNUNET_SCHEDULER_shutdown();
385} 383}
386 384
387/** 385/**
@@ -393,33 +391,33 @@ print_finish (void *cls)
393 * @param succes Whether statistics were obtained successfully. 391 * @param succes Whether statistics were obtained successfully.
394 */ 392 */
395static void 393static void
396continuation_print (void *cls, int success) 394continuation_print(void *cls, int success)
397{ 395{
398 const unsigned index_node = *(unsigned *) cls; 396 const unsigned index_node = *(unsigned *)cls;
399 397
400 nodes[index_node].gh = NULL; 398 nodes[index_node].gh = NULL;
401 if (GNUNET_OK != success) 399 if (GNUNET_OK != success)
402 { 400 {
403 if (NULL == remote_host) 401 if (NULL == remote_host)
404 fprintf (stderr, "%s", _ ("Failed to obtain statistics.\n")); 402 fprintf(stderr, "%s", _("Failed to obtain statistics.\n"));
405 else 403 else
406 fprintf (stderr, 404 fprintf(stderr,
407 _ ("Failed to obtain statistics from host `%s:%llu'\n"), 405 _("Failed to obtain statistics from host `%s:%llu'\n"),
408 remote_host, 406 remote_host,
409 remote_port); 407 remote_port);
410 ret = 1; 408 ret = 1;
411 } 409 }
412 if (NULL != nodes[index_node].shutdown_task) 410 if (NULL != nodes[index_node].shutdown_task)
413 { 411 {
414 GNUNET_SCHEDULER_cancel (nodes[index_node].shutdown_task); 412 GNUNET_SCHEDULER_cancel(nodes[index_node].shutdown_task);
415 nodes[index_node].shutdown_task = NULL; 413 nodes[index_node].shutdown_task = NULL;
416 } 414 }
417 GNUNET_SCHEDULER_add_now (clean_node, &nodes[index_node].index_node); 415 GNUNET_SCHEDULER_add_now(clean_node, &nodes[index_node].index_node);
418 num_nodes_ready++; 416 num_nodes_ready++;
419 if (num_nodes_ready == num_nodes) 417 if (num_nodes_ready == num_nodes)
420 { 418 {
421 GNUNET_SCHEDULER_add_now (print_finish, NULL); 419 GNUNET_SCHEDULER_add_now(print_finish, NULL);
422 } 420 }
423} 421}
424 422
425/** 423/**
@@ -430,24 +428,24 @@ continuation_print (void *cls, int success)
430 * successfully obtained, #GNUNET_SYSERR if not. 428 * successfully obtained, #GNUNET_SYSERR if not.
431 */ 429 */
432static void 430static void
433cleanup (void *cls, int success) 431cleanup(void *cls, int success)
434{ 432{
435 for (unsigned i = 0; i < num_nodes; i++) 433 for (unsigned i = 0; i < num_nodes; i++)
436 { 434 {
437 nodes[i].gh = NULL; 435 nodes[i].gh = NULL;
438 } 436 }
439 if (GNUNET_OK != success) 437 if (GNUNET_OK != success)
440 { 438 {
441 if (NULL == remote_host) 439 if (NULL == remote_host)
442 fprintf (stderr, "%s", _ ("Failed to obtain statistics.\n")); 440 fprintf(stderr, "%s", _("Failed to obtain statistics.\n"));
443 else 441 else
444 fprintf (stderr, 442 fprintf(stderr,
445 _ ("Failed to obtain statistics from host `%s:%llu'\n"), 443 _("Failed to obtain statistics from host `%s:%llu'\n"),
446 remote_host, 444 remote_host,
447 remote_port); 445 remote_port);
448 ret = 1; 446 ret = 1;
449 } 447 }
450 GNUNET_SCHEDULER_shutdown (); 448 GNUNET_SCHEDULER_shutdown();
451} 449}
452 450
453/** 451/**
@@ -463,40 +461,40 @@ cleanup (void *cls, int success)
463 * @return GNUNET_OK - continue. 461 * @return GNUNET_OK - continue.
464 */ 462 */
465static int 463static int
466collector (void *cls, 464collector(void *cls,
467 const char *subsystem, 465 const char *subsystem,
468 const char *name, 466 const char *name,
469 uint64_t value, 467 uint64_t value,
470 int is_persistent) 468 int is_persistent)
471{ 469{
472 const unsigned index_node = *(unsigned *) cls; 470 const unsigned index_node = *(unsigned *)cls;
473 struct GNUNET_HashCode *key; 471 struct GNUNET_HashCode *key;
474 struct GNUNET_HashCode hc; 472 struct GNUNET_HashCode hc;
475 char *subsys_name; 473 char *subsys_name;
476 unsigned len_subsys_name; 474 unsigned len_subsys_name;
477 struct ValueSet *value_set; 475 struct ValueSet *value_set;
478 476
479 len_subsys_name = strlen (subsystem) + 3 + strlen (name) + 1; 477 len_subsys_name = strlen(subsystem) + 3 + strlen(name) + 1;
480 subsys_name = GNUNET_malloc (len_subsys_name); 478 subsys_name = GNUNET_malloc(len_subsys_name);
481 sprintf (subsys_name, "%s---%s", subsystem, name); 479 sprintf(subsys_name, "%s---%s", subsystem, name);
482 key = &hc; 480 key = &hc;
483 GNUNET_CRYPTO_hash (subsys_name, len_subsys_name, key); 481 GNUNET_CRYPTO_hash(subsys_name, len_subsys_name, key);
484 GNUNET_free (subsys_name); 482 GNUNET_free(subsys_name);
485 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (values, key)) 483 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(values, key))
486 { 484 {
487 value_set = GNUNET_CONTAINER_multihashmap_get (values, key); 485 value_set = GNUNET_CONTAINER_multihashmap_get(values, key);
488 } 486 }
489 else 487 else
490 { 488 {
491 value_set = new_value_set (subsystem, name, num_nodes, is_persistent); 489 value_set = new_value_set(subsystem, name, num_nodes, is_persistent);
492 } 490 }
493 value_set->values[index_node] = value; 491 value_set->values[index_node] = value;
494 GNUNET_assert (GNUNET_YES == 492 GNUNET_assert(GNUNET_YES ==
495 GNUNET_CONTAINER_multihashmap_put ( 493 GNUNET_CONTAINER_multihashmap_put(
496 values, 494 values,
497 key, 495 key,
498 value_set, 496 value_set,
499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
500 return GNUNET_OK; 498 return GNUNET_OK;
501} 499}
502 500
@@ -506,80 +504,80 @@ collector (void *cls,
506 * @param cls closure with our configuration 504 * @param cls closure with our configuration
507 */ 505 */
508static void 506static void
509main_task (void *cls) 507main_task(void *cls)
510{ 508{
511 unsigned index_node = *(unsigned *) cls; 509 unsigned index_node = *(unsigned *)cls;
512 const struct GNUNET_CONFIGURATION_Handle *cfg = nodes[index_node].conf; 510 const struct GNUNET_CONFIGURATION_Handle *cfg = nodes[index_node].conf;
513 511
514 if (set_value) 512 if (set_value)
515 {
516 if (NULL == subsystem)
517 { 513 {
518 fprintf (stderr, "%s", _ ("Missing argument: subsystem \n")); 514 if (NULL == subsystem)
519 ret = 1; 515 {
520 return; 516 fprintf(stderr, "%s", _("Missing argument: subsystem \n"));
521 } 517 ret = 1;
522 if (NULL == name) 518 return;
523 { 519 }
524 fprintf (stderr, "%s", _ ("Missing argument: name\n")); 520 if (NULL == name)
525 ret = 1; 521 {
522 fprintf(stderr, "%s", _("Missing argument: name\n"));
523 ret = 1;
524 return;
525 }
526 nodes[index_node].handle = GNUNET_STATISTICS_create(subsystem, cfg);
527 if (NULL == nodes[index_node].handle)
528 {
529 ret = 1;
530 return;
531 }
532 GNUNET_STATISTICS_set(nodes[index_node].handle,
533 name,
534 (uint64_t)set_val,
535 persistent);
536 GNUNET_STATISTICS_destroy(nodes[index_node].handle, GNUNET_YES);
537 nodes[index_node].handle = NULL;
526 return; 538 return;
527 } 539 }
528 nodes[index_node].handle = GNUNET_STATISTICS_create (subsystem, cfg); 540 if (NULL == (nodes[index_node].handle =
529 if (NULL == nodes[index_node].handle) 541 GNUNET_STATISTICS_create("gnunet-statistics", cfg)))
530 { 542 {
531 ret = 1; 543 ret = 1;
532 return; 544 return;
533 } 545 }
534 GNUNET_STATISTICS_set (nodes[index_node].handle,
535 name,
536 (uint64_t) set_val,
537 persistent);
538 GNUNET_STATISTICS_destroy (nodes[index_node].handle, GNUNET_YES);
539 nodes[index_node].handle = NULL;
540 return;
541 }
542 if (NULL == (nodes[index_node].handle =
543 GNUNET_STATISTICS_create ("gnunet-statistics", cfg)))
544 {
545 ret = 1;
546 return;
547 }
548 if (GNUNET_NO == watch) 546 if (GNUNET_NO == watch)
549 {
550 if (NULL == (nodes[index_node].gh =
551 GNUNET_STATISTICS_get (nodes[index_node].handle,
552 subsystem,
553 name,
554 &continuation_print,
555 &collector,
556 &nodes[index_node].index_node)))
557 cleanup (nodes[index_node].handle, GNUNET_SYSERR);
558 }
559 else
560 {
561 if ((NULL == subsystem) || (NULL == name))
562 { 547 {
563 printf (_ ("No subsystem or name given\n")); 548 if (NULL == (nodes[index_node].gh =
564 GNUNET_STATISTICS_destroy (nodes[index_node].handle, GNUNET_NO); 549 GNUNET_STATISTICS_get(nodes[index_node].handle,
565 nodes[index_node].handle = NULL; 550 subsystem,
566 ret = 1; 551 name,
567 return; 552 &continuation_print,
553 &collector,
554 &nodes[index_node].index_node)))
555 cleanup(nodes[index_node].handle, GNUNET_SYSERR);
568 } 556 }
569 if (GNUNET_OK != GNUNET_STATISTICS_watch (nodes[index_node].handle, 557 else
570 subsystem,
571 name,
572 &printer_watch,
573 &nodes[index_node].index_node))
574 { 558 {
575 fprintf (stderr, _ ("Failed to initialize watch routine\n")); 559 if ((NULL == subsystem) || (NULL == name))
576 nodes[index_node].shutdown_task = 560 {
577 GNUNET_SCHEDULER_add_now (&clean_node, &nodes[index_node].index_node); 561 printf(_("No subsystem or name given\n"));
578 return; 562 GNUNET_STATISTICS_destroy(nodes[index_node].handle, GNUNET_NO);
563 nodes[index_node].handle = NULL;
564 ret = 1;
565 return;
566 }
567 if (GNUNET_OK != GNUNET_STATISTICS_watch(nodes[index_node].handle,
568 subsystem,
569 name,
570 &printer_watch,
571 &nodes[index_node].index_node))
572 {
573 fprintf(stderr, _("Failed to initialize watch routine\n"));
574 nodes[index_node].shutdown_task =
575 GNUNET_SCHEDULER_add_now(&clean_node, &nodes[index_node].index_node);
576 return;
577 }
579 } 578 }
580 }
581 nodes[index_node].shutdown_task = 579 nodes[index_node].shutdown_task =
582 GNUNET_SCHEDULER_add_shutdown (&clean_node, &nodes[index_node].index_node); 580 GNUNET_SCHEDULER_add_shutdown(&clean_node, &nodes[index_node].index_node);
583} 581}
584 582
585/** 583/**
@@ -594,27 +592,27 @@ main_task (void *cls)
594 * @return to continue iteration or not to 592 * @return to continue iteration or not to
595 */ 593 */
596static int 594static int
597iter_check_config (void *cls, const char *filename) 595iter_check_config(void *cls, const char *filename)
598{ 596{
599 if (0 == strncmp (GNUNET_STRINGS_get_short_name (filename), "config", 6)) 597 if (0 == strncmp(GNUNET_STRINGS_get_short_name(filename), "config", 6))
600 {
601 /* Found the config - stop iteration successfully */
602 GNUNET_array_grow (nodes, num_nodes, num_nodes + 1);
603 nodes[num_nodes - 1].conf = GNUNET_CONFIGURATION_create ();
604 nodes[num_nodes - 1].index_node = num_nodes - 1;
605 if (GNUNET_OK !=
606 GNUNET_CONFIGURATION_load (nodes[num_nodes - 1].conf, filename))
607 { 598 {
608 fprintf (stderr, "Failed loading config `%s'\n", filename); 599 /* Found the config - stop iteration successfully */
609 return GNUNET_SYSERR; 600 GNUNET_array_grow(nodes, num_nodes, num_nodes + 1);
601 nodes[num_nodes - 1].conf = GNUNET_CONFIGURATION_create();
602 nodes[num_nodes - 1].index_node = num_nodes - 1;
603 if (GNUNET_OK !=
604 GNUNET_CONFIGURATION_load(nodes[num_nodes - 1].conf, filename))
605 {
606 fprintf(stderr, "Failed loading config `%s'\n", filename);
607 return GNUNET_SYSERR;
608 }
609 return GNUNET_NO;
610 } 610 }
611 return GNUNET_NO;
612 }
613 else 611 else
614 { 612 {
615 /* Continue iteration */ 613 /* Continue iteration */
616 return GNUNET_OK; 614 return GNUNET_OK;
617 } 615 }
618} 616}
619 617
620/** 618/**
@@ -631,21 +629,21 @@ iter_check_config (void *cls, const char *filename)
631 * @return status whether to continue iteration 629 * @return status whether to continue iteration
632 */ 630 */
633static int 631static int
634iter_testbed_path (void *cls, const char *filename) 632iter_testbed_path(void *cls, const char *filename)
635{ 633{
636 unsigned index_node; 634 unsigned index_node;
637 635
638 GNUNET_assert (NULL != filename); 636 GNUNET_assert(NULL != filename);
639 if (1 == sscanf (GNUNET_STRINGS_get_short_name (filename), "%u", &index_node)) 637 if (1 == sscanf(GNUNET_STRINGS_get_short_name(filename), "%u", &index_node))
640 {
641 if (-1 == GNUNET_DISK_directory_scan (filename, iter_check_config, NULL))
642 { 638 {
643 /* This is probably no directory for a testbed node 639 if (-1 == GNUNET_DISK_directory_scan(filename, iter_check_config, NULL))
644 * Go on with iteration */ 640 {
641 /* This is probably no directory for a testbed node
642 * Go on with iteration */
643 return GNUNET_OK;
644 }
645 return GNUNET_OK; 645 return GNUNET_OK;
646 } 646 }
647 return GNUNET_OK;
648 }
649 return GNUNET_OK; 647 return GNUNET_OK;
650} 648}
651 649
@@ -657,17 +655,17 @@ iter_testbed_path (void *cls, const char *filename)
657 * @return number of running nodes 655 * @return number of running nodes
658 */ 656 */
659static int 657static int
660discover_testbed_nodes (const char *path_testbed) 658discover_testbed_nodes(const char *path_testbed)
661{ 659{
662 int num_dir_entries; 660 int num_dir_entries;
663 661
664 num_dir_entries = 662 num_dir_entries =
665 GNUNET_DISK_directory_scan (path_testbed, iter_testbed_path, NULL); 663 GNUNET_DISK_directory_scan(path_testbed, iter_testbed_path, NULL);
666 if (-1 == num_dir_entries) 664 if (-1 == num_dir_entries)
667 { 665 {
668 fprintf (stderr, "Failure during scanning directory `%s'\n", path_testbed); 666 fprintf(stderr, "Failure during scanning directory `%s'\n", path_testbed);
669 return -1; 667 return -1;
670 } 668 }
671 return 0; 669 return 0;
672} 670}
673 671
@@ -680,90 +678,90 @@ discover_testbed_nodes (const char *path_testbed)
680 * @param cfg configuration 678 * @param cfg configuration
681 */ 679 */
682static void 680static void
683run (void *cls, 681run(void *cls,
684 char *const *args, 682 char *const *args,
685 const char *cfgfile, 683 const char *cfgfile,
686 const struct GNUNET_CONFIGURATION_Handle *cfg) 684 const struct GNUNET_CONFIGURATION_Handle *cfg)
687{ 685{
688 struct GNUNET_CONFIGURATION_Handle *c; 686 struct GNUNET_CONFIGURATION_Handle *c;
689 687
690 c = (struct GNUNET_CONFIGURATION_Handle *) cfg; 688 c = (struct GNUNET_CONFIGURATION_Handle *)cfg;
691 set_value = GNUNET_NO; 689 set_value = GNUNET_NO;
692 if (NULL == csv_separator) 690 if (NULL == csv_separator)
693 csv_separator = ""; 691 csv_separator = "";
694 if (NULL != args[0]) 692 if (NULL != args[0])
695 {
696 if (1 != sscanf (args[0], "%llu", &set_val))
697 { 693 {
698 fprintf (stderr, _ ("Invalid argument `%s'\n"), args[0]); 694 if (1 != sscanf(args[0], "%llu", &set_val))
699 ret = 1; 695 {
700 return; 696 fprintf(stderr, _("Invalid argument `%s'\n"), args[0]);
697 ret = 1;
698 return;
699 }
700 set_value = GNUNET_YES;
701 } 701 }
702 set_value = GNUNET_YES;
703 }
704 if (NULL != remote_host) 702 if (NULL != remote_host)
705 {
706 if (0 == remote_port)
707 { 703 {
708 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, 704 if (0 == remote_port)
709 "statistics", 705 {
710 "PORT", 706 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number(cfg,
711 &remote_port)) 707 "statistics",
712 { 708 "PORT",
713 fprintf (stderr, 709 &remote_port))
714 _ ("A port is required to connect to host `%s'\n"), 710 {
715 remote_host); 711 fprintf(stderr,
716 return; 712 _("A port is required to connect to host `%s'\n"),
717 } 713 remote_host);
714 return;
715 }
716 }
717 else if (65535 <= remote_port)
718 {
719 fprintf(stderr,
720 _(
721 "A port has to be between 1 and 65535 to connect to host `%s'\n"),
722 remote_host);
723 return;
724 }
725
726 /* Manipulate configuration */
727 GNUNET_CONFIGURATION_set_value_string(c, "statistics", "UNIXPATH", "");
728 GNUNET_CONFIGURATION_set_value_string(c,
729 "statistics",
730 "HOSTNAME",
731 remote_host);
732 GNUNET_CONFIGURATION_set_value_number(c,
733 "statistics",
734 "PORT",
735 remote_port);
718 } 736 }
719 else if (65535 <= remote_port)
720 {
721 fprintf (stderr,
722 _ (
723 "A port has to be between 1 and 65535 to connect to host `%s'\n"),
724 remote_host);
725 return;
726 }
727
728 /* Manipulate configuration */
729 GNUNET_CONFIGURATION_set_value_string (c, "statistics", "UNIXPATH", "");
730 GNUNET_CONFIGURATION_set_value_string (c,
731 "statistics",
732 "HOSTNAME",
733 remote_host);
734 GNUNET_CONFIGURATION_set_value_number (c,
735 "statistics",
736 "PORT",
737 remote_port);
738 }
739 if (NULL == path_testbed) 737 if (NULL == path_testbed)
740 {
741 values = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
742 GNUNET_array_grow (nodes, num_nodes, 1);
743 nodes[0].index_node = 0;
744 nodes[0].conf = c;
745 GNUNET_SCHEDULER_add_now (&main_task, &nodes[0].index_node);
746 }
747 else
748 {
749 if (GNUNET_YES == watch)
750 {
751 printf (
752 _ ("Not able to watch testbed nodes (yet - feel free to implement)\n"));
753 ret = 1;
754 return;
755 }
756 values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
757 if (-1 == discover_testbed_nodes (path_testbed))
758 { 738 {
759 return; 739 values = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_NO);
740 GNUNET_array_grow(nodes, num_nodes, 1);
741 nodes[0].index_node = 0;
742 nodes[0].conf = c;
743 GNUNET_SCHEDULER_add_now(&main_task, &nodes[0].index_node);
760 } 744 }
761 /* For each config/node collect statistics */ 745 else
762 for (unsigned i = 0; i < num_nodes; i++)
763 { 746 {
764 GNUNET_SCHEDULER_add_now (&main_task, &nodes[i].index_node); 747 if (GNUNET_YES == watch)
748 {
749 printf(
750 _("Not able to watch testbed nodes (yet - feel free to implement)\n"));
751 ret = 1;
752 return;
753 }
754 values = GNUNET_CONTAINER_multihashmap_create(4, GNUNET_NO);
755 if (-1 == discover_testbed_nodes(path_testbed))
756 {
757 return;
758 }
759 /* For each config/node collect statistics */
760 for (unsigned i = 0; i < num_nodes; i++)
761 {
762 GNUNET_SCHEDULER_add_now(&main_task, &nodes[i].index_node);
763 }
765 } 764 }
766 }
767} 765}
768 766
769 767
@@ -775,84 +773,85 @@ run (void *cls,
775 * @return 0 ok, 1 on error 773 * @return 0 ok, 1 on error
776 */ 774 */
777int 775int
778main (int argc, char *const *argv) 776main(int argc, char *const *argv)
779{ 777{
780 struct GNUNET_GETOPT_CommandLineOption options[] = 778 struct GNUNET_GETOPT_CommandLineOption options[] =
781 {GNUNET_GETOPT_option_string ( 779 { GNUNET_GETOPT_option_string(
782 'n', 780 'n',
783 "name", 781 "name",
784 "NAME", 782 "NAME",
785 gettext_noop ("limit output to statistics for the given NAME"), 783 gettext_noop("limit output to statistics for the given NAME"),
786 &name), 784 &name),
787 785
788 GNUNET_GETOPT_option_flag ('p', 786 GNUNET_GETOPT_option_flag('p',
789 "persistent", 787 "persistent",
790 gettext_noop ( 788 gettext_noop(
791 "make the value being set persistent"), 789 "make the value being set persistent"),
792 &persistent), 790 &persistent),
793 791
794 GNUNET_GETOPT_option_string ('s', 792 GNUNET_GETOPT_option_string('s',
795 "subsystem", 793 "subsystem",
796 "SUBSYSTEM", 794 "SUBSYSTEM",
797 gettext_noop ( 795 gettext_noop(
798 "limit output to the given SUBSYSTEM"), 796 "limit output to the given SUBSYSTEM"),
799 &subsystem), 797 &subsystem),
800 798
801 GNUNET_GETOPT_option_string ('S', 799 GNUNET_GETOPT_option_string('S',
802 "csv-separator", 800 "csv-separator",
803 "CSV_SEPARATOR", 801 "CSV_SEPARATOR",
804 gettext_noop ("use as csv separator"), 802 gettext_noop("use as csv separator"),
805 &csv_separator), 803 &csv_separator),
806 804
807 GNUNET_GETOPT_option_filename ( 805 GNUNET_GETOPT_option_filename(
808 't', 806 't',
809 "testbed", 807 "testbed",
810 "TESTBED", 808 "TESTBED",
811 gettext_noop ("path to the folder containing the testbed data"), 809 gettext_noop("path to the folder containing the testbed data"),
812 &path_testbed), 810 &path_testbed),
813 811
814 GNUNET_GETOPT_option_flag ('q', 812 GNUNET_GETOPT_option_flag('q',
815 "quiet", 813 "quiet",
816 gettext_noop ( 814 gettext_noop(
817 "just print the statistics value"), 815 "just print the statistics value"),
818 &quiet), 816 &quiet),
819 817
820 GNUNET_GETOPT_option_flag ('w', 818 GNUNET_GETOPT_option_flag('w',
821 "watch", 819 "watch",
822 gettext_noop ("watch value continuously"), 820 gettext_noop("watch value continuously"),
823 &watch), 821 &watch),
824 822
825 GNUNET_GETOPT_option_string ('r', 823 GNUNET_GETOPT_option_string('r',
826 "remote", 824 "remote",
827 "REMOTE", 825 "REMOTE",
828 gettext_noop ("connect to remote host"), 826 gettext_noop("connect to remote host"),
829 &remote_host), 827 &remote_host),
830 828
831 GNUNET_GETOPT_option_ulong ('o', 829 GNUNET_GETOPT_option_ulong('o',
832 "port", 830 "port",
833 "PORT", 831 "PORT",
834 gettext_noop ("port for remote host"), 832 gettext_noop("port for remote host"),
835 &remote_port), 833 &remote_port),
836 834
837 GNUNET_GETOPT_OPTION_END}; 835 GNUNET_GETOPT_OPTION_END };
836
838 remote_port = 0; 837 remote_port = 0;
839 remote_host = NULL; 838 remote_host = NULL;
840 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 839 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
841 return 2; 840 return 2;
842 841
843 ret = (GNUNET_OK == 842 ret = (GNUNET_OK ==
844 GNUNET_PROGRAM_run (argc, 843 GNUNET_PROGRAM_run(argc,
845 argv, 844 argv,
846 "gnunet-statistics [options [value]]", 845 "gnunet-statistics [options [value]]",
847 gettext_noop ( 846 gettext_noop(
848 "Print statistics about GNUnet operations."), 847 "Print statistics about GNUnet operations."),
849 options, 848 options,
850 &run, 849 &run,
851 NULL)) 850 NULL))
852 ? ret 851 ? ret
853 : 1; 852 : 1;
854 GNUNET_free_non_null (remote_host); 853 GNUNET_free_non_null(remote_host);
855 GNUNET_free ((void *) argv); 854 GNUNET_free((void *)argv);
856 return ret; 855 return ret;
857} 856}
858 857
diff --git a/src/statistics/statistics.h b/src/statistics/statistics.h
index f5fa48580..c01dca15a 100644
--- a/src/statistics/statistics.h
+++ b/src/statistics/statistics.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @author Christian Grothoff 22 * @author Christian Grothoff
@@ -37,8 +37,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
37 * The struct is be followed by the service name and 37 * The struct is be followed by the service name and
38 * name of the statistic, both 0-terminated. 38 * name of the statistic, both 0-terminated.
39 */ 39 */
40struct GNUNET_STATISTICS_ReplyMessage 40struct GNUNET_STATISTICS_ReplyMessage {
41{
42 /** 41 /**
43 * Type: GNUNET_MESSAGE_TYPE_STATISTICS_VALUE 42 * Type: GNUNET_MESSAGE_TYPE_STATISTICS_VALUE
44 */ 43 */
@@ -56,14 +55,13 @@ struct GNUNET_STATISTICS_ReplyMessage
56 * The value. 55 * The value.
57 */ 56 */
58 uint64_t value GNUNET_PACKED; 57 uint64_t value GNUNET_PACKED;
59
60}; 58};
61 59
62/** 60/**
63 * Flag for the `struct GNUNET_STATISTICS_ReplyMessage` UID only. 61 * Flag for the `struct GNUNET_STATISTICS_ReplyMessage` UID only.
64 * Note that other messages use #GNUNET_STATISTICS_SETFLAG_PERSISTENT. 62 * Note that other messages use #GNUNET_STATISTICS_SETFLAG_PERSISTENT.
65 */ 63 */
66#define GNUNET_STATISTICS_PERSIST_BIT ((uint32_t) (1LLU<<31)) 64#define GNUNET_STATISTICS_PERSIST_BIT ((uint32_t)(1LLU << 31))
67 65
68/** 66/**
69 * The value being set is an absolute change. 67 * The value being set is an absolute change.
@@ -89,8 +87,7 @@ struct GNUNET_STATISTICS_ReplyMessage
89 * by the subsystem name and the name of 87 * by the subsystem name and the name of
90 * the statistic (each 0-terminated). 88 * the statistic (each 0-terminated).
91 */ 89 */
92struct GNUNET_STATISTICS_SetMessage 90struct GNUNET_STATISTICS_SetMessage {
93{
94 /** 91 /**
95 * Type: #GNUNET_MESSAGE_TYPE_STATISTICS_SET 92 * Type: #GNUNET_MESSAGE_TYPE_STATISTICS_SET
96 */ 93 */
@@ -107,15 +104,13 @@ struct GNUNET_STATISTICS_SetMessage
107 * be signed even though the type given here is unsigned. 104 * be signed even though the type given here is unsigned.
108 */ 105 */
109 uint64_t value GNUNET_PACKED; 106 uint64_t value GNUNET_PACKED;
110
111}; 107};
112 108
113 109
114/** 110/**
115 * Message transmitted if a watched value changes. 111 * Message transmitted if a watched value changes.
116 */ 112 */
117struct GNUNET_STATISTICS_WatchValueMessage 113struct GNUNET_STATISTICS_WatchValueMessage {
118{
119 /** 114 /**
120 * Type: #GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE 115 * Type: #GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE
121 */ 116 */
@@ -145,7 +140,6 @@ struct GNUNET_STATISTICS_WatchValueMessage
145 * be signed even though the type given here is unsigned. 140 * be signed even though the type given here is unsigned.
146 */ 141 */
147 uint64_t value GNUNET_PACKED; 142 uint64_t value GNUNET_PACKED;
148
149}; 143};
150GNUNET_NETWORK_STRUCT_END 144GNUNET_NETWORK_STRUCT_END
151 145
diff --git a/src/statistics/statistics_api.c b/src/statistics/statistics_api.c
index da307c887..487f2569c 100644
--- a/src/statistics/statistics_api.c
+++ b/src/statistics/statistics_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file statistics/statistics_api.c 22 * @file statistics/statistics_api.c
@@ -35,15 +35,14 @@
35 * a value times out? (The update will be lost if the 35 * a value times out? (The update will be lost if the
36 * service does not react within this timeframe). 36 * service does not react within this timeframe).
37 */ 37 */
38#define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2) 38#define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2)
39 39
40#define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from(kind, "statistics-api", __VA_ARGS__)
41 41
42/** 42/**
43 * Types of actions. 43 * Types of actions.
44 */ 44 */
45enum ActionType 45enum ActionType {
46{
47 /** 46 /**
48 * Get a value. 47 * Get a value.
49 */ 48 */
@@ -69,9 +68,7 @@ enum ActionType
69/** 68/**
70 * Entry kept for each value we are watching. 69 * Entry kept for each value we are watching.
71 */ 70 */
72struct GNUNET_STATISTICS_WatchEntry 71struct GNUNET_STATISTICS_WatchEntry {
73{
74
75 /** 72 /**
76 * What subsystem is this action about? (never NULL) 73 * What subsystem is this action about? (never NULL)
77 */ 74 */
@@ -91,16 +88,13 @@ struct GNUNET_STATISTICS_WatchEntry
91 * Closure for @e proc 88 * Closure for @e proc
92 */ 89 */
93 void *proc_cls; 90 void *proc_cls;
94
95}; 91};
96 92
97 93
98/** 94/**
99 * Linked list of things we still need to do. 95 * Linked list of things we still need to do.
100 */ 96 */
101struct GNUNET_STATISTICS_GetHandle 97struct GNUNET_STATISTICS_GetHandle {
102{
103
104 /** 98 /**
105 * This is a doubly linked list. 99 * This is a doubly linked list.
106 */ 100 */
@@ -170,15 +164,13 @@ struct GNUNET_STATISTICS_GetHandle
170 * Size of the message that we will be transmitting. 164 * Size of the message that we will be transmitting.
171 */ 165 */
172 uint16_t msize; 166 uint16_t msize;
173
174}; 167};
175 168
176 169
177/** 170/**
178 * Handle for the service. 171 * Handle for the service.
179 */ 172 */
180struct GNUNET_STATISTICS_Handle 173struct GNUNET_STATISTICS_Handle {
181{
182 /** 174 /**
183 * Name of our subsystem. 175 * Name of our subsystem.
184 */ 176 */
@@ -257,7 +249,6 @@ struct GNUNET_STATISTICS_Handle
257 * Are we currently receiving from the service? 249 * Are we currently receiving from the service?
258 */ 250 */
259 int receiving; 251 int receiving;
260
261}; 252};
262 253
263 254
@@ -266,7 +257,7 @@ struct GNUNET_STATISTICS_Handle
266 * report those as well (if they changed). 257 * report those as well (if they changed).
267 */ 258 */
268static void 259static void
269update_memory_statistics (struct GNUNET_STATISTICS_Handle *h) 260update_memory_statistics(struct GNUNET_STATISTICS_Handle *h)
270{ 261{
271#if ENABLE_HEAP_STATISTICS 262#if ENABLE_HEAP_STATISTICS
272 uint64_t current_heap_size = 0; 263 uint64_t current_heap_size = 0;
@@ -286,28 +277,28 @@ update_memory_statistics (struct GNUNET_STATISTICS_Handle *h)
286 { 277 {
287 struct rusage ru; 278 struct rusage ru;
288 279
289 if (0 == getrusage (RUSAGE_SELF, &ru)) 280 if (0 == getrusage(RUSAGE_SELF, &ru))
290 { 281 {
291 current_rss = 1024LL * ru.ru_maxrss; 282 current_rss = 1024LL * ru.ru_maxrss;
292 } 283 }
293 } 284 }
294#endif 285#endif
295 if (current_heap_size > h->peak_heap_size) 286 if (current_heap_size > h->peak_heap_size)
296 { 287 {
297 h->peak_heap_size = current_heap_size; 288 h->peak_heap_size = current_heap_size;
298 GNUNET_STATISTICS_set (h, 289 GNUNET_STATISTICS_set(h,
299 "# peak heap size", 290 "# peak heap size",
300 current_heap_size, 291 current_heap_size,
301 GNUNET_NO); 292 GNUNET_NO);
302 } 293 }
303 if (current_rss > h->peak_rss) 294 if (current_rss > h->peak_rss)
304 { 295 {
305 h->peak_rss = current_rss; 296 h->peak_rss = current_rss;
306 GNUNET_STATISTICS_set (h, 297 GNUNET_STATISTICS_set(h,
307 "# peak resident set size", 298 "# peak resident set size",
308 current_rss, 299 current_rss,
309 GNUNET_NO); 300 GNUNET_NO);
310 } 301 }
311#endif 302#endif
312} 303}
313 304
@@ -318,7 +309,7 @@ update_memory_statistics (struct GNUNET_STATISTICS_Handle *h)
318 * @param h statistics handle 309 * @param h statistics handle
319 */ 310 */
320static void 311static void
321reconnect_later (struct GNUNET_STATISTICS_Handle *h); 312reconnect_later(struct GNUNET_STATISTICS_Handle *h);
322 313
323 314
324/** 315/**
@@ -327,7 +318,7 @@ reconnect_later (struct GNUNET_STATISTICS_Handle *h);
327 * @param cls statistics handle to reconnect 318 * @param cls statistics handle to reconnect
328 */ 319 */
329static void 320static void
330schedule_action (void *cls); 321schedule_action(void *cls);
331 322
332 323
333/** 324/**
@@ -338,35 +329,35 @@ schedule_action (void *cls);
338 * @param watch watch entry of the value to watch 329 * @param watch watch entry of the value to watch
339 */ 330 */
340static void 331static void
341schedule_watch_request (struct GNUNET_STATISTICS_Handle *h, 332schedule_watch_request(struct GNUNET_STATISTICS_Handle *h,
342 struct GNUNET_STATISTICS_WatchEntry *watch) 333 struct GNUNET_STATISTICS_WatchEntry *watch)
343{ 334{
344 struct GNUNET_STATISTICS_GetHandle *ai; 335 struct GNUNET_STATISTICS_GetHandle *ai;
345 size_t slen; 336 size_t slen;
346 size_t nlen; 337 size_t nlen;
347 size_t nsize; 338 size_t nsize;
348 339
349 slen = strlen (watch->subsystem) + 1; 340 slen = strlen(watch->subsystem) + 1;
350 nlen = strlen (watch->name) + 1; 341 nlen = strlen(watch->name) + 1;
351 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen; 342 nsize = sizeof(struct GNUNET_MessageHeader) + slen + nlen;
352 if (nsize >= GNUNET_MAX_MESSAGE_SIZE) 343 if (nsize >= GNUNET_MAX_MESSAGE_SIZE)
353 { 344 {
354 GNUNET_break (0); 345 GNUNET_break(0);
355 return; 346 return;
356 } 347 }
357 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle); 348 ai = GNUNET_new(struct GNUNET_STATISTICS_GetHandle);
358 ai->sh = h; 349 ai->sh = h;
359 ai->subsystem = GNUNET_strdup (watch->subsystem); 350 ai->subsystem = GNUNET_strdup(watch->subsystem);
360 ai->name = GNUNET_strdup (watch->name); 351 ai->name = GNUNET_strdup(watch->name);
361 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS; 352 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
362 ai->msize = nsize; 353 ai->msize = nsize;
363 ai->type = ACTION_WATCH; 354 ai->type = ACTION_WATCH;
364 ai->proc = watch->proc; 355 ai->proc = watch->proc;
365 ai->cls = watch->proc_cls; 356 ai->cls = watch->proc_cls;
366 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, 357 GNUNET_CONTAINER_DLL_insert_tail(h->action_head,
367 h->action_tail, 358 h->action_tail,
368 ai); 359 ai);
369 schedule_action (h); 360 schedule_action(h);
370} 361}
371 362
372 363
@@ -376,11 +367,11 @@ schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
376 * @param gh action item to free 367 * @param gh action item to free
377 */ 368 */
378static void 369static void
379free_action_item (struct GNUNET_STATISTICS_GetHandle *gh) 370free_action_item(struct GNUNET_STATISTICS_GetHandle *gh)
380{ 371{
381 GNUNET_free_non_null (gh->subsystem); 372 GNUNET_free_non_null(gh->subsystem);
382 GNUNET_free_non_null (gh->name); 373 GNUNET_free_non_null(gh->name);
383 GNUNET_free (gh); 374 GNUNET_free(gh);
384} 375}
385 376
386 377
@@ -390,28 +381,28 @@ free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
390 * @param h statistics handle to disconnect from 381 * @param h statistics handle to disconnect from
391 */ 382 */
392static void 383static void
393do_disconnect (struct GNUNET_STATISTICS_Handle *h) 384do_disconnect(struct GNUNET_STATISTICS_Handle *h)
394{ 385{
395 struct GNUNET_STATISTICS_GetHandle *c; 386 struct GNUNET_STATISTICS_GetHandle *c;
396 387
397 h->receiving = GNUNET_NO; 388 h->receiving = GNUNET_NO;
398 if (NULL != (c = h->current)) 389 if (NULL != (c = h->current))
399 {
400 h->current = NULL;
401 if ( (NULL != c->cont) &&
402 (GNUNET_YES != c->aborted) )
403 { 390 {
404 c->cont (c->cls, 391 h->current = NULL;
405 GNUNET_SYSERR); 392 if ((NULL != c->cont) &&
406 c->cont = NULL; 393 (GNUNET_YES != c->aborted))
394 {
395 c->cont(c->cls,
396 GNUNET_SYSERR);
397 c->cont = NULL;
398 }
399 free_action_item(c);
407 } 400 }
408 free_action_item (c);
409 }
410 if (NULL != h->mq) 401 if (NULL != h->mq)
411 { 402 {
412 GNUNET_MQ_destroy (h->mq); 403 GNUNET_MQ_destroy(h->mq);
413 h->mq = NULL; 404 h->mq = NULL;
414 } 405 }
415} 406}
416 407
417 408
@@ -423,25 +414,25 @@ do_disconnect (struct GNUNET_STATISTICS_Handle *h)
423 * @return #GNUNET_OK if the message was well-formed 414 * @return #GNUNET_OK if the message was well-formed
424 */ 415 */
425static int 416static int
426check_statistics_value (void *cls, 417check_statistics_value(void *cls,
427 const struct GNUNET_STATISTICS_ReplyMessage *smsg) 418 const struct GNUNET_STATISTICS_ReplyMessage *smsg)
428{ 419{
429 const char *service; 420 const char *service;
430 const char *name; 421 const char *name;
431 uint16_t size; 422 uint16_t size;
432 423
433 size = ntohs (smsg->header.size); 424 size = ntohs(smsg->header.size);
434 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage); 425 size -= sizeof(struct GNUNET_STATISTICS_ReplyMessage);
435 if (size != 426 if (size !=
436 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], 427 GNUNET_STRINGS_buffer_tokenize((const char *)&smsg[1],
437 size, 428 size,
438 2, 429 2,
439 &service, 430 &service,
440 &name)) 431 &name))
441 { 432 {
442 GNUNET_break (0); 433 GNUNET_break(0);
443 return GNUNET_SYSERR; 434 return GNUNET_SYSERR;
444 } 435 }
445 return GNUNET_OK; 436 return GNUNET_OK;
446} 437}
447 438
@@ -454,8 +445,8 @@ check_statistics_value (void *cls,
454 * @return #GNUNET_OK if the message was well-formed 445 * @return #GNUNET_OK if the message was well-formed
455 */ 446 */
456static void 447static void
457handle_statistics_value (void *cls, 448handle_statistics_value(void *cls,
458 const struct GNUNET_STATISTICS_ReplyMessage *smsg) 449 const struct GNUNET_STATISTICS_ReplyMessage *smsg)
459{ 450{
460 struct GNUNET_STATISTICS_Handle *h = cls; 451 struct GNUNET_STATISTICS_Handle *h = cls;
461 const char *service; 452 const char *service;
@@ -465,30 +456,30 @@ handle_statistics_value (void *cls,
465 if (h->current->aborted) 456 if (h->current->aborted)
466 return; /* iteration aborted, don't bother */ 457 return; /* iteration aborted, don't bother */
467 458
468 size = ntohs (smsg->header.size); 459 size = ntohs(smsg->header.size);
469 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage); 460 size -= sizeof(struct GNUNET_STATISTICS_ReplyMessage);
470 GNUNET_assert (size == 461 GNUNET_assert(size ==
471 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], 462 GNUNET_STRINGS_buffer_tokenize((const char *)&smsg[1],
472 size, 463 size,
473 2, 464 2,
474 &service, 465 &service,
475 &name)); 466 &name));
476 LOG (GNUNET_ERROR_TYPE_DEBUG, 467 LOG(GNUNET_ERROR_TYPE_DEBUG,
477 "Received valid statistic on `%s:%s': %llu\n", 468 "Received valid statistic on `%s:%s': %llu\n",
478 service, name, 469 service, name,
479 GNUNET_ntohll (smsg->value)); 470 GNUNET_ntohll(smsg->value));
480 if (GNUNET_OK != 471 if (GNUNET_OK !=
481 h->current->proc (h->current->cls, 472 h->current->proc(h->current->cls,
482 service, 473 service,
483 name, 474 name,
484 GNUNET_ntohll (smsg->value), 475 GNUNET_ntohll(smsg->value),
485 0 != 476 0 !=
486 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT))) 477 (ntohl(smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
487 { 478 {
488 LOG (GNUNET_ERROR_TYPE_DEBUG, 479 LOG(GNUNET_ERROR_TYPE_DEBUG,
489 "Processing of remaining statistics aborted by client.\n"); 480 "Processing of remaining statistics aborted by client.\n");
490 h->current->aborted = GNUNET_YES; 481 h->current->aborted = GNUNET_YES;
491 } 482 }
492} 483}
493 484
494 485
@@ -499,29 +490,29 @@ handle_statistics_value (void *cls,
499 * @param msg the watch value message 490 * @param msg the watch value message
500 */ 491 */
501static void 492static void
502handle_statistics_watch_value (void *cls, 493handle_statistics_watch_value(void *cls,
503 const struct GNUNET_STATISTICS_WatchValueMessage *wvm) 494 const struct GNUNET_STATISTICS_WatchValueMessage *wvm)
504{ 495{
505 struct GNUNET_STATISTICS_Handle *h = cls; 496 struct GNUNET_STATISTICS_Handle *h = cls;
506 struct GNUNET_STATISTICS_WatchEntry *w; 497 struct GNUNET_STATISTICS_WatchEntry *w;
507 uint32_t wid; 498 uint32_t wid;
508 499
509 GNUNET_break (0 == ntohl (wvm->reserved)); 500 GNUNET_break(0 == ntohl(wvm->reserved));
510 wid = ntohl (wvm->wid); 501 wid = ntohl(wvm->wid);
511 if (wid >= h->watches_size) 502 if (wid >= h->watches_size)
512 { 503 {
513 do_disconnect (h); 504 do_disconnect(h);
514 reconnect_later (h); 505 reconnect_later(h);
515 return; 506 return;
516 } 507 }
517 w = h->watches[wid]; 508 w = h->watches[wid];
518 if (NULL == w) 509 if (NULL == w)
519 return; 510 return;
520 (void) w->proc (w->proc_cls, 511 (void)w->proc(w->proc_cls,
521 w->subsystem, 512 w->subsystem,
522 w->name, 513 w->name,
523 GNUNET_ntohll (wvm->value), 514 GNUNET_ntohll(wvm->value),
524 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT)); 515 0 != (ntohl(wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
525} 516}
526 517
527 518
@@ -534,25 +525,25 @@ handle_statistics_watch_value (void *cls,
534 * @param error error code 525 * @param error error code
535 */ 526 */
536static void 527static void
537mq_error_handler (void *cls, 528mq_error_handler(void *cls,
538 enum GNUNET_MQ_Error error) 529 enum GNUNET_MQ_Error error)
539{ 530{
540 struct GNUNET_STATISTICS_Handle *h = cls; 531 struct GNUNET_STATISTICS_Handle *h = cls;
541 532
542 if (GNUNET_NO != h->do_destroy) 533 if (GNUNET_NO != h->do_destroy)
543 {
544 h->do_destroy = GNUNET_NO;
545 if (NULL != h->destroy_task)
546 { 534 {
547 GNUNET_SCHEDULER_cancel (h->destroy_task); 535 h->do_destroy = GNUNET_NO;
548 h->destroy_task = NULL; 536 if (NULL != h->destroy_task)
537 {
538 GNUNET_SCHEDULER_cancel(h->destroy_task);
539 h->destroy_task = NULL;
540 }
541 GNUNET_STATISTICS_destroy(h,
542 GNUNET_NO);
543 return;
549 } 544 }
550 GNUNET_STATISTICS_destroy (h, 545 do_disconnect(h);
551 GNUNET_NO); 546 reconnect_later(h);
552 return;
553 }
554 do_disconnect (h);
555 reconnect_later (h);
556} 547}
557 548
558 549
@@ -562,16 +553,16 @@ mq_error_handler (void *cls,
562 * @param cls the `struct GNUNET_STATISTICS_Handle` 553 * @param cls the `struct GNUNET_STATISTICS_Handle`
563 */ 554 */
564static void 555static void
565do_destroy (void *cls) 556do_destroy(void *cls)
566{ 557{
567 struct GNUNET_STATISTICS_Handle *h = cls; 558 struct GNUNET_STATISTICS_Handle *h = cls;
568 559
569 h->destroy_task = NULL; 560 h->destroy_task = NULL;
570 h->do_destroy = GNUNET_NO; 561 h->do_destroy = GNUNET_NO;
571 LOG (GNUNET_ERROR_TYPE_DEBUG, 562 LOG(GNUNET_ERROR_TYPE_DEBUG,
572 "Running final destruction\n"); 563 "Running final destruction\n");
573 GNUNET_STATISTICS_destroy (h, 564 GNUNET_STATISTICS_destroy(h,
574 GNUNET_NO); 565 GNUNET_NO);
575} 566}
576 567
577 568
@@ -584,25 +575,25 @@ do_destroy (void *cls)
584 * @param msg the message 575 * @param msg the message
585 */ 576 */
586static void 577static void
587handle_disconnect_confirm (void *cls, 578handle_disconnect_confirm(void *cls,
588 const struct GNUNET_MessageHeader *msg) 579 const struct GNUNET_MessageHeader *msg)
589{ 580{
590 struct GNUNET_STATISTICS_Handle *h = cls; 581 struct GNUNET_STATISTICS_Handle *h = cls;
591 582
592 if (GNUNET_SYSERR != h->do_destroy) 583 if (GNUNET_SYSERR != h->do_destroy)
593 { 584 {
594 /* not in shutdown, why do we get 'TEST'? */ 585 /* not in shutdown, why do we get 'TEST'? */
595 GNUNET_break (0); 586 GNUNET_break(0);
596 do_disconnect (h); 587 do_disconnect(h);
597 reconnect_later (h); 588 reconnect_later(h);
598 return; 589 return;
599 } 590 }
600 LOG (GNUNET_ERROR_TYPE_DEBUG, 591 LOG(GNUNET_ERROR_TYPE_DEBUG,
601 "Received DISCONNNECT_CONFIRM message from statistics, can complete disconnect\n"); 592 "Received DISCONNNECT_CONFIRM message from statistics, can complete disconnect\n");
602 if (NULL != h->destroy_task) 593 if (NULL != h->destroy_task)
603 GNUNET_SCHEDULER_cancel (h->destroy_task); 594 GNUNET_SCHEDULER_cancel(h->destroy_task);
604 h->destroy_task = GNUNET_SCHEDULER_add_now (&do_destroy, 595 h->destroy_task = GNUNET_SCHEDULER_add_now(&do_destroy,
605 h); 596 h);
606} 597}
607 598
608 599
@@ -615,31 +606,31 @@ handle_disconnect_confirm (void *cls,
615 * @param msg the message 606 * @param msg the message
616 */ 607 */
617static void 608static void
618handle_statistics_end (void *cls, 609handle_statistics_end(void *cls,
619 const struct GNUNET_MessageHeader *msg) 610 const struct GNUNET_MessageHeader *msg)
620{ 611{
621 struct GNUNET_STATISTICS_Handle *h = cls; 612 struct GNUNET_STATISTICS_Handle *h = cls;
622 struct GNUNET_STATISTICS_GetHandle *c; 613 struct GNUNET_STATISTICS_GetHandle *c;
623 614
624 LOG (GNUNET_ERROR_TYPE_DEBUG, 615 LOG(GNUNET_ERROR_TYPE_DEBUG,
625 "Received end of statistics marker\n"); 616 "Received end of statistics marker\n");
626 if (NULL == (c = h->current)) 617 if (NULL == (c = h->current))
627 { 618 {
628 GNUNET_break (0); 619 GNUNET_break(0);
629 do_disconnect (h); 620 do_disconnect(h);
630 reconnect_later (h); 621 reconnect_later(h);
631 return; 622 return;
632 } 623 }
633 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS; 624 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
634 h->current = NULL; 625 h->current = NULL;
635 schedule_action (h); 626 schedule_action(h);
636 if (NULL != c->cont) 627 if (NULL != c->cont)
637 { 628 {
638 c->cont (c->cls, 629 c->cont(c->cls,
639 GNUNET_OK); 630 GNUNET_OK);
640 c->cont = NULL; 631 c->cont = NULL;
641 } 632 }
642 free_action_item (c); 633 free_action_item(c);
643} 634}
644 635
645 636
@@ -650,26 +641,26 @@ handle_statistics_end (void *cls,
650 * @return #GNUNET_YES on success, #GNUNET_NO on failure. 641 * @return #GNUNET_YES on success, #GNUNET_NO on failure.
651 */ 642 */
652static int 643static int
653try_connect (struct GNUNET_STATISTICS_Handle *h) 644try_connect(struct GNUNET_STATISTICS_Handle *h)
654{ 645{
655 struct GNUNET_MQ_MessageHandler handlers[] = { 646 struct GNUNET_MQ_MessageHandler handlers[] = {
656 GNUNET_MQ_hd_fixed_size (disconnect_confirm, 647 GNUNET_MQ_hd_fixed_size(disconnect_confirm,
657 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM, 648 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM,
658 struct GNUNET_MessageHeader, 649 struct GNUNET_MessageHeader,
659 h), 650 h),
660 GNUNET_MQ_hd_fixed_size (statistics_end, 651 GNUNET_MQ_hd_fixed_size(statistics_end,
661 GNUNET_MESSAGE_TYPE_STATISTICS_END, 652 GNUNET_MESSAGE_TYPE_STATISTICS_END,
662 struct GNUNET_MessageHeader, 653 struct GNUNET_MessageHeader,
663 h), 654 h),
664 GNUNET_MQ_hd_var_size (statistics_value, 655 GNUNET_MQ_hd_var_size(statistics_value,
665 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE, 656 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE,
666 struct GNUNET_STATISTICS_ReplyMessage, 657 struct GNUNET_STATISTICS_ReplyMessage,
667 h), 658 h),
668 GNUNET_MQ_hd_fixed_size (statistics_watch_value, 659 GNUNET_MQ_hd_fixed_size(statistics_watch_value,
669 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE, 660 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE,
670 struct GNUNET_STATISTICS_WatchValueMessage, 661 struct GNUNET_STATISTICS_WatchValueMessage,
671 h), 662 h),
672 GNUNET_MQ_handler_end () 663 GNUNET_MQ_handler_end()
673 }; 664 };
674 struct GNUNET_STATISTICS_GetHandle *gh; 665 struct GNUNET_STATISTICS_GetHandle *gh;
675 struct GNUNET_STATISTICS_GetHandle *gn; 666 struct GNUNET_STATISTICS_GetHandle *gn;
@@ -678,33 +669,33 @@ try_connect (struct GNUNET_STATISTICS_Handle *h)
678 return GNUNET_NO; 669 return GNUNET_NO;
679 if (NULL != h->mq) 670 if (NULL != h->mq)
680 return GNUNET_YES; 671 return GNUNET_YES;
681 h->mq = GNUNET_CLIENT_connect (h->cfg, 672 h->mq = GNUNET_CLIENT_connect(h->cfg,
682 "statistics", 673 "statistics",
683 handlers, 674 handlers,
684 &mq_error_handler, 675 &mq_error_handler,
685 h); 676 h);
686 if (NULL == h->mq) 677 if (NULL == h->mq)
687 { 678 {
688 LOG (GNUNET_ERROR_TYPE_DEBUG, 679 LOG(GNUNET_ERROR_TYPE_DEBUG,
689 "Failed to connect to statistics service!\n"); 680 "Failed to connect to statistics service!\n");
690 return GNUNET_NO; 681 return GNUNET_NO;
691 } 682 }
692 gn = h->action_head; 683 gn = h->action_head;
693 while (NULL != (gh = gn)) 684 while (NULL != (gh = gn))
694 {
695 gn = gh->next;
696 if (gh->type == ACTION_WATCH)
697 { 685 {
698 GNUNET_CONTAINER_DLL_remove (h->action_head, 686 gn = gh->next;
699 h->action_tail, 687 if (gh->type == ACTION_WATCH)
700 gh); 688 {
701 free_action_item (gh); 689 GNUNET_CONTAINER_DLL_remove(h->action_head,
690 h->action_tail,
691 gh);
692 free_action_item(gh);
693 }
702 } 694 }
703 }
704 for (unsigned int i = 0; i < h->watches_size; i++) 695 for (unsigned int i = 0; i < h->watches_size; i++)
705 if (NULL != h->watches[i]) 696 if (NULL != h->watches[i])
706 schedule_watch_request (h, 697 schedule_watch_request(h,
707 h->watches[i]); 698 h->watches[i]);
708 return GNUNET_YES; 699 return GNUNET_YES;
709} 700}
710 701
@@ -715,12 +706,12 @@ try_connect (struct GNUNET_STATISTICS_Handle *h)
715 * @param cls the `struct GNUNET_STATISTICS_Handle` to reconnect 706 * @param cls the `struct GNUNET_STATISTICS_Handle` to reconnect
716 */ 707 */
717static void 708static void
718reconnect_task (void *cls) 709reconnect_task(void *cls)
719{ 710{
720 struct GNUNET_STATISTICS_Handle *h = cls; 711 struct GNUNET_STATISTICS_Handle *h = cls;
721 712
722 h->backoff_task = NULL; 713 h->backoff_task = NULL;
723 schedule_action (h); 714 schedule_action(h);
724} 715}
725 716
726 717
@@ -730,38 +721,38 @@ reconnect_task (void *cls)
730 * @param h statistics handle 721 * @param h statistics handle
731 */ 722 */
732static void 723static void
733reconnect_later (struct GNUNET_STATISTICS_Handle *h) 724reconnect_later(struct GNUNET_STATISTICS_Handle *h)
734{ 725{
735 int loss; 726 int loss;
736 struct GNUNET_STATISTICS_GetHandle *gh; 727 struct GNUNET_STATISTICS_GetHandle *gh;
737 728
738 GNUNET_assert (NULL == h->backoff_task); 729 GNUNET_assert(NULL == h->backoff_task);
739 if (GNUNET_YES == h->do_destroy) 730 if (GNUNET_YES == h->do_destroy)
740 { 731 {
741 /* So we are shutting down and the service is not reachable. 732 /* So we are shutting down and the service is not reachable.
742 * Chances are that it's down for good and we are not going to connect to 733 * Chances are that it's down for good and we are not going to connect to
743 * it anymore. 734 * it anymore.
744 * Give up and don't sync the rest of the data. 735 * Give up and don't sync the rest of the data.
745 */ 736 */
746 loss = GNUNET_NO; 737 loss = GNUNET_NO;
747 for (gh = h->action_head; NULL != gh; gh = gh->next) 738 for (gh = h->action_head; NULL != gh; gh = gh->next)
748 if ( (gh->make_persistent) && 739 if ((gh->make_persistent) &&
749 (ACTION_SET == gh->type) ) 740 (ACTION_SET == gh->type))
750 loss = GNUNET_YES; 741 loss = GNUNET_YES;
751 if (GNUNET_YES == loss) 742 if (GNUNET_YES == loss)
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 743 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
753 _("Could not save some persistent statistics\n")); 744 _("Could not save some persistent statistics\n"));
754 if (NULL != h->destroy_task) 745 if (NULL != h->destroy_task)
755 GNUNET_SCHEDULER_cancel (h->destroy_task); 746 GNUNET_SCHEDULER_cancel(h->destroy_task);
756 h->destroy_task = GNUNET_SCHEDULER_add_now (&do_destroy, 747 h->destroy_task = GNUNET_SCHEDULER_add_now(&do_destroy,
757 h); 748 h);
758 return; 749 return;
759 } 750 }
760 h->backoff_task 751 h->backoff_task
761 = GNUNET_SCHEDULER_add_delayed (h->backoff, 752 = GNUNET_SCHEDULER_add_delayed(h->backoff,
762 &reconnect_task, 753 &reconnect_task,
763 h); 754 h);
764 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff); 755 h->backoff = GNUNET_TIME_STD_BACKOFF(h->backoff);
765} 756}
766 757
767 758
@@ -773,7 +764,7 @@ reconnect_later (struct GNUNET_STATISTICS_Handle *h)
773 * @param handle statistics handle 764 * @param handle statistics handle
774 */ 765 */
775static void 766static void
776transmit_get (struct GNUNET_STATISTICS_Handle *handle) 767transmit_get(struct GNUNET_STATISTICS_Handle *handle)
777{ 768{
778 struct GNUNET_STATISTICS_GetHandle *c; 769 struct GNUNET_STATISTICS_GetHandle *c;
779 struct GNUNET_MessageHeader *hdr; 770 struct GNUNET_MessageHeader *hdr;
@@ -781,23 +772,23 @@ transmit_get (struct GNUNET_STATISTICS_Handle *handle)
781 size_t slen1; 772 size_t slen1;
782 size_t slen2; 773 size_t slen2;
783 774
784 GNUNET_assert (NULL != (c = handle->current)); 775 GNUNET_assert(NULL != (c = handle->current));
785 slen1 = strlen (c->subsystem) + 1; 776 slen1 = strlen(c->subsystem) + 1;
786 slen2 = strlen (c->name) + 1; 777 slen2 = strlen(c->name) + 1;
787 env = GNUNET_MQ_msg_extra (hdr, 778 env = GNUNET_MQ_msg_extra(hdr,
788 slen1 + slen2, 779 slen1 + slen2,
789 GNUNET_MESSAGE_TYPE_STATISTICS_GET); 780 GNUNET_MESSAGE_TYPE_STATISTICS_GET);
790 GNUNET_assert (slen1 + slen2 == 781 GNUNET_assert(slen1 + slen2 ==
791 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], 782 GNUNET_STRINGS_buffer_fill((char *)&hdr[1],
792 slen1 + slen2, 783 slen1 + slen2,
793 2, 784 2,
794 c->subsystem, 785 c->subsystem,
795 c->name)); 786 c->name));
796 GNUNET_MQ_notify_sent (env, 787 GNUNET_MQ_notify_sent(env,
797 &schedule_action, 788 &schedule_action,
798 handle); 789 handle);
799 GNUNET_MQ_send (handle->mq, 790 GNUNET_MQ_send(handle->mq,
800 env); 791 env);
801} 792}
802 793
803 794
@@ -808,36 +799,36 @@ transmit_get (struct GNUNET_STATISTICS_Handle *handle)
808 * @param handle statistics handle 799 * @param handle statistics handle
809 */ 800 */
810static void 801static void
811transmit_watch (struct GNUNET_STATISTICS_Handle *handle) 802transmit_watch(struct GNUNET_STATISTICS_Handle *handle)
812{ 803{
813 struct GNUNET_MessageHeader *hdr; 804 struct GNUNET_MessageHeader *hdr;
814 struct GNUNET_MQ_Envelope *env; 805 struct GNUNET_MQ_Envelope *env;
815 size_t slen1; 806 size_t slen1;
816 size_t slen2; 807 size_t slen2;
817 808
818 LOG (GNUNET_ERROR_TYPE_DEBUG, 809 LOG(GNUNET_ERROR_TYPE_DEBUG,
819 "Transmitting watch request for `%s'\n", 810 "Transmitting watch request for `%s'\n",
820 handle->current->name); 811 handle->current->name);
821 slen1 = strlen (handle->current->subsystem) + 1; 812 slen1 = strlen(handle->current->subsystem) + 1;
822 slen2 = strlen (handle->current->name) + 1; 813 slen2 = strlen(handle->current->name) + 1;
823 env = GNUNET_MQ_msg_extra (hdr, 814 env = GNUNET_MQ_msg_extra(hdr,
824 slen1 + slen2, 815 slen1 + slen2,
825 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH); 816 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
826 GNUNET_assert (slen1 + slen2 == 817 GNUNET_assert(slen1 + slen2 ==
827 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], 818 GNUNET_STRINGS_buffer_fill((char *)&hdr[1],
828 slen1 + slen2, 819 slen1 + slen2,
829 2, 820 2,
830 handle->current->subsystem, 821 handle->current->subsystem,
831 handle->current->name)); 822 handle->current->name));
832 GNUNET_MQ_notify_sent (env, 823 GNUNET_MQ_notify_sent(env,
833 &schedule_action, 824 &schedule_action,
834 handle); 825 handle);
835 GNUNET_MQ_send (handle->mq, 826 GNUNET_MQ_send(handle->mq,
836 env); 827 env);
837 GNUNET_assert (NULL == handle->current->cont); 828 GNUNET_assert(NULL == handle->current->cont);
838 free_action_item (handle->current); 829 free_action_item(handle->current);
839 handle->current = NULL; 830 handle->current = NULL;
840 schedule_action (handle); 831 schedule_action(handle);
841} 832}
842 833
843 834
@@ -847,39 +838,39 @@ transmit_watch (struct GNUNET_STATISTICS_Handle *handle)
847 * @param handle statistics handle 838 * @param handle statistics handle
848 */ 839 */
849static void 840static void
850transmit_set (struct GNUNET_STATISTICS_Handle *handle) 841transmit_set(struct GNUNET_STATISTICS_Handle *handle)
851{ 842{
852 struct GNUNET_STATISTICS_SetMessage *r; 843 struct GNUNET_STATISTICS_SetMessage *r;
853 struct GNUNET_MQ_Envelope *env; 844 struct GNUNET_MQ_Envelope *env;
854 size_t slen; 845 size_t slen;
855 size_t nlen; 846 size_t nlen;
856 847
857 slen = strlen (handle->current->subsystem) + 1; 848 slen = strlen(handle->current->subsystem) + 1;
858 nlen = strlen (handle->current->name) + 1; 849 nlen = strlen(handle->current->name) + 1;
859 env = GNUNET_MQ_msg_extra (r, 850 env = GNUNET_MQ_msg_extra(r,
860 slen + nlen, 851 slen + nlen,
861 GNUNET_MESSAGE_TYPE_STATISTICS_SET); 852 GNUNET_MESSAGE_TYPE_STATISTICS_SET);
862 r->flags = 0; 853 r->flags = 0;
863 r->value = GNUNET_htonll (handle->current->value); 854 r->value = GNUNET_htonll(handle->current->value);
864 if (handle->current->make_persistent) 855 if (handle->current->make_persistent)
865 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT); 856 r->flags |= htonl(GNUNET_STATISTICS_SETFLAG_PERSISTENT);
866 if (handle->current->type == ACTION_UPDATE) 857 if (handle->current->type == ACTION_UPDATE)
867 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE); 858 r->flags |= htonl(GNUNET_STATISTICS_SETFLAG_RELATIVE);
868 GNUNET_assert (slen + nlen == 859 GNUNET_assert(slen + nlen ==
869 GNUNET_STRINGS_buffer_fill ((char *) &r[1], 860 GNUNET_STRINGS_buffer_fill((char *)&r[1],
870 slen + nlen, 861 slen + nlen,
871 2, 862 2,
872 handle->current->subsystem, 863 handle->current->subsystem,
873 handle->current->name)); 864 handle->current->name));
874 GNUNET_assert (NULL == handle->current->cont); 865 GNUNET_assert(NULL == handle->current->cont);
875 free_action_item (handle->current); 866 free_action_item(handle->current);
876 handle->current = NULL; 867 handle->current = NULL;
877 update_memory_statistics (handle); 868 update_memory_statistics(handle);
878 GNUNET_MQ_notify_sent (env, 869 GNUNET_MQ_notify_sent(env,
879 &schedule_action, 870 &schedule_action,
880 handle); 871 handle);
881 GNUNET_MQ_send (handle->mq, 872 GNUNET_MQ_send(handle->mq,
882 env); 873 env);
883} 874}
884 875
885 876
@@ -891,19 +882,19 @@ transmit_set (struct GNUNET_STATISTICS_Handle *handle)
891 * @return handle to use 882 * @return handle to use
892 */ 883 */
893struct GNUNET_STATISTICS_Handle * 884struct GNUNET_STATISTICS_Handle *
894GNUNET_STATISTICS_create (const char *subsystem, 885GNUNET_STATISTICS_create(const char *subsystem,
895 const struct GNUNET_CONFIGURATION_Handle *cfg) 886 const struct GNUNET_CONFIGURATION_Handle *cfg)
896{ 887{
897 struct GNUNET_STATISTICS_Handle *h; 888 struct GNUNET_STATISTICS_Handle *h;
898 889
899 if (GNUNET_YES == 890 if (GNUNET_YES ==
900 GNUNET_CONFIGURATION_get_value_yesno (cfg, 891 GNUNET_CONFIGURATION_get_value_yesno(cfg,
901 "statistics", 892 "statistics",
902 "DISABLE")) 893 "DISABLE"))
903 return NULL; 894 return NULL;
904 h = GNUNET_new (struct GNUNET_STATISTICS_Handle); 895 h = GNUNET_new(struct GNUNET_STATISTICS_Handle);
905 h->cfg = cfg; 896 h->cfg = cfg;
906 h->subsystem = GNUNET_strdup (subsystem); 897 h->subsystem = GNUNET_strdup(subsystem);
907 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS; 898 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
908 return h; 899 return h;
909} 900}
@@ -918,82 +909,82 @@ GNUNET_STATISTICS_create (const char *subsystem,
918 * be completed 909 * be completed
919 */ 910 */
920void 911void
921GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, 912GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h,
922 int sync_first) 913 int sync_first)
923{ 914{
924 struct GNUNET_STATISTICS_GetHandle *pos; 915 struct GNUNET_STATISTICS_GetHandle *pos;
925 struct GNUNET_STATISTICS_GetHandle *next; 916 struct GNUNET_STATISTICS_GetHandle *next;
926 917
927 if (NULL == h) 918 if (NULL == h)
928 return; 919 return;
929 GNUNET_assert (GNUNET_NO == h->do_destroy); /* Don't call twice. */ 920 GNUNET_assert(GNUNET_NO == h->do_destroy); /* Don't call twice. */
930 if ( (sync_first) && 921 if ((sync_first) &&
931 (NULL != h->mq) && 922 (NULL != h->mq) &&
932 (0 != GNUNET_MQ_get_length (h->mq)) ) 923 (0 != GNUNET_MQ_get_length(h->mq)))
933 {
934 if ( (NULL != h->current) &&
935 (ACTION_GET == h->current->type) )
936 h->current->aborted = GNUNET_YES;
937 next = h->action_head;
938 while (NULL != (pos = next))
939 { 924 {
940 next = pos->next; 925 if ((NULL != h->current) &&
941 if ( (ACTION_GET == pos->type) || 926 (ACTION_GET == h->current->type))
942 (ACTION_WATCH == pos->type) ) 927 h->current->aborted = GNUNET_YES;
943 { 928 next = h->action_head;
944 GNUNET_CONTAINER_DLL_remove (h->action_head, 929 while (NULL != (pos = next))
945 h->action_tail, 930 {
946 pos); 931 next = pos->next;
947 free_action_item (pos); 932 if ((ACTION_GET == pos->type) ||
948 } 933 (ACTION_WATCH == pos->type))
949 } 934 {
950 h->do_destroy = GNUNET_YES; 935 GNUNET_CONTAINER_DLL_remove(h->action_head,
951 schedule_action (h); 936 h->action_tail,
952 GNUNET_assert (NULL == h->destroy_task); 937 pos);
953 h->destroy_task 938 free_action_item(pos);
954 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (h->backoff, 939 }
940 }
941 h->do_destroy = GNUNET_YES;
942 schedule_action(h);
943 GNUNET_assert(NULL == h->destroy_task);
944 h->destroy_task
945 = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(h->backoff,
955 5), 946 5),
956 &do_destroy, 947 &do_destroy,
957 h); 948 h);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 949 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
959 "Deferring destruction\n"); 950 "Deferring destruction\n");
960 return; /* do not finish destruction just yet */ 951 return; /* do not finish destruction just yet */
961 } 952 }
962 /* do clean up all */ 953 /* do clean up all */
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 954 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
964 "Cleaning all up\n"); 955 "Cleaning all up\n");
965 while (NULL != (pos = h->action_head)) 956 while (NULL != (pos = h->action_head))
966 { 957 {
967 GNUNET_CONTAINER_DLL_remove (h->action_head, 958 GNUNET_CONTAINER_DLL_remove(h->action_head,
968 h->action_tail, 959 h->action_tail,
969 pos); 960 pos);
970 free_action_item (pos); 961 free_action_item(pos);
971 } 962 }
972 do_disconnect (h); 963 do_disconnect(h);
973 if (NULL != h->backoff_task) 964 if (NULL != h->backoff_task)
974 { 965 {
975 GNUNET_SCHEDULER_cancel (h->backoff_task); 966 GNUNET_SCHEDULER_cancel(h->backoff_task);
976 h->backoff_task = NULL; 967 h->backoff_task = NULL;
977 } 968 }
978 if (NULL != h->destroy_task) 969 if (NULL != h->destroy_task)
979 { 970 {
980 GNUNET_break (0); 971 GNUNET_break(0);
981 GNUNET_SCHEDULER_cancel (h->destroy_task); 972 GNUNET_SCHEDULER_cancel(h->destroy_task);
982 h->destroy_task = NULL; 973 h->destroy_task = NULL;
983 } 974 }
984 for (unsigned int i = 0; i < h->watches_size; i++) 975 for (unsigned int i = 0; i < h->watches_size; i++)
985 { 976 {
986 if (NULL == h->watches[i]) 977 if (NULL == h->watches[i])
987 continue; 978 continue;
988 GNUNET_free (h->watches[i]->subsystem); 979 GNUNET_free(h->watches[i]->subsystem);
989 GNUNET_free (h->watches[i]->name); 980 GNUNET_free(h->watches[i]->name);
990 GNUNET_free (h->watches[i]); 981 GNUNET_free(h->watches[i]);
991 } 982 }
992 GNUNET_array_grow (h->watches, 983 GNUNET_array_grow(h->watches,
993 h->watches_size, 984 h->watches_size,
994 0); 985 0);
995 GNUNET_free (h->subsystem); 986 GNUNET_free(h->subsystem);
996 GNUNET_free (h); 987 GNUNET_free(h);
997} 988}
998 989
999 990
@@ -1003,63 +994,66 @@ GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
1003 * @param cls statistics handle 994 * @param cls statistics handle
1004 */ 995 */
1005static void 996static void
1006schedule_action (void *cls) 997schedule_action(void *cls)
1007{ 998{
1008 struct GNUNET_STATISTICS_Handle *h = cls; 999 struct GNUNET_STATISTICS_Handle *h = cls;
1009 1000
1010 if (NULL != h->backoff_task) 1001 if (NULL != h->backoff_task)
1011 return; /* action already pending */ 1002 return; /* action already pending */
1012 if (GNUNET_YES != try_connect (h)) 1003 if (GNUNET_YES != try_connect(h))
1013 {
1014 reconnect_later (h);
1015 return;
1016 }
1017 if (0 < GNUNET_MQ_get_length (h->mq))
1018 return; /* Wait for queue to be reduced more */
1019 /* schedule next action */
1020 while (NULL == h->current)
1021 {
1022 h->current = h->action_head;
1023 if (NULL == h->current)
1024 { 1004 {
1025 struct GNUNET_MessageHeader *hdr; 1005 reconnect_later(h);
1026 struct GNUNET_MQ_Envelope *env;
1027
1028 if (GNUNET_YES != h->do_destroy)
1029 return; /* nothing to do */
1030 /* let service know that we're done */
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Notifying service that we are done\n");
1033 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1034 env = GNUNET_MQ_msg (hdr,
1035 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT);
1036 GNUNET_MQ_notify_sent (env,
1037 &schedule_action,
1038 h);
1039 GNUNET_MQ_send (h->mq,
1040 env);
1041 return; 1006 return;
1042 } 1007 }
1043 GNUNET_CONTAINER_DLL_remove (h->action_head, 1008 if (0 < GNUNET_MQ_get_length(h->mq))
1044 h->action_tail, 1009 return; /* Wait for queue to be reduced more */
1045 h->current); 1010 /* schedule next action */
1046 switch (h->current->type) 1011 while (NULL == h->current)
1047 { 1012 {
1048 case ACTION_GET: 1013 h->current = h->action_head;
1049 transmit_get (h); 1014 if (NULL == h->current)
1050 break; 1015 {
1051 case ACTION_SET: 1016 struct GNUNET_MessageHeader *hdr;
1052 case ACTION_UPDATE: 1017 struct GNUNET_MQ_Envelope *env;
1053 transmit_set (h); 1018
1054 break; 1019 if (GNUNET_YES != h->do_destroy)
1055 case ACTION_WATCH: 1020 return; /* nothing to do */
1056 transmit_watch (h); 1021 /* let service know that we're done */
1057 break; 1022 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1058 default: 1023 "Notifying service that we are done\n");
1059 GNUNET_assert (0); 1024 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1060 break; 1025 env = GNUNET_MQ_msg(hdr,
1026 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT);
1027 GNUNET_MQ_notify_sent(env,
1028 &schedule_action,
1029 h);
1030 GNUNET_MQ_send(h->mq,
1031 env);
1032 return;
1033 }
1034 GNUNET_CONTAINER_DLL_remove(h->action_head,
1035 h->action_tail,
1036 h->current);
1037 switch (h->current->type)
1038 {
1039 case ACTION_GET:
1040 transmit_get(h);
1041 break;
1042
1043 case ACTION_SET:
1044 case ACTION_UPDATE:
1045 transmit_set(h);
1046 break;
1047
1048 case ACTION_WATCH:
1049 transmit_watch(h);
1050 break;
1051
1052 default:
1053 GNUNET_assert(0);
1054 break;
1055 }
1061 } 1056 }
1062 }
1063} 1057}
1064 1058
1065 1059
@@ -1076,12 +1070,12 @@ schedule_action (void *cls)
1076 * @return NULL on error 1070 * @return NULL on error
1077 */ 1071 */
1078struct GNUNET_STATISTICS_GetHandle * 1072struct GNUNET_STATISTICS_GetHandle *
1079GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle, 1073GNUNET_STATISTICS_get(struct GNUNET_STATISTICS_Handle *handle,
1080 const char *subsystem, 1074 const char *subsystem,
1081 const char *name, 1075 const char *name,
1082 GNUNET_STATISTICS_Callback cont, 1076 GNUNET_STATISTICS_Callback cont,
1083 GNUNET_STATISTICS_Iterator proc, 1077 GNUNET_STATISTICS_Iterator proc,
1084 void *cls) 1078 void *cls)
1085{ 1079{
1086 size_t slen1; 1080 size_t slen1;
1087 size_t slen2; 1081 size_t slen2;
@@ -1089,29 +1083,29 @@ GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1089 1083
1090 if (NULL == handle) 1084 if (NULL == handle)
1091 return NULL; 1085 return NULL;
1092 GNUNET_assert (NULL != proc); 1086 GNUNET_assert(NULL != proc);
1093 GNUNET_assert (GNUNET_NO == handle->do_destroy); 1087 GNUNET_assert(GNUNET_NO == handle->do_destroy);
1094 if (NULL == subsystem) 1088 if (NULL == subsystem)
1095 subsystem = ""; 1089 subsystem = "";
1096 if (NULL == name) 1090 if (NULL == name)
1097 name = ""; 1091 name = "";
1098 slen1 = strlen (subsystem) + 1; 1092 slen1 = strlen(subsystem) + 1;
1099 slen2 = strlen (name) + 1; 1093 slen2 = strlen(name) + 1;
1100 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) < 1094 GNUNET_assert(slen1 + slen2 + sizeof(struct GNUNET_MessageHeader) <
1101 GNUNET_MAX_MESSAGE_SIZE); 1095 GNUNET_MAX_MESSAGE_SIZE);
1102 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle); 1096 ai = GNUNET_new(struct GNUNET_STATISTICS_GetHandle);
1103 ai->sh = handle; 1097 ai->sh = handle;
1104 ai->subsystem = GNUNET_strdup (subsystem); 1098 ai->subsystem = GNUNET_strdup(subsystem);
1105 ai->name = GNUNET_strdup (name); 1099 ai->name = GNUNET_strdup(name);
1106 ai->cont = cont; 1100 ai->cont = cont;
1107 ai->proc = proc; 1101 ai->proc = proc;
1108 ai->cls = cls; 1102 ai->cls = cls;
1109 ai->type = ACTION_GET; 1103 ai->type = ACTION_GET;
1110 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader); 1104 ai->msize = slen1 + slen2 + sizeof(struct GNUNET_MessageHeader);
1111 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, 1105 GNUNET_CONTAINER_DLL_insert_tail(handle->action_head,
1112 handle->action_tail, 1106 handle->action_tail,
1113 ai); 1107 ai);
1114 schedule_action (handle); 1108 schedule_action(handle);
1115 return ai; 1109 return ai;
1116} 1110}
1117 1111
@@ -1123,22 +1117,22 @@ GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1123 * @param gh handle of the request to cancel 1117 * @param gh handle of the request to cancel
1124 */ 1118 */
1125void 1119void
1126GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh) 1120GNUNET_STATISTICS_get_cancel(struct GNUNET_STATISTICS_GetHandle *gh)
1127{ 1121{
1128 if (NULL == gh) 1122 if (NULL == gh)
1129 return; 1123 return;
1130 gh->cont = NULL; 1124 gh->cont = NULL;
1131 if (gh->sh->current == gh) 1125 if (gh->sh->current == gh)
1132 { 1126 {
1133 gh->aborted = GNUNET_YES; 1127 gh->aborted = GNUNET_YES;
1134 return; 1128 return;
1135 } 1129 }
1136 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, 1130 GNUNET_CONTAINER_DLL_remove(gh->sh->action_head,
1137 gh->sh->action_tail, 1131 gh->sh->action_tail,
1138 gh); 1132 gh);
1139 GNUNET_free (gh->name); 1133 GNUNET_free(gh->name);
1140 GNUNET_free (gh->subsystem); 1134 GNUNET_free(gh->subsystem);
1141 GNUNET_free (gh); 1135 GNUNET_free(gh);
1142} 1136}
1143 1137
1144 1138
@@ -1153,26 +1147,26 @@ GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1153 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 1147 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1154 */ 1148 */
1155int 1149int
1156GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle, 1150GNUNET_STATISTICS_watch(struct GNUNET_STATISTICS_Handle *handle,
1157 const char *subsystem, 1151 const char *subsystem,
1158 const char *name, 1152 const char *name,
1159 GNUNET_STATISTICS_Iterator proc, 1153 GNUNET_STATISTICS_Iterator proc,
1160 void *proc_cls) 1154 void *proc_cls)
1161{ 1155{
1162 struct GNUNET_STATISTICS_WatchEntry *w; 1156 struct GNUNET_STATISTICS_WatchEntry *w;
1163 1157
1164 if (NULL == handle) 1158 if (NULL == handle)
1165 return GNUNET_SYSERR; 1159 return GNUNET_SYSERR;
1166 w = GNUNET_new (struct GNUNET_STATISTICS_WatchEntry); 1160 w = GNUNET_new(struct GNUNET_STATISTICS_WatchEntry);
1167 w->subsystem = GNUNET_strdup (subsystem); 1161 w->subsystem = GNUNET_strdup(subsystem);
1168 w->name = GNUNET_strdup (name); 1162 w->name = GNUNET_strdup(name);
1169 w->proc = proc; 1163 w->proc = proc;
1170 w->proc_cls = proc_cls; 1164 w->proc_cls = proc_cls;
1171 GNUNET_array_append (handle->watches, 1165 GNUNET_array_append(handle->watches,
1172 handle->watches_size, 1166 handle->watches_size,
1173 w); 1167 w);
1174 schedule_watch_request (handle, 1168 schedule_watch_request(handle,
1175 w); 1169 w);
1176 return GNUNET_OK; 1170 return GNUNET_OK;
1177} 1171}
1178 1172
@@ -1188,35 +1182,35 @@ GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1188 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch) 1182 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch)
1189 */ 1183 */
1190int 1184int
1191GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle, 1185GNUNET_STATISTICS_watch_cancel(struct GNUNET_STATISTICS_Handle *handle,
1192 const char *subsystem, 1186 const char *subsystem,
1193 const char *name, 1187 const char *name,
1194 GNUNET_STATISTICS_Iterator proc, 1188 GNUNET_STATISTICS_Iterator proc,
1195 void *proc_cls) 1189 void *proc_cls)
1196{ 1190{
1197 struct GNUNET_STATISTICS_WatchEntry *w; 1191 struct GNUNET_STATISTICS_WatchEntry *w;
1198 1192
1199 if (NULL == handle) 1193 if (NULL == handle)
1200 return GNUNET_SYSERR; 1194 return GNUNET_SYSERR;
1201 for (unsigned int i=0;i<handle->watches_size;i++) 1195 for (unsigned int i = 0; i < handle->watches_size; i++)
1202 {
1203 w = handle->watches[i];
1204 if (NULL == w)
1205 continue;
1206 if ( (w->proc == proc) &&
1207 (w->proc_cls == proc_cls) &&
1208 (0 == strcmp (w->name,
1209 name)) &&
1210 (0 == strcmp (w->subsystem,
1211 subsystem)) )
1212 { 1196 {
1213 GNUNET_free (w->name); 1197 w = handle->watches[i];
1214 GNUNET_free (w->subsystem); 1198 if (NULL == w)
1215 GNUNET_free (w); 1199 continue;
1216 handle->watches[i] = NULL; 1200 if ((w->proc == proc) &&
1217 return GNUNET_OK; 1201 (w->proc_cls == proc_cls) &&
1202 (0 == strcmp(w->name,
1203 name)) &&
1204 (0 == strcmp(w->subsystem,
1205 subsystem)))
1206 {
1207 GNUNET_free(w->name);
1208 GNUNET_free(w->subsystem);
1209 GNUNET_free(w);
1210 handle->watches[i] = NULL;
1211 return GNUNET_OK;
1212 }
1218 } 1213 }
1219 }
1220 return GNUNET_SYSERR; 1214 return GNUNET_SYSERR;
1221} 1215}
1222 1216
@@ -1231,11 +1225,11 @@ GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1231 * @param type type of the action (#ACTION_SET or #ACTION_UPDATE) 1225 * @param type type of the action (#ACTION_SET or #ACTION_UPDATE)
1232 */ 1226 */
1233static void 1227static void
1234add_setter_action (struct GNUNET_STATISTICS_Handle *h, 1228add_setter_action(struct GNUNET_STATISTICS_Handle *h,
1235 const char *name, 1229 const char *name,
1236 int make_persistent, 1230 int make_persistent,
1237 uint64_t value, 1231 uint64_t value,
1238 enum ActionType type) 1232 enum ActionType type)
1239{ 1233{
1240 struct GNUNET_STATISTICS_GetHandle *ai; 1234 struct GNUNET_STATISTICS_GetHandle *ai;
1241 size_t slen; 1235 size_t slen;
@@ -1243,83 +1237,83 @@ add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1243 size_t nsize; 1237 size_t nsize;
1244 int64_t delta; 1238 int64_t delta;
1245 1239
1246 slen = strlen (h->subsystem) + 1; 1240 slen = strlen(h->subsystem) + 1;
1247 nlen = strlen (name) + 1; 1241 nlen = strlen(name) + 1;
1248 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen; 1242 nsize = sizeof(struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1249 if (nsize >= GNUNET_MAX_MESSAGE_SIZE) 1243 if (nsize >= GNUNET_MAX_MESSAGE_SIZE)
1250 {
1251 GNUNET_break (0);
1252 return;
1253 }
1254 for (ai = h->action_head; NULL != ai; ai = ai->next)
1255 {
1256 if (! ( (0 == strcmp (ai->subsystem,
1257 h->subsystem)) &&
1258 (0 == strcmp (ai->name,
1259 name)) &&
1260 ( (ACTION_UPDATE == ai->type) ||
1261 (ACTION_SET == ai->type) ) ) )
1262 continue;
1263 if (ACTION_SET == ai->type)
1264 { 1244 {
1265 if (ACTION_UPDATE == type) 1245 GNUNET_break(0);
1266 { 1246 return;
1267 delta = (int64_t) value;
1268 if (delta > 0)
1269 {
1270 /* update old set by new delta */
1271 ai->value += delta;
1272 }
1273 else
1274 {
1275 /* update old set by new delta, but never go negative */
1276 if (ai->value < -delta)
1277 ai->value = 0;
1278 else
1279 ai->value += delta;
1280 }
1281 }
1282 else
1283 {
1284 /* new set overrides old set */
1285 ai->value = value;
1286 }
1287 } 1247 }
1288 else 1248 for (ai = h->action_head; NULL != ai; ai = ai->next)
1289 { 1249 {
1290 if (ACTION_UPDATE == type) 1250 if (!((0 == strcmp(ai->subsystem,
1291 { 1251 h->subsystem)) &&
1292 /* make delta cummulative */ 1252 (0 == strcmp(ai->name,
1293 delta = (int64_t) value; 1253 name)) &&
1294 ai->value += delta; 1254 ((ACTION_UPDATE == ai->type) ||
1295 } 1255 (ACTION_SET == ai->type))))
1256 continue;
1257 if (ACTION_SET == ai->type)
1258 {
1259 if (ACTION_UPDATE == type)
1260 {
1261 delta = (int64_t)value;
1262 if (delta > 0)
1263 {
1264 /* update old set by new delta */
1265 ai->value += delta;
1266 }
1267 else
1268 {
1269 /* update old set by new delta, but never go negative */
1270 if (ai->value < -delta)
1271 ai->value = 0;
1272 else
1273 ai->value += delta;
1274 }
1275 }
1276 else
1277 {
1278 /* new set overrides old set */
1279 ai->value = value;
1280 }
1281 }
1296 else 1282 else
1297 { 1283 {
1298 /* drop old 'update', use new 'set' instead */ 1284 if (ACTION_UPDATE == type)
1299 ai->value = value; 1285 {
1300 ai->type = type; 1286 /* make delta cummulative */
1301 } 1287 delta = (int64_t)value;
1288 ai->value += delta;
1289 }
1290 else
1291 {
1292 /* drop old 'update', use new 'set' instead */
1293 ai->value = value;
1294 ai->type = type;
1295 }
1296 }
1297 ai->timeout
1298 = GNUNET_TIME_relative_to_absolute(SET_TRANSMIT_TIMEOUT);
1299 ai->make_persistent
1300 = make_persistent;
1301 return;
1302 } 1302 }
1303 ai->timeout
1304 = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1305 ai->make_persistent
1306 = make_persistent;
1307 return;
1308 }
1309 /* no existing entry matches, create a fresh one */ 1303 /* no existing entry matches, create a fresh one */
1310 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle); 1304 ai = GNUNET_new(struct GNUNET_STATISTICS_GetHandle);
1311 ai->sh = h; 1305 ai->sh = h;
1312 ai->subsystem = GNUNET_strdup (h->subsystem); 1306 ai->subsystem = GNUNET_strdup(h->subsystem);
1313 ai->name = GNUNET_strdup (name); 1307 ai->name = GNUNET_strdup(name);
1314 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT); 1308 ai->timeout = GNUNET_TIME_relative_to_absolute(SET_TRANSMIT_TIMEOUT);
1315 ai->make_persistent = make_persistent; 1309 ai->make_persistent = make_persistent;
1316 ai->msize = nsize; 1310 ai->msize = nsize;
1317 ai->value = value; 1311 ai->value = value;
1318 ai->type = type; 1312 ai->type = type;
1319 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, 1313 GNUNET_CONTAINER_DLL_insert_tail(h->action_head,
1320 h->action_tail, 1314 h->action_tail,
1321 ai); 1315 ai);
1322 schedule_action (h); 1316 schedule_action(h);
1323} 1317}
1324 1318
1325 1319
@@ -1333,19 +1327,19 @@ add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1333 * @param make_persistent should the value be kept across restarts? 1327 * @param make_persistent should the value be kept across restarts?
1334 */ 1328 */
1335void 1329void
1336GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle, 1330GNUNET_STATISTICS_set(struct GNUNET_STATISTICS_Handle *handle,
1337 const char *name, 1331 const char *name,
1338 uint64_t value, 1332 uint64_t value,
1339 int make_persistent) 1333 int make_persistent)
1340{ 1334{
1341 if (NULL == handle) 1335 if (NULL == handle)
1342 return; 1336 return;
1343 GNUNET_assert (GNUNET_NO == handle->do_destroy); 1337 GNUNET_assert(GNUNET_NO == handle->do_destroy);
1344 add_setter_action (handle, 1338 add_setter_action(handle,
1345 name, 1339 name,
1346 make_persistent, 1340 make_persistent,
1347 value, 1341 value,
1348 ACTION_SET); 1342 ACTION_SET);
1349} 1343}
1350 1344
1351 1345
@@ -1359,21 +1353,21 @@ GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1359 * @param make_persistent should the value be kept across restarts? 1353 * @param make_persistent should the value be kept across restarts?
1360 */ 1354 */
1361void 1355void
1362GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle, 1356GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle,
1363 const char *name, 1357 const char *name,
1364 int64_t delta, 1358 int64_t delta,
1365 int make_persistent) 1359 int make_persistent)
1366{ 1360{
1367 if (NULL == handle) 1361 if (NULL == handle)
1368 return; 1362 return;
1369 if (0 == delta) 1363 if (0 == delta)
1370 return; 1364 return;
1371 GNUNET_assert (GNUNET_NO == handle->do_destroy); 1365 GNUNET_assert(GNUNET_NO == handle->do_destroy);
1372 add_setter_action (handle, 1366 add_setter_action(handle,
1373 name, 1367 name,
1374 make_persistent, 1368 make_persistent,
1375 (uint64_t) delta, 1369 (uint64_t)delta,
1376 ACTION_UPDATE); 1370 ACTION_UPDATE);
1377} 1371}
1378 1372
1379 1373
diff --git a/src/statistics/test_statistics_api.c b/src/statistics/test_statistics_api.c
index 7a0756cda..b21fa5ef1 100644
--- a/src/statistics/test_statistics_api.c
+++ b/src/statistics/test_statistics_api.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file statistics/test_statistics_api.c 21 * @file statistics/test_statistics_api.c
22 * @brief testcase for statistics_api.c 22 * @brief testcase for statistics_api.c
@@ -33,220 +33,219 @@ static struct GNUNET_STATISTICS_GetHandle *g;
33 33
34 34
35static void 35static void
36do_shutdown () 36do_shutdown()
37{ 37{
38 if (NULL != g) 38 if (NULL != g)
39 { 39 {
40 GNUNET_STATISTICS_get_cancel (g); 40 GNUNET_STATISTICS_get_cancel(g);
41 g = NULL; 41 g = NULL;
42 } 42 }
43 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 43 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
44 h = NULL; 44 h = NULL;
45} 45}
46 46
47 47
48static int 48static int
49check_1 (void *cls, 49check_1(void *cls,
50 const char *subsystem, 50 const char *subsystem,
51 const char *name, 51 const char *name,
52 uint64_t value, 52 uint64_t value,
53 int is_persistent) 53 int is_persistent)
54{ 54{
55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 55 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
56 "Received value %llu for `%s:%s\n", 56 "Received value %llu for `%s:%s\n",
57 (unsigned long long) value, 57 (unsigned long long)value,
58 subsystem, 58 subsystem,
59 name); 59 name);
60 GNUNET_assert (0 == strcmp (name, "test-1")); 60 GNUNET_assert(0 == strcmp(name, "test-1"));
61 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api")); 61 GNUNET_assert(0 == strcmp(subsystem, "test-statistics-api"));
62 GNUNET_assert (value == 1); 62 GNUNET_assert(value == 1);
63 GNUNET_assert (is_persistent == GNUNET_NO); 63 GNUNET_assert(is_persistent == GNUNET_NO);
64 return GNUNET_OK; 64 return GNUNET_OK;
65} 65}
66 66
67 67
68static int 68static int
69check_2 (void *cls, 69check_2(void *cls,
70 const char *subsystem, 70 const char *subsystem,
71 const char *name, 71 const char *name,
72 uint64_t value, 72 uint64_t value,
73 int is_persistent) 73 int is_persistent)
74{ 74{
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 75 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
76 "Received value %llu for `%s:%s\n", 76 "Received value %llu for `%s:%s\n",
77 (unsigned long long) value, 77 (unsigned long long)value,
78 subsystem, 78 subsystem,
79 name); 79 name);
80 GNUNET_assert (0 == strcmp (name, "test-2")); 80 GNUNET_assert(0 == strcmp(name, "test-2"));
81 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api")); 81 GNUNET_assert(0 == strcmp(subsystem, "test-statistics-api"));
82 GNUNET_assert (value == 2); 82 GNUNET_assert(value == 2);
83 GNUNET_assert (is_persistent == GNUNET_NO); 83 GNUNET_assert(is_persistent == GNUNET_NO);
84 return GNUNET_OK; 84 return GNUNET_OK;
85} 85}
86 86
87 87
88static int 88static int
89check_3 (void *cls, 89check_3(void *cls,
90 const char *subsystem, 90 const char *subsystem,
91 const char *name, 91 const char *name,
92 uint64_t value, 92 uint64_t value,
93 int is_persistent) 93 int is_persistent)
94{ 94{
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 95 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
96 "Received value %llu for `%s:%s\n", 96 "Received value %llu for `%s:%s\n",
97 (unsigned long long) value, 97 (unsigned long long)value,
98 subsystem, 98 subsystem,
99 name); 99 name);
100 GNUNET_assert (0 == strcmp (name, "test-3")); 100 GNUNET_assert(0 == strcmp(name, "test-3"));
101 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api")); 101 GNUNET_assert(0 == strcmp(subsystem, "test-statistics-api"));
102 GNUNET_assert (value == 3); 102 GNUNET_assert(value == 3);
103 GNUNET_assert (is_persistent == GNUNET_YES); 103 GNUNET_assert(is_persistent == GNUNET_YES);
104 return GNUNET_OK; 104 return GNUNET_OK;
105} 105}
106 106
107 107
108static void 108static void
109next_fin (void *cls, 109next_fin(void *cls,
110 int success) 110 int success)
111{ 111{
112 int *ok = cls; 112 int *ok = cls;
113 113
114 g = NULL; 114 g = NULL;
115 GNUNET_SCHEDULER_shutdown (); 115 GNUNET_SCHEDULER_shutdown();
116 GNUNET_assert (success == GNUNET_OK); 116 GNUNET_assert(success == GNUNET_OK);
117 *ok = 0; 117 *ok = 0;
118} 118}
119 119
120 120
121static void 121static void
122next (void *cls, int success) 122next(void *cls, int success)
123{ 123{
124 g = NULL; 124 g = NULL;
125 GNUNET_assert (success == GNUNET_OK); 125 GNUNET_assert(success == GNUNET_OK);
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 126 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
127 "Issuing GET request\n"); 127 "Issuing GET request\n");
128 GNUNET_break (NULL != 128 GNUNET_break(NULL !=
129 GNUNET_STATISTICS_get (h, NULL, "test-2", 129 GNUNET_STATISTICS_get(h, NULL, "test-2",
130 &next_fin, 130 &next_fin,
131 &check_2, cls)); 131 &check_2, cls));
132} 132}
133 133
134 134
135static void 135static void
136run (void *cls, 136run(void *cls,
137 char *const *args, 137 char *const *args,
138 const char *cfgfile, 138 const char *cfgfile,
139 const struct GNUNET_CONFIGURATION_Handle *cfg) 139 const struct GNUNET_CONFIGURATION_Handle *cfg)
140{ 140{
141 h = GNUNET_STATISTICS_create ("test-statistics-api", cfg); 141 h = GNUNET_STATISTICS_create("test-statistics-api", cfg);
142 if (NULL == h) 142 if (NULL == h)
143 { 143 {
144 GNUNET_break (0); 144 GNUNET_break(0);
145 return; 145 return;
146 } 146 }
147 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 147 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
148 NULL); 148 NULL);
149 GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO); 149 GNUNET_STATISTICS_set(h, "test-1", 1, GNUNET_NO);
150 GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO); 150 GNUNET_STATISTICS_set(h, "test-2", 2, GNUNET_NO);
151 GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO); 151 GNUNET_STATISTICS_set(h, "test-3", 2, GNUNET_NO);
152 GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES); 152 GNUNET_STATISTICS_update(h, "test-3", 1, GNUNET_YES);
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 153 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
154 "Issuing GET request\n"); 154 "Issuing GET request\n");
155 GNUNET_break (NULL != 155 GNUNET_break(NULL !=
156 (g = GNUNET_STATISTICS_get (h, NULL, "test-1", 156 (g = GNUNET_STATISTICS_get(h, NULL, "test-1",
157 &next, 157 &next,
158 &check_1, cls))); 158 &check_1, cls)));
159} 159}
160 160
161 161
162static void 162static void
163run_more (void *cls, 163run_more(void *cls,
164 char *const *args, 164 char *const *args,
165 const char *cfgfile, 165 const char *cfgfile,
166 const struct GNUNET_CONFIGURATION_Handle *cfg) 166 const struct GNUNET_CONFIGURATION_Handle *cfg)
167{ 167{
168 h = GNUNET_STATISTICS_create ("test-statistics-api", 168 h = GNUNET_STATISTICS_create("test-statistics-api",
169 cfg); 169 cfg);
170 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 170 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
171 NULL); 171 NULL);
172 GNUNET_break (NULL != 172 GNUNET_break(NULL !=
173 (g = GNUNET_STATISTICS_get (h, NULL, 173 (g = GNUNET_STATISTICS_get(h, NULL,
174 "test-3", 174 "test-3",
175 &next_fin, 175 &next_fin,
176 &check_3, cls))); 176 &check_3, cls)));
177} 177}
178 178
179 179
180int 180int
181main (int argc, char *argv_ign[]) 181main(int argc, char *argv_ign[])
182{ 182{
183 int ok = 1; 183 int ok = 1;
184 char *const argv[] = { "test-statistics-api", 184 char *const argv[] = { "test-statistics-api",
185 "-c", 185 "-c",
186 "test_statistics_api_data.conf", 186 "test_statistics_api_data.conf",
187 "-L", "WARNING", 187 "-L", "WARNING",
188 NULL 188 NULL };
189 };
190 struct GNUNET_GETOPT_CommandLineOption options[] = { 189 struct GNUNET_GETOPT_CommandLineOption options[] = {
191 GNUNET_GETOPT_OPTION_END 190 GNUNET_GETOPT_OPTION_END
192 }; 191 };
193 struct GNUNET_OS_Process *proc; 192 struct GNUNET_OS_Process *proc;
194 char *binary; 193 char *binary;
195 194
196 GNUNET_log_setup ("test_statistics_api", 195 GNUNET_log_setup("test_statistics_api",
197 "WARNING", 196 "WARNING",
198 NULL); 197 NULL);
199 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics"); 198 binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-statistics");
200 proc = 199 proc =
201 GNUNET_OS_start_process (GNUNET_YES, 200 GNUNET_OS_start_process(GNUNET_YES,
202 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 201 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
203 NULL, NULL, NULL, 202 NULL, NULL, NULL,
204 binary, 203 binary,
205 "gnunet-service-statistics", 204 "gnunet-service-statistics",
206 "-c", "test_statistics_api_data.conf", NULL); 205 "-c", "test_statistics_api_data.conf", NULL);
207 GNUNET_assert (NULL != proc); 206 GNUNET_assert(NULL != proc);
208 GNUNET_PROGRAM_run (5, argv, 207 GNUNET_PROGRAM_run(5, argv,
209 "test-statistics-api", "nohelp", 208 "test-statistics-api", "nohelp",
210 options, &run, 209 options, &run,
211 &ok); 210 &ok);
212 if (0 != GNUNET_OS_process_kill (proc, 211 if (0 != GNUNET_OS_process_kill(proc,
213 GNUNET_TERM_SIG)) 212 GNUNET_TERM_SIG))
214 { 213 {
215 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 214 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
216 ok = 1; 215 ok = 1;
217 } 216 }
218 GNUNET_OS_process_wait (proc); 217 GNUNET_OS_process_wait(proc);
219 GNUNET_OS_process_destroy (proc); 218 GNUNET_OS_process_destroy(proc);
220 proc = NULL; 219 proc = NULL;
221 if (ok != 0) 220 if (ok != 0)
222 { 221 {
223 GNUNET_free (binary); 222 GNUNET_free(binary);
224 return ok; 223 return ok;
225 } 224 }
226 ok = 1; 225 ok = 1;
227 /* restart to check persistence! */ 226 /* restart to check persistence! */
228 proc = 227 proc =
229 GNUNET_OS_start_process (GNUNET_YES, 228 GNUNET_OS_start_process(GNUNET_YES,
230 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 229 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
231 NULL, NULL, NULL, 230 NULL, NULL, NULL,
232 binary, 231 binary,
233 "gnunet-service-statistics", 232 "gnunet-service-statistics",
234 "-c", "test_statistics_api_data.conf", 233 "-c", "test_statistics_api_data.conf",
235 NULL); 234 NULL);
236 GNUNET_PROGRAM_run (5, argv, 235 GNUNET_PROGRAM_run(5, argv,
237 "test-statistics-api", "nohelp", 236 "test-statistics-api", "nohelp",
238 options, 237 options,
239 &run_more, &ok); 238 &run_more, &ok);
240 if (0 != GNUNET_OS_process_kill (proc, 239 if (0 != GNUNET_OS_process_kill(proc,
241 GNUNET_TERM_SIG)) 240 GNUNET_TERM_SIG))
242 { 241 {
243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 242 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
244 ok = 1; 243 ok = 1;
245 } 244 }
246 GNUNET_OS_process_wait (proc); 245 GNUNET_OS_process_wait(proc);
247 GNUNET_OS_process_destroy (proc); 246 GNUNET_OS_process_destroy(proc);
248 proc = NULL; 247 proc = NULL;
249 GNUNET_free (binary); 248 GNUNET_free(binary);
250 return ok; 249 return ok;
251} 250}
252 251
diff --git a/src/statistics/test_statistics_api_loop.c b/src/statistics/test_statistics_api_loop.c
index 2438fd95a..61cf1130a 100644
--- a/src/statistics/test_statistics_api_loop.c
+++ b/src/statistics/test_statistics_api_loop.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file statistics/test_statistics_api_loop.c 21 * @file statistics/test_statistics_api_loop.c
22 * @brief testcase for statistics_api.c 22 * @brief testcase for statistics_api.c
@@ -31,91 +31,90 @@ static struct GNUNET_STATISTICS_Handle *h;
31 31
32 32
33static int 33static int
34check_1 (void *cls, 34check_1(void *cls,
35 const char *subsystem, 35 const char *subsystem,
36 const char *name, 36 const char *name,
37 uint64_t value, 37 uint64_t value,
38 int is_persistent) 38 int is_persistent)
39{ 39{
40 GNUNET_assert (0 == strcmp (name, "test-0")); 40 GNUNET_assert(0 == strcmp(name, "test-0"));
41 GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api-loop")); 41 GNUNET_assert(0 == strcmp(subsystem, "test-statistics-api-loop"));
42 GNUNET_assert (is_persistent == GNUNET_NO); 42 GNUNET_assert(is_persistent == GNUNET_NO);
43 return GNUNET_OK; 43 return GNUNET_OK;
44} 44}
45 45
46 46
47static void 47static void
48next (void *cls, 48next(void *cls,
49 int success) 49 int success)
50{ 50{
51 int *ok = cls; 51 int *ok = cls;
52 52
53 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 53 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
54 GNUNET_assert (success == GNUNET_OK); 54 GNUNET_assert(success == GNUNET_OK);
55 *ok = 0; 55 *ok = 0;
56} 56}
57 57
58 58
59static void 59static void
60run (void *cls, 60run(void *cls,
61 char *const *args, 61 char *const *args,
62 const char *cfgfile, 62 const char *cfgfile,
63 const struct GNUNET_CONFIGURATION_Handle *cfg) 63 const struct GNUNET_CONFIGURATION_Handle *cfg)
64{ 64{
65 unsigned int i; 65 unsigned int i;
66 char name[128]; 66 char name[128];
67 67
68 h = GNUNET_STATISTICS_create ("test-statistics-api-loop", cfg); 68 h = GNUNET_STATISTICS_create("test-statistics-api-loop", cfg);
69 for (i = 0; i < ROUNDS; i++) 69 for (i = 0; i < ROUNDS; i++)
70 { 70 {
71 GNUNET_snprintf (name, sizeof (name), "test-%d", i % 32); 71 GNUNET_snprintf(name, sizeof(name), "test-%d", i % 32);
72 GNUNET_STATISTICS_set (h, name, i, GNUNET_NO); 72 GNUNET_STATISTICS_set(h, name, i, GNUNET_NO);
73 GNUNET_snprintf (name, sizeof (name), "test-%d", i % 16); 73 GNUNET_snprintf(name, sizeof(name), "test-%d", i % 16);
74 GNUNET_STATISTICS_update (h, name, 1, GNUNET_NO); 74 GNUNET_STATISTICS_update(h, name, 1, GNUNET_NO);
75 } 75 }
76 i = 0; 76 i = 0;
77 GNUNET_break (NULL != 77 GNUNET_break(NULL !=
78 GNUNET_STATISTICS_get (h, NULL, "test-0", 78 GNUNET_STATISTICS_get(h, NULL, "test-0",
79 &next, 79 &next,
80 &check_1, cls)); 80 &check_1, cls));
81} 81}
82 82
83 83
84int 84int
85main (int argc, char *argv_ign[]) 85main(int argc, char *argv_ign[])
86{ 86{
87 int ok = 1; 87 int ok = 1;
88 88
89 char *const argv[] = { "test-statistics-api", 89 char *const argv[] = { "test-statistics-api",
90 "-c", 90 "-c",
91 "test_statistics_api_data.conf", 91 "test_statistics_api_data.conf",
92 NULL 92 NULL };
93 };
94 struct GNUNET_GETOPT_CommandLineOption options[] = { 93 struct GNUNET_GETOPT_CommandLineOption options[] = {
95 GNUNET_GETOPT_OPTION_END 94 GNUNET_GETOPT_OPTION_END
96 }; 95 };
97 struct GNUNET_OS_Process *proc; 96 struct GNUNET_OS_Process *proc;
98 char *binary; 97 char *binary;
99 98
100 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics"); 99 binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-statistics");
101 proc = 100 proc =
102 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 101 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
103 NULL, NULL, NULL, 102 NULL, NULL, NULL,
104 binary, 103 binary,
105 "gnunet-service-statistics", 104 "gnunet-service-statistics",
106 "-c", "test_statistics_api_data.conf", NULL); 105 "-c", "test_statistics_api_data.conf", NULL);
107 GNUNET_assert (NULL != proc); 106 GNUNET_assert(NULL != proc);
108 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run, 107 GNUNET_PROGRAM_run(3, argv, "test-statistics-api", "nohelp", options, &run,
109 &ok); 108 &ok);
110 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) 109 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG))
111 { 110 {
112 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 111 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
113 ok = 1; 112 ok = 1;
114 } 113 }
115 GNUNET_OS_process_wait (proc); 114 GNUNET_OS_process_wait(proc);
116 GNUNET_OS_process_destroy (proc); 115 GNUNET_OS_process_destroy(proc);
117 proc = NULL; 116 proc = NULL;
118 GNUNET_free (binary); 117 GNUNET_free(binary);
119 return ok; 118 return ok;
120} 119}
121 120
diff --git a/src/statistics/test_statistics_api_watch.c b/src/statistics/test_statistics_api_watch.c
index 38ba0e84e..e8cd15047 100644
--- a/src/statistics/test_statistics_api_watch.c
+++ b/src/statistics/test_statistics_api_watch.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file statistics/test_statistics_api_watch.c 21 * @file statistics/test_statistics_api_watch.c
22 * @brief testcase for statistics_api.c watch functions 22 * @brief testcase for statistics_api.c watch functions
@@ -37,118 +37,117 @@ static struct GNUNET_SCHEDULER_Task *shutdown_task;
37 37
38 38
39static void 39static void
40force_shutdown (void *cls) 40force_shutdown(void *cls)
41{ 41{
42 fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok); 42 fprintf(stderr, "Timeout, failed to receive notifications: %d\n", ok);
43 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 43 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
44 GNUNET_STATISTICS_destroy (h2, GNUNET_NO); 44 GNUNET_STATISTICS_destroy(h2, GNUNET_NO);
45 ok = 7; 45 ok = 7;
46} 46}
47 47
48 48
49static void 49static void
50normal_shutdown (void *cls) 50normal_shutdown(void *cls)
51{ 51{
52 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 52 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
53 GNUNET_STATISTICS_destroy (h2, GNUNET_NO); 53 GNUNET_STATISTICS_destroy(h2, GNUNET_NO);
54} 54}
55 55
56 56
57static int 57static int
58watch_1 (void *cls, 58watch_1(void *cls,
59 const char *subsystem, 59 const char *subsystem,
60 const char *name, 60 const char *name,
61 uint64_t value, 61 uint64_t value,
62 int is_persistent) 62 int is_persistent)
63{ 63{
64 GNUNET_assert (value == 42); 64 GNUNET_assert(value == 42);
65 GNUNET_assert (0 == strcmp (name, "test-1")); 65 GNUNET_assert(0 == strcmp(name, "test-1"));
66 ok &= ~1; 66 ok &= ~1;
67 if (0 == ok) 67 if (0 == ok)
68 { 68 {
69 GNUNET_SCHEDULER_cancel (shutdown_task); 69 GNUNET_SCHEDULER_cancel(shutdown_task);
70 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 70 GNUNET_SCHEDULER_add_now(&normal_shutdown, NULL);
71 } 71 }
72 return GNUNET_OK; 72 return GNUNET_OK;
73} 73}
74 74
75 75
76static int 76static int
77watch_2 (void *cls, 77watch_2(void *cls,
78 const char *subsystem, 78 const char *subsystem,
79 const char *name, 79 const char *name,
80 uint64_t value, 80 uint64_t value,
81 int is_persistent) 81 int is_persistent)
82{ 82{
83 GNUNET_assert (value == 43); 83 GNUNET_assert(value == 43);
84 GNUNET_assert (0 == strcmp (name, "test-2")); 84 GNUNET_assert(0 == strcmp(name, "test-2"));
85 ok &= ~2; 85 ok &= ~2;
86 if (0 == ok) 86 if (0 == ok)
87 { 87 {
88 GNUNET_SCHEDULER_cancel (shutdown_task); 88 GNUNET_SCHEDULER_cancel(shutdown_task);
89 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 89 GNUNET_SCHEDULER_add_now(&normal_shutdown, NULL);
90 } 90 }
91 return GNUNET_OK; 91 return GNUNET_OK;
92} 92}
93 93
94 94
95static void 95static void
96run (void *cls, 96run(void *cls,
97 char *const *args, 97 char *const *args,
98 const char *cfgfile, 98 const char *cfgfile,
99 const struct GNUNET_CONFIGURATION_Handle *cfg) 99 const struct GNUNET_CONFIGURATION_Handle *cfg)
100{ 100{
101 h = GNUNET_STATISTICS_create ("dummy", cfg); 101 h = GNUNET_STATISTICS_create("dummy", cfg);
102 GNUNET_assert (GNUNET_OK == 102 GNUNET_assert(GNUNET_OK ==
103 GNUNET_STATISTICS_watch (h, "test-statistics-api-watch", 103 GNUNET_STATISTICS_watch(h, "test-statistics-api-watch",
104 "test-1", &watch_1, NULL)); 104 "test-1", &watch_1, NULL));
105 GNUNET_assert (GNUNET_OK == 105 GNUNET_assert(GNUNET_OK ==
106 GNUNET_STATISTICS_watch (h, "test-statistics-api-watch", 106 GNUNET_STATISTICS_watch(h, "test-statistics-api-watch",
107 "test-2", &watch_2, NULL)); 107 "test-2", &watch_2, NULL));
108 h2 = GNUNET_STATISTICS_create ("test-statistics-api-watch", cfg); 108 h2 = GNUNET_STATISTICS_create("test-statistics-api-watch", cfg);
109 GNUNET_STATISTICS_set (h2, "test-1", 42, GNUNET_NO); 109 GNUNET_STATISTICS_set(h2, "test-1", 42, GNUNET_NO);
110 GNUNET_STATISTICS_set (h2, "test-2", 43, GNUNET_NO); 110 GNUNET_STATISTICS_set(h2, "test-2", 43, GNUNET_NO);
111 shutdown_task = 111 shutdown_task =
112 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 112 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES,
113 &force_shutdown, 113 &force_shutdown,
114 NULL); 114 NULL);
115} 115}
116 116
117 117
118int 118int
119main (int argc, char *argv_ign[]) 119main(int argc, char *argv_ign[])
120{ 120{
121 char *const argv[] = { "test-statistics-api", 121 char *const argv[] = { "test-statistics-api",
122 "-c", 122 "-c",
123 "test_statistics_api_data.conf", 123 "test_statistics_api_data.conf",
124 NULL 124 NULL };
125 };
126 struct GNUNET_GETOPT_CommandLineOption options[] = { 125 struct GNUNET_GETOPT_CommandLineOption options[] = {
127 GNUNET_GETOPT_OPTION_END 126 GNUNET_GETOPT_OPTION_END
128 }; 127 };
129 struct GNUNET_OS_Process *proc; 128 struct GNUNET_OS_Process *proc;
130 char *binary; 129 char *binary;
131 130
132 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics"); 131 binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-statistics");
133 proc = 132 proc =
134 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 133 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
135 NULL, NULL, NULL, 134 NULL, NULL, NULL,
136 binary, 135 binary,
137 "gnunet-service-statistics", 136 "gnunet-service-statistics",
138 "-c", "test_statistics_api_data.conf", NULL); 137 "-c", "test_statistics_api_data.conf", NULL);
139 GNUNET_assert (NULL != proc); 138 GNUNET_assert(NULL != proc);
140 ok = 3; 139 ok = 3;
141 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run, 140 GNUNET_PROGRAM_run(3, argv, "test-statistics-api", "nohelp", options, &run,
142 NULL); 141 NULL);
143 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) 142 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG))
144 { 143 {
145 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 144 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
146 ok = 1; 145 ok = 1;
147 } 146 }
148 GNUNET_OS_process_wait (proc); 147 GNUNET_OS_process_wait(proc);
149 GNUNET_OS_process_destroy (proc); 148 GNUNET_OS_process_destroy(proc);
150 proc = NULL; 149 proc = NULL;
151 GNUNET_free (binary); 150 GNUNET_free(binary);
152 return ok; 151 return ok;
153} 152}
154 153
diff --git a/src/statistics/test_statistics_api_watch_zero_value.c b/src/statistics/test_statistics_api_watch_zero_value.c
index 05dc75366..1dc093658 100644
--- a/src/statistics/test_statistics_api_watch_zero_value.c
+++ b/src/statistics/test_statistics_api_watch_zero_value.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file statistics/test_statistics_api_watch_zero_value.c 21 * @file statistics/test_statistics_api_watch_zero_value.c
22 * @brief testcase for statistics_api.c watch functions with initial 0 value 22 * @brief testcase for statistics_api.c watch functions with initial 0 value
@@ -37,157 +37,156 @@ static struct GNUNET_SCHEDULER_Task *shutdown_task;
37 37
38 38
39static void 39static void
40force_shutdown (void *cls) 40force_shutdown(void *cls)
41{ 41{
42 fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok); 42 fprintf(stderr, "Timeout, failed to receive notifications: %d\n", ok);
43 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 43 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
44 GNUNET_STATISTICS_destroy (h2, GNUNET_NO); 44 GNUNET_STATISTICS_destroy(h2, GNUNET_NO);
45 ok = 7; 45 ok = 7;
46} 46}
47 47
48 48
49static void 49static void
50normal_shutdown (void *cls) 50normal_shutdown(void *cls)
51{ 51{
52 GNUNET_STATISTICS_destroy (h, GNUNET_NO); 52 GNUNET_STATISTICS_destroy(h, GNUNET_NO);
53 GNUNET_STATISTICS_destroy (h2, GNUNET_NO); 53 GNUNET_STATISTICS_destroy(h2, GNUNET_NO);
54} 54}
55 55
56 56
57static int 57static int
58watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value, 58watch_1(void *cls, const char *subsystem, const char *name, uint64_t value,
59 int is_persistent) 59 int is_persistent)
60{ 60{
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 61 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
62 "Received value `%s' `%s' %llu\n", 62 "Received value `%s' `%s' %llu\n",
63 subsystem, 63 subsystem,
64 name, 64 name,
65 (unsigned long long) value); 65 (unsigned long long)value);
66 GNUNET_assert (0 == strcmp (name, "test-1")); 66 GNUNET_assert(0 == strcmp(name, "test-1"));
67 if ((0 == value) && (3 == ok)) 67 if ((0 == value) && (3 == ok))
68 { 68 {
69 ok--; 69 ok--;
70 GNUNET_STATISTICS_set (h, "test-1", 42, GNUNET_NO); 70 GNUNET_STATISTICS_set(h, "test-1", 42, GNUNET_NO);
71 } 71 }
72 72
73 if ((42 == value) && (2 == ok)) 73 if ((42 == value) && (2 == ok))
74 { 74 {
75 ok--; 75 ok--;
76 GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO); 76 GNUNET_STATISTICS_set(h, "test-1", 0, GNUNET_NO);
77 } 77 }
78 78
79 if ((0 == value) && (1 == ok)) 79 if ((0 == value) && (1 == ok))
80 { 80 {
81 ok--; 81 ok--;
82 } 82 }
83 if ((0 == ok) && (0 == ok2)) 83 if ((0 == ok) && (0 == ok2))
84 { 84 {
85 GNUNET_SCHEDULER_cancel (shutdown_task); 85 GNUNET_SCHEDULER_cancel(shutdown_task);
86 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 86 GNUNET_SCHEDULER_add_now(&normal_shutdown, NULL);
87 } 87 }
88 88
89 return GNUNET_OK; 89 return GNUNET_OK;
90} 90}
91 91
92 92
93static int 93static int
94watch_2 (void *cls, 94watch_2(void *cls,
95 const char *subsystem, 95 const char *subsystem,
96 const char *name, 96 const char *name,
97 uint64_t value, 97 uint64_t value,
98 int is_persistent) 98 int is_persistent)
99{ 99{
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 100 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
101 "Received value `%s' `%s' %llu\n", 101 "Received value `%s' `%s' %llu\n",
102 subsystem, 102 subsystem,
103 name, 103 name,
104 (unsigned long long) value); 104 (unsigned long long)value);
105 105
106 GNUNET_assert (0 == strcmp (name, "test-2")); 106 GNUNET_assert(0 == strcmp(name, "test-2"));
107 if ((42 == value) && (1 == ok2)) 107 if ((42 == value) && (1 == ok2))
108 {
109 ok2 = 0;
110 if (0 == ok)
111 { 108 {
112 GNUNET_SCHEDULER_cancel (shutdown_task); 109 ok2 = 0;
113 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 110 if (0 == ok)
111 {
112 GNUNET_SCHEDULER_cancel(shutdown_task);
113 GNUNET_SCHEDULER_add_now(&normal_shutdown, NULL);
114 }
114 } 115 }
115 }
116 else 116 else
117 { 117 {
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 118 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
119 "Received unexpected value %llu\n", 119 "Received unexpected value %llu\n",
120 (unsigned long long) value); 120 (unsigned long long)value);
121 121
122 GNUNET_break (0); 122 GNUNET_break(0);
123 GNUNET_SCHEDULER_cancel (shutdown_task); 123 GNUNET_SCHEDULER_cancel(shutdown_task);
124 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL); 124 GNUNET_SCHEDULER_add_now(&normal_shutdown, NULL);
125 } 125 }
126 126
127 return GNUNET_OK; 127 return GNUNET_OK;
128} 128}
129 129
130 130
131static void 131static void
132run (void *cls, char *const *args, const char *cfgfile, 132run(void *cls, char *const *args, const char *cfgfile,
133 const struct GNUNET_CONFIGURATION_Handle *cfg) 133 const struct GNUNET_CONFIGURATION_Handle *cfg)
134{ 134{
135 h = GNUNET_STATISTICS_create ("dummy", cfg); 135 h = GNUNET_STATISTICS_create("dummy", cfg);
136 h2 = GNUNET_STATISTICS_create ("dummy-2", cfg); 136 h2 = GNUNET_STATISTICS_create("dummy-2", cfg);
137 GNUNET_assert (GNUNET_OK == 137 GNUNET_assert(GNUNET_OK ==
138 GNUNET_STATISTICS_watch (h, "dummy", 138 GNUNET_STATISTICS_watch(h, "dummy",
139 "test-1", &watch_1, NULL)); 139 "test-1", &watch_1, NULL));
140 140
141 GNUNET_assert (GNUNET_OK == 141 GNUNET_assert(GNUNET_OK ==
142 GNUNET_STATISTICS_watch (h2, "dummy-2", 142 GNUNET_STATISTICS_watch(h2, "dummy-2",
143 "test-2", &watch_2, NULL)); 143 "test-2", &watch_2, NULL));
144 144
145 /* Set initial value to 0 */ 145 /* Set initial value to 0 */
146 GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO); 146 GNUNET_STATISTICS_set(h, "test-1", 0, GNUNET_NO);
147 GNUNET_STATISTICS_set (h2, "test-2", 42, GNUNET_NO); 147 GNUNET_STATISTICS_set(h2, "test-2", 42, GNUNET_NO);
148 148
149 shutdown_task = 149 shutdown_task =
150 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 150 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MINUTES,
151 &force_shutdown, 151 &force_shutdown,
152 NULL); 152 NULL);
153} 153}
154 154
155 155
156int 156int
157main (int argc, char *argv_ign[]) 157main(int argc, char *argv_ign[])
158{ 158{
159 char *const argv[] = { "test-statistics-api", 159 char *const argv[] = { "test-statistics-api",
160 "-c", 160 "-c",
161 "test_statistics_api_data.conf", 161 "test_statistics_api_data.conf",
162 NULL 162 NULL };
163 };
164 struct GNUNET_GETOPT_CommandLineOption options[] = { 163 struct GNUNET_GETOPT_CommandLineOption options[] = {
165 GNUNET_GETOPT_OPTION_END 164 GNUNET_GETOPT_OPTION_END
166 }; 165 };
167 struct GNUNET_OS_Process *proc; 166 struct GNUNET_OS_Process *proc;
168 char *binary; 167 char *binary;
169 168
170 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics"); 169 binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-statistics");
171 proc = 170 proc =
172 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 171 GNUNET_OS_start_process(GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
173 NULL, NULL, NULL, 172 NULL, NULL, NULL,
174 binary, 173 binary,
175 "gnunet-service-statistics", 174 "gnunet-service-statistics",
176 "-c", "test_statistics_api_data.conf", NULL); 175 "-c", "test_statistics_api_data.conf", NULL);
177 GNUNET_assert (NULL != proc); 176 GNUNET_assert(NULL != proc);
178 ok = 3; 177 ok = 3;
179 ok2 = 1; 178 ok2 = 1;
180 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run, 179 GNUNET_PROGRAM_run(3, argv, "test-statistics-api", "nohelp", options, &run,
181 NULL); 180 NULL);
182 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) 181 if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG))
183 { 182 {
184 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 183 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
185 ok = 1; 184 ok = 1;
186 } 185 }
187 GNUNET_OS_process_wait (proc); 186 GNUNET_OS_process_wait(proc);
188 GNUNET_OS_process_destroy (proc); 187 GNUNET_OS_process_destroy(proc);
189 proc = NULL; 188 proc = NULL;
190 GNUNET_free (binary); 189 GNUNET_free(binary);
191 if ((0 == ok) && (0 == ok2)) 190 if ((0 == ok) && (0 == ok2))
192 return 0; 191 return 0;
193 return 1; 192 return 1;