fixing tests ftbfs
[oweals/gnunet.git] / src / ats-tests / perf_ats.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 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 ats/perf_ats.c
22  * @brief ats benchmark: start peers and modify preferences, monitor change over time
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31 #include "ats-testing.h"
32
33
34 #define TEST_ATS_PREFRENCE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
35 #define TEST_ATS_PREFRENCE_START 1.0
36 #define TEST_ATS_PREFRENCE_DELTA 1.0
37
38 #define TEST_MESSAGE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
39
40 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
41 #define BENCHMARK_DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
42 #define LOGGING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
43 #define TESTNAME_PREFIX "perf_ats_"
44 #define DEFAULT_SLAVES_NUM 2
45 #define DEFAULT_MASTERS_NUM 1
46 /**
47  * Shutdown task
48  */
49 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
50
51 /**
52  * Progress task
53  */
54 static GNUNET_SCHEDULER_TaskIdentifier progress_task;
55
56 /**
57  * Test result
58  */
59 static int result;
60
61 /**
62  * Test result logging
63  */
64 static int logging;
65
66 /**Test core (GNUNET_YES) or transport (GNUNET_NO)
67  */
68 static int test_core;
69
70 /**
71  * Solver string
72  */
73 static char *solver;
74
75 /**
76  * Preference string
77  */
78 static char *testname;
79
80 /**
81  * Preference string
82  */
83 static char *pref_str;
84
85 /**
86  * ATS preference value
87  */
88 static int pref_val;
89
90 /**
91  * Benchmark duration
92  */
93 static struct GNUNET_TIME_Relative perf_duration;
94
95 /**
96  * Logging frequency
97  */
98 static struct GNUNET_TIME_Relative log_frequency;
99
100 /**
101  * Number master peers
102  */
103 static unsigned int num_masters;
104
105 /**
106  * Array of master peers
107  */
108 static struct BenchmarkPeer *mps;
109
110 /**
111  * Number slave peers
112  */
113 static unsigned int num_slaves;
114
115 /**
116  * Array of master peers
117  */
118 static struct BenchmarkPeer *sps;
119
120 static struct LoggingHandle *l;
121
122 static void
123 evaluate ()
124 {
125   int c_m;
126   int c_s;
127   unsigned int duration;
128   struct BenchmarkPeer *mp;
129   struct BenchmarkPartner *p;
130
131   unsigned int kb_sent_sec;
132   double kb_sent_percent;
133   unsigned int kb_recv_sec;
134   double kb_recv_percent;
135   unsigned int rtt;
136
137   duration = (perf_duration.rel_value_us / (1000 * 1000));
138   for (c_m = 0; c_m < num_masters; c_m++)
139   {
140     mp = &mps[c_m];
141     fprintf (stderr,
142         _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
143         mp->no, mp->total_bytes_sent / 1024, duration,
144         (mp->total_bytes_sent / 1024) / duration,
145         mp->total_bytes_received / 1024, duration,
146         (mp->total_bytes_received / 1024) / duration);
147
148     for (c_s = 0; c_s < num_slaves; c_s++)
149     {
150       p = &mp->partners[c_s];
151
152       kb_sent_sec = 0;
153       kb_recv_sec = 0;
154       kb_sent_percent = 0.0;
155       kb_recv_percent = 0.0;
156       rtt = 0;
157
158       if (duration > 0)
159       {
160           kb_sent_sec = (p->bytes_sent / 1024) / duration;
161           kb_recv_sec = (p->bytes_received / 1024) / duration;
162       }
163
164       if (mp->total_bytes_sent > 0)
165           kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
166       if (mp->total_bytes_received > 0)
167           kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
168       if (1000 * p->messages_sent > 0)
169           rtt = p->total_app_rtt / (1000 * p->messages_sent);
170       fprintf (stderr,
171           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
172           (mp->pref_partner == p->dest) ? '*' : ' ',
173           mp->no, p->dest->no,
174           kb_sent_sec, kb_sent_percent,
175                   kb_recv_sec, kb_recv_percent);
176       fprintf (stderr,
177           "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
178           (mp->pref_partner == p->dest) ? '*' : ' ',
179           mp->no, p->dest->no, rtt);
180     }
181   }
182 }
183
184 /**
185  * Shutdown nicely
186  *
187  * @param cls NULL
188  * @param tc the task context
189  */
190 static void
191 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 {
193
194   if (GNUNET_YES == logging)
195     GNUNET_ATS_TEST_logging_clean_up(l);
196
197   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
198   if (GNUNET_SCHEDULER_NO_TASK != progress_task)
199   {
200     fprintf (stderr, "0\n");
201     GNUNET_SCHEDULER_cancel (progress_task);
202   }
203   progress_task = GNUNET_SCHEDULER_NO_TASK;
204
205   evaluate ();
206   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
207
208   GNUNET_ATS_TEST_shutdown_topology();
209 }
210
211
212 static void
213 print_progress ()
214 {
215   static int calls;
216   progress_task = GNUNET_SCHEDULER_NO_TASK;
217
218   fprintf (stderr, "%llu..",
219       (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
220   calls++;
221
222   progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
223       &print_progress, NULL );
224 }
225
226 static void
227 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   struct BenchmarkPeer *me = cls;
230
231   me->ats_task = GNUNET_SCHEDULER_NO_TASK;
232
233   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
234       me->no, me->pref_partner->no, me->pref_value);
235   GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
236       &me->pref_partner->id,
237       pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
238   me->pref_value += TEST_ATS_PREFRENCE_DELTA;
239   me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
240       &ats_pref_task, cls);
241 }
242
243 static void
244 start_benchmark()
245 {
246   int c_m;
247   int c_s;
248
249   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
250
251   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
252     GNUNET_SCHEDULER_cancel(shutdown_task);
253   shutdown_task = GNUNET_SCHEDULER_add_delayed(perf_duration, &do_shutdown,
254       NULL );
255
256   progress_task = GNUNET_SCHEDULER_add_now(&print_progress, NULL );
257
258   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
259       "Topology connected, start benchmarking...\n");
260
261   /* Start sending test messages */
262   for (c_m = 0; c_m < num_masters; c_m++)
263     {
264       for (c_s = 0; c_s < num_slaves; c_s++)
265       {
266         GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m], &mps[c_m].partners[c_s],
267             GNUNET_ATS_TEST_TG_LINEAR, UINT32_MAX, UINT32_MAX,
268             GNUNET_TIME_UNIT_MINUTES, GNUNET_TIME_UNIT_FOREVER_REL);
269       }
270       if (pref_val != GNUNET_ATS_PREFERENCE_END)
271         mps[c_m].ats_task = GNUNET_SCHEDULER_add_now(&ats_pref_task, &mps[c_m]);
272     }
273
274   if (GNUNET_YES == logging)
275     l = GNUNET_ATS_TEST_logging_start (log_frequency, testname, mps,
276         num_masters, num_slaves, GNUNET_NO);
277 }
278
279 static void
280 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
281 {
282   mps = masters;
283   sps = slaves;
284
285   GNUNET_SCHEDULER_add_now(&start_benchmark, NULL);
286 }
287
288
289
290
291 static struct BenchmarkPartner *
292 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
293 {
294   int c_m;
295   GNUNET_assert (NULL != me);
296   GNUNET_assert (NULL != peer);
297
298   for (c_m = 0; c_m < me->num_partners; c_m++)
299   {
300     /* Find a partner with other as destination */
301     if (0 == memcmp (peer, &me->partners[c_m].dest->id,
302             sizeof(struct GNUNET_PeerIdentity)))
303     {
304       return &me->partners[c_m];
305     }
306   }
307   return NULL;
308 }
309
310 static void
311 test_recv_cb (void *cls,
312                       const struct GNUNET_PeerIdentity * peer,
313                       const struct GNUNET_MessageHeader * message)
314 {
315
316 }
317
318
319 static void
320 log_request_cb (void *cls, const struct GNUNET_HELLO_Address *address,
321     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
322     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
323     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
324 {
325   struct BenchmarkPeer *me = cls;
326   struct BenchmarkPartner *p;
327   int c_a;
328   char *peer_id;
329
330   p = find_partner (me, &address->peer);
331   if (NULL == p)
332   {
333     /* This is not one of my partners
334      * Will happen since the peers will connect to each other due to gossiping
335      */
336     return;
337   }
338   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
339
340   if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
341       (p->bandwidth_out != ntohl (bandwidth_out.value__)))
342   p->bandwidth_in = ntohl (bandwidth_in.value__);
343   p->bandwidth_out = ntohl (bandwidth_out.value__);
344
345   for (c_a = 0; c_a < ats_count; c_a++)
346   {
347     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
348         (GNUNET_YES == p->me->master) ? "Master" : "Slave",
349         p->me->no,
350         GNUNET_i2s (&p->dest->id),
351         GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
352         ntohl(ats[c_a].value));
353   }
354   GNUNET_free(peer_id);
355   if (NULL != l)
356     GNUNET_ATS_TEST_logging_now (l);
357 }
358
359
360 /*
361  * Start the performance test case
362  */
363 int
364 main (int argc, char *argv[])
365 {
366   char *tmp;
367   char *tmp_sep;
368   char *test_name;
369   char *conf_name;
370   char *comm_name;
371   char *dotexe;
372   char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
373   int c;
374
375   result = 0;
376
377   /* figure out testname */
378   tmp = strstr (argv[0], TESTNAME_PREFIX);
379   if (NULL == tmp)
380   {
381     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
382     return GNUNET_SYSERR;
383   }
384   tmp += strlen (TESTNAME_PREFIX);
385   solver = GNUNET_strdup (tmp);
386   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
387     dotexe[0] = '\0';
388   tmp_sep = strchr (solver, '_');
389   if (NULL == tmp_sep)
390   {
391     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
392     GNUNET_free(solver);
393     return GNUNET_SYSERR;
394   }
395   tmp_sep[0] = '\0';
396   comm_name = GNUNET_strdup (&tmp_sep[1]);
397   tmp_sep = strchr (comm_name, '_');
398   if (NULL == tmp_sep)
399   {
400     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
401     GNUNET_free(solver);
402     return GNUNET_SYSERR;
403   }
404   tmp_sep[0] = '\0';
405   for (c = 0; c <= strlen (comm_name); c++)
406     comm_name[c] = toupper (comm_name[c]);
407   if (0 == strcmp (comm_name, "CORE"))
408     test_core = GNUNET_YES;
409   else if (0 == strcmp (comm_name, "TRANSPORT"))
410     test_core = GNUNET_NO;
411   else
412   {
413     GNUNET_free (comm_name);
414     GNUNET_free (solver);
415     return GNUNET_SYSERR;
416   }
417
418   pref_str = GNUNET_strdup(tmp_sep + 1);
419
420   GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
421       pref_str);
422   GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
423
424   for (c = 0; c <= strlen (pref_str); c++)
425     pref_str[c] = toupper (pref_str[c]);
426   pref_val = -1;
427
428   if (0 != strcmp (pref_str, "NONE"))
429   {
430     for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
431     {
432       if (0 == strcmp (pref_str, prefs[c]))
433       {
434         pref_val = c;
435         break;
436       }
437     }
438   }
439   else
440   {
441     /* abuse terminator to indicate no pref */
442     pref_val = GNUNET_ATS_PREFERENCE_END;
443   }
444   if (-1 == pref_val)
445   {
446     fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
447     GNUNET_free(solver);
448     GNUNET_free(pref_str);
449     GNUNET_free (comm_name);
450     return -1;
451   }
452
453   for (c = 0; c < (argc - 1); c++)
454   {
455     if (0 == strcmp (argv[c], "-d"))
456       break;
457   }
458   if (c < argc - 1)
459   {
460     if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
461         fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
462   }
463   else
464   {
465     perf_duration = BENCHMARK_DURATION;
466   }
467   fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
468
469   for (c = 0; c < (argc - 1); c++)
470   {
471     if (0 == strcmp (argv[c], "-s"))
472       break;
473   }
474   if (c < argc - 1)
475   {
476     if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
477         && (num_slaves >= 1))
478       fprintf (stderr, "Starting %u slave peers\n", num_slaves);
479     else
480       num_slaves = DEFAULT_SLAVES_NUM;
481   }
482   else
483     num_slaves = DEFAULT_SLAVES_NUM;
484
485   for (c = 0; c < (argc - 1); c++)
486   {
487     if (0 == strcmp (argv[c], "-m"))
488       break;
489   }
490   if (c < argc - 1)
491   {
492     if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
493         && (num_masters >= 2))
494       fprintf (stderr, "Starting %u master peers\n", num_masters);
495     else
496       num_masters = DEFAULT_MASTERS_NUM;
497   }
498   else
499     num_masters = DEFAULT_MASTERS_NUM;
500
501   logging = GNUNET_NO;
502   for (c = 0; c < argc; c++)
503   {
504     if (0 == strcmp (argv[c], "-l"))
505       logging = GNUNET_YES;
506   }
507
508   if (GNUNET_YES == logging)
509   {
510     for (c = 0; c < (argc - 1); c++)
511     {
512       if (0 == strcmp (argv[c], "-f"))
513         break;
514     }
515     if (c < argc - 1)
516     {
517       if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
518           fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
519     }
520     else
521     {
522       log_frequency = LOGGING_FREQUENCY;
523     }
524     fprintf (stderr, "Using log frequency %llu ms\n",
525         (unsigned long long) (log_frequency.rel_value_us) / (1000));
526   }
527
528   GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
529
530   if (num_slaves < num_masters)
531   {
532     fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
533     GNUNET_free(test_name);
534     GNUNET_free(solver);
535     GNUNET_free(pref_str);
536     GNUNET_free (comm_name);
537     return GNUNET_SYSERR;
538   }
539
540   /**
541    * Setup the topology
542    */
543   GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
544       num_slaves, num_masters,
545       test_core,
546       &do_benchmark,
547       NULL,
548       &test_recv_cb,
549       &log_request_cb);
550
551   return result;
552 }
553
554 /* end of file perf_ats.c */