2 This file is part of GNUnet.
3 (C) 2009 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/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 #include "gnunet_testing_lib.h"
30 #include "gnunet_nse_service.h"
32 #define VERBOSE GNUNET_NO
40 struct GNUNET_TESTING_Daemon *daemon;
42 struct GNUNET_NSE_Handle *nse_handle;
45 struct NSEPeer *peer_head;
47 struct NSEPeer *peer_tail;
50 * How long until we give up on connecting the peers?
52 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
62 * Total number of peers in the test.
64 static unsigned long long num_peers;
67 * Global configuration file
69 struct GNUNET_CONFIGURATION_Handle *testing_cfg;
72 * Total number of currently running peers.
74 static unsigned long long peers_running;
77 * Current round we are in.
79 static unsigned long long current_round;
82 * Peers desired in the next round.
84 static unsigned long long peers_next_round;
87 * Total number of connections in the whole network.
89 static unsigned int total_connections;
92 * The currently running peer group.
94 static struct GNUNET_TESTING_PeerGroup *pg;
97 * File to report results to.
99 static struct GNUNET_DISK_FileHandle *output_file;
102 * How many data points to capture before triggering next round?
104 static struct GNUNET_TIME_Relative wait_time;
107 * Task called to disconnect peers.
109 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
112 * Task called to shutdown test.
114 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
117 * Task used to churn the network.
119 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
122 * Check whether peers successfully shut down.
125 shutdown_callback (void *cls, const char *emsg)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "All peers successfully shut down!\n");
146 shutdown_task (void *cls,
147 const struct GNUNET_SCHEDULER_TaskContext *tc)
151 fprintf(stderr, "Ending test.\n");
154 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
156 GNUNET_SCHEDULER_cancel(disconnect_task);
157 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
159 while (NULL != (pos = peer_head))
161 if (pos->nse_handle != NULL)
162 GNUNET_NSE_disconnect(pos->nse_handle);
163 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
167 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
171 * Callback to call when network size estimate is updated.
174 * @param estimate the value of the current network size estimate
175 * @param std_dev standard deviation (rounded down to nearest integer)
176 * of the size estimation values seen
180 handle_estimate (void *cls, double estimate, double std_dev)
182 struct NSEPeer *peer = cls;
185 //fprintf(stderr, "Received network size estimate from peer %s. Size: %f std.dev. %f\n", GNUNET_i2s(&peer->daemon->id), estimate, std_dev);
186 if (output_file != NULL)
188 size = GNUNET_asprintf(&output_buffer, "%s %u %f %f\n", GNUNET_i2s(&peer->daemon->id), peers_running, estimate, std_dev);
189 if (size != GNUNET_DISK_file_write(output_file, output_buffer, size))
190 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: Unable to write to file!\n", "nse-profiler");
193 fprintf(stderr, "Received network size estimate from peer %s. Size: %f std.dev. %f\n", GNUNET_i2s(&peer->daemon->id), estimate, std_dev);
199 connect_nse_service (void *cls,
200 const struct GNUNET_SCHEDULER_TaskContext *tc)
202 struct NSEPeer *current_peer;
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
207 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
208 for (i = 0; i < num_peers; i++)
210 current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
211 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
212 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
214 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate, current_peer);
215 GNUNET_assert(current_peer->nse_handle != NULL);
217 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
222 churn_peers (void *cls,
223 const struct GNUNET_SCHEDULER_TaskContext *tc);
226 disconnect_nse_peers (void *cls,
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
231 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
234 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "TEST_NSE_MULTIPEER: disconnecting nse service of peers\n");
237 if (pos->nse_handle != NULL)
239 GNUNET_NSE_disconnect(pos->nse_handle);
240 pos->nse_handle = NULL;
245 GNUNET_asprintf(&buf, "round%llu", current_round);
246 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler", buf, &peers_next_round))
249 GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
250 churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
252 else /* No more rounds, let's shut it down! */
254 GNUNET_SCHEDULER_cancel(shutdown_handle);
255 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
261 * Prototype of a function that will be called when a
262 * particular operation was completed the testing library.
265 * @param emsg NULL on success
268 churn_callback (void *cls, const char *emsg)
270 if (emsg == NULL) /* Everything is okay! */
272 peers_running = GNUNET_TESTING_daemons_running(pg);
273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
274 "Round %lu, churn finished successfully.\n", current_round);
275 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
276 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL);
277 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
282 "Round %lu, churn FAILED!!\n", current_round);
283 GNUNET_SCHEDULER_cancel(shutdown_handle);
284 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
289 churn_peers (void *cls,
290 const struct GNUNET_SCHEDULER_TaskContext *tc)
292 peers_running = GNUNET_TESTING_daemons_running(pg);
293 churn_task = GNUNET_SCHEDULER_NO_TASK;
294 if (peers_next_round == peers_running)
296 /* Nothing to do... */
297 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
298 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
299 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL);
300 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n", current_round);
304 if (peers_next_round > num_peers)
306 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Asked to turn on more peers than have!!\n");
307 GNUNET_SCHEDULER_cancel(shutdown_handle);
308 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
310 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311 "Round %lu, turning off %lu peers, turning on %lu peers!\n",
313 (peers_running > peers_next_round) ? peers_running
314 - peers_next_round : 0,
315 (peers_next_round > peers_running) ? peers_next_round
316 - peers_running : 0);
317 GNUNET_TESTING_daemons_churn (pg,
318 (peers_running > peers_next_round) ? peers_running
321 (peers_next_round > peers_running) ? peers_next_round
323 : 0, wait_time, &churn_callback,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Peergroup callback called with error, aborting test!\n");
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
340 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Peer Group started successfully, connecting to NSE service for each peer!\n");
347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n", total_connections);
348 peers_running = GNUNET_TESTING_daemons_running(pg);
349 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
350 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL);
354 * Prototype of a function that will be called whenever
355 * two daemons are connected by the testing library.
358 * @param first peer id for first daemon
359 * @param second peer id for the second daemon
360 * @param distance distance between the connected peers
361 * @param first_cfg config for the first daemon
362 * @param second_cfg config for the second daemon
363 * @param first_daemon handle for the first daemon
364 * @param second_daemon handle for the second daemon
365 * @param emsg error message (NULL on success)
367 void connect_cb (void *cls,
368 const struct GNUNET_PeerIdentity *first,
369 const struct GNUNET_PeerIdentity *second,
371 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
372 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
373 struct GNUNET_TESTING_Daemon *first_daemon,
374 struct GNUNET_TESTING_Daemon *second_daemon,
379 second_id = GNUNET_strdup(GNUNET_i2s(second));
388 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
391 unsigned long long temp_wait;
393 testing_cfg = GNUNET_CONFIGURATION_create();
394 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(testing_cfg, cfgfile));
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
397 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
401 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing", "num_peers", &num_peers))
403 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n");
407 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler", "wait_time", &temp_wait))
409 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option nse-profiler:wait_time is required!\n");
412 wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait);
414 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg, "nse-profiler", "output_file", &temp_str))
416 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
417 | GNUNET_DISK_OPEN_CREATE,
418 GNUNET_DISK_PERM_USER_READ |
419 GNUNET_DISK_PERM_USER_WRITE);
420 if (output_file == NULL)
421 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n", temp_str);
423 GNUNET_free_non_null(temp_str);
425 pg = GNUNET_TESTING_peergroup_start(testing_cfg,
431 GNUNET_assert (pg != NULL);
432 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(), &shutdown_task, NULL);
438 * nse-profiler command line options
440 static struct GNUNET_GETOPT_CommandLineOption options[] = {
441 {'V', "verbose", NULL,
442 gettext_noop ("be verbose (print progress information)"),
443 0, &GNUNET_GETOPT_set_one, &verbose},
444 GNUNET_GETOPT_OPTION_END
448 main (int argc, char *argv[])
452 GNUNET_log_setup ("nse-profiler",
460 GNUNET_PROGRAM_run (argc,
461 argv, "nse-profiler", gettext_noop
462 ("Run a test of the NSE service."),
465 GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
469 /* end of nse-profiler.c */