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