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