aboutsummaryrefslogtreecommitdiff
path: root/src/statistics
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2012-04-18 09:55:09 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2012-04-18 09:55:09 +0000
commit9458bb0f06206d74f4e8e7a4cdadd0d2b579a340 (patch)
treed98aacdd61d673d8e302c152745a6e0330716d06 /src/statistics
parenta5146a4cbe70177d9110553072d605ba151867be (diff)
downloadgnunet-9458bb0f06206d74f4e8e7a4cdadd0d2b579a340.tar.gz
gnunet-9458bb0f06206d74f4e8e7a4cdadd0d2b579a340.zip
fixing bug 2272: added functionality for watch to notifz about fresh created entries with value 0
Diffstat (limited to 'src/statistics')
-rw-r--r--src/statistics/Makefile.am9
-rw-r--r--src/statistics/gnunet-service-statistics.c44
-rw-r--r--src/statistics/test_statistics_api_watch_zero_value.c196
3 files changed, 245 insertions, 4 deletions
diff --git a/src/statistics/Makefile.am b/src/statistics/Makefile.am
index 2d1daf8d7..09f1905f4 100644
--- a/src/statistics/Makefile.am
+++ b/src/statistics/Makefile.am
@@ -51,7 +51,8 @@ gnunet_service_statistics_DEPENDENCIES = \
51check_PROGRAMS = \ 51check_PROGRAMS = \
52 test_statistics_api \ 52 test_statistics_api \
53 test_statistics_api_loop \ 53 test_statistics_api_loop \
54 test_statistics_api_watch 54 test_statistics_api_watch \
55 test_statistics_api_watch_zero_value
55 56
56if ENABLE_TEST_RUN 57if ENABLE_TEST_RUN
57TESTS = $(check_PROGRAMS) $(check_SCRIPTS) 58TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
@@ -75,6 +76,12 @@ test_statistics_api_watch_LDADD = \
75 $(top_builddir)/src/statistics/libgnunetstatistics.la \ 76 $(top_builddir)/src/statistics/libgnunetstatistics.la \
76 $(top_builddir)/src/util/libgnunetutil.la 77 $(top_builddir)/src/util/libgnunetutil.la
77 78
79test_statistics_api_watch_zero_value_SOURCES = \
80 test_statistics_api_watch_zero_value.c
81test_statistics_api_watch_zero_value_LDADD = \
82 $(top_builddir)/src/statistics/libgnunetstatistics.la \
83 $(top_builddir)/src/util/libgnunetutil.la
84
78check_SCRIPTS = \ 85check_SCRIPTS = \
79 test_gnunet_statistics.sh 86 test_gnunet_statistics.sh
80 87
diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c
index a1a2c3abd..5b96edfa0 100644
--- a/src/statistics/gnunet-service-statistics.c
+++ b/src/statistics/gnunet-service-statistics.c
@@ -66,6 +66,12 @@ struct WatchEntry
66 */ 66 */
67 uint32_t wid; 67 uint32_t wid;
68 68
69 /**
70 * Is last_value valid
71 * GNUNET_NO : last_value is n/a, GNUNET_YES: last_value is valid
72 */
73 int last_value_set;
74
69}; 75};
70 76
71 77
@@ -152,6 +158,12 @@ struct StatsEntry
152 */ 158 */
153 int persistent; 159 int persistent;
154 160
161 /**
162 * Is this value set?
163 * GNUNET_NO : value is n/a, GNUNET_YES: value is valid
164 */
165 int set;
166
155}; 167};
156 168
157/** 169/**
@@ -462,8 +474,15 @@ notify_change (struct StatsEntry *se)
462 474
463 for (pos = se->we_head; NULL != pos; pos = pos->next) 475 for (pos = se->we_head; NULL != pos; pos = pos->next)
464 { 476 {
465 if (pos->last_value == se->value) 477 if (GNUNET_YES == pos->last_value_set)
466 continue; 478 {
479 if (pos->last_value == se->value)
480 continue;
481 }
482 else
483 {
484 pos->last_value_set = GNUNET_YES;
485 }
467 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE); 486 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
468 wvm.header.size = 487 wvm.header.size =
469 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage)); 488 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
@@ -499,6 +518,7 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
499 uint64_t value; 518 uint64_t value;
500 int64_t delta; 519 int64_t delta;
501 int changed; 520 int changed;
521 int initial_set;
502 522
503 if ( (NULL != client) && 523 if ( (NULL != client) &&
504 (NULL == make_client_entry (client)) ) 524 (NULL == make_client_entry (client)) )
@@ -532,6 +552,7 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
532 { 552 {
533 if (matches (pos, service, name)) 553 if (matches (pos, service, name))
534 { 554 {
555 initial_set = 0;
535 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) 556 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
536 { 557 {
537 changed = (pos->value != value); 558 changed = (pos->value != value);
@@ -552,6 +573,11 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
552 pos->value += delta; 573 pos->value += delta;
553 } 574 }
554 } 575 }
576 if (GNUNET_NO == pos->set)
577 {
578 pos->set = GNUNET_YES;
579 initial_set = 1;
580 }
555 pos->msg->value = GNUNET_htonll (pos->value); 581 pos->msg->value = GNUNET_htonll (pos->value);
556 pos->msg->flags = msg->flags; 582 pos->msg->flags = msg->flags;
557 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT)); 583 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
@@ -565,7 +591,7 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Statistic `%s:%s' updated to value %llu.\n", service, name, 592 "Statistic `%s:%s' updated to value %llu.\n", service, name,
567 pos->value); 593 pos->value);
568 if (changed) 594 if ((changed) || (1 == initial_set))
569 notify_change (pos); 595 notify_change (pos);
570 GNUNET_SERVER_receive_done (client, GNUNET_OK); 596 GNUNET_SERVER_receive_done (client, GNUNET_OK);
571 return; 597 return;
@@ -577,7 +603,14 @@ handle_set (void *cls, struct GNUNET_SERVER_Client *client,
577 pos->next = start; 603 pos->next = start;
578 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) || 604 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
579 (0 < (int64_t) GNUNET_ntohll (msg->value))) 605 (0 < (int64_t) GNUNET_ntohll (msg->value)))
606 {
580 pos->value = GNUNET_ntohll (msg->value); 607 pos->value = GNUNET_ntohll (msg->value);
608 pos->set = GNUNET_YES;
609 }
610 else
611 {
612 pos->set = GNUNET_NO;
613 }
581 pos->uid = uidgen++; 614 pos->uid = uidgen++;
582 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT)); 615 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
583 pos->msg = (void *) &pos[1]; 616 pos->msg = (void *) &pos[1];
@@ -652,6 +685,7 @@ handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
652 sizeof (struct GNUNET_STATISTICS_SetMessage) + size); 685 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
653 pos->next = start; 686 pos->next = start;
654 pos->uid = uidgen++; 687 pos->uid = uidgen++;
688 pos->set = GNUNET_NO;
655 pos->msg = (void *) &pos[1]; 689 pos->msg = (void *) &pos[1];
656 pos->msg->header.size = 690 pos->msg->header.size =
657 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size); 691 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
@@ -662,9 +696,13 @@ handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
662 pos->name = &pos->service[slen]; 696 pos->name = &pos->service[slen];
663 memcpy ((void *) pos->name, name, strlen (name) + 1); 697 memcpy ((void *) pos->name, name, strlen (name) + 1);
664 start = pos; 698 start = pos;
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "New statistic on `%s:%s' with value %llu created.\n", service,
701 name, pos->value);
665 } 702 }
666 we = GNUNET_malloc (sizeof (struct WatchEntry)); 703 we = GNUNET_malloc (sizeof (struct WatchEntry));
667 we->client = client; 704 we->client = client;
705 we->last_value_set = GNUNET_NO;
668 GNUNET_SERVER_client_keep (client); 706 GNUNET_SERVER_client_keep (client);
669 we->wid = ce->max_wid++; 707 we->wid = ce->max_wid++;
670 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we); 708 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
diff --git a/src/statistics/test_statistics_api_watch_zero_value.c b/src/statistics/test_statistics_api_watch_zero_value.c
new file mode 100644
index 000000000..bbbefe904
--- /dev/null
+++ b/src/statistics/test_statistics_api_watch_zero_value.c
@@ -0,0 +1,196 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file statistics/test_statistics_api_watch_zero_value.c
22 * @brief testcase for statistics_api.c watch functions with initial 0 value
23 */
24#include "platform.h"
25#include "gnunet_common.h"
26#include "gnunet_getopt_lib.h"
27#include "gnunet_os_lib.h"
28#include "gnunet_program_lib.h"
29#include "gnunet_scheduler_lib.h"
30#include "gnunet_statistics_service.h"
31
32#define VERBOSE GNUNET_NO
33
34#define START_SERVICE GNUNET_YES
35
36static int ok;
37static int ok2;
38
39static struct GNUNET_STATISTICS_Handle *h;
40static struct GNUNET_STATISTICS_Handle *h2;
41
42static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
43
44
45static void
46force_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
47{
48 fprintf (stderr, "Timeout, failed to receive notifications: %d\n", ok);
49 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
50 GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
51 ok = 7;
52}
53
54
55static void
56normal_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57{
58 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
59 GNUNET_STATISTICS_destroy (h2, GNUNET_NO);
60}
61
62
63static int
64watch_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
65 int is_persistent)
66{
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value `%s' `%s' %llu\n",
68 subsystem, name, value);
69 GNUNET_assert (0 == strcmp (name, "test-1"));
70 if ((0 == value) && (3 == ok))
71 {
72 ok--;
73 GNUNET_STATISTICS_set (h, "test-1", 42, GNUNET_NO);
74 }
75
76 if ((42 == value) && (2 == ok))
77 {
78 ok--;
79 GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO);
80 }
81
82 if ((0 == value) && (1 == ok))
83 {
84 ok--;
85 }
86 if ((0 == ok) && (0 == ok2))
87 {
88 GNUNET_SCHEDULER_cancel (shutdown_task);
89 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
90 }
91
92 return GNUNET_OK;
93}
94
95static int
96watch_2 (void *cls, const char *subsystem, const char *name, uint64_t value,
97 int is_persistent)
98{
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value `%s' `%s' %llu\n",
100 subsystem, name, value);
101
102 GNUNET_assert (0 == strcmp (name, "test-2"));
103 if ((42 == value) && (1 == ok2))
104 {
105 ok2 = 0;
106 }
107 else
108 {
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received unexpected value %llu\n", value);
110
111 GNUNET_break (0);
112 GNUNET_SCHEDULER_cancel (shutdown_task);
113 GNUNET_SCHEDULER_add_now (&normal_shutdown, NULL);
114 }
115
116 return GNUNET_OK;
117}
118
119static void
120run (void *cls, char *const *args, const char *cfgfile,
121 const struct GNUNET_CONFIGURATION_Handle *cfg)
122{
123 h = GNUNET_STATISTICS_create ("dummy", cfg);
124 h2 = GNUNET_STATISTICS_create ("dummy-2", cfg);
125 GNUNET_assert (GNUNET_OK ==
126 GNUNET_STATISTICS_watch (h, "dummy",
127 "test-1", &watch_1, NULL));
128
129 GNUNET_assert (GNUNET_OK ==
130 GNUNET_STATISTICS_watch (h2, "dummy-2",
131 "test-2", &watch_2, NULL));
132
133 /* Set initial value to 0 */
134 GNUNET_STATISTICS_set (h, "test-1", 0, GNUNET_NO);
135 GNUNET_STATISTICS_set (h2, "test-2", 42, GNUNET_NO);
136
137 shutdown_task =
138 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &force_shutdown,
139 NULL);
140}
141
142
143static int
144check ()
145{
146 char *const argv[] = { "test-statistics-api",
147 "-c",
148 "test_statistics_api_data.conf",
149 NULL
150 };
151 struct GNUNET_GETOPT_CommandLineOption options[] = {
152 GNUNET_GETOPT_OPTION_END
153 };
154#if START_SERVICE
155 struct GNUNET_OS_Process *proc;
156
157 proc =
158 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-statistics",
159 "gnunet-service-statistics",
160#if VERBOSE
161 "-L", "DEBUG",
162#endif
163 "-c", "test_statistics_api_data.conf", NULL);
164#endif
165 GNUNET_assert (NULL != proc);
166 ok = 3;
167 ok2 = 1;
168 GNUNET_PROGRAM_run (3, argv, "test-statistics-api", "nohelp", options, &run,
169 NULL);
170#if START_SERVICE
171 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
172 {
173 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
174 ok = 1;
175 }
176 GNUNET_OS_process_wait (proc);
177 GNUNET_OS_process_close (proc);
178 proc = NULL;
179#endif
180 if ((0 == ok) && (0 == ok2))
181 return 0;
182 else
183 return 1;
184}
185
186int
187main (int argc, char *argv[])
188{
189 int ret;
190
191 ret = check ();
192
193 return ret;
194}
195
196/* end of test_statistics_api_watch_zero_value.c */