-fix dht flags
[oweals/gnunet.git] / src / fs / perf_gnunet_service_fs_p2p.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 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.c
23  * @brief profile P2P routing using simple publish + download operation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "fs_test_lib.h"
28 #include "gnunet_testbed_service.h"
29
30 #define VERBOSE GNUNET_NO
31
32 /**
33  * File-size we use for testing.
34  */
35 #define FILESIZE (1024 * 1024 * 10)
36
37 /**
38  * How long until we give up on transmitting the message?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
41
42 #define NUM_DAEMONS 2
43
44 #define SEED 42
45
46 static struct GNUNET_TESTBED_Peer *daemons[NUM_DAEMONS];
47
48 static int ok;
49
50 static struct GNUNET_TIME_Absolute start_time;
51
52 static const char *progname;
53
54
55 /**
56  * Master context for 'stat_run'.
57  */
58 struct StatMaster
59 {
60   struct GNUNET_STATISTICS_Handle *stat;
61   struct GNUNET_TESTBED_Operation *op;
62   unsigned int daemon;
63   unsigned int value;
64 };
65
66 struct StatValues
67 {
68   const char *subsystem;
69   const char *name;
70 };
71
72 /**
73  * Statistics we print out.
74  */
75 static struct StatValues stats[] = {
76   {"fs", "# artificial delays introduced (ms)"},
77   {"fs", "# queries forwarded"},
78   {"fs", "# replies received and matched"},
79   {"fs", "# results found locally"},
80   {"fs", "# requests forwarded due to high load"},
81   {"fs", "# requests done for free (low load)"},
82   {"fs", "# requests dropped, priority insufficient"},
83   {"fs", "# requests done for a price (normal load)"},
84   {"fs", "# requests dropped by datastore (queue length limit)"},
85   {"fs", "# P2P searches received"},
86   {"fs", "# P2P searches discarded (queue length bound)"},
87   {"fs", "# replies received for local clients"},
88   {"fs", "# queries retransmitted to same target"},
89   {"core", "# bytes decrypted"},
90   {"core", "# bytes encrypted"},
91   {"core", "# discarded CORE_SEND requests"},
92   {"core", "# discarded CORE_SEND request bytes"},
93   {"core", "# discarded lower priority CORE_SEND requests"},
94   {"core", "# discarded lower priority CORE_SEND request bytes"},
95   {"transport", "# bytes received via TCP"},
96   {"transport", "# bytes transmitted via TCP"},
97   {"datacache", "# bytes stored"},
98   {NULL, NULL}
99 };
100
101
102 /**
103  * Callback function to process statistic values.
104  *
105  * @param cls closure
106  * @param subsystem name of subsystem that created the statistic
107  * @param name the name of the datum
108  * @param value the current value
109  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
110  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
111  */
112 static int
113 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
114             int is_persistent)
115 {
116   struct StatMaster *sm = cls;
117
118   FPRINTF (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
119            name, (unsigned long long) value);
120   return GNUNET_OK;
121 }
122
123
124 /**
125  * Function that gathers stats from all daemons.
126  */
127 static void
128 stat_run (void *cls, 
129           struct GNUNET_TESTBED_Operation *op,
130           void *ca_result,
131           const char *emsg);
132
133
134 /**
135  * Function called when GET operation on stats is done.
136  */
137 static void
138 get_done (void *cls, int success)
139 {
140   struct StatMaster *sm = cls;
141
142   GNUNET_break (GNUNET_OK == success);
143   sm->value++;
144   stat_run (sm, sm->op, sm->stat, NULL);
145 }
146
147
148 /**
149  * Adapter function called to establish a connection to
150  * statistics service.
151  * 
152  * @param cls closure
153  * @param cfg configuration of the peer to connect to; will be available until
154  *          GNUNET_TESTBED_operation_done() is called on the operation returned
155  *          from GNUNET_TESTBED_service_connect()
156  * @return service handle to return in 'op_result', NULL on error
157  */
158 static void *
159 statistics_connect_adapter (void *cls,
160                             const struct GNUNET_CONFIGURATION_Handle *cfg)
161 {
162   return GNUNET_STATISTICS_create ("<driver>",
163                                    cfg);
164 }
165
166
167 /**
168  * Adapter function called to destroy a connection to
169  * statistics service.
170  * 
171  * @param cls closure
172  * @param op_result service handle returned from the connect adapter
173  */
174 static void 
175 statistics_disconnect_adapter (void *cls,
176                                void *op_result)
177 {
178   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
179 }
180
181
182 /**
183  * Function that gathers stats from all daemons.
184  */
185 static void
186 stat_run (void *cls, 
187           struct GNUNET_TESTBED_Operation *op,
188           void *ca_result,
189           const char *emsg)
190 {
191   struct StatMaster *sm = cls;
192
193   if (NULL != emsg)
194     {
195       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196                   "Failed to statistics service: %s\n",
197                   emsg);
198       GNUNET_SCHEDULER_shutdown ();
199       return;
200     }
201   sm->stat = ca_result;
202
203   if (stats[sm->value].name != NULL)
204   {
205     GNUNET_STATISTICS_get (sm->stat,
206 #if 0
207                            NULL, NULL,
208 #else
209                            stats[sm->value].subsystem, stats[sm->value].name,
210 #endif
211                            GNUNET_TIME_UNIT_FOREVER_REL, &get_done, &print_stat,
212                            sm);
213     return;
214   }
215   GNUNET_TESTBED_operation_done (sm->op);
216   sm->value = 0;
217   sm->daemon++;
218   if (NUM_DAEMONS == sm->daemon)
219   {
220     GNUNET_free (sm);
221     GNUNET_SCHEDULER_shutdown ();
222     return;
223   }
224   sm->op =
225     GNUNET_TESTBED_service_connect (NULL,
226                                     daemons[sm->daemon],
227                                     "statistics",
228                                     &stat_run, sm,
229                                     &statistics_connect_adapter,
230                                     &statistics_disconnect_adapter,
231                                     NULL);
232 }
233
234
235 static void
236 do_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   struct GNUNET_TIME_Relative del;
239   char *fancy;
240   struct StatMaster *sm;
241
242   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) 
243   {
244     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245                 "Timeout during download, shutting down with error\n");
246     ok = 1;
247     GNUNET_SCHEDULER_shutdown ();
248     return;
249   }
250   del = GNUNET_TIME_absolute_get_duration (start_time);
251   if (del.rel_value == 0)
252     del.rel_value = 1;
253   fancy =
254     GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
255                                     1000LL / del.rel_value);
256   FPRINTF (stdout, "Download speed was %s/s\n", fancy);
257   GNUNET_free (fancy);
258   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
259               (unsigned long long) FILESIZE);
260   sm = GNUNET_malloc (sizeof (struct StatMaster));
261   sm->op =
262     GNUNET_TESTBED_service_connect (NULL,
263                                     daemons[sm->daemon],
264                                     "statistics",
265                                     &stat_run, sm,
266                                     &statistics_connect_adapter,
267                                     &statistics_disconnect_adapter,
268                                     NULL);
269 }
270
271
272 static void
273 do_download (void *cls, const struct GNUNET_FS_Uri *uri)
274 {
275   int anonymity;
276
277   if (NULL == uri)
278     {
279     GNUNET_SCHEDULER_shutdown ();
280     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281                 "Timeout during upload attempt, shutting down with error\n");
282     ok = 1;
283     return;
284   }
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
286               (unsigned long long) FILESIZE);
287   start_time = GNUNET_TIME_absolute_get ();
288   if (NULL != strstr (progname, "dht"))
289     anonymity = 0;
290   else
291     anonymity = 1;
292   GNUNET_FS_TEST_download (daemons[0], TIMEOUT, anonymity, SEED, uri, VERBOSE,
293                            &do_report, NULL);
294 }
295
296
297 static void
298 do_publish (void *cls,
299             struct GNUNET_TESTBED_Operation *op,
300             const char *emsg)
301 {
302   int do_index;
303   int anonymity;
304
305   if (NULL != emsg)
306   {
307     GNUNET_SCHEDULER_shutdown ();
308     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error trying to connect: %s\n", emsg);
309     ok = 1;
310     return;
311   }
312   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
313               (unsigned long long) FILESIZE);
314   if (NULL != strstr (progname, "index"))
315     do_index = GNUNET_YES;
316   else
317     do_index = GNUNET_NO;
318   if (NULL != strstr (progname, "dht"))
319     anonymity = 0;
320   else
321     anonymity = 1;
322
323   GNUNET_FS_TEST_publish (daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity,
324                           do_index, FILESIZE, SEED, VERBOSE, &do_download,
325                           NULL);
326 }
327
328
329 static void
330 do_connect (void *cls, 
331             unsigned int num_peers,
332             struct GNUNET_TESTBED_Peer **peers)
333 {
334   unsigned int i;
335  
336   GNUNET_assert (NUM_DAEMONS == num_peers);
337   for (i=0;i<num_peers;i++)
338     daemons[i] = peers[i];
339   GNUNET_TESTBED_overlay_connect (NULL,
340                                   &do_publish,
341                                   NULL,
342                                   peers[0],
343                                   peers[1]);
344   // FIXME: was supposed to connect in line...
345 }
346
347
348 int
349 main (int argc, char *argv[])
350 {
351   GNUNET_TESTBED_test_run ("perf-gnunet-service-fs-p2p",
352                            "perf_gnunet_service_fs_p2p.conf",
353                            NUM_DAEMONS,
354                            0, NULL, NULL,
355                            &do_connect, NULL);
356   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
357   return ok;
358 }
359
360 /* end of perf_gnunet_service_fs_p2p.c */