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"
30 #include "testbed_api_hosts.h"
34 * An array of hosts loaded from the hostkeys file
36 static struct GNUNET_TESTBED_Host **hosts;
39 * The array of peers; we fill this as the peers are given to us by the testbed
41 static struct GNUNET_TESTBED_Peer **peers;
46 static struct GNUNET_TESTBED_Operation *op;
49 * Abort task identifier
51 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
59 * Number of peers to be started by the profiler
61 static unsigned int num_peers;
64 * Number of hosts in the hosts array
66 static unsigned int num_hosts;
69 * Global testing status
78 * @param tc the task context
81 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
86 GNUNET_SCHEDULER_cancel (abort_task);
87 GNUNET_free_non_null (peers);
88 for (nhost = 0; nhost < num_hosts; nhost++)
89 if (NULL != hosts[nhost])
90 GNUNET_TESTBED_host_destroy (hosts[nhost]);
91 GNUNET_free_non_null (hosts);
92 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
97 * abort task to run on test timed out
100 * @param tc the task context
103 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Profiling timedout -- Aborting\n");
106 abort_task = GNUNET_SCHEDULER_NO_TASK;
107 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
112 * Task to be executed when peers are ready
115 * @param tc the task context
118 master_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 GNUNET_assert (NULL != peers[0]);
122 op = GNUNET_TESTBED_peer_stop (peers[0], NULL, NULL);
123 GNUNET_assert (NULL != op);
128 * Controller event callback
131 * @param event the controller event
134 controller_event_cb (void *cls,
135 const struct GNUNET_TESTBED_EventInformation *event)
140 case GNUNET_TESTBED_ET_PEER_START:
141 GNUNET_assert (NULL == peers[peer_id]);
142 GNUNET_assert (NULL != event->details.peer_start.peer);
143 peers[peer_id++] = event->details.peer_start.peer;
145 case GNUNET_TESTBED_ET_PEER_STOP:
146 GNUNET_assert (NULL != op);
147 GNUNET_TESTBED_operation_done (op);
148 GNUNET_assert (peers[0] == event->details.peer_stop.peer);
149 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
158 * Main function that will be run by the scheduler.
161 * @param args remaining command-line arguments
162 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
163 * @param cfg configuration
166 run (void *cls, char *const *args, const char *cfgfile,
167 const struct GNUNET_CONFIGURATION_Handle *config)
174 FPRINTF (stderr, _("No hosts-file specified on command line\n"));
182 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
185 FPRINTF (stderr, _("No hosts loaded\n"));
188 for (nhost = 0; nhost < num_hosts; nhost++)
190 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
192 FPRINTF (stderr, _("Host %s cannot start testbed\n"),
193 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
197 if (num_hosts != nhost)
199 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
202 peers = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Peer *));
204 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
205 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
206 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
207 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
208 GNUNET_TESTBED_run (NULL, config, num_peers, event_mask, &controller_event_cb,
209 NULL, &master_task, NULL);
211 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
212 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
220 * @return 0 on success
223 main (int argc, char *const *argv)
225 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
226 { 'n', "num-peers", "COUNT",
227 gettext_noop ("create COUNT number of peers"),
228 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
229 { 'n', "num-peers", "COUNT",
230 gettext_noop ("create COUNT number of peers"),
231 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
232 GNUNET_GETOPT_OPTION_END
236 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
239 result = GNUNET_SYSERR;
241 GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
242 _("Profiler for testbed"),
243 options, &run, NULL);
244 if (GNUNET_OK != ret)
246 if (GNUNET_OK != result)