2 This file is part of GNUnet.
3 (C) 2014 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file dht/gnunet_dht_profiler.c
23 * @brief Profiler for GNUnet DHT
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_dht_service.h"
33 GNUNET_log (GNUNET_ERROR_TYPE_INFO, __VA_ARGS__)
36 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
39 * Number of peers which should perform a PUT out of 100 peers
41 #define PUT_PROBABILITY 50
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Name of the file with the hosts to run the test over
51 static char *hosts_file;
54 * Context for a peer which actively does DHT PUT/GET
59 * Context to hold data of peer
65 * The testbed peer this context belongs to
67 struct GNUNET_TESTBED_Peer *peer;
70 * Testbed operation acting on this peer
72 struct GNUNET_TESTBED_Operation *op;
75 * Active context; NULL if this peer is not an active peer
77 struct ActiveContext *ac;
82 * Context for a peer which actively does DHT PUT/GET
87 * The linked peer context
92 * Handler to the DHT service
94 struct GNUNET_DHT_Handle *dht;
97 * The data used for do a PUT. Will be NULL if a PUT hasn't been performed yet
102 * The active context used for our DHT GET
104 struct ActiveContext *get_ac;
109 struct GNUNET_DHT_PutHandle *dht_put;
114 struct GNUNET_DHT_GetHandle *dht_get;
117 * The hash of the @e put_data
119 struct GNUNET_HashCode hash;
124 GNUNET_SCHEDULER_TaskIdentifier delay_task;
127 * The size of the @e put_data
129 uint16_t put_data_size;
132 * The number of peers currently doing GET on our data
139 * An array of contexts. The size of this array should be equal to @a num_peers
141 static struct Context *a_ctx;
144 * Array of active peers
146 static struct ActiveContext *a_ac;
149 * The delay between starting to do PUTS and GETS
151 static struct GNUNET_TIME_Relative delay;
154 * The timeout for GET and PUT
156 static struct GNUNET_TIME_Relative timeout;
161 static unsigned int num_peers;
164 * Number of active peers
166 static unsigned int n_active;
169 * Number of DHT service connections we currently have
171 static unsigned int n_dht;
174 * Number of DHT PUTs made
176 static unsigned int n_puts;
179 * Number of DHT PUTs succeeded
181 static unsigned int n_puts_ok;
184 * Number of DHT PUTs failed
186 static unsigned int n_puts_fail;
189 * Number of DHT GETs made
191 static unsigned int n_gets;
194 * Number of DHT GETs succeeded
196 static unsigned int n_gets_ok;
199 * Number of DHT GETs succeeded
201 static unsigned int n_gets_fail;
205 * Shutdown task. Cleanup all resources and operations.
208 * @param tc scheduler task context
211 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
213 struct ActiveContext *ac;
218 for (cnt=0; cnt < num_peers; cnt++)
220 if (NULL != a_ctx[cnt].op)
221 GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
223 /* Cleanup active context if this peer is an active peer */
227 if (GNUNET_SCHEDULER_NO_TASK != ac->delay_task)
228 GNUNET_SCHEDULER_cancel (ac->delay_task);
229 if (NULL != ac->put_data)
230 GNUNET_free (ac->put_data);
231 if (NULL != ac->dht_put)
232 GNUNET_DHT_put_cancel (ac->dht_put);
233 if (NULL != ac->dht_get)
234 GNUNET_DHT_get_stop (ac->dht_get);
239 GNUNET_free_non_null (a_ac);
246 INFO ("# PUTS made: %u\n", n_puts);
247 INFO ("# PUTS succeeded: %u\n", n_puts_ok);
248 INFO ("# PUTS failed: %u\n", n_puts_fail);
249 INFO ("# GETS made: %u\n", n_gets);
250 INFO ("# GETS succeeded: %u\n", n_gets_ok);
251 INFO ("# GETS failed: %u\n", n_gets_fail);
252 GNUNET_SCHEDULER_shutdown ();
257 * Task to cancel DHT GET.
260 * @param tc scheduler task context
263 cancel_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 struct ActiveContext *ac = cls;
266 struct Context *ctx = ac->ctx;
268 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
269 GNUNET_assert (NULL != ac->dht_get);
270 GNUNET_DHT_get_stop (ac->dht_get);
272 GNUNET_TESTBED_operation_done (ctx->op);
276 /* If profiling is complete, summarize */
277 if (n_active == n_gets_fail + n_gets_ok)
283 * Iterator called on each result obtained for a DHT
284 * operation that expects a reply
287 * @param exp when will this value expire
288 * @param key key of the result
289 * @param get_path peers on reply path (or NULL if not recorded)
290 * [0] = datastore's first neighbor, [length - 1] = local peer
291 * @param get_path_length number of entries in @a get_path
292 * @param put_path peers on the PUT path (or NULL if not recorded)
293 * [0] = origin, [length - 1] = datastore
294 * @param put_path_length number of entries in @a put_path
295 * @param type type of the result
296 * @param size number of bytes in @a data
297 * @param data pointer to the result data
301 struct GNUNET_TIME_Absolute exp,
302 const struct GNUNET_HashCode *key,
303 const struct GNUNET_PeerIdentity *get_path,
304 unsigned int get_path_length,
305 const struct GNUNET_PeerIdentity *put_path,
306 unsigned int put_path_length,
307 enum GNUNET_BLOCK_Type type,
308 size_t size, const void *data)
310 struct ActiveContext *ac = cls;
311 struct ActiveContext *get_ac = ac->get_ac;
312 struct Context *ctx = ac->ctx;
314 /* Check the keys of put and get match or not. */
315 GNUNET_assert (0 == memcmp (key, &get_ac->hash, sizeof (struct GNUNET_HashCode)));
316 /* we found the data we are looking for */
317 DEBUG ("We found a GET request; %u remaining\n", n_gets - (n_gets_fail + n_gets_ok));
320 GNUNET_DHT_get_stop (ac->dht_get);
322 GNUNET_SCHEDULER_cancel (ac->delay_task);
323 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
324 GNUNET_TESTBED_operation_done (ctx->op);
327 /* Summarize if profiling is complete */
328 if (n_active == n_gets_fail + n_gets_ok)
334 * Task to do DHT GETs
336 * @param cls the active context
337 * @param tc the scheduler task context
340 delayed_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct ActiveContext *ac = cls;
343 struct ActiveContext *get_ac;
346 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
350 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, n_active);
352 if (NULL != get_ac->put_data)
357 ac->dht_get = GNUNET_DHT_get_start (ac->dht,
358 GNUNET_BLOCK_TYPE_TEST,
360 1, /* replication level */
362 NULL, 0, /* extended query and size */
363 get_iter, ac); /* GET iterator and closure
367 /* schedule the timeout task for GET */
368 ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout, &cancel_get, ac);
373 * Queue up a delayed task for doing DHT GET
375 * @param cls the active context
376 * @param success #GNUNET_OK if the PUT was transmitted,
377 * #GNUNET_NO on timeout,
378 * #GNUNET_SYSERR on disconnect from service
379 * after the PUT message was transmitted
380 * (so we don't know if it was received or not)
383 put_cont (void *cls, int success)
385 struct ActiveContext *ac = cls;
392 ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_get, ac);
397 * Task to do DHT PUTS
399 * @param cls the active context
400 * @param tc the scheduler task context
403 delayed_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
405 struct ActiveContext *ac = cls;
407 ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
408 /* Generate and DHT PUT some random data */
409 ac->put_data_size = 16; /* minimum */
410 ac->put_data_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
412 ac->put_data = GNUNET_malloc (ac->put_data_size);
413 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
414 ac->put_data, ac->put_data_size);
415 GNUNET_CRYPTO_hash (ac->put_data, ac->put_data_size, &ac->hash);
416 DEBUG ("Doing a DHT PUT with data of size %u\n", ac->put_data_size);
417 ac->dht_put = GNUNET_DHT_put (ac->dht, &ac->hash,
418 1, /* replication level */
420 GNUNET_BLOCK_TYPE_TEST,
423 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
424 timeout, /* PUT timeout */
425 put_cont, ac); /* continuation and its closure */
431 * Connection to DHT has been established. Call the delay task.
433 * @param cls the active context
434 * @param op the operation that has been finished
435 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
436 * @param emsg error message in case the operation has failed; will be NULL if
437 * operation has executed successfully.
440 dht_connected (void *cls,
441 struct GNUNET_TESTBED_Operation *op,
445 struct ActiveContext *ac = cls;
446 struct Context *ctx = ac->ctx;
448 GNUNET_assert (NULL != ctx);
449 GNUNET_assert (NULL != ctx->op);
450 GNUNET_assert (ctx->op == op);
451 ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
455 GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect() */
459 ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_put, ac);
464 * Connect to DHT service and return the DHT client handler
466 * @param cls the active context
467 * @param cfg configuration of the peer to connect to; will be available until
468 * GNUNET_TESTBED_operation_done() is called on the operation returned
469 * from GNUNET_TESTBED_service_connect()
470 * @return service handle to return in 'op_result', NULL on error
473 dht_connect (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
476 return GNUNET_DHT_connect (cfg, 10);
481 * Adapter function called to destroy a connection to
484 * @param cls the active context
485 * @param op_result service handle returned from the connect adapter
488 dht_disconnect (void *cls, void *op_result)
490 struct ActiveContext *ac = cls;
492 GNUNET_assert (NULL != ac->dht);
493 GNUNET_assert (ac->dht == op_result);
494 GNUNET_DHT_disconnect (ac->dht);
497 GNUNET_SCHEDULER_shutdown ();
502 * Callback called when DHT service on the peer is started
504 * @param cls the context
505 * @param op the operation that has been finished
506 * @param emsg error message in case the operation has failed; will be NULL if
507 * operation has executed successfully.
510 service_started (void *cls,
511 struct GNUNET_TESTBED_Operation *op,
514 struct Context *ctx = cls;
516 GNUNET_assert (NULL != ctx);
517 GNUNET_assert (NULL != ctx->op);
518 GNUNET_TESTBED_operation_done (ctx->op);
522 /* FIXME: connect to the DHT service and wait before starting a PUT */
523 ctx->op = GNUNET_TESTBED_service_connect (ctx, ctx->peer,
525 &dht_connected, ctx->ac,
533 * Signature of a main function for a testcase.
536 * @param h the run handle
537 * @param num_peers number of peers in 'peers'
538 * @param peers handle to peers run in the testbed
539 * @param links_succeeded the number of overlay link connection attempts that
541 * @param links_failed the number of overlay link
545 struct GNUNET_TESTBED_RunHandle *h,
546 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
547 unsigned int links_succeeded,
548 unsigned int links_failed)
558 INFO ("%u peers started\n", num_peers);
559 a_ctx = GNUNET_malloc (sizeof (struct Context) * num_peers);
561 /* select the peers which actively participate in profiling */
562 n_active = num_peers * PUT_PROBABILITY / 100;
565 GNUNET_SCHEDULER_shutdown ();
569 a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
571 for (cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
573 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
576 a_ctx[cnt].ac = &a_ac[ac_cnt];
577 a_ac[ac_cnt].ctx = &a_ctx[cnt];
581 a_ac = GNUNET_realloc (a_ac, n_active * sizeof (struct ActiveContext));
582 INFO ("Active peers: %u\n", n_active);
584 /* start DHT service on all peers */
585 for (cnt = 0; cnt < num_peers; cnt++)
587 a_ctx[cnt].peer = peers[cnt];
588 a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
599 * Main function that will be run by the scheduler.
602 * @param args remaining command-line arguments
603 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
604 * @param config configuration
607 run (void *cls, char *const *args, const char *cfgfile,
608 const struct GNUNET_CONFIGURATION_Handle *config)
614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
618 cfg = GNUNET_CONFIGURATION_dup (config);
620 GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, NULL,
621 NULL, &test_run, NULL);
622 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
630 * @return 0 on success
633 main (int argc, char *const *argv)
637 static struct GNUNET_GETOPT_CommandLineOption options[] = {
638 {'n', "peers", "COUNT",
639 gettext_noop ("number of peers to start"),
640 1, &GNUNET_GETOPT_set_uint, &num_peers},
641 {'H', "hosts", "FILENAME",
642 gettext_noop ("name of the file with the login information for the testbed"),
643 1, &GNUNET_GETOPT_set_string, &hosts_file},
644 {'d', "delay", "DELAY",
645 gettext_noop ("delay for starting DHT PUT and GET"),
646 1, &GNUNET_GETOPT_set_relative_time, &delay},
647 {'t', "timeout", "TIMEOUT",
648 gettext_noop ("timeout for DHT PUT and GET requests"),
649 1, &GNUNET_GETOPT_set_relative_time, &timeout},
650 GNUNET_GETOPT_OPTION_END
653 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
655 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3); /* default delay */
656 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3); /* default timeout */
659 GNUNET_PROGRAM_run (argc, argv, "dht-profiler",
661 ("Measure quality and performance of the DHT service."),
662 options, &run, NULL))