3030749e299d8635f927d60ff8f30f088243484e
[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   if (stats[sm->value].name != NULL)
228   {
229     GNUNET_STATISTICS_get (sm->stat,
230 #if 0
231                            NULL, NULL,
232 #else
233                            stats[sm->value].subsystem, stats[sm->value].name,
234 #endif
235                            GNUNET_TIME_UNIT_FOREVER_REL, &get_done, &print_stat,
236                            sm);
237     return;
238   }
239   GNUNET_TESTBED_operation_done (sm->op);
240   sm->value = 0;
241   sm->daemon++;
242   if (NUM_DAEMONS == sm->daemon)
243   {
244     GNUNET_free (sm);
245     GNUNET_SCHEDULER_shutdown ();
246     return;
247   }
248   sm->op =
249     GNUNET_TESTBED_service_connect (NULL,
250                                     daemons[sm->daemon],
251                                     "statistics",
252                                     &stat_run, sm,
253                                     &statistics_connect_adapter,
254                                     &statistics_disconnect_adapter,
255                                     NULL);
256 }
257
258
259 static void
260 do_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 {
262   static int download_counter;
263   const char *type = cls;
264   struct GNUNET_TIME_Relative del;
265   char *fancy;
266   struct StatMaster *sm;
267
268   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271                 "Timeout during download for type `%s', shutting down with error\n",
272                 type);
273     ok = 1;
274     GNUNET_SCHEDULER_shutdown ();
275     return;
276   }
277   del = GNUNET_TIME_absolute_get_duration (start_time);
278   if (del.rel_value == 0)
279     del.rel_value = 1;
280   fancy =
281     GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
282                                     1000LL / del.rel_value);
283   FPRINTF (stderr, "Download speed of type `%s' was %s/s\n", type, fancy);
284   GNUNET_free (fancy);
285   if (NUM_DAEMONS != ++download_counter)
286     return;                   /* more downloads to come */
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "Finished all downloads, shutting down\n",
289               (unsigned long long) FILESIZE);
290   sm = GNUNET_malloc (sizeof (struct StatMaster));
291   sm->op =
292     GNUNET_TESTBED_service_connect (NULL,
293                                     daemons[sm->daemon],
294                                     "statistics",
295                                     &stat_run, sm,
296                                     &statistics_connect_adapter,
297                                     &statistics_disconnect_adapter,
298                                     NULL);
299 }
300
301
302 static void
303 do_downloads (void *cls, const struct GNUNET_FS_Uri *u2)
304 {
305   int anonymity;
306   unsigned int i;
307
308   if (NULL == u2)
309   {
310     GNUNET_SCHEDULER_shutdown ();
311     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312                 "Timeout during upload attempt, shutting down with error\n");
313     ok = 1;
314     return;
315   }
316   uri2 = GNUNET_FS_uri_dup (u2);
317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
318               (unsigned long long) FILESIZE);
319   start_time = GNUNET_TIME_absolute_get ();
320   if (NULL != strstr (progname, "dht"))
321     anonymity = 0;
322   else
323     anonymity = 1;
324   /* (semi) leach-download(s); not true leaches since
325    * these peers do participate in sharing, they just
326    * don't have to offer anything *initially*.  */
327   for (i = 0; i < NUM_DAEMONS - 2; i++)
328     GNUNET_FS_TEST_download (daemons[i], TIMEOUT, anonymity,
329                              0 == (i % 2) ? SEED1 : SEED2,
330                              0 == (i % 2) ? uri1 : uri2, VERBOSE, &do_report,
331                              "leach");
332   /* mutual downloads of (primary) sharing peers */
333   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity, SEED1,
334                            uri1, VERBOSE, &do_report, "seeder 2");
335   GNUNET_FS_TEST_download (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity, SEED2,
336                            uri2, VERBOSE, &do_report, "seeder 1");
337 }
338
339
340 static void
341 do_publish2 (void *cls,      
342              const struct GNUNET_FS_Uri *u1)
343 {
344   int do_index;
345   int anonymity;
346
347   if (NULL == u1)
348     {
349     GNUNET_SCHEDULER_shutdown ();
350     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351                 "Timeout during upload attempt, shutting down with error\n");
352     ok = 1;
353     return;
354   }
355   uri1 = GNUNET_FS_uri_dup (u1);
356   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
357               (unsigned long long) FILESIZE);
358   if (NULL != strstr (progname, "index"))
359     do_index = GNUNET_YES;
360   else
361     do_index = GNUNET_NO;
362   if (NULL != strstr (progname, "dht"))
363     anonymity = 0;
364   else
365     anonymity = 1;
366
367   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 2], TIMEOUT, anonymity,
368                           do_index, FILESIZE, SEED2, VERBOSE, &do_downloads,
369                           NULL);
370 }
371
372 static void
373 do_publish1 (void *cls, 
374              struct GNUNET_TESTBED_Operation *op,
375              const char *emsg)
376 {
377   unsigned int *coco = cls;
378   int do_index;
379   int anonymity;
380
381   if (NULL != emsg)
382   {
383     GNUNET_SCHEDULER_shutdown ();
384     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error trying to connect: %s\n", emsg);
385     ok = 1;
386     return;
387   }
388   if (0 != (--(*coco)))
389     return; /* more connections to be created */
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
391               (unsigned long long) FILESIZE);
392   if (NULL != strstr (progname, "index"))
393     do_index = GNUNET_YES;
394   else
395     do_index = GNUNET_NO;
396   if (NULL != strstr (progname, "dht"))
397     anonymity = 0;
398   else
399     anonymity = 1;
400   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity,
401                           do_index, FILESIZE, SEED1, VERBOSE, &do_publish2,
402                           NULL);
403 }
404
405
406 static void
407 do_connect (void *cls, 
408             unsigned int num_peers,
409             struct GNUNET_TESTBED_Peer **peers)
410 {
411   static unsigned int coco;
412   unsigned int i;
413   unsigned int j;
414  
415   GNUNET_assert (NUM_DAEMONS == num_peers);
416   for (i=0;i<num_peers;i++)
417     daemons[i] = peers[i];
418   for (i=0;i<NUM_DAEMONS;i++)
419     for (j=i+1;j<NUM_DAEMONS;j++)
420       {
421         coco++;
422         GNUNET_TESTBED_overlay_connect (NULL,
423                                         &do_publish1,
424                                         &coco,
425                                         peers[i],
426                                         peers[j]);
427       }
428 }
429
430
431 int
432 main (int argc, char *argv[])
433 {
434   GNUNET_TESTBED_test_run ("perf-gnunet-service-fs-p2p-respect",
435                            "perf_gnunet_service_fs_p2p.conf",
436                            NUM_DAEMONS,
437                            0, NULL, NULL,
438                            &do_connect, NULL);
439   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
440   return ok;
441 }
442
443 /* end of perf_gnunet_service_fs_p2p_respect.c */