2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file statistics/gnunet-statistics.c
23 * @brief tool to obtain statistics
24 * @author Christian Grothoff
25 * @author Igor Wronsky
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "statistics.h"
33 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
41 * Set to subsystem that we're going to get stats for (or NULL for all).
43 static char *subsystem;
46 * Set to the specific stat value that we are after (or NULL for all).
51 * Make the value that is being set persistent.
53 static int persistent;
56 * Callback function to process statistic values.
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
67 const char *subsystem,
69 uint64_t value, int is_persistent)
72 "%s%-12s %-50s: %16llu\n",
73 is_persistent ? "!" : " ", subsystem, _(name),
74 (unsigned long long) value);
80 * Function called last by the statistics code.
83 * @param success GNUNET_OK if statistics were
84 * successfully obtained, GNUNET_SYSERR if not.
87 cleanup (void *cls, int success)
89 struct GNUNET_STATISTICS_Handle *h = cls;
91 if (success != GNUNET_OK)
94 _("Failed to obtain statistics.\n"));
98 GNUNET_STATISTICS_destroy (h,
104 * Main function that will be run by the scheduler.
107 * @param args remaining command-line arguments
108 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
109 * @param cfg configuration
115 const struct GNUNET_CONFIGURATION_Handle *cfg)
117 struct GNUNET_STATISTICS_Handle *h;
118 unsigned long long val;
122 if ((1 != SSCANF (args[0], "%llu", &val)) ||
123 (subsystem == NULL) || (name == NULL))
125 FPRINTF (stderr, _("Invalid argument `%s'\n"), args[0]);
129 h = GNUNET_STATISTICS_create (subsystem, cfg);
135 GNUNET_STATISTICS_set (h, name, (uint64_t) val, persistent);
136 GNUNET_STATISTICS_destroy (h, GNUNET_YES);
139 h = GNUNET_STATISTICS_create ("gnunet-statistics", cfg);
145 if (NULL == GNUNET_STATISTICS_get (h,
146 subsystem, name, GET_TIMEOUT, &cleanup, &printer, h))
147 cleanup (h, GNUNET_SYSERR);
151 * The main function to obtain statistics in GNUnet.
153 * @param argc number of arguments from the command line
154 * @param argv command line arguments
155 * @return 0 ok, 1 on error
158 main (int argc, char *const *argv)
160 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
161 {'n', "name", "NAME",
162 gettext_noop ("limit output to statistcs for the given NAME"), 1,
163 &GNUNET_GETOPT_set_string, &name},
164 {'p', "persistent", NULL,
165 gettext_noop ("make the value being set persistent"), 0,
166 &GNUNET_GETOPT_set_one, &persistent},
167 {'s', "subsystem", "SUBSYSTEM",
168 gettext_noop ("limit output to the given SUBSYSTEM"), 1,
169 &GNUNET_GETOPT_set_string, &subsystem},
170 GNUNET_GETOPT_OPTION_END
173 GNUNET_PROGRAM_run (argc,
175 "gnunet-statistics [options [value]]",
177 ("Print statistics about GNUnet operations."),
178 options, &run, NULL)) ? ret : 1;
181 /* end of gnunet-statistics.c */