2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file nse/gnunet-nse-profiler.c
23 * @brief Profiling driver for the network size estimation service.
24 * Generally, the profiler starts a given number of peers,
25 * then churns some off, waits a certain amount of time, then
26 * churns again, and repeats.
27 * @author Christian Grothoff
28 * @author Nathan Evans
29 * @author Sree Harsha Totakura
33 #include "gnunet_testbed_service.h"
34 #include "gnunet_nse_service.h"
37 * Generic loggins shorthand
39 #define LOG(kind,...) \
40 GNUNET_log (kind, __VA_ARGS__)
43 * Debug logging shorthand
45 #define LOG_DEBUG(...) \
46 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
50 * Information we track for a peer in the testbed.
55 * Prev reference in DLL.
60 * Next reference in DLL.
65 * Handle with testbed.
67 struct GNUNET_TESTBED_Peer *daemon;
70 * Testbed operation to connect to NSE service.
72 struct GNUNET_TESTBED_Operation *nse_op;
75 * Testbed operation to connect to statistics service
77 struct GNUNET_TESTBED_Operation *stat_op;
80 * Handle to the statistics service
82 struct GNUNET_STATISTICS_Handle *sh;
87 * Context for the stats task?
93 * How many messages have peers received during the test.
95 unsigned long long total_nse_received_messages;
98 * How many messages have peers send during the test (should be == received).
100 unsigned long long total_nse_transmitted_messages;
103 * How many messages have travelled an edge in both directions.
105 unsigned long long total_nse_cross;
108 * How many extra messages per edge (corrections) have been received.
110 unsigned long long total_nse_extra;
113 * How many messages have been discarded.
115 unsigned long long total_discarded;
120 * Operation map entry
127 struct OpListEntry *next;
132 struct OpListEntry *prev;
135 * The testbed operation
137 struct GNUNET_TESTBED_Operation *op;
143 * Head of DLL of peers we monitor closely.
145 static struct NSEPeer *peer_head;
148 * Tail of DLL of peers we monitor closely.
150 static struct NSEPeer *peer_tail;
153 * Return value from 'main' (0 == success)
158 * Be verbose (configuration option)
163 * Name of the file with the hosts to run the test over (configuration option)
165 static char *hosts_file;
168 * Maximum number of peers in the test.
170 static unsigned int num_peers;
173 * Total number of rounds to execute.
175 static unsigned int num_rounds;
178 * Current round we are in.
180 static unsigned int current_round;
183 * Array of size 'num_rounds' with the requested number of peers in the given round.
185 static unsigned int *num_peers_in_round;
188 * How many peers are running right now?
190 static unsigned int peers_running;
193 * Specification for the numbers of peers to have in each round.
195 static char *num_peer_spec;
198 * Handles to all of the running peers.
200 static struct GNUNET_TESTBED_Peer **daemons;
203 * Global configuration file
205 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
210 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
213 * Maximum number of connections to NSE services.
215 static unsigned int connection_limit;
218 * Total number of connections in the whole network.
220 static unsigned int total_connections;
223 * File to report results to.
225 static struct GNUNET_DISK_FileHandle *output_file;
228 * Filename to log results to.
230 static char *output_filename;
233 * File to log connection info, statistics to.
235 static struct GNUNET_DISK_FileHandle *data_file;
238 * Filename to log connection info, statistics to.
240 static char *data_filename;
243 * How long to wait before triggering next round?
246 static struct GNUNET_TIME_Relative wait_time = { 60 * 1000 };
249 * DLL head for operation list
251 static struct OpListEntry *oplist_head;
254 * DLL tail for operation list
256 static struct OpListEntry *oplist_tail;
259 * Are we shutting down
261 static int shutting_down;
265 * Clean up all of the monitoring connections to NSE and
266 * STATISTICS that we keep to selected peers.
269 close_monitor_connections ()
272 struct OpListEntry *oplist_entry;
274 while (NULL != (pos = peer_head))
276 if (NULL != pos->nse_op)
277 GNUNET_TESTBED_operation_done (pos->nse_op);
278 if (NULL != pos->stat_op)
279 GNUNET_TESTBED_operation_done (pos->stat_op);
280 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
283 while (NULL != (oplist_entry = oplist_head))
285 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, oplist_entry);
286 GNUNET_TESTBED_operation_done (oplist_entry->op);
287 GNUNET_free (oplist_entry);
293 * Task run on shutdown; cleans up everything.
299 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
301 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
302 if (GNUNET_YES == shutting_down)
304 shutting_down = GNUNET_YES;
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
306 close_monitor_connections ();
307 if (NULL != data_file)
309 GNUNET_DISK_file_close (data_file);
312 if (NULL != output_file)
314 GNUNET_DISK_file_close (output_file);
317 if (NULL != testing_cfg)
318 GNUNET_CONFIGURATION_destroy (testing_cfg);
324 * Schedules shutdown task to be run now
329 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task_id)
330 GNUNET_SCHEDULER_cancel (shutdown_task_id);
331 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
336 * Callback to call when network size estimate is updated.
338 * @param cls closure with the 'struct NSEPeer' providing the update
339 * @param timestamp server timestamp
340 * @param estimate the value of the current network size estimate
341 * @param std_dev standard deviation (rounded down to nearest integer)
342 * of the size estimation values seen
345 handle_estimate (void *cls,
346 struct GNUNET_TIME_Absolute timestamp,
347 double estimate, double std_dev)
349 struct NSEPeer *peer = cls;
350 char output_buffer[512];
353 if (NULL == output_file)
356 "Received network size estimate from peer %p. Size: %f std.dev. %f\n",
357 peer, estimate, std_dev);
360 size = GNUNET_snprintf (output_buffer,
361 sizeof (output_buffer),
362 "%p %llu %llu %f %f %f\n",
365 GNUNET_NSE_log_estimate_to_n (estimate), estimate,
367 if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
369 "Unable to write to file!\n");
374 * Adapter function called to establish a connection to
377 * @param cls closure (the 'struct NSEPeer')
378 * @param cfg configuration of the peer to connect to; will be available until
379 * GNUNET_TESTBED_operation_done() is called on the operation returned
380 * from GNUNET_TESTBED_service_connect()
381 * @return service handle to return in 'op_result', NULL on error
384 nse_connect_adapter (void *cls,
385 const struct GNUNET_CONFIGURATION_Handle *cfg)
387 struct NSEPeer *current_peer = cls;
389 return GNUNET_NSE_connect (cfg, &handle_estimate, current_peer);
394 * Adapter function called to destroy a connection to
398 * @param op_result service handle returned from the connect adapter
401 nse_disconnect_adapter (void *cls,
404 GNUNET_NSE_disconnect (op_result);
409 * Callback function to process statistic values.
411 * @param cls struct StatsContext
412 * @param peer the peer the statistics belong to
413 * @param subsystem name of subsystem that created the statistic
414 * @param name the name of the datum
415 * @param value the current value
416 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
417 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
420 stat_iterator (void *cls, const char *subsystem, const char *name,
421 uint64_t value, int is_persistent)
424 struct GNUNET_TIME_Absolute now;
428 GNUNET_assert (NULL != data_file);
429 now = GNUNET_TIME_absolute_get ();
430 flag = strcasecmp (subsystem, "core");
433 size = GNUNET_asprintf (&output_buffer, "%llu %llu %u\n",
434 now.abs_value/1000, value, flag);
435 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
438 return GNUNET_SYSERR;
445 * Called to open a connection to the peer's statistics
447 * @param cls peer context
448 * @param cfg configuration of the peer to connect to; will be available until
449 * GNUNET_TESTBED_operation_done() is called on the operation returned
450 * from GNUNET_TESTBED_service_connect()
451 * @return service handle to return in 'op_result', NULL on error
454 stat_connect_adapter (void *cls,
455 const struct GNUNET_CONFIGURATION_Handle *cfg)
457 struct NSEPeer *peer = cls;
459 peer->sh = GNUNET_STATISTICS_create ("nse-profiler", cfg);
465 * Called to disconnect from peer's statistics service
467 * @param cls peer context
468 * @param op_result service handle returned from the connect adapter
471 stat_disconnect_adapter (void *cls, void *op_result)
473 struct NSEPeer *peer = cls;
475 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
476 (peer->sh, "core", "# peers connected",
477 stat_iterator, peer));
478 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
479 (peer->sh, "nse", "# peers connected",
480 stat_iterator, peer));
481 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
487 * Called after successfully opening a connection to a peer's statistics
488 * service; we register statistics monitoring for CORE and NSE here.
490 * @param cls the callback closure from functions generating an operation
491 * @param op the operation that has been finished
492 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
493 * @param emsg error message in case the operation has failed; will be NULL if
494 * operation has executed successfully.
497 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
498 void *ca_result, const char *emsg )
500 struct GNUNET_STATISTICS_Handle *sh = ca_result;
501 struct NSEPeer *peer = cls;
508 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
509 (sh, "core", "# peers connected",
510 stat_iterator, peer));
511 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
512 (sh, "nse", "# peers connected",
513 stat_iterator, peer));
518 * Task run to connect to the NSE and statistics services to a subset of
519 * all of the running peers.
522 connect_nse_service ()
524 struct NSEPeer *current_peer;
526 unsigned int connections;
528 if (0 == connection_limit)
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
532 for (i = 0; i < num_peers_in_round[current_round]; i++)
534 if ((num_peers_in_round[current_round] > connection_limit) &&
535 (0 != (i % (num_peers_in_round[current_round] / connection_limit))))
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "nse-profiler: connecting to nse service of peer %d\n", i);
539 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
540 current_peer->daemon = daemons[i];
542 = GNUNET_TESTBED_service_connect (NULL,
543 current_peer->daemon,
546 &nse_connect_adapter,
547 &nse_disconnect_adapter,
549 if (NULL != data_file)
550 current_peer->stat_op
551 = GNUNET_TESTBED_service_connect (NULL,
552 current_peer->daemon,
556 &stat_connect_adapter,
557 &stat_disconnect_adapter,
559 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
560 if (++connections == connection_limit)
567 * Task that starts/stops peers to move to the next round.
569 * @param cls NULL, unused
570 * @param tc scheduler context (unused)
573 next_round (void *cls,
574 const struct GNUNET_SCHEDULER_TaskContext *tc);
578 * We're at the end of a round. Stop monitoring, write total
579 * number of connections to log and get full stats. Then trigger
582 * @param cls unused, NULL
586 finish_round (void *cls,
587 const struct GNUNET_SCHEDULER_TaskContext *tc)
589 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
591 LOG (GNUNET_ERROR_TYPE_INFO, "Have %u connections\n", total_connections);
592 close_monitor_connections ();
593 GNUNET_SCHEDULER_add_now (&next_round, NULL);
598 * We have reached the desired number of peers for the current round.
599 * Run it (by connecting and monitoring a few peers and waiting the
600 * specified delay before finishing the round).
605 LOG_DEBUG ("Running round %u\n", current_round);
606 connect_nse_service ();
607 GNUNET_SCHEDULER_add_delayed (wait_time,
614 * Creates an oplist entry and adds it to the oplist DLL
616 static struct OpListEntry *
619 struct OpListEntry *entry;
621 entry = GNUNET_malloc (sizeof (struct OpListEntry));
622 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
628 * Functions of this signature are called when a peer has been successfully
629 * started or stopped.
632 * @param emsg NULL on success; otherwise an error description
635 peer_churn_cb (void *cls, const char *emsg)
637 struct OpListEntry *entry = cls;
639 GNUNET_TESTBED_operation_done (entry->op);
640 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
642 if (num_peers_in_round[current_round] == peers_running)
648 * Adjust the number of running peers to match the required number of running
649 * peers for the round
652 adjust_running_peers ()
654 struct OpListEntry *entry;
657 /* start peers if we have too few */
658 for (i=peers_running;i<num_peers_in_round[current_round];i++)
660 entry = make_oplist_entry ();
661 entry->op = GNUNET_TESTBED_peer_start (NULL, daemons[i],
662 &peer_churn_cb, entry);
664 /* stop peers if we have too many */
665 for (i=num_peers_in_round[current_round];i<peers_running;i++)
667 entry = make_oplist_entry ();
668 entry->op = GNUNET_TESTBED_peer_stop (NULL, daemons[i],
669 &peer_churn_cb, entry);
675 * Task run at the end of a round. Disconnect from all monitored
676 * peers; then get statistics from *all* peers.
678 * @param cls NULL, unused
682 next_round (void *cls,
683 const struct GNUNET_SCHEDULER_TaskContext *tc)
685 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
689 if (current_round == num_rounds)
691 /* this was the last round, terminate */
693 GNUNET_SCHEDULER_shutdown ();
696 if (num_peers_in_round[current_round] == peers_running)
698 /* no need to churn, just run next round */
702 adjust_running_peers ();
707 * Function that will be called whenever something in the
710 * @param cls closure, NULL
711 * @param event information on what is happening
714 master_controller_cb (void *cls,
715 const struct GNUNET_TESTBED_EventInformation *event)
719 case GNUNET_TESTBED_ET_PEER_START:
722 case GNUNET_TESTBED_ET_PEER_STOP:
725 case GNUNET_TESTBED_ET_CONNECT:
728 case GNUNET_TESTBED_ET_DISCONNECT:
738 * Signature of a main function for a testcase.
741 * @param num_peers_ number of peers in 'peers'
742 * @param peers handle to peers run in the testbed. NULL upon timeout (see
743 * GNUNET_TESTBED_test_run()).
744 * @param links_succeeded the number of overlay link connection attempts that
746 * @param links_failed the number of overlay link connection attempts that
750 test_master (void *cls,
751 unsigned int num_peers_,
752 struct GNUNET_TESTBED_Peer **peers,
753 unsigned int links_succeeded,
754 unsigned int links_failed)
762 GNUNET_break (num_peers_ == num_peers);
763 peers_running = num_peers;
764 if (num_peers_in_round[current_round] == peers_running)
766 /* no need to churn, just run the starting round */
770 adjust_running_peers ();
775 * Actual main function that runs the emulation.
778 * @param args remaining args, unused
779 * @param cfgfile name of the configuration
780 * @param cfg configuration handle
783 run (void *cls, char *const *args, const char *cfgfile,
784 const struct GNUNET_CONFIGURATION_Handle *cfg)
791 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
793 if (NULL == num_peer_spec)
795 fprintf (stderr, "You need to specify the number of peers to run\n");
798 for (tok = strtok (num_peer_spec, ","); NULL != tok; tok = strtok (NULL, ","))
800 if (1 != sscanf (tok, "%u", &num))
802 fprintf (stderr, "You need to specify numbers, not `%s'\n", tok);
807 fprintf (stderr, "Refusing to run a round with 0 peers\n");
810 GNUNET_array_append (num_peers_in_round, num_rounds, num);
811 num_peers = GNUNET_MAX (num_peers, num);
815 fprintf (stderr, "Refusing to run a testbed with no rounds\n");
818 if ( (NULL != data_filename) &&
819 (NULL == (data_file =
820 GNUNET_DISK_file_open (data_filename,
821 GNUNET_DISK_OPEN_READWRITE |
822 GNUNET_DISK_OPEN_TRUNCATE |
823 GNUNET_DISK_OPEN_CREATE,
824 GNUNET_DISK_PERM_USER_READ |
825 GNUNET_DISK_PERM_USER_WRITE))) )
826 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
830 if ( (NULL != output_filename) &&
831 (NULL == (output_file =
832 GNUNET_DISK_file_open (output_filename,
833 GNUNET_DISK_OPEN_READWRITE |
834 GNUNET_DISK_OPEN_CREATE,
835 GNUNET_DISK_PERM_USER_READ |
836 GNUNET_DISK_PERM_USER_WRITE))) )
837 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
840 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
841 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
842 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
843 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
844 GNUNET_TESTBED_run (hosts_file,
848 master_controller_cb,
849 NULL, /* master_controller_cb cls */
851 NULL); /* test_master cls */
853 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
854 &shutdown_task, NULL);
861 * @return 0 on success
864 main (int argc, char *const *argv)
866 static struct GNUNET_GETOPT_CommandLineOption options[] = {
867 {'C', "connections", "COUNT",
868 gettext_noop ("limit to the number of connections to NSE services, 0 for none"),
869 1, &GNUNET_GETOPT_set_uint, &connection_limit},
870 {'d', "details", "FILENAME",
871 gettext_noop ("name of the file for writing connection information and statistics"),
872 1, &GNUNET_GETOPT_set_string, &data_filename},
873 {'H', "hosts", "FILENAME",
874 gettext_noop ("name of the file with the login information for the testbed"),
875 1, &GNUNET_GETOPT_set_string, &hosts_file},
876 {'o', "output", "FILENAME",
877 gettext_noop ("name of the file for writing the main results"),
878 1, &GNUNET_GETOPT_set_string, &output_filename},
879 {'p', "peers", "NETWORKSIZESPEC",
880 gettext_noop ("Number of peers to run in each round, separated by commas"),
881 1, &GNUNET_GETOPT_set_string, &num_peer_spec},
882 {'V', "verbose", NULL,
883 gettext_noop ("be verbose (print progress information)"),
884 0, &GNUNET_GETOPT_increment_value, &verbose},
885 {'w', "wait", "DELAY",
886 gettext_noop ("delay between rounds"),
887 1, &GNUNET_GETOPT_set_relative_time, &wait_time},
888 GNUNET_GETOPT_OPTION_END
890 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
893 GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
895 ("Measure quality and performance of the NSE service."),
896 options, &run, NULL))
901 /* end of nse-profiler.c */