2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004-2007, 2009, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file statistics/gnunet-statistics.c
21 * @brief tool to obtain statistics
22 * @author Christian Grothoff
23 * @author Igor Wronsky
26 #include "gnunet_util_lib.h"
27 #include "gnunet_statistics_service.h"
28 #include "statistics.h"
37 * Set to subsystem that we're going to get stats for (or NULL for all).
39 static char *subsystem;
42 * The path of the testbed data.
44 static char *path_testbed;
47 * Set to the specific stat value that we are after (or NULL for all).
52 * Make the value that is being set persistent.
54 static int persistent;
57 * Watch value continuously
67 * @brief Separator string for csv.
69 static char *csv_separator;
74 static char *remote_host;
79 static unsigned long long remote_port;
84 static unsigned long long set_val;
92 * @brief Representation of all (testbed) nodes.
96 * @brief Index of the node in this array.
101 * @brief Configuration handle for this node
103 struct GNUNET_CONFIGURATION_Handle *conf;
106 * Handle for pending GET operation.
108 struct GNUNET_STATISTICS_GetHandle *gh;
111 * @brief Statistics handle nodes.
113 struct GNUNET_STATISTICS_Handle *handle;
115 * @brief Identifier for shutdown task for this node.
117 struct GNUNET_SCHEDULER_Task *shutdown_task;
121 * @brief Number of configurations of all (testbed) nodes.
123 static unsigned num_nodes;
126 * @brief Set of values for a combination of subsystem and name.
131 * @brief Subsystem of the valueset.
136 * @brief Name of the valueset.
146 * @brief Persistence of the values.
152 * @brief Collection of all values (represented with #ValueSet).
154 static struct GNUNET_CONTAINER_MultiHashMap *values;
157 * @brief Number of nodes that have their values ready.
159 static int num_nodes_ready;
162 * @brief Number of nodes that have their values ready.
164 static int num_nodes_ready_shutdown;
167 * @brief Create a new #ValueSet
169 * @param subsystem Subsystem of the valueset.
170 * @param name Name of the valueset.
171 * @param num_values Number of values in valueset - number of peers.
172 * @param is_persistent Persistence status of values.
174 * @return Newly allocated #ValueSet.
176 static struct ValueSet *
177 new_value_set (const char *subsystem,
182 struct ValueSet *value_set;
184 value_set = GNUNET_new (struct ValueSet);
185 value_set->subsystem = GNUNET_strdup (subsystem);
186 value_set->name = GNUNET_strdup (name);
187 value_set->values = GNUNET_new_array (num_values, uint64_t);
188 value_set->is_persistent = persistent;
193 * @brief Print the (collected) values.
195 * Implements #GNUNET_CONTAINER_HashMapIterator.
197 * @param cls Closure - unused
198 * @param key #GNUNET_HashCode key of #GNUNET_CONTAINER_MultiHashMap iterator -
200 * @param value Values represented as #ValueSet.
202 * @return GNUNET_YES - continue iteration.
206 const struct GNUNET_HashCode *key,
209 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
211 struct ValueSet *value_set = value;
213 if (quiet == GNUNET_NO)
215 if (GNUNET_YES == watch)
217 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
219 "%24s%s %s%s%12s%s %50s%s ",
222 value_set->is_persistent ? "!" : " ",
224 value_set->subsystem,
227 (0 == strlen (csv_separator) ? ":": csv_separator));
232 "%s%s%12s%s %50s%s ",
233 value_set->is_persistent ? "!" : " ",
235 value_set->subsystem,
238 (0 == strlen (csv_separator) ? ":": csv_separator));
241 for (unsigned i = 0; i < num_nodes; i++)
245 (unsigned long long) value_set->values[i],
248 FPRINTF (stdout, "\n");
249 GNUNET_free (value_set->subsystem);
250 GNUNET_free (value_set->name);
251 GNUNET_free (value_set->values);
252 GNUNET_free (value_set);
257 * Callback function to process statistic values.
260 * @param subsystem name of subsystem that created the statistic
261 * @param name the name of the datum
262 * @param value the current value
263 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
264 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
267 printer_watch (void *cls,
268 const char *subsystem,
273 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
276 if (quiet == GNUNET_NO)
278 if (GNUNET_YES == watch)
280 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
282 "%24s%s %s%s%12s%s %50s%s %16llu\n",
285 is_persistent ? "!" : " ",
290 (0 == strlen (csv_separator) ? ":": csv_separator),
291 (unsigned long long) value);
296 "%s%s%12s%s %50s%s %16llu\n",
297 is_persistent ? "!" : " ",
302 (0 == strlen (csv_separator) ? ":": csv_separator),
303 (unsigned long long) value);
309 (unsigned long long) value);
315 * @brief Clean all data structures related to given node.
317 * Also clears global structures if we are the last node to clean.
319 * @param cls the index of the node
322 clean_node (void *cls)
324 const unsigned index_node = *(unsigned *) cls;
325 struct GNUNET_STATISTICS_Handle *h;
326 struct GNUNET_STATISTICS_GetHandle *gh;
328 if ( (NULL != path_testbed) && /* were issued with -t <testbed-path> option */
329 (NULL != nodes[index_node].conf) )
331 GNUNET_CONFIGURATION_destroy (nodes[index_node].conf);
332 nodes[index_node].conf = NULL;
335 h = nodes[index_node].handle;
336 gh = nodes[index_node].gh;
340 GNUNET_STATISTICS_get_cancel (gh);
343 if (GNUNET_YES == watch)
345 GNUNET_assert (GNUNET_OK ==
346 GNUNET_STATISTICS_watch_cancel (h,
350 &nodes[index_node].index_node));
355 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
359 num_nodes_ready_shutdown++;
360 if (num_nodes == num_nodes_ready_shutdown)
362 GNUNET_array_grow (nodes, num_nodes, 0);
363 GNUNET_CONTAINER_multihashmap_destroy (values);
368 * @brief Print and shutdown
373 print_finish (void *cls)
375 GNUNET_CONTAINER_multihashmap_iterate (values, printer, NULL);
376 GNUNET_SCHEDULER_shutdown();
380 * @brief Called once all statistic values are available.
382 * Implements #GNUNET_STATISTICS_Callback
384 * @param cls Closure - The index of the node.
385 * @param succes Whether statistics were obtained successfully.
388 continuation_print (void *cls,
391 const unsigned index_node = *(unsigned *) cls;
393 nodes[index_node].gh = NULL;
394 if (GNUNET_OK != success)
396 if (NULL == remote_host)
399 _("Failed to obtain statistics.\n"));
402 _("Failed to obtain statistics from host `%s:%llu'\n"),
407 if (NULL != nodes[index_node].shutdown_task)
409 GNUNET_SCHEDULER_cancel (nodes[index_node].shutdown_task);
410 nodes[index_node].shutdown_task = NULL;
412 GNUNET_SCHEDULER_add_now (clean_node, &nodes[index_node].index_node);
414 if (num_nodes_ready == num_nodes)
416 GNUNET_SCHEDULER_add_now (print_finish, NULL);
421 * Function called last by the statistics code.
424 * @param success #GNUNET_OK if statistics were
425 * successfully obtained, #GNUNET_SYSERR if not.
431 for (unsigned i = 0; i < num_nodes; i++)
435 if (GNUNET_OK != success)
437 if (NULL == remote_host)
440 _("Failed to obtain statistics.\n"));
443 _("Failed to obtain statistics from host `%s:%llu'\n"),
448 GNUNET_SCHEDULER_shutdown ();
452 * @brief Iterate over statistics values and store them in #values.
453 * They will be printed once all are available.
455 * @param cls Cosure - Node index.
456 * @param subsystem Subsystem of the value.
457 * @param name Name of the value.
458 * @param value Value itself.
459 * @param is_persistent Persistence.
461 * @return GNUNET_OK - continue.
464 collector (void *cls,
465 const char *subsystem,
470 const unsigned index_node = *(unsigned *) cls;
471 struct GNUNET_HashCode *key;
472 struct GNUNET_HashCode hc;
474 unsigned len_subsys_name;
475 struct ValueSet *value_set;
477 len_subsys_name = strlen (subsystem) + 3 + strlen (name) + 1;
478 subsys_name = GNUNET_malloc (len_subsys_name);
479 SPRINTF (subsys_name, "%s---%s", subsystem, name);
481 GNUNET_CRYPTO_hash (subsys_name, len_subsys_name, key);
482 GNUNET_free (subsys_name);
483 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (values, key))
486 value_set = GNUNET_CONTAINER_multihashmap_get (values, key);
491 value_set = new_value_set (subsystem, name, num_nodes, is_persistent);
494 value_set->values[index_node] = value;
496 GNUNET_CONTAINER_multihashmap_put (values, key, value_set,
497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
502 * Main task that does the actual work.
504 * @param cls closure with our configuration
507 main_task (void *cls)
509 unsigned index_node = *(unsigned *) cls;
510 const struct GNUNET_CONFIGURATION_Handle *cfg = nodes[index_node].conf;
514 if (NULL == subsystem)
518 _("Missing argument: subsystem \n"));
526 _("Missing argument: name\n"));
530 nodes[index_node].handle = GNUNET_STATISTICS_create (subsystem,
532 if (NULL == nodes[index_node].handle)
537 GNUNET_STATISTICS_set (nodes[index_node].handle,
541 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
543 nodes[index_node].handle = NULL;
546 if (NULL == (nodes[index_node].handle = GNUNET_STATISTICS_create ("gnunet-statistics",
552 if (GNUNET_NO == watch)
555 (nodes[index_node].gh = GNUNET_STATISTICS_get (nodes[index_node].handle,
560 &nodes[index_node].index_node)) )
561 cleanup (nodes[index_node].handle,
566 if ( (NULL == subsystem) ||
569 printf (_("No subsystem or name given\n"));
570 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
572 nodes[index_node].handle = NULL;
577 GNUNET_STATISTICS_watch (nodes[index_node].handle,
581 &nodes[index_node].index_node))
584 _("Failed to initialize watch routine\n"));
585 nodes[index_node].shutdown_task =
586 GNUNET_SCHEDULER_add_now (&clean_node,
587 &nodes[index_node].index_node);
591 nodes[index_node].shutdown_task =
592 GNUNET_SCHEDULER_add_shutdown (&clean_node,
593 &nodes[index_node].index_node);
597 * @brief Iter over content of a node's directory to check for existence of a
600 * Implements #GNUNET_FileNameCallback
602 * @param cls pointer to indicate success
603 * @param filename filename inside the directory of the potential node
605 * @return to continue iteration or not to
608 iter_check_config (void *cls,
609 const char *filename)
611 if (0 == strncmp (GNUNET_STRINGS_get_short_name (filename), "config", 6))
613 /* Found the config - stop iteration successfully */
614 GNUNET_array_grow (nodes, num_nodes, num_nodes+1);
615 nodes[num_nodes-1].conf = GNUNET_CONFIGURATION_create();
616 nodes[num_nodes-1].index_node = num_nodes-1;
617 if (GNUNET_OK != GNUNET_CONFIGURATION_load (nodes[num_nodes-1].conf, filename))
619 FPRINTF (stderr, "Failed loading config `%s'\n", filename);
620 return GNUNET_SYSERR;
626 /* Continue iteration */
632 * @brief Iterates over filenames in testbed directory.
634 * Implements #GNUNET_FileNameCallback
636 * Checks if the file is a directory for a testbed node
637 * and counts the nodes.
639 * @param cls counter of nodes
640 * @param filename full path of the file in testbed
642 * @return status whether to continue iteration
645 iter_testbed_path (void *cls,
646 const char *filename)
650 GNUNET_assert (NULL != filename);
651 if (1 == SSCANF (GNUNET_STRINGS_get_short_name (filename),
655 if (-1 == GNUNET_DISK_directory_scan (filename,
659 /* This is probably no directory for a testbed node
660 * Go on with iteration */
669 * @brief Count the number of nodes running in the testbed
671 * @param path_testbed path to the testbed data
673 * @return number of running nodes
676 discover_testbed_nodes (const char *path_testbed)
680 num_dir_entries = GNUNET_DISK_directory_scan (path_testbed,
683 if (-1 == num_dir_entries)
686 "Failure during scanning directory `%s'\n",
694 * Main function that will be run by the scheduler.
697 * @param args remaining command-line arguments
698 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
699 * @param cfg configuration
705 const struct GNUNET_CONFIGURATION_Handle *cfg)
707 struct GNUNET_CONFIGURATION_Handle *c;
709 c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
710 set_value = GNUNET_NO;
711 if (NULL == csv_separator) csv_separator = "";
714 if (1 != SSCANF (args[0],
719 _("Invalid argument `%s'\n"),
724 set_value = GNUNET_YES;
726 if (NULL != remote_host)
728 if (0 == remote_port)
731 GNUNET_CONFIGURATION_get_value_number (cfg,
737 _("A port is required to connect to host `%s'\n"),
742 else if (65535 <= remote_port)
745 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
750 /* Manipulate configuration */
751 GNUNET_CONFIGURATION_set_value_string (c,
755 GNUNET_CONFIGURATION_set_value_string (c,
759 GNUNET_CONFIGURATION_set_value_number (c,
764 if (NULL == path_testbed)
766 values = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
767 GNUNET_array_grow (nodes, num_nodes, 1);
768 nodes[0].index_node = 0;
770 GNUNET_SCHEDULER_add_now (&main_task, &nodes[0].index_node);
774 if (GNUNET_YES == watch)
776 printf (_("Not able to watch testbed nodes (yet - feel free to implement)\n"));
780 values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
781 if (-1 == discover_testbed_nodes (path_testbed))
785 /* For each config/node collect statistics */
786 for (unsigned i = 0; i < num_nodes; i++)
788 GNUNET_SCHEDULER_add_now (&main_task,
789 &nodes[i].index_node);
796 * The main function to obtain statistics in GNUnet.
798 * @param argc number of arguments from the command line
799 * @param argv command line arguments
800 * @return 0 ok, 1 on error
803 main (int argc, char *const *argv)
805 struct GNUNET_GETOPT_CommandLineOption options[] = {
806 GNUNET_GETOPT_option_string ('n',
809 gettext_noop ("limit output to statistics for the given NAME"),
812 GNUNET_GETOPT_option_flag ('p',
814 gettext_noop ("make the value being set persistent"),
817 GNUNET_GETOPT_option_string ('s',
820 gettext_noop ("limit output to the given SUBSYSTEM"),
823 GNUNET_GETOPT_option_string ('S',
826 gettext_noop ("use as csv separator"),
829 GNUNET_GETOPT_option_filename ('t',
832 gettext_noop ("path to the folder containing the testbed data"),
835 GNUNET_GETOPT_option_flag ('q',
837 gettext_noop ("just print the statistics value"),
840 GNUNET_GETOPT_option_flag ('w',
842 gettext_noop ("watch value continuously"),
845 GNUNET_GETOPT_option_string ('r',
848 gettext_noop ("connect to remote host"),
851 GNUNET_GETOPT_option_ulong ('o',
854 gettext_noop ("port for remote host"),
857 GNUNET_GETOPT_OPTION_END
862 GNUNET_STRINGS_get_utf8_args (argc, argv,
867 GNUNET_PROGRAM_run (argc,
869 "gnunet-statistics [options [value]]",
871 ("Print statistics about GNUnet operations."),
875 GNUNET_free_non_null (remote_host);
876 GNUNET_free ((void*) argv);
880 /* end of gnunet-statistics.c */