-lots
[oweals/gnunet.git] / src / fs / perf_gnunet_service_fs_p2p_respect.c
1 /*
2      This file is part of GNUnet.
3      (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
90 /**
91  * Master context for 'stat_run'.
92  */
93 struct StatMaster
94 {
95   struct GNUNET_STATISTICS_Handle *stat;
96   struct GNUNET_TESTBED_Operation *op;
97   unsigned int daemon;
98   unsigned int value;
99 };
100
101 struct StatValues
102 {
103   const char *subsystem;
104   const char *name;
105 };
106
107 /**
108  * Statistics we print out.
109  */
110 static struct StatValues stats[] = {
111   {"fs", "# artificial delays introduced (ms)"},
112   {"fs", "# queries forwarded"},
113   {"fs", "# replies received and matched"},
114   {"fs", "# results found locally"},
115   {"fs", "# requests forwarded due to high load"},
116   {"fs", "# requests done for free (low load)"},
117   {"fs", "# requests dropped, priority insufficient"},
118   {"fs", "# requests done for a price (normal load)"},
119   {"fs", "# requests dropped by datastore (queue length limit)"},
120   {"fs", "# P2P searches received"},
121   {"fs", "# P2P searches discarded (queue length bound)"},
122   {"fs", "# replies received for local clients"},
123   {"fs", "# queries retransmitted to same target"},
124   {"core", "# bytes decrypted"},
125   {"core", "# bytes encrypted"},
126   {"core", "# discarded CORE_SEND requests"},
127   {"core", "# discarded lower priority CORE_SEND requests"},
128   {"transport", "# bytes received via TCP"},
129   {"transport", "# bytes transmitted via TCP"},
130   {"datacache", "# bytes stored"},
131   {NULL, NULL}
132 };
133
134
135 /**
136  * Callback function to process statistic values.
137  *
138  * @param cls closure
139  * @param subsystem name of subsystem that created the statistic
140  * @param name the name of the datum
141  * @param value the current value
142  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
143  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
144  */
145 static int
146 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
147             int is_persistent)
148 {
149   struct StatMaster *sm = cls;
150
151   FPRINTF (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
152            name, (unsigned long long) value);
153   return GNUNET_OK;
154 }
155
156
157 /**
158  * Function that gathers stats from all daemons.
159  */
160 static void
161 stat_run (void *cls, 
162           struct GNUNET_TESTBED_Operation *op,
163           void *ca_result,
164           const char *emsg);
165
166
167 /**
168  * Function called when GET operation on stats is done.
169  */
170 static void
171 get_done (void *cls, int success)
172 {
173   struct StatMaster *sm = cls;
174
175   GNUNET_break (GNUNET_OK == success);
176   sm->value++;
177   stat_run (sm, sm->op, sm->stat, NULL);
178 }
179
180
181
182 /**
183  * Adapter function called to establish a connection to
184  * statistics service.
185  * 
186  * @param cls closure
187  * @param cfg configuration of the peer to connect to; will be available until
188  *          GNUNET_TESTBED_operation_done() is called on the operation returned
189  *          from GNUNET_TESTBED_service_connect()
190  * @return service handle to return in 'op_result', NULL on error
191  */
192 static void *
193 statistics_connect_adapter (void *cls,
194                             const struct GNUNET_CONFIGURATION_Handle *cfg)
195 {
196   return GNUNET_STATISTICS_create ("<driver>",
197                                    cfg);
198 }
199
200
201 /**
202  * Adapter function called to destroy a connection to
203  * statistics service.
204  * 
205  * @param cls closure
206  * @param op_result service handle returned from the connect adapter
207  */
208 static void 
209 statistics_disconnect_adapter (void *cls,
210                                void *op_result)
211 {
212   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
213 }
214
215
216 /**
217  * Function that gathers stats from all daemons.
218  */
219 static void
220 stat_run (void *cls, 
221           struct GNUNET_TESTBED_Operation *op,
222           void *ca_result,
223           const char *emsg)
224 {
225   struct StatMaster *sm = cls;
226
227   sm->stat = ca_result;
228   GNUNET_assert (NULL != sm->stat);
229   if (NULL != stats[sm->value].name)
230   {
231     GNUNET_STATISTICS_get (sm->stat,
232 #if 0
233                            NULL, NULL,
234 #else
235                            stats[sm->value].subsystem, stats[sm->value].name,
236 #endif
237                            GNUNET_TIME_UNIT_FOREVER_REL, &get_done, &print_stat,
238                            sm);
239     return;
240   }
241   GNUNET_TESTBED_operation_done (sm->op);
242   sm->value = 0;
243   sm->daemon++;
244   if (NUM_DAEMONS == sm->daemon)
245   {
246     GNUNET_free (sm);
247     GNUNET_SCHEDULER_shutdown ();
248     return;
249   }
250   sm->op =
251     GNUNET_TESTBED_service_connect (NULL,
252                                     daemons[sm->daemon],
253                                     "statistics",
254                                     &stat_run, sm,
255                                     &statistics_connect_adapter,
256                                     &statistics_disconnect_adapter,
257                                     NULL);
258 }
259
260
261 static void
262 do_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
263 {
264   static int download_counter;
265   const char *type = cls;
266   struct GNUNET_TIME_Relative del;
267   char *fancy;
268   struct StatMaster *sm;
269
270   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
271   {
272     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273                 "Timeout during download for type `%s', shutting down with error\n",
274                 type);
275     ok = 1;
276     GNUNET_SCHEDULER_shutdown ();
277     return;
278   }
279   del = GNUNET_TIME_absolute_get_duration (start_time);
280   if (del.rel_value == 0)
281     del.rel_value = 1;
282   fancy =
283     GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
284                                     1000LL / del.rel_value);
285   FPRINTF (stderr, "Download speed of type `%s' was %s/s\n", type, fancy);
286   GNUNET_free (fancy);
287   if (NUM_DAEMONS != ++download_counter)
288     return;                   /* more downloads to come */
289   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290               "Finished all downloads, getting statistics\n");
291   sm = GNUNET_malloc (sizeof (struct StatMaster));
292   sm->op =
293     GNUNET_TESTBED_service_connect (NULL,
294                                     daemons[sm->daemon],
295                                     "statistics",
296                                     &stat_run, sm,
297                                     &statistics_connect_adapter,
298                                     &statistics_disconnect_adapter,
299                                     NULL);
300 }
301
302
303 static void
304 do_downloads (void *cls, const struct GNUNET_FS_Uri *u2)
305 {
306   int anonymity;
307   unsigned int i;
308
309   if (NULL == u2)
310   {
311     GNUNET_SCHEDULER_shutdown ();
312     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313                 "Timeout during upload attempt, shutting down with error\n");
314     ok = 1;
315     return;
316   }
317   uri2 = GNUNET_FS_uri_dup (u2);
318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
319               (unsigned long long) FILESIZE);
320   start_time = GNUNET_TIME_absolute_get ();
321   if (NULL != strstr (progname, "dht"))
322     anonymity = 0;
323   else
324     anonymity = 1;
325   /* (semi) leach-download(s); not true leaches since
326    * these peers do participate in sharing, they just
327    * don't have to offer anything *initially*.  */
328   for (i = 0; i < NUM_DAEMONS - 2; i++)
329     GNUNET_FS_TEST_download (daemons[i], TIMEOUT, anonymity,
330                              0 == (i % 2) ? SEED1 : SEED2,
331                              0 == (i % 2) ? uri1 : uri2, VERBOSE, &do_report,
332                              "leach");
333   /* mutual downloads of (primary) sharing peers */
334   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity, SEED1,
335                            uri1, VERBOSE, &do_report, "seeder 2");
336   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity, SEED2,
337                            uri2, VERBOSE, &do_report, "seeder 1");
338 }
339
340
341 static void
342 do_publish2 (void *cls,      
343              const struct GNUNET_FS_Uri *u1)
344 {
345   int do_index;
346   int anonymity;
347
348   if (NULL == u1)
349     {
350     GNUNET_SCHEDULER_shutdown ();
351     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352                 "Timeout during upload attempt, shutting down with error\n");
353     ok = 1;
354     return;
355   }
356   uri1 = GNUNET_FS_uri_dup (u1);
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
358               (unsigned long long) FILESIZE);
359   if (NULL != strstr (progname, "index"))
360     do_index = GNUNET_YES;
361   else
362     do_index = GNUNET_NO;
363   if (NULL != strstr (progname, "dht"))
364     anonymity = 0;
365   else
366     anonymity = 1;
367
368   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity,
369                           do_index, FILESIZE, SEED2, VERBOSE, &do_downloads,
370                           NULL);
371 }
372
373
374 static void
375 do_publish1 (void *cls, 
376              struct GNUNET_TESTBED_Operation *op,
377              const char *emsg)
378 {
379   unsigned int *coco = cls;
380   int do_index;
381   int anonymity;
382
383   GNUNET_TESTBED_operation_done (op);
384   if (NULL != emsg)
385   {
386     GNUNET_SCHEDULER_shutdown ();
387     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error trying to connect: %s\n", emsg);
388     ok = 1;
389     return;
390   }
391   if (0 != (--(*coco)))
392     return; /* more connections to be created */
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
394               (unsigned long long) FILESIZE);
395   if (NULL != strstr (progname, "index"))
396     do_index = GNUNET_YES;
397   else
398     do_index = GNUNET_NO;
399   if (NULL != strstr (progname, "dht"))
400     anonymity = 0;
401   else
402     anonymity = 1;
403   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity,
404                           do_index, FILESIZE, SEED1, VERBOSE, &do_publish2,
405                           NULL);
406 }
407
408
409 static void
410 do_connect (void *cls, 
411             unsigned int num_peers,
412             struct GNUNET_TESTBED_Peer **peers)
413 {
414   static unsigned int coco;
415   unsigned int i;
416   unsigned int j;
417  
418   GNUNET_assert (NUM_DAEMONS == num_peers);
419   for (i=0;i<num_peers;i++)
420     daemons[i] = peers[i];
421   for (i=0;i<NUM_DAEMONS;i++)
422     for (j=i+1;j<NUM_DAEMONS;j++)
423       {
424         coco++;
425         GNUNET_TESTBED_overlay_connect (NULL,
426                                         &do_publish1,
427                                         &coco,
428                                         peers[i],
429                                         peers[j]);
430       }
431 }
432
433
434 int
435 main (int argc, char *argv[])
436 {
437   progname = argv[0];
438   GNUNET_TESTBED_test_run ("perf-gnunet-service-fs-p2p-respect",
439                            "perf_gnunet_service_fs_p2p.conf",
440                            NUM_DAEMONS,
441                            0, NULL, NULL,
442                            &do_connect, NULL);
443   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
444   return ok;
445 }
446
447 /* end of perf_gnunet_service_fs_p2p_respect.c */