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 Create a new #ValueSet
164 * @param subsystem Subsystem of the valueset.
165 * @param name Name of the valueset.
166 * @param num_values Number of values in valueset - number of peers.
167 * @param is_persistent Persistence status of values.
169 * @return Newly allocated #ValueSet.
171 static struct ValueSet *
172 new_value_set (const char *subsystem,
177 struct ValueSet *value_set;
179 value_set = GNUNET_new (struct ValueSet);
180 value_set->subsystem = GNUNET_strdup (subsystem);
181 value_set->name = GNUNET_strdup (name);
182 value_set->values = GNUNET_new_array (num_values, uint64_t);
183 value_set->is_persistent = persistent;
188 * @brief Print the (collected) values.
190 * Implements #GNUNET_CONTAINER_HashMapIterator.
192 * @param cls Closure - unused
193 * @param key #GNUNET_HashCode key of #GNUNET_CONTAINER_MultiHashMap iterator -
195 * @param value Values represented as #ValueSet.
197 * @return GNUNET_YES - continue iteration.
201 const struct GNUNET_HashCode *key,
204 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
206 struct ValueSet *value_set = value;
208 if (quiet == GNUNET_NO)
210 if (GNUNET_YES == watch)
212 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
214 "%24s%s %s%s%12s%s %50s%s ",
217 value_set->is_persistent ? "!" : " ",
219 value_set->subsystem,
222 (0 == strlen (csv_separator) ? ":": csv_separator));
227 "%s%s%12s%s %50s%s ",
228 value_set->is_persistent ? "!" : " ",
230 value_set->subsystem,
233 (0 == strlen (csv_separator) ? ":": csv_separator));
236 for (unsigned i = 0; i < num_nodes; i++)
240 (unsigned long long) value_set->values[i],
243 FPRINTF (stdout, "\n");
244 GNUNET_free (value_set->subsystem);
245 GNUNET_free (value_set->name);
246 GNUNET_free (value_set->values);
247 GNUNET_free (value_set);
252 * @brief Called once all statistic values are available.
254 * Implements #GNUNET_STATISTICS_Callback
256 * @param cls Closure - The index of the node.
257 * @param succes Whether statistics were obtained successfully.
260 continuation_print (void *cls,
263 const unsigned index_node = *(unsigned *) cls;
265 nodes[index_node].gh = NULL;
266 if (GNUNET_OK != success)
268 if (NULL == remote_host)
271 _("Failed to obtain statistics.\n"));
274 _("Failed to obtain statistics from host `%s:%llu'\n"),
280 if (num_nodes_ready == num_nodes)
282 GNUNET_CONTAINER_multihashmap_iterate (values, printer, NULL);
283 GNUNET_SCHEDULER_shutdown();
288 * Callback function to process statistic values.
291 * @param subsystem name of subsystem that created the statistic
292 * @param name the name of the datum
293 * @param value the current value
294 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
295 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
298 printer_watch (void *cls,
299 const char *subsystem,
304 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
307 if (quiet == GNUNET_NO)
309 if (GNUNET_YES == watch)
311 now_str = GNUNET_STRINGS_absolute_time_to_string (now);
313 "%24s%s %s%s%12s%s %50s%s %16llu\n",
316 is_persistent ? "!" : " ",
321 (0 == strlen (csv_separator) ? ":": csv_separator),
322 (unsigned long long) value);
327 "%s%s%12s%s %50s%s %16llu\n",
328 is_persistent ? "!" : " ",
333 (0 == strlen (csv_separator) ? ":": csv_separator),
334 (unsigned long long) value);
340 (unsigned long long) value);
346 * Function called last by the statistics code.
349 * @param success #GNUNET_OK if statistics were
350 * successfully obtained, #GNUNET_SYSERR if not.
356 for (unsigned i = 0; i < num_nodes; i++)
360 if (GNUNET_OK != success)
362 if (NULL == remote_host)
365 _("Failed to obtain statistics.\n"));
368 _("Failed to obtain statistics from host `%s:%llu'\n"),
373 GNUNET_SCHEDULER_shutdown ();
377 * @brief Iterate over statistics values and store them in #values.
378 * They will be printed once all are available.
380 * @param cls Cosure - Node index.
381 * @param subsystem Subsystem of the value.
382 * @param name Name of the value.
383 * @param value Value itself.
384 * @param is_persistent Persistence.
386 * @return GNUNET_OK - continue.
389 collector (void *cls,
390 const char *subsystem,
395 const unsigned index_node = *(unsigned *) cls;
396 struct GNUNET_HashCode *key;
397 struct GNUNET_HashCode hc;
399 unsigned len_subsys_name;
400 struct ValueSet *value_set;
402 len_subsys_name = strlen (subsystem) + 3 + strlen (name) + 1;
403 subsys_name = GNUNET_malloc (len_subsys_name);
404 SPRINTF (subsys_name, "%s---%s", subsystem, name);
406 GNUNET_CRYPTO_hash (subsys_name, len_subsys_name, key);
407 GNUNET_free (subsys_name);
408 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (values, key))
411 value_set = GNUNET_CONTAINER_multihashmap_get (values, key);
416 value_set = new_value_set (subsystem, name, num_nodes, is_persistent);
419 value_set->values[index_node] = value;
421 GNUNET_CONTAINER_multihashmap_put (values, key, value_set,
422 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
427 * Function run on shutdown to clean up.
429 * @param cls the statistics handle
432 shutdown_task (void *cls)
434 const unsigned index_node = *(unsigned *) cls;
435 struct GNUNET_STATISTICS_Handle *h;
436 struct GNUNET_STATISTICS_GetHandle *gh;
438 nodes[index_node].shutdown_task = NULL;
439 if ( (NULL != path_testbed) &&
440 (NULL != nodes[index_node].conf) )
442 GNUNET_CONFIGURATION_destroy (nodes[index_node].conf);
443 nodes[index_node].conf = NULL;
446 h = nodes[index_node].handle;
447 gh = nodes[index_node].gh;
451 if (0 == num_nodes_ready)
453 GNUNET_array_grow (nodes, num_nodes, 0);
454 GNUNET_CONTAINER_multihashmap_destroy (values);
460 GNUNET_STATISTICS_get_cancel (gh);
463 if ( (GNUNET_YES == watch) &&
464 (NULL != subsystem) &&
466 GNUNET_assert (GNUNET_OK ==
467 GNUNET_STATISTICS_watch_cancel (h,
471 &nodes[index_node].index_node));
472 GNUNET_STATISTICS_destroy (h,
477 if (0 == num_nodes_ready)
479 GNUNET_array_grow (nodes, num_nodes, 0);
480 GNUNET_CONTAINER_multihashmap_destroy (values);
486 * Main task that does the actual work.
488 * @param cls closure with our configuration
491 main_task (void *cls)
493 unsigned index_node = *(unsigned *) cls;
494 const struct GNUNET_CONFIGURATION_Handle *cfg = nodes[index_node].conf;
498 if (NULL == subsystem)
502 _("Missing argument: subsystem \n"));
510 _("Missing argument: name\n"));
514 nodes[index_node].handle = GNUNET_STATISTICS_create (subsystem,
516 if (NULL == nodes[index_node].handle)
521 GNUNET_STATISTICS_set (nodes[index_node].handle,
525 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
527 nodes[index_node].handle = NULL;
530 if (NULL == (nodes[index_node].handle = GNUNET_STATISTICS_create ("gnunet-statistics",
536 if (GNUNET_NO == watch)
539 (nodes[index_node].gh = GNUNET_STATISTICS_get (nodes[index_node].handle,
544 &nodes[index_node].index_node)) )
545 cleanup (nodes[index_node].handle,
550 if ( (NULL == subsystem) ||
553 printf (_("No subsystem or name given\n"));
554 GNUNET_STATISTICS_destroy (nodes[index_node].handle,
556 nodes[index_node].handle = NULL;
561 GNUNET_STATISTICS_watch (nodes[index_node].handle,
565 &nodes[index_node].index_node))
568 _("Failed to initialize watch routine\n"));
569 nodes[index_node].shutdown_task =
570 GNUNET_SCHEDULER_add_now (&shutdown_task,
571 &nodes[index_node].index_node);
575 nodes[index_node].shutdown_task =
576 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
577 &nodes[index_node].index_node);
581 * @brief Iter over content of a node's directory to check for existence of a
584 * Implements #GNUNET_FileNameCallback
586 * @param cls pointer to indicate success
587 * @param filename filename inside the directory of the potential node
589 * @return to continue iteration or not to
592 iter_check_config (void *cls,
593 const char *filename)
595 if (0 == strncmp (GNUNET_STRINGS_get_short_name (filename), "config", 6))
597 /* Found the config - stop iteration successfully */
598 GNUNET_array_grow (nodes, num_nodes, num_nodes+1);
599 nodes[num_nodes-1].conf = GNUNET_CONFIGURATION_create();
600 nodes[num_nodes-1].index_node = num_nodes-1;
601 if (GNUNET_OK != GNUNET_CONFIGURATION_load (nodes[num_nodes-1].conf, filename))
603 FPRINTF (stderr, "Failed loading config `%s'\n", filename);
604 return GNUNET_SYSERR;
610 /* Continue iteration */
616 * @brief Iterates over filenames in testbed directory.
618 * Implements #GNUNET_FileNameCallback
620 * Checks if the file is a directory for a testbed node
621 * and counts the nodes.
623 * @param cls counter of nodes
624 * @param filename full path of the file in testbed
626 * @return status whether to continue iteration
629 iter_testbed_path (void *cls,
630 const char *filename)
634 GNUNET_assert (NULL != filename);
635 if (1 == SSCANF (GNUNET_STRINGS_get_short_name (filename),
639 if (-1 == GNUNET_DISK_directory_scan (filename,
643 /* This is probably no directory for a testbed node
644 * Go on with iteration */
653 * @brief Count the number of nodes running in the testbed
655 * @param path_testbed path to the testbed data
657 * @return number of running nodes
660 discover_testbed_nodes (const char *path_testbed)
664 num_dir_entries = GNUNET_DISK_directory_scan (path_testbed,
667 if (-1 == num_dir_entries)
670 "Failure during scanning directory `%s'\n",
678 * Main function that will be run by the scheduler.
681 * @param args remaining command-line arguments
682 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
683 * @param cfg configuration
689 const struct GNUNET_CONFIGURATION_Handle *cfg)
691 struct GNUNET_CONFIGURATION_Handle *c;
693 c = (struct GNUNET_CONFIGURATION_Handle *) cfg;
694 set_value = GNUNET_NO;
695 if (NULL == csv_separator) csv_separator = "";
698 if (1 != SSCANF (args[0],
703 _("Invalid argument `%s'\n"),
708 set_value = GNUNET_YES;
710 if (NULL != remote_host)
712 if (0 == remote_port)
715 GNUNET_CONFIGURATION_get_value_number (cfg,
721 _("A port is required to connect to host `%s'\n"),
726 else if (65535 <= remote_port)
729 _("A port has to be between 1 and 65535 to connect to host `%s'\n"),
734 /* Manipulate configuration */
735 GNUNET_CONFIGURATION_set_value_string (c,
739 GNUNET_CONFIGURATION_set_value_string (c,
743 GNUNET_CONFIGURATION_set_value_number (c,
748 if (NULL == path_testbed)
750 values = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
751 GNUNET_array_grow (nodes, num_nodes, 1);
752 nodes[0].index_node = 0;
754 GNUNET_SCHEDULER_add_now (&main_task, &nodes[0].index_node);
758 if (GNUNET_YES == watch)
760 printf (_("Not able to watch testbed nodes (yet - feel free to implement)\n"));
764 values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
765 if (-1 == discover_testbed_nodes (path_testbed))
769 /* For each config/node collect statistics */
770 for (unsigned i = 0; i < num_nodes; i++)
772 GNUNET_SCHEDULER_add_now (&main_task,
773 &nodes[i].index_node);
780 * The main function to obtain statistics in GNUnet.
782 * @param argc number of arguments from the command line
783 * @param argv command line arguments
784 * @return 0 ok, 1 on error
787 main (int argc, char *const *argv)
789 struct GNUNET_GETOPT_CommandLineOption options[] = {
790 GNUNET_GETOPT_option_string ('n',
793 gettext_noop ("limit output to statistics for the given NAME"),
796 GNUNET_GETOPT_option_flag ('p',
798 gettext_noop ("make the value being set persistent"),
801 GNUNET_GETOPT_option_string ('s',
804 gettext_noop ("limit output to the given SUBSYSTEM"),
807 GNUNET_GETOPT_option_string ('S',
810 gettext_noop ("use as csv separator"),
813 GNUNET_GETOPT_option_filename ('t',
816 gettext_noop ("path to the folder containing the testbed data"),
819 GNUNET_GETOPT_option_flag ('q',
821 gettext_noop ("just print the statistics value"),
824 GNUNET_GETOPT_option_flag ('w',
826 gettext_noop ("watch value continuously"),
829 GNUNET_GETOPT_option_string ('r',
832 gettext_noop ("connect to remote host"),
835 GNUNET_GETOPT_option_ulong ('o',
838 gettext_noop ("port for remote host"),
841 GNUNET_GETOPT_OPTION_END
846 GNUNET_STRINGS_get_utf8_args (argc, argv,
851 GNUNET_PROGRAM_run (argc,
853 "gnunet-statistics [options [value]]",
855 ("Print statistics about GNUnet operations."),
859 GNUNET_free_non_null (remote_host);
860 GNUNET_free ((void*) argv);
864 /* end of gnunet-statistics.c */