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