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