-more to remove
[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 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 /**
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, 1)
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  * Watch value continuously
57  */
58 static int watch;
59
60 /**
61  * Quiet mode
62  */
63 static int quiet;
64
65 /**
66  * Callback function to process statistic values.
67  *
68  * @param cls closure
69  * @param subsystem name of subsystem that created the statistic
70  * @param name the name of the datum
71  * @param value the current value
72  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
73  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
74  */
75 static int
76 printer (void *cls, const char *subsystem, const char *name, uint64_t value,
77          int is_persistent)
78 {
79   if (quiet == GNUNET_NO)
80     FPRINTF (stdout, "%s%-12s %-50s: %16llu\n", is_persistent ? "!" : " ",
81            subsystem, _(name), (unsigned long long) value);
82   else
83     FPRINTF (stdout, "%llu\n", (unsigned long long) value);
84
85   return GNUNET_OK;
86 }
87
88
89 /**
90  * Function called last by the statistics code.
91  *
92  * @param cls closure
93  * @param success GNUNET_OK if statistics were
94  *        successfully obtained, GNUNET_SYSERR if not.
95  */
96 static void
97 cleanup (void *cls, int success)
98 {
99   struct GNUNET_STATISTICS_Handle *h = cls;
100
101   if (success != GNUNET_OK)
102   {
103     FPRINTF (stderr, "%s", _("Failed to obtain statistics.\n"));
104     ret = 1;
105   }
106   if (h != NULL)
107     GNUNET_STATISTICS_destroy (h, GNUNET_NO);
108 }
109 static void
110 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   struct GNUNET_STATISTICS_Handle *h = cls;
113   GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h);
114   if (h != NULL)
115     GNUNET_STATISTICS_destroy (h, GNUNET_NO);
116 }
117
118 /**
119  * Main function that will be run by the scheduler.
120  *
121  * @param cls closure
122  * @param args remaining command-line arguments
123  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
124  * @param cfg configuration
125  */
126 static void
127 run (void *cls, char *const *args, const char *cfgfile,
128      const struct GNUNET_CONFIGURATION_Handle *cfg)
129 {
130   struct GNUNET_STATISTICS_Handle *h;
131   unsigned long long val;
132
133   if (args[0] != NULL)
134   {
135     if ((1 != SSCANF (args[0], "%llu", &val)) || (subsystem == NULL) ||
136         (name == NULL))
137     {
138       FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
139       ret = 1;
140       return;
141     }
142     h = GNUNET_STATISTICS_create (subsystem, cfg);
143     if (h == NULL)
144     {
145       ret = 1;
146       return;
147     }
148     GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
149     GNUNET_STATISTICS_destroy (h, GNUNET_YES);
150     return;
151   }
152   h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg);
153   if (h == NULL)
154   {
155     ret = 1;
156     return;
157   }
158   if (GNUNET_NO == watch)
159   {
160     if (NULL ==
161       GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT, &cleanup,
162                              &printer, h))
163     cleanup (h, GNUNET_SYSERR);
164   }
165   else
166   {
167     if ((NULL == subsystem) || (NULL == name))
168     {
169       printf (_("No subsystem or name given\n"));
170       if (h != NULL)
171         GNUNET_STATISTICS_destroy (h, GNUNET_NO);
172       ret = 1;
173       return;
174     }
175     GNUNET_STATISTICS_watch(h, subsystem, name, &printer, h);
176     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, h);
177   }
178 }
179
180 /**
181  * The main function to obtain statistics in GNUnet.
182  *
183  * @param argc number of arguments from the command line
184  * @param argv command line arguments
185  * @return 0 ok, 1 on error
186  */
187 int
188 main (int argc, char *const *argv)
189 {
190   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
191     {'n', "name", "NAME",
192      gettext_noop ("limit output to statistics for the given NAME"), 1,
193      &GNUNET_GETOPT_set_string, &name},
194     {'p', "persistent", NULL,
195      gettext_noop ("make the value being set persistent"), 0,
196      &GNUNET_GETOPT_set_one, &persistent},
197     {'s', "subsystem", "SUBSYSTEM",
198      gettext_noop ("limit output to the given SUBSYSTEM"), 1,
199      &GNUNET_GETOPT_set_string, &subsystem},
200     {'q', "quiet", NULL,
201      gettext_noop ("just print the statistics value"), 0,
202      &GNUNET_GETOPT_set_one, &quiet},
203     {'w', "watch", NULL,
204      gettext_noop ("watch value continously"), 0,
205      &GNUNET_GETOPT_set_one, &watch},
206     GNUNET_GETOPT_OPTION_END
207   };
208   return (GNUNET_OK ==
209           GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
210                               gettext_noop
211                               ("Print statistics about GNUnet operations."),
212                               options, &run, NULL)) ? ret : 1;
213 }
214
215 /* end of gnunet-statistics.c */