use correct plugin name; thanks Corvus Corax
[oweals/gnunet.git] / src / ats-tests / gnunet-ats-sim.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2010-2013 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-tests/gnunet-ats-sim.c
22  * @brief ats traffic simulator: this tool uses the ats-test library to setup a
23  * topology and generate traffic between these peers. The traffic description
24  * is loaded from a experiment description file
25  * @author Christian Grothoff
26  * @author Matthias Wachs
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_core_service.h"
33 #include "ats-testing.h"
34
35 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
36                                                     10)
37
38 static struct BenchmarkPeer *masters_p;
39 static struct BenchmarkPeer *slaves_p;
40
41 /**
42  * cmd option -e: experiment file
43  */
44 static char *opt_exp_file;
45
46 /**
47  * cmd option -l: enable logging
48  */
49 static int opt_log;
50
51 /**
52  * cmd option -p: enable plots
53  */
54 static int opt_plot;
55
56 /**
57  * cmd option -v: verbose logs
58  */
59 static int opt_verbose;
60
61 static struct GNUNET_SCHEDULER_Task *timeout_task;
62
63 static struct Experiment *e;
64
65 static struct LoggingHandle *l;
66
67
68 static void
69 evaluate (struct GNUNET_TIME_Relative duration_total)
70 {
71   int c_m;
72   int c_s;
73   unsigned int duration;
74   struct BenchmarkPeer *mp;
75   struct BenchmarkPartner *p;
76
77   unsigned int b_sent_sec;
78   double kb_sent_percent;
79   unsigned int b_recv_sec;
80   double kb_recv_percent;
81   unsigned int rtt;
82
83
84   duration = (duration_total.rel_value_us / (1000 * 1000));
85   if (0 == duration)
86     duration = 1;
87   for (c_m = 0; c_m < e->num_masters; c_m++)
88   {
89     mp = &masters_p[c_m];
90     fprintf (stderr,
91              _ (
92                "Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
93              mp->no, mp->total_bytes_sent / 1024,
94              duration,
95              (mp->total_bytes_sent / 1024) / duration,
96              mp->total_bytes_received / 1024,
97              duration,
98              (mp->total_bytes_received / 1024) / duration);
99
100     for (c_s = 0; c_s < e->num_slaves; c_s++)
101     {
102       p = &mp->partners[c_s];
103
104       b_sent_sec = 0;
105       b_recv_sec = 0;
106       kb_sent_percent = 0.0;
107       kb_recv_percent = 0.0;
108       rtt = 0;
109
110       if (duration > 0)
111       {
112         b_sent_sec = p->bytes_sent / duration;
113         b_recv_sec = p->bytes_received / duration;
114       }
115
116       if (mp->total_bytes_sent > 0)
117         kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
118       if (mp->total_bytes_received > 0)
119         kb_recv_percent = ((double) p->bytes_received * 100)
120                           / mp->total_bytes_received;
121       if (1000 * p->messages_sent > 0)
122         rtt = p->total_app_rtt / (1000 * p->messages_sent);
123       fprintf (stderr,
124                "%c Master [%u] -> Slave [%u]: sent %u Bips (%.2f %%), received %u Bips (%.2f %%)\n",
125                (mp->pref_partner == p->dest) ? '*' : ' ',
126                mp->no, p->dest->no,
127                b_sent_sec, kb_sent_percent,
128                b_recv_sec, kb_recv_percent);
129       fprintf (stderr,
130                "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
131                (mp->pref_partner == p->dest) ? '*' : ' ',
132                mp->no, p->dest->no, rtt);
133     }
134   }
135 }
136
137
138 static void
139 do_shutdown (void *cls)
140 {
141   fprintf (stderr, "Shutdown\n");
142   if (NULL != timeout_task)
143   {
144     GNUNET_SCHEDULER_cancel (timeout_task);
145     timeout_task = NULL;
146   }
147   if (NULL != l)
148   {
149     GNUNET_ATS_TEST_logging_stop (l);
150     GNUNET_ATS_TEST_logging_clean_up (l);
151     l = NULL;
152   }
153
154   /* Stop traffic generation */
155   GNUNET_ATS_TEST_generate_traffic_stop_all ();
156
157   /* Stop all preference generations */
158   GNUNET_ATS_TEST_generate_preferences_stop_all ();
159
160   if (NULL != e)
161   {
162     GNUNET_ATS_TEST_experimentation_stop (e);
163     e = NULL;
164   }
165   GNUNET_ATS_TEST_shutdown_topology ();
166 }
167
168
169 static void
170 do_timeout (void *cls)
171 {
172   timeout_task = NULL;
173   GNUNET_SCHEDULER_shutdown ();
174 }
175
176
177 static void
178 log_request__cb (void *cls,
179                  const struct GNUNET_HELLO_Address *address,
180                  int address_active,
181                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
182                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
183                  const struct GNUNET_ATS_Properties *ats)
184 {
185   if (NULL != l)
186   {
187     // GNUNET_break (0);
188     // GNUNET_ATS_TEST_logging_now (l);
189   }
190 }
191
192
193 static void
194 experiment_done_cb (struct Experiment *e,
195                     struct GNUNET_TIME_Relative duration,
196                     int success)
197 {
198   if (GNUNET_OK == success)
199     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200                 "Experiment done successful in %s\n",
201                 GNUNET_STRINGS_relative_time_to_string (duration,
202                                                         GNUNET_YES));
203   else
204     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
205
206   /* Stop logging */
207   GNUNET_ATS_TEST_logging_stop (l);
208
209   /* Stop traffic generation */
210   GNUNET_ATS_TEST_generate_traffic_stop_all ();
211
212   /* Stop all preference generations */
213   GNUNET_ATS_TEST_generate_preferences_stop_all ();
214
215   evaluate (duration);
216   if (opt_log)
217     GNUNET_ATS_TEST_logging_write_to_file (l, opt_exp_file, opt_plot);
218   GNUNET_SCHEDULER_shutdown ();
219 }
220
221
222 static void
223 episode_done_cb (struct Episode *ep)
224 {
225   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
226               "Episode %u done\n",
227               ep->id);
228 }
229
230
231 static void
232 topology_setup_done (void *cls,
233                      struct BenchmarkPeer *masters,
234                      struct BenchmarkPeer *slaves)
235 {
236   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237               "Topology setup complete!\n");
238
239   masters_p = masters;
240   slaves_p = slaves;
241
242   l = GNUNET_ATS_TEST_logging_start (e->log_freq,
243                                      e->name,
244                                      masters_p,
245                                      e->num_masters, e->num_slaves,
246                                      opt_verbose);
247   GNUNET_ATS_TEST_experimentation_run (e,
248                                        &episode_done_cb,
249                                        &experiment_done_cb);
250 /*
251    GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
252       GNUNET_ATS_TEST_TG_CONSTANT, 1, 1, GNUNET_TIME_UNIT_SECONDS,
253       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
254       GNUNET_ATS_PREFERENCE_BANDWIDTH);
255  */
256 /*
257    GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
258       GNUNET_ATS_TEST_TG_LINEAR, 1, 50,
259       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
260       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
261       GNUNET_ATS_PREFERENCE_BANDWIDTH);
262  */
263 /*
264    GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
265         GNUNET_ATS_TEST_TG_RANDOM, 1, 50,
266         GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
267         GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
268         GNUNET_ATS_PREFERENCE_BANDWIDTH);
269  */
270 /*
271    GNUNET_ATS_TEST_generate_preferences_start(&masters[0],&masters[0].partners[0],
272       GNUNET_ATS_TEST_TG_SINUS, 10, 5,
273       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
274       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250),
275       GNUNET_ATS_PREFERENCE_BANDWIDTH);
276  */
277 #if 0
278   int c_m;
279   int c_s;
280   for (c_m = 0; c_m < e->num_masters; c_m++)
281   {
282     for (c_s = 0; c_s < e->num_slaves; c_s++)
283     {
284       /* Generate maximum traffic to all peers */
285       /* Example: Generate traffic with constant 10,000 Bytes/s */
286       GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
287                                               &masters[c_m].partners[c_s],
288                                               GNUNET_ATS_TEST_TG_CONSTANT,
289                                               10000,
290                                               GNUNET_TIME_UNIT_FOREVER_REL);
291       /* Example: Generate traffic with an increasing rate from 1000 to 2000
292        * Bytes/s with in a minute */
293       GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
294                                               &masters[c_m].partners[c_s],
295                                               GNUNET_ATS_TEST_TG_LINEAR,
296                                               1000,
297                                               2000,
298                                               GNUNET_TIME_UNIT_MINUTES,
299                                               GNUNET_TIME_UNIT_FOREVER_REL);
300       /* Example: Generate traffic with a random rate between 1000 to 2000
301        * Bytes/s */
302       GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
303                                               &masters[c_m].partners[c_s],
304                                               GNUNET_ATS_TEST_TG_RANDOM,
305                                               1000,
306                                               2000,
307                                               GNUNET_TIME_UNIT_FOREVER_REL,
308                                               GNUNET_TIME_UNIT_FOREVER_REL);
309       /* Example: Generate traffic with a sinus form, a base rate of
310        * 1000 Bytes/s, an amplitude of (max-base), and a period of 1 minute */
311       GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
312                                               &masters[c_m].partners[c_s],
313                                               GNUNET_ATS_TEST_TG_SINUS,
314                                               1000,
315                                               2000,
316                                               GNUNET_TIME_UNIT_MINUTES,
317                                               GNUNET_TIME_UNIT_FOREVER_REL);
318     }
319   }
320 #endif
321
322   timeout_task
323     = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (
324                                       GNUNET_TIME_UNIT_MINUTES,
325                                       e->max_duration),
326                                     &do_timeout,
327                                     NULL);
328   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
329 }
330
331
332 static void
333 parse_args (int argc, char *argv[])
334 {
335   int c;
336
337   opt_exp_file = NULL;
338   opt_log = GNUNET_NO;
339   opt_plot = GNUNET_NO;
340
341   for (c = 0; c < argc; c++)
342   {
343     if ((c < (argc - 1)) && (0 == strcmp (argv[c], "-e")))
344     {
345       GNUNET_free_non_null (opt_exp_file);
346       opt_exp_file = GNUNET_strdup (argv[c + 1]);
347     }
348     if (0 == strcmp (argv[c], "-l"))
349     {
350       opt_log = GNUNET_YES;
351     }
352     if (0 == strcmp (argv[c], "-p"))
353     {
354       opt_plot = GNUNET_YES;
355     }
356     if (0 == strcmp (argv[c], "-v"))
357     {
358       opt_verbose = GNUNET_YES;
359     }
360   }
361 }
362
363
364 int
365 main (int argc, char *argv[])
366 {
367   GNUNET_log_setup ("gnunet-ats-sim", "INFO", NULL);
368
369   parse_args (argc, argv);
370   if (NULL == opt_exp_file)
371   {
372     fprintf (stderr, "No experiment given...\n");
373     return 1;
374   }
375
376   fprintf (stderr, "Loading experiment `%s' \n", opt_exp_file);
377   e = GNUNET_ATS_TEST_experimentation_load (opt_exp_file);
378   if (NULL == e)
379   {
380     fprintf (stderr, "Invalid experiment\n");
381     return 1;
382   }
383   if (0 == e->num_episodes)
384   {
385     fprintf (stderr, "No episodes included\n");
386     return 1;
387   }
388
389   /* Setup a topology with */
390   GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
391                                    e->num_slaves,
392                                    e->num_masters,
393                                    GNUNET_NO,
394                                    &topology_setup_done,
395                                    NULL,
396                                    &log_request__cb);
397   GNUNET_free (opt_exp_file);
398   return 0;
399 }
400 /* end of file gnunet-ats-sim.c */