0df6ddf7d623b76d42b77825ba5a19bd164d71f2
[oweals/gnunet.git] / src / nse / gnunet-nse-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file nse/gnunet-nse-profiler.c
22  *
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.
27  */
28 #include "platform.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_nse_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 struct NSEPeer
35 {
36   struct NSEPeer *prev;
37
38   struct NSEPeer *next;
39
40   struct GNUNET_TESTING_Daemon *daemon;
41
42   struct GNUNET_NSE_Handle *nse_handle;
43 };
44
45
46 struct StatsContext
47 {
48   unsigned long long total_nse_bytes;
49 };
50
51
52 static struct NSEPeer *peer_head;
53
54 static struct NSEPeer *peer_tail;
55
56 /**
57  * How long until we give up on connecting the peers?
58  */
59 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
60
61 static int ok;
62
63 /**
64  * Be verbose
65  */
66 static int verbose;
67
68 /**
69  * Total number of peers in the test.
70  */
71 static unsigned long long num_peers;
72
73 /**
74  * Global configuration file
75  */
76 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
77
78 /**
79  * Total number of currently running peers.
80  */
81 static unsigned long long peers_running;
82
83 /**
84  * Current round we are in.
85  */
86 static unsigned long long current_round;
87
88 /**
89  * Peers desired in the next round.
90  */
91 static unsigned long long peers_next_round;
92
93 /**
94  * Maximum number of connections to NSE services.
95  */
96 static unsigned long long connection_limit;
97
98 /**
99  * Total number of connections in the whole network.
100  */
101 static unsigned int total_connections;
102
103 /**
104  * The currently running peer group.
105  */
106 static struct GNUNET_TESTING_PeerGroup *pg;
107
108 /**
109  * File to report results to.
110  */
111 static struct GNUNET_DISK_FileHandle *output_file;
112
113 /**
114  * File to log connection info, statistics to.
115  */
116 static struct GNUNET_DISK_FileHandle *data_file;
117
118 /**
119  * How many data points to capture before triggering next round?
120  */
121 static struct GNUNET_TIME_Relative wait_time;
122
123 /**
124  * Task called to disconnect peers.
125  */
126 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
127
128 /**
129  * Task called to shutdown test.
130  */
131 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
132
133 /**
134  * Task used to churn the network.
135  */
136 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
137
138 static char *topology_file;
139
140 static char *data_filename;
141
142 static uint64_t clock_skew;
143
144 /**
145  * Check whether peers successfully shut down.
146  */
147 static void
148 shutdown_callback (void *cls, const char *emsg)
149 {
150   if (emsg != NULL)
151   {
152 #if VERBOSE
153     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
154 #endif
155     if (ok == 0)
156       ok = 666;
157   }
158   else
159   {
160 #if VERBOSE
161     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
162 #endif
163     ok = 0;
164   }
165 }
166
167
168 static void
169 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171   struct NSEPeer *pos;
172
173 #if VERBOSE
174   fprintf (stderr, "Ending test.\n");
175 #endif
176
177   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
178   {
179     GNUNET_SCHEDULER_cancel (disconnect_task);
180     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
181   }
182   while (NULL != (pos = peer_head))
183   {
184     if (pos->nse_handle != NULL)
185       GNUNET_NSE_disconnect (pos->nse_handle);
186     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
187     GNUNET_free (pos);
188   }
189
190   if (data_file != NULL)
191     GNUNET_DISK_file_close (data_file);
192   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
193 }
194
195
196 /**
197  * Callback to call when network size estimate is updated.
198  *
199  * @param cls closure
200  * @param timestamp server timestamp
201  * @param estimate the value of the current network size estimate
202  * @param std_dev standard deviation (rounded down to nearest integer)
203  *                of the size estimation values seen
204  *
205  */
206 static void
207 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
208                  double estimate, double std_dev)
209 {
210   struct NSEPeer *peer = cls;
211   char *output_buffer;
212   size_t size;
213
214   if (output_file != NULL)
215   {
216     size =
217         GNUNET_asprintf (&output_buffer, "%s %llu %llu %f %f %f\n",
218                          GNUNET_i2s (&peer->daemon->id), peers_running,
219                          timestamp.abs_value,
220                          GNUNET_NSE_log_estimate_to_n (estimate), estimate,
221                          std_dev);
222     if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
223       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
224     GNUNET_free (output_buffer);
225   }
226   else
227     fprintf (stderr,
228              "Received network size estimate from peer %s. Size: %f std.dev. %f\n",
229              GNUNET_i2s (&peer->daemon->id), estimate, std_dev);
230
231 }
232
233
234 static void
235 connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
236 {
237   struct NSEPeer *current_peer;
238   unsigned int i;
239
240 #if VERBOSE
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
242 #endif
243   for (i = 0; i < num_peers; i++)
244   {
245     if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
246       continue;
247 #if VERBOSE
248     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
249                 "nse-profiler: connecting to nse service of peer %d\n", i);
250 #endif
251     current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
252     current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
253     if (GNUNET_YES ==
254         GNUNET_TESTING_daemon_running (GNUNET_TESTING_daemon_get (pg, i)))
255     {
256       current_peer->nse_handle =
257           GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate,
258                               current_peer);
259       GNUNET_assert (current_peer->nse_handle != NULL);
260     }
261     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
262   }
263 }
264
265
266 static void
267 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
268
269
270 /**
271  * Continuation called by the "get_all" and "get" functions.
272  *
273  * @param cls struct StatsContext
274  * @param success GNUNET_OK if statistics were
275  *        successfully obtained, GNUNET_SYSERR if not.
276  */
277 static void
278 stats_finished_callback (void *cls, int success)
279 {
280   struct StatsContext *stats_context = cls;
281   char *buf;
282   int buf_len;
283
284   if ((GNUNET_OK == success) && (data_file != NULL))
285   {
286     /* Stats lookup successful, write out data */
287     buf = NULL;
288     buf_len =
289         GNUNET_asprintf (&buf, "TOTAL_NSE_BYTES: %u\n",
290                          stats_context->total_nse_bytes);
291     if (buf_len > 0)
292     {
293       GNUNET_DISK_file_write (data_file, buf, buf_len);
294     }
295     GNUNET_free_non_null (buf);
296   }
297
298   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
299   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
300   GNUNET_free (stats_context);
301 }
302
303
304 /**
305  * Callback function to process statistic values.
306  *
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
314  */
315 static int
316 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
317                      const char *subsystem, const char *name, uint64_t value,
318                      int is_persistent)
319 {
320   struct StatsContext *stats_context = cls;
321
322   if ((0 == strstr (subsystem, "nse")) &&
323       (0 == strstr (name, "# flood messages received")))
324     stats_context->total_nse_bytes += value;
325   return GNUNET_OK;
326 }
327
328
329 static void
330 disconnect_nse_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 {
332   struct NSEPeer *pos;
333   char *buf;
334   struct StatsContext *stats_context;
335
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
337   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
338   pos = peer_head;
339   while (NULL != (pos = peer_head))
340   {
341     if (pos->nse_handle != NULL)
342     {
343       GNUNET_NSE_disconnect (pos->nse_handle);
344       pos->nse_handle = NULL;
345     }
346     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
347     GNUNET_free (pos);
348   }
349
350   GNUNET_asprintf (&buf, "round%llu", current_round);
351   if (GNUNET_OK ==
352       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler", buf,
353                                              &peers_next_round))
354   {
355     current_round++;
356     GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
357     churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
358   }
359   else                          /* No more rounds, let's shut it down! */
360   {
361     stats_context = GNUNET_malloc (sizeof (struct StatsContext));
362     GNUNET_SCHEDULER_cancel (shutdown_handle);
363     shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
364     GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
365                                    &statistics_iterator, stats_context);
366   }
367   GNUNET_free (buf);
368 }
369
370
371 /**
372  * FIXME.
373  *
374  * @param cls unused
375  * @param emsg NULL on success
376  */
377 static void
378 topology_output_callback (void *cls, const char *emsg)
379 {
380   disconnect_task =
381       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
382   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
383 }
384
385
386 /**
387  * FIXME.
388  *
389  * @param cls closure
390  * @param emsg NULL on success
391  */
392 static void
393 churn_callback (void *cls, const char *emsg)
394 {
395   char *temp_output_file;
396
397   if (emsg == NULL)             /* Everything is okay! */
398   {
399     peers_running = peers_next_round;
400     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
401                 "Round %llu, churn finished successfully.\n", current_round);
402     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
403     GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
404                      current_round);
405     GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
406                                                &topology_output_callback, NULL);
407     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
408                 temp_output_file);
409     GNUNET_free (temp_output_file);
410   }
411   else
412   {
413     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
414                 current_round);
415     GNUNET_SCHEDULER_cancel (shutdown_handle);
416     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
417   }
418 }
419
420
421 static void
422 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
423 {
424   /* peers_running = GNUNET_TESTING_daemons_running(pg); */
425   churn_task = GNUNET_SCHEDULER_NO_TASK;
426   if (peers_next_round == peers_running)
427   {
428     /* Nothing to do... */
429     GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
430     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
431     disconnect_task =
432         GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
433     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
434                 current_round);
435   }
436   else
437   {
438     if (peers_next_round > num_peers)
439     {
440       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441                   "Asked to turn on more peers than we have!!\n");
442       GNUNET_SCHEDULER_cancel (shutdown_handle);
443       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
444     }
445     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
446                 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
447                 current_round,
448                 (peers_running >
449                  peers_next_round) ? peers_running - peers_next_round : 0,
450                 (peers_next_round >
451                  peers_running) ? peers_next_round - peers_running : 0);
452     GNUNET_TESTING_daemons_churn (pg, "nse",
453                                   (peers_running >
454                                    peers_next_round) ? peers_running -
455                                   peers_next_round : 0,
456                                   (peers_next_round >
457                                    peers_running) ? peers_next_round -
458                                   peers_running : 0, wait_time, &churn_callback,
459                                   NULL);
460   }
461 }
462
463
464 static void
465 nse_started_cb (void *cls, const char *emsg)
466 {
467   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
468   disconnect_task =
469       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
470 }
471
472
473 static void
474 my_cb (void *cls, const char *emsg)
475 {
476   char *buf;
477   int buf_len;
478
479   if (emsg != NULL)
480   {
481     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482                 "Peergroup callback called with error, aborting test!\n");
483     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
484     ok = 1;
485     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
486     return;
487   }
488 #if VERBOSE
489   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490               "Peer Group started successfully, connecting to NSE service for each peer!\n");
491 #endif
492   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
493               total_connections);
494   if (data_file != NULL)
495   {
496     buf = NULL;
497     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
498     if (buf_len > 0)
499       GNUNET_DISK_file_write (data_file, buf, buf_len);
500     GNUNET_free (buf);
501   }
502   peers_running = GNUNET_TESTING_daemons_running (pg);
503   GNUNET_TESTING_daemons_start_service (pg, "nse", wait_time, &nse_started_cb,
504                                         NULL);
505
506 }
507
508
509 /**
510  * Function that will be called whenever two daemons are connected by
511  * the testing library.
512  *
513  * @param cls closure
514  * @param first peer id for first daemon
515  * @param second peer id for the second daemon
516  * @param distance distance between the connected peers
517  * @param first_cfg config for the first daemon
518  * @param second_cfg config for the second daemon
519  * @param first_daemon handle for the first daemon
520  * @param second_daemon handle for the second daemon
521  * @param emsg error message (NULL on success)
522  */
523 static void
524 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
525             const struct GNUNET_PeerIdentity *second, uint32_t distance,
526             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
527             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
528             struct GNUNET_TESTING_Daemon *first_daemon,
529             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
530 {
531   if (emsg == NULL)
532     total_connections++;
533 }
534
535
536 static void
537 run (void *cls, char *const *args, const char *cfgfile,
538      const struct GNUNET_CONFIGURATION_Handle *cfg)
539 {
540   char *temp_str;
541   unsigned long long temp_wait;
542   struct GNUNET_TESTING_Host *hosts;
543
544   ok = 1;
545   testing_cfg = GNUNET_CONFIGURATION_create ();
546 #if VERBOSE
547   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
548   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
549                                          "use_progressbars", "YES");
550 #endif
551   if (GNUNET_OK !=
552       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
553                                              "num_peers", &num_peers))
554   {
555     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
556                 "Option TESTING:NUM_PEERS is required!\n");
557     return;
558   }
559
560   if (GNUNET_OK !=
561       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler",
562                                              "wait_time", &temp_wait))
563   {
564     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
565                 "Option nse-profiler:wait_time is required!\n");
566     return;
567   }
568
569   if (GNUNET_OK !=
570       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler",
571                                              "connection_limit",
572                                              &connection_limit))
573   {
574     connection_limit = 0;
575   }
576
577   if (GNUNET_OK !=
578       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "nse-profiler",
579                                              "topology_output_file",
580                                              &topology_file))
581   {
582     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583                 "Option nse-profiler:topology_output_file is required!\n");
584     return;
585   }
586
587   if (GNUNET_OK !=
588       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "nse-profiler",
589                                              "data_output_file",
590                                              &data_filename))
591   {
592     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
593                 "Option nse-profiler:data_output_file is required!\n");
594     return;
595   }
596
597   if (GNUNET_YES ==
598       GNUNET_CONFIGURATION_get_value_yesno (testing_cfg, "nse-profiler",
599                                             "skew_clock"))
600   {
601     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Setting our clock as skewed...\n");
602     clock_skew =
603         GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
604                                   GNUNET_TIME_UNIT_MINUTES.rel_value);
605   }
606
607
608   data_file =
609       GNUNET_DISK_file_open (data_filename,
610                              GNUNET_DISK_OPEN_READWRITE |
611                              GNUNET_DISK_OPEN_CREATE,
612                              GNUNET_DISK_PERM_USER_READ |
613                              GNUNET_DISK_PERM_USER_WRITE);
614   if (data_file == NULL)
615     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
616                 data_filename);
617   GNUNET_free (data_filename);
618
619   wait_time =
620       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
621
622   if (GNUNET_YES ==
623       GNUNET_CONFIGURATION_get_value_string (cfg, "nse-profiler", "output_file",
624                                              &temp_str))
625   {
626     output_file =
627         GNUNET_DISK_file_open (temp_str,
628                                GNUNET_DISK_OPEN_READWRITE |
629                                GNUNET_DISK_OPEN_CREATE,
630                                GNUNET_DISK_PERM_USER_READ |
631                                GNUNET_DISK_PERM_USER_WRITE);
632     if (output_file == NULL)
633       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
634                   temp_str);
635   }
636   GNUNET_free_non_null (temp_str);
637
638   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
639
640   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
641                                        &connect_cb, &my_cb, NULL, hosts);
642   GNUNET_assert (pg != NULL);
643   shutdown_handle =
644       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
645                                     &shutdown_task, NULL);
646 }
647
648
649
650 /**
651  * nse-profiler command line options
652  */
653 static struct GNUNET_GETOPT_CommandLineOption options[] = {
654   {'V', "verbose", NULL,
655    gettext_noop ("be verbose (print progress information)"),
656    0, &GNUNET_GETOPT_set_one, &verbose},
657   GNUNET_GETOPT_OPTION_END
658 };
659
660
661 int
662 main (int argc, char *argv[])
663 {
664   GNUNET_log_setup ("nse-profiler",
665 #if VERBOSE
666                     "DEBUG",
667 #else
668                     "WARNING",
669 #endif
670                     NULL);
671   GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
672                       gettext_noop
673                       ("Measure quality and performance of the NSE service."),
674                       options, &run, NULL);
675 #if REMOVE_DIR
676   GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
677 #endif
678   return ok;
679 }
680
681 /* end of nse-profiler.c */