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;
48 unsigned long long total_nse_bytes;
52 static struct NSEPeer *peer_head;
54 static struct NSEPeer *peer_tail;
57 * How long until we give up on connecting the peers?
59 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
69 * Total number of peers in the test.
71 static unsigned long long num_peers;
74 * Global configuration file
76 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
79 * Total number of currently running peers.
81 static unsigned long long peers_running;
84 * Current round we are in.
86 static unsigned long long current_round;
89 * Peers desired in the next round.
91 static unsigned long long peers_next_round;
94 * Maximum number of connections to NSE services.
96 static unsigned long long connection_limit;
99 * Total number of connections in the whole network.
101 static unsigned int total_connections;
104 * The currently running peer group.
106 static struct GNUNET_TESTING_PeerGroup *pg;
109 * File to report results to.
111 static struct GNUNET_DISK_FileHandle *output_file;
114 * File to log connection info, statistics to.
116 static struct GNUNET_DISK_FileHandle *data_file;
119 * How many data points to capture before triggering next round?
121 static struct GNUNET_TIME_Relative wait_time;
124 * Task called to disconnect peers.
126 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
129 * Task called to shutdown test.
131 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
134 * Task used to churn the network.
136 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
138 static char *topology_file;
140 static char *data_filename;
142 static uint64_t clock_skew;
145 * Check whether peers successfully shut down.
148 shutdown_callback (void *cls, const char *emsg)
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "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);
198 * Callback to call when network size estimate is updated.
201 * @param timestamp server timestamp
202 * @param estimate the value of the current network size estimate
203 * @param std_dev standard deviation (rounded down to nearest integer)
204 * of the size estimation values seen
208 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp, double estimate, double std_dev)
210 struct NSEPeer *peer = cls;
214 if (output_file != NULL)
216 size = GNUNET_asprintf(&output_buffer,
217 "%s %llu %llu %f %f %f\n",
218 GNUNET_i2s(&peer->daemon->id),
221 GNUNET_NSE_log_estimate_to_n(estimate),
224 if (size != GNUNET_DISK_file_write(output_file, output_buffer, size))
225 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
226 "Unable to write to file!\n");
227 GNUNET_free (output_buffer);
231 "Received network size estimate from peer %s. Size: %f std.dev. %f\n",
232 GNUNET_i2s(&peer->daemon->id),
240 connect_nse_service (void *cls,
241 const struct GNUNET_SCHEDULER_TaskContext *tc)
243 struct NSEPeer *current_peer;
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Connecting to nse service of peers\n");
250 for (i = 0; i < num_peers; i++)
252 if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "nse-profiler: connecting to nse service of peer %d\n", i);
257 current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
258 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
259 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
261 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
264 GNUNET_assert(current_peer->nse_handle != NULL);
266 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
272 churn_peers (void *cls,
273 const struct GNUNET_SCHEDULER_TaskContext *tc);
277 * Continuation called by the "get_all" and "get" functions.
279 * @param cls struct StatsContext
280 * @param success GNUNET_OK if statistics were
281 * successfully obtained, GNUNET_SYSERR if not.
284 stats_finished_callback (void *cls, int success)
286 struct StatsContext *stats_context = cls;
290 if ( (GNUNET_OK == success) &&
291 (data_file != NULL) )
293 /* Stats lookup successful, write out data */
295 buf_len = GNUNET_asprintf(&buf,
296 "TOTAL_NSE_BYTES: %u\n",
297 stats_context->total_nse_bytes);
300 GNUNET_DISK_file_write(data_file, buf, buf_len);
302 GNUNET_free_non_null(buf);
305 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
306 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
307 GNUNET_free(stats_context);
312 * Callback function to process statistic values.
314 * @param cls struct StatsContext
315 * @param peer the peer the statistics belong to
316 * @param subsystem name of subsystem that created the statistic
317 * @param name the name of the datum
318 * @param value the current value
319 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
320 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
323 statistics_iterator (void *cls,
324 const struct GNUNET_PeerIdentity *peer,
325 const char *subsystem,
330 struct StatsContext *stats_context = cls;
332 if ( (0 == strstr(subsystem, "nse")) &&
333 (0 == strstr(name, "# flood messages received")) )
334 stats_context->total_nse_bytes += value;
340 disconnect_nse_peers (void *cls,
341 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 struct StatsContext *stats_context;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "disconnecting nse service of peers\n");
349 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
351 while (NULL != (pos = peer_head))
353 if (pos->nse_handle != NULL)
355 GNUNET_NSE_disconnect(pos->nse_handle);
356 pos->nse_handle = NULL;
358 GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
362 GNUNET_asprintf(&buf,
365 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg,
371 GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
372 churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
374 else /* No more rounds, let's shut it down! */
376 stats_context = GNUNET_malloc(sizeof(struct StatsContext));
377 GNUNET_SCHEDULER_cancel(shutdown_handle);
378 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
379 GNUNET_TESTING_get_statistics(pg,
380 &stats_finished_callback,
381 &statistics_iterator,
392 * @param emsg NULL on success
395 topology_output_callback (void *cls, const char *emsg)
397 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
398 &disconnect_nse_peers, NULL);
399 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
407 * @param emsg NULL on success
410 churn_callback (void *cls, const char *emsg)
412 char *temp_output_file;
414 if (emsg == NULL) /* Everything is okay! */
416 peers_running = peers_next_round;
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418 "Round %llu, churn finished successfully.\n",
420 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
421 GNUNET_asprintf(&temp_output_file,
425 GNUNET_TESTING_peergroup_topology_to_file(pg,
427 &topology_output_callback,
429 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
430 "Writing topology to file %s\n",
432 GNUNET_free(temp_output_file);
436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
437 "Round %llu, churn FAILED!!\n",
439 GNUNET_SCHEDULER_cancel(shutdown_handle);
440 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
446 churn_peers (void *cls,
447 const struct GNUNET_SCHEDULER_TaskContext *tc)
449 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
450 churn_task = GNUNET_SCHEDULER_NO_TASK;
451 if (peers_next_round == peers_running)
453 /* Nothing to do... */
454 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
455 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
456 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
457 &disconnect_nse_peers, NULL);
458 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
459 "Round %lu, doing nothing!\n",
464 if (peers_next_round > num_peers)
466 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
467 "Asked to turn on more peers than we have!!\n");
468 GNUNET_SCHEDULER_cancel(shutdown_handle);
469 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
472 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
474 (peers_running > peers_next_round)
475 ? peers_running - peers_next_round
477 (peers_next_round > peers_running)
478 ? peers_next_round - peers_running
480 GNUNET_TESTING_daemons_churn (pg, "nse",
481 (peers_running > peers_next_round)
482 ? peers_running - peers_next_round
484 (peers_next_round > peers_running)
485 ? peers_next_round - peers_running
488 &churn_callback, NULL);
494 nse_started_cb(void *cls, const char *emsg)
496 GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
497 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_nse_peers, NULL);
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Peergroup callback called with error, aborting test!\n");
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
513 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Peer Group started successfully, connecting to NSE service for each peer!\n");
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
521 "Have %u connections\n",
523 if (data_file != NULL)
526 buf_len = GNUNET_asprintf(&buf,
527 "CONNECTIONS_0: %u\n",
530 GNUNET_DISK_file_write(data_file, buf, buf_len);
533 peers_running = GNUNET_TESTING_daemons_running(pg);
534 GNUNET_TESTING_daemons_start_service (pg,
544 * Function that will be called whenever two daemons are connected by
545 * the testing library.
548 * @param first peer id for first daemon
549 * @param second peer id for the second daemon
550 * @param distance distance between the connected peers
551 * @param first_cfg config for the first daemon
552 * @param second_cfg config for the second daemon
553 * @param first_daemon handle for the first daemon
554 * @param second_daemon handle for the second daemon
555 * @param emsg error message (NULL on success)
558 connect_cb (void *cls,
559 const struct GNUNET_PeerIdentity *first,
560 const struct GNUNET_PeerIdentity *second,
562 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
563 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
564 struct GNUNET_TESTING_Daemon *first_daemon,
565 struct GNUNET_TESTING_Daemon *second_daemon,
576 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
579 unsigned long long temp_wait;
580 struct GNUNET_TESTING_Host *hosts;
583 testing_cfg = GNUNET_CONFIGURATION_create();
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
586 GNUNET_CONFIGURATION_set_value_string (testing_cfg,
591 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
593 "num_peers", &num_peers))
595 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n");
599 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
604 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
605 "Option nse-profiler:wait_time is required!\n");
609 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
610 "nse-profiler", "connection_limit",
613 connection_limit = 0;
616 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
617 "nse-profiler", "topology_output_file",
620 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
621 "Option nse-profiler:topology_output_file is required!\n");
625 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
626 "nse-profiler", "data_output_file",
629 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
630 "Option nse-profiler:data_output_file is required!\n");
634 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (testing_cfg,
638 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
639 "Setting our clock as skewed...\n");
640 clock_skew = GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK,
641 GNUNET_TIME_UNIT_MINUTES.rel_value);
645 data_file = GNUNET_DISK_file_open (data_filename,
646 GNUNET_DISK_OPEN_READWRITE
647 | GNUNET_DISK_OPEN_CREATE,
648 GNUNET_DISK_PERM_USER_READ |
649 GNUNET_DISK_PERM_USER_WRITE);
650 if (data_file == NULL)
651 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
652 "Failed to open %s for output!\n",
654 GNUNET_free(data_filename);
656 wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait);
658 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg,
663 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
664 | GNUNET_DISK_OPEN_CREATE,
665 GNUNET_DISK_PERM_USER_READ |
666 GNUNET_DISK_PERM_USER_WRITE);
667 if (output_file == NULL)
668 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
669 "Failed to open %s for output!\n",
672 GNUNET_free_non_null(temp_str);
674 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
676 pg = GNUNET_TESTING_peergroup_start(testing_cfg,
682 GNUNET_assert (pg != NULL);
683 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(),
691 * nse-profiler command line options
693 static struct GNUNET_GETOPT_CommandLineOption options[] = {
694 {'V', "verbose", NULL,
695 gettext_noop ("be verbose (print progress information)"),
696 0, &GNUNET_GETOPT_set_one, &verbose},
697 GNUNET_GETOPT_OPTION_END
702 main (int argc, char *argv[])
704 GNUNET_log_setup ("nse-profiler",
711 GNUNET_PROGRAM_run (argc,
712 argv, "nse-profiler",
713 gettext_noop ("Measure quality and performance of the NSE service."),
717 GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
722 /* end of nse-profiler.c */