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