Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / fs / perf_gnunet_service_fs_p2p_respect.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2011, 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/perf_gnunet_service_fs_p2p_respect.c
23  * @brief profile P2P routing respect mechanism. Creates
24  *        a clique of NUM_DAEMONS (i.e. 3) where two
25  *        peers share (seed) different files and download
26  *        them from each other while all the other peers
27  *        just "leach" those files.  Ideally, the seeders
28  *        "learn" that they contribute (to each other),
29  *        and give the other seeder higher priority;
30  *        naturally, this only happens nicely for larger
31  *        files; finally, once the seeders are done, the
32  *        leachers should see fast download rates as well.
33  * @author Christian Grothoff
34  *
35  * Sample output:
36  * - 10 MB, 3 peers, with delays:
37  * Download speed of type `seeder 1' was 757 KiB/s
38  * Download speed of type `seeder 2' was 613 KiB/s
39  * Download speed of type `leach` was 539 KiB/s
40  *
41  * - 10 MB, 3 peers, without delays:
42  * Download speed of type `seeder 1' was 1784 KiB/s
43  * Download speed of type `seeder 2' was 1604 KiB/s
44  * Download speed of type `leach` was 1384 KiB/s
45  */
46 #include "platform.h"
47 #include "fs_test_lib.h"
48 #include "gnunet_testbed_service.h"
49
50 #define VERBOSE GNUNET_NO
51
52 /**
53  * File-size we use for testing.
54  */
55 #define FILESIZE (1024 * 1024 * 1)
56
57 /**
58  * How long until we give up on transmitting the message?
59  */
60 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
61
62 /**
63  * Number of daemons in clique, must be at least 3 (!).
64  */
65 #define NUM_DAEMONS 3
66
67 /**
68  * Seed for first file on offer.
69  */
70 #define SEED1 42
71
72 /**
73  * Seed for second file on offer.
74  */
75 #define SEED2 43
76
77 static struct GNUNET_TESTBED_Peer *daemons[NUM_DAEMONS];
78
79 static int ok;
80
81 static struct GNUNET_TIME_Absolute start_time;
82
83 static const char *progname;
84
85 static struct GNUNET_FS_Uri *uri1;
86
87 static struct GNUNET_FS_Uri *uri2;
88
89 static char *fn1;
90
91 static char *fn2;
92
93 /**
94  * Master context for 'stat_run'.
95  */
96 struct StatMaster
97 {
98   struct GNUNET_STATISTICS_Handle *stat;
99   struct GNUNET_TESTBED_Operation *op;
100   unsigned int daemon;
101   unsigned int value;
102 };
103
104 struct StatValues
105 {
106   const char *subsystem;
107   const char *name;
108 };
109
110 /**
111  * Statistics we print out.
112  */
113 static struct StatValues stats[] = {
114   {"fs", "# artificial delays introduced (ms)"},
115   {"fs", "# queries forwarded"},
116   {"fs", "# replies received and matched"},
117   {"fs", "# results found locally"},
118   {"fs", "# requests forwarded due to high load"},
119   {"fs", "# requests done for free (low load)"},
120   {"fs", "# requests dropped, priority insufficient"},
121   {"fs", "# requests done for a price (normal load)"},
122   {"fs", "# requests dropped by datastore (queue length limit)"},
123   {"fs", "# P2P searches received"},
124   {"fs", "# P2P searches discarded (queue length bound)"},
125   {"fs", "# replies received for local clients"},
126   {"fs", "# queries retransmitted to same target"},
127   {"core", "# bytes decrypted"},
128   {"core", "# bytes encrypted"},
129   {"core", "# discarded CORE_SEND requests"},
130   {"core", "# discarded lower priority CORE_SEND requests"},
131   {"transport", "# bytes received via TCP"},
132   {"transport", "# bytes transmitted via TCP"},
133   {"datacache", "# bytes stored"},
134   {NULL, NULL}
135 };
136
137
138 static void
139 cleanup ()
140 {
141   GNUNET_SCHEDULER_shutdown ();
142   if (NULL != fn1)
143   {
144     GNUNET_DISK_directory_remove (fn1);
145     GNUNET_free (fn1);
146   }
147   if (NULL != fn2)
148   {
149     GNUNET_DISK_directory_remove (fn2);
150     GNUNET_free (fn2);
151   }
152 }
153
154
155 /**
156  * Callback function to process statistic values.
157  *
158  * @param cls closure
159  * @param subsystem name of subsystem that created the statistic
160  * @param name the name of the datum
161  * @param value the current value
162  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
163  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
164  */
165 static int
166 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
167             int is_persistent)
168 {
169   struct StatMaster *sm = cls;
170
171   FPRINTF (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
172            name, (unsigned long long) value);
173   return GNUNET_OK;
174 }
175
176
177 /**
178  * Function that gathers stats from all daemons.
179  */
180 static void
181 stat_run (void *cls,
182           struct GNUNET_TESTBED_Operation *op,
183           void *ca_result,
184           const char *emsg);
185
186
187 /**
188  * Function called when GET operation on stats is done.
189  */
190 static void
191 get_done (void *cls, int success)
192 {
193   struct StatMaster *sm = cls;
194
195   GNUNET_break (GNUNET_OK == success);
196   sm->value++;
197   stat_run (sm, sm->op, sm->stat, NULL);
198 }
199
200
201
202 /**
203  * Adapter function called to establish a connection to
204  * statistics service.
205  *
206  * @param cls closure
207  * @param cfg configuration of the peer to connect to; will be available until
208  *          GNUNET_TESTBED_operation_done() is called on the operation returned
209  *          from GNUNET_TESTBED_service_connect()
210  * @return service handle to return in 'op_result', NULL on error
211  */
212 static void *
213 statistics_connect_adapter (void *cls,
214                             const struct GNUNET_CONFIGURATION_Handle *cfg)
215 {
216   return GNUNET_STATISTICS_create ("<driver>",
217                                    cfg);
218 }
219
220
221 /**
222  * Adapter function called to destroy a connection to
223  * statistics service.
224  *
225  * @param cls closure
226  * @param op_result service handle returned from the connect adapter
227  */
228 static void
229 statistics_disconnect_adapter (void *cls,
230                                void *op_result)
231 {
232   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
233 }
234
235
236 /**
237  * Function that gathers stats from all daemons.
238  */
239 static void
240 stat_run (void *cls,
241           struct GNUNET_TESTBED_Operation *op,
242           void *ca_result,
243           const char *emsg)
244 {
245   struct StatMaster *sm = cls;
246
247   sm->stat = ca_result;
248   GNUNET_assert (NULL != sm->stat);
249   if (NULL != stats[sm->value].name)
250   {
251     GNUNET_STATISTICS_get (sm->stat,
252 #if 0
253                            NULL, NULL,
254 #else
255                            stats[sm->value].subsystem, stats[sm->value].name,
256 #endif
257                            &get_done, &print_stat,
258                            sm);
259     return;
260   }
261   GNUNET_TESTBED_operation_done (sm->op);
262   sm->value = 0;
263   sm->daemon++;
264   if (NUM_DAEMONS == sm->daemon)
265   {
266     GNUNET_free (sm);
267     cleanup ();
268     return;
269   }
270   sm->op =
271     GNUNET_TESTBED_service_connect (NULL,
272                                     daemons[sm->daemon],
273                                     "statistics",
274                                     &stat_run, sm,
275                                     &statistics_connect_adapter,
276                                     &statistics_disconnect_adapter,
277                                     NULL);
278 }
279
280
281 static void
282 do_report (void *cls)
283 {
284   static int download_counter;
285   const char *type = cls;
286   struct GNUNET_TIME_Relative del;
287   char *fancy;
288   struct StatMaster *sm;
289
290   if (0 ==
291       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
292                                                                     TIMEOUT)).rel_value_us)
293   {
294     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295                 "Timeout during download for type `%s', shutting down with error\n",
296                 type);
297     ok = 1;
298     cleanup ();
299     return;
300   }
301   del = GNUNET_TIME_absolute_get_duration (start_time);
302   if (del.rel_value_us == 0)
303     del.rel_value_us = 1;
304   fancy =
305     GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
306                                     1000000LL / del.rel_value_us);
307   FPRINTF (stderr, "Download speed of type `%s' was %s/s\n", type, fancy);
308   GNUNET_free (fancy);
309   if (NUM_DAEMONS != ++download_counter)
310     return;                   /* more downloads to come */
311   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312               "Finished all downloads, getting statistics\n");
313   sm = GNUNET_new (struct StatMaster);
314   sm->op =
315     GNUNET_TESTBED_service_connect (NULL,
316                                     daemons[sm->daemon],
317                                     "statistics",
318                                     &stat_run, sm,
319                                     &statistics_connect_adapter,
320                                     &statistics_disconnect_adapter,
321                                     NULL);
322 }
323
324
325 static void
326 do_downloads (void *cls, const struct GNUNET_FS_Uri *u2,
327               const char *fn)
328 {
329   int anonymity;
330   unsigned int i;
331
332   if (NULL == u2)
333   {
334     cleanup ();
335     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336                 "Timeout during upload attempt, shutting down with error\n");
337     ok = 1;
338     return;
339   }
340   if (NULL != fn)
341     fn2 = GNUNET_strdup (fn);
342   uri2 = GNUNET_FS_uri_dup (u2);
343   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
344               (unsigned long long) FILESIZE);
345   start_time = GNUNET_TIME_absolute_get ();
346   if (NULL != strstr (progname, "dht"))
347     anonymity = 0;
348   else
349     anonymity = 1;
350   /* (semi) leach-download(s); not true leaches since
351    * these peers do participate in sharing, they just
352    * don't have to offer anything *initially*.  */
353   for (i = 0; i < NUM_DAEMONS - 2; i++)
354     GNUNET_FS_TEST_download (daemons[i], TIMEOUT, anonymity,
355                              0 == (i % 2) ? SEED1 : SEED2,
356                              0 == (i % 2) ? uri1 : uri2, VERBOSE, &do_report,
357                              "leach");
358   /* mutual downloads of (primary) sharing peers */
359   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity, SEED1,
360                            uri1, VERBOSE, &do_report, "seeder 2");
361   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity, SEED2,
362                            uri2, VERBOSE, &do_report, "seeder 1");
363 }
364
365
366 static void
367 do_publish2 (void *cls,
368              const struct GNUNET_FS_Uri *u1,
369              const char *fn)
370 {
371   int do_index;
372   int anonymity;
373
374   if (NULL == u1)
375   {
376     cleanup ();
377     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378                 "Timeout during upload attempt, shutting down with error\n");
379     ok = 1;
380     return;
381   }
382   if (NULL != fn)
383     fn1 = GNUNET_strdup (fn);
384   uri1 = GNUNET_FS_uri_dup (u1);
385   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
386               (unsigned long long) FILESIZE);
387   if (NULL != strstr (progname, "index"))
388     do_index = GNUNET_YES;
389   else
390     do_index = GNUNET_NO;
391   if (NULL != strstr (progname, "dht"))
392     anonymity = 0;
393   else
394     anonymity = 1;
395
396   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity,
397                           do_index, FILESIZE, SEED2, VERBOSE, &do_downloads,
398                           NULL);
399 }
400
401
402 static void
403 do_publish1 (void *cls,
404              struct GNUNET_TESTBED_Operation *op,
405              const char *emsg)
406 {
407   unsigned int *coco = cls;
408   int do_index;
409   int anonymity;
410
411   GNUNET_TESTBED_operation_done (op);
412   if (NULL != emsg)
413   {
414     cleanup ();
415     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error trying to connect: %s\n", emsg);
416     ok = 1;
417     return;
418   }
419   if (0 != (--(*coco)))
420     return; /* more connections to be created */
421   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
422               (unsigned long long) FILESIZE);
423   if (NULL != strstr (progname, "index"))
424     do_index = GNUNET_YES;
425   else
426     do_index = GNUNET_NO;
427   if (NULL != strstr (progname, "dht"))
428     anonymity = 0;
429   else
430     anonymity = 1;
431   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity,
432                           do_index, FILESIZE, SEED1, VERBOSE, &do_publish2,
433                           NULL);
434 }
435
436
437 static void
438 do_connect (void *cls,
439             struct GNUNET_TESTBED_RunHandle *h,
440             unsigned int num_peers,
441             struct GNUNET_TESTBED_Peer **peers,
442             unsigned int links_succeeded,
443             unsigned int links_failed)
444 {
445   static unsigned int coco;
446   unsigned int i;
447   unsigned int j;
448
449   GNUNET_assert (NUM_DAEMONS == num_peers);
450   for (i=0;i<num_peers;i++)
451     daemons[i] = peers[i];
452   for (i=0;i<NUM_DAEMONS;i++)
453     for (j=i+1;j<NUM_DAEMONS;j++)
454       {
455         coco++;
456         GNUNET_TESTBED_overlay_connect (NULL,
457                                         &do_publish1,
458                                         &coco,
459                                         peers[i],
460                                         peers[j]);
461       }
462 }
463
464
465 int
466 main (int argc, char *argv[])
467 {
468   progname = argv[0];
469   (void) GNUNET_TESTBED_test_run ("perf-gnunet-service-fs-p2p-respect",
470                                   "perf_gnunet_service_fs_p2p.conf",
471                                   NUM_DAEMONS,
472                                   0, NULL, NULL,
473                                   &do_connect, NULL);
474   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
475   return ok;
476 }
477
478 /* end of perf_gnunet_service_fs_p2p_respect.c */