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 %s%50s%s%s ",
222 value_set->is_persistent ? "!" : " ",
224 value_set->subsystem,
226 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
228 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
229 (0 == strlen (csv_separator) ? ":": csv_separator));
234 "%s%s%12s%s %s%50s%s%s ",
235 value_set->is_persistent ? "!" : " ",
237 value_set->subsystem,
239 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
241 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
242 (0 == strlen (csv_separator) ? ":": csv_separator));
245 for (unsigned i = 0; i < num_nodes; i++)
249 (unsigned long long) value_set->values[i],
252 FPRINTF (stdout, "\n");
253 GNUNET_free (value_set->subsystem);
254 GNUNET_free (value_set->name);
255 GNUNET_free (value_set->values);
256 GNUNET_free (value_set);
261 * Callback function to process statistic values.
264 * @param subsystem name of subsystem that created the statistic
265 * @param name the name of the datum
266 * @param value the current value
267 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
268 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
271 printer_watch (void *cls,
272 const char *subsystem,
277 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
280 if (quiet == GNUNET_NO)
282 if (GNUNET_YES == watch)
284 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
286 "%24s%s %s%s%12s%s %s%50s%s%s %16llu\n",
289 is_persistent ? "!" : " ",
293 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
295 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
296 (0 == strlen (csv_separator) ? ":": csv_separator),
297 (unsigned long long) value);
302 "%s%s%12s%s %s%50s%s%s %16llu\n",
303 is_persistent ? "!" : " ",
307 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
309 (0 == strlen (csv_separator) ? "": "\""), /* quotes if csv */
310 (0 == strlen (csv_separator) ? ":": csv_separator),
311 (unsigned long long) value);
317 (unsigned long long) value);
323 * @brief Clean all data structures related to given node.
325 * Also clears global structures if we are the last node to clean.
327 * @param cls the index of the node
330 clean_node (void *cls)
332 const unsigned index_node = *(unsigned *) cls;
333 struct GNUNET_STATISTICS_Handle *h;
334 struct GNUNET_STATISTICS_GetHandle *gh;
336 if ( (NULL != path_testbed) && /* were issued with -t <testbed-path> option */
337 (NULL != nodes[index_node].conf) )
339 GNUNET_CONFIGURATION_destroy (nodes[index_node].conf);
340 nodes[index_node].conf = NULL;
343 h = nodes[index_node].handle;
344 gh = nodes[index_node].gh;
348 GNUNET_STATISTICS_get_cancel (gh);
351 if (GNUNET_YES == watch)
353 GNUNET_assert (GNUNET_OK ==
354 GNUNET_STATISTICS_watch_cancel (h,
358 &nodes[index_node].index_node));
363 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
367 num_nodes_ready_shutdown++;
368 if (num_nodes == num_nodes_ready_shutdown)
370 GNUNET_array_grow (nodes, num_nodes, 0);
371 GNUNET_CONTAINER_multihashmap_destroy (values);
376 * @brief Print and shutdown
381 print_finish (void *cls)
383 GNUNET_CONTAINER_multihashmap_iterate (values, printer, NULL);
384 GNUNET_SCHEDULER_shutdown();
388 * @brief Called once all statistic values are available.
390 * Implements #GNUNET_STATISTICS_Callback
392 * @param cls Closure - The index of the node.
393 * @param succes Whether statistics were obtained successfully.
396 continuation_print (void *cls,
399 const unsigned index_node = *(unsigned *) cls;
401 nodes[index_node].gh = NULL;
402 if (GNUNET_OK != success)
404 if (NULL == remote_host)
407 _("Failed to obtain statistics.\n"));
410 _("Failed to obtain statistics from host `%s:%llu'\n"),
415 if (NULL != nodes[index_node].shutdown_task)
417 GNUNET_SCHEDULER_cancel (nodes[index_node].shutdown_task);
418 nodes[index_node].shutdown_task = NULL;
420 GNUNET_SCHEDULER_add_now (clean_node, &nodes[index_node].index_node);
422 if (num_nodes_ready == num_nodes)
424 GNUNET_SCHEDULER_add_now (print_finish, NULL);
429 * Function called last by the statistics code.
432 * @param success #GNUNET_OK if statistics were
433 * successfully obtained, #GNUNET_SYSERR if not.
439 for (unsigned i = 0; i < num_nodes; i++)
443 if (GNUNET_OK != success)
445 if (NULL == remote_host)
448 _("Failed to obtain statistics.\n"));
451 _("Failed to obtain statistics from host `%s:%llu'\n"),
456 GNUNET_SCHEDULER_shutdown ();
460 * @brief Iterate over statistics values and store them in #values.
461 * They will be printed once all are available.
463 * @param cls Cosure - Node index.
464 * @param subsystem Subsystem of the value.
465 * @param name Name of the value.
466 * @param value Value itself.
467 * @param is_persistent Persistence.
469 * @return GNUNET_OK - continue.
472 collector (void *cls,
473 const char *subsystem,
478 const unsigned index_node = *(unsigned *) cls;
479 struct GNUNET_HashCode *key;
480 struct GNUNET_HashCode hc;
482 unsigned len_subsys_name;
483 struct ValueSet *value_set;
485 len_subsys_name = strlen (subsystem) + 3 + strlen (name) + 1;
486 subsys_name = GNUNET_malloc (len_subsys_name);
487 SPRINTF (subsys_name, "%s---%s", subsystem, name);
489 GNUNET_CRYPTO_hash (subsys_name, len_subsys_name, key);
490 GNUNET_free (subsys_name);
491 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (values, key))
494 value_set = GNUNET_CONTAINER_multihashmap_get (values, key);
499 value_set = new_value_set (subsystem, name, num_nodes, is_persistent);
502 value_set->values[index_node] = value;
504 GNUNET_CONTAINER_multihashmap_put (values, key, value_set,
505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
510 * Main task that does the actual work.
512 * @param cls closure with our configuration
515 main_task (void *cls)
517 unsigned index_node = *(unsigned *) cls;
518 const struct GNUNET_CONFIGURATION_Handle *cfg = nodes[index_node].conf;
522 if (NULL == subsystem)
526 _("Missing argument: subsystem \n"));
534 _("Missing argument: name\n"));
538 nodes[index_node].handle = GNUNET_STATISTICS_create (subsystem,
540 if (NULL == nodes[index_node].handle)
545 GNUNET_STATISTICS_set (nodes[index_node].handle,
549 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
551 nodes[index_node].handle = NULL;
554 if (NULL == (nodes[index_node].handle = GNUNET_STATISTICS_create ("gnunet-statistics",
560 if (GNUNET_NO == watch)
563 (nodes[index_node].gh = GNUNET_STATISTICS_get (nodes[index_node].handle,
568 &nodes[index_node].index_node)) )
569 cleanup (nodes[index_node].handle,
574 if ( (NULL == subsystem) ||
577 printf (_("No subsystem or name given\n"));
578 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
580 nodes[index_node].handle = NULL;
585 GNUNET_STATISTICS_watch (nodes[index_node].handle,
589 &nodes[index_node].index_node))
592 _("Failed to initialize watch routine\n"));
593 nodes[index_node].shutdown_task =
594 GNUNET_SCHEDULER_add_now (&clean_node,
595 &nodes[index_node].index_node);
599 nodes[index_node].shutdown_task =
600 GNUNET_SCHEDULER_add_shutdown (&clean_node,
601 &nodes[index_node].index_node);
605 * @brief Iter over content of a node's directory to check for existence of a
608 * Implements #GNUNET_FileNameCallback
610 * @param cls pointer to indicate success
611 * @param filename filename inside the directory of the potential node
613 * @return to continue iteration or not to
616 iter_check_config (void *cls,
617 const char *filename)
619 if (0 == strncmp (GNUNET_STRINGS_get_short_name (filename), "config", 6))
621 /* Found the config - stop iteration successfully */
622 GNUNET_array_grow (nodes, num_nodes, num_nodes+1);
623 nodes[num_nodes-1].conf = GNUNET_CONFIGURATION_create();
624 nodes[num_nodes-1].index_node = num_nodes-1;
625 if (GNUNET_OK != GNUNET_CONFIGURATION_load (nodes[num_nodes-1].conf, filename))
627 FPRINTF (stderr, "Failed loading config `%s'\n", filename);
628 return GNUNET_SYSERR;
634 /* Continue iteration */
640 * @brief Iterates over filenames in testbed directory.
642 * Implements #GNUNET_FileNameCallback
644 * Checks if the file is a directory for a testbed node
645 * and counts the nodes.
647 * @param cls counter of nodes
648 * @param filename full path of the file in testbed
650 * @return status whether to continue iteration
653 iter_testbed_path (void *cls,
654 const char *filename)
658 GNUNET_assert (NULL != filename);
659 if (1 == SSCANF (GNUNET_STRINGS_get_short_name (filename),
663 if (-1 == GNUNET_DISK_directory_scan (filename,
667 /* This is probably no directory for a testbed node
668 * Go on with iteration */
677 * @brief Count the number of nodes running in the testbed
679 * @param path_testbed path to the testbed data
681 * @return number of running nodes
684 discover_testbed_nodes (const char *path_testbed)
688 num_dir_entries = GNUNET_DISK_directory_scan (path_testbed,
691 if (-1 == num_dir_entries)
694 "Failure during scanning directory `%s'\n",
702 * Main function that will be run by the scheduler.
705 * @param args remaining command-line arguments
706 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
707 * @param cfg configuration
713 const struct GNUNET_CONFIGURATION_Handle *cfg)
715 struct GNUNET_CONFIGURATION_Handle *c;
717 c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
718 set_value = GNUNET_NO;
719 if (NULL == csv_separator) csv_separator = "";
722 if (1 != SSCANF (args[0],
727 _("Invalid argument `%s'\n"),
732 set_value = GNUNET_YES;
734 if (NULL != remote_host)
736 if (0 == remote_port)
739 GNUNET_CONFIGURATION_get_value_number (cfg,
745 _("A port is required to connect to host `%s'\n"),
750 else if (65535 <= remote_port)
753 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
758 /* Manipulate configuration */
759 GNUNET_CONFIGURATION_set_value_string (c,
763 GNUNET_CONFIGURATION_set_value_string (c,
767 GNUNET_CONFIGURATION_set_value_number (c,
772 if (NULL == path_testbed)
774 values = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
775 GNUNET_array_grow (nodes, num_nodes, 1);
776 nodes[0].index_node = 0;
778 GNUNET_SCHEDULER_add_now (&main_task, &nodes[0].index_node);
782 if (GNUNET_YES == watch)
784 printf (_("Not able to watch testbed nodes (yet - feel free to implement)\n"));
788 values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
789 if (-1 == discover_testbed_nodes (path_testbed))
793 /* For each config/node collect statistics */
794 for (unsigned i = 0; i < num_nodes; i++)
796 GNUNET_SCHEDULER_add_now (&main_task,
797 &nodes[i].index_node);
804 * The main function to obtain statistics in GNUnet.
806 * @param argc number of arguments from the command line
807 * @param argv command line arguments
808 * @return 0 ok, 1 on error
811 main (int argc, char *const *argv)
813 struct GNUNET_GETOPT_CommandLineOption options[] = {
814 GNUNET_GETOPT_option_string ('n',
817 gettext_noop ("limit output to statistics for the given NAME"),
820 GNUNET_GETOPT_option_flag ('p',
822 gettext_noop ("make the value being set persistent"),
825 GNUNET_GETOPT_option_string ('s',
828 gettext_noop ("limit output to the given SUBSYSTEM"),
831 GNUNET_GETOPT_option_string ('S',
834 gettext_noop ("use as csv separator"),
837 GNUNET_GETOPT_option_filename ('t',
840 gettext_noop ("path to the folder containing the testbed data"),
843 GNUNET_GETOPT_option_flag ('q',
845 gettext_noop ("just print the statistics value"),
848 GNUNET_GETOPT_option_flag ('w',
850 gettext_noop ("watch value continuously"),
853 GNUNET_GETOPT_option_string ('r',
856 gettext_noop ("connect to remote host"),
859 GNUNET_GETOPT_option_ulong ('o',
862 gettext_noop ("port for remote host"),
865 GNUNET_GETOPT_OPTION_END
870 GNUNET_STRINGS_get_utf8_args (argc, argv,
875 GNUNET_PROGRAM_run (argc,
877 "gnunet-statistics [options [value]]",
879 ("Print statistics about GNUnet operations."),
883 GNUNET_free_non_null (remote_host);
884 GNUNET_free ((void*) argv);
888 /* end of gnunet-statistics.c */