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