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