uncrustify as demanded.
[oweals/gnunet.git] / src / fs / perf_gnunet_service_fs_p2p.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010, 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 static struct GNUNET_TIME_Absolute start_time;
55
56
57 /**
58  * Master context for 'stat_run'.
59  */
60 struct StatMaster {
61   struct GNUNET_STATISTICS_Handle *stat;
62   struct GNUNET_TESTBED_Operation *op;
63   unsigned int daemon;
64   unsigned int value;
65 };
66
67 struct StatValues {
68   const char *subsystem;
69   const char *name;
70 };
71
72 /**
73  * Statistics we print out.
74  */
75 static struct StatValues stats[] = {
76   { "fs", "# queries forwarded" },
77   { "fs", "# replies received and matched" },
78   { "fs", "# results found locally" },
79   { "fs", "# requests forwarded due to high load" },
80   { "fs", "# requests done for free (low load)" },
81   { "fs", "# requests dropped, priority insufficient" },
82   { "fs", "# requests done for a price (normal load)" },
83   { "fs", "# requests dropped by datastore (queue length limit)" },
84   { "fs", "# P2P searches received" },
85   { "fs", "# P2P searches discarded (queue length bound)" },
86   { "fs", "# replies received for local clients" },
87   { "fs", "# queries retransmitted to same target" },
88   { "core", "# bytes decrypted" },
89   { "core", "# bytes encrypted" },
90   { "core", "# discarded CORE_SEND requests" },
91   { "core", "# discarded CORE_SEND request bytes" },
92   { "core", "# discarded lower priority CORE_SEND requests" },
93   { "core", "# discarded lower priority CORE_SEND request bytes" },
94   { "transport", "# bytes received via TCP" },
95   { "transport", "# bytes transmitted via TCP" },
96   { "datacache", "# bytes stored" },
97   { NULL, NULL }
98 };
99
100
101 /**
102  * Callback function to process statistic values.
103  *
104  * @param cls closure
105  * @param subsystem name of subsystem that created the statistic
106  * @param name the name of the datum
107  * @param value the current value
108  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
109  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
110  */
111 static int
112 print_stat(void *cls, const char *subsystem, const char *name, uint64_t value,
113            int is_persistent)
114 {
115   struct StatMaster *sm = cls;
116
117   fprintf(stderr,
118           "Peer %2u: %12s/%50s = %12llu\n",
119           sm->daemon,
120           subsystem,
121           name,
122           (unsigned long long)value);
123   return GNUNET_OK;
124 }
125
126
127 /**
128  * Function that gathers stats from all daemons.
129  */
130 static void
131 stat_run(void *cls,
132          struct GNUNET_TESTBED_Operation *op,
133          void *ca_result,
134          const char *emsg);
135
136
137 /**
138  * Function called when GET operation on stats is done.
139  */
140 static void
141 get_done(void *cls, int success)
142 {
143   struct StatMaster *sm = cls;
144
145   GNUNET_break(GNUNET_OK == success);
146   sm->value++;
147   stat_run(sm, sm->op, sm->stat, NULL);
148 }
149
150
151 /**
152  * Adapter function called to establish a connection to
153  * statistics service.
154  *
155  * @param cls closure
156  * @param cfg configuration of the peer to connect to; will be available until
157  *          GNUNET_TESTBED_operation_done() is called on the operation returned
158  *          from GNUNET_TESTBED_service_connect()
159  * @return service handle to return in 'op_result', NULL on error
160  */
161 static void *
162 statistics_connect_adapter(void *cls,
163                            const struct GNUNET_CONFIGURATION_Handle *cfg)
164 {
165   return GNUNET_STATISTICS_create("<driver>",
166                                   cfg);
167 }
168
169
170 /**
171  * Adapter function called to destroy a connection to
172  * statistics service.
173  *
174  * @param cls closure
175  * @param op_result service handle returned from the connect adapter
176  */
177 static void
178 statistics_disconnect_adapter(void *cls,
179                               void *op_result)
180 {
181   GNUNET_STATISTICS_destroy(op_result, GNUNET_NO);
182 }
183
184
185 /**
186  * Function that gathers stats from all daemons.
187  */
188 static void
189 stat_run(void *cls,
190          struct GNUNET_TESTBED_Operation *op,
191          void *ca_result,
192          const char *emsg)
193 {
194   struct StatMaster *sm = cls;
195
196   if (NULL != emsg)
197     {
198       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
199                  "Failed to statistics service: %s\n",
200                  emsg);
201       GNUNET_SCHEDULER_shutdown();
202       return;
203     }
204   sm->stat = ca_result;
205
206   if (stats[sm->value].name != NULL)
207     {
208       GNUNET_STATISTICS_get(sm->stat,
209 #if 0
210                             NULL, NULL,
211 #else
212                             stats[sm->value].subsystem, stats[sm->value].name,
213 #endif
214                             &get_done, &print_stat,
215                             sm);
216       return;
217     }
218   GNUNET_TESTBED_operation_done(sm->op);
219   sm->value = 0;
220   sm->daemon++;
221   if (NUM_DAEMONS == sm->daemon)
222     {
223       GNUNET_free(sm);
224       GNUNET_SCHEDULER_shutdown();
225       return;
226     }
227   sm->op =
228     GNUNET_TESTBED_service_connect(NULL,
229                                    daemons[sm->daemon],
230                                    "statistics",
231                                    &stat_run, sm,
232                                    &statistics_connect_adapter,
233                                    &statistics_disconnect_adapter,
234                                    NULL);
235 }
236
237
238 static void
239 do_report(void *cls)
240 {
241   char *fn = cls;
242   struct GNUNET_TIME_Relative del;
243   char *fancy;
244   struct StatMaster *sm;
245
246   if (NULL != fn)
247     {
248       GNUNET_DISK_directory_remove(fn);
249       GNUNET_free(fn);
250     }
251   if (0 ==
252       GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_add(start_time,
253                                                                   TIMEOUT)).rel_value_us)
254     {
255       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
256                  "Timeout during download, shutting down with error\n");
257       ok = 1;
258       GNUNET_SCHEDULER_shutdown();
259       return;
260     }
261
262   del = GNUNET_TIME_absolute_get_duration(start_time);
263   if (del.rel_value_us == 0)
264     del.rel_value_us = 1;
265   fancy =
266     GNUNET_STRINGS_byte_size_fancy(((unsigned long long)FILESIZE) *
267                                    1000000LL / del.rel_value_us);
268   fprintf(stdout,
269           "Download speed was %s/s\n",
270           fancy);
271   GNUNET_free(fancy);
272   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
273              "Finished download, shutting down\n");
274   sm = GNUNET_new(struct StatMaster);
275   sm->op =
276     GNUNET_TESTBED_service_connect(NULL,
277                                    daemons[sm->daemon],
278                                    "statistics",
279                                    &stat_run, sm,
280                                    &statistics_connect_adapter,
281                                    &statistics_disconnect_adapter,
282                                    NULL);
283 }
284
285
286 static void
287 do_download(void *cls,
288             const struct GNUNET_FS_Uri *uri,
289             const char *fn)
290 {
291   int anonymity;
292
293   if (NULL == uri)
294     {
295       GNUNET_SCHEDULER_shutdown();
296       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
297                  "Timeout during upload attempt, shutting down with error\n");
298       ok = 1;
299       return;
300     }
301   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
302              (unsigned long long)FILESIZE);
303   start_time = GNUNET_TIME_absolute_get();
304   if (NULL != strstr(progname, "dht"))
305     anonymity = 0;
306   else
307     anonymity = 1;
308   start_time = GNUNET_TIME_absolute_get();
309   GNUNET_FS_TEST_download(daemons[0],
310                           TIMEOUT,
311                           anonymity,
312                           SEED,
313                           uri,
314                           VERBOSE,
315                           &do_report,
316                           (NULL == fn) ? NULL : GNUNET_strdup(fn));
317 }
318
319
320 static void
321 do_publish(void *cls,
322            struct GNUNET_TESTBED_RunHandle *h,
323            unsigned int num_peers,
324            struct GNUNET_TESTBED_Peer **peers,
325            unsigned int links_succeeded,
326            unsigned int links_failed)
327 {
328   unsigned int i;
329   int do_index;
330   int anonymity;
331
332   GNUNET_assert(NUM_DAEMONS == num_peers);
333   for (i = 0; i < num_peers; i++)
334     daemons[i] = peers[i];
335   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
336              (unsigned long long)FILESIZE);
337   if (NULL != strstr(progname, "index"))
338     do_index = GNUNET_YES;
339   else
340     do_index = GNUNET_NO;
341   if (NULL != strstr(progname, "dht"))
342     anonymity = 0;
343   else
344     anonymity = 1;
345   GNUNET_FS_TEST_publish(daemons[NUM_DAEMONS - 1], TIMEOUT, anonymity,
346                          do_index, FILESIZE, SEED, VERBOSE, &do_download,
347                          NULL);
348 }
349
350
351 int
352 main(int argc, char *argv[])
353 {
354   progname = argv[0];
355   (void)GNUNET_TESTBED_test_run("perf-gnunet-service-fs-p2p",
356                                 "perf_gnunet_service_fs_p2p.conf",
357                                 NUM_DAEMONS,
358                                 0, NULL, NULL,
359                                 &do_publish, NULL);
360   GNUNET_DISK_directory_remove("/tmp/gnunet-test-fs-lib/");
361   return ok;
362 }
363
364 /* end of perf_gnunet_service_fs_p2p.c */