976a8217472acb6f89e000e370ea18575cb02b72
[oweals/gnunet.git] / src / statistics / gnunet-statistics.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 2, 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 /**
22  * @file statistics/gnunet-statistics.c
23  * @brief tool to obtain statistics
24  * @author Christian Grothoff
25  * @author Igor Wronsky
26  */
27 #include "platform.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "statistics.h"
32
33 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
34
35 /**
36  * Final status code.
37  */
38 static int ret;
39
40 /**
41  * Set to subsystem that we're going to get stats for (or NULL for all).
42  */
43 static char *subsystem;
44
45 /**
46  * Set to the specific stat value that we are after (or NULL for all).
47  */
48 static char *name;
49
50 /**
51  * Make the value that is being set persistent.
52  */
53 static int persistent;
54
55 /**
56  * Callback function to process statistic values.
57  *
58  * @param cls closure
59  * @param subsystem name of subsystem that created the statistic
60  * @param name the name of the datum
61  * @param value the current value
62  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
63  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
64  */
65 static int
66 printer (void *cls,
67          const char *subsystem,
68          const char *name, 
69          uint64_t value, int is_persistent)
70 {
71   FPRINTF (stdout,
72            "%s%-12s %-50s: %16llu\n",
73            is_persistent ? "!" : " ", subsystem, _(name),
74            (unsigned long long) value);
75   return GNUNET_OK;
76 }
77
78
79 /**
80  * Function called last by the statistics code.
81  *
82  * @param cls closure
83  * @param success GNUNET_OK if statistics were
84  *        successfully obtained, GNUNET_SYSERR if not.
85  */
86 static void
87 cleanup (void *cls, int success)
88 {
89   struct GNUNET_STATISTICS_Handle *h = cls;
90
91   if (success != GNUNET_OK)
92     ret = 1;
93   if (h != NULL)
94     GNUNET_STATISTICS_destroy (h,
95                                GNUNET_NO);
96 }
97
98
99 /**
100  * Main function that will be run by the scheduler.
101  *
102  * @param cls closure
103  * @param sched the scheduler to use
104  * @param args remaining command-line arguments
105  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
106  * @param cfg configuration
107  */
108 static void
109 run (void *cls,
110      struct GNUNET_SCHEDULER_Handle *sched,
111      char *const *args,
112      const char *cfgfile,
113      const struct GNUNET_CONFIGURATION_Handle *cfg)
114 {
115   struct GNUNET_STATISTICS_Handle *h;
116   unsigned long long val;
117
118   if (args[0] != NULL)
119     {
120       if ((1 != SSCANF (args[0], "%llu", &val)) ||
121           (subsystem == NULL) || (name == NULL))
122         {
123           FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
124           ret = 1;
125           return;
126         }
127       h = GNUNET_STATISTICS_create (sched, subsystem, cfg);
128       if (h == NULL)
129         {
130           ret = 1;
131           return;
132         }
133       GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
134       GNUNET_STATISTICS_destroy (h, GNUNET_YES);
135       return;
136     }
137   h = GNUNET_STATISTICS_create (sched, "gnunet-statistics", cfg);
138   if (h == NULL)
139     {
140       ret = 1;
141       return;
142     }
143   if (NULL == GNUNET_STATISTICS_get (h,
144                                      subsystem, name, GET_TIMEOUT, &cleanup, &printer, h))
145     cleanup (h, GNUNET_SYSERR);
146 }
147
148 /**
149  * gnunet-statistics command line options
150  */
151 static struct GNUNET_GETOPT_CommandLineOption options[] = {
152   {'n', "name", "NAME",
153    gettext_noop ("limit output to statistcs for the given NAME"), 1,
154    &GNUNET_GETOPT_set_string, &name},
155   {'p', "persistent", NULL,
156    gettext_noop ("make the value being set persistent"), 0,
157    &GNUNET_GETOPT_set_one, &persistent},
158   {'s', "subsystem", "SUBSYSTEM",
159    gettext_noop ("limit output to the given SUBSYSTEM"), 1,
160    &GNUNET_GETOPT_set_string, &subsystem},
161   GNUNET_GETOPT_OPTION_END
162 };
163
164
165 /**
166  * The main function to obtain statistics in GNUnet.
167  *
168  * @param argc number of arguments from the command line
169  * @param argv command line arguments
170  * @return 0 ok, 1 on error
171  */
172 int
173 main (int argc, char *const *argv)
174 {
175   return (GNUNET_OK ==
176           GNUNET_PROGRAM_run (argc,
177                               argv,
178                               "gnunet-statistics",
179                               gettext_noop
180                               ("Print statistics about GNUnet operations."),
181                               options, &run, NULL)) ? ret : 1;
182 }
183
184 /* end of gnunet-statistics.c */