changes to traffic generation
[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 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 = (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
150       kb_sent_sec = 0;
151       kb_recv_sec = 0;
152       kb_sent_percent = 0.0;
153       kb_recv_percent = 0.0;
154       rtt = 0;
155
156       if (duration > 0)
157       {
158           kb_sent_sec = (p->bytes_sent / 1024) / duration;
159           kb_recv_sec = (p->bytes_received / 1024) / duration;
160       }
161
162       if (mp->total_bytes_sent > 0)
163           kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
164       if (mp->total_bytes_received > 0)
165           kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
166       if (1000 * p->messages_sent > 0)
167           rtt = p->total_app_rtt / (1000 * p->messages_sent);
168       fprintf (stderr,
169           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
170           (mp->pref_partner == p->dest) ? '*' : ' ',
171           mp->no, p->dest->no,
172           kb_sent_sec, kb_sent_percent,
173                   kb_recv_sec, kb_recv_percent);
174       fprintf (stderr,
175           "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
176           (mp->pref_partner == p->dest) ? '*' : ' ',
177           mp->no, p->dest->no, rtt);
178     }
179   }
180 }
181
182 /**
183  * Shutdown nicely
184  *
185  * @param cls NULL
186  * @param tc the task context
187  */
188 static void
189 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 {
191
192   if (GNUNET_YES == logging)
193     GNUNET_ATS_TEST_logging_stop();
194
195   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
196   if (GNUNET_SCHEDULER_NO_TASK != progress_task)
197   {
198     fprintf (stderr, "0\n");
199     GNUNET_SCHEDULER_cancel (progress_task);
200   }
201   progress_task = GNUNET_SCHEDULER_NO_TASK;
202
203   evaluate ();
204   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
205
206   GNUNET_ATS_TEST_shutdown_topology();
207 }
208
209
210 static void
211 print_progress ()
212 {
213   static int calls;
214   progress_task = GNUNET_SCHEDULER_NO_TASK;
215
216   fprintf (stderr, "%llu..",
217       (long long unsigned) perf_duration.rel_value_us / (1000 * 1000) - calls);
218   calls++;
219
220   progress_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
221       &print_progress, NULL );
222 }
223
224 static void
225 ats_pref_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 {
227   struct BenchmarkPeer *me = cls;
228
229   me->ats_task = GNUNET_SCHEDULER_NO_TASK;
230
231   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, " Master [%u] set preference for slave [%u] to %f\n",
232       me->no, me->pref_partner->no, me->pref_value);
233   GNUNET_ATS_performance_change_preference (me->ats_perf_handle,
234       &me->pref_partner->id,
235       pref_val, me->pref_value, GNUNET_ATS_PREFERENCE_END);
236   me->pref_value += TEST_ATS_PREFRENCE_DELTA;
237   me->ats_task = GNUNET_SCHEDULER_add_delayed (TEST_ATS_PREFRENCE_FREQUENCY,
238       &ats_pref_task, cls);
239 }
240
241 static void
242 start_benchmark()
243 {
244   int c_m;
245   int c_s;
246
247   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking start\n"));
248
249   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
250     GNUNET_SCHEDULER_cancel(shutdown_task);
251   shutdown_task = GNUNET_SCHEDULER_add_delayed(perf_duration, &do_shutdown,
252       NULL );
253
254   progress_task = GNUNET_SCHEDULER_add_now(&print_progress, NULL );
255
256   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
257       "Topology connected, start benchmarking...\n");
258
259   /* Start sending test messages */
260   for (c_m = 0; c_m < num_masters; c_m++)
261     {
262       for (c_s = 0; c_s < num_slaves; c_s++)
263       {
264         GNUNET_ATS_TEST_generate_traffic_start (&mps[c_m], &mps[c_m].partners[c_s],
265             UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL);
266       }
267       if (pref_val != GNUNET_ATS_PREFERENCE_END)
268         mps[c_m].ats_task = GNUNET_SCHEDULER_add_now(&ats_pref_task, &mps[c_m]);
269     }
270
271   if (GNUNET_YES == logging)
272     GNUNET_ATS_TEST_logging_start(log_frequency, testname, mps, num_masters);
273 }
274
275 static void
276 do_benchmark (void *cls, struct BenchmarkPeer *masters, struct BenchmarkPeer *slaves)
277 {
278   mps = masters;
279   sps = slaves;
280
281   GNUNET_SCHEDULER_add_now(&start_benchmark, NULL);
282 }
283
284
285
286
287 static struct BenchmarkPartner *
288 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
289 {
290   int c_m;
291   GNUNET_assert (NULL != me);
292   GNUNET_assert (NULL != peer);
293
294   for (c_m = 0; c_m < me->num_partners; c_m++)
295   {
296     /* Find a partner with other as destination */
297     if (0 == memcmp (peer, &me->partners[c_m].dest->id,
298             sizeof(struct GNUNET_PeerIdentity)))
299     {
300       return &me->partners[c_m];
301     }
302   }
303   return NULL;
304 }
305
306 static void
307 test_recv_cb (void *cls,
308                       const struct GNUNET_PeerIdentity * peer,
309                       const struct GNUNET_MessageHeader * message)
310 {
311
312 }
313
314
315 static void
316 ats_performance_info_cb (void *cls, const struct GNUNET_HELLO_Address *address,
317     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
318     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
319     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
320 {
321   struct BenchmarkPeer *me = cls;
322   struct BenchmarkPartner *p;
323   int c_a;
324   int log;
325   char *peer_id;
326
327   p = find_partner (me, &address->peer);
328   if (NULL == p)
329   {
330     /* This is not one of my partners
331      * Will happen since the peers will connect to each other due to gossiping
332      */
333     return;
334   }
335   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
336
337   log = GNUNET_NO;
338   if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
339       (p->bandwidth_out != ntohl (bandwidth_out.value__)))
340       log = GNUNET_YES;
341   p->bandwidth_in = ntohl (bandwidth_in.value__);
342   p->bandwidth_out = ntohl (bandwidth_out.value__);
343
344   for (c_a = 0; c_a < ats_count; c_a++)
345   {
346     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s [%u] received ATS information: %s %s %u\n",
347         (GNUNET_YES == p->me->master) ? "Master" : "Slave",
348         p->me->no,
349         GNUNET_i2s (&p->dest->id),
350         GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
351         ntohl(ats[c_a].value));
352   }
353   GNUNET_free(peer_id);
354
355 }
356
357
358 /*
359  * Start the performance test case
360  */
361 int
362 main (int argc, char *argv[])
363 {
364   char *tmp;
365   char *tmp_sep;
366   char *test_name;
367   char *conf_name;
368   char *comm_name;
369   char *dotexe;
370   char *prefs[GNUNET_ATS_PreferenceCount] = GNUNET_ATS_PreferenceTypeString;
371   int c;
372
373   result = 0;
374
375   /* figure out testname */
376   tmp = strstr (argv[0], TESTNAME_PREFIX);
377   if (NULL == tmp)
378   {
379     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
380     return GNUNET_SYSERR;
381   }
382   tmp += strlen (TESTNAME_PREFIX);
383   solver = GNUNET_strdup (tmp);
384   if (NULL != (dotexe = strstr (solver, ".exe")) && dotexe[4] == '\0')
385     dotexe[0] = '\0';
386   tmp_sep = strchr (solver, '_');
387   if (NULL == tmp_sep)
388   {
389     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
390     GNUNET_free(solver);
391     return GNUNET_SYSERR;
392   }
393   tmp_sep[0] = '\0';
394   comm_name = GNUNET_strdup (&tmp_sep[1]);
395   tmp_sep = strchr (comm_name, '_');
396   if (NULL == tmp_sep)
397   {
398     fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
399     GNUNET_free(solver);
400     return GNUNET_SYSERR;
401   }
402   tmp_sep[0] = '\0';
403   for (c = 0; c <= strlen (comm_name); c++)
404     comm_name[c] = toupper (comm_name[c]);
405   if (0 == strcmp (comm_name, "CORE"))
406     test_core = GNUNET_YES;
407   else if (0 == strcmp (comm_name, "TRANSPORT"))
408     test_core = GNUNET_NO;
409   else
410   {
411     GNUNET_free (comm_name);
412     GNUNET_free (solver);
413     return GNUNET_SYSERR;
414   }
415
416   pref_str = GNUNET_strdup(tmp_sep + 1);
417
418   GNUNET_asprintf (&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver,
419       pref_str);
420   GNUNET_asprintf (&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, pref_str);
421
422   for (c = 0; c <= strlen (pref_str); c++)
423     pref_str[c] = toupper (pref_str[c]);
424   pref_val = -1;
425
426   if (0 != strcmp (pref_str, "NONE"))
427   {
428     for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
429     {
430       if (0 == strcmp (pref_str, prefs[c]))
431       {
432         pref_val = c;
433         break;
434       }
435     }
436   }
437   else
438   {
439     /* abuse terminator to indicate no pref */
440     pref_val = GNUNET_ATS_PREFERENCE_END;
441   }
442   if (-1 == pref_val)
443   {
444     fprintf (stderr, "Unknown preference: `%s'\n", pref_str);
445     GNUNET_free(solver);
446     GNUNET_free(pref_str);
447     GNUNET_free (comm_name);
448     return -1;
449   }
450
451   for (c = 0; c < (argc - 1); c++)
452   {
453     if (0 == strcmp (argv[c], "-d"))
454       break;
455   }
456   if (c < argc - 1)
457   {
458     if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &perf_duration))
459         fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
460   }
461   else
462   {
463     perf_duration = BENCHMARK_DURATION;
464   }
465   fprintf (stderr, "Running benchmark for %llu secs\n", (unsigned long long) (perf_duration.rel_value_us) / (1000 * 1000));
466
467   for (c = 0; c < (argc - 1); c++)
468   {
469     if (0 == strcmp (argv[c], "-s"))
470       break;
471   }
472   if (c < argc - 1)
473   {
474     if ((0L != (num_slaves = strtol (argv[c + 1], NULL, 10)))
475         && (num_slaves >= 1))
476       fprintf (stderr, "Starting %u slave peers\n", num_slaves);
477     else
478       num_slaves = DEFAULT_SLAVES_NUM;
479   }
480   else
481     num_slaves = DEFAULT_SLAVES_NUM;
482
483   for (c = 0; c < (argc - 1); c++)
484   {
485     if (0 == strcmp (argv[c], "-m"))
486       break;
487   }
488   if (c < argc - 1)
489   {
490     if ((0L != (num_masters = strtol (argv[c + 1], NULL, 10)))
491         && (num_masters >= 2))
492       fprintf (stderr, "Starting %u master peers\n", num_masters);
493     else
494       num_masters = DEFAULT_MASTERS_NUM;
495   }
496   else
497     num_masters = DEFAULT_MASTERS_NUM;
498
499   logging = GNUNET_NO;
500   for (c = 0; c < argc; c++)
501   {
502     if (0 == strcmp (argv[c], "-l"))
503       logging = GNUNET_YES;
504   }
505
506   if (GNUNET_YES == logging)
507   {
508     for (c = 0; c < (argc - 1); c++)
509     {
510       if (0 == strcmp (argv[c], "-f"))
511         break;
512     }
513     if (c < argc - 1)
514     {
515       if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (argv[c + 1], &log_frequency))
516           fprintf (stderr, "Failed to parse duration `%s'\n", argv[c + 1]);
517     }
518     else
519     {
520       log_frequency = LOGGING_FREQUENCY;
521     }
522     fprintf (stderr, "Using log frequency %llu ms\n",
523         (unsigned long long) (log_frequency.rel_value_us) / (1000));
524   }
525
526   GNUNET_asprintf (&testname, "%s_%s_%s",solver, comm_name, pref_str);
527
528   if (num_slaves < num_masters)
529   {
530     fprintf (stderr, "Number of master peers is lower than slaves! exit...\n");
531     GNUNET_free(test_name);
532     GNUNET_free(solver);
533     GNUNET_free(pref_str);
534     GNUNET_free (comm_name);
535     return GNUNET_SYSERR;
536   }
537
538   /**
539    * Setup the topology
540    */
541   GNUNET_ATS_TEST_create_topology ("perf-ats", conf_name,
542       num_slaves, num_masters,
543       test_core,
544       &do_benchmark,
545       NULL,
546       &test_recv_cb,
547       &ats_performance_info_cb);
548
549   return result;
550 }
551
552 /* end of file perf_ats.c */