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.
22 * @file testbed/gnunet-testbed-profiler.c
23 * @brief Profiling driver for the testbed.
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_common.h"
29 #include "gnunet_testbed_service.h"
33 * The array of peers; we fill this as the peers are given to us by the testbed
35 static struct GNUNET_TESTBED_Peer **peers;
40 static struct GNUNET_TESTBED_Operation *op;
43 * Abort task identifier
45 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
53 * Number of peers to be started by the profiler
55 static unsigned int num_peers;
58 * Global testing status
67 * @param tc the task context
70 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
72 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
73 GNUNET_SCHEDULER_cancel (abort_task);
74 GNUNET_free_non_null (peers);
75 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
80 * abort task to run on test timed out
83 * @param tc the task context
86 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
89 abort_task = GNUNET_SCHEDULER_NO_TASK;
90 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
95 * Task to be executed when peers are ready
98 * @param tc the task context
101 master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 GNUNET_assert (NULL != peers[0]);
105 op = GNUNET_TESTBED_peer_stop (peers[0], NULL, NULL);
106 GNUNET_assert (NULL != op);
111 * Controller event callback
114 * @param event the controller event
117 controller_event_cb (void *cls,
118 const struct GNUNET_TESTBED_EventInformation *event)
123 case GNUNET_TESTBED_ET_PEER_START:
124 GNUNET_assert (NULL == peers[peer_id]);
125 GNUNET_assert (NULL != event->details.peer_start.peer);
126 peers[peer_id++] = event->details.peer_start.peer;
128 case GNUNET_TESTBED_ET_PEER_STOP:
129 GNUNET_assert (NULL != op);
130 GNUNET_TESTBED_operation_done (op);
131 GNUNET_assert (peers[0] == event->details.peer_stop.peer);
132 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
141 * Main function that will be run by the scheduler.
144 * @param args remaining command-line arguments
145 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
146 * @param cfg configuration
149 run (void *cls, char *const *args, const char *cfgfile,
150 const struct GNUNET_CONFIGURATION_Handle *config)
159 peers = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Peer *));
161 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
162 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
163 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
164 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
165 GNUNET_TESTBED_run (NULL, config, num_peers, event_mask, &controller_event_cb,
166 NULL, &master_task, NULL);
168 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
169 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
177 * @return 0 on success
180 main (int argc, char *const *argv)
182 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
183 { 'n', "num-peers", "COUNT",
184 gettext_noop ("create COUNT number of peers"),
185 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
186 GNUNET_GETOPT_OPTION_END
190 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
193 result = GNUNET_SYSERR;
195 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS]",
196 _("Profiler for testbed"),
197 options, &run, NULL);
198 if (GNUNET_OK != ret)
200 if (GNUNET_OK != result)