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