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-test/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;
40 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
43 struct LoggingHandle *l;
50 unsigned int duration;
51 struct BenchmarkPeer *mp;
52 struct BenchmarkPartner *p;
54 unsigned int kb_sent_sec;
55 double kb_sent_percent;
56 unsigned int kb_recv_sec;
57 double kb_recv_percent;
60 duration = (TEST_TIMEOUT.rel_value_us / (1000 * 1000));
61 for (c_m = 0; c_m < e->num_masters; c_m++)
65 _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
66 mp->no, mp->total_bytes_sent / 1024, duration,
67 (mp->total_bytes_sent / 1024) / duration,
68 mp->total_bytes_received / 1024, duration,
69 (mp->total_bytes_received / 1024) / duration);
71 for (c_s = 0; c_s < e->num_slaves; c_s++)
73 p = &mp->partners[c_s];
77 kb_sent_percent = 0.0;
78 kb_recv_percent = 0.0;
83 kb_sent_sec = (p->bytes_sent / 1024) / duration;
84 kb_recv_sec = (p->bytes_received / 1024) / duration;
87 if (mp->total_bytes_sent > 0)
88 kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
89 if (mp->total_bytes_received > 0)
90 kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
91 if (1000 * p->messages_sent > 0)
92 rtt = p->total_app_rtt / (1000 * p->messages_sent);
94 "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
95 (mp->pref_partner == p->dest) ? '*' : ' ',
97 kb_sent_sec, kb_sent_percent,
98 kb_recv_sec, kb_recv_percent);
100 "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
101 (mp->pref_partner == p->dest) ? '*' : ' ',
102 mp->no, p->dest->no, rtt);
113 GNUNET_ATS_TEST_experimentation_stop (e);
116 GNUNET_ATS_TEST_shutdown_topology ();
120 transport_recv_cb (void *cls,
121 const struct GNUNET_PeerIdentity * peer,
122 const struct GNUNET_MessageHeader * message)
128 log_request__cb (void *cls, const struct GNUNET_HELLO_Address *address,
129 int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
130 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
131 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
134 GNUNET_ATS_TEST_logging_now (l);
138 experiment_done_cb (struct Experiment *e, int success)
140 if (GNUNET_OK == success)
141 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment `%s' done successful\n", e->name);
143 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment `%s' failed \n", e->name);
144 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
146 GNUNET_SCHEDULER_cancel (timeout_task);
147 timeout_task = GNUNET_SCHEDULER_NO_TASK;
150 GNUNET_ATS_TEST_logging_stop (l);
153 /* Stop traffic generation */
154 GNUNET_ATS_TEST_generate_traffic_stop_all();
155 /* Clean up experiment */
156 GNUNET_ATS_TEST_experimentation_stop (e);
159 /* Shutdown topology */
160 GNUNET_ATS_TEST_shutdown_topology ();
164 episode_done_cb (struct Episode *ep)
166 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
169 static void topology_setup_done (void *cls,
170 struct BenchmarkPeer *masters,
171 struct BenchmarkPeer *slaves)
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n");
180 l = GNUNET_ATS_TEST_logging_start (GNUNET_TIME_UNIT_SECONDS, e->name,
181 masters_p, e->num_masters);
182 GNUNET_ATS_TEST_experimentation_run (e, &episode_done_cb, &experiment_done_cb);
184 for (c_m = 0; c_m < e->num_masters; c_m++)
186 for (c_s = 0; c_s < e->num_slaves; c_s++)
188 /* Generate maximum traffic to all peers */
189 GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
190 &masters[c_m].partners[c_s],
192 GNUNET_TIME_UNIT_FOREVER_REL);
196 timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
197 e->max_duration), &do_shutdown, NULL);
202 main (int argc, char *argv[])
204 GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);
207 fprintf (stderr, "No experiment given...\n");
211 fprintf (stderr, "Loading experiment `%s' \n", argv[1]);
212 e = GNUNET_ATS_TEST_experimentation_load (argv[1]);
215 fprintf (stderr, "Invalid experiment\n");
218 if (0 == e->num_episodes)
220 fprintf (stderr, "No episodes included\n");
224 /* Setup a topology with */
225 GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
229 &topology_setup_done,
235 /* end of file gnunet-ats-sim.c */