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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file statistics/gnunet-statistics.c
23 * @brief tool to obtain statistics
24 * @author Christian Grothoff
25 * @author Igor Wronsky
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "statistics.h"
39 * Set to subsystem that we're going to get stats for (or NULL for all).
41 static char *subsystem;
44 * The path of the testbed data.
46 static char *path_testbed;
49 * Set to the specific stat value that we are after (or NULL for all).
54 * Make the value that is being set persistent.
56 static int persistent;
59 * Watch value continuously
69 * @brief Separator string for csv.
71 static char *csv_separator;
76 static char *remote_host;
81 static unsigned long long remote_port;
86 static unsigned long long set_val;
94 * @brief Representation of all (testbed) nodes.
98 * @brief Index of the node in this array.
103 * @brief Configuration handle for this node
105 struct GNUNET_CONFIGURATION_Handle *conf;
108 * Handle for pending GET operation.
110 struct GNUNET_STATISTICS_GetHandle *gh;
113 * @brief Statistics handle nodes.
115 struct GNUNET_STATISTICS_Handle *handle;
117 * @brief Identifier for shutdown task for this node.
119 struct GNUNET_SCHEDULER_Task *shutdown_task;
123 * @brief Number of configurations of all (testbed) nodes.
125 static unsigned num_nodes;
128 * @brief Set of values for a combination of subsystem and name.
133 * @brief Subsystem of the valueset.
138 * @brief Name of the valueset.
148 * @brief Persistence of the values.
154 * @brief Collection of all values (represented with #ValueSet).
156 static struct GNUNET_CONTAINER_MultiHashMap *values;
159 * @brief Number of nodes that have their values ready.
161 static int num_nodes_ready;
164 * @brief Number of nodes that have their values ready.
166 static int num_nodes_ready_shutdown;
169 * @brief Create a new #ValueSet
171 * @param subsystem Subsystem of the valueset.
172 * @param name Name of the valueset.
173 * @param num_values Number of values in valueset - number of peers.
174 * @param is_persistent Persistence status of values.
176 * @return Newly allocated #ValueSet.
178 static struct ValueSet *
179 new_value_set (const char *subsystem,
184 struct ValueSet *value_set;
186 value_set = GNUNET_new (struct ValueSet);
187 value_set->subsystem = GNUNET_strdup (subsystem);
188 value_set->name = GNUNET_strdup (name);
189 value_set->values = GNUNET_new_array (num_values, uint64_t);
190 value_set->is_persistent = persistent;
195 * @brief Print the (collected) values.
197 * Implements #GNUNET_CONTAINER_HashMapIterator.
199 * @param cls Closure - unused
200 * @param key #GNUNET_HashCode key of #GNUNET_CONTAINER_MultiHashMap iterator -
202 * @param value Values represented as #ValueSet.
204 * @return GNUNET_YES - continue iteration.
208 const struct GNUNET_HashCode *key,
211 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
213 struct ValueSet *value_set = value;
215 if (quiet == GNUNET_NO)
217 if (GNUNET_YES == watch)
219 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
221 "%24s%s %s%s%12s%s %s%50s%s%s ",
224 value_set->is_persistent ? "!" : " ",
226 value_set->subsystem,
228 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
230 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
231 (0 == strlen (csv_separator) ? ":": csv_separator));
236 "%s%s%12s%s %s%50s%s%s ",
237 value_set->is_persistent ? "!" : " ",
239 value_set->subsystem,
241 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
243 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
244 (0 == strlen (csv_separator) ? ":": csv_separator));
247 for (unsigned i = 0; i < num_nodes; i++)
251 (unsigned long long) value_set->values[i],
254 FPRINTF (stdout, "\n");
255 GNUNET_free (value_set->subsystem);
256 GNUNET_free (value_set->name);
257 GNUNET_free (value_set->values);
258 GNUNET_free (value_set);
263 * Callback function to process statistic values.
266 * @param subsystem name of subsystem that created the statistic
267 * @param name the name of the datum
268 * @param value the current value
269 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
270 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
273 printer_watch (void *cls,
274 const char *subsystem,
279 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
282 if (quiet == GNUNET_NO)
284 if (GNUNET_YES == watch)
286 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
288 "%24s%s %s%s%12s%s %s%50s%s%s %16llu\n",
291 is_persistent ? "!" : " ",
295 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
297 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
298 (0 == strlen (csv_separator) ? ":": csv_separator),
299 (unsigned long long) value);
304 "%s%s%12s%s %s%50s%s%s %16llu\n",
305 is_persistent ? "!" : " ",
309 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
311 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
312 (0 == strlen (csv_separator) ? ":": csv_separator),
313 (unsigned long long) value);
319 (unsigned long long) value);
325 * @brief Clean all data structures related to given node.
327 * Also clears global structures if we are the last node to clean.
329 * @param cls the index of the node
332 clean_node (void *cls)
334 const unsigned index_node = *(unsigned *) cls;
335 struct GNUNET_STATISTICS_Handle *h;
336 struct GNUNET_STATISTICS_GetHandle *gh;
338 if ( (NULL != path_testbed) && /* were issued with -t <testbed-path> option */
339 (NULL != nodes[index_node].conf) )
341 GNUNET_CONFIGURATION_destroy (nodes[index_node].conf);
342 nodes[index_node].conf = NULL;
345 h = nodes[index_node].handle;
346 gh = nodes[index_node].gh;
350 GNUNET_STATISTICS_get_cancel (gh);
353 if (GNUNET_YES == watch)
355 GNUNET_assert (GNUNET_OK ==
356 GNUNET_STATISTICS_watch_cancel (h,
360 &nodes[index_node].index_node));
365 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
369 num_nodes_ready_shutdown++;
370 if (num_nodes == num_nodes_ready_shutdown)
372 GNUNET_array_grow (nodes, num_nodes, 0);
373 GNUNET_CONTAINER_multihashmap_destroy (values);
378 * @brief Print and shutdown
383 print_finish (void *cls)
385 GNUNET_CONTAINER_multihashmap_iterate (values, printer, NULL);
386 GNUNET_SCHEDULER_shutdown();
390 * @brief Called once all statistic values are available.
392 * Implements #GNUNET_STATISTICS_Callback
394 * @param cls Closure - The index of the node.
395 * @param succes Whether statistics were obtained successfully.
398 continuation_print (void *cls,
401 const unsigned index_node = *(unsigned *) cls;
403 nodes[index_node].gh = NULL;
404 if (GNUNET_OK != success)
406 if (NULL == remote_host)
409 _("Failed to obtain statistics.\n"));
412 _("Failed to obtain statistics from host `%s:%llu'\n"),
417 if (NULL != nodes[index_node].shutdown_task)
419 GNUNET_SCHEDULER_cancel (nodes[index_node].shutdown_task);
420 nodes[index_node].shutdown_task = NULL;
422 GNUNET_SCHEDULER_add_now (clean_node, &nodes[index_node].index_node);
424 if (num_nodes_ready == num_nodes)
426 GNUNET_SCHEDULER_add_now (print_finish, NULL);
431 * Function called last by the statistics code.
434 * @param success #GNUNET_OK if statistics were
435 * successfully obtained, #GNUNET_SYSERR if not.
441 for (unsigned i = 0; i < num_nodes; i++)
445 if (GNUNET_OK != success)
447 if (NULL == remote_host)
450 _("Failed to obtain statistics.\n"));
453 _("Failed to obtain statistics from host `%s:%llu'\n"),
458 GNUNET_SCHEDULER_shutdown ();
462 * @brief Iterate over statistics values and store them in #values.
463 * They will be printed once all are available.
465 * @param cls Cosure - Node index.
466 * @param subsystem Subsystem of the value.
467 * @param name Name of the value.
468 * @param value Value itself.
469 * @param is_persistent Persistence.
471 * @return GNUNET_OK - continue.
474 collector (void *cls,
475 const char *subsystem,
480 const unsigned index_node = *(unsigned *) cls;
481 struct GNUNET_HashCode *key;
482 struct GNUNET_HashCode hc;
484 unsigned len_subsys_name;
485 struct ValueSet *value_set;
487 len_subsys_name = strlen (subsystem) + 3 + strlen (name) + 1;
488 subsys_name = GNUNET_malloc (len_subsys_name);
489 SPRINTF (subsys_name, "%s---%s", subsystem, name);
491 GNUNET_CRYPTO_hash (subsys_name, len_subsys_name, key);
492 GNUNET_free (subsys_name);
493 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (values, key))
496 value_set = GNUNET_CONTAINER_multihashmap_get (values, key);
501 value_set = new_value_set (subsystem, name, num_nodes, is_persistent);
504 value_set->values[index_node] = value;
506 GNUNET_CONTAINER_multihashmap_put (values, key, value_set,
507 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
512 * Main task that does the actual work.
514 * @param cls closure with our configuration
517 main_task (void *cls)
519 unsigned index_node = *(unsigned *) cls;
520 const struct GNUNET_CONFIGURATION_Handle *cfg = nodes[index_node].conf;
524 if (NULL == subsystem)
528 _("Missing argument: subsystem \n"));
536 _("Missing argument: name\n"));
540 nodes[index_node].handle = GNUNET_STATISTICS_create (subsystem,
542 if (NULL == nodes[index_node].handle)
547 GNUNET_STATISTICS_set (nodes[index_node].handle,
551 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
553 nodes[index_node].handle = NULL;
556 if (NULL == (nodes[index_node].handle = GNUNET_STATISTICS_create ("gnunet-statistics",
562 if (GNUNET_NO == watch)
565 (nodes[index_node].gh = GNUNET_STATISTICS_get (nodes[index_node].handle,
570 &nodes[index_node].index_node)) )
571 cleanup (nodes[index_node].handle,
576 if ( (NULL == subsystem) ||
579 printf (_("No subsystem or name given\n"));
580 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
582 nodes[index_node].handle = NULL;
587 GNUNET_STATISTICS_watch (nodes[index_node].handle,
591 &nodes[index_node].index_node))
594 _("Failed to initialize watch routine\n"));
595 nodes[index_node].shutdown_task =
596 GNUNET_SCHEDULER_add_now (&clean_node,
597 &nodes[index_node].index_node);
601 nodes[index_node].shutdown_task =
602 GNUNET_SCHEDULER_add_shutdown (&clean_node,
603 &nodes[index_node].index_node);
607 * @brief Iter over content of a node's directory to check for existence of a
610 * Implements #GNUNET_FileNameCallback
612 * @param cls pointer to indicate success
613 * @param filename filename inside the directory of the potential node
615 * @return to continue iteration or not to
618 iter_check_config (void *cls,
619 const char *filename)
621 if (0 == strncmp (GNUNET_STRINGS_get_short_name (filename), "config", 6))
623 /* Found the config - stop iteration successfully */
624 GNUNET_array_grow (nodes, num_nodes, num_nodes+1);
625 nodes[num_nodes-1].conf = GNUNET_CONFIGURATION_create();
626 nodes[num_nodes-1].index_node = num_nodes-1;
627 if (GNUNET_OK != GNUNET_CONFIGURATION_load (nodes[num_nodes-1].conf, filename))
629 FPRINTF (stderr, "Failed loading config `%s'\n", filename);
630 return GNUNET_SYSERR;
636 /* Continue iteration */
642 * @brief Iterates over filenames in testbed directory.
644 * Implements #GNUNET_FileNameCallback
646 * Checks if the file is a directory for a testbed node
647 * and counts the nodes.
649 * @param cls counter of nodes
650 * @param filename full path of the file in testbed
652 * @return status whether to continue iteration
655 iter_testbed_path (void *cls,
656 const char *filename)
660 GNUNET_assert (NULL != filename);
661 if (1 == SSCANF (GNUNET_STRINGS_get_short_name (filename),
665 if (-1 == GNUNET_DISK_directory_scan (filename,
669 /* This is probably no directory for a testbed node
670 * Go on with iteration */
679 * @brief Count the number of nodes running in the testbed
681 * @param path_testbed path to the testbed data
683 * @return number of running nodes
686 discover_testbed_nodes (const char *path_testbed)
690 num_dir_entries = GNUNET_DISK_directory_scan (path_testbed,
693 if (-1 == num_dir_entries)
696 "Failure during scanning directory `%s'\n",
704 * Main function that will be run by the scheduler.
707 * @param args remaining command-line arguments
708 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
709 * @param cfg configuration
715 const struct GNUNET_CONFIGURATION_Handle *cfg)
717 struct GNUNET_CONFIGURATION_Handle *c;
719 c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
720 set_value = GNUNET_NO;
721 if (NULL == csv_separator) csv_separator = "";
724 if (1 != SSCANF (args[0],
729 _("Invalid argument `%s'\n"),
734 set_value = GNUNET_YES;
736 if (NULL != remote_host)
738 if (0 == remote_port)
741 GNUNET_CONFIGURATION_get_value_number (cfg,
747 _("A port is required to connect to host `%s'\n"),
752 else if (65535 <= remote_port)
755 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
760 /* Manipulate configuration */
761 GNUNET_CONFIGURATION_set_value_string (c,
765 GNUNET_CONFIGURATION_set_value_string (c,
769 GNUNET_CONFIGURATION_set_value_number (c,
774 if (NULL == path_testbed)
776 values = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
777 GNUNET_array_grow (nodes, num_nodes, 1);
778 nodes[0].index_node = 0;
780 GNUNET_SCHEDULER_add_now (&main_task, &nodes[0].index_node);
784 if (GNUNET_YES == watch)
786 printf (_("Not able to watch testbed nodes (yet - feel free to implement)\n"));
790 values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
791 if (-1 == discover_testbed_nodes (path_testbed))
795 /* For each config/node collect statistics */
796 for (unsigned i = 0; i < num_nodes; i++)
798 GNUNET_SCHEDULER_add_now (&main_task,
799 &nodes[i].index_node);
806 * The main function to obtain statistics in GNUnet.
808 * @param argc number of arguments from the command line
809 * @param argv command line arguments
810 * @return 0 ok, 1 on error
813 main (int argc, char *const *argv)
815 struct GNUNET_GETOPT_CommandLineOption options[] = {
816 GNUNET_GETOPT_option_string ('n',
819 gettext_noop ("limit output to statistics for the given NAME"),
822 GNUNET_GETOPT_option_flag ('p',
824 gettext_noop ("make the value being set persistent"),
827 GNUNET_GETOPT_option_string ('s',
830 gettext_noop ("limit output to the given SUBSYSTEM"),
833 GNUNET_GETOPT_option_string ('S',
836 gettext_noop ("use as csv separator"),
839 GNUNET_GETOPT_option_filename ('t',
842 gettext_noop ("path to the folder containing the testbed data"),
845 GNUNET_GETOPT_option_flag ('q',
847 gettext_noop ("just print the statistics value"),
850 GNUNET_GETOPT_option_flag ('w',
852 gettext_noop ("watch value continuously"),
855 GNUNET_GETOPT_option_string ('r',
858 gettext_noop ("connect to remote host"),
861 GNUNET_GETOPT_option_ulong ('o',
864 gettext_noop ("port for remote host"),
867 GNUNET_GETOPT_OPTION_END
872 GNUNET_STRINGS_get_utf8_args (argc, argv,
877 GNUNET_PROGRAM_run (argc,
879 "gnunet-statistics [options [value]]",
881 ("Print statistics about GNUnet operations."),
885 GNUNET_free_non_null (remote_host);
886 GNUNET_free ((void*) argv);
890 /* end of gnunet-statistics.c */