2 This file is part of GNUnet.
3 (C) 2011 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;
47 GNUNET_SCHEDULER_Task task;
48 GNUNET_SCHEDULER_TaskIdentifier *task_id;
50 unsigned long long total_nse_bytes;
53 static struct NSEPeer *peer_head;
55 static struct NSEPeer *peer_tail;
58 * How long until we give up on connecting the peers?
60 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
70 * Total number of peers in the test.
72 static unsigned long long num_peers;
75 * Global configuration file
77 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
80 * Total number of currently running peers.
82 static unsigned long long peers_running;
85 * Current round we are in.
87 static unsigned long long current_round;
90 * Peers desired in the next round.
92 static unsigned long long peers_next_round;
95 * Maximum number of connections to NSE services.
97 static unsigned long long connection_limit;
100 * Total number of connections in the whole network.
102 static unsigned int total_connections;
105 * The currently running peer group.
107 static struct GNUNET_TESTING_PeerGroup *pg;
110 * File to report results to.
112 static struct GNUNET_DISK_FileHandle *output_file;
115 * File to log connection info, statistics to.
117 static struct GNUNET_DISK_FileHandle *data_file;
120 * How many data points to capture before triggering next round?
122 static struct GNUNET_TIME_Relative wait_time;
125 * Task called to disconnect peers.
127 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
130 * Task called to shutdown test.
132 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
135 * Task used to churn the network.
137 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
139 static char *topology_file;
141 static char *data_filename;
143 static uint64_t clock_skew;
146 * Check whether peers successfully shut down.
149 shutdown_callback (void *cls, const char *emsg)
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "All peers successfully shut down!\n");
170 shutdown_task (void *cls,
171 const struct GNUNET_SCHEDULER_TaskContext *tc)
175 fprintf(stderr, "Ending test.\n");
178 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
180 GNUNET_SCHEDULER_cancel(disconnect_task);
181 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
183 while (NULL != (pos = peer_head))
185 if (pos->nse_handle != NULL)
186 GNUNET_NSE_disconnect(pos->nse_handle);
187 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
191 if (data_file != NULL)
192 GNUNET_DISK_file_close(data_file);
193 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
197 * Callback to call when network size estimate is updated.
200 * @param estimate the value of the current network size estimate
201 * @param std_dev standard deviation (rounded down to nearest integer)
202 * of the size estimation values seen
206 handle_estimate (void *cls, double estimate, double std_dev)
208 struct NSEPeer *peer = cls;
212 if (output_file != NULL)
214 size = GNUNET_asprintf(&output_buffer,
216 GNUNET_i2s(&peer->daemon->id),
221 if (size != GNUNET_DISK_file_write(output_file, output_buffer, size))
222 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
223 "Unable to write to file!\n");
224 GNUNET_free (output_buffer);
228 "Received network size estimate from peer %s. Size: %f std.dev. %f\n",
229 GNUNET_i2s(&peer->daemon->id),
236 connect_nse_service (void *cls,
237 const struct GNUNET_SCHEDULER_TaskContext *tc)
239 struct NSEPeer *current_peer;
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Connecting to nse service of peers\n");
246 for (i = 0; i < num_peers; i++)
248 if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "TEST_NSE_MULTIPEER: connecting to nse service of peer %d\n", i);
251 current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
252 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
253 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
255 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
258 GNUNET_assert(current_peer->nse_handle != NULL);
260 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
266 churn_peers (void *cls,
267 const struct GNUNET_SCHEDULER_TaskContext *tc);
271 * Continuation called by the "get_all" and "get" functions.
273 * @param cls struct StatsContext
274 * @param success GNUNET_OK if statistics were
275 * successfully obtained, GNUNET_SYSERR if not.
278 stats_finished_callback (void *cls, int success)
280 struct StatsContext *stats_context = (struct StatsContext *)cls;
284 if ((GNUNET_OK == success) && (data_file != NULL)) /* Stats lookup successful, write out data */
287 buf_len = GNUNET_asprintf(&buf,
288 "TOTAL_NSE_BYTES: %u\n",
289 stats_context->total_nse_bytes);
292 GNUNET_DISK_file_write(data_file, buf, buf_len);
294 GNUNET_free_non_null(buf);
297 if (stats_context->task != NULL)
298 *stats_context->task_id = GNUNET_SCHEDULER_add_now(stats_context->task,
299 stats_context->task_cls);
300 GNUNET_free(stats_context);
305 * Callback function to process statistic values.
307 * @param cls struct StatsContext
308 * @param peer the peer the statistics belong to
309 * @param subsystem name of subsystem that created the statistic
310 * @param name the name of the datum
311 * @param value the current value
312 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
313 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
316 statistics_iterator (void *cls,
317 const struct GNUNET_PeerIdentity *peer,
318 const char *subsystem,
323 struct StatsContext *stats_context = cls;
326 GNUNET_assert(0 < GNUNET_asprintf(&buf,
327 "bytes of messages of type %d received",
328 GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD));
329 if ((0 == strstr(subsystem, "core")) && (0 == strstr(name, buf)))
331 stats_context->total_nse_bytes += value;
339 * @param cls struct StatsContext
340 * @param tc task context
343 get_statistics (void *cls,
344 const struct GNUNET_SCHEDULER_TaskContext *tc)
346 struct StatsContext *stats_context = cls;
348 GNUNET_TESTING_get_statistics(pg,
349 &stats_finished_callback,
350 &statistics_iterator,
356 disconnect_nse_peers (void *cls,
357 const struct GNUNET_SCHEDULER_TaskContext *tc)
361 struct StatsContext *stats_context;
362 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "disconnecting nse service of peers\n");
367 while (NULL != (pos = peer_head))
369 if (pos->nse_handle != NULL)
371 GNUNET_NSE_disconnect(pos->nse_handle);
372 pos->nse_handle = NULL;
374 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
378 GNUNET_asprintf(&buf,
381 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg,
387 GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
388 churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
390 else /* No more rounds, let's shut it down! */
392 stats_context = GNUNET_malloc(sizeof(struct StatsContext));
393 GNUNET_SCHEDULER_cancel(shutdown_handle);
394 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
395 stats_context->task = &shutdown_task;
396 stats_context->task_cls = NULL;
397 stats_context->task_id = &shutdown_handle;
398 GNUNET_SCHEDULER_add_now(&get_statistics, stats_context);
399 //shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
406 * Prototype of a function that will be called when a
407 * particular operation was completed the testing library.
410 * @param emsg NULL on success
413 topology_output_callback (void *cls, const char *emsg)
415 struct StatsContext *stats_context;
416 stats_context = GNUNET_malloc(sizeof(struct StatsContext));
418 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL);
419 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
424 * Prototype of a function that will be called when a
425 * particular operation was completed the testing library.
428 * @param emsg NULL on success
431 churn_callback (void *cls, const char *emsg)
433 char *temp_output_file;
435 if (emsg == NULL) /* Everything is okay! */
437 peers_running = GNUNET_TESTING_daemons_running(pg);
438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
439 "Round %lu, churn finished successfully.\n",
441 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
442 GNUNET_asprintf(&temp_output_file,
446 GNUNET_TESTING_peergroup_topology_to_file(pg,
448 &topology_output_callback,
450 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
451 "Writing topology to file %s\n",
453 GNUNET_free(temp_output_file);
457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
458 "Round %lu, churn FAILED!!\n",
460 GNUNET_SCHEDULER_cancel(shutdown_handle);
461 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
467 churn_peers (void *cls,
468 const struct GNUNET_SCHEDULER_TaskContext *tc)
470 peers_running = GNUNET_TESTING_daemons_running(pg);
471 churn_task = GNUNET_SCHEDULER_NO_TASK;
472 if (peers_next_round == peers_running)
474 /* Nothing to do... */
475 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
476 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
477 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
478 &disconnect_nse_peers, NULL);
479 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
480 "Round %lu, doing nothing!\n",
485 if (peers_next_round > num_peers)
487 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
488 "Asked to turn on more peers than have!!\n");
489 GNUNET_SCHEDULER_cancel(shutdown_handle);
490 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493 "Round %lu, turning off %lu peers, turning on %lu peers!\n",
495 (peers_running > peers_next_round) ? peers_running
496 - peers_next_round : 0,
497 (peers_next_round > peers_running) ? peers_next_round
498 - peers_running : 0);
499 GNUNET_TESTING_daemons_churn (pg,
500 (peers_running > peers_next_round) ? peers_running
503 (peers_next_round > peers_running) ? peers_next_round
505 : 0, wait_time, &churn_callback,
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Peergroup callback called with error, aborting test!\n");
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
523 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Peer Group started successfully, connecting to NSE service for each peer!\n");
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Have %u connections\n", total_connections);
532 if (data_file != NULL)
535 buf_len = GNUNET_asprintf(&buf,
536 "CONNECTIONS_0: %u\n",
539 GNUNET_DISK_file_write(data_file, buf, buf_len);
542 peers_running = GNUNET_TESTING_daemons_running(pg);
543 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
544 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL);
549 * Function that will be called whenever two daemons are connected by
550 * the testing library.
553 * @param first peer id for first daemon
554 * @param second peer id for the second daemon
555 * @param distance distance between the connected peers
556 * @param first_cfg config for the first daemon
557 * @param second_cfg config for the second daemon
558 * @param first_daemon handle for the first daemon
559 * @param second_daemon handle for the second daemon
560 * @param emsg error message (NULL on success)
563 connect_cb (void *cls,
564 const struct GNUNET_PeerIdentity *first,
565 const struct GNUNET_PeerIdentity *second,
567 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
568 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
569 struct GNUNET_TESTING_Daemon *first_daemon,
570 struct GNUNET_TESTING_Daemon *second_daemon,
581 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
584 unsigned long long temp_wait;
587 testing_cfg = GNUNET_CONFIGURATION_create();
588 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(testing_cfg, cfgfile));
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
591 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
595 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
597 "num_peers", &num_peers))
599 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n");
603 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
608 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
609 "Option nse-profiler:wait_time is required!\n");
613 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
614 "nse-profiler", "connection_limit",
617 connection_limit = 0;
620 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
621 "nse-profiler", "topology_output_file",
624 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
625 "Option nse-profiler:topology_output_file is required!\n");
629 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
630 "nse-profiler", "data_output_file",
633 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
634 "Option nse-profiler:data_output_file is required!\n");
638 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (testing_cfg,
642 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
643 "Setting our clock as skewed...\n");
644 clock_skew = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
645 GNUNET_TIME_UNIT_MINUTES.rel_value);
649 data_file = GNUNET_DISK_file_open (data_filename,
650 GNUNET_DISK_OPEN_READWRITE
651 | GNUNET_DISK_OPEN_CREATE,
652 GNUNET_DISK_PERM_USER_READ |
653 GNUNET_DISK_PERM_USER_WRITE);
654 if (data_file == NULL)
655 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
656 "Failed to open %s for output!\n",
658 GNUNET_free(data_filename);
660 wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait);
662 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg,
667 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
668 | GNUNET_DISK_OPEN_CREATE,
669 GNUNET_DISK_PERM_USER_READ |
670 GNUNET_DISK_PERM_USER_WRITE);
671 if (output_file == NULL)
672 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
673 "Failed to open %s for output!\n",
676 GNUNET_free_non_null(temp_str);
678 pg = GNUNET_TESTING_peergroup_start(testing_cfg,
684 GNUNET_assert (pg != NULL);
685 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(),
693 * nse-profiler command line options
695 static struct GNUNET_GETOPT_CommandLineOption options[] = {
696 {'V', "verbose", NULL,
697 gettext_noop ("be verbose (print progress information)"),
698 0, &GNUNET_GETOPT_set_one, &verbose},
699 GNUNET_GETOPT_OPTION_END
704 main (int argc, char *argv[])
708 GNUNET_log_setup ("nse-profiler",
716 GNUNET_PROGRAM_run (argc,
717 argv, "nse-profiler", gettext_noop
718 ("Run a test of the NSE service."),
720 GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
724 /* end of nse-profiler.c */