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;
140 * Depending on whether we start or stop NSE service at the peer set this to 1
148 * Head of DLL of peers we monitor closely.
150 static struct NSEPeer *peer_head;
153 * Tail of DLL of peers we monitor closely.
155 static struct NSEPeer *peer_tail;
158 * Return value from 'main' (0 == success)
163 * Be verbose (configuration option)
168 * Name of the file with the hosts to run the test over (configuration option)
170 static char *hosts_file;
173 * Maximum number of peers in the test.
175 static unsigned int num_peers;
178 * Total number of rounds to execute.
180 static unsigned int num_rounds;
183 * Current round we are in.
185 static unsigned int current_round;
188 * Array of size 'num_rounds' with the requested number of peers in the given round.
190 static unsigned int *num_peers_in_round;
193 * How many peers are running right now?
195 static unsigned int peers_running;
198 * Specification for the numbers of peers to have in each round.
200 static char *num_peer_spec;
203 * Handles to all of the running peers.
205 static struct GNUNET_TESTBED_Peer **daemons;
208 * Global configuration file
210 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
215 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
218 * Maximum number of connections to NSE services.
220 static unsigned int connection_limit;
223 * Total number of connections in the whole network.
225 static unsigned int total_connections;
228 * File to report results to.
230 static struct GNUNET_DISK_FileHandle *output_file;
233 * Filename to log results to.
235 static char *output_filename;
238 * File to log connection info, statistics to.
240 static struct GNUNET_DISK_FileHandle *data_file;
243 * Filename to log connection info, statistics to.
245 static char *data_filename;
248 * How long to wait before triggering next round?
251 static struct GNUNET_TIME_Relative wait_time = { 60 * 1000 };
254 * DLL head for operation list
256 static struct OpListEntry *oplist_head;
259 * DLL tail for operation list
261 static struct OpListEntry *oplist_tail;
264 * Are we shutting down
266 static int shutting_down;
270 * Clean up all of the monitoring connections to NSE and
271 * STATISTICS that we keep to selected peers.
274 close_monitor_connections ()
277 struct OpListEntry *oplist_entry;
279 while (NULL != (pos = peer_head))
281 if (NULL != pos->nse_op)
282 GNUNET_TESTBED_operation_done (pos->nse_op);
283 if (NULL != pos->stat_op)
284 GNUNET_TESTBED_operation_done (pos->stat_op);
285 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
288 while (NULL != (oplist_entry = oplist_head))
290 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, oplist_entry);
291 GNUNET_TESTBED_operation_done (oplist_entry->op);
292 GNUNET_free (oplist_entry);
298 * Task run on shutdown; cleans up everything.
304 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
306 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
307 if (GNUNET_YES == shutting_down)
309 shutting_down = GNUNET_YES;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
311 close_monitor_connections ();
312 if (NULL != data_file)
314 GNUNET_DISK_file_close (data_file);
317 if (NULL != output_file)
319 GNUNET_DISK_file_close (output_file);
322 if (NULL != testing_cfg)
323 GNUNET_CONFIGURATION_destroy (testing_cfg);
329 * Schedules shutdown task to be run now
334 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task_id)
335 GNUNET_SCHEDULER_cancel (shutdown_task_id);
336 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
341 * Callback to call when network size estimate is updated.
343 * @param cls closure with the 'struct NSEPeer' providing the update
344 * @param timestamp server timestamp
345 * @param estimate the value of the current network size estimate
346 * @param std_dev standard deviation (rounded down to nearest integer)
347 * of the size estimation values seen
350 handle_estimate (void *cls,
351 struct GNUNET_TIME_Absolute timestamp,
352 double estimate, double std_dev)
354 struct NSEPeer *peer = cls;
355 char output_buffer[512];
358 if (NULL == output_file)
361 "Received network size estimate from peer %p. Size: %f std.dev. %f\n",
362 peer, estimate, std_dev);
365 size = GNUNET_snprintf (output_buffer,
366 sizeof (output_buffer),
367 "%p %llu %llu %f %f %f\n",
370 GNUNET_NSE_log_estimate_to_n (estimate), estimate,
372 if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
374 "Unable to write to file!\n");
379 * Adapter function called to establish a connection to
382 * @param cls closure (the 'struct NSEPeer')
383 * @param cfg configuration of the peer to connect to; will be available until
384 * GNUNET_TESTBED_operation_done() is called on the operation returned
385 * from GNUNET_TESTBED_service_connect()
386 * @return service handle to return in 'op_result', NULL on error
389 nse_connect_adapter (void *cls,
390 const struct GNUNET_CONFIGURATION_Handle *cfg)
392 struct NSEPeer *current_peer = cls;
394 return GNUNET_NSE_connect (cfg, &handle_estimate, current_peer);
399 * Adapter function called to destroy a connection to
403 * @param op_result service handle returned from the connect adapter
406 nse_disconnect_adapter (void *cls,
409 GNUNET_NSE_disconnect (op_result);
414 * Callback function to process statistic values.
416 * @param cls struct StatsContext
417 * @param subsystem name of subsystem that created the statistic
418 * @param name the name of the datum
419 * @param value the current value
420 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
421 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
424 stat_iterator (void *cls, const char *subsystem, const char *name,
425 uint64_t value, int is_persistent)
428 struct GNUNET_TIME_Absolute now;
432 GNUNET_assert (NULL != data_file);
433 now = GNUNET_TIME_absolute_get ();
434 flag = strcasecmp (subsystem, "core");
437 size = GNUNET_asprintf (&output_buffer, "%llu %llu %u\n",
438 now.abs_value/1000, value, flag);
439 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
442 return GNUNET_SYSERR;
449 * Called to open a connection to the peer's statistics
451 * @param cls peer context
452 * @param cfg configuration of the peer to connect to; will be available until
453 * GNUNET_TESTBED_operation_done() is called on the operation returned
454 * from GNUNET_TESTBED_service_connect()
455 * @return service handle to return in 'op_result', NULL on error
458 stat_connect_adapter (void *cls,
459 const struct GNUNET_CONFIGURATION_Handle *cfg)
461 struct NSEPeer *peer = cls;
463 peer->sh = GNUNET_STATISTICS_create ("nse-profiler", cfg);
469 * Called to disconnect from peer's statistics service
471 * @param cls peer context
472 * @param op_result service handle returned from the connect adapter
475 stat_disconnect_adapter (void *cls, void *op_result)
477 struct NSEPeer *peer = cls;
479 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
480 (peer->sh, "core", "# peers connected",
481 stat_iterator, peer));
482 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
483 (peer->sh, "nse", "# peers connected",
484 stat_iterator, peer));
485 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
491 * Called after successfully opening a connection to a peer's statistics
492 * service; we register statistics monitoring for CORE and NSE here.
494 * @param cls the callback closure from functions generating an operation
495 * @param op the operation that has been finished
496 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
497 * @param emsg error message in case the operation has failed; will be NULL if
498 * operation has executed successfully.
501 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
502 void *ca_result, const char *emsg )
504 struct GNUNET_STATISTICS_Handle *sh = ca_result;
505 struct NSEPeer *peer = cls;
512 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
513 (sh, "core", "# peers connected",
514 stat_iterator, peer));
515 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
516 (sh, "nse", "# peers connected",
517 stat_iterator, peer));
522 * Task run to connect to the NSE and statistics services to a subset of
523 * all of the running peers.
526 connect_nse_service ()
528 struct NSEPeer *current_peer;
530 unsigned int connections;
532 if (0 == connection_limit)
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
536 for (i = 0; i < num_peers_in_round[current_round]; i++)
538 if ((num_peers_in_round[current_round] > connection_limit) &&
539 (0 != (i % (num_peers_in_round[current_round] / connection_limit))))
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "nse-profiler: connecting to nse service of peer %d\n", i);
543 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
544 current_peer->daemon = daemons[i];
546 = GNUNET_TESTBED_service_connect (NULL,
547 current_peer->daemon,
550 &nse_connect_adapter,
551 &nse_disconnect_adapter,
553 if (NULL != data_file)
554 current_peer->stat_op
555 = GNUNET_TESTBED_service_connect (NULL,
556 current_peer->daemon,
560 &stat_connect_adapter,
561 &stat_disconnect_adapter,
563 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
564 if (++connections == connection_limit)
571 * Task that starts/stops peers to move to the next round.
573 * @param cls NULL, unused
574 * @param tc scheduler context (unused)
577 next_round (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc);
582 * We're at the end of a round. Stop monitoring, write total
583 * number of connections to log and get full stats. Then trigger
586 * @param cls unused, NULL
590 finish_round (void *cls,
591 const struct GNUNET_SCHEDULER_TaskContext *tc)
593 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
595 LOG (GNUNET_ERROR_TYPE_INFO, "Have %u connections\n", total_connections);
596 close_monitor_connections ();
597 GNUNET_SCHEDULER_add_now (&next_round, NULL);
602 * We have reached the desired number of peers for the current round.
603 * Run it (by connecting and monitoring a few peers and waiting the
604 * specified delay before finishing the round).
609 LOG_DEBUG ("Running round %u\n", current_round);
610 connect_nse_service ();
611 GNUNET_SCHEDULER_add_delayed (wait_time,
618 * Creates an oplist entry and adds it to the oplist DLL
620 static struct OpListEntry *
623 struct OpListEntry *entry;
625 entry = GNUNET_malloc (sizeof (struct OpListEntry));
626 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
632 * Callback to be called when NSE service is started or stopped at peers
635 * @param op the operation handle
636 * @param emsg NULL on success; otherwise an error description
639 manage_service_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
642 struct OpListEntry *entry = cls;
644 GNUNET_TESTBED_operation_done (entry->op);
647 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop NSE at a peer\n");
648 GNUNET_SCHEDULER_shutdown ();
651 GNUNET_assert (0 != entry->delta);
652 peers_running += entry->delta;
653 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
655 if (num_peers_in_round[current_round] == peers_running)
661 * Adjust the number of running peers to match the required number of running
662 * peers for the round
665 adjust_running_peers ()
667 struct OpListEntry *entry;
670 /* start peers if we have too few */
671 for (i=peers_running;i<num_peers_in_round[current_round];i++)
673 entry = make_oplist_entry ();
675 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
682 /* stop peers if we have too many */
683 for (i=num_peers_in_round[current_round];i<peers_running;i++)
685 entry = make_oplist_entry ();
687 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
698 * Task run at the end of a round. Disconnect from all monitored
699 * peers; then get statistics from *all* peers.
701 * @param cls NULL, unused
705 next_round (void *cls,
706 const struct GNUNET_SCHEDULER_TaskContext *tc)
708 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
712 if (current_round == num_rounds)
714 /* this was the last round, terminate */
716 GNUNET_SCHEDULER_shutdown ();
719 if (num_peers_in_round[current_round] == peers_running)
721 /* no need to churn, just run next round */
725 adjust_running_peers ();
730 * Function that will be called whenever something in the
733 * @param cls closure, NULL
734 * @param event information on what is happening
737 master_controller_cb (void *cls,
738 const struct GNUNET_TESTBED_EventInformation *event)
742 case GNUNET_TESTBED_ET_CONNECT:
745 case GNUNET_TESTBED_ET_DISCONNECT:
755 * Signature of a main function for a testcase.
758 * @param num_peers_ number of peers in 'peers'
759 * @param peers handle to peers run in the testbed. NULL upon timeout (see
760 * GNUNET_TESTBED_test_run()).
761 * @param links_succeeded the number of overlay link connection attempts that
763 * @param links_failed the number of overlay link connection attempts that
767 test_master (void *cls,
768 unsigned int num_peers_,
769 struct GNUNET_TESTBED_Peer **peers,
770 unsigned int links_succeeded,
771 unsigned int links_failed)
779 GNUNET_break (num_peers_ == num_peers);
780 peers_running = num_peers;
781 if (num_peers_in_round[current_round] == peers_running)
783 /* no need to churn, just run the starting round */
787 adjust_running_peers ();
792 * Actual main function that runs the emulation.
795 * @param args remaining args, unused
796 * @param cfgfile name of the configuration
797 * @param cfg configuration handle
800 run (void *cls, char *const *args, const char *cfgfile,
801 const struct GNUNET_CONFIGURATION_Handle *cfg)
808 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
810 if (NULL == num_peer_spec)
812 fprintf (stderr, "You need to specify the number of peers to run\n");
815 for (tok = strtok (num_peer_spec, ","); NULL != tok; tok = strtok (NULL, ","))
817 if (1 != sscanf (tok, "%u", &num))
819 fprintf (stderr, "You need to specify numbers, not `%s'\n", tok);
824 fprintf (stderr, "Refusing to run a round with 0 peers\n");
827 GNUNET_array_append (num_peers_in_round, num_rounds, num);
828 num_peers = GNUNET_MAX (num_peers, num);
832 fprintf (stderr, "Refusing to run a testbed with no rounds\n");
835 if ( (NULL != data_filename) &&
836 (NULL == (data_file =
837 GNUNET_DISK_file_open (data_filename,
838 GNUNET_DISK_OPEN_READWRITE |
839 GNUNET_DISK_OPEN_TRUNCATE |
840 GNUNET_DISK_OPEN_CREATE,
841 GNUNET_DISK_PERM_USER_READ |
842 GNUNET_DISK_PERM_USER_WRITE))) )
843 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
847 if ( (NULL != output_filename) &&
848 (NULL == (output_file =
849 GNUNET_DISK_file_open (output_filename,
850 GNUNET_DISK_OPEN_READWRITE |
851 GNUNET_DISK_OPEN_CREATE,
852 GNUNET_DISK_PERM_USER_READ |
853 GNUNET_DISK_PERM_USER_WRITE))) )
854 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
857 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
858 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
859 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
860 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
861 GNUNET_TESTBED_run (hosts_file,
865 master_controller_cb,
866 NULL, /* master_controller_cb cls */
868 NULL); /* test_master cls */
870 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
871 &shutdown_task, NULL);
878 * @return 0 on success
881 main (int argc, char *const *argv)
883 static struct GNUNET_GETOPT_CommandLineOption options[] = {
884 {'C', "connections", "COUNT",
885 gettext_noop ("limit to the number of connections to NSE services, 0 for none"),
886 1, &GNUNET_GETOPT_set_uint, &connection_limit},
887 {'d', "details", "FILENAME",
888 gettext_noop ("name of the file for writing connection information and statistics"),
889 1, &GNUNET_GETOPT_set_string, &data_filename},
890 {'H', "hosts", "FILENAME",
891 gettext_noop ("name of the file with the login information for the testbed"),
892 1, &GNUNET_GETOPT_set_string, &hosts_file},
893 {'o', "output", "FILENAME",
894 gettext_noop ("name of the file for writing the main results"),
895 1, &GNUNET_GETOPT_set_string, &output_filename},
896 {'p', "peers", "NETWORKSIZESPEC",
897 gettext_noop ("Number of peers to run in each round, separated by commas"),
898 1, &GNUNET_GETOPT_set_string, &num_peer_spec},
899 {'V', "verbose", NULL,
900 gettext_noop ("be verbose (print progress information)"),
901 0, &GNUNET_GETOPT_increment_value, &verbose},
902 {'w', "wait", "DELAY",
903 gettext_noop ("delay between rounds"),
904 1, &GNUNET_GETOPT_set_relative_time, &wait_time},
905 GNUNET_GETOPT_OPTION_END
907 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
910 GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
912 ("Measure quality and performance of the NSE service."),
913 options, &run, NULL))
918 /* end of nse-profiler.c */