4a4bea5eddb312306272aa8ea8bbc7bf6133b8fd
[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   struct GNUNET_STATISTICS_Handle *stats;
45   
46   GNUNET_SCHEDULER_TaskIdentifier stats_task;
47 };
48
49
50 struct StatsContext
51 {
52   /**
53    * Whether or not shoutdown after finishing.
54    */
55   int shutdown;
56
57   /**
58    * How many messages have peers received during the test.
59    */
60   unsigned long long total_nse_received_messages;
61
62   /**
63    * How many messages have peers send during the test (should be == received).
64    */
65   unsigned long long total_nse_transmitted_messages;
66
67   /**
68    * How many messages have travelled an edge in both directions.
69    */
70   unsigned long long total_nse_cross;
71
72   /**
73    * How many extra messages per edge (corrections) have been received.
74    */
75   unsigned long long total_nse_extra;
76
77   /**
78    * How many messages have been discarded.
79    */
80   unsigned long long total_discarded;
81 };
82
83
84 static struct NSEPeer *peer_head;
85
86 static struct NSEPeer *peer_tail;
87
88 /**
89  * How long until we give up on connecting the peers?
90  */
91 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
92
93 static int ok;
94
95 /**
96  * Be verbose
97  */
98 static int verbose;
99
100 /**
101  * Total number of peers in the test.
102  */
103 static unsigned long long num_peers;
104
105 /**
106  * Global configuration file
107  */
108 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
109
110 /**
111  * Total number of currently running peers.
112  */
113 static unsigned long long peers_running;
114
115 /**
116  * Current round we are in.
117  */
118 static unsigned long long current_round;
119
120 /**
121  * Peers desired in the next round.
122  */
123 static unsigned long long peers_next_round;
124
125 /**
126  * Maximum number of connections to NSE services.
127  */
128 static unsigned long long connection_limit;
129
130 /**
131  * Total number of connections in the whole network.
132  */
133 static unsigned int total_connections;
134
135 /**
136  * The currently running peer group.
137  */
138 static struct GNUNET_TESTING_PeerGroup *pg;
139
140 /**
141  * File to report results to.
142  */
143 static struct GNUNET_DISK_FileHandle *output_file;
144
145 /**
146  * File to log connection info, statistics to.
147  */
148 static struct GNUNET_DISK_FileHandle *data_file;
149
150 /**
151  * How many data points to capture before triggering next round?
152  */
153 static struct GNUNET_TIME_Relative wait_time;
154
155 /**
156  * NSE interval.
157  */
158 static struct GNUNET_TIME_Relative interval;
159
160 /**
161  * Task called to disconnect peers.
162  */
163 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
164
165 /**
166  * Task called to shutdown test.
167  */
168 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
169
170 /**
171  * Task used to churn the network.
172  */
173 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
174
175 static char *topology_file;
176
177 /**
178  * Check whether peers successfully shut down.
179  */
180 static void
181 shutdown_callback (void *cls, const char *emsg)
182 {
183   if (emsg != NULL)
184   {
185 #if VERBOSE
186     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
187 #endif
188     if (ok == 0)
189       ok = 666;
190   }
191   else
192   {
193 #if VERBOSE
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
195 #endif
196     ok = 0;
197   }
198 }
199
200
201 static void
202 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 {
204   struct NSEPeer *pos;
205
206 #if VERBOSE
207   FPRINTF (stderr, "%s",  "Ending test.\n");
208 #endif
209
210   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
211   {
212     GNUNET_SCHEDULER_cancel (disconnect_task);
213     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
214   }
215   while (NULL != (pos = peer_head))
216   {
217     if (pos->nse_handle != NULL)
218       GNUNET_NSE_disconnect (pos->nse_handle);
219     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
220     if (GNUNET_SCHEDULER_NO_TASK != pos->stats_task)
221     {
222       GNUNET_SCHEDULER_cancel (pos->stats_task);
223       if (NULL != pos->stats)
224         GNUNET_STATISTICS_destroy(pos->stats, GNUNET_NO);
225     }
226     GNUNET_free (pos);
227   }
228
229   if (data_file != NULL)
230     GNUNET_DISK_file_close (data_file);
231   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
232 }
233
234
235 /**
236  * Callback to call when network size estimate is updated.
237  *
238  * @param cls closure
239  * @param timestamp server timestamp
240  * @param estimate the value of the current network size estimate
241  * @param std_dev standard deviation (rounded down to nearest integer)
242  *                of the size estimation values seen
243  *
244  */
245 static void
246 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
247                  double estimate, double std_dev)
248 {
249   struct NSEPeer *peer = cls;
250   char *output_buffer;
251   size_t size;
252
253   if (output_file != NULL)
254   {
255     size =
256         GNUNET_asprintf (&output_buffer, "%s %llu %llu %f %f %f\n",
257                          GNUNET_i2s (&peer->daemon->id), peers_running,
258                          timestamp.abs_value,
259                          GNUNET_NSE_log_estimate_to_n (estimate), estimate,
260                          std_dev);
261     if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
262       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
263     GNUNET_free (output_buffer);
264   }
265   else
266     FPRINTF (stderr,
267              "Received network size estimate from peer %s. Size: %f std.dev. %f\n",
268              GNUNET_i2s (&peer->daemon->id), estimate, std_dev);
269
270 }
271
272 /**
273  * Process core statistic values.
274  *
275  * @param cls closure
276  * @param subsystem name of subsystem that created the statistic
277  * @param name the name of the datum
278  * @param value the current value
279  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
280  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
281  */
282 static int
283 core_stats_iterator (void *cls, const char *subsystem, const char *name,
284                      uint64_t value, int is_persistent)
285 {
286   struct NSEPeer *peer = cls;
287   char *output_buffer;
288   size_t size;
289
290   if (output_file != NULL)
291   {
292     size =
293         GNUNET_asprintf (&output_buffer, "%s [%s] %s %llu\n",
294                           GNUNET_i2s (&peer->daemon->id),
295                          subsystem, name, value);
296     if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
297       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
298     GNUNET_free (output_buffer);
299   }
300   else
301     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
302                 "%s -> %s [%s]: %llu\n",
303                 GNUNET_i2s (&peer->daemon->id), subsystem, name, value);
304
305   return GNUNET_OK;
306 }
307
308 /**
309  * Continuation called by "get_stats" function.
310  *
311  * @param cls closure
312  * @param success GNUNET_OK if statistics were
313  *        successfully obtained, GNUNET_SYSERR if not.
314  */
315 static void
316 core_stats_cont (void *cls, int success);
317
318 static void
319 core_get_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
320 {
321   struct NSEPeer *peer = cls;
322
323   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
324   {
325     GNUNET_STATISTICS_destroy(peer->stats, GNUNET_NO);
326     peer->stats = NULL;
327     return;
328   }
329   else
330   {
331     GNUNET_STATISTICS_get(peer->stats, "core", NULL,
332                           GNUNET_TIME_UNIT_FOREVER_REL,
333                           &core_stats_cont, &core_stats_iterator, peer);
334     GNUNET_STATISTICS_get(peer->stats, "transport", NULL,
335                           GNUNET_TIME_UNIT_FOREVER_REL,
336                           NULL, &core_stats_iterator, peer);
337     GNUNET_STATISTICS_get(peer->stats, "nse", NULL,
338                           GNUNET_TIME_UNIT_FOREVER_REL,
339                           NULL, &core_stats_iterator, peer);
340   }
341   peer->stats_task = GNUNET_SCHEDULER_NO_TASK;
342 }
343
344 /**
345  * Continuation called by "get_stats" function.
346  *
347  * @param cls closure
348  * @param success GNUNET_OK if statistics were
349  *        successfully obtained, GNUNET_SYSERR if not.
350  */
351 static void
352 core_stats_cont (void *cls, int success)
353 {
354   struct NSEPeer *peer = cls;
355   peer->stats_task = GNUNET_SCHEDULER_add_delayed (interval, &core_get_stats,
356                                                    peer);
357 }
358
359
360 /**
361  *
362  */
363 static void
364 connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
365 {
366   struct NSEPeer *current_peer;
367   unsigned int i;
368
369 #if VERBOSE
370   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
371 #endif
372   for (i = 0; i < num_peers; i++)
373   {
374     if ((connection_limit > 0) &&
375         (num_peers > connection_limit) && 
376         (i % (num_peers / connection_limit) != 0))
377       continue;
378 #if VERBOSE
379     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
380                 "nse-profiler: connecting to nse service of peer %d\n", i);
381 #endif
382     current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
383     current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
384     if (GNUNET_YES ==
385         GNUNET_TESTING_test_daemon_running (GNUNET_TESTING_daemon_get (pg, i)))
386     {
387       current_peer->nse_handle =
388           GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate,
389                               current_peer);
390       GNUNET_assert (current_peer->nse_handle != NULL);
391     }
392     current_peer->stats = GNUNET_STATISTICS_create("profiler", current_peer->daemon->cfg);
393     GNUNET_STATISTICS_get(current_peer->stats, "core", NULL,
394                           GNUNET_TIME_UNIT_FOREVER_REL,
395                           &core_stats_cont, &core_stats_iterator, current_peer);
396     GNUNET_STATISTICS_get(current_peer->stats, "transport", NULL,
397                           GNUNET_TIME_UNIT_FOREVER_REL,
398                           NULL, &core_stats_iterator, current_peer);
399     GNUNET_STATISTICS_get(current_peer->stats, "nse", NULL,
400                           GNUNET_TIME_UNIT_FOREVER_REL,
401                           NULL, &core_stats_iterator, current_peer);
402     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
403   }
404 }
405
406
407 static void
408 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
409
410
411 /**
412  * Continuation called by the "get_all" and "get" functions.
413  *
414  * @param cls struct StatsContext
415  * @param success GNUNET_OK if statistics were
416  *        successfully obtained, GNUNET_SYSERR if not.
417  */
418 static void
419 stats_finished_callback (void *cls, int success)
420 {
421   struct StatsContext *stats_context = cls;
422   char *buf;
423   int buf_len;
424
425   if ((GNUNET_OK == success) && (data_file != NULL))
426   {
427     /* Stats lookup successful, write out data */
428     buf = NULL;
429     buf_len =
430         GNUNET_asprintf (&buf, "TOTAL_NSE_RECEIVED_MESSAGES_%d: %u \n",
431                          stats_context->shutdown, 
432                          stats_context->total_nse_received_messages);
433     if (buf_len > 0)
434     {
435       GNUNET_DISK_file_write (data_file, buf, buf_len);
436     }
437     GNUNET_free_non_null (buf);
438
439     buf = NULL;
440     buf_len =
441         GNUNET_asprintf (&buf, "TOTAL_NSE_TRANSMITTED_MESSAGES_%d: %u\n",
442                          stats_context->shutdown, 
443                          stats_context->total_nse_transmitted_messages);
444     if (buf_len > 0)
445     {
446       GNUNET_DISK_file_write (data_file, buf, buf_len);
447     }
448     GNUNET_free_non_null (buf);
449
450     buf = NULL;
451     buf_len =
452         GNUNET_asprintf (&buf, "TOTAL_NSE_CROSS_%d: %u \n",
453                          stats_context->shutdown, 
454                          stats_context->total_nse_cross);
455     if (buf_len > 0)
456     {
457       GNUNET_DISK_file_write (data_file, buf, buf_len);
458     }
459     GNUNET_free_non_null (buf);
460
461     buf = NULL;
462     buf_len =
463         GNUNET_asprintf (&buf, "TOTAL_NSE_EXTRA_%d: %u \n",
464                          stats_context->shutdown, 
465                          stats_context->total_nse_extra);
466     if (buf_len > 0)
467     {
468       GNUNET_DISK_file_write (data_file, buf, buf_len);
469     }
470     GNUNET_free_non_null (buf);
471
472     buf = NULL;
473     buf_len =
474         GNUNET_asprintf (&buf, "TOTAL_NSE_DISCARDED_%d: %u \n",
475                          stats_context->shutdown, 
476                          stats_context->total_discarded);
477     if (buf_len > 0)
478     {
479       GNUNET_DISK_file_write (data_file, buf, buf_len);
480     }
481     GNUNET_free_non_null (buf);
482
483   }
484
485   if (GNUNET_YES == stats_context->shutdown)
486   {
487     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
488     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
489   }
490   else
491   {
492     GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
493     churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
494   }
495   GNUNET_free (stats_context);
496 }
497
498
499 /**
500  * Callback function to process statistic values.
501  *
502  * @param cls struct StatsContext
503  * @param peer the peer the statistics belong to
504  * @param subsystem name of subsystem that created the statistic
505  * @param name the name of the datum
506  * @param value the current value
507  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
508  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
509  */
510 static int
511 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
512                      const char *subsystem, const char *name, uint64_t value,
513                      int is_persistent)
514 {
515   struct StatsContext *stats_context = cls;
516
517   if (0 == strcmp (subsystem, "nse"))
518   {
519     if (0 == strcmp (name, "# flood messages received"))
520     {
521       stats_context->total_nse_received_messages += value;
522 #if VERBOSE
523       if (data_file != NULL)
524       {
525         char *buf;
526         int buf_len;
527
528         buf = NULL;
529         buf_len =
530             GNUNET_asprintf (&buf, "%s %u RECEIVED\n", GNUNET_i2s(peer), value);
531         if (buf_len > 0)
532         {
533           GNUNET_DISK_file_write (data_file, buf, buf_len);
534         }
535         GNUNET_free_non_null (buf);
536       }
537 #endif
538     }
539     if (0 == strcmp (name, "# flood messages transmitted"))
540     {
541       stats_context->total_nse_transmitted_messages += value;
542 #if VERBOSE
543       if (data_file != NULL)
544       {
545         char *buf;
546         int buf_len;
547
548         buf = NULL;
549         buf_len =
550             GNUNET_asprintf (&buf, "%s %u TRANSMITTED\n", 
551                              GNUNET_i2s(peer), value);
552         if (buf_len > 0)
553         {
554           GNUNET_DISK_file_write (data_file, buf, buf_len);
555         }
556         GNUNET_free_non_null (buf);
557       }
558 #endif
559     }
560     if (0 == strcmp (name, "# cross messages"))
561     {
562       stats_context->total_nse_cross += value;
563     }
564     if (0 == strcmp (name, "# extra messages"))
565     {
566       stats_context->total_nse_extra += value;
567     }
568     if (0 == strcmp (name, "# flood messages discarded (clock skew too large)"))
569     {
570       stats_context->total_discarded += value;
571     }
572   }
573   return GNUNET_OK;
574 }
575
576
577 static void
578 disconnect_nse_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
579 {
580   struct NSEPeer *pos;
581   char *buf;
582   struct StatsContext *stats_context;
583
584   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
585   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
586   while (NULL != (pos = peer_head))
587   {
588     if (pos->nse_handle != NULL)
589     {
590       GNUNET_NSE_disconnect (pos->nse_handle);
591       pos->nse_handle = NULL;
592     }
593     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
594     if (NULL != pos->stats)
595       GNUNET_STATISTICS_destroy(pos->stats, GNUNET_NO);
596     if (GNUNET_SCHEDULER_NO_TASK != pos->stats_task)
597       GNUNET_SCHEDULER_cancel (pos->stats_task);
598     GNUNET_free (pos);
599   }
600
601   GNUNET_asprintf (&buf, "round%llu", current_round);
602   if (GNUNET_OK ==
603       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler", buf,
604                                              &peers_next_round))
605   {
606     current_round++;
607     if (current_round == 1)
608     {
609       stats_context = GNUNET_malloc (sizeof (struct StatsContext));
610       stats_context->shutdown = GNUNET_NO;
611       GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
612                                     &statistics_iterator, stats_context);
613     }
614     else
615     {
616       GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
617       churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
618     }
619   }
620   else                          /* No more rounds, let's shut it down! */
621   {
622     stats_context = GNUNET_malloc (sizeof (struct StatsContext));
623     stats_context->shutdown = GNUNET_YES;
624     GNUNET_SCHEDULER_cancel (shutdown_handle);
625     shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
626     GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
627                                    &statistics_iterator, stats_context);
628   }
629   GNUNET_free (buf);
630 }
631
632
633 /**
634  * FIXME.
635  *
636  * @param cls unused
637  * @param emsg NULL on success
638  */
639 static void
640 topology_output_callback (void *cls, const char *emsg)
641 {
642   disconnect_task =
643       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
644   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
645 }
646
647
648 /**
649  * FIXME.
650  *
651  * @param cls closure
652  * @param emsg NULL on success
653  */
654 static void
655 churn_callback (void *cls, const char *emsg)
656 {
657   char *temp_output_file;
658
659   if (emsg == NULL)             /* Everything is okay! */
660   {
661     peers_running = peers_next_round;
662     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
663                 "Round %llu, churn finished successfully.\n", current_round);
664     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
665     GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
666                      current_round);
667     GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
668                                                &topology_output_callback, NULL);
669     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
670                 temp_output_file);
671     GNUNET_free (temp_output_file);
672   }
673   else
674   {
675     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
676                 current_round);
677     GNUNET_SCHEDULER_cancel (shutdown_handle);
678     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
679   }
680 }
681
682
683 static void
684 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
685 {
686   /* peers_running = GNUNET_TESTING_daemons_running(pg); */
687   churn_task = GNUNET_SCHEDULER_NO_TASK;
688   if (peers_next_round == peers_running)
689   {
690     /* Nothing to do... */
691     GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
692     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
693     disconnect_task =
694         GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
695     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
696                 current_round);
697   }
698   else
699   {
700     if (peers_next_round > num_peers)
701     {
702       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
703                   "Asked to turn on more peers than we have!!\n");
704       GNUNET_SCHEDULER_cancel (shutdown_handle);
705       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
706     }
707     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
708                 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
709                 current_round,
710                 (peers_running >
711                  peers_next_round) ? peers_running - peers_next_round : 0,
712                 (peers_next_round >
713                  peers_running) ? peers_next_round - peers_running : 0);
714     GNUNET_TESTING_daemons_churn (pg, "nse",
715                                   (peers_running >
716                                    peers_next_round) ? peers_running -
717                                   peers_next_round : 0,
718                                   (peers_next_round >
719                                    peers_running) ? peers_next_round -
720                                   peers_running : 0, wait_time, &churn_callback,
721                                   NULL);
722   }
723 }
724
725
726 static void
727 nse_started_cb (void *cls, const char *emsg)
728 {
729   GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
730   disconnect_task =
731       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_nse_peers, NULL);
732 }
733
734
735 static void
736 my_cb (void *cls, const char *emsg)
737 {
738   char *buf;
739   int buf_len;
740
741   if (emsg != NULL)
742   {
743     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744                 "Peergroup callback called with error, aborting test!\n");
745     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
746     ok = 1;
747     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
748     return;
749   }
750 #if VERBOSE
751   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752               "Peer Group started successfully, connecting to NSE service for each peer!\n");
753 #endif
754   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
755               total_connections);
756   if (data_file != NULL)
757   {
758     buf = NULL;
759     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
760     if (buf_len > 0)
761       GNUNET_DISK_file_write (data_file, buf, buf_len);
762     GNUNET_free (buf);
763   }
764   peers_running = GNUNET_TESTING_daemons_running (pg);
765   GNUNET_TESTING_daemons_start_service (pg, "nse", wait_time, &nse_started_cb,
766                                         NULL);
767
768 }
769
770
771 /**
772  * Function that will be called whenever two daemons are connected by
773  * the testing library.
774  *
775  * @param cls closure
776  * @param first peer id for first daemon
777  * @param second peer id for the second daemon
778  * @param distance distance between the connected peers
779  * @param first_cfg config for the first daemon
780  * @param second_cfg config for the second daemon
781  * @param first_daemon handle for the first daemon
782  * @param second_daemon handle for the second daemon
783  * @param emsg error message (NULL on success)
784  */
785 static void
786 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
787             const struct GNUNET_PeerIdentity *second, uint32_t distance,
788             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
789             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
790             struct GNUNET_TESTING_Daemon *first_daemon,
791             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
792 {
793   if (emsg == NULL)
794     total_connections++;
795 }
796
797
798 static void
799 run (void *cls, char *const *args, const char *cfgfile,
800      const struct GNUNET_CONFIGURATION_Handle *cfg)
801 {
802   char *temp_str;
803   struct GNUNET_TESTING_Host *hosts;
804   char *data_filename;
805
806   ok = 1;
807   //testing_cfg = GNUNET_CONFIGURATION_create ();
808   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
809 #if VERBOSE
810   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
811   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
812                                          "use_progressbars", "YES");
813 #endif
814   if (GNUNET_OK !=
815       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
816                                              "num_peers", &num_peers))
817   {
818     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
819                 "Option TESTING:NUM_PEERS is required!\n");
820     return;
821   }
822
823   if (GNUNET_OK !=
824       GNUNET_CONFIGURATION_get_value_time (testing_cfg, "nse-profiler",
825                                            "WAIT_TIME", &wait_time))
826   {
827     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
828                 "Option nse-profiler:wait_time is required!\n");
829     return;
830   }
831
832   if (GNUNET_OK !=
833       GNUNET_CONFIGURATION_get_value_time (testing_cfg, "nse",
834                                            "INTERVAL", &interval))
835   {
836     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837                 "Option nse:interval is required!\n");
838     return;
839   }
840
841   if (GNUNET_OK !=
842       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "nse-profiler",
843                                              "connection_limit",
844                                              &connection_limit))
845   {
846     connection_limit = 0;
847   }
848
849   if (GNUNET_OK !=
850       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "nse-profiler",
851                                              "topology_output_file",
852                                              &topology_file))
853   {
854     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
855                 "Option nse-profiler:topology_output_file is required!\n");
856     return;
857   }
858
859   if (GNUNET_OK ==
860       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "nse-profiler",
861                                              "data_output_file",
862                                              &data_filename))
863   {
864     data_file =
865         GNUNET_DISK_file_open (data_filename,
866                                GNUNET_DISK_OPEN_READWRITE |
867                                GNUNET_DISK_OPEN_TRUNCATE |
868                                GNUNET_DISK_OPEN_CREATE,
869                                GNUNET_DISK_PERM_USER_READ |
870                                GNUNET_DISK_PERM_USER_WRITE);
871     if (data_file == NULL)
872       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
873                   data_filename);
874     GNUNET_free (data_filename);
875   }
876
877   if (GNUNET_YES ==
878       GNUNET_CONFIGURATION_get_value_string (cfg, "nse-profiler", "output_file",
879                                              &temp_str))
880   {
881     output_file =
882         GNUNET_DISK_file_open (temp_str,
883                                GNUNET_DISK_OPEN_READWRITE |
884                                GNUNET_DISK_OPEN_CREATE,
885                                GNUNET_DISK_PERM_USER_READ |
886                                GNUNET_DISK_PERM_USER_WRITE);
887     if (output_file == NULL)
888       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
889                   temp_str);
890   }
891   GNUNET_free_non_null (temp_str);
892
893   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
894
895   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
896                                        &connect_cb, &my_cb, NULL, hosts);
897   GNUNET_assert (pg != NULL);
898   shutdown_handle =
899     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
900                                     &shutdown_task, NULL);
901 }
902
903
904
905 /**
906  * nse-profiler command line options
907  */
908 static struct GNUNET_GETOPT_CommandLineOption options[] = {
909   {'V', "verbose", NULL,
910    gettext_noop ("be verbose (print progress information)"),
911    0, &GNUNET_GETOPT_set_one, &verbose},
912   GNUNET_GETOPT_OPTION_END
913 };
914
915
916 int
917 main (int argc, char *argv[])
918 {
919   GNUNET_log_setup ("nse-profiler",
920 #if VERBOSE
921                     "DEBUG",
922 #else
923                     "WARNING",
924 #endif
925                     NULL);
926   GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
927                       gettext_noop
928                       ("Measure quality and performance of the NSE service."),
929                       options, &run, NULL);
930 #if REMOVE_DIR
931   GNUNET_DISK_directory_remove ("/tmp/nse-profiler");
932 #endif
933   return ok;
934 }
935
936 /* end of nse-profiler.c */