From 5acd913903a8fa4a4244a5c490204daf7759d451 Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Thu, 11 Oct 2012 15:43:18 +0000 Subject: [PATCH] testbed profiler --- src/testbed/Makefile.am | 9 ++ src/testbed/gnunet-testbed-profiler.c | 203 ++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 src/testbed/gnunet-testbed-profiler.c diff --git a/src/testbed/Makefile.am b/src/testbed/Makefile.am index 0432e8bf7..ec371e59d 100644 --- a/src/testbed/Makefile.am +++ b/src/testbed/Makefile.am @@ -18,6 +18,9 @@ bin_PROGRAMS = \ gnunet-service-testbed \ gnunet-helper-testbed +noinst_PROGRAMS = \ + gnunet-testbed-profiler + gnunet_service_testbed_SOURCES = \ gnunet-service-testbed.c gnunet_service_testbed_LDADD = $(XLIB) \ @@ -31,6 +34,12 @@ gnunet_service_testbed_LDADD = $(XLIB) \ gnunet_service_testbed_DEPENDENCIES = \ libgnunettestbed.la +gnunet_testbed_profiler_SOURCES = \ + gnunet-testbed-profiler.c +gnunet_testbed_profiler_LDADD = $(XLIB) \ + $(top_builddir)/src/util/libgnunetutil.la \ + libgnunettestbed.la + gnunet_helper_testbed_SOURCES = \ gnunet-helper-testbed.c gnunet_helper_testbed_LDADD = $(XLIB) \ diff --git a/src/testbed/gnunet-testbed-profiler.c b/src/testbed/gnunet-testbed-profiler.c new file mode 100644 index 000000000..ead28f360 --- /dev/null +++ b/src/testbed/gnunet-testbed-profiler.c @@ -0,0 +1,203 @@ +/* + This file is part of GNUnet. + (C) 2011, 2012 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file testbed/gnunet-testbed-profiler.c + * @brief Profiling driver for the testbed. + * @author Sree Harsha Totakura + */ + +#include "platform.h" +#include "gnunet_common.h" +#include "gnunet_testbed_service.h" + + +/** + * The array of peers; we fill this as the peers are given to us by the testbed + */ +static struct GNUNET_TESTBED_Peer **peers; + +/** + * Operation handle + */ +static struct GNUNET_TESTBED_Operation *op; + +/** + * Abort task identifier + */ +static GNUNET_SCHEDULER_TaskIdentifier abort_task; + +/** + * Current peer id + */ +unsigned int peer_id; + +/** + * Number of peers to be started by the profiler + */ +static unsigned int num_peers; + +/** + * Global testing status + */ +static int result; + + +/** + * Shutdown nicely + * + * @param cls NULL + * @param tc the task context + */ +static void +do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (GNUNET_SCHEDULER_NO_TASK != abort_task) + GNUNET_SCHEDULER_cancel (abort_task); + GNUNET_free_non_null (peers); + GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */ +} + + +/** + * abort task to run on test timed out + * + * @param cls NULL + * @param tc the task context + */ +static void +do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n"); + abort_task = GNUNET_SCHEDULER_NO_TASK; + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); +} + + +/** + * Task to be executed when peers are ready + * + * @param cls NULL + * @param tc the task context + */ +static void +master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + result = GNUNET_OK; + GNUNET_assert (NULL != peers[0]); + op = GNUNET_TESTBED_peer_stop (peers[0], NULL, NULL); + GNUNET_assert (NULL != op); +} + + +/** + * Controller event callback + * + * @param cls NULL + * @param event the controller event + */ +static void +controller_event_cb (void *cls, + const struct GNUNET_TESTBED_EventInformation *event) +{ + + switch (event->type) + { + case GNUNET_TESTBED_ET_PEER_START: + GNUNET_assert (NULL == peers[peer_id]); + GNUNET_assert (NULL != event->details.peer_start.peer); + peers[peer_id++] = event->details.peer_start.peer; + break; + case GNUNET_TESTBED_ET_PEER_STOP: + GNUNET_assert (NULL != op); + GNUNET_TESTBED_operation_done (op); + GNUNET_assert (peers[0] == event->details.peer_stop.peer); + GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + break; + default: + GNUNET_assert (0); + } +} + + +/** + * Main function that will be run by the scheduler. + * + * @param cls closure + * @param args remaining command-line arguments + * @param cfgfile name of the configuration file used (for saving, can be NULL!) + * @param cfg configuration + */ +static void +run (void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *config) +{ + uint64_t event_mask; + + if (0 == num_peers) + { + result = GNUNET_OK; + return; + } + peers = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Peer *)); + event_mask = 0; + event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START); + event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP); + event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT); + event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT); + GNUNET_TESTBED_run (NULL, config, num_peers, event_mask, &controller_event_cb, + NULL, &master_task, NULL); + abort_task = + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply + (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort, + NULL); +} + + +/** + * Main function. + * + * @return 0 on success + */ +int +main (int argc, char *const *argv) +{ + static const struct GNUNET_GETOPT_CommandLineOption options[] = { + { 'n', "num-peers", "COUNT", + gettext_noop ("create COUNT number of peers"), + GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers }, + GNUNET_GETOPT_OPTION_END + }; + int ret; + + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) + return 2; + + result = GNUNET_SYSERR; + ret = + GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS]", + _("Profiler for testbed"), + options, &run, NULL); + if (GNUNET_OK != ret) + return ret; + if (GNUNET_OK != result) + return 1; + return 0; +} -- 2.25.1