2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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"
35 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
37 static struct BenchmarkPeer *masters_p;
38 static struct BenchmarkPeer *slaves_p;
41 * cmd option -e: experiment file
43 static char *opt_exp_file;
46 * cmd option -l: enable logging
51 * cmd option -p: enable plots
56 * cmd option -v: verbose logs
58 static int opt_verbose;
60 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
63 struct LoggingHandle *l;
66 evaluate (struct GNUNET_TIME_Relative duration_total)
70 unsigned int duration;
71 struct BenchmarkPeer *mp;
72 struct BenchmarkPartner *p;
74 unsigned int b_sent_sec;
75 double kb_sent_percent;
76 unsigned int b_recv_sec;
77 double kb_recv_percent;
81 duration = (duration_total.rel_value_us / (1000 * 1000));
82 for (c_m = 0; c_m < e->num_masters; c_m++)
86 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
87 mp->no, mp->total_bytes_sent / 1024, duration,
88 (mp->total_bytes_sent / 1024) / duration,
89 mp->total_bytes_received / 1024, duration,
90 (mp->total_bytes_received / 1024) / duration);
92 for (c_s = 0; c_s < e->num_slaves; c_s++)
94 p = &mp->partners[c_s];
98 kb_sent_percent = 0.0;
99 kb_recv_percent = 0.0;
104 b_sent_sec = p->bytes_sent / duration;
105 b_recv_sec = p->bytes_received / duration;
108 if (mp->total_bytes_sent > 0)
109 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
110 if (mp->total_bytes_received > 0)
111 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
112 if (1000 * p->messages_sent > 0)
113 rtt = p->total_app_rtt / (1000 * p->messages_sent);
115 "%c Master [%u] -> Slave [%u]: sent %u Bips (%.2f %%), received %u Bips (%.2f %%)\n",
116 (mp->pref_partner == p->dest) ? '*' : ' ',
118 b_sent_sec, kb_sent_percent,
119 b_recv_sec, kb_recv_percent);
121 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
122 (mp->pref_partner == p->dest) ? '*' : ' ',
123 mp->no, p->dest->no, rtt);
131 fprintf (stderr, "Shutdown\n");
135 GNUNET_ATS_TEST_logging_stop (l);
136 GNUNET_ATS_TEST_logging_clean_up (l);
142 GNUNET_ATS_TEST_experimentation_stop (e);
146 GNUNET_ATS_TEST_shutdown_topology ();
151 transport_recv_cb (void *cls,
152 const struct GNUNET_PeerIdentity * peer,
153 const struct GNUNET_MessageHeader * message)
159 log_request__cb (void *cls, const struct GNUNET_HELLO_Address *address,
160 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
161 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
162 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
168 //GNUNET_ATS_TEST_logging_now (l);
174 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
176 if (GNUNET_OK == success)
177 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
178 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
180 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
181 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
183 GNUNET_SCHEDULER_cancel (timeout_task);
184 timeout_task = GNUNET_SCHEDULER_NO_TASK;
187 GNUNET_ATS_TEST_logging_stop (l);
189 /* Stop traffic generation */
190 GNUNET_ATS_TEST_generate_traffic_stop_all();
194 GNUNET_ATS_TEST_logging_write_to_file(l, opt_exp_file, opt_plot);
198 GNUNET_ATS_TEST_logging_stop (l);
199 GNUNET_ATS_TEST_logging_clean_up (l);
203 /* Clean up experiment */
204 GNUNET_ATS_TEST_experimentation_stop (e);
207 /* Shutdown topology */
208 GNUNET_ATS_TEST_shutdown_topology ();
212 episode_done_cb (struct Episode *ep)
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
217 static void topology_setup_done (void *cls,
218 struct BenchmarkPeer *masters,
219 struct BenchmarkPeer *slaves)
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n");
226 l = GNUNET_ATS_TEST_logging_start (e->log_freq,
229 e->num_masters, e->num_slaves,
231 GNUNET_ATS_TEST_experimentation_run (e, &episode_done_cb, &experiment_done_cb);
236 for (c_m = 0; c_m < e->num_masters; c_m++)
238 for (c_s = 0; c_s < e->num_slaves; c_s++)
240 /* Generate maximum traffic to all peers */
241 /* Example: Generate traffic with constant 10,000 Bytes/s */
242 GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
243 &masters[c_m].partners[c_s],
244 GNUNET_ATS_TEST_TG_CONSTANT,
246 GNUNET_TIME_UNIT_FOREVER_REL);
247 /* Example: Generate traffic with an increasing rate from 1000 to 2000
248 * Bytes/s with in a minute */
249 GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
250 &masters[c_m].partners[c_s],
251 GNUNET_ATS_TEST_TG_LINEAR,
254 GNUNET_TIME_UNIT_MINUTES,
255 GNUNET_TIME_UNIT_FOREVER_REL);
256 /* Example: Generate traffic with a random rate between 1000 to 2000
258 GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
259 &masters[c_m].partners[c_s],
260 GNUNET_ATS_TEST_TG_RANDOM,
263 GNUNET_TIME_UNIT_FOREVER_REL,
264 GNUNET_TIME_UNIT_FOREVER_REL);
265 /* Example: Generate traffic with a sinus form, a base rate of
266 * 1000 Bytes/s, an amplitude of (max-base), and a period of 1 minute */
267 GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
268 &masters[c_m].partners[c_s],
269 GNUNET_ATS_TEST_TG_SINUS,
272 GNUNET_TIME_UNIT_MINUTES,
273 GNUNET_TIME_UNIT_FOREVER_REL);
278 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
279 e->max_duration), &do_shutdown, NULL);
283 parse_args (int argc, char *argv[])
288 opt_plot = GNUNET_NO;
290 for (c = 0; c < argc; c++)
292 if ((c < (argc - 1)) && (0 == strcmp (argv[c], "-e")))
294 opt_exp_file = GNUNET_strdup ( argv[c + 1]);
296 if (0 == strcmp (argv[c], "-l"))
298 opt_log = GNUNET_YES;
300 if (0 == strcmp (argv[c], "-p"))
302 opt_plot = GNUNET_YES;
304 if (0 == strcmp (argv[c], "-v"))
306 opt_verbose = GNUNET_YES;
312 main (int argc, char *argv[])
314 GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);
316 parse_args (argc, argv);
317 if (NULL == opt_exp_file )
319 fprintf (stderr, "No experiment given...\n");
323 fprintf (stderr, "Loading experiment `%s' \n", opt_exp_file );
324 e = GNUNET_ATS_TEST_experimentation_load (opt_exp_file);
327 fprintf (stderr, "Invalid experiment\n");
330 if (0 == e->num_episodes)
332 fprintf (stderr, "No episodes included\n");
336 /* Setup a topology with */
337 GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
341 &topology_setup_done,
345 GNUNET_free (opt_exp_file);
348 /* end of file gnunet-ats-sim.c */