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