aboutsummaryrefslogtreecommitdiff
path: root/src/service/dht/gnunet_dht_profiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dht/gnunet_dht_profiler.c')
-rw-r--r--src/service/dht/gnunet_dht_profiler.c1032
1 files changed, 1032 insertions, 0 deletions
diff --git a/src/service/dht/gnunet_dht_profiler.c b/src/service/dht/gnunet_dht_profiler.c
new file mode 100644
index 000000000..55a34bdf0
--- /dev/null
+++ b/src/service/dht/gnunet_dht_profiler.c
@@ -0,0 +1,1032 @@
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 */
43static unsigned int put_probability = 100;
44
45/**
46 * Configuration
47 */
48static const struct GNUNET_CONFIGURATION_Handle *cfg;
49
50/**
51 * Name of the file with the hosts to run the test over
52 */
53static char *hosts_file;
54
55/**
56 * Context for a peer which actively does DHT PUT/GET
57 */
58struct ActiveContext;
59
60/**
61 * Context to hold data of peer
62 */
63struct 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 */
85struct 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 */
138static struct Context *a_ctx;
139
140/**
141 * Array of active peers
142 */
143static struct ActiveContext *a_ac;
144
145/**
146 * The delay between rounds for collecting statistics
147 */
148static struct GNUNET_TIME_Relative delay_stats;
149
150/**
151 * The delay to start puts.
152 */
153static struct GNUNET_TIME_Relative delay_put;
154
155/**
156 * The delay to start puts.
157 */
158static struct GNUNET_TIME_Relative delay_get;
159
160/**
161 * The timeout for GET and PUT
162 */
163static struct GNUNET_TIME_Relative timeout;
164
165/**
166 * Number of peers
167 */
168static unsigned int num_peers;
169
170/**
171 * Number of active peers
172 */
173static unsigned int n_active;
174
175/**
176 * Number of DHT service connections we currently have
177 */
178static unsigned int n_dht;
179
180/**
181 * Number of DHT PUTs made
182 */
183static unsigned long long n_puts;
184
185/**
186 * Number of DHT PUTs to be made per peer.
187 */
188static unsigned int num_puts_per_peer = 1;
189
190/**
191 * Number of DHT PUTs succeeded
192 */
193static unsigned long long n_puts_ok;
194
195/**
196 * Number of DHT GETs made
197 */
198static unsigned int n_gets;
199
200/**
201 * Number of DHT GETs succeeded
202 */
203static unsigned int n_gets_ok;
204
205/**
206 * Number of DHT GETs succeeded
207 */
208static unsigned int n_gets_fail;
209
210/**
211 * Replication degree
212 */
213static unsigned int replication;
214
215/**
216 * Testbed Operation (to get stats).
217 */
218static struct GNUNET_TESTBED_Operation *bandwidth_stats_op;
219
220/**
221 * Testbed peer handles.
222 */
223static struct GNUNET_TESTBED_Peer **testbed_handles;
224
225/**
226 * Total number of messages sent by peer.
227 */
228static uint64_t outgoing_bandwidth;
229
230/**
231 * Total number of messages received by peer.
232 */
233static uint64_t incoming_bandwidth;
234
235/**
236 * Average number of hops taken to do put.
237 */
238static double average_put_path_length;
239
240/**
241 * Average number of hops taken to do get.
242 */
243static double average_get_path_length;
244
245/**
246 * Total put path length across all peers.
247 */
248static unsigned int total_put_path_length;
249
250/**
251 * Total get path length across all peers.
252 */
253static unsigned int total_get_path_length;
254
255/**
256 * Counter to keep track of peers added to peer_context lists.
257 */
258static int peers_started = 0;
259
260/**
261 * Should we do a PUT (mode = 0) or GET (mode = 1);
262 */
263static enum
264{
265 MODE_PUT = 0,
266
267 MODE_GET = 1
268} mode;
269
270
271/**
272 * Are we shutting down
273 */
274static int in_shutdown = 0;
275
276
277/**
278 * Connect to DHT services of active peers
279 */
280static void
281start_profiling (void);
282
283
284/**
285 * Shutdown task. Cleanup all resources and operations.
286 *
287 * @param cls NULL
288 */
289static void
290do_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 (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 */
339static void
340bandwidth_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 */
366static int
367bandwidth_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
385static void
386summarize ()
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 */
424static void
425cancel_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 trunc_peer peer the path was truncated at, or NULL
459 * @param get_path peers on reply path (or NULL if not recorded)
460 * [0] = datastore's first neighbor, [length - 1] = local peer
461 * @param get_path_length number of entries in @a get_path
462 * @param put_path peers on the PUT path (or NULL if not recorded)
463 * [0] = origin, [length - 1] = datastore
464 * @param put_path_length number of entries in @a put_path
465 * @param type type of the result
466 * @param size number of bytes in @a data
467 * @param data pointer to the result data
468 */
469static void
470get_iter (void *cls,
471 struct GNUNET_TIME_Absolute exp,
472 const struct GNUNET_HashCode *key,
473 const struct GNUNET_PeerIdentity *trunc_peer,
474 const struct GNUNET_DHT_PathElement *get_path,
475 unsigned int get_path_length,
476 const struct GNUNET_DHT_PathElement *put_path,
477 unsigned int put_path_length,
478 enum GNUNET_BLOCK_Type type,
479 size_t size,
480 const void *data)
481{
482 struct ActiveContext *ac = cls;
483 struct ActiveContext *get_ac = ac->get_ac;
484 struct Context *ctx = ac->ctx;
485
486 /* we found the data we are looking for */
487 DEBUG ("We found a GET request; %u remaining\n",
488 n_gets - (n_gets_fail + n_gets_ok)); // FIXME: It always prints 1.
489 n_gets_ok++;
490 get_ac->nrefs--;
491 GNUNET_DHT_get_stop (ac->dht_get);
492 ac->dht_get = NULL;
493 if (ac->delay_task != NULL)
494 GNUNET_SCHEDULER_cancel (ac->delay_task);
495 ac->delay_task = NULL;
496 GNUNET_assert (NULL != ctx->op);
497 GNUNET_TESTBED_operation_done (ctx->op);
498 ctx->op = NULL;
499
500 total_put_path_length = total_put_path_length + (double) put_path_length;
501 total_get_path_length = total_get_path_length + (double) get_path_length;
502 DEBUG ("total_put_path_length = %u,put_path \n",
503 total_put_path_length);
504 /* Summarize if profiling is complete */
505 if (n_active == n_gets_fail + n_gets_ok)
506 {
507 average_put_path_length = (double) total_put_path_length
508 / (double) n_active;
509 average_get_path_length = (double) total_get_path_length
510 / (double ) n_gets_ok;
511 summarize ();
512 }
513}
514
515
516/**
517 * Task to do DHT GETs
518 *
519 * @param cls the active context
520 */
521static void
522delayed_get (void *cls)
523{
524 struct ActiveContext *ac = cls;
525 struct ActiveContext *get_ac;
526 unsigned int r;
527
528 ac->delay_task = NULL;
529 get_ac = NULL;
530 while (1)
531 {
532 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
533 n_active);
534 get_ac = &a_ac[r];
535 if (NULL != get_ac->hash)
536 break;
537 }
538 get_ac->nrefs++;
539 ac->get_ac = get_ac;
540 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
541 num_puts_per_peer);
542 DEBUG ("GET_REQUEST_START key %s \n",
543 GNUNET_h2s (&get_ac->hash[r]));
544 ac->dht_get = GNUNET_DHT_get_start (ac->dht,
545 GNUNET_BLOCK_TYPE_TEST,
546 &get_ac->hash[r],
547 1, /* replication level */
548 GNUNET_DHT_RO_NONE,
549 NULL,
550 0, /* extended query and size */
551 &get_iter,
552 ac); /* GET iterator and closure */
553 n_gets++;
554
555 /* schedule the timeout task for GET */
556 ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout,
557 &cancel_get,
558 ac);
559}
560
561
562/**
563 * Task to do DHT PUTs. If the "put_count" hits zero,
564 * we stop the TESTBED operation (connection to DHT)
565 * so that others PUTs have a chance.
566 *
567 * @param cls the active context
568 */
569static void
570delayed_put (void *cls);
571
572
573/**
574 * Conclude individual PUT operation, schedule the
575 * next one.
576 *
577 * @param cls the active context
578 */
579static void
580put_cont (void *cls)
581{
582 struct ActiveContext *ac = cls;
583
584 ac->dht_put = NULL;
585 n_puts_ok++;
586 ac->delay_task = GNUNET_SCHEDULER_add_now (&delayed_put,
587 ac);
588}
589
590
591/**
592 * Task to do DHT PUTs. If the "put_count" hits zero,
593 * we stop the TESTBED operation (connection to DHT)
594 * so that others PUTs have a chance.
595 *
596 * @param cls the active context
597 */
598static void
599delayed_put (void *cls)
600{
601 struct ActiveContext *ac = cls;
602 char block[65536];
603 size_t block_size;
604
605 ac->delay_task = NULL;
606 if (0 == ac->put_count)
607 {
608 struct Context *ctx = ac->ctx;
609 struct GNUNET_TESTBED_Operation *op;
610
611 GNUNET_assert (NULL != ctx);
612 op = ctx->op;
613 ctx->op = NULL;
614 GNUNET_TESTBED_operation_done (op);
615 return;
616 }
617
618
619 /* Generate and DHT PUT some random data */
620 block_size = 16; /* minimum */
621 /* make random payload, reserve 512 - 16 bytes for DHT headers */
622 block_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
623 GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE
624 - 512);
625 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
626 block,
627 block_size);
628 ac->put_count--;
629 GNUNET_CRYPTO_hash (block,
630 block_size,
631 &ac->hash[ac->put_count]);
632 DEBUG ("PUT_REQUEST_START key %s\n",
633 GNUNET_h2s (&ac->hash[ac->put_count]));
634 ac->dht_put = GNUNET_DHT_put (ac->dht,
635 &ac->hash[ac->put_count],
636 replication,
637 GNUNET_DHT_RO_RECORD_ROUTE,
638 GNUNET_BLOCK_TYPE_TEST,
639 block_size,
640 block,
641 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
642 &put_cont,
643 ac); /* continuation and its closure */
644 n_puts++;
645}
646
647
648/**
649 * Connection to DHT has been established. Call the delay task.
650 *
651 * @param cls the active context
652 * @param op the operation that has been finished
653 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
654 * @param emsg error message in case the operation has failed; will be NULL if
655 * operation has executed successfully.
656 */
657static void
658dht_connected (void *cls,
659 struct GNUNET_TESTBED_Operation *op,
660 void *ca_result,
661 const char *emsg)
662{
663 struct ActiveContext *ac = cls;
664 struct Context *ctx = ac->ctx;
665
666 GNUNET_assert (NULL != ctx); // FIXME: Fails
667 GNUNET_assert (NULL != ctx->op);
668 GNUNET_assert (ctx->op == op);
669 ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
670 if (NULL != emsg)
671 {
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 "Connection to DHT service failed: %s\n",
674 emsg);
675 GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect() */
676 ctx->op = NULL;
677 return;
678 }
679 switch (mode)
680 {
681 case MODE_PUT:
682 {
683 struct GNUNET_TIME_Relative peer_delay_put;
684
685 peer_delay_put.rel_value_us =
686 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
687 delay_put.rel_value_us);
688 ac->put_count = num_puts_per_peer;
689 ac->hash = calloc (ac->put_count,
690 sizeof(struct GNUNET_HashCode));
691 if (NULL == ac->hash)
692 {
693 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
694 "calloc");
695 GNUNET_SCHEDULER_shutdown ();
696 return;
697 }
698 ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_put,
699 &delayed_put,
700 ac);
701 break;
702 }
703
704 case MODE_GET:
705 {
706 struct GNUNET_TIME_Relative peer_delay_get;
707
708 peer_delay_get.rel_value_us =
709 delay_get.rel_value_us
710 + GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
711 delay_get.rel_value_us);
712 ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_get,
713 &delayed_get,
714 ac);
715 break;
716 }
717 }
718}
719
720
721/**
722 * Connect to DHT service and return the DHT client handler
723 *
724 * @param cls the active context
725 * @param cfg configuration of the peer to connect to; will be available until
726 * GNUNET_TESTBED_operation_done() is called on the operation returned
727 * from GNUNET_TESTBED_service_connect()
728 * @return service handle to return in 'op_result', NULL on error
729 */
730static void *
731dht_connect (void *cls,
732 const struct GNUNET_CONFIGURATION_Handle *cfg)
733{
734 n_dht++;
735 return GNUNET_DHT_connect (cfg,
736 10);
737}
738
739
740/**
741 * Adapter function called to destroy a connection to
742 * a service.
743 *
744 * @param cls the active context
745 * @param op_result service handle returned from the connect adapter
746 */
747static void
748dht_disconnect (void *cls,
749 void *op_result)
750{
751 struct ActiveContext *ac = cls;
752
753 GNUNET_assert (NULL != ac->dht);
754 GNUNET_assert (ac->dht == op_result);
755 GNUNET_DHT_disconnect (ac->dht);
756 ac->dht = NULL;
757 n_dht--;
758 if (0 != n_dht)
759 return;
760 if (GNUNET_YES == in_shutdown)
761 return;
762 switch (mode)
763 {
764 case MODE_PUT:
765 if (n_puts_ok != ((unsigned long long) n_active) * num_puts_per_peer)
766 return;
767 /* Start GETs if all PUTs have been made */
768 mode = MODE_GET;
769 start_profiling ();
770 return;
771
772 case MODE_GET:
773 if ((n_gets_ok + n_gets_fail) != n_active)
774 return;
775 break;
776 }
777}
778
779
780/**
781 * Connect to DHT services of active peers
782 */
783static void
784start_profiling ()
785{
786 struct Context *ctx;
787
788 DEBUG ("GNUNET_TESTBED_service_connect\n");
789 GNUNET_break (GNUNET_YES != in_shutdown);
790 for (unsigned int i = 0; i < n_active; i++)
791 {
792 struct ActiveContext *ac = &a_ac[i];
793 GNUNET_assert (NULL != (ctx = ac->ctx));
794 GNUNET_assert (NULL == ctx->op);
795 ctx->op = GNUNET_TESTBED_service_connect (ctx,
796 ctx->peer,
797 "dht",
798 &dht_connected, ac,
799 &dht_connect,
800 &dht_disconnect,
801 ac);
802 }
803}
804
805
806/**
807 * Callback called when DHT service on the peer is started
808 *
809 * @param cls the context
810 * @param op the operation that has been finished
811 * @param emsg error message in case the operation has failed; will be NULL if
812 * operation has executed successfully.
813 */
814static void
815service_started (void *cls,
816 struct GNUNET_TESTBED_Operation *op,
817 const char *emsg)
818{
819 struct Context *ctx = cls;
820
821 GNUNET_assert (NULL != ctx);
822 GNUNET_assert (NULL != ctx->op);
823 GNUNET_TESTBED_operation_done (ctx->op);
824 ctx->op = NULL;
825 peers_started++;
826 DEBUG ("Peers Started = %d; num_peers = %d \n",
827 peers_started,
828 num_peers);
829 if (peers_started == num_peers)
830 start_profiling ();
831}
832
833
834/**
835 * Signature of a main function for a testcase.
836 *
837 * @param cls closure
838 * @param h the run handle
839 * @param num_peers number of peers in 'peers'
840 * @param peers handle to peers run in the testbed
841 * @param links_succeeded the number of overlay link connection attempts that
842 * succeeded
843 * @param links_failed the number of overlay link
844 */
845static void
846test_run (void *cls,
847 struct GNUNET_TESTBED_RunHandle *h,
848 unsigned int num_peers,
849 struct GNUNET_TESTBED_Peer **peers,
850 unsigned int links_succeeded,
851 unsigned int links_failed)
852{
853 unsigned int ac_cnt;
854
855 testbed_handles = peers;
856 if (NULL == peers)
857 {
858 /* exit */
859 GNUNET_assert (0);
860 }
861 MESSAGE ("%u peers started, %u/%u links up\n",
862 num_peers,
863 links_succeeded,
864 links_succeeded + links_failed);
865 a_ctx = GNUNET_new_array (num_peers,
866 struct Context);
867 /* select the peers which actively participate in profiling */
868 n_active = num_peers * put_probability / 100;
869 if (0 == n_active)
870 {
871 GNUNET_SCHEDULER_shutdown ();
872 GNUNET_free (a_ctx);
873 a_ctx = NULL;
874 return;
875 }
876
877 a_ac = GNUNET_new_array (n_active,
878 struct ActiveContext);
879 ac_cnt = 0;
880 for (unsigned int cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
881 {
882 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
883 100) >= put_probability)
884 continue;
885
886 a_ctx[cnt].ac = &a_ac[ac_cnt];
887 a_ac[ac_cnt].ctx = &a_ctx[cnt];
888 ac_cnt++;
889 }
890 n_active = ac_cnt;
891
892 /* start DHT service on all peers */
893 for (unsigned int cnt = 0; cnt < num_peers; cnt++)
894 {
895 a_ctx[cnt].peer = peers[cnt];
896 a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
897 peers[cnt],
898 "dht",
899 &service_started,
900 &a_ctx[cnt],
901 1);
902 }
903}
904
905
906/**
907 * Main function that will be run by the scheduler.
908 *
909 * @param cls closure
910 * @param args remaining command-line arguments
911 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
912 * @param config configuration
913 */
914static void
915run (void *cls,
916 char *const *args,
917 const char *cfgfile,
918 const struct GNUNET_CONFIGURATION_Handle *config)
919{
920 uint64_t event_mask;
921
922 if (0 == num_peers)
923 {
924 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
925 _ ("Exiting as the number of peers is %u\n"),
926 num_peers);
927 return;
928 }
929 cfg = config;
930 event_mask = 0;
931 GNUNET_TESTBED_run (hosts_file,
932 cfg,
933 num_peers,
934 event_mask,
935 NULL,
936 NULL,
937 &test_run,
938 NULL);
939 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
940 NULL);
941}
942
943
944/**
945 * Main function.
946 *
947 * @return 0 on success
948 */
949int
950main (int argc,
951 char *const *argv)
952{
953 int rc;
954 struct GNUNET_GETOPT_CommandLineOption options[] = {
955 GNUNET_GETOPT_option_uint ('n',
956 "peers",
957 "COUNT",
958 gettext_noop ("number of peers to start"),
959 &num_peers),
960 GNUNET_GETOPT_option_uint ('p',
961 "peer-put-count",
962 "COUNT",
963 gettext_noop (
964 "number of PUTs to perform per peer"),
965 &num_puts_per_peer),
966 GNUNET_GETOPT_option_string ('H',
967 "hosts",
968 "FILENAME",
969 gettext_noop (
970 "name of the file with the login information for the testbed"),
971 &hosts_file),
972 GNUNET_GETOPT_option_relative_time ('D',
973 "delay",
974 "DELAY",
975 gettext_noop (
976 "delay between rounds for collecting statistics (default: 30 sec)"),
977 &delay_stats),
978 GNUNET_GETOPT_option_relative_time ('P',
979 "PUT-delay",
980 "DELAY",
981 gettext_noop (
982 "delay to start doing PUTs (default: 1 sec)"),
983 &delay_put),
984 GNUNET_GETOPT_option_relative_time ('G',
985 "GET-delay",
986 "DELAY",
987 gettext_noop (
988 "delay to start doing GETs (default: 5 min)"),
989 &delay_get),
990 GNUNET_GETOPT_option_uint ('r',
991 "replication",
992 "DEGREE",
993 gettext_noop ("replication degree for DHT PUTs"),
994 &replication),
995 GNUNET_GETOPT_option_uint ('R',
996 "random-chance",
997 "PROBABILITY",
998 gettext_noop (
999 "chance that a peer is selected at random for PUTs"),
1000 &put_probability),
1001 GNUNET_GETOPT_option_relative_time ('t',
1002 "timeout",
1003 "TIMEOUT",
1004 gettext_noop (
1005 "timeout for DHT PUT and GET requests (default: 1 min)"),
1006 &timeout),
1007 GNUNET_GETOPT_OPTION_END
1008 };
1009
1010 if (GNUNET_OK !=
1011 GNUNET_STRINGS_get_utf8_args (argc, argv,
1012 &argc, &argv))
1013 return 2;
1014 /* set default delays */
1015 delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1016 delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1017 delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1018 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1019 replication = 1; /* default replication */
1020 rc = 0;
1021 if (GNUNET_OK !=
1022 GNUNET_PROGRAM_run (argc,
1023 argv,
1024 "gnunet-dht-profiler",
1025 gettext_noop (
1026 "Measure quality and performance of the DHT service."),
1027 options,
1028 &run,
1029 NULL))
1030 rc = 1;
1031 return rc;
1032}