2 This file is part of GNUnet.
3 Copyright (C) 2011, 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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;
94 struct OpListEntry *next;
99 struct OpListEntry *prev;
102 * The testbed operation
104 struct GNUNET_TESTBED_Operation *op;
107 * Depending on whether we start or stop NSE service at the peer set this to 1
115 * Head of DLL of peers we monitor closely.
117 static struct NSEPeer *peer_head;
120 * Tail of DLL of peers we monitor closely.
122 static struct NSEPeer *peer_tail;
125 * Return value from 'main' (0 == success)
130 * Be verbose (configuration option)
135 * Name of the file with the hosts to run the test over (configuration option)
137 static char *hosts_file;
140 * Maximum number of peers in the test.
142 static unsigned int num_peers;
145 * Total number of rounds to execute.
147 static unsigned int num_rounds;
150 * Current round we are in.
152 static unsigned int current_round;
155 * Array of size 'num_rounds' with the requested number of peers in the given round.
157 static unsigned int *num_peers_in_round;
160 * How many peers are running right now?
162 static unsigned int peers_running;
165 * Specification for the numbers of peers to have in each round.
167 static char *num_peer_spec;
170 * Handles to all of the running peers.
172 static struct GNUNET_TESTBED_Peer **daemons;
175 * Global configuration file
177 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
182 static struct GNUNET_SCHEDULER_Task * shutdown_task_id;
185 * Maximum number of connections to NSE services.
187 static unsigned int connection_limit;
190 * Total number of connections in the whole network.
192 static unsigned int total_connections;
195 * File to report results to.
197 static struct GNUNET_DISK_FileHandle *output_file;
200 * Filename to log results to.
202 static char *output_filename;
205 * File to log connection info, statistics to.
207 static struct GNUNET_DISK_FileHandle *data_file;
210 * Filename to log connection info, statistics to.
212 static char *data_filename;
215 * How long to wait before triggering next round?
218 static struct GNUNET_TIME_Relative wait_time = { 60 * 1000 };
221 * DLL head for operation list
223 static struct OpListEntry *oplist_head;
226 * DLL tail for operation list
228 static struct OpListEntry *oplist_tail;
231 * Are we shutting down
233 static int shutting_down;
237 * Clean up all of the monitoring connections to NSE and
238 * STATISTICS that we keep to selected peers.
241 close_monitor_connections ()
244 struct OpListEntry *oplist_entry;
246 while (NULL != (pos = peer_head))
248 if (NULL != pos->nse_op)
249 GNUNET_TESTBED_operation_done (pos->nse_op);
250 if (NULL != pos->stat_op)
251 GNUNET_TESTBED_operation_done (pos->stat_op);
252 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
255 while (NULL != (oplist_entry = oplist_head))
257 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, oplist_entry);
258 GNUNET_TESTBED_operation_done (oplist_entry->op);
259 GNUNET_free (oplist_entry);
265 * Task run on shutdown; cleans up everything.
271 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
273 shutdown_task_id = NULL;
274 if (GNUNET_YES == shutting_down)
276 shutting_down = GNUNET_YES;
277 LOG_DEBUG ("Ending test.\n");
278 close_monitor_connections ();
279 if (NULL != data_file)
281 GNUNET_DISK_file_close (data_file);
284 if (NULL != output_file)
286 GNUNET_DISK_file_close (output_file);
289 if (NULL != testing_cfg)
290 GNUNET_CONFIGURATION_destroy (testing_cfg);
296 * Schedules shutdown task to be run now
301 if (NULL != shutdown_task_id)
302 GNUNET_SCHEDULER_cancel (shutdown_task_id);
303 shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
308 * Callback to call when network size estimate is updated.
310 * @param cls closure with the 'struct NSEPeer' providing the update
311 * @param timestamp server timestamp
312 * @param estimate the value of the current network size estimate
313 * @param std_dev standard deviation (rounded down to nearest integer)
314 * of the size estimation values seen
317 handle_estimate (void *cls,
318 struct GNUNET_TIME_Absolute timestamp,
319 double estimate, double std_dev)
321 struct NSEPeer *peer = cls;
322 char output_buffer[512];
325 if (NULL == output_file)
328 "Received network size estimate from peer %p. Size: %f std.dev. %f\n",
329 peer, estimate, std_dev);
332 size = GNUNET_snprintf (output_buffer,
333 sizeof (output_buffer),
334 "%p %llu %llu %f %f %f\n",
336 (unsigned long long) timestamp.abs_value_us,
337 GNUNET_NSE_log_estimate_to_n (estimate), estimate,
339 if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
341 "Unable to write to file!\n");
346 * Adapter function called to establish a connection to
349 * @param cls closure (the 'struct NSEPeer')
350 * @param cfg configuration of the peer to connect to; will be available until
351 * GNUNET_TESTBED_operation_done() is called on the operation returned
352 * from GNUNET_TESTBED_service_connect()
353 * @return service handle to return in 'op_result', NULL on error
356 nse_connect_adapter (void *cls,
357 const struct GNUNET_CONFIGURATION_Handle *cfg)
359 struct NSEPeer *current_peer = cls;
361 return GNUNET_NSE_connect (cfg, &handle_estimate, current_peer);
366 * Adapter function called to destroy a connection to
370 * @param op_result service handle returned from the connect adapter
373 nse_disconnect_adapter (void *cls,
376 GNUNET_NSE_disconnect (op_result);
381 * Callback function to process statistic values.
383 * @param cls `struct NSEPeer`
384 * @param subsystem name of subsystem that created the statistic
385 * @param name the name of the datum
386 * @param value the current value
387 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
388 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
391 stat_iterator (void *cls,
392 const char *subsystem,
394 uint64_t value, int is_persistent)
397 struct GNUNET_TIME_Absolute now;
401 GNUNET_assert (NULL != data_file);
402 now = GNUNET_TIME_absolute_get ();
403 flag = strcasecmp (subsystem, "core");
406 size = GNUNET_asprintf (&output_buffer, "%llu %llu %u\n",
407 now.abs_value_us / 1000LL / 1000LL,
409 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
411 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
412 GNUNET_free (output_buffer);
413 return GNUNET_SYSERR;
415 GNUNET_free (output_buffer);
421 * Called to open a connection to the peer's statistics
423 * @param cls peer context
424 * @param cfg configuration of the peer to connect to; will be available until
425 * GNUNET_TESTBED_operation_done() is called on the operation returned
426 * from GNUNET_TESTBED_service_connect()
427 * @return service handle to return in 'op_result', NULL on error
430 stat_connect_adapter (void *cls,
431 const struct GNUNET_CONFIGURATION_Handle *cfg)
433 struct NSEPeer *peer = cls;
435 peer->sh = GNUNET_STATISTICS_create ("nse-profiler", cfg);
441 * Called to disconnect from peer's statistics service
443 * @param cls peer context
444 * @param op_result service handle returned from the connect adapter
447 stat_disconnect_adapter (void *cls, void *op_result)
449 struct NSEPeer *peer = cls;
451 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
452 (peer->sh, "core", "# peers connected",
453 stat_iterator, peer));
454 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
455 (peer->sh, "nse", "# peers connected",
456 stat_iterator, peer));
457 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
463 * Called after successfully opening a connection to a peer's statistics
464 * service; we register statistics monitoring for CORE and NSE here.
466 * @param cls the callback closure from functions generating an operation
467 * @param op the operation that has been finished
468 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
469 * @param emsg error message in case the operation has failed; will be NULL if
470 * operation has executed successfully.
473 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
474 void *ca_result, const char *emsg )
476 struct GNUNET_STATISTICS_Handle *sh = ca_result;
477 struct NSEPeer *peer = cls;
484 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
485 (sh, "core", "# peers connected",
486 stat_iterator, peer));
487 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
488 (sh, "nse", "# peers connected",
489 stat_iterator, peer));
494 * Task run to connect to the NSE and statistics services to a subset of
495 * all of the running peers.
498 connect_nse_service ()
500 struct NSEPeer *current_peer;
502 unsigned int connections;
504 if (0 == connection_limit)
506 LOG_DEBUG ("Connecting to nse service of peers\n");
508 for (i = 0; i < num_peers_in_round[current_round]; i++)
510 if ((num_peers_in_round[current_round] > connection_limit) &&
511 (0 != (i % (num_peers_in_round[current_round] / connection_limit))))
513 LOG_DEBUG ("Connecting to nse service of peer %d\n", i);
514 current_peer = GNUNET_new (struct NSEPeer);
515 current_peer->daemon = daemons[i];
517 = GNUNET_TESTBED_service_connect (NULL,
518 current_peer->daemon,
521 &nse_connect_adapter,
522 &nse_disconnect_adapter,
524 if (NULL != data_file)
525 current_peer->stat_op
526 = GNUNET_TESTBED_service_connect (NULL,
527 current_peer->daemon,
531 &stat_connect_adapter,
532 &stat_disconnect_adapter,
534 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
535 if (++connections == connection_limit)
542 * Task that starts/stops peers to move to the next round.
544 * @param cls NULL, unused
545 * @param tc scheduler context (unused)
548 next_round (void *cls,
549 const struct GNUNET_SCHEDULER_TaskContext *tc);
553 * We're at the end of a round. Stop monitoring, write total
554 * number of connections to log and get full stats. Then trigger
557 * @param cls unused, NULL
561 finish_round (void *cls,
562 const struct GNUNET_SCHEDULER_TaskContext *tc)
564 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
566 LOG (GNUNET_ERROR_TYPE_INFO, "Have %u connections\n", total_connections);
567 close_monitor_connections ();
568 GNUNET_SCHEDULER_add_now (&next_round, NULL);
573 * We have reached the desired number of peers for the current round.
574 * Run it (by connecting and monitoring a few peers and waiting the
575 * specified delay before finishing the round).
580 LOG_DEBUG ("Running round %u\n", current_round);
581 connect_nse_service ();
582 GNUNET_SCHEDULER_add_delayed (wait_time,
589 * Creates an oplist entry and adds it to the oplist DLL
591 static struct OpListEntry *
594 struct OpListEntry *entry;
596 entry = GNUNET_new (struct OpListEntry);
597 GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
603 * Callback to be called when NSE service is started or stopped at peers
606 * @param op the operation handle
607 * @param emsg NULL on success; otherwise an error description
610 manage_service_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
613 struct OpListEntry *entry = cls;
615 GNUNET_TESTBED_operation_done (entry->op);
618 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start/stop NSE at a peer\n");
619 GNUNET_SCHEDULER_shutdown ();
622 GNUNET_assert (0 != entry->delta);
623 peers_running += entry->delta;
624 GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
626 if (num_peers_in_round[current_round] == peers_running)
632 * Adjust the number of running peers to match the required number of running
633 * peers for the round
636 adjust_running_peers ()
638 struct OpListEntry *entry;
641 /* start peers if we have too few */
642 for (i=peers_running;i<num_peers_in_round[current_round];i++)
644 entry = make_oplist_entry ();
646 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
653 /* stop peers if we have too many */
654 for (i=num_peers_in_round[current_round];i<peers_running;i++)
656 entry = make_oplist_entry ();
658 entry->op = GNUNET_TESTBED_peer_manage_service (NULL,
669 * Task run at the end of a round. Disconnect from all monitored
670 * peers; then get statistics from *all* peers.
672 * @param cls NULL, unused
676 next_round (void *cls,
677 const struct GNUNET_SCHEDULER_TaskContext *tc)
679 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
681 LOG_DEBUG ("Disconnecting nse service of peers\n");
683 if (current_round == num_rounds)
685 /* this was the last round, terminate */
687 GNUNET_SCHEDULER_shutdown ();
690 if (num_peers_in_round[current_round] == peers_running)
692 /* no need to churn, just run next round */
696 adjust_running_peers ();
701 * Function that will be called whenever something in the
704 * @param cls closure, NULL
705 * @param event information on what is happening
708 master_controller_cb (void *cls,
709 const struct GNUNET_TESTBED_EventInformation *event)
713 case GNUNET_TESTBED_ET_CONNECT:
716 case GNUNET_TESTBED_ET_DISCONNECT:
726 * Signature of a main function for a testcase.
729 * @param h the run handle
730 * @param num_peers_ number of peers in 'peers'
731 * @param peers handle to peers run in the testbed. NULL upon timeout (see
732 * GNUNET_TESTBED_test_run()).
733 * @param links_succeeded the number of overlay link connection attempts that
735 * @param links_failed the number of overlay link connection attempts that
739 test_master (void *cls,
740 struct GNUNET_TESTBED_RunHandle *h,
741 unsigned int num_peers_,
742 struct GNUNET_TESTBED_Peer **peers,
743 unsigned int links_succeeded,
744 unsigned int links_failed)
752 GNUNET_break (num_peers_ == num_peers);
753 peers_running = num_peers;
754 if (num_peers_in_round[current_round] == peers_running)
756 /* no need to churn, just run the starting round */
760 adjust_running_peers ();
765 * Actual main function that runs the emulation.
768 * @param args remaining args, unused
769 * @param cfgfile name of the configuration
770 * @param cfg configuration handle
773 run (void *cls, char *const *args, const char *cfgfile,
774 const struct GNUNET_CONFIGURATION_Handle *cfg)
781 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
782 LOG_DEBUG ("Starting daemons.\n");
783 if (NULL == num_peer_spec)
785 fprintf (stderr, "You need to specify the number of peers to run\n");
788 for (tok = strtok (num_peer_spec, ","); NULL != tok; tok = strtok (NULL, ","))
790 if (1 != sscanf (tok, "%u", &num))
792 fprintf (stderr, "You need to specify numbers, not `%s'\n", tok);
797 fprintf (stderr, "Refusing to run a round with 0 peers\n");
800 GNUNET_array_append (num_peers_in_round, num_rounds, num);
801 num_peers = GNUNET_MAX (num_peers, num);
805 fprintf (stderr, "Refusing to run a testbed with no rounds\n");
808 if ( (NULL != data_filename) &&
809 (NULL == (data_file =
810 GNUNET_DISK_file_open (data_filename,
811 GNUNET_DISK_OPEN_READWRITE |
812 GNUNET_DISK_OPEN_TRUNCATE |
813 GNUNET_DISK_OPEN_CREATE,
814 GNUNET_DISK_PERM_USER_READ |
815 GNUNET_DISK_PERM_USER_WRITE))) )
816 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
820 if ( (NULL != output_filename) &&
821 (NULL == (output_file =
822 GNUNET_DISK_file_open (output_filename,
823 GNUNET_DISK_OPEN_READWRITE |
824 GNUNET_DISK_OPEN_CREATE,
825 GNUNET_DISK_PERM_USER_READ |
826 GNUNET_DISK_PERM_USER_WRITE))) )
827 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
830 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
831 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
832 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
833 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
834 GNUNET_TESTBED_run (hosts_file,
838 master_controller_cb,
839 NULL, /* master_controller_cb cls */
841 NULL); /* test_master cls */
843 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
844 &shutdown_task, NULL);
851 * @return 0 on success
854 main (int argc, char *const *argv)
856 static struct GNUNET_GETOPT_CommandLineOption options[] = {
857 {'C', "connections", "COUNT",
858 gettext_noop ("limit to the number of connections to NSE services, 0 for none"),
859 1, &GNUNET_GETOPT_set_uint, &connection_limit},
860 {'d', "details", "FILENAME",
861 gettext_noop ("name of the file for writing connection information and statistics"),
862 1, &GNUNET_GETOPT_set_string, &data_filename},
863 {'H', "hosts", "FILENAME",
864 gettext_noop ("name of the file with the login information for the testbed"),
865 1, &GNUNET_GETOPT_set_string, &hosts_file},
866 {'o', "output", "FILENAME",
867 gettext_noop ("name of the file for writing the main results"),
868 1, &GNUNET_GETOPT_set_string, &output_filename},
869 {'p', "peers", "NETWORKSIZESPEC",
870 gettext_noop ("Number of peers to run in each round, separated by commas"),
871 1, &GNUNET_GETOPT_set_string, &num_peer_spec},
872 {'V', "verbose", NULL,
873 gettext_noop ("be verbose (print progress information)"),
874 0, &GNUNET_GETOPT_increment_value, &verbose},
875 {'w', "wait", "DELAY",
876 gettext_noop ("delay between rounds"),
877 1, &GNUNET_GETOPT_set_relative_time, &wait_time},
878 GNUNET_GETOPT_OPTION_END
880 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
883 GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
885 ("Measure quality and performance of the NSE service."),
886 options, &run, NULL))
891 /* end of nse-profiler.c */