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