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