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