3eef887fc697cb27ac8e4c52eae07630726236b0
[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_util_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "statistics.h"
33
34 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35
36 /**
37  * Final status code.
38  */
39 static int ret;
40
41 /**
42  * Set to subsystem that we're going to get stats for (or NULL for all).
43  */
44 static char *subsystem;
45
46 /**
47  * Set to the specific stat value that we are after (or NULL for all).
48  */
49 static char *name;
50
51 /**
52  * Make the value that is being set persistent.
53  */
54 static int persistent;
55
56 /**
57  * Watch value continuously
58  */
59 static int watch;
60
61 /**
62  * Quiet mode
63  */
64 static int quiet;
65
66
67 /**
68  * Callback function to process statistic values.
69  *
70  * @param cls closure
71  * @param subsystem name of subsystem that created the statistic
72  * @param name the name of the datum
73  * @param value the current value
74  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
75  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
76  */
77 static int
78 printer (void *cls, const char *subsystem, const char *name, uint64_t value,
79          int is_persistent)
80 {
81   struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
82   char * now_str;
83   if (quiet == GNUNET_NO)
84   {
85     if (GNUNET_YES == watch)
86     {
87       now_str = GNUNET_STRINGS_absolute_time_to_string(now);
88       FPRINTF (stdout, "%24s %s%12s %50s: %16llu \n",
89                now_str,
90                is_persistent ? "!" : " ",
91                subsystem, _(name), (unsigned long long) value);
92       GNUNET_free (now_str);
93     }
94     else
95     {
96       FPRINTF (stdout, "%s%12s %50s: %16llu \n",
97                is_persistent ? "!" : " ",
98                subsystem, _(name), (unsigned long long) value);
99     }
100   }
101   else
102     FPRINTF (stdout, "%llu\n", (unsigned long long) value);
103
104   return GNUNET_OK;
105 }
106
107
108 /**
109  * Function called last by the statistics code.
110  *
111  * @param cls closure
112  * @param success GNUNET_OK if statistics were
113  *        successfully obtained, GNUNET_SYSERR if not.
114  */
115 static void
116 cleanup (void *cls, int success)
117 {
118   struct GNUNET_STATISTICS_Handle *h = cls;
119
120   if (success != GNUNET_OK)
121   {
122     FPRINTF (stderr, "%s", _("Failed to obtain statistics.\n"));
123     ret = 1;
124   }
125   if (NULL != h)
126   {
127     GNUNET_STATISTICS_destroy (h, GNUNET_NO);
128     h = NULL;
129   }
130 }
131
132
133 static void
134 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   struct GNUNET_STATISTICS_Handle *h = cls;
137
138   GNUNET_STATISTICS_watch_cancel (h, subsystem, name, &printer, h);
139   if (NULL != h)
140   {
141     GNUNET_STATISTICS_destroy (h, GNUNET_NO);
142     h = NULL;
143   }
144 }
145
146
147 /**
148  * Main function that will be run by the scheduler.
149  *
150  * @param cls closure
151  * @param args remaining command-line arguments
152  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
153  * @param cfg configuration
154  */
155 static void
156 run (void *cls, char *const *args, const char *cfgfile,
157      const struct GNUNET_CONFIGURATION_Handle *cfg)
158 {
159   struct GNUNET_STATISTICS_Handle *h;
160   unsigned long long val;
161
162   if (args[0] != NULL)
163   {
164     if ((1 != SSCANF (args[0], "%llu", &val)) || (subsystem == NULL) ||
165         (name == NULL))
166     {
167       FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
168       ret = 1;
169       return;
170     }
171     h = GNUNET_STATISTICS_create (subsystem, cfg);
172     if (NULL == h)
173     {
174       ret = 1;
175       return;
176     }
177     GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
178     GNUNET_STATISTICS_destroy (h, GNUNET_YES);
179     h = NULL;
180     return;
181   }
182   h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg);
183   if (NULL == h)
184   {
185     ret = 1;
186     return;
187   }
188   if (GNUNET_NO == watch)
189   {
190     if (NULL ==
191       GNUNET_STATISTICS_get (h, subsystem, name, GET_TIMEOUT, &cleanup,
192                              &printer, h))
193     cleanup (h, GNUNET_SYSERR);
194   }
195   else
196   {
197     if ((NULL == subsystem) || (NULL == name))
198     {
199       printf (_("No subsystem or name given\n"));
200       if (h != NULL)
201         GNUNET_STATISTICS_destroy (h, GNUNET_NO);
202       ret = 1;
203       return;
204     }
205     if (GNUNET_OK != GNUNET_STATISTICS_watch (h, subsystem, name, &printer, h))
206     {
207       fprintf (stderr, _("Failed to initialize watch routine\n"));
208       GNUNET_SCHEDULER_add_now (&shutdown_task, h);
209       return;
210     }
211     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, h);
212   }
213 }
214
215 /**
216  * The main function to obtain statistics in GNUnet.
217  *
218  * @param argc number of arguments from the command line
219  * @param argv command line arguments
220  * @return 0 ok, 1 on error
221  */
222 int
223 main (int argc, char *const *argv)
224 {
225   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
226     {'n', "name", "NAME",
227      gettext_noop ("limit output to statistics for the given NAME"), 1,
228      &GNUNET_GETOPT_set_string, &name},
229     {'p', "persistent", NULL,
230      gettext_noop ("make the value being set persistent"), 0,
231      &GNUNET_GETOPT_set_one, &persistent},
232     {'s', "subsystem", "SUBSYSTEM",
233      gettext_noop ("limit output to the given SUBSYSTEM"), 1,
234      &GNUNET_GETOPT_set_string, &subsystem},
235     {'q', "quiet", NULL,
236      gettext_noop ("just print the statistics value"), 0,
237      &GNUNET_GETOPT_set_one, &quiet},
238     {'w', "watch", NULL,
239      gettext_noop ("watch value continously"), 0,
240      &GNUNET_GETOPT_set_one, &watch},
241     GNUNET_GETOPT_OPTION_END
242   };
243   return (GNUNET_OK ==
244           GNUNET_PROGRAM_run (argc, argv, "gnunet-statistics [options [value]]",
245                               gettext_noop
246                               ("Print statistics about GNUnet operations."),
247                               options, &run, NULL)) ? ret : 1;
248 }
249
250 /* end of gnunet-statistics.c */