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.
29 * - need to check for leaks (especially FD leaks)
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;
78 * Context for the stats task?
84 * How many messages have peers received during the test.
86 unsigned long long total_nse_received_messages;
89 * How many messages have peers send during the test (should be == received).
91 unsigned long long total_nse_transmitted_messages;
94 * How many messages have travelled an edge in both directions.
96 unsigned long long total_nse_cross;
99 * How many extra messages per edge (corrections) have been received.
101 unsigned long long total_nse_extra;
104 * How many messages have been discarded.
106 unsigned long long total_discarded;
111 * Operation map entry
118 struct OpListEntry *next;
123 struct OpListEntry *prev;
126 * The testbed operation
128 struct GNUNET_TESTBED_Operation *op;
134 * Head of DLL of peers we monitor closely.
136 static struct NSEPeer *peer_head;
139 * Tail of DLL of peers we monitor closely.
141 static struct NSEPeer *peer_tail;
144 * Return value from 'main' (0 == success)
149 * Be verbose (configuration option)
154 * Name of the file with the hosts to run the test over (configuration option)
156 static char *hosts_file;
159 * Maximum number of peers in the test.
161 static unsigned int num_peers;
164 * Total number of rounds to execute.
166 static unsigned int num_rounds;
169 * Current round we are in.
171 static unsigned int current_round;
174 * Array of size 'num_rounds' with the requested number of peers in the given round.
176 static unsigned int *num_peers_in_round;
179 * How many peers are running right now?
181 static unsigned int peers_running;
184 * Specification for the numbers of peers to have in each round.
186 static char *num_peer_spec;
189 * Handles to all of the running peers.
191 static struct GNUNET_TESTBED_Peer **daemons;
194 * Global configuration file
196 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
201 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
204 * Maximum number of connections to NSE services.
206 static unsigned int connection_limit;
209 * Total number of connections in the whole network.
211 static unsigned int total_connections;
214 * File to report results to.
216 static struct GNUNET_DISK_FileHandle *output_file;
219 * Filename to log results to.
221 static char *output_filename;
224 * File to log connection info, statistics to.
226 static struct GNUNET_DISK_FileHandle *data_file;
229 * Filename to log connection info, statistics to.
231 static char *data_filename;
234 * How long to wait before triggering next round?
237 static struct GNUNET_TIME_Relative wait_time = { 60 * 1000 };
240 * DLL head for operation list
242 static struct OpListEntry *oplist_head;
245 * DLL tail for operation list
247 static struct OpListEntry *oplist_tail;
250 * The get stats operation
252 static struct GNUNET_TESTBED_Operation *get_stats_op;
255 * Are we shutting down
257 static int shutting_down;
261 * Clean up all of the monitoring connections to NSE and
262 * STATISTICS that we keep to selected peers.
265 close_monitor_connections ()
268 struct OpListEntry *oplist_entry;
270 while (NULL != (pos = peer_head))
272 if (NULL != pos->nse_op)
273 GNUNET_TESTBED_operation_done (pos->nse_op);
274 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
277 while (NULL != (oplist_entry = oplist_head))
279 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, oplist_entry);
280 GNUNET_TESTBED_operation_done (oplist_entry->op);
281 GNUNET_free (oplist_entry);
287 * Task run on shutdown; cleans up everything.
293 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
295 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
296 if (GNUNET_YES == shutting_down)
298 shutting_down = GNUNET_YES;
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
300 close_monitor_connections ();
301 if (NULL != get_stats_op)
303 GNUNET_TESTBED_operation_done (get_stats_op);
306 // FIXME: what about closing other files!?
307 if (NULL != data_file)
308 GNUNET_DISK_file_close (data_file);
309 if (NULL != testing_cfg)
310 GNUNET_CONFIGURATION_destroy (testing_cfg);
316 * Schedules shutdown task to be run now
321 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task_id)
322 GNUNET_SCHEDULER_cancel (shutdown_task_id);
323 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
328 * Callback to call when network size estimate is updated.
330 * @param cls closure with the 'struct NSEPeer' providing the update
331 * @param timestamp server timestamp
332 * @param estimate the value of the current network size estimate
333 * @param std_dev standard deviation (rounded down to nearest integer)
334 * of the size estimation values seen
338 handle_estimate (void *cls,
339 struct GNUNET_TIME_Absolute timestamp,
340 double estimate, double std_dev)
342 struct NSEPeer *peer = cls;
343 char output_buffer[512];
346 if (NULL == output_file)
349 "Received network size estimate from peer %p. Size: %f std.dev. %f\n",
350 peer, estimate, std_dev);
353 size = GNUNET_snprintf (output_buffer,
354 sizeof (output_buffer),
355 "%p %llu %llu %f %f %f\n",
358 GNUNET_NSE_log_estimate_to_n (estimate), estimate,
360 if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
361 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
362 "Unable to write to file!\n");
367 * Adapter function called to establish a connection to
370 * @param cls closure (the 'struct NSEPeer')
371 * @param cfg configuration of the peer to connect to; will be available until
372 * GNUNET_TESTBED_operation_done() is called on the operation returned
373 * from GNUNET_TESTBED_service_connect()
374 * @return service handle to return in 'op_result', NULL on error
377 nse_connect_adapter (void *cls,
378 const struct GNUNET_CONFIGURATION_Handle *cfg)
380 struct NSEPeer *current_peer = cls;
382 return GNUNET_NSE_connect (cfg, &handle_estimate, current_peer);
387 * Adapter function called to destroy a connection to
391 * @param op_result service handle returned from the connect adapter
394 nse_disconnect_adapter (void *cls,
397 GNUNET_NSE_disconnect (op_result);
402 * Task run to connect to the NSE and statistics services to a subset of
403 * all of the running peers.
406 connect_nse_service ()
408 struct NSEPeer *current_peer;
410 unsigned int connections;
412 if (0 == connection_limit)
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
416 for (i = 0; i < num_peers_in_round[current_round]; i++)
418 if ((num_peers_in_round[current_round] > connection_limit) &&
419 (0 != (i % (num_peers_in_round[current_round] / connection_limit))))
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "nse-profiler: connecting to nse service of peer %d\n", i);
423 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
424 current_peer->daemon = daemons[i];
426 = GNUNET_TESTBED_service_connect (NULL,
427 current_peer->daemon,
430 &nse_connect_adapter,
431 &nse_disconnect_adapter,
433 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
434 if (++connections == connection_limit)
441 * Task that starts/stops peers to move to the next round.
443 * @param cls NULL, unused
444 * @param tc scheduler context (unused)
447 next_round (void *cls,
448 const struct GNUNET_SCHEDULER_TaskContext *tc);
452 * Continuation called by the "get_all" and "get" functions at the
453 * end of a round. Obtains the final statistics and writes them to
454 * the file, then either starts the next round, or, if this was the
455 * last round, terminates the run.
457 * @param cls struct StatsContext
458 * @param op operation handle
459 * @param emsg error message, NULL on success
462 stats_finished_callback (void *cls,
463 struct GNUNET_TESTBED_Operation *op,
466 struct StatsContext *stats_context = cls;
470 GNUNET_TESTBED_operation_done (get_stats_op);
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 "Failed to get statistics: %s\n",
477 GNUNET_SCHEDULER_shutdown ();
478 GNUNET_free (stats_context);
481 LOG_DEBUG ("Finished collecting statistics\n");
482 if (NULL != data_file)
484 /* Stats lookup successful, write out data */
486 GNUNET_snprintf (buf, sizeof (buf),
487 "TOTAL_NSE_RECEIVED_MESSAGES_%u: %u \n",
489 stats_context->total_nse_received_messages);
490 GNUNET_DISK_file_write (data_file, buf, buf_len);
492 GNUNET_snprintf (buf, sizeof (buf),
493 "TOTAL_NSE_TRANSMITTED_MESSAGES_%u: %u\n",
495 stats_context->total_nse_transmitted_messages);
496 GNUNET_DISK_file_write (data_file, buf, buf_len);
498 GNUNET_snprintf (buf, sizeof (buf),
499 "TOTAL_NSE_CROSS_%u: %u \n",
501 stats_context->total_nse_cross);
502 GNUNET_DISK_file_write (data_file, buf, buf_len);
504 GNUNET_snprintf (buf, sizeof (buf),
505 "TOTAL_NSE_EXTRA_%u: %u \n",
507 stats_context->total_nse_extra);
508 GNUNET_DISK_file_write (data_file, buf, buf_len);
510 GNUNET_snprintf (buf, sizeof (buf),
511 "TOTAL_NSE_DISCARDED_%u: %u \n",
513 stats_context->total_discarded);
514 GNUNET_DISK_file_write (data_file, buf, buf_len);
516 GNUNET_SCHEDULER_add_now (&next_round, NULL);
517 GNUNET_free (stats_context);
522 * Callback function to process statistic values.
524 * @param cls struct StatsContext
525 * @param peer the peer the statistics belong to
526 * @param subsystem name of subsystem that created the statistic
527 * @param name the name of the datum
528 * @param value the current value
529 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
530 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
533 statistics_iterator (void *cls,
534 const struct GNUNET_TESTBED_Peer *peer,
535 const char *subsystem, const char *name, uint64_t value,
538 struct StatsContext *stats_context = cls;
540 const char *const sections[] = {"core", "transport", "nse"};
543 unsigned int nsections;
545 nsections = sizeof (sections) / sizeof (char *);
546 for (cnt = 0; cnt < nsections; cnt++)
547 if (0 == strcmp (subsystem, sections[cnt]))
549 if (cnt == nsections)
552 if (NULL == data_file)
554 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
555 "%p -> %s [%s]: %llu\n",
556 peer, subsystem, name, value);
561 GNUNET_snprintf (buf,
565 subsystem, name, value);
566 if (buf_len != GNUNET_DISK_file_write (data_file, buf, buf_len))
567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
569 if (0 != strcmp (subsystem, "nse"))
571 if (0 == strcmp (name, "# flood messages received"))
573 stats_context->total_nse_received_messages += value;
574 if ( (verbose > 1) &&
575 (NULL != data_file) )
578 GNUNET_snprintf (buf, sizeof (buf),
581 GNUNET_DISK_file_write (data_file, buf, buf_len);
584 if (0 == strcmp (name, "# flood messages transmitted"))
586 stats_context->total_nse_transmitted_messages += value;
587 if ( (verbose > 1) &&
588 (NULL != data_file) )
591 GNUNET_snprintf (buf, sizeof (buf),
592 "%p %u TRANSMITTED\n",
594 GNUNET_DISK_file_write (data_file, buf, buf_len);
597 if (0 == strcmp (name, "# cross messages"))
598 stats_context->total_nse_cross += value;
599 if (0 == strcmp (name, "# extra messages"))
600 stats_context->total_nse_extra += value;
601 if (0 == strcmp (name, "# flood messages discarded (clock skew too large)"))
602 stats_context->total_discarded += value;
608 * We're at the end of a round. Stop monitoring, write total
609 * number of connections to log and get full stats. Then trigger
612 * @param cls unused, NULL
616 finish_round (void *cls,
617 const struct GNUNET_SCHEDULER_TaskContext *tc)
619 struct StatsContext *stats_context;
623 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
625 LOG (GNUNET_ERROR_TYPE_INFO, "Have %u connections\n", total_connections);
626 if (NULL != data_file)
628 buf_len = GNUNET_snprintf (buf, sizeof (buf),
629 "CONNECTIONS_0: %u\n",
631 GNUNET_DISK_file_write (data_file, buf, buf_len);
633 close_monitor_connections ();
634 stats_context = GNUNET_malloc (sizeof (struct StatsContext));
636 GNUNET_TESTBED_get_statistics (num_peers_in_round[current_round],
638 &statistics_iterator,
639 &stats_finished_callback,
645 * We have reached the desired number of peers for the current round.
646 * Run it (by connecting and monitoring a few peers and waiting the
647 * specified delay before finishing the round).
652 LOG_DEBUG ("Running round %u\n", current_round);
653 connect_nse_service ();
654 GNUNET_SCHEDULER_add_delayed (wait_time,
661 * Creates an oplist entry and adds it to the oplist DLL
663 static struct OpListEntry *
666 struct OpListEntry *entry;
668 entry = GNUNET_malloc (sizeof (struct OpListEntry));
669 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
675 * Functions of this signature are called when a peer has been successfully
676 * started or stopped.
679 * @param emsg NULL on success; otherwise an error description
682 peer_churn_cb (void *cls, const char *emsg)
684 struct OpListEntry *entry = cls;
686 GNUNET_TESTBED_operation_done (entry->op);
687 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
689 if (num_peers_in_round[current_round] == peers_running)
695 * Adjust the number of running peers to match the required number of running
696 * peers for the round
702 adjust_running_peers ()
704 struct OpListEntry *entry;
707 /* start peers if we have too few */
708 for (i=peers_running;i<num_peers_in_round[current_round];i++)
710 entry = make_oplist_entry ();
711 entry->op = GNUNET_TESTBED_peer_start (NULL, daemons[i],
712 &peer_churn_cb, entry);
714 /* stop peers if we have too many */
715 for (i=num_peers_in_round[current_round];i<peers_running;i++)
717 entry = make_oplist_entry ();
718 entry->op = GNUNET_TESTBED_peer_stop (NULL, daemons[i],
719 &peer_churn_cb, entry);
725 * Task run at the end of a round. Disconnect from all monitored
726 * peers; then get statistics from *all* peers.
728 * @param cls NULL, unused
732 next_round (void *cls,
733 const struct GNUNET_SCHEDULER_TaskContext *tc)
735 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
739 if (current_round == num_rounds)
741 /* this was the last round, terminate */
743 GNUNET_SCHEDULER_shutdown ();
746 if (num_peers_in_round[current_round] == peers_running)
748 /* no need to churn, just run next round */
752 adjust_running_peers ();
757 * Function that will be called whenever something in the
760 * @param cls closure, NULL
761 * @param event information on what is happening
764 master_controller_cb (void *cls,
765 const struct GNUNET_TESTBED_EventInformation *event)
769 case GNUNET_TESTBED_ET_PEER_START:
772 case GNUNET_TESTBED_ET_PEER_STOP:
775 case GNUNET_TESTBED_ET_CONNECT:
778 case GNUNET_TESTBED_ET_DISCONNECT:
788 * Signature of a main function for a testcase.
791 * @param num_peers_ number of peers in 'peers'
792 * @param peers handle to peers run in the testbed. NULL upon timeout (see
793 * GNUNET_TESTBED_test_run()).
796 test_master (void *cls,
797 unsigned int num_peers_,
798 struct GNUNET_TESTBED_Peer **peers)
806 GNUNET_break (num_peers_ == num_peers);
807 peers_running = num_peers;
808 if (num_peers_in_round[current_round] == peers_running)
810 /* no need to churn, just run the starting round */
814 adjust_running_peers ();
819 * Actual main function that runs the emulation.
822 * @param args remaining args, unused
823 * @param cfgfile name of the configuration
824 * @param cfg configuration handle
827 run (void *cls, char *const *args, const char *cfgfile,
828 const struct GNUNET_CONFIGURATION_Handle *cfg)
835 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
837 if (NULL == num_peer_spec)
839 fprintf (stderr, "You need to specify the number of peers to run\n");
842 for (tok = strtok (num_peer_spec, ","); NULL != tok; tok = strtok (NULL, ","))
844 if (1 != sscanf (tok, "%u", &num))
846 fprintf (stderr, "You need to specify numbers, not `%s'\n", tok);
851 fprintf (stderr, "Refusing to run a round with 0 peers\n");
854 GNUNET_array_append (num_peers_in_round, num_rounds, num);
855 num_peers = GNUNET_MAX (num_peers, num);
859 fprintf (stderr, "Refusing to run a testbed with no rounds\n");
862 if ( (NULL != data_filename) &&
863 (NULL == (data_file =
864 GNUNET_DISK_file_open (data_filename,
865 GNUNET_DISK_OPEN_READWRITE |
866 GNUNET_DISK_OPEN_TRUNCATE |
867 GNUNET_DISK_OPEN_CREATE,
868 GNUNET_DISK_PERM_USER_READ |
869 GNUNET_DISK_PERM_USER_WRITE))) )
870 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
874 if ( (NULL != output_filename) &&
875 (NULL == (output_file =
876 GNUNET_DISK_file_open (output_filename,
877 GNUNET_DISK_OPEN_READWRITE |
878 GNUNET_DISK_OPEN_CREATE,
879 GNUNET_DISK_PERM_USER_READ |
880 GNUNET_DISK_PERM_USER_WRITE))) )
881 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
884 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
885 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
886 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
887 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
888 GNUNET_TESTBED_run (hosts_file,
892 master_controller_cb,
893 NULL, /* master_controller_cb cls */
895 NULL); /* test_master cls */
897 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
898 &shutdown_task, NULL);
905 * @return 0 on success
908 main (int argc, char *const *argv)
910 static struct GNUNET_GETOPT_CommandLineOption options[] = {
911 {'C', "connections", "COUNT",
912 gettext_noop ("limit to the number of connections to NSE services, 0 for none"),
913 1, &GNUNET_GETOPT_set_uint, &connection_limit},
914 {'d', "details", "FILENAME",
915 gettext_noop ("name of the file for writing connection information and statistics"),
916 1, &GNUNET_GETOPT_set_string, &data_filename},
917 {'H', "hosts", "FILENAME",
918 gettext_noop ("name of the file with the login information for the testbed"),
919 1, &GNUNET_GETOPT_set_string, &hosts_file},
920 {'o', "output", "FILENAME",
921 gettext_noop ("name of the file for writing the main results"),
922 1, &GNUNET_GETOPT_set_string, &output_filename},
923 {'p', "peers", "NETWORKSIZESPEC",
924 gettext_noop ("Number of peers to run in each round, separated by commas"),
925 1, &GNUNET_GETOPT_set_string, &num_peer_spec},
926 {'V', "verbose", NULL,
927 gettext_noop ("be verbose (print progress information)"),
928 0, &GNUNET_GETOPT_increment_value, &verbose},
929 {'w', "wait", "DELAY",
930 gettext_noop ("delay between rounds"),
931 1, &GNUNET_GETOPT_set_relative_time, &wait_time},
932 GNUNET_GETOPT_OPTION_END
934 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
937 GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
939 ("Measure quality and performance of the NSE service."),
940 options, &run, NULL))
945 /* end of nse-profiler.c */