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