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;
199 * Maximum number of connections to NSE services.
201 static unsigned int connection_limit;
204 * Total number of connections in the whole network.
206 static unsigned int total_connections;
209 * File to report results to.
211 static struct GNUNET_DISK_FileHandle *output_file;
214 * Filename to log results to.
216 static char *output_filename;
219 * File to log connection info, statistics to.
221 static struct GNUNET_DISK_FileHandle *data_file;
224 * Filename to log connection info, statistics to.
226 static char *data_filename;
229 * How long to wait before triggering next round?
232 static struct GNUNET_TIME_Relative wait_time = { 60 * 1000 };
235 * DLL head for operation list
237 static struct OpListEntry *oplist_head;
240 * DLL tail for operation list
242 static struct OpListEntry *oplist_tail;
245 * The get stats operation
247 static struct GNUNET_TESTBED_Operation *get_stats_op;
251 * Clean up all of the monitoring connections to NSE and
252 * STATISTICS that we keep to selected peers.
255 close_monitor_connections ()
258 struct OpListEntry *oplist_entry;
260 while (NULL != (pos = peer_head))
262 if (NULL != pos->nse_op)
263 GNUNET_TESTBED_operation_done (pos->nse_op);
264 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
267 while (NULL != (oplist_entry = oplist_head))
269 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, oplist_entry);
270 GNUNET_TESTBED_operation_done (oplist_entry->op);
271 GNUNET_free (oplist_entry);
277 * Task run on shutdown; cleans up everything.
283 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
286 close_monitor_connections ();
287 if (NULL != get_stats_op)
289 GNUNET_TESTBED_operation_done (get_stats_op);
292 // FIXME: what about closing other files!?
293 if (NULL != data_file)
294 GNUNET_DISK_file_close (data_file);
295 if (NULL != testing_cfg)
296 GNUNET_CONFIGURATION_destroy (testing_cfg);
302 * Callback to call when network size estimate is updated.
304 * @param cls closure with the 'struct NSEPeer' providing the update
305 * @param timestamp server timestamp
306 * @param estimate the value of the current network size estimate
307 * @param std_dev standard deviation (rounded down to nearest integer)
308 * of the size estimation values seen
312 handle_estimate (void *cls,
313 struct GNUNET_TIME_Absolute timestamp,
314 double estimate, double std_dev)
316 struct NSEPeer *peer = cls;
317 char output_buffer[512];
320 if (NULL == output_file)
323 "Received network size estimate from peer %p. Size: %f std.dev. %f\n",
324 peer, estimate, std_dev);
327 size = GNUNET_snprintf (output_buffer,
328 sizeof (output_buffer),
329 "%p %llu %llu %f %f %f\n",
332 GNUNET_NSE_log_estimate_to_n (estimate), estimate,
334 if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
336 "Unable to write to file!\n");
341 * Adapter function called to establish a connection to
344 * @param cls closure (the 'struct NSEPeer')
345 * @param cfg configuration of the peer to connect to; will be available until
346 * GNUNET_TESTBED_operation_done() is called on the operation returned
347 * from GNUNET_TESTBED_service_connect()
348 * @return service handle to return in 'op_result', NULL on error
351 nse_connect_adapter (void *cls,
352 const struct GNUNET_CONFIGURATION_Handle *cfg)
354 struct NSEPeer *current_peer = cls;
356 return GNUNET_NSE_connect (cfg, &handle_estimate, current_peer);
361 * Adapter function called to destroy a connection to
365 * @param op_result service handle returned from the connect adapter
368 nse_disconnect_adapter (void *cls,
371 GNUNET_NSE_disconnect (op_result);
376 * Task run to connect to the NSE and statistics services to a subset of
377 * all of the running peers.
380 connect_nse_service ()
382 struct NSEPeer *current_peer;
384 unsigned int connections;
386 if (0 == connection_limit)
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
390 for (i = 0; i < num_peers_in_round[current_round]; i++)
392 if ((num_peers_in_round[current_round] > connection_limit) &&
393 (0 != (i % (num_peers_in_round[current_round] / connection_limit))))
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "nse-profiler: connecting to nse service of peer %d\n", i);
397 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
398 current_peer->daemon = daemons[i];
400 = GNUNET_TESTBED_service_connect (NULL,
401 current_peer->daemon,
404 &nse_connect_adapter,
405 &nse_disconnect_adapter,
407 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
408 if (++connections == connection_limit)
415 * Task that starts/stops peers to move to the next round.
417 * @param cls NULL, unused
418 * @param tc scheduler context (unused)
421 next_round (void *cls,
422 const struct GNUNET_SCHEDULER_TaskContext *tc);
426 * Continuation called by the "get_all" and "get" functions at the
427 * end of a round. Obtains the final statistics and writes them to
428 * the file, then either starts the next round, or, if this was the
429 * last round, terminates the run.
431 * @param cls struct StatsContext
432 * @param op operation handle
433 * @param emsg error message, NULL on success
436 stats_finished_callback (void *cls,
437 struct GNUNET_TESTBED_Operation *op,
440 struct StatsContext *stats_context = cls;
444 GNUNET_TESTBED_operation_done (get_stats_op);
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449 "Failed to get statistics: %s\n",
451 GNUNET_SCHEDULER_shutdown ();
452 GNUNET_free (stats_context);
455 LOG_DEBUG ("Finished collecting statistics\n");
456 if (NULL != data_file)
458 /* Stats lookup successful, write out data */
460 GNUNET_snprintf (buf, sizeof (buf),
461 "TOTAL_NSE_RECEIVED_MESSAGES_%u: %u \n",
463 stats_context->total_nse_received_messages);
464 GNUNET_DISK_file_write (data_file, buf, buf_len);
466 GNUNET_snprintf (buf, sizeof (buf),
467 "TOTAL_NSE_TRANSMITTED_MESSAGES_%u: %u\n",
469 stats_context->total_nse_transmitted_messages);
470 GNUNET_DISK_file_write (data_file, buf, buf_len);
472 GNUNET_snprintf (buf, sizeof (buf),
473 "TOTAL_NSE_CROSS_%u: %u \n",
475 stats_context->total_nse_cross);
476 GNUNET_DISK_file_write (data_file, buf, buf_len);
478 GNUNET_snprintf (buf, sizeof (buf),
479 "TOTAL_NSE_EXTRA_%u: %u \n",
481 stats_context->total_nse_extra);
482 GNUNET_DISK_file_write (data_file, buf, buf_len);
484 GNUNET_snprintf (buf, sizeof (buf),
485 "TOTAL_NSE_DISCARDED_%u: %u \n",
487 stats_context->total_discarded);
488 GNUNET_DISK_file_write (data_file, buf, buf_len);
490 GNUNET_SCHEDULER_add_now (&next_round, NULL);
491 GNUNET_free (stats_context);
496 * Callback function to process statistic values.
498 * @param cls struct StatsContext
499 * @param peer the peer the statistics belong to
500 * @param subsystem name of subsystem that created the statistic
501 * @param name the name of the datum
502 * @param value the current value
503 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
504 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
507 statistics_iterator (void *cls,
508 const struct GNUNET_TESTBED_Peer *peer,
509 const char *subsystem, const char *name, uint64_t value,
512 struct StatsContext *stats_context = cls;
514 const char *const sections[] = {"core", "transport", "nse"};
517 unsigned int nsections;
519 nsections = sizeof (sections) / sizeof (char *);
520 for (cnt = 0; cnt < nsections; cnt++)
521 if (0 == strcmp (subsystem, sections[cnt]))
523 if (cnt == nsections)
526 if (NULL == data_file)
528 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
529 "%p -> %s [%s]: %llu\n",
530 peer, subsystem, name, value);
535 GNUNET_snprintf (buf,
539 subsystem, name, value);
540 if (buf_len != GNUNET_DISK_file_write (data_file, buf, buf_len))
541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
543 if (0 != strcmp (subsystem, "nse"))
545 if (0 == strcmp (name, "# flood messages received"))
547 stats_context->total_nse_received_messages += value;
548 if ( (verbose > 1) &&
549 (NULL != data_file) )
552 GNUNET_snprintf (buf, sizeof (buf),
555 GNUNET_DISK_file_write (data_file, buf, buf_len);
558 if (0 == strcmp (name, "# flood messages transmitted"))
560 stats_context->total_nse_transmitted_messages += value;
561 if ( (verbose > 1) &&
562 (NULL != data_file) )
565 GNUNET_snprintf (buf, sizeof (buf),
566 "%p %u TRANSMITTED\n",
568 GNUNET_DISK_file_write (data_file, buf, buf_len);
571 if (0 == strcmp (name, "# cross messages"))
572 stats_context->total_nse_cross += value;
573 if (0 == strcmp (name, "# extra messages"))
574 stats_context->total_nse_extra += value;
575 if (0 == strcmp (name, "# flood messages discarded (clock skew too large)"))
576 stats_context->total_discarded += value;
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 struct StatsContext *stats_context;
597 LOG (GNUNET_ERROR_TYPE_INFO, "Have %u connections\n", total_connections);
598 if (NULL != data_file)
600 buf_len = GNUNET_snprintf (buf, sizeof (buf),
601 "CONNECTIONS_0: %u\n",
603 GNUNET_DISK_file_write (data_file, buf, buf_len);
605 close_monitor_connections ();
606 stats_context = GNUNET_malloc (sizeof (struct StatsContext));
608 GNUNET_TESTBED_get_statistics (num_peers_in_round[current_round],
610 &statistics_iterator,
611 &stats_finished_callback,
617 * We have reached the desired number of peers for the current round.
618 * Run it (by connecting and monitoring a few peers and waiting the
619 * specified delay before finishing the round).
624 LOG_DEBUG ("Running round %u\n", current_round);
625 connect_nse_service ();
626 GNUNET_SCHEDULER_add_delayed (wait_time,
633 * Creates an oplist entry and adds it to the oplist DLL
635 static struct OpListEntry *
638 struct OpListEntry *entry;
640 entry = GNUNET_malloc (sizeof (struct OpListEntry));
641 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
647 * Functions of this signature are called when a peer has been successfully
648 * started or stopped.
651 * @param emsg NULL on success; otherwise an error description
654 peer_churn_cb (void *cls, const char *emsg)
656 struct OpListEntry *entry = cls;
658 GNUNET_TESTBED_operation_done (entry->op);
659 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
661 if (num_peers_in_round[current_round] == peers_running)
667 * Adjust the number of running peers to match the required number of running
668 * peers for the round
674 adjust_running_peers ()
676 struct OpListEntry *entry;
679 /* start peers if we have too few */
680 for (i=peers_running;i<num_peers_in_round[current_round];i++)
682 entry = make_oplist_entry ();
683 entry->op = GNUNET_TESTBED_peer_start (NULL, daemons[i],
684 &peer_churn_cb, entry);
687 /* stop peers if we have too many */
688 for (i=num_peers_in_round[current_round];i<peers_running;i++)
690 entry = make_oplist_entry ();
691 entry->op = GNUNET_TESTBED_peer_stop (NULL, daemons[i],
692 &peer_churn_cb, entry);
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)
709 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_PEER_START:
745 case GNUNET_TESTBED_ET_PEER_STOP:
748 case GNUNET_TESTBED_ET_CONNECT:
751 case GNUNET_TESTBED_ET_DISCONNECT:
761 * Signature of a main function for a testcase.
764 * @param num_peers_ number of peers in 'peers'
765 * @param peers handle to peers run in the testbed. NULL upon timeout (see
766 * GNUNET_TESTBED_test_run()).
769 test_master (void *cls,
770 unsigned int num_peers_,
771 struct GNUNET_TESTBED_Peer **peers)
774 GNUNET_break (num_peers_ == num_peers);
775 peers_running = num_peers;
776 if (num_peers_in_round[current_round] == peers_running)
778 /* no need to churn, just run next round */
782 adjust_running_peers ();
787 * Actual main function that runs the emulation.
790 * @param args remaining args, unused
791 * @param cfgfile name of the configuration
792 * @param cfg configuration handle
795 run (void *cls, char *const *args, const char *cfgfile,
796 const struct GNUNET_CONFIGURATION_Handle *cfg)
803 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
805 if (NULL == num_peer_spec)
807 fprintf (stderr, "You need to specify the number of peers to run\n");
810 for (tok = strtok (num_peer_spec, ","); NULL != tok; tok = strtok (NULL, ","))
812 if (1 != sscanf (tok, "%u", &num))
814 fprintf (stderr, "You need to specify numbers, not `%s'\n", tok);
819 fprintf (stderr, "Refusing to run a round with 0 peers\n");
822 GNUNET_array_append (num_peers_in_round, num_rounds, num);
823 num_peers = GNUNET_MAX (num_peers, num);
827 fprintf (stderr, "Refusing to run a testbed with no rounds\n");
830 if ( (NULL != data_filename) &&
831 (NULL == (data_file =
832 GNUNET_DISK_file_open (data_filename,
833 GNUNET_DISK_OPEN_READWRITE |
834 GNUNET_DISK_OPEN_TRUNCATE |
835 GNUNET_DISK_OPEN_CREATE,
836 GNUNET_DISK_PERM_USER_READ |
837 GNUNET_DISK_PERM_USER_WRITE))) )
838 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
842 if ( (NULL != output_filename) &&
843 (NULL == (output_file =
844 GNUNET_DISK_file_open (output_filename,
845 GNUNET_DISK_OPEN_READWRITE |
846 GNUNET_DISK_OPEN_CREATE,
847 GNUNET_DISK_PERM_USER_READ |
848 GNUNET_DISK_PERM_USER_WRITE))) )
849 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
852 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
853 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
854 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
855 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
856 GNUNET_TESTBED_run (hosts_file,
860 master_controller_cb,
861 NULL, /* master_controller_cb cls */
863 NULL); /* test_master cls */
864 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
865 &shutdown_task, NULL);
872 * @return 0 on success
875 main (int argc, char *const *argv)
877 static struct GNUNET_GETOPT_CommandLineOption options[] = {
878 {'C', "connections", "COUNT",
879 gettext_noop ("limit to the number of connections to NSE services, 0 for none"),
880 1, &GNUNET_GETOPT_set_uint, &connection_limit},
881 {'d', "details", "FILENAME",
882 gettext_noop ("name of the file for writing connection information and statistics"),
883 1, &GNUNET_GETOPT_set_string, &data_filename},
884 {'H', "hosts", "FILENAME",
885 gettext_noop ("name of the file with the login information for the testbed"),
886 1, &GNUNET_GETOPT_set_string, &hosts_file},
887 {'o', "output", "FILENAME",
888 gettext_noop ("name of the file for writing the main results"),
889 1, &GNUNET_GETOPT_set_string, &output_filename},
890 {'p', "peers", "NETWORKSIZESPEC",
891 gettext_noop ("Number of peers to run in each round, separated by commas"),
892 1, &GNUNET_GETOPT_set_string, &num_peer_spec},
893 {'V', "verbose", NULL,
894 gettext_noop ("be verbose (print progress information)"),
895 0, &GNUNET_GETOPT_increment_value, &verbose},
896 {'w', "wait", "DELAY",
897 gettext_noop ("delay between rounds"),
898 1, &GNUNET_GETOPT_set_relative_time, &wait_time},
899 GNUNET_GETOPT_OPTION_END
901 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
904 GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
906 ("Measure quality and performance of the NSE service."),
907 options, &run, NULL))
912 /* end of nse-profiler.c */