97af79b1a18d454a1e726308e8f13fe200aee899
[oweals/gnunet.git] / src / dht / gnunet_dht_profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2014 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 /**
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
32 #define INFO(...)                                       \
33   GNUNET_log (GNUNET_ERROR_TYPE_INFO, __VA_ARGS__)
34
35 #define DEBUG(...)                                           \
36   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
37
38 /**
39  * Number of peers which should perform a PUT out of 100 peers
40  */
41 #define PUT_PROBABILITY 50
42
43 /**
44  * Configuration
45  */
46 static struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 /**
49  * Name of the file with the hosts to run the test over
50  */
51 static char *hosts_file;
52
53 /**
54  * Context for a peer which actively does DHT PUT/GET
55  */
56 struct ActiveContext;
57
58 /**
59  * Context to hold data of peer
60  */
61 struct Context
62 {
63
64   /**
65    * The testbed peer this context belongs to
66    */
67   struct GNUNET_TESTBED_Peer *peer;
68
69   /**
70    * Testbed operation acting on this peer
71    */
72   struct GNUNET_TESTBED_Operation *op;
73
74   /**
75    * Active context; NULL if this peer is not an active peer
76    */
77   struct ActiveContext *ac;
78 };
79
80
81 /**
82  * Context for a peer which actively does DHT PUT/GET
83  */
84 struct ActiveContext
85 {
86   /**
87    * The linked peer context
88    */
89   struct Context *ctx;
90
91   /**
92    * Handler to the DHT service
93    */
94   struct GNUNET_DHT_Handle *dht;
95
96   /**
97    * The data used for do a PUT.  Will be NULL if a PUT hasn't been performed yet
98    */
99   void *put_data;
100
101   /**
102    * The active context used for our DHT GET
103    */
104   struct ActiveContext *get_ac;
105
106   /**
107    * The put handle
108    */
109   struct GNUNET_DHT_PutHandle *dht_put;
110
111   /**
112    * The get handle
113    */
114   struct GNUNET_DHT_GetHandle *dht_get;
115
116   /**
117    * The hash of the @e put_data
118    */
119   struct GNUNET_HashCode hash;
120
121   /**
122    * Delay task
123    */
124   GNUNET_SCHEDULER_TaskIdentifier delay_task;
125
126   /**
127    * The size of the @e put_data
128    */
129   uint16_t put_data_size;
130
131   /**
132    * The number of peers currently doing GET on our data
133    */
134   uint16_t nrefs;
135 };
136
137
138 /**
139  * An array of contexts.  The size of this array should be equal to @a num_peers
140  */
141 static struct Context *a_ctx;
142
143 /**
144  * Array of active peers
145  */
146 static struct ActiveContext *a_ac;
147
148 /**
149  * The delay between starting to do PUTS and GETS
150  */
151 static struct GNUNET_TIME_Relative delay;
152
153 /**
154  * The timeout for GET and PUT
155  */
156 static struct GNUNET_TIME_Relative timeout;
157
158 /**
159  * Number of peers
160  */
161 static unsigned int num_peers;
162
163 /**
164  * Number of active peers
165  */
166 static unsigned int n_active;
167
168 /**
169  * Number of DHT service connections we currently have
170  */
171 static unsigned int n_dht;
172
173 /**
174  * Number of DHT PUTs made
175  */
176 static unsigned int n_puts;
177
178 /**
179  * Number of DHT PUTs succeeded
180  */
181 static unsigned int n_puts_ok;
182
183 /**
184  * Number of DHT PUTs failed
185  */
186 static unsigned int n_puts_fail;
187
188 /**
189  * Number of DHT GETs made
190  */
191 static unsigned int n_gets;
192
193 /**
194  * Number of DHT GETs succeeded
195  */
196 static unsigned int n_gets_ok;
197
198 /**
199  * Number of DHT GETs succeeded
200  */
201 static unsigned int n_gets_fail;
202
203 /**
204  * Replication degree
205  */
206 static unsigned int replication;
207
208
209 /**
210  * Shutdown task.  Cleanup all resources and operations.
211  *
212  * @param cls NULL
213  * @param tc scheduler task context
214  */
215 static void
216 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   struct ActiveContext *ac;
219   unsigned int cnt;
220
221   if (NULL != a_ctx)
222   {
223     for (cnt=0; cnt < num_peers; cnt++)
224     {
225       if (NULL != a_ctx[cnt].op)
226         GNUNET_TESTBED_operation_done (a_ctx[cnt].op);
227
228       /* Cleanup active context if this peer is an active peer */
229       ac = a_ctx[cnt].ac;
230       if (NULL == ac)
231         continue;
232       if (GNUNET_SCHEDULER_NO_TASK != ac->delay_task)
233         GNUNET_SCHEDULER_cancel (ac->delay_task);
234       if (NULL != ac->put_data)
235         GNUNET_free (ac->put_data);
236       if (NULL != ac->dht_put)
237         GNUNET_DHT_put_cancel (ac->dht_put);
238       if (NULL != ac->dht_get)
239         GNUNET_DHT_get_stop (ac->dht_get);
240     }
241     GNUNET_free (a_ctx);
242     a_ctx = NULL;
243   }
244   GNUNET_free_non_null (a_ac);
245 }
246
247
248 static void
249 summarize ()
250 {
251   INFO ("# PUTS made: %u\n", n_puts);
252   INFO ("# PUTS succeeded: %u\n", n_puts_ok);
253   INFO ("# PUTS failed: %u\n", n_puts_fail);
254   INFO ("# GETS made: %u\n", n_gets);
255   INFO ("# GETS succeeded: %u\n", n_gets_ok);
256   INFO ("# GETS failed: %u\n", n_gets_fail);
257   GNUNET_SCHEDULER_shutdown ();
258 }
259
260
261 /**
262  * Task to cancel DHT GET.
263  *
264  * @param cls NULL
265  * @param tc scheduler task context
266  */
267 static void
268 cancel_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 {
270   struct ActiveContext *ac = cls;
271   struct Context *ctx = ac->ctx;
272
273   ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
274   GNUNET_assert (NULL != ac->dht_get);
275   GNUNET_DHT_get_stop (ac->dht_get);
276   ac->dht_get = NULL;
277   GNUNET_TESTBED_operation_done (ctx->op);
278   ctx->op = NULL;
279   n_gets_fail++;
280
281   /* If profiling is complete, summarize */
282   if (n_active == n_gets_fail + n_gets_ok)
283     summarize ();
284 }
285
286
287 /**
288  * Iterator called on each result obtained for a DHT
289  * operation that expects a reply
290  *
291  * @param cls closure
292  * @param exp when will this value expire
293  * @param key key of the result
294  * @param get_path peers on reply path (or NULL if not recorded)
295  *                 [0] = datastore's first neighbor, [length - 1] = local peer
296  * @param get_path_length number of entries in @a get_path
297  * @param put_path peers on the PUT path (or NULL if not recorded)
298  *                 [0] = origin, [length - 1] = datastore
299  * @param put_path_length number of entries in @a put_path
300  * @param type type of the result
301  * @param size number of bytes in @a data
302  * @param data pointer to the result data
303  */
304 static void
305 get_iter (void *cls,
306           struct GNUNET_TIME_Absolute exp,
307           const struct GNUNET_HashCode *key,
308           const struct GNUNET_PeerIdentity *get_path,
309           unsigned int get_path_length,
310           const struct GNUNET_PeerIdentity *put_path,
311           unsigned int put_path_length,
312           enum GNUNET_BLOCK_Type type,
313           size_t size, const void *data)
314 {
315   struct ActiveContext *ac = cls;
316   struct ActiveContext *get_ac = ac->get_ac;
317   struct Context *ctx = ac->ctx;
318
319   /* Check the keys of put and get match or not. */
320   GNUNET_assert (0 == memcmp (key, &get_ac->hash, sizeof (struct GNUNET_HashCode)));
321   /* we found the data we are looking for */
322   DEBUG ("We found a GET request; %u remaining\n", n_gets - (n_gets_fail + n_gets_ok));
323   n_gets_ok++;
324   get_ac->nrefs--;
325   GNUNET_DHT_get_stop (ac->dht_get);
326   ac->dht_get = NULL;
327   GNUNET_SCHEDULER_cancel (ac->delay_task);
328   ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
329   GNUNET_TESTBED_operation_done (ctx->op);
330   ctx->op = NULL;
331   
332   /* Summarize if profiling is complete */
333   if (n_active == n_gets_fail + n_gets_ok)
334     summarize ();
335 }
336
337
338 /**
339  * Task to do DHT GETs
340  *
341  * @param cls the active context
342  * @param tc the scheduler task context
343  */
344 static void
345 delayed_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 {
347   struct ActiveContext *ac = cls;
348   struct ActiveContext *get_ac;
349   unsigned int r;
350
351   ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
352   get_ac = NULL;
353   while (1)
354   {
355     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, n_active);
356     get_ac = &a_ac[r];
357     if (NULL != get_ac->put_data)
358       break;
359   }
360   get_ac->nrefs++;
361   ac->get_ac = get_ac;
362   DEBUG ("Doing a DHT GET for data of size %u\n", get_ac->put_data_size);
363   ac->dht_get = GNUNET_DHT_get_start (ac->dht,
364                                       GNUNET_BLOCK_TYPE_TEST,
365                                       &get_ac->hash,
366                                       1, /* replication level */
367                                       GNUNET_DHT_RO_NONE,
368                                       NULL, 0, /* extended query and size */
369                                       get_iter, ac); /* GET iterator and closure
370                                                         */
371   n_gets++;
372
373   /* schedule the timeout task for GET */
374   ac->delay_task = GNUNET_SCHEDULER_add_delayed (timeout, &cancel_get, ac);
375 }
376
377
378 /**
379  * Queue up a delayed task for doing DHT GET
380  *
381  * @param cls the active context
382  * @param success #GNUNET_OK if the PUT was transmitted,
383  *                #GNUNET_NO on timeout,
384  *                #GNUNET_SYSERR on disconnect from service
385  *                after the PUT message was transmitted
386  *                (so we don't know if it was received or not)
387  */
388 static void
389 put_cont (void *cls, int success)
390 {
391   struct ActiveContext *ac = cls;
392
393   ac->dht_put = NULL;
394   if (success)
395     n_puts_ok++;
396   else
397     n_puts_fail++;
398   ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_get, ac);
399 }
400
401
402 /**
403  * Task to do DHT PUTS
404  *
405  * @param cls the active context
406  * @param tc the scheduler task context
407  */
408 static void
409 delayed_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
410 {
411   struct ActiveContext *ac = cls;
412
413   ac->delay_task = GNUNET_SCHEDULER_NO_TASK;
414   /* Generate and DHT PUT some random data */
415   ac->put_data_size = 16;       /* minimum */
416   ac->put_data_size += GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
417                                                  (63*1024));
418   ac->put_data = GNUNET_malloc (ac->put_data_size);
419   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
420                               ac->put_data, ac->put_data_size);
421   GNUNET_CRYPTO_hash (ac->put_data, ac->put_data_size, &ac->hash);
422   DEBUG ("Doing a DHT PUT with data of size %u\n", ac->put_data_size);
423   ac->dht_put = GNUNET_DHT_put (ac->dht, &ac->hash,
424                                 replication,
425                                 GNUNET_DHT_RO_NONE,
426                                 GNUNET_BLOCK_TYPE_TEST,
427                                 ac->put_data_size,
428                                 ac->put_data,
429                                 GNUNET_TIME_UNIT_FOREVER_ABS, /* expiration time */
430                                 timeout,                      /* PUT timeout */
431                                 put_cont, ac);                /* continuation and its closure */
432   n_puts++;
433 }
434
435
436 /**
437  * Connection to DHT has been established.  Call the delay task.
438  *
439  * @param cls the active context
440  * @param op the operation that has been finished
441  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
442  * @param emsg error message in case the operation has failed; will be NULL if
443  *          operation has executed successfully.
444  */
445 static void
446 dht_connected (void *cls,
447                struct GNUNET_TESTBED_Operation *op,
448                void *ca_result,
449                const char *emsg)
450 {
451   struct ActiveContext *ac = cls;
452   struct Context *ctx = ac->ctx;
453
454   GNUNET_assert (NULL != ctx);
455   GNUNET_assert (NULL != ctx->op);
456   GNUNET_assert (ctx->op == op);
457   ac->dht = (struct GNUNET_DHT_Handle *) ca_result;
458   if (NULL != emsg)
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
461     GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect() */
462     ctx->op = NULL;
463     return;
464   }
465   ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay, &delayed_put, ac);
466 }
467
468
469 /**
470  * Connect to DHT service and return the DHT client handler
471  *
472  * @param cls the active context
473  * @param cfg configuration of the peer to connect to; will be available until
474  *          GNUNET_TESTBED_operation_done() is called on the operation returned
475  *          from GNUNET_TESTBED_service_connect()
476  * @return service handle to return in 'op_result', NULL on error
477  */
478 static void *
479 dht_connect (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
480 {
481   n_dht++;
482   return GNUNET_DHT_connect (cfg, 10);
483 }
484
485
486 /**
487  * Adapter function called to destroy a connection to
488  * a service.
489  *
490  * @param cls the active context
491  * @param op_result service handle returned from the connect adapter
492  */
493 static void
494 dht_disconnect (void *cls, void *op_result)
495 {
496   struct ActiveContext *ac = cls;
497
498   GNUNET_assert (NULL != ac->dht);
499   GNUNET_assert (ac->dht == op_result);
500   GNUNET_DHT_disconnect (ac->dht);
501   n_dht--;
502   if (0 == n_dht)
503     GNUNET_SCHEDULER_shutdown ();
504 }
505
506
507 /**
508  * Callback called when DHT service on the peer is started
509  *
510  * @param cls the context
511  * @param op the operation that has been finished
512  * @param emsg error message in case the operation has failed; will be NULL if
513  *          operation has executed successfully.
514  */
515 static void
516 service_started (void *cls,
517                  struct GNUNET_TESTBED_Operation *op,
518                  const char *emsg)
519 {
520   struct Context *ctx = cls;
521
522   GNUNET_assert (NULL != ctx);
523   GNUNET_assert (NULL != ctx->op);
524   GNUNET_TESTBED_operation_done (ctx->op);
525   ctx->op = NULL;
526   if (NULL == ctx->ac)
527     return;
528   /* FIXME: connect to the DHT service and wait before starting a PUT */
529   ctx->op = GNUNET_TESTBED_service_connect (ctx, ctx->peer,
530                                             "dht",
531                                             &dht_connected, ctx->ac,
532                                             &dht_connect,
533                                             &dht_disconnect,
534                                             ctx->ac);
535 }
536
537
538 /**
539  * Signature of a main function for a testcase.
540  *
541  * @param cls closure
542  * @param h the run handle
543  * @param num_peers number of peers in 'peers'
544  * @param peers handle to peers run in the testbed
545  * @param links_succeeded the number of overlay link connection attempts that
546  *          succeeded
547  * @param links_failed the number of overlay link
548  */
549 static void
550 test_run (void *cls,
551           struct GNUNET_TESTBED_RunHandle *h,
552           unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
553           unsigned int links_succeeded,
554           unsigned int links_failed)
555 {
556   unsigned int cnt;
557   unsigned int ac_cnt;
558     
559   if (NULL == peers)
560   {
561     /* exit */
562     GNUNET_assert (0);
563   }
564   INFO ("%u peers started\n", num_peers);
565   a_ctx = GNUNET_malloc (sizeof (struct Context) * num_peers);
566
567   /* select the peers which actively participate in profiling */
568   n_active = num_peers * PUT_PROBABILITY / 100;
569   if (0 == n_active)
570   {
571     GNUNET_SCHEDULER_shutdown ();
572     GNUNET_free (a_ctx);
573     return;
574   }
575   a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
576   ac_cnt = 0;
577   for (cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
578   {
579     if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
580         PUT_PROBABILITY)
581       continue;
582     a_ctx[cnt].ac = &a_ac[ac_cnt];
583     a_ac[ac_cnt].ctx = &a_ctx[cnt];
584     ac_cnt++;
585   }
586   n_active = ac_cnt;
587   a_ac = GNUNET_realloc (a_ac, n_active * sizeof (struct ActiveContext));
588   INFO ("Active peers: %u\n", n_active);
589
590   /* start DHT service on all peers */
591   for (cnt = 0; cnt < num_peers; cnt++)
592   {
593     a_ctx[cnt].peer = peers[cnt];
594     a_ctx[cnt].op = GNUNET_TESTBED_peer_manage_service (&a_ctx[cnt],
595                                                         peers[cnt],
596                                                         "dht",
597                                                         &service_started,
598                                                         &a_ctx[cnt],
599                                                         1);
600   }
601 }
602
603
604 /**
605  * Main function that will be run by the scheduler.
606  *
607  * @param cls closure
608  * @param args remaining command-line arguments
609  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
610  * @param config configuration
611  */
612 static void
613 run (void *cls, char *const *args, const char *cfgfile,
614      const struct GNUNET_CONFIGURATION_Handle *config)
615 {
616   uint64_t event_mask;
617
618   if (0 == num_peers)
619   {
620     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting as the number of peers is %u\n"),
621                 num_peers);
622     return;
623   }
624   cfg = GNUNET_CONFIGURATION_dup (config);
625   event_mask = 0;
626   GNUNET_TESTBED_run (hosts_file, cfg, num_peers, event_mask, NULL,
627                       NULL, &test_run, NULL);
628   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
629                                 NULL);
630 }
631
632
633 /**
634  * Main function.
635  *
636  * @return 0 on success
637  */
638 int
639 main (int argc, char *const *argv)
640 {
641   int rc;
642
643   static struct GNUNET_GETOPT_CommandLineOption options[] = {
644     {'n', "peers", "COUNT",
645      gettext_noop ("number of peers to start"),
646      1, &GNUNET_GETOPT_set_uint, &num_peers},
647     {'H', "hosts", "FILENAME",
648      gettext_noop ("name of the file with the login information for the testbed"),
649      1, &GNUNET_GETOPT_set_string, &hosts_file},
650     {'d', "delay", "DELAY",
651      gettext_noop ("delay for starting DHT PUT and GET"),
652      1, &GNUNET_GETOPT_set_relative_time, &delay},
653     {'r', "replication", "DEGREE",
654      gettext_noop ("replication degree for DHT PUTs"),
655      1, &GNUNET_GETOPT_set_uint, &replication},
656     {'t', "timeout", "TIMEOUT",
657      gettext_noop ("timeout for DHT PUT and GET requests"),
658      1, &GNUNET_GETOPT_set_relative_time, &timeout},
659     GNUNET_GETOPT_OPTION_END
660   };
661
662   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
663     return 2;
664   delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3); /* default delay */
665   timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3); /* default timeout */
666   replication = 1;      /* default replication */
667   rc = 0;
668   if (GNUNET_OK !=
669       GNUNET_PROGRAM_run (argc, argv, "dht-profiler",
670                           gettext_noop
671                           ("Measure quality and performance of the DHT service."),
672                           options, &run, NULL))
673     rc = 1;
674   return rc;
675 }