global reindent, now with uncrustify hook enabled
[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 /**
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)).
293       rel_value_us)
294   {
295     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296                 "Timeout during download for type `%s', shutting down with error\n",
297                 type);
298     ok = 1;
299     cleanup ();
300     return;
301   }
302   del = GNUNET_TIME_absolute_get_duration (start_time);
303   if (del.rel_value_us == 0)
304     del.rel_value_us = 1;
305   fancy =
306     GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE)
307                                     * 1000000LL / del.rel_value_us);
308   fprintf (stderr, "Download speed of type `%s' was %s/s\n", type, fancy);
309   GNUNET_free (fancy);
310   if (NUM_DAEMONS != ++download_counter)
311     return;                   /* more downloads to come */
312   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313               "Finished all downloads, getting statistics\n");
314   sm = GNUNET_new (struct StatMaster);
315   sm->op =
316     GNUNET_TESTBED_service_connect (NULL,
317                                     daemons[sm->daemon],
318                                     "statistics",
319                                     &stat_run, sm,
320                                     &statistics_connect_adapter,
321                                     &statistics_disconnect_adapter,
322                                     NULL);
323 }
324
325
326 static void
327 do_downloads (void *cls, const struct GNUNET_FS_Uri *u2,
328               const char *fn)
329 {
330   int anonymity;
331   unsigned int i;
332
333   if (NULL == u2)
334   {
335     cleanup ();
336     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
337                 "Timeout during upload attempt, shutting down with error\n");
338     ok = 1;
339     return;
340   }
341   if (NULL != fn)
342     fn2 = GNUNET_strdup (fn);
343   uri2 = GNUNET_FS_uri_dup (u2);
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
345               (unsigned long long) FILESIZE);
346   start_time = GNUNET_TIME_absolute_get ();
347   if (NULL != strstr (progname, "dht"))
348     anonymity = 0;
349   else
350     anonymity = 1;
351   /* (semi) leach-download(s); not true leaches since
352    * these peers do participate in sharing, they just
353    * don't have to offer anything *initially*.  */
354   for (i = 0; i < NUM_DAEMONS - 2; i++)
355     GNUNET_FS_TEST_download (daemons[i], TIMEOUT, anonymity,
356                              0 == (i % 2) ? SEED1 : SEED2,
357                              0 == (i % 2) ? uri1 : uri2, VERBOSE, &do_report,
358                              "leach");
359   /* mutual downloads of (primary) sharing peers */
360   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity, SEED1,
361                            uri1, VERBOSE, &do_report, "seeder 2");
362   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity, SEED2,
363                            uri2, VERBOSE, &do_report, "seeder 1");
364 }
365
366
367 static void
368 do_publish2 (void *cls,
369              const struct GNUNET_FS_Uri *u1,
370              const char *fn)
371 {
372   int do_index;
373   int anonymity;
374
375   if (NULL == u1)
376   {
377     cleanup ();
378     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379                 "Timeout during upload attempt, shutting down with error\n");
380     ok = 1;
381     return;
382   }
383   if (NULL != fn)
384     fn1 = GNUNET_strdup (fn);
385   uri1 = GNUNET_FS_uri_dup (u1);
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
387               (unsigned long long) FILESIZE);
388   if (NULL != strstr (progname, "index"))
389     do_index = GNUNET_YES;
390   else
391     do_index = GNUNET_NO;
392   if (NULL != strstr (progname, "dht"))
393     anonymity = 0;
394   else
395     anonymity = 1;
396
397   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity,
398                           do_index, FILESIZE, SEED2, VERBOSE, &do_downloads,
399                           NULL);
400 }
401
402
403 static void
404 do_publish1 (void *cls,
405              struct GNUNET_TESTBED_Operation *op,
406              const char *emsg)
407 {
408   unsigned int *coco = cls;
409   int do_index;
410   int anonymity;
411
412   GNUNET_TESTBED_operation_done (op);
413   if (NULL != emsg)
414   {
415     cleanup ();
416     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error trying to connect: %s\n", emsg);
417     ok = 1;
418     return;
419   }
420   if (0 != (--(*coco)))
421     return; /* more connections to be created */
422   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
423               (unsigned long long) FILESIZE);
424   if (NULL != strstr (progname, "index"))
425     do_index = GNUNET_YES;
426   else
427     do_index = GNUNET_NO;
428   if (NULL != strstr (progname, "dht"))
429     anonymity = 0;
430   else
431     anonymity = 1;
432   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity,
433                           do_index, FILESIZE, SEED1, VERBOSE, &do_publish2,
434                           NULL);
435 }
436
437
438 static void
439 do_connect (void *cls,
440             struct GNUNET_TESTBED_RunHandle *h,
441             unsigned int num_peers,
442             struct GNUNET_TESTBED_Peer **peers,
443             unsigned int links_succeeded,
444             unsigned int links_failed)
445 {
446   static unsigned int coco;
447   unsigned int i;
448   unsigned int j;
449
450   GNUNET_assert (NUM_DAEMONS == num_peers);
451   for (i = 0; i < num_peers; i++)
452     daemons[i] = peers[i];
453   for (i = 0; i < NUM_DAEMONS; i++)
454     for (j = i + 1; j < NUM_DAEMONS; j++)
455     {
456       coco++;
457       GNUNET_TESTBED_overlay_connect (NULL,
458                                       &do_publish1,
459                                       &coco,
460                                       peers[i],
461                                       peers[j]);
462     }
463 }
464
465
466 int
467 main (int argc, char *argv[])
468 {
469   progname = argv[0];
470   (void) GNUNET_TESTBED_test_run ("perf-gnunet-service-fs-p2p-respect",
471                                   "perf_gnunet_service_fs_p2p.conf",
472                                   NUM_DAEMONS,
473                                   0, NULL, NULL,
474                                   &do_connect, NULL);
475   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
476   return ok;
477 }
478
479 /* end of perf_gnunet_service_fs_p2p_respect.c */