08ab74ac6ee24cc1d7742dc82607668aacf823a1
[oweals/gnunet.git] / src / fs / gnunet-fs-profiler.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 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 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
16 /**
17  * @file fs/gnunet-fs-profiler.c
18  * @brief tool to benchmark/profile file-sharing
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testbed_service.h"
24
25 /**
26  * Final status code.
27  */
28 static int ret;
29
30 /**
31  * Data file with the hosts for the testbed.
32  */
33 static char *host_filename;
34
35 /**
36  * Number of peers to run in the experiment.
37  */
38 static unsigned int num_peers;
39
40 /**
41  * After how long do we abort the test?
42  */
43 static struct GNUNET_TIME_Relative timeout;
44
45 /**
46  * Handle to the task run during termination.
47  */
48 static struct GNUNET_SCHEDULER_Task * terminate_taskid;
49
50
51 /**
52  * Function called after we've collected the statistics.
53  *
54  * @param cls NULL
55  * @param op the operation that has been finished
56  * @param emsg error message in case the operation has failed; will be NULL if
57  *          operation has executed successfully.
58  */
59 static void
60 shutdown_task (void *cls,
61                struct GNUNET_TESTBED_Operation *op,
62                const char *emsg)
63 {
64   if (NULL != emsg)
65     fprintf (stderr,
66              "Error collecting statistics: %s\n",
67              emsg);
68   GNUNET_SCHEDULER_shutdown ();
69 }
70
71
72 /**
73  * Callback function to process statistic values from all peers.
74  * Prints them out.
75  *
76  * @param cls closure
77  * @param peer the peer the statistic belong to
78  * @param subsystem name of subsystem that created the statistic
79  * @param name the name of the datum
80  * @param value the current value
81  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
82  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
83  */
84 static int
85 process_stats (void *cls,
86                const struct GNUNET_TESTBED_Peer *peer,
87                const char *subsystem,
88                const char *name,
89                uint64_t value,
90                int is_persistent)
91 {
92   fprintf (stdout,
93            "%p-%s: %s = %llu\n",
94            peer,
95            subsystem,
96            name,
97            (unsigned long long) value);
98   return GNUNET_OK;
99 }
100
101
102 /**
103  * Task run on shutdown to terminate.  Triggers printing out
104  * all statistics.
105  *
106  * @param cls NULL
107  */
108 static void
109 terminate_task (void *cls)
110 {
111   if (NULL != terminate_taskid)
112   {
113     GNUNET_SCHEDULER_cancel (terminate_taskid);
114     terminate_taskid = NULL;
115   }
116   GNUNET_TESTBED_get_statistics (0, NULL,
117                                  NULL, NULL,
118                                  &process_stats,
119                                  &shutdown_task,
120                                  NULL);
121 }
122
123
124 /**
125  * Task run on timeout to terminate.  Triggers printing out
126  * all statistics.
127  *
128  * @param cls NULL
129  */
130 static void
131 timeout_task (void *cls)
132 {
133   terminate_taskid = NULL;
134   GNUNET_SCHEDULER_shutdown ();
135 }
136
137
138 /**
139  * Signature of a main function for a testcase.
140  *
141  * @param cls closure
142  * @param h the run handle
143  * @param num_peers number of peers in 'peers'
144  * @param peers handle to peers run in the testbed
145  * @param links_succeeded the number of overlay link connection attempts that
146  *          succeeded
147  * @param links_failed the number of overlay link connection attempts that
148  *          failed
149  */
150 static void
151 test_master (void *cls,
152              struct GNUNET_TESTBED_RunHandle *h,
153              unsigned int num_peers,
154              struct GNUNET_TESTBED_Peer **peers,
155              unsigned int links_succeeded,
156              unsigned int links_failed)
157 {
158   // const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
159   // FIXME: enable clients to signal 'completion' before timeout;
160   // in that case, run the 'terminate_task' "immediately"
161
162   if (0 != timeout.rel_value_us)
163     terminate_taskid = GNUNET_SCHEDULER_add_delayed (timeout,
164                                                      &timeout_task,
165                                                      NULL);
166    GNUNET_SCHEDULER_add_shutdown (&terminate_task,
167                                   NULL);
168 }
169
170
171 /**
172  * Main function that will be run by the scheduler.
173  *
174  * @param cls closure
175  * @param args remaining command-line arguments
176  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
177  * @param cfg configuration
178  */
179 static void
180 run (void *cls, char *const *args, const char *cfgfile,
181      const struct GNUNET_CONFIGURATION_Handle *cfg)
182 {
183   GNUNET_TESTBED_run (host_filename,
184                       cfg,
185                       num_peers,
186                       0, NULL, NULL,
187                       &test_master, (void *) cfg);
188 }
189
190
191 /**
192  * Program to run a file-sharing testbed.
193  *
194  * @param argc number of arguments from the command line
195  * @param argv command line arguments
196  * @return 0 ok, 1 on error
197  */
198 int
199 main (int argc, char *const *argv)
200 {
201   struct GNUNET_GETOPT_CommandLineOption options[] = {
202
203     GNUNET_GETOPT_option_uint ('n',
204                                    "num-peers",
205                                    "COUNT",
206                                    gettext_noop ("run the experiment with COUNT peers"),
207                                    &num_peers),
208
209     GNUNET_GETOPT_option_string ('H',
210                                  "hosts",
211                                  "HOSTFILE",
212                                  gettext_noop ("specifies name of a file with the HOSTS the testbed should use"),
213                                  &host_filename),
214
215     GNUNET_GETOPT_option_relative_time ('t',
216                                             "timeout",
217                                             "DELAY",
218                                             gettext_noop ("automatically terminate experiment after DELAY"),
219                                             &timeout),
220
221     GNUNET_GETOPT_OPTION_END
222   };
223   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
224     return 2;
225
226   ret = (GNUNET_OK ==
227          GNUNET_PROGRAM_run (argc, argv, "gnunet-fs-profiler",
228                              gettext_noop ("run a testbed to measure file-sharing performance"), options, &run,
229                              NULL)) ? ret : 1;
230   GNUNET_free ((void*) argv);
231   return ret;
232 }
233
234 /* end of gnunet-fs-profiler.c */