some fixes to the pt/vpn testcase.
[oweals/gnunet.git] / src / nse / gnunet-nse-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 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  * @author Christian Grothoff
28  * @author Nathan Evans
29  * @author Sree Harsha Totakura
30  */
31
32 #include "platform.h"
33 #include "gnunet_testbed_service.h"
34 #include "gnunet_nse_service.h"
35
36 /**
37  * Generic loggins shorthand
38  */
39 #define LOG(kind,...)                                           \
40   GNUNET_log (kind, __VA_ARGS__)
41
42 /**
43  * Debug logging shorthand
44  */
45 #define LOG_DEBUG(...)                          \
46   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
47
48
49 /**
50  * Information we track for a peer in the testbed.
51  */
52 struct NSEPeer
53 {
54   /**
55    * Prev reference in DLL.
56    */
57   struct NSEPeer *prev;
58
59   /**
60    * Next reference in DLL.
61    */
62   struct NSEPeer *next;
63
64   /**
65    * Handle with testbed.
66    */
67   struct GNUNET_TESTBED_Peer *daemon;
68
69   /**
70    * Testbed operation to connect to NSE service.
71    */
72   struct GNUNET_TESTBED_Operation *nse_op;
73
74 };
75
76
77 /**
78  * Context for the stats task?
79  */
80 struct StatsContext
81 {
82
83   /**
84    * How many messages have peers received during the test.
85    */
86   unsigned long long total_nse_received_messages;
87
88   /**
89    * How many messages have peers send during the test (should be == received).
90    */
91   unsigned long long total_nse_transmitted_messages;
92
93   /**
94    * How many messages have travelled an edge in both directions.
95    */
96   unsigned long long total_nse_cross;
97
98   /**
99    * How many extra messages per edge (corrections) have been received.
100    */
101   unsigned long long total_nse_extra;
102
103   /**
104    * How many messages have been discarded.
105    */
106   unsigned long long total_discarded;
107 };
108
109
110 /**
111  * Operation map entry
112  */
113 struct OpListEntry
114 {
115   /**
116    * DLL next ptr
117    */
118   struct OpListEntry *next;
119
120   /**
121    * DLL prev ptr
122    */
123   struct OpListEntry *prev;
124
125   /**
126    * The testbed operation
127    */
128   struct GNUNET_TESTBED_Operation *op;
129
130 };
131
132
133 /**
134  * Head of DLL of peers we monitor closely.
135  */
136 static struct NSEPeer *peer_head;
137
138 /**
139  * Tail of DLL of peers we monitor closely.
140  */
141 static struct NSEPeer *peer_tail;
142
143 /**
144  * Return value from 'main' (0 == success)
145  */
146 static int ok;
147
148 /**
149  * Be verbose (configuration option)
150  */
151 static int verbose;
152
153 /**
154  * Name of the file with the hosts to run the test over (configuration option)
155  */ 
156 static char *hosts_file;
157
158 /**
159  * Maximum number of peers in the test.
160  */
161 static unsigned int num_peers;
162
163 /**
164  * Total number of rounds to execute.
165  */
166 static unsigned int num_rounds;
167
168 /**
169  * Current round we are in.
170  */
171 static unsigned int current_round;
172
173 /**
174  * Array of size 'num_rounds' with the requested number of peers in the given round.
175  */
176 static unsigned int *num_peers_in_round;
177
178 /**
179  * How many peers are running right now?
180  */
181 static unsigned int peers_running;
182
183 /**
184  * Specification for the numbers of peers to have in each round.
185  */
186 static char *num_peer_spec;
187
188 /**
189  * Handles to all of the running peers.
190  */
191 static struct GNUNET_TESTBED_Peer **daemons;
192
193 /**
194  * Global configuration file
195  */
196 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
197
198 /**
199  * The shutdown task
200  */
201 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
202
203 /**
204  * Maximum number of connections to NSE services.
205  */
206 static unsigned int connection_limit;
207
208 /**
209  * Total number of connections in the whole network.
210  */
211 static unsigned int total_connections;
212
213 /**
214  * File to report results to.
215  */
216 static struct GNUNET_DISK_FileHandle *output_file;
217
218 /**
219  * Filename to log results to.
220  */
221 static char *output_filename;
222
223 /**
224  * File to log connection info, statistics to.
225  */
226 static struct GNUNET_DISK_FileHandle *data_file;
227
228 /**
229  * Filename to log connection info, statistics to.
230  */
231 static char *data_filename;
232
233 /**
234  * How long to wait before triggering next round?
235  * Default: 60 s.
236  */
237 static struct GNUNET_TIME_Relative wait_time = { 60 * 1000 };
238
239 /**
240  * DLL head for operation list
241  */
242 static struct OpListEntry *oplist_head;
243
244 /**
245  * DLL tail for operation list
246  */
247 static struct OpListEntry *oplist_tail;
248
249 /**
250  * The get stats operation
251  */
252 static struct GNUNET_TESTBED_Operation *get_stats_op;
253
254 /**
255  * Are we shutting down
256  */
257 static int shutting_down;
258
259
260 /**
261  * Clean up all of the monitoring connections to NSE and
262  * STATISTICS that we keep to selected peers.
263  */
264 static void
265 close_monitor_connections ()
266 {
267   struct NSEPeer *pos;
268   struct OpListEntry *oplist_entry;
269
270   while (NULL != (pos = peer_head))
271   {
272     if (NULL != pos->nse_op)
273       GNUNET_TESTBED_operation_done (pos->nse_op);
274     GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
275     GNUNET_free (pos);
276   }
277   while (NULL != (oplist_entry = oplist_head))
278   {
279     GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, oplist_entry);
280     GNUNET_TESTBED_operation_done (oplist_entry->op);
281     GNUNET_free (oplist_entry);
282   }
283 }
284
285
286 /**
287  * Task run on shutdown; cleans up everything.
288  *
289  * @param cls unused
290  * @param tc unused
291  */
292 static void
293 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
294 {
295   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
296   if (GNUNET_YES == shutting_down)
297     return;
298   shutting_down = GNUNET_YES;
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");    
300   close_monitor_connections ();
301   if (NULL != get_stats_op)
302   {
303     GNUNET_TESTBED_operation_done (get_stats_op);
304     get_stats_op = NULL;
305   }
306   if (NULL != data_file)
307   {
308     GNUNET_DISK_file_close (data_file);
309     data_file = NULL;
310   }
311   if (NULL != output_file)
312   {
313     GNUNET_DISK_file_close (output_file);
314     output_file = NULL;
315   }
316   if (NULL != testing_cfg)
317     GNUNET_CONFIGURATION_destroy (testing_cfg);
318   testing_cfg = NULL;
319 }
320
321
322 /**
323  * Schedules shutdown task to be run now
324  */
325 static void
326 shutdown_now ()
327 {
328   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task_id)
329     GNUNET_SCHEDULER_cancel (shutdown_task_id);
330   shutdown_task_id = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
331 }
332
333
334 /**
335  * Callback to call when network size estimate is updated.
336  *
337  * @param cls closure with the 'struct NSEPeer' providing the update
338  * @param timestamp server timestamp
339  * @param estimate the value of the current network size estimate
340  * @param std_dev standard deviation (rounded down to nearest integer)
341  *                of the size estimation values seen
342  */
343 static void
344 handle_estimate (void *cls, 
345                  struct GNUNET_TIME_Absolute timestamp,
346                  double estimate, double std_dev)
347 {
348   struct NSEPeer *peer = cls;
349   char output_buffer[512];
350   size_t size;
351
352   if (NULL == output_file)
353     {
354       FPRINTF (stderr,
355                "Received network size estimate from peer %p. Size: %f std.dev. %f\n",
356                peer, estimate, std_dev);
357       return;
358     }
359   size = GNUNET_snprintf (output_buffer, 
360                           sizeof (output_buffer),
361                           "%p %llu %llu %f %f %f\n",
362                           peer, peers_running,
363                           timestamp.abs_value,
364                           GNUNET_NSE_log_estimate_to_n (estimate), estimate,
365                           std_dev);
366   if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
367     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
368                 "Unable to write to file!\n");
369 }
370
371
372 /**
373  * Adapter function called to establish a connection to
374  * NSE service.
375  * 
376  * @param cls closure (the 'struct NSEPeer')
377  * @param cfg configuration of the peer to connect to; will be available until
378  *          GNUNET_TESTBED_operation_done() is called on the operation returned
379  *          from GNUNET_TESTBED_service_connect()
380  * @return service handle to return in 'op_result', NULL on error
381  */
382 static void *
383 nse_connect_adapter (void *cls,
384                      const struct GNUNET_CONFIGURATION_Handle *cfg)
385 {
386   struct NSEPeer *current_peer = cls;
387
388   return GNUNET_NSE_connect (cfg, &handle_estimate, current_peer);
389 }
390
391
392 /**
393  * Adapter function called to destroy a connection to
394  * NSE service.
395  * 
396  * @param cls closure
397  * @param op_result service handle returned from the connect adapter
398  */
399 static void 
400 nse_disconnect_adapter (void *cls,
401                         void *op_result)
402 {
403   GNUNET_NSE_disconnect (op_result);
404 }
405
406
407 /**
408  * Task run to connect to the NSE and statistics services to a subset of
409  * all of the running peers.
410  */
411 static void
412 connect_nse_service ()
413 {
414   struct NSEPeer *current_peer;
415   unsigned int i;
416   unsigned int connections;
417
418   if (0 == connection_limit)
419     return;  
420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to nse service of peers\n");
421   connections = 0;
422   for (i = 0; i < num_peers_in_round[current_round]; i++)
423   {
424     if ((num_peers_in_round[current_round] > connection_limit) && 
425         (0 != (i % (num_peers_in_round[current_round] / connection_limit))))
426       continue;
427     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428                 "nse-profiler: connecting to nse service of peer %d\n", i);
429     current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
430     current_peer->daemon = daemons[i];
431     current_peer->nse_op 
432         = GNUNET_TESTBED_service_connect (NULL,
433                                           current_peer->daemon,
434                                           "nse",
435                                           NULL, NULL,
436                                           &nse_connect_adapter,
437                                           &nse_disconnect_adapter,
438                                           current_peer);
439     GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
440     if (++connections == connection_limit)
441       break;
442   }
443 }
444
445
446 /**
447  * Task that starts/stops peers to move to the next round.
448  *
449  * @param cls NULL, unused
450  * @param tc scheduler context (unused)
451  */
452 static void
453 next_round (void *cls, 
454             const struct GNUNET_SCHEDULER_TaskContext *tc);
455
456
457 /**
458  * Continuation called by the "get_all" and "get" functions at the
459  * end of a round.  Obtains the final statistics and writes them to
460  * the file, then either starts the next round, or, if this was the
461  * last round, terminates the run.
462  *
463  * @param cls struct StatsContext
464  * @param op operation handle
465  * @param emsg error message, NULL on success
466  */
467 static void
468 stats_finished_callback (void *cls,
469                          struct GNUNET_TESTBED_Operation *op,
470                          const char *emsg)
471 {
472   struct StatsContext *stats_context = cls;
473   char buf[512];
474   size_t buf_len;
475
476   GNUNET_TESTBED_operation_done (get_stats_op);
477   get_stats_op = NULL;
478   if (NULL != emsg)
479     {
480       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481                   "Failed to get statistics: %s\n",
482                   emsg);
483       GNUNET_SCHEDULER_shutdown ();
484       GNUNET_free (stats_context);
485       return;
486     }
487   LOG_DEBUG ("Finished collecting statistics\n");
488   if (NULL != data_file)
489     {
490       /* Stats lookup successful, write out data */
491       buf_len =
492         GNUNET_snprintf (buf, sizeof (buf),
493                          "TOTAL_NSE_RECEIVED_MESSAGES_%u: %u \n",
494                          current_round,
495                          stats_context->total_nse_received_messages);
496       GNUNET_DISK_file_write (data_file, buf, buf_len);
497       buf_len =
498         GNUNET_snprintf (buf, sizeof (buf),
499                          "TOTAL_NSE_TRANSMITTED_MESSAGES_%u: %u\n",
500                          current_round,
501                          stats_context->total_nse_transmitted_messages);
502       GNUNET_DISK_file_write (data_file, buf, buf_len);    
503       buf_len =
504         GNUNET_snprintf (buf, sizeof (buf),
505                          "TOTAL_NSE_CROSS_%u: %u \n",
506                          current_round,
507                          stats_context->total_nse_cross);
508       GNUNET_DISK_file_write (data_file, buf, buf_len);
509       buf_len =
510         GNUNET_snprintf (buf, sizeof (buf),
511                          "TOTAL_NSE_EXTRA_%u: %u \n",
512                          current_round,
513                          stats_context->total_nse_extra);
514       GNUNET_DISK_file_write (data_file, buf, buf_len);
515       buf_len =
516         GNUNET_snprintf (buf, sizeof (buf),
517                          "TOTAL_NSE_DISCARDED_%u: %u \n",
518                          current_round,
519                          stats_context->total_discarded);
520       GNUNET_DISK_file_write (data_file, buf, buf_len);    
521     }  
522   GNUNET_SCHEDULER_add_now (&next_round, NULL);
523   GNUNET_free (stats_context);
524 }
525
526
527 /**
528  * Callback function to process statistic values.
529  *
530  * @param cls struct StatsContext
531  * @param peer the peer the statistics belong to
532  * @param subsystem name of subsystem that created the statistic
533  * @param name the name of the datum
534  * @param value the current value
535  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
536  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
537  */
538 static int
539 statistics_iterator (void *cls, 
540                      const struct GNUNET_TESTBED_Peer *peer,
541                      const char *subsystem, const char *name, uint64_t value,
542                      int is_persistent)
543 {
544   struct StatsContext *stats_context = cls;
545   char buf[512];
546   size_t buf_len;
547
548   if (0 != strcasecmp (subsystem, "nse"))
549     return GNUNET_SYSERR;
550   if (0 == strcmp (name, "# flood messages received"))
551   {
552     stats_context->total_nse_received_messages += value;
553     if ( (verbose > 1) && 
554          (NULL != data_file) )
555     {
556       buf_len =
557           GNUNET_snprintf (buf, sizeof (buf),
558                            "%p %u RECEIVED\n", 
559                            peer, value);
560       GNUNET_DISK_file_write (data_file, buf, buf_len);
561     }
562   }
563   if (0 == strcmp (name, "# flood messages transmitted"))
564   {
565     stats_context->total_nse_transmitted_messages += value;
566     if ( (verbose > 1) &&
567          (NULL != data_file) )
568     {
569       buf_len =
570           GNUNET_snprintf (buf, sizeof (buf),
571                            "%p %u TRANSMITTED\n", 
572                            peer, value);
573       GNUNET_DISK_file_write (data_file, buf, buf_len);
574     }
575   }
576   if (0 == strcmp (name, "# cross messages"))
577     stats_context->total_nse_cross += value;    
578   if (0 == strcmp (name, "# extra messages"))    
579     stats_context->total_nse_extra += value;
580   if (0 == strcmp (name, "# flood messages discarded (clock skew too large)"))
581     stats_context->total_discarded += value;    
582   return GNUNET_OK;
583 }
584
585
586 /**
587  * We're at the end of a round.  Stop monitoring, write total
588  * number of connections to log and get full stats.  Then trigger
589  * the next round.
590  *
591  * @param cls unused, NULL
592  * @param tc unused
593  */
594 static void
595 finish_round (void *cls, 
596               const struct GNUNET_SCHEDULER_TaskContext *tc)
597 {
598   struct StatsContext *stats_context;
599   char buf[1024];
600   size_t buf_len;
601
602   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
603     return;
604   LOG (GNUNET_ERROR_TYPE_INFO, "Have %u connections\n", total_connections);
605   if (NULL != data_file)
606     {
607       buf_len = GNUNET_snprintf (buf, sizeof (buf),
608                                  "CONNECTIONS_0: %u\n", 
609                                  total_connections);
610       GNUNET_DISK_file_write (data_file, buf, buf_len);
611     }
612   close_monitor_connections ();    
613   stats_context = GNUNET_malloc (sizeof (struct StatsContext));
614   get_stats_op =
615       GNUNET_TESTBED_get_statistics (num_peers_in_round[current_round],
616                                      daemons,
617                                      "nse", NULL,
618                                      &statistics_iterator,
619                                      &stats_finished_callback,
620                                      stats_context);
621 }
622
623
624 /**
625  * We have reached the desired number of peers for the current round.
626  * Run it (by connecting and monitoring a few peers and waiting the
627  * specified delay before finishing the round).
628  */
629 static void
630 run_round ()
631 {
632   LOG_DEBUG ("Running round %u\n", current_round);
633   connect_nse_service ();
634   GNUNET_SCHEDULER_add_delayed (wait_time,
635                                 &finish_round,
636                                 NULL);
637 }
638
639
640 /**
641  * Creates an oplist entry and adds it to the oplist DLL
642  */
643 static struct OpListEntry *
644 make_oplist_entry ()
645 {
646   struct OpListEntry *entry;
647
648   entry = GNUNET_malloc (sizeof (struct OpListEntry));
649   GNUNET_CONTAINER_DLL_insert_tail (oplist_head, oplist_tail, entry);
650   return entry;
651 }
652
653
654 /**
655  * Functions of this signature are called when a peer has been successfully
656  * started or stopped.
657  *
658  * @param cls NULL
659  * @param emsg NULL on success; otherwise an error description
660  */
661 static void 
662 peer_churn_cb (void *cls, const char *emsg)
663 {
664   struct OpListEntry *entry = cls;
665   
666   GNUNET_TESTBED_operation_done (entry->op);
667   GNUNET_CONTAINER_DLL_remove (oplist_head, oplist_tail, entry);
668   GNUNET_free (entry);
669   if (num_peers_in_round[current_round] == peers_running)
670     run_round ();
671 }
672
673
674 /**
675  * Adjust the number of running peers to match the required number of running
676  * peers for the round
677  */
678 static void
679 adjust_running_peers ()
680 {
681   struct OpListEntry *entry;
682   unsigned int i;
683
684   /* start peers if we have too few */
685   for (i=peers_running;i<num_peers_in_round[current_round];i++)
686   {
687     entry = make_oplist_entry ();
688     entry->op = GNUNET_TESTBED_peer_start (NULL, daemons[i], 
689                                            &peer_churn_cb, entry);
690   }
691   /* stop peers if we have too many */
692   for (i=num_peers_in_round[current_round];i<peers_running;i++)
693   {
694     entry = make_oplist_entry ();
695     entry->op = GNUNET_TESTBED_peer_stop (NULL, daemons[i], 
696                                           &peer_churn_cb, entry);
697   }
698 }
699
700
701 /**
702  * Task run at the end of a round.  Disconnect from all monitored
703  * peers; then get statistics from *all* peers.
704  *
705  * @param cls NULL, unused
706  * @param tc unused
707  */
708 static void
709 next_round (void *cls, 
710             const struct GNUNET_SCHEDULER_TaskContext *tc)
711 {
712   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
713     return;
714   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting nse service of peers\n");
715   current_round++;  
716   if (current_round == num_rounds)
717     {
718       /* this was the last round, terminate */
719       ok = 0;
720       GNUNET_SCHEDULER_shutdown ();
721       return;
722     }
723   if (num_peers_in_round[current_round] == peers_running)
724     {
725       /* no need to churn, just run next round */
726       run_round ();
727       return;
728     }
729   adjust_running_peers ();
730 }
731
732
733 /**
734  * Function that will be called whenever something in the
735  * testbed changes.
736  *
737  * @param cls closure, NULL
738  * @param event information on what is happening
739  */
740 static void
741 master_controller_cb (void *cls, 
742                       const struct GNUNET_TESTBED_EventInformation *event)
743 {
744   switch (event->type)
745     {
746     case GNUNET_TESTBED_ET_PEER_START:
747       peers_running++;
748       break;
749     case GNUNET_TESTBED_ET_PEER_STOP:
750       peers_running--;
751       break;
752     case GNUNET_TESTBED_ET_CONNECT:
753       total_connections++;
754       break;
755     case GNUNET_TESTBED_ET_DISCONNECT:
756       total_connections--;
757       break;
758     default:
759       break;
760     }
761 }
762
763
764 /**
765  * Signature of a main function for a testcase.
766  *
767  * @param cls NULL
768  * @param num_peers_ number of peers in 'peers'
769  * @param peers handle to peers run in the testbed.  NULL upon timeout (see
770  *          GNUNET_TESTBED_test_run()).
771  */
772 static void 
773 test_master (void *cls,
774              unsigned int num_peers_,
775              struct GNUNET_TESTBED_Peer **peers)
776 {
777   if (NULL == peers)
778   {
779     shutdown_now ();
780     return;
781   }
782   daemons = peers;
783   GNUNET_break (num_peers_ == num_peers);
784   peers_running = num_peers;
785   if (num_peers_in_round[current_round] == peers_running)
786   {
787     /* no need to churn, just run the starting round */
788     run_round ();
789     return;
790   }
791   adjust_running_peers ();
792 }
793
794
795 /**
796  * Actual main function that runs the emulation.
797  *
798  * @param cls unused
799  * @param args remaining args, unused
800  * @param cfgfile name of the configuration
801  * @param cfg configuration handle
802  */
803 static void
804 run (void *cls, char *const *args, const char *cfgfile,
805      const struct GNUNET_CONFIGURATION_Handle *cfg)
806 {
807   char *tok;
808   uint64_t event_mask;
809   unsigned int num;  
810
811   ok = 1;
812   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
813   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
814   if (NULL == num_peer_spec)
815   {
816     fprintf (stderr, "You need to specify the number of peers to run\n");
817     return;
818   }
819   for (tok = strtok (num_peer_spec, ","); NULL != tok; tok = strtok (NULL, ","))
820     {
821       if (1 != sscanf (tok, "%u", &num))
822         {
823           fprintf (stderr, "You need to specify numbers, not `%s'\n", tok);
824           return;
825         }
826       if (0 == num)
827         {
828           fprintf (stderr, "Refusing to run a round with 0 peers\n");
829           return;
830         }
831       GNUNET_array_append (num_peers_in_round, num_rounds, num);
832       num_peers = GNUNET_MAX (num_peers, num);
833     }
834   if (0 == num_peers)
835     {
836       fprintf (stderr, "Refusing to run a testbed with no rounds\n");
837       return;
838     }
839   if ( (NULL != data_filename) &&
840        (NULL == (data_file = 
841                  GNUNET_DISK_file_open (data_filename,
842                                         GNUNET_DISK_OPEN_READWRITE |
843                                         GNUNET_DISK_OPEN_TRUNCATE |
844                                         GNUNET_DISK_OPEN_CREATE,
845                                         GNUNET_DISK_PERM_USER_READ |
846                                         GNUNET_DISK_PERM_USER_WRITE))) )
847     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
848                               "open",
849                               data_filename);
850
851   if ( (NULL != output_filename) &&
852        (NULL == (output_file =
853                  GNUNET_DISK_file_open (output_filename,
854                                         GNUNET_DISK_OPEN_READWRITE |
855                                         GNUNET_DISK_OPEN_CREATE,
856                                         GNUNET_DISK_PERM_USER_READ |
857                                         GNUNET_DISK_PERM_USER_WRITE))) )
858     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
859                               output_filename);
860   event_mask = 0LL;
861   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
862   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
863   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
864   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
865   GNUNET_TESTBED_run (hosts_file,
866                       cfg,
867                       num_peers,
868                       event_mask,
869                       master_controller_cb,
870                       NULL,     /* master_controller_cb cls */
871                       &test_master,
872                       NULL);    /* test_master cls */
873   shutdown_task_id = 
874       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
875                                     &shutdown_task, NULL);
876 }
877
878
879 /**
880  * Main function.
881  *
882  * @return 0 on success
883  */
884 int
885 main (int argc, char *const *argv)
886 {
887   static struct GNUNET_GETOPT_CommandLineOption options[] = {
888     {'C', "connections", "COUNT",
889      gettext_noop ("limit to the number of connections to NSE services, 0 for none"),
890      1, &GNUNET_GETOPT_set_uint, &connection_limit},
891     {'d', "details", "FILENAME",
892      gettext_noop ("name of the file for writing connection information and statistics"),
893      1, &GNUNET_GETOPT_set_string, &data_filename},
894     {'H', "hosts", "FILENAME",
895      gettext_noop ("name of the file with the login information for the testbed"),
896      1, &GNUNET_GETOPT_set_string, &hosts_file},
897     {'o', "output", "FILENAME",
898      gettext_noop ("name of the file for writing the main results"),
899      1, &GNUNET_GETOPT_set_string, &output_filename},
900     {'p', "peers", "NETWORKSIZESPEC",
901      gettext_noop ("Number of peers to run in each round, separated by commas"),
902      1, &GNUNET_GETOPT_set_string, &num_peer_spec},
903     {'V', "verbose", NULL,
904      gettext_noop ("be verbose (print progress information)"),
905      0, &GNUNET_GETOPT_increment_value, &verbose},
906     {'w', "wait", "DELAY",
907      gettext_noop ("delay between rounds"),
908      1, &GNUNET_GETOPT_set_relative_time, &wait_time},
909     GNUNET_GETOPT_OPTION_END
910   };
911   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
912     return 2;
913   if (GNUNET_OK !=
914       GNUNET_PROGRAM_run (argc, argv, "nse-profiler",
915                           gettext_noop
916                           ("Measure quality and performance of the NSE service."),
917                           options, &run, NULL))
918     ok = 1;
919   return ok;
920 }
921
922 /* end of nse-profiler.c */