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