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