-remove debug message
[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 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 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  * Adapter function called to establish a connection to
203  * statistics service.
204  *
205  * @param cls closure
206  * @param cfg configuration of the peer to connect to; will be available until
207  *          GNUNET_TESTBED_operation_done() is called on the operation returned
208  *          from GNUNET_TESTBED_service_connect()
209  * @return service handle to return in 'op_result', NULL on error
210  */
211 static void *
212 statistics_connect_adapter (void *cls,
213                             const struct GNUNET_CONFIGURATION_Handle *cfg)
214 {
215   return GNUNET_STATISTICS_create ("<driver>",
216                                    cfg);
217 }
218
219
220 /**
221  * Adapter function called to destroy a connection to
222  * statistics service.
223  *
224  * @param cls closure
225  * @param op_result service handle returned from the connect adapter
226  */
227 static void
228 statistics_disconnect_adapter (void *cls,
229                                void *op_result)
230 {
231   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
232 }
233
234
235 /**
236  * Function that gathers stats from all daemons.
237  */
238 static void
239 stat_run (void *cls,
240           struct GNUNET_TESTBED_Operation *op,
241           void *ca_result,
242           const char *emsg)
243 {
244   struct StatMaster *sm = cls;
245
246   sm->stat = ca_result;
247   GNUNET_assert (NULL != sm->stat);
248   if (NULL != stats[sm->value].name)
249   {
250     GNUNET_STATISTICS_get (sm->stat,
251 #if 0
252                            NULL, NULL,
253 #else
254                            stats[sm->value].subsystem, stats[sm->value].name,
255 #endif
256                            &get_done, &print_stat,
257                            sm);
258     return;
259   }
260   GNUNET_TESTBED_operation_done (sm->op);
261   sm->value = 0;
262   sm->daemon++;
263   if (NUM_DAEMONS == sm->daemon)
264   {
265     GNUNET_free (sm);
266     cleanup ();
267     return;
268   }
269   sm->op =
270     GNUNET_TESTBED_service_connect (NULL,
271                                     daemons[sm->daemon],
272                                     "statistics",
273                                     &stat_run, sm,
274                                     &statistics_connect_adapter,
275                                     &statistics_disconnect_adapter,
276                                     NULL);
277 }
278
279
280 static void
281 do_report (void *cls)
282 {
283   static int download_counter;
284   const char *type = cls;
285   struct GNUNET_TIME_Relative del;
286   char *fancy;
287   struct StatMaster *sm;
288
289   if (0 ==
290       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
291                                                                     TIMEOUT)).
292       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
479 /* end of perf_gnunet_service_fs_p2p_respect.c */