remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / dht / gnunet_dht_profiler.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2014, 2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file dht/gnunet_dht_profiler.c
23  * @brief Profiler for GNUnet DHT
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_constants.h"
32
33
34 #define MESSAGE(...)                                       \
35   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, __VA_ARGS__)
36
37 #define DEBUG(...)                                           \
38   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
39
40 /**
41  * Number of peers which should perform a PUT out of 100 peers
42  */
43 static unsigned int put_probability = 100;
44
45 /**
46  * Configuration
47  */
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
49
50 /**
51  * Name of the file with the hosts to run the test over
52  */
53 static char *hosts_file;
54
55 /**
56  * Context for a peer which actively does DHT PUT/GET
57  */
58 struct ActiveContext;
59
60 /**
61  * Context to hold data of peer
62  */
63 struct Context
64 {
65   /**
66    * The testbed peer this context belongs to
67    */
68   struct GNUNET_TESTBED_Peer *peer;
69
70   /**
71    * Testbed operation acting on this peer
72    */
73   struct GNUNET_TESTBED_Operation *op;
74
75   /**
76    * Active context; NULL if this peer is not an active peer
77    */
78   struct ActiveContext *ac;
79 };
80
81
82 /**
83  * Context for a peer which actively does DHT PUT/GET
84  */
85 struct ActiveContext
86 {
87   /**
88    * The linked peer context
89    */
90   struct Context *ctx;
91
92   /**
93    * Handler to the DHT service
94    */
95   struct GNUNET_DHT_Handle *dht;
96
97   /**
98    * The active context used for our DHT GET
99    */
100   struct ActiveContext *get_ac;
101
102   /**
103    * The put handle
104    */
105   struct GNUNET_DHT_PutHandle *dht_put;
106
107   /**
108    * The get handle
109    */
110   struct GNUNET_DHT_GetHandle *dht_get;
111
112   /**
113    * The hashes of the values stored via this activity context.
114    * Array of length #num_puts_per_peer.
115    */
116   struct GNUNET_HashCode *hash;
117
118   /**
119    * Delay task
120    */
121   struct GNUNET_SCHEDULER_Task *delay_task;
122
123   /**
124    * How many puts should we still issue?
125    */
126   unsigned int put_count;
127
128   /**
129    * The number of peers currently doing GET on our data
130    */
131   uint16_t nrefs;
132 };
133
134
135 /**
136  * An array of contexts.  The size of this array should be equal to @a num_peers
137  */
138 static struct Context *a_ctx;
139
140 /**
141  * Array of active peers
142  */
143 static struct ActiveContext *a_ac;
144
145 /**
146  * The delay between rounds for collecting statistics
147  */
148 static struct GNUNET_TIME_Relative delay_stats;
149
150 /**
151  * The delay to start puts.
152  */
153 static struct GNUNET_TIME_Relative delay_put;
154
155 /**
156  * The delay to start puts.
157  */
158 static struct GNUNET_TIME_Relative delay_get;
159
160 /**
161  * The timeout for GET and PUT
162  */
163 static struct GNUNET_TIME_Relative timeout;
164
165 /**
166  * Number of peers
167  */
168 static unsigned int num_peers;
169
170 /**
171  * Number of active peers
172  */
173 static unsigned int n_active;
174
175 /**
176  * Number of DHT service connections we currently have
177  */
178 static unsigned int n_dht;
179
180 /**
181  * Number of DHT PUTs made
182  */
183 static unsigned long long n_puts;
184
185 /**
186  * Number of DHT PUTs to be made per peer.
187  */
188 static unsigned int num_puts_per_peer = 1;
189
190 /**
191  * Number of DHT PUTs succeeded
192  */
193 static unsigned long long n_puts_ok;
194
195 /**
196  * Number of DHT GETs made
197  */
198 static unsigned int n_gets;
199
200 /**
201  * Number of DHT GETs succeeded
202  */
203 static unsigned int n_gets_ok;
204
205 /**
206  * Number of DHT GETs succeeded
207  */
208 static unsigned int n_gets_fail;
209
210 /**
211  * Replication degree
212  */
213 static unsigned int replication;
214
215 /**
216  * Testbed Operation (to get stats).
217  */
218 static struct GNUNET_TESTBED_Operation *bandwidth_stats_op;
219
220 /**
221  * Testbed peer handles.
222  */
223 static struct GNUNET_TESTBED_Peer **testbed_handles;
224
225 /**
226  * Total number of messages sent by peer.
227  */
228 static uint64_t outgoing_bandwidth;
229
230 /**
231  * Total number of messages received by peer.
232  */
233 static uint64_t incoming_bandwidth;
234
235 /**
236  * Average number of hops taken to do put.
237  */
238 static double average_put_path_length;
239
240 /**
241  * Average number of hops taken to do get.
242  */
243 static double average_get_path_length;
244
245 /**
246  * Total put path length across all peers.
247  */
248 static unsigned int total_put_path_length;
249
250 /**
251  * Total get path length across all peers.
252  */
253 static unsigned int total_get_path_length;
254
255 /**
256  * Counter to keep track of peers added to peer_context lists.
257  */
258 static int peers_started = 0;
259
260 /**
261  * Should we do a PUT (mode = 0) or GET (mode = 1);
262  */
263 static enum
264 {
265   MODE_PUT = 0,
266
267   MODE_GET = 1
268 } mode;
269
270
271 /**
272  * Are we shutting down
273  */
274 static int in_shutdown = 0;
275
276
277 /**
278  * Connect to DHT services of active peers
279  */
280 static void
281 start_profiling (void);
282
283
284 /**
285  * Shutdown task.  Cleanup all resources and operations.
286  *
287  * @param cls NULL
288  */
289 static void
290 do_shutdown (void *cls)
291 {
292   struct ActiveContext *ac;
293
294   in_shutdown = GNUNET_YES;
295   if (NULL != a_ctx)
296   {
297     for (unsigned int cnt = 0; cnt < num_peers; cnt++)
298     {
299       /* Cleanup active context if this peer is an active peer */
300       ac = a_ctx[cnt].ac;
301       if (NULL != ac)
302       {
303         if (NULL != ac->delay_task)
304           GNUNET_SCHEDULER_cancel (ac->delay_task);
305         if (NULL != ac->hash)
306           free (ac->hash);
307         if (NULL != ac->dht_put)
308           GNUNET_DHT_put_cancel (ac->dht_put);
309         if (NULL != ac->dht_get)
310           GNUNET_DHT_get_stop (ac->dht_get);
311       }
312       /* Cleanup testbed operation handle at the last as this operation may
313          contain service connection to DHT */
314       if (NULL != a_ctx[cnt].op)
315         GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
316     }
317     GNUNET_free (a_ctx);
318     a_ctx = NULL;
319   }
320   // FIXME: Should we collect stats only for put/get not for other messages.
321   if (NULL != bandwidth_stats_op)
322   {
323     GNUNET_TESTBED_operation_done (bandwidth_stats_op);
324     bandwidth_stats_op = NULL;
325   }
326   GNUNET_free_non_null (a_ac);
327 }
328
329
330 /**
331  * Stats callback. Finish the stats testbed operation and when all stats have
332  * been iterated, shutdown the test.
333  *
334  * @param cls closure
335  * @param op the operation that has been finished
336  * @param emsg error message in case the operation has failed; will be NULL if
337  *          operation has executed successfully.
338  */
339 static void
340 bandwidth_stats_cont (void *cls,
341                       struct GNUNET_TESTBED_Operation *op,
342                       const char *emsg)
343 {
344   MESSAGE ("# Outgoing (core) bandwidth: %llu bytes\n",
345            (unsigned long long) outgoing_bandwidth);
346   MESSAGE ("# Incoming (core) bandwidth: %llu bytes\n",
347            (unsigned long long) incoming_bandwidth);
348   fprintf (stderr,
349            "Benchmark done. Collect data via gnunet-statistics, then press ENTER to exit.\n");
350   (void) getchar ();
351   GNUNET_SCHEDULER_shutdown ();
352 }
353
354
355 /**
356  * Process statistic values.
357  *
358  * @param cls closure
359  * @param peer the peer the statistic belong to
360  * @param subsystem name of subsystem that created the statistic
361  * @param name the name of the datum
362  * @param value the current value
363  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
364  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
365  */
366 static int
367 bandwidth_stats_iterator (void *cls,
368                           const struct GNUNET_TESTBED_Peer *peer,
369                           const char *subsystem,
370                           const char *name,
371                           uint64_t value,
372                           int is_persistent)
373 {
374   static const char *s_sent = "# bytes encrypted";
375   static const char *s_recv = "# bytes decrypted";
376
377   if (0 == strncmp (s_sent, name, strlen (s_sent)))
378     outgoing_bandwidth = outgoing_bandwidth + value;
379   else if (0 == strncmp (s_recv, name, strlen (s_recv)))
380     incoming_bandwidth = incoming_bandwidth + value;
381   return GNUNET_OK;
382 }
383
384
385 static void
386 summarize ()
387 {
388   MESSAGE ("# PUTS started: %llu\n",
389            n_puts);
390   MESSAGE ("# PUTS succeeded: %llu\n",
391            n_puts_ok);
392   MESSAGE ("# GETS made: %u\n",
393            n_gets);
394   MESSAGE ("# GETS succeeded: %u\n",
395            n_gets_ok);
396   MESSAGE ("# GETS failed: %u\n",
397            n_gets_fail);
398   MESSAGE ("# average_put_path_length: %f\n",
399            average_put_path_length);
400   MESSAGE ("# average_get_path_length: %f\n",
401            average_get_path_length);
402
403   if (NULL == testbed_handles)
404   {
405     MESSAGE ("No peers found\n");
406     return;
407   }
408   /* Collect Stats*/
409   bandwidth_stats_op = GNUNET_TESTBED_get_statistics (n_active,
410                                                       testbed_handles,
411                                                       "core",
412                                                       NULL,
413                                                       &bandwidth_stats_iterator,
414                                                       &bandwidth_stats_cont,
415                                                       NULL);
416 }
417
418
419 /**
420  * Task to cancel DHT GET.
421  *
422  * @param cls NULL
423  */
424 static void
425 cancel_get (void *cls)
426 {
427   struct ActiveContext *ac = cls;
428   struct Context *ctx = ac->ctx;
429
430   ac->delay_task = NULL;
431   GNUNET_assert (NULL != ac->dht_get);
432   GNUNET_DHT_get_stop (ac->dht_get);
433   ac->dht_get = NULL;
434   n_gets_fail++;
435   GNUNET_assert (NULL != ctx->op);
436   GNUNET_TESTBED_operation_done (ctx->op);
437   ctx->op = NULL;
438
439   /* If profiling is complete, summarize */
440   if (n_active == n_gets_fail + n_gets_ok)
441   {
442     average_put_path_length = (double) total_put_path_length
443                               / (double) n_active;
444     average_get_path_length = (double) total_get_path_length
445                               / (double ) n_gets_ok;
446     summarize ();
447   }
448 }
449
450
451 /**
452  * Iterator called on each result obtained for a DHT
453  * operation that expects a reply
454  *
455  * @param cls closure
456  * @param exp when will this value expire
457  * @param key key of the result
458  * @param get_path peers on reply path (or NULL if not recorded)
459  *                 [0] = datastore's first neighbor, [length - 1] = local peer
460  * @param get_path_length number of entries in @a get_path
461  * @param put_path peers on the PUT path (or NULL if not recorded)
462  *                 [0] = origin, [length - 1] = datastore
463  * @param put_path_length number of entries in @a put_path
464  * @param type type of the result
465  * @param size number of bytes in @a data
466  * @param data pointer to the result data
467  */
468 static void
469 get_iter (void *cls,
470           struct GNUNET_TIME_Absolute exp,
471           const struct GNUNET_HashCode *key,
472           const struct GNUNET_PeerIdentity *get_path,
473           unsigned int get_path_length,
474           const struct GNUNET_PeerIdentity *put_path,
475           unsigned int put_path_length,
476           enum GNUNET_BLOCK_Type type,
477           size_t size, const void *data)
478 {
479   struct ActiveContext *ac = cls;
480   struct ActiveContext *get_ac = ac->get_ac;
481   struct Context *ctx = ac->ctx;
482
483   /* we found the data we are looking for */
484   DEBUG ("We found a GET request; %u remaining\n",
485          n_gets - (n_gets_fail + n_gets_ok)); // FIXME: It always prints 1.
486   n_gets_ok++;
487   get_ac->nrefs--;
488   GNUNET_DHT_get_stop (ac->dht_get);
489   ac->dht_get = NULL;
490   if (ac->delay_task != NULL)
491     GNUNET_SCHEDULER_cancel (ac->delay_task);
492   ac->delay_task = NULL;
493   GNUNET_assert (NULL != ctx->op);
494   GNUNET_TESTBED_operation_done (ctx->op);
495   ctx->op = NULL;
496
497   total_put_path_length = total_put_path_length + (double) put_path_length;
498   total_get_path_length = total_get_path_length + (double) get_path_length;
499   DEBUG ("total_put_path_length = %u,put_path \n",
500          total_put_path_length);
501   /* Summarize if profiling is complete */
502   if (n_active == n_gets_fail + n_gets_ok)
503   {
504     average_put_path_length = (double) total_put_path_length
505                               / (double) n_active;
506     average_get_path_length = (double) total_get_path_length
507                               / (double ) n_gets_ok;
508     summarize ();
509   }
510 }
511
512
513 /**
514  * Task to do DHT GETs
515  *
516  * @param cls the active context
517  */
518 static void
519 delayed_get (void *cls)
520 {
521   struct ActiveContext *ac = cls;
522   struct ActiveContext *get_ac;
523   unsigned int r;
524
525   ac->delay_task = NULL;
526   get_ac = NULL;
527   while (1)
528   {
529     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
530                                   n_active);
531     get_ac = &a_ac[r];
532     if (NULL != get_ac->hash)
533       break;
534   }
535   get_ac->nrefs++;
536   ac->get_ac = get_ac;
537   r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
538                                 num_puts_per_peer);
539   DEBUG ("GET_REQUEST_START key %s \n",
540          GNUNET_h2s (&get_ac->hash[r]));
541   ac->dht_get = GNUNET_DHT_get_start (ac->dht,
542                                       GNUNET_BLOCK_TYPE_TEST,
543                                       &get_ac->hash[r],
544                                       1, /* replication level */
545                                       GNUNET_DHT_RO_NONE,
546                                       NULL,
547                                       0, /* extended query and size */
548                                       &get_iter,
549                                       ac); /* GET iterator and closure */
550   n_gets++;
551
552   /* schedule the timeout task for GET */
553   ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout,
554                                                  &cancel_get,
555                                                  ac);
556 }
557
558
559 /**
560  * Task to do DHT PUTs.  If the "put_count" hits zero,
561  * we stop the TESTBED operation (connection to DHT)
562  * so that others PUTs have a chance.
563  *
564  * @param cls the active context
565  */
566 static void
567 delayed_put (void *cls);
568
569
570 /**
571  * Conclude individual PUT operation, schedule the
572  * next one.
573  *
574  * @param cls the active context
575  */
576 static void
577 put_cont (void *cls)
578 {
579   struct ActiveContext *ac = cls;
580
581   ac->dht_put = NULL;
582   n_puts_ok++;
583   ac->delay_task = GNUNET_SCHEDULER_add_now (&delayed_put,
584                                              ac);
585 }
586
587
588 /**
589  * Task to do DHT PUTs.  If the "put_count" hits zero,
590  * we stop the TESTBED operation (connection to DHT)
591  * so that others PUTs have a chance.
592  *
593  * @param cls the active context
594  */
595 static void
596 delayed_put (void *cls)
597 {
598   struct ActiveContext *ac = cls;
599   char block[65536];
600   size_t block_size;
601
602   ac->delay_task = NULL;
603   if (0 == ac->put_count)
604   {
605     struct Context *ctx = ac->ctx;
606     struct GNUNET_TESTBED_Operation *op;
607
608     GNUNET_assert (NULL != ctx);
609     op = ctx->op;
610     ctx->op = NULL;
611     GNUNET_TESTBED_operation_done (op);
612     return;
613   }
614
615
616   /* Generate and DHT PUT some random data */
617   block_size = 16; /* minimum */
618   /* make random payload, reserve 512 - 16 bytes for DHT headers */
619   block_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
620                                           GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE
621                                           - 512);
622   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
623                               block,
624                               block_size);
625   ac->put_count--;
626   GNUNET_CRYPTO_hash (block,
627                       block_size,
628                       &ac->hash[ac->put_count]);
629   DEBUG ("PUT_REQUEST_START key %s\n",
630          GNUNET_h2s (&ac->hash[ac->put_count]));
631   ac->dht_put = GNUNET_DHT_put (ac->dht,
632                                 &ac->hash[ac->put_count],
633                                 replication,
634                                 GNUNET_DHT_RO_RECORD_ROUTE,
635                                 GNUNET_BLOCK_TYPE_TEST,
636                                 block_size,
637                                 block,
638                                 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
639                                 &put_cont,
640                                 ac);                /* continuation and its closure */
641   n_puts++;
642 }
643
644
645 /**
646  * Connection to DHT has been established.  Call the delay task.
647  *
648  * @param cls the active context
649  * @param op the operation that has been finished
650  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
651  * @param emsg error message in case the operation has failed; will be NULL if
652  *          operation has executed successfully.
653  */
654 static void
655 dht_connected (void *cls,
656                struct GNUNET_TESTBED_Operation *op,
657                void *ca_result,
658                const char *emsg)
659 {
660   struct ActiveContext *ac = cls;
661   struct Context *ctx = ac->ctx;
662
663   GNUNET_assert (NULL != ctx);  // FIXME: Fails
664   GNUNET_assert (NULL != ctx->op);
665   GNUNET_assert (ctx->op == op);
666   ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
667   if (NULL != emsg)
668   {
669     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
670                 "Connection to DHT service failed: %s\n",
671                 emsg);
672     GNUNET_TESTBED_operation_done (ctx->op);  /* Calls dht_disconnect() */
673     ctx->op = NULL;
674     return;
675   }
676   switch (mode)
677   {
678   case MODE_PUT:
679     {
680       struct GNUNET_TIME_Relative peer_delay_put;
681
682       peer_delay_put.rel_value_us =
683         GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
684                                   delay_put.rel_value_us);
685       ac->put_count = num_puts_per_peer;
686       ac->hash = calloc (ac->put_count,
687                          sizeof(struct GNUNET_HashCode));
688       if (NULL == ac->hash)
689       {
690         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
691                              "calloc");
692         GNUNET_SCHEDULER_shutdown ();
693         return;
694       }
695       ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_put,
696                                                      &delayed_put,
697                                                      ac);
698       break;
699     }
700
701   case MODE_GET:
702     {
703       struct GNUNET_TIME_Relative peer_delay_get;
704
705       peer_delay_get.rel_value_us =
706         delay_get.rel_value_us
707         + GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
708                                     delay_get.rel_value_us);
709       ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_get,
710                                                      &delayed_get,
711                                                      ac);
712       break;
713     }
714   }
715 }
716
717
718 /**
719  * Connect to DHT service and return the DHT client handler
720  *
721  * @param cls the active context
722  * @param cfg configuration of the peer to connect to; will be available until
723  *          GNUNET_TESTBED_operation_done() is called on the operation returned
724  *          from GNUNET_TESTBED_service_connect()
725  * @return service handle to return in 'op_result', NULL on error
726  */
727 static void *
728 dht_connect (void *cls,
729              const struct GNUNET_CONFIGURATION_Handle *cfg)
730 {
731   n_dht++;
732   return GNUNET_DHT_connect (cfg,
733                              10);
734 }
735
736
737 /**
738  * Adapter function called to destroy a connection to
739  * a service.
740  *
741  * @param cls the active context
742  * @param op_result service handle returned from the connect adapter
743  */
744 static void
745 dht_disconnect (void *cls,
746                 void *op_result)
747 {
748   struct ActiveContext *ac = cls;
749
750   GNUNET_assert (NULL != ac->dht);
751   GNUNET_assert (ac->dht == op_result);
752   GNUNET_DHT_disconnect (ac->dht);
753   ac->dht = NULL;
754   n_dht--;
755   if (0 != n_dht)
756     return;
757   if (GNUNET_YES == in_shutdown)
758     return;
759   switch (mode)
760   {
761   case MODE_PUT:
762     if (n_puts_ok != ((unsigned long long) n_active) * num_puts_per_peer)
763       return;
764     /* Start GETs if all PUTs have been made */
765     mode = MODE_GET;
766     start_profiling ();
767     return;
768
769   case MODE_GET:
770     if ((n_gets_ok + n_gets_fail) != n_active)
771       return;
772     break;
773   }
774 }
775
776
777 /**
778  * Connect to DHT services of active peers
779  */
780 static void
781 start_profiling ()
782 {
783   struct Context *ctx;
784
785   DEBUG ("GNUNET_TESTBED_service_connect\n");
786   GNUNET_break (GNUNET_YES != in_shutdown);
787   for (unsigned int i = 0; i < n_active; i++)
788   {
789     struct ActiveContext *ac = &a_ac[i];
790     GNUNET_assert (NULL != (ctx = ac->ctx));
791     GNUNET_assert (NULL == ctx->op);
792     ctx->op = GNUNET_TESTBED_service_connect (ctx,
793                                               ctx->peer,
794                                               "dht",
795                                               &dht_connected, ac,
796                                               &dht_connect,
797                                               &dht_disconnect,
798                                               ac);
799   }
800 }
801
802
803 /**
804  * Callback called when DHT service on the peer is started
805  *
806  * @param cls the context
807  * @param op the operation that has been finished
808  * @param emsg error message in case the operation has failed; will be NULL if
809  *          operation has executed successfully.
810  */
811 static void
812 service_started (void *cls,
813                  struct GNUNET_TESTBED_Operation *op,
814                  const char *emsg)
815 {
816   struct Context *ctx = cls;
817
818   GNUNET_assert (NULL != ctx);
819   GNUNET_assert (NULL != ctx->op);
820   GNUNET_TESTBED_operation_done (ctx->op);
821   ctx->op = NULL;
822   peers_started++;
823   DEBUG ("Peers Started = %d; num_peers = %d \n",
824          peers_started,
825          num_peers);
826   if (peers_started == num_peers)
827     start_profiling ();
828 }
829
830
831 /**
832  * Signature of a main function for a testcase.
833  *
834  * @param cls closure
835  * @param h the run handle
836  * @param num_peers number of peers in 'peers'
837  * @param peers handle to peers run in the testbed
838  * @param links_succeeded the number of overlay link connection attempts that
839  *          succeeded
840  * @param links_failed the number of overlay link
841  */
842 static void
843 test_run (void *cls,
844           struct GNUNET_TESTBED_RunHandle *h,
845           unsigned int num_peers,
846           struct GNUNET_TESTBED_Peer **peers,
847           unsigned int links_succeeded,
848           unsigned int links_failed)
849 {
850   unsigned int ac_cnt;
851
852   testbed_handles = peers;
853   if (NULL == peers)
854   {
855     /* exit */
856     GNUNET_assert (0);
857   }
858   MESSAGE ("%u peers started, %u/%u links up\n",
859            num_peers,
860            links_succeeded,
861            links_succeeded + links_failed);
862   a_ctx = GNUNET_new_array (num_peers,
863                             struct Context);
864   /* select the peers which actively participate in profiling */
865   n_active = num_peers * put_probability / 100;
866   if (0 == n_active)
867   {
868     GNUNET_SCHEDULER_shutdown ();
869     GNUNET_free (a_ctx);
870     a_ctx = NULL;
871     return;
872   }
873
874   a_ac = GNUNET_new_array (n_active,
875                            struct ActiveContext);
876   ac_cnt = 0;
877   for (unsigned int cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
878   {
879     if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
880                                   100) >= put_probability)
881       continue;
882
883     a_ctx[cnt].ac = &a_ac[ac_cnt];
884     a_ac[ac_cnt].ctx = &a_ctx[cnt];
885     ac_cnt++;
886   }
887   n_active = ac_cnt;
888
889   /* start DHT service on all peers */
890   for (unsigned int cnt = 0; cnt < num_peers; cnt++)
891   {
892     a_ctx[cnt].peer = peers[cnt];
893     a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
894                                                         peers[cnt],
895                                                         "dht",
896                                                         &service_started,
897                                                         &a_ctx[cnt],
898                                                         1);
899   }
900 }
901
902
903 /**
904  * Main function that will be run by the scheduler.
905  *
906  * @param cls closure
907  * @param args remaining command-line arguments
908  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
909  * @param config configuration
910  */
911 static void
912 run (void *cls,
913      char *const *args,
914      const char *cfgfile,
915      const struct GNUNET_CONFIGURATION_Handle *config)
916 {
917   uint64_t event_mask;
918
919   if (0 == num_peers)
920   {
921     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922                 _ ("Exiting as the number of peers is %u\n"),
923                 num_peers);
924     return;
925   }
926   cfg = config;
927   event_mask = 0;
928   GNUNET_TESTBED_run (hosts_file,
929                       cfg,
930                       num_peers,
931                       event_mask,
932                       NULL,
933                       NULL,
934                       &test_run,
935                       NULL);
936   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
937                                  NULL);
938 }
939
940
941 /**
942  * Main function.
943  *
944  * @return 0 on success
945  */
946 int
947 main (int argc,
948       char *const *argv)
949 {
950   int rc;
951   struct GNUNET_GETOPT_CommandLineOption options[] = {
952     GNUNET_GETOPT_option_uint ('n',
953                                "peers",
954                                "COUNT",
955                                gettext_noop ("number of peers to start"),
956                                &num_peers),
957     GNUNET_GETOPT_option_uint ('p',
958                                "peer-put-count",
959                                "COUNT",
960                                gettext_noop (
961                                  "number of PUTs to perform per peer"),
962                                &num_puts_per_peer),
963     GNUNET_GETOPT_option_string ('H',
964                                  "hosts",
965                                  "FILENAME",
966                                  gettext_noop (
967                                    "name of the file with the login information for the testbed"),
968                                  &hosts_file),
969     GNUNET_GETOPT_option_relative_time ('D',
970                                         "delay",
971                                         "DELAY",
972                                         gettext_noop (
973                                           "delay between rounds for collecting statistics (default: 30 sec)"),
974                                         &delay_stats),
975     GNUNET_GETOPT_option_relative_time ('P',
976                                         "PUT-delay",
977                                         "DELAY",
978                                         gettext_noop (
979                                           "delay to start doing PUTs (default: 1 sec)"),
980                                         &delay_put),
981     GNUNET_GETOPT_option_relative_time ('G',
982                                         "GET-delay",
983                                         "DELAY",
984                                         gettext_noop (
985                                           "delay to start doing GETs (default: 5 min)"),
986                                         &delay_get),
987     GNUNET_GETOPT_option_uint ('r',
988                                "replication",
989                                "DEGREE",
990                                gettext_noop ("replication degree for DHT PUTs"),
991                                &replication),
992     GNUNET_GETOPT_option_uint ('R',
993                                "random-chance",
994                                "PROBABILITY",
995                                gettext_noop (
996                                  "chance that a peer is selected at random for PUTs"),
997                                &put_probability),
998     GNUNET_GETOPT_option_relative_time ('t',
999                                         "timeout",
1000                                         "TIMEOUT",
1001                                         gettext_noop (
1002                                           "timeout for DHT PUT and GET requests (default: 1 min)"),
1003                                         &timeout),
1004     GNUNET_GETOPT_OPTION_END
1005   };
1006
1007   if (GNUNET_OK !=
1008       GNUNET_STRINGS_get_utf8_args (argc, argv,
1009                                     &argc, &argv))
1010     return 2;
1011   /* set default delays */
1012   delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1013   delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1014   delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1015   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1016   replication = 1;      /* default replication */
1017   rc = 0;
1018   if (GNUNET_OK !=
1019       GNUNET_PROGRAM_run (argc,
1020                           argv,
1021                           "gnunet-dht-profiler",
1022                           gettext_noop (
1023                             "Measure quality and performance of the DHT service."),
1024                           options,
1025                           &run,
1026                           NULL))
1027     rc = 1;
1028   return rc;
1029 }