Some fixes
[oweals/gnunet.git] / src / transport / test_transport_ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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  * @file testing/test_transport_ats.c
22  * @brief testcase for ats functionality
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_scheduler_lib.h"
27
28 #define VERBOSE GNUNET_NO
29
30 #define NUM_PEERS 11
31 #define MEASUREMENTS 5
32
33 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36
37 static int ok;
38
39 static int peers_left;
40
41 static int failed_peers;
42
43 static int measurement_started;
44
45 static struct GNUNET_TESTING_PeerGroup *pg;
46
47 static  GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
48 static  GNUNET_SCHEDULER_TaskIdentifier stats_task;
49 struct GNUNET_TESTING_Daemon * master_deamon;
50
51 struct GNUNET_STATISTICS_Handle * stats;
52
53 struct TEST_result
54 {
55         uint64_t timestamp;
56         uint64_t duration;
57         uint64_t mechs;
58         uint64_t peers;
59         uint64_t solution;
60 };
61
62 static int r_index;
63 //static int measurements;
64 static int peers;
65 static struct TEST_result results[MEASUREMENTS];
66
67 struct GNUNET_STATISTICS_GetHandle * s_solution;
68 struct GNUNET_STATISTICS_GetHandle * s_time;
69 struct GNUNET_STATISTICS_GetHandle * s_peers;
70 struct GNUNET_STATISTICS_GetHandle * s_mechs;
71 struct GNUNET_STATISTICS_GetHandle * s_duration;
72
73 /**
74  * Check whether peers successfully shut down.
75  */
76 void
77 shutdown_callback (void *cls, const char *emsg)
78 {
79   if (emsg != NULL)
80     {
81 #if VERBOSE
82       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
83 #endif
84       if (ok == 0)
85         ok = 666;
86     }
87   else
88     {
89 #if VERBOSE
90       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91                   "All peers successfully shut down!\n");
92         if (stats != NULL)
93                 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
94         stats = NULL;
95 #endif
96     }
97 }
98
99 static void shutdown_peers()
100 {
101         if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
102         {
103                 GNUNET_SCHEDULER_cancel(shutdown_task);
104                 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
105         }
106         if (stats_task != GNUNET_SCHEDULER_NO_TASK)
107         {
108                 GNUNET_SCHEDULER_cancel(stats_task);
109                 stats_task = GNUNET_SCHEDULER_NO_TASK;
110         }
111
112         if (s_time != NULL)
113         {
114                 GNUNET_STATISTICS_get_cancel(s_time);
115                 s_time = NULL;
116         }
117         if (s_peers != NULL)
118         {
119                 GNUNET_STATISTICS_get_cancel(s_peers);
120                 s_peers = NULL;
121         }
122         if (s_mechs != NULL)
123         {
124                 GNUNET_STATISTICS_get_cancel(s_mechs);
125                 s_mechs = NULL;
126         }
127         if (s_solution != NULL)
128         {
129                 GNUNET_STATISTICS_get_cancel(s_solution);
130                 s_solution = NULL;
131         }
132         if (s_duration != NULL)
133         {
134                 GNUNET_STATISTICS_get_cancel(s_duration);
135                 s_duration = NULL;
136         }
137
138
139     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
140 }
141
142 static void evaluate_measurements()
143 {
144         int c;
145         char * output = NULL;
146         char * temp;
147         for (c=0; c<MEASUREMENTS;c++)
148         {
149                 GNUNET_asprintf(&temp, "%s ,%i,%llu,%llu,%llu,%llu,", (output==NULL) ? "" : output, c, results[c].peers, results[c].mechs, results[c].duration, results[c].solution);
150                 GNUNET_free_non_null (output);
151                 output = temp;
152         }
153         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"%s\n",output);
154
155
156         shutdown_peers();
157 }
158
159 int stats_cb (void *cls,
160                            const char *subsystem,
161                            const char *name,
162                            uint64_t value,
163                            int is_persistent)
164 {
165         if (0 == strcmp (name,"ATS solution"))
166         {
167                 s_solution = NULL;
168         }
169
170         if (0 == strcmp (name,"ATS peers"))
171         {
172                 s_peers = NULL;
173         }
174
175         if (0 == strcmp (name,"ATS mechanisms"))
176         {
177                 s_mechs = NULL;
178         }
179
180         if (0 == strcmp (name,"ATS duration"))
181         {
182                 s_duration = NULL;
183         }
184         if (0 == strcmp (name,"ATS timestamp"))
185         {
186                 s_time = NULL;
187         }
188
189     if ((measurement_started == GNUNET_NO) && (0 == strcmp (name, "ATS peers")) && (value == peers-1))
190     {
191                 measurement_started = GNUNET_YES;
192                 r_index = 0;
193                 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All %llu peers connected\n", value);
194     }
195     if (measurement_started == GNUNET_YES)
196     {
197                 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
198                 if (0 == strcmp (name,"ATS timestamp"))
199                 {
200                         if (results[r_index].timestamp == 0)
201                                 results[r_index].timestamp = value;
202                         if (results[r_index].timestamp != value)
203                         {
204                                 r_index++;
205                                 if (r_index >= MEASUREMENTS)
206                                 {
207                                         if (stats_task != GNUNET_SCHEDULER_NO_TASK)
208                                         {
209                                                 GNUNET_SCHEDULER_cancel(stats_task);
210                                                 stats_task = GNUNET_SCHEDULER_NO_TASK;
211                                         }
212                                         evaluate_measurements();
213                                         return GNUNET_NO;
214                                 }
215                                 results[r_index].timestamp = value;
216                         }
217
218                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219                                                 "[%i] ATS solution: %s %llu \n", r_index, name, value);
220                 }
221
222                 if (0 == strcmp (name,"ATS solution"))
223                 {
224                         results[r_index].solution = value;
225                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226                                                 "[%i] ATS solution: %s %llu \n", r_index, name, value);
227                 }
228
229                 if (0 == strcmp (name,"ATS peers"))
230                 {
231                         results[r_index].peers = value;
232                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233                                                 "[%i] ATS peers: %s %llu \n", r_index, name, value);
234                 }
235
236                 if (0 == strcmp (name,"ATS mechanisms"))
237                 {
238                         results[r_index].mechs = value;
239                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS mechanisms: %s %llu \n", r_index, name, value);
240                 }
241
242                 if (0 == strcmp (name,"ATS duration"))
243                 {
244                         results[r_index].duration = value;
245                         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] ATS duration: %s %llu \n", r_index, name, value);
246                 }
247     }
248     return GNUNET_OK;
249 }
250
251
252 void
253 stats_get_task (void *cls,
254                           const struct GNUNET_SCHEDULER_TaskContext *tc)
255 {
256         stats_task = GNUNET_SCHEDULER_NO_TASK;
257         if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
258             return;
259
260         s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp", TIMEOUT, NULL, &stats_cb, NULL);
261         s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution", TIMEOUT, NULL, &stats_cb, NULL);
262         s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration", TIMEOUT, NULL, &stats_cb, NULL);
263         s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers", TIMEOUT, NULL, &stats_cb, NULL);
264         s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms", TIMEOUT, NULL, &stats_cb, NULL);
265
266         stats_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250), &stats_get_task, NULL);
267 }
268
269 void
270 delay (void *cls,
271                           const struct GNUNET_SCHEDULER_TaskContext *tc)
272 {
273         shutdown_task = GNUNET_SCHEDULER_NO_TASK;
274         if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
275             return;
276
277 #if VERBOSE
278         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Delay over\n");
279 #endif
280         shutdown_peers ();
281 }
282
283 static void connect_peers()
284 {
285     shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
286
287 }
288
289 void daemon_connect_cb(void *cls,
290                                                 const struct GNUNET_PeerIdentity *first,
291                                                 const struct GNUNET_PeerIdentity *second,
292                                                 uint32_t distance,
293                                                 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
294                                                 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
295                                                 struct GNUNET_TESTING_Daemon *first_daemon,
296                                                 struct GNUNET_TESTING_Daemon *second_daemon,
297                                                 const char *emsg)
298 {
299           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peers `%s'<->`%s': `%s' \n", GNUNET_i2s(first), GNUNET_i2s(second), (emsg==NULL) ? "OK" : emsg);
300 }
301
302 void cont_cb (void *cls, int success)
303 {
304     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
305                 "STATS cont_cb: %i\n", success);
306 }
307
308 static void
309 daemon_start_cb (void *cls,
310        const struct GNUNET_PeerIdentity *id,
311        const struct GNUNET_CONFIGURATION_Handle *cfg,
312        struct GNUNET_TESTING_Daemon *d, const char *emsg)
313 {
314   if (id == NULL)
315     {
316       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317                   "Start callback called with error (too long starting peers), aborting test!\n");
318       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
319       failed_peers++;
320       if (failed_peers == peers_left)
321         {
322           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323                       "Too many peers failed, ending test!\n");
324           ok = 1;
325         shutdown_peers ();
326         }
327       return;
328     }
329   peers_left--;
330
331   if (master_deamon == NULL)
332   {
333           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Master peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
334
335           master_deamon = d;
336           stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
337           GNUNET_assert (stats != NULL);
338           stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
339   }
340   else
341   {
342           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peer `%s'\n", GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
343           GNUNET_TESTING_daemons_connect(d, master_deamon, TIMEOUT, 0, GNUNET_YES,&daemon_connect_cb, NULL);
344   }
345
346   if (peers_left == 0)
347     {
348       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349                   "All peers started successfully!\n");
350       connect_peers();
351       ok = 0;
352     }
353   else if (failed_peers == peers_left)
354     {
355       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356                   "Too many peers failed, ending test!\n");
357       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
358       ok = 1;
359     }
360 }
361
362
363 static void
364 run (void *cls,
365      char *const *args,
366      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
367 {
368   ok = 1;
369   measurement_started = GNUNET_NO;
370 #if VERBOSE
371   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", NUM_PEERS);
372 #endif
373   peers_left = peers;
374   pg = GNUNET_TESTING_daemons_start (cfg,
375                                      peers_left, /* Total number of peers */
376                                      peers_left, /* Number of outstanding connections */
377                                      peers_left, /* Number of parallel ssh connections, or peers being started at once */
378                                      TIMEOUT,
379                                      NULL, NULL,
380                                      &daemon_start_cb, NULL, NULL, NULL, NULL);
381   GNUNET_assert (pg != NULL);
382 }
383
384 static int
385 check ()
386 {
387   char *const argv[] = { "test-testing",
388     "-c",
389     "test_transport_ats.conf",
390 #if VERBOSE
391     "-L", "DEBUG",
392 #endif
393     NULL
394   };
395   struct GNUNET_GETOPT_CommandLineOption options[] = {
396     GNUNET_GETOPT_OPTION_END
397   };
398   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
399                       argv, "test-testing-group", "nohelp",
400                       options, &run, &ok);
401   return ok;
402 }
403
404 int
405 main (int argc, char *argv[])
406 {
407   int ret;
408
409   GNUNET_log_setup ("test-transport-ats",
410 #if VERBOSE
411                     "DEBUG",
412 #else
413                     "WARNING",
414 #endif
415                     NULL);
416
417   peers = NUM_PEERS;
418   if (argc >= 2)
419   {
420           peers = atoi(argv[1]);
421           peers++;
422           if(peers <1)
423                   peers = NUM_PEERS;
424   }
425   ret = check ();
426   /**
427    * Still need to remove the base testing directory here,
428    * because group starts will create subdirectories under this
429    * main dir.  However, we no longer need to sleep, as the
430    * shutdown sequence won't return until everything is cleaned
431    * up.
432    */
433   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
434   return ret;
435 }
436
437 /* end of test_transport_ats.c*/