-do not send previous round messages if we are just going online and did not setup...
[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 3
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 /**
141  * Check whether peers successfully shut down.
142  */
143 static void
144 shutdown_callback (void *cls, const char *emsg)
145 {
146   if (emsg != NULL)
147   {
148 #if VERBOSE
149     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
150 #endif
151     if (ok == 0)
152       ok = 666;
153   }
154   else
155   {
156 #if VERBOSE
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
158 #endif
159     ok = 0;
160   }
161 }
162
163
164 static void
165 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   struct NSEPeer *pos;
168
169 #if VERBOSE
170   fprintf (stderr, "Ending test.\n");
171 #endif
172
173   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
174   {
175     GNUNET_SCHEDULER_cancel (disconnect_task);
176     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
177   }
178   while (NULL != (pos = peer_head))
179   {
180     if (pos->nse_handle != NULL)
181       GNUNET_NSE_disconnect (pos->nse_handle);
182     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
183     GNUNET_free (pos);
184   }
185
186   if (data_file != NULL)
187     GNUNET_DISK_file_close (data_file);
188   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
189 }
190
191
192 /**
193  * Callback to call when network size estimate is updated.
194  *
195  * @param cls closure
196  * @param timestamp server timestamp
197  * @param estimate the value of the current network size estimate
198  * @param std_dev standard deviation (rounded down to nearest integer)
199  *                of the size estimation values seen
200  *
201  */
202 static void
203 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
204                  double estimate, double std_dev)
205 {
206   struct NSEPeer *peer = cls;
207   char *output_buffer;
208   size_t size;
209
210   if (output_file != NULL)
211   {
212     size =
213         GNUNET_asprintf (&output_buffer, "%s %llu %llu %f %f %f\n",
214                          GNUNET_i2s (&peer->daemon->id), peers_running,
215                          timestamp.abs_value,
216                          GNUNET_NSE_log_estimate_to_n (estimate), estimate,
217                          std_dev);
218     if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
219       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
220     GNUNET_free (output_buffer);
221   }
222   else
223     fprintf (stderr,
224              "Received network size estimate from peer %s. Size: %f std.dev. %f\n",
225              GNUNET_i2s (&peer->daemon->id), estimate, std_dev);
226
227 }
228
229
230 static void
231 connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
232 {
233   struct NSEPeer *current_peer;
234   unsigned int i;
235
236 #if VERBOSE
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
238 #endif
239   for (i = 0; i < num_peers; i++)
240   {
241     if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
242       continue;
243 #if VERBOSE
244     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
245                 "nse-profiler: connecting to nse service of peer %d\n", i);
246 #endif
247     current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
248     current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
249     if (GNUNET_YES ==
250         GNUNET_TESTING_test_daemon_running (GNUNET_TESTING_daemon_get (pg, i)))
251     {
252       current_peer->nse_handle =
253           GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate,
254                               current_peer);
255       GNUNET_assert (current_peer->nse_handle != NULL);
256     }
257     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
258   }
259 }
260
261
262 static void
263 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
264
265
266 /**
267  * Continuation called by the "get_all" and "get" functions.
268  *
269  * @param cls struct StatsContext
270  * @param success GNUNET_OK if statistics were
271  *        successfully obtained, GNUNET_SYSERR if not.
272  */
273 static void
274 stats_finished_callback (void *cls, int success)
275 {
276   struct StatsContext *stats_context = cls;
277   char *buf;
278   int buf_len;
279
280   if ((GNUNET_OK == success) && (data_file != NULL))
281   {
282     /* Stats lookup successful, write out data */
283     buf = NULL;
284     buf_len =
285         GNUNET_asprintf (&buf, "TOTAL_NSE_BYTES: %u\n",
286                          stats_context->total_nse_bytes);
287     if (buf_len > 0)
288     {
289       GNUNET_DISK_file_write (data_file, buf, buf_len);
290     }
291     GNUNET_free_non_null (buf);
292   }
293
294   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
295   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
296   GNUNET_free (stats_context);
297 }
298
299
300 /**
301  * Callback function to process statistic values.
302  *
303  * @param cls struct StatsContext
304  * @param peer the peer the statistics belong to
305  * @param subsystem name of subsystem that created the statistic
306  * @param name the name of the datum
307  * @param value the current value
308  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
309  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
310  */
311 static int
312 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
313                      const char *subsystem, const char *name, uint64_t value,
314                      int is_persistent)
315 {
316   struct StatsContext *stats_context = cls;
317
318   if ((0 == strstr (subsystem, "nse")) &&
319       (0 == strstr (name, "# flood messages received")))
320     stats_context->total_nse_bytes += value;
321   return GNUNET_OK;
322 }
323
324
325 static void
326 disconnect_nse_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
327 {
328   struct NSEPeer *pos;
329   char *buf;
330   struct StatsContext *stats_context;
331
332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
333   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
334   pos = peer_head;
335   while (NULL != (pos = peer_head))
336   {
337     if (pos->nse_handle != NULL)
338     {
339       GNUNET_NSE_disconnect (pos->nse_handle);
340       pos->nse_handle = NULL;
341     }
342     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
343     GNUNET_free (pos);
344   }
345
346   GNUNET_asprintf (&buf, "round%llu", current_round);
347   if (GNUNET_OK ==
348       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler", buf,
349                                              &peers_next_round))
350   {
351     current_round++;
352     GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
353     churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
354   }
355   else                          /* No more rounds, let's shut it down! */
356   {
357     stats_context = GNUNET_malloc (sizeof (struct StatsContext));
358     GNUNET_SCHEDULER_cancel (shutdown_handle);
359     shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
360     GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
361                                    &statistics_iterator, stats_context);
362   }
363   GNUNET_free (buf);
364 }
365
366
367 /**
368  * FIXME.
369  *
370  * @param cls unused
371  * @param emsg NULL on success
372  */
373 static void
374 topology_output_callback (void *cls, const char *emsg)
375 {
376   disconnect_task =
377       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
378   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
379 }
380
381
382 /**
383  * FIXME.
384  *
385  * @param cls closure
386  * @param emsg NULL on success
387  */
388 static void
389 churn_callback (void *cls, const char *emsg)
390 {
391   char *temp_output_file;
392
393   if (emsg == NULL)             /* Everything is okay! */
394   {
395     peers_running = peers_next_round;
396     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
397                 "Round %llu, churn finished successfully.\n", current_round);
398     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
399     GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
400                      current_round);
401     GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
402                                                &topology_output_callback, NULL);
403     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
404                 temp_output_file);
405     GNUNET_free (temp_output_file);
406   }
407   else
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
410                 current_round);
411     GNUNET_SCHEDULER_cancel (shutdown_handle);
412     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
413   }
414 }
415
416
417 static void
418 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
419 {
420   /* peers_running = GNUNET_TESTING_daemons_running(pg); */
421   churn_task = GNUNET_SCHEDULER_NO_TASK;
422   if (peers_next_round == peers_running)
423   {
424     /* Nothing to do... */
425     GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
426     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
427     disconnect_task =
428         GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
429     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
430                 current_round);
431   }
432   else
433   {
434     if (peers_next_round > num_peers)
435     {
436       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437                   "Asked to turn on more peers than we have!!\n");
438       GNUNET_SCHEDULER_cancel (shutdown_handle);
439       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
440     }
441     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
442                 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
443                 current_round,
444                 (peers_running >
445                  peers_next_round) ? peers_running - peers_next_round : 0,
446                 (peers_next_round >
447                  peers_running) ? peers_next_round - peers_running : 0);
448     GNUNET_TESTING_daemons_churn (pg, "nse",
449                                   (peers_running >
450                                    peers_next_round) ? peers_running -
451                                   peers_next_round : 0,
452                                   (peers_next_round >
453                                    peers_running) ? peers_next_round -
454                                   peers_running : 0, wait_time, &churn_callback,
455                                   NULL);
456   }
457 }
458
459
460 static void
461 nse_started_cb (void *cls, const char *emsg)
462 {
463   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
464   disconnect_task =
465       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
466 }
467
468
469 static void
470 my_cb (void *cls, const char *emsg)
471 {
472   char *buf;
473   int buf_len;
474
475   if (emsg != NULL)
476   {
477     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478                 "Peergroup callback called with error, aborting test!\n");
479     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
480     ok = 1;
481     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
482     return;
483   }
484 #if VERBOSE
485   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486               "Peer Group started successfully, connecting to NSE service for each peer!\n");
487 #endif
488   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
489               total_connections);
490   if (data_file != NULL)
491   {
492     buf = NULL;
493     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
494     if (buf_len > 0)
495       GNUNET_DISK_file_write (data_file, buf, buf_len);
496     GNUNET_free (buf);
497   }
498   peers_running = GNUNET_TESTING_daemons_running (pg);
499   GNUNET_TESTING_daemons_start_service (pg, "nse", wait_time, &nse_started_cb,
500                                         NULL);
501
502 }
503
504
505 /**
506  * Function that will be called whenever two daemons are connected by
507  * the testing library.
508  *
509  * @param cls closure
510  * @param first peer id for first daemon
511  * @param second peer id for the second daemon
512  * @param distance distance between the connected peers
513  * @param first_cfg config for the first daemon
514  * @param second_cfg config for the second daemon
515  * @param first_daemon handle for the first daemon
516  * @param second_daemon handle for the second daemon
517  * @param emsg error message (NULL on success)
518  */
519 static void
520 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
521             const struct GNUNET_PeerIdentity *second, uint32_t distance,
522             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
523             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
524             struct GNUNET_TESTING_Daemon *first_daemon,
525             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
526 {
527   if (emsg == NULL)
528     total_connections++;
529 }
530
531
532 static void
533 run (void *cls, char *const *args, const char *cfgfile,
534      const struct GNUNET_CONFIGURATION_Handle *cfg)
535 {
536   char *temp_str;
537   struct GNUNET_TESTING_Host *hosts;
538   char *data_filename;
539
540   ok = 1;
541   //testing_cfg = GNUNET_CONFIGURATION_create ();
542   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
543 #if VERBOSE
544   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
545   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
546                                          "use_progressbars", "YES");
547 #endif
548   if (GNUNET_OK !=
549       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
550                                              "num_peers", &num_peers))
551   {
552     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
553                 "Option TESTING:NUM_PEERS is required!\n");
554     return;
555   }
556
557   if (GNUNET_OK !=
558       GNUNET_CONFIGURATION_get_value_time (testing_cfg, "nse-profiler",
559                                            "WAIT_TIME", &wait_time))
560   {
561     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
562                 "Option nse-profiler:wait_time is required!\n");
563     return;
564   }
565
566   if (GNUNET_OK !=
567       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler",
568                                              "connection_limit",
569                                              &connection_limit))
570   {
571     connection_limit = 0;
572   }
573
574   if (GNUNET_OK !=
575       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "nse-profiler",
576                                              "topology_output_file",
577                                              &topology_file))
578   {
579     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580                 "Option nse-profiler:topology_output_file is required!\n");
581     return;
582   }
583
584   if (GNUNET_OK ==
585       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "nse-profiler",
586                                              "data_output_file",
587                                              &data_filename))
588   {
589     data_file =
590         GNUNET_DISK_file_open (data_filename,
591                                GNUNET_DISK_OPEN_READWRITE |
592                                GNUNET_DISK_OPEN_CREATE,
593                                GNUNET_DISK_PERM_USER_READ |
594                                GNUNET_DISK_PERM_USER_WRITE);
595     if (data_file == NULL)
596       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
597                   data_filename);
598     GNUNET_free (data_filename);
599   }
600
601   if (GNUNET_YES ==
602       GNUNET_CONFIGURATION_get_value_string (cfg, "nse-profiler", "output_file",
603                                              &temp_str))
604   {
605     output_file =
606         GNUNET_DISK_file_open (temp_str,
607                                GNUNET_DISK_OPEN_READWRITE |
608                                GNUNET_DISK_OPEN_CREATE,
609                                GNUNET_DISK_PERM_USER_READ |
610                                GNUNET_DISK_PERM_USER_WRITE);
611     if (output_file == NULL)
612       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
613                   temp_str);
614   }
615   GNUNET_free_non_null (temp_str);
616
617   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
618
619   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
620                                        &connect_cb, &my_cb, NULL, hosts);
621   GNUNET_assert (pg != NULL);
622   shutdown_handle =
623       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
624                                     &shutdown_task, NULL);
625 }
626
627
628
629 /**
630  * nse-profiler command line options
631  */
632 static struct GNUNET_GETOPT_CommandLineOption options[] = {
633   {'V', "verbose", NULL,
634    gettext_noop ("be verbose (print progress information)"),
635    0, &GNUNET_GETOPT_set_one, &verbose},
636   GNUNET_GETOPT_OPTION_END
637 };
638
639
640 int
641 main (int argc, char *argv[])
642 {
643   GNUNET_log_setup ("nse-profiler",
644 #if VERBOSE
645                     "DEBUG",
646 #else
647                     "WARNING",
648 #endif
649                     NULL);
650   GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
651                       gettext_noop
652                       ("Measure quality and performance of the NSE service."),
653                       options, &run, NULL);
654 #if REMOVE_DIR
655   GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
656 #endif
657   return ok;
658 }
659
660 /* end of nse-profiler.c */