remove extension
[oweals/gnunet.git] / src / ats-tests / gnunet-ats-sim.c
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18  Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
56
57 struct Experiment *e;
58 struct LoggingHandle *l;
59
60 static void
61 evaluate (struct GNUNET_TIME_Relative duration_total)
62 {
63   int c_m;
64   int c_s;
65   unsigned int duration;
66   struct BenchmarkPeer *mp;
67   struct BenchmarkPartner *p;
68
69   unsigned int kb_sent_sec;
70   double kb_sent_percent;
71   unsigned int kb_recv_sec;
72   double kb_recv_percent;
73   unsigned int rtt;
74
75
76   duration = (duration_total.rel_value_us / (1000 * 1000));
77   for (c_m = 0; c_m < e->num_masters; c_m++)
78   {
79     mp = &masters_p[c_m];
80     fprintf (stderr,
81         _("Master [%u]: sent: %u KiB in %u sec. = %u KiB/s, received: %u KiB in %u sec. = %u KiB/s\n"),
82         mp->no, mp->total_bytes_sent / 1024, duration,
83         (mp->total_bytes_sent / 1024) / duration,
84         mp->total_bytes_received / 1024, duration,
85         (mp->total_bytes_received / 1024) / duration);
86
87     for (c_s = 0; c_s < e->num_slaves; c_s++)
88     {
89       p = &mp->partners[c_s];
90
91       kb_sent_sec = 0;
92       kb_recv_sec = 0;
93       kb_sent_percent = 0.0;
94       kb_recv_percent = 0.0;
95       rtt = 0;
96
97       if (duration > 0)
98       {
99           kb_sent_sec = (p->bytes_sent / 1024) / duration;
100           kb_recv_sec = (p->bytes_received / 1024) / duration;
101       }
102
103       if (mp->total_bytes_sent > 0)
104           kb_sent_percent = ((double) p->bytes_sent * 100) / mp->total_bytes_sent;
105       if (mp->total_bytes_received > 0)
106           kb_recv_percent = ((double) p->bytes_received * 100) / mp->total_bytes_received;
107       if (1000 * p->messages_sent > 0)
108           rtt = p->total_app_rtt / (1000 * p->messages_sent);
109       fprintf (stderr,
110           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
111           (mp->pref_partner == p->dest) ? '*' : ' ',
112           mp->no, p->dest->no,
113           kb_sent_sec, kb_sent_percent,
114                   kb_recv_sec, kb_recv_percent);
115       fprintf (stderr,
116           "%c Master [%u] -> Slave [%u]: Average application layer RTT: %u ms\n",
117           (mp->pref_partner == p->dest) ? '*' : ' ',
118           mp->no, p->dest->no, rtt);
119     }
120   }
121 }
122
123 static void
124 do_shutdown ()
125 {
126   /* timeout */
127   if (NULL != e)
128   {
129     GNUNET_ATS_TEST_experimentation_stop (e);
130     e = NULL;
131   }
132   GNUNET_ATS_TEST_shutdown_topology ();
133 }
134
135 static void
136 transport_recv_cb (void *cls,
137                    const struct GNUNET_PeerIdentity * peer,
138                    const struct GNUNET_MessageHeader * message)
139 {
140
141 }
142
143 static void
144 log_request__cb (void *cls, const struct GNUNET_HELLO_Address *address,
145     int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
146     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
147     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
148 {
149   if (NULL != l)
150     GNUNET_ATS_TEST_logging_now (l);
151 }
152
153 static void
154 experiment_done_cb (struct Experiment *e, struct GNUNET_TIME_Relative duration,int success)
155 {
156   if (GNUNET_OK == success)
157     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment done successful in %s\n",
158         GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
159   else
160     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment failed \n");
161   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
162   {
163     GNUNET_SCHEDULER_cancel (timeout_task);
164     timeout_task = GNUNET_SCHEDULER_NO_TASK;
165   }
166   /* Stop logging */
167   GNUNET_ATS_TEST_logging_stop (l);
168   evaluate (duration);
169   if (opt_log)
170     GNUNET_ATS_TEST_logging_write_to_file(l, opt_exp_file, opt_plot);
171
172   /* Stop traffic generation */
173   GNUNET_ATS_TEST_generate_traffic_stop_all();
174   /* Clean up experiment */
175   GNUNET_ATS_TEST_experimentation_stop (e);
176   GNUNET_ATS_TEST_logging_clean_up (l);
177   e = NULL;
178
179   /* Shutdown topology */
180   GNUNET_ATS_TEST_shutdown_topology ();
181 }
182
183 static void
184 episode_done_cb (struct Episode *ep)
185 {
186   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
187 }
188
189 static void topology_setup_done (void *cls,
190     struct BenchmarkPeer *masters,
191     struct BenchmarkPeer *slaves)
192 {
193   int c_m;
194   int c_s;
195   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n");
196
197   masters_p = masters;
198   slaves_p = slaves;
199
200   l = GNUNET_ATS_TEST_logging_start (GNUNET_TIME_UNIT_SECONDS,
201       e->name,
202       masters_p,
203       e->num_masters);
204   GNUNET_ATS_TEST_experimentation_run (e, &episode_done_cb, &experiment_done_cb);
205
206   for (c_m = 0; c_m < e->num_masters; c_m++)
207   {
208       for (c_s = 0; c_s < e->num_slaves; c_s++)
209       {
210         /* Generate maximum traffic to all peers */
211         GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
212             &masters[c_m].partners[c_s],
213             10000,
214             GNUNET_TIME_UNIT_FOREVER_REL);
215       }
216   }
217
218   timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
219       e->max_duration), &do_shutdown, NULL);
220 }
221
222 static void
223 parse_args (int argc, char *argv[])
224 {
225   int c;
226   opt_exp_file = NULL;
227   opt_log = GNUNET_NO;
228   opt_plot = GNUNET_NO;
229
230   for (c = 0; c < argc; c++)
231   {
232     if ((c < (argc - 1)) && (0 == strcmp (argv[c], "-e")))
233     {
234       opt_exp_file = GNUNET_strdup ( argv[c + 1]);
235     }
236     if (0 == strcmp (argv[c], "-l"))
237     {
238       opt_log = GNUNET_YES;
239     }
240     if (0 == strcmp (argv[c], "-p"))
241     {
242       opt_plot = GNUNET_YES;
243     }
244   }
245 }
246
247 int
248 main (int argc, char *argv[])
249 {
250   GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);
251
252   parse_args (argc, argv);
253   if (NULL == opt_exp_file )
254   {
255     fprintf (stderr, "No experiment given...\n");
256     return 1;
257   }
258
259   fprintf (stderr, "Loading experiment `%s' \n", opt_exp_file );
260   e = GNUNET_ATS_TEST_experimentation_load (opt_exp_file);
261   if (NULL == e)
262   {
263     fprintf (stderr, "Invalid experiment\n");
264     return 1;
265   }
266   if (0 == e->num_episodes)
267   {
268     fprintf (stderr, "No episodes included\n");
269     return 1;
270   }
271
272   /* Setup a topology with */
273   GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
274       e->num_slaves,
275       e->num_masters,
276       GNUNET_NO,
277       &topology_setup_done,
278       NULL,
279       &transport_recv_cb,
280       &log_request__cb);
281   GNUNET_free (opt_exp_file);
282   return 0;
283 }
284 /* end of file gnunet-ats-sim.c */