Merge branch 'master' of 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 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
19 /**
20  * @file fs/perf_gnunet_service_fs_p2p_respect.c
21  * @brief profile P2P routing respect mechanism. Creates
22  *        a clique of NUM_DAEMONS (i.e. 3) where two
23  *        peers share (seed) different files and download
24  *        them from each other while all the other peers
25  *        just "leach" those files.  Ideally, the seeders
26  *        "learn" that they contribute (to each other),
27  *        and give the other seeder higher priority;
28  *        naturally, this only happens nicely for larger
29  *        files; finally, once the seeders are done, the
30  *        leachers should see fast download rates as well.
31  * @author Christian Grothoff
32  *
33  * Sample output:
34  * - 10 MB, 3 peers, with delays:
35  * Download speed of type `seeder 1' was 757 KiB/s
36  * Download speed of type `seeder 2' was 613 KiB/s
37  * Download speed of type `leach` was 539 KiB/s
38  *
39  * - 10 MB, 3 peers, without delays:
40  * Download speed of type `seeder 1' was 1784 KiB/s
41  * Download speed of type `seeder 2' was 1604 KiB/s
42  * Download speed of type `leach` was 1384 KiB/s
43  */
44 #include "platform.h"
45 #include "fs_test_lib.h"
46 #include "gnunet_testbed_service.h"
47
48 #define VERBOSE GNUNET_NO
49
50 /**
51  * File-size we use for testing.
52  */
53 #define FILESIZE (1024 * 1024 * 1)
54
55 /**
56  * How long until we give up on transmitting the message?
57  */
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
59
60 /**
61  * Number of daemons in clique, must be at least 3 (!).
62  */
63 #define NUM_DAEMONS 3
64
65 /**
66  * Seed for first file on offer.
67  */
68 #define SEED1 42
69
70 /**
71  * Seed for second file on offer.
72  */
73 #define SEED2 43
74
75 static struct GNUNET_TESTBED_Peer *daemons[NUM_DAEMONS];
76
77 static int ok;
78
79 static struct GNUNET_TIME_Absolute start_time;
80
81 static const char *progname;
82
83 static struct GNUNET_FS_Uri *uri1;
84
85 static struct GNUNET_FS_Uri *uri2;
86
87 static char *fn1;
88
89 static char *fn2;
90
91 /**
92  * Master context for 'stat_run'.
93  */
94 struct StatMaster
95 {
96   struct GNUNET_STATISTICS_Handle *stat;
97   struct GNUNET_TESTBED_Operation *op;
98   unsigned int daemon;
99   unsigned int value;
100 };
101
102 struct StatValues
103 {
104   const char *subsystem;
105   const char *name;
106 };
107
108 /**
109  * Statistics we print out.
110  */
111 static struct StatValues stats[] = {
112   {"fs", "# artificial delays introduced (ms)"},
113   {"fs", "# queries forwarded"},
114   {"fs", "# replies received and matched"},
115   {"fs", "# results found locally"},
116   {"fs", "# requests forwarded due to high load"},
117   {"fs", "# requests done for free (low load)"},
118   {"fs", "# requests dropped, priority insufficient"},
119   {"fs", "# requests done for a price (normal load)"},
120   {"fs", "# requests dropped by datastore (queue length limit)"},
121   {"fs", "# P2P searches received"},
122   {"fs", "# P2P searches discarded (queue length bound)"},
123   {"fs", "# replies received for local clients"},
124   {"fs", "# queries retransmitted to same target"},
125   {"core", "# bytes decrypted"},
126   {"core", "# bytes encrypted"},
127   {"core", "# discarded CORE_SEND requests"},
128   {"core", "# discarded lower priority CORE_SEND requests"},
129   {"transport", "# bytes received via TCP"},
130   {"transport", "# bytes transmitted via TCP"},
131   {"datacache", "# bytes stored"},
132   {NULL, NULL}
133 };
134
135
136 static void
137 cleanup ()
138 {
139   GNUNET_SCHEDULER_shutdown ();
140   if (NULL != fn1)
141   {
142     GNUNET_DISK_directory_remove (fn1);
143     GNUNET_free (fn1);
144   }
145   if (NULL != fn2)
146   {
147     GNUNET_DISK_directory_remove (fn2);
148     GNUNET_free (fn2);
149   }
150 }
151
152
153 /**
154  * Callback function to process statistic values.
155  *
156  * @param cls closure
157  * @param subsystem name of subsystem that created the statistic
158  * @param name the name of the datum
159  * @param value the current value
160  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
161  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
162  */
163 static int
164 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
165             int is_persistent)
166 {
167   struct StatMaster *sm = cls;
168
169   FPRINTF (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
170            name, (unsigned long long) value);
171   return GNUNET_OK;
172 }
173
174
175 /**
176  * Function that gathers stats from all daemons.
177  */
178 static void
179 stat_run (void *cls,
180           struct GNUNET_TESTBED_Operation *op,
181           void *ca_result,
182           const char *emsg);
183
184
185 /**
186  * Function called when GET operation on stats is done.
187  */
188 static void
189 get_done (void *cls, int success)
190 {
191   struct StatMaster *sm = cls;
192
193   GNUNET_break (GNUNET_OK == success);
194   sm->value++;
195   stat_run (sm, sm->op, sm->stat, NULL);
196 }
197
198
199
200 /**
201  * Adapter function called to establish a connection to
202  * statistics service.
203  *
204  * @param cls closure
205  * @param cfg configuration of the peer to connect to; will be available until
206  *          GNUNET_TESTBED_operation_done() is called on the operation returned
207  *          from GNUNET_TESTBED_service_connect()
208  * @return service handle to return in 'op_result', NULL on error
209  */
210 static void *
211 statistics_connect_adapter (void *cls,
212                             const struct GNUNET_CONFIGURATION_Handle *cfg)
213 {
214   return GNUNET_STATISTICS_create ("<driver>",
215                                    cfg);
216 }
217
218
219 /**
220  * Adapter function called to destroy a connection to
221  * statistics service.
222  *
223  * @param cls closure
224  * @param op_result service handle returned from the connect adapter
225  */
226 static void
227 statistics_disconnect_adapter (void *cls,
228                                void *op_result)
229 {
230   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
231 }
232
233
234 /**
235  * Function that gathers stats from all daemons.
236  */
237 static void
238 stat_run (void *cls,
239           struct GNUNET_TESTBED_Operation *op,
240           void *ca_result,
241           const char *emsg)
242 {
243   struct StatMaster *sm = cls;
244
245   sm->stat = ca_result;
246   GNUNET_assert (NULL != sm->stat);
247   if (NULL != stats[sm->value].name)
248   {
249     GNUNET_STATISTICS_get (sm->stat,
250 #if 0
251                            NULL, NULL,
252 #else
253                            stats[sm->value].subsystem, stats[sm->value].name,
254 #endif
255                            &get_done, &print_stat,
256                            sm);
257     return;
258   }
259   GNUNET_TESTBED_operation_done (sm->op);
260   sm->value = 0;
261   sm->daemon++;
262   if (NUM_DAEMONS == sm->daemon)
263   {
264     GNUNET_free (sm);
265     cleanup ();
266     return;
267   }
268   sm->op =
269     GNUNET_TESTBED_service_connect (NULL,
270                                     daemons[sm->daemon],
271                                     "statistics",
272                                     &stat_run, sm,
273                                     &statistics_connect_adapter,
274                                     &statistics_disconnect_adapter,
275                                     NULL);
276 }
277
278
279 static void
280 do_report (void *cls)
281 {
282   static int download_counter;
283   const char *type = cls;
284   struct GNUNET_TIME_Relative del;
285   char *fancy;
286   struct StatMaster *sm;
287
288   if (0 ==
289       GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
290                                                                     TIMEOUT)).rel_value_us)
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 */