new logging functionality
[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-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
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 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
41
42 struct Experiment *e;
43 struct LoggingHandle *l;
44
45 static void
46 evaluate ()
47 {
48   int c_m;
49   int c_s;
50   unsigned int duration;
51   struct BenchmarkPeer *mp;
52   struct BenchmarkPartner *p;
53
54   unsigned int kb_sent_sec;
55   double kb_sent_percent;
56   unsigned int kb_recv_sec;
57   double kb_recv_percent;
58   unsigned int rtt;
59
60   duration = (TEST_TIMEOUT.rel_value_us / (1000 * 1000));
61   for (c_m = 0; c_m < e->num_masters; c_m++)
62   {
63     mp = &masters_p[c_m];
64     fprintf (stderr,
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);
70
71     for (c_s = 0; c_s < e->num_slaves; c_s++)
72     {
73       p = &mp->partners[c_s];
74
75       kb_sent_sec = 0;
76       kb_recv_sec = 0;
77       kb_sent_percent = 0.0;
78       kb_recv_percent = 0.0;
79       rtt = 0;
80
81       if (duration > 0)
82       {
83           kb_sent_sec = (p->bytes_sent / 1024) / duration;
84           kb_recv_sec = (p->bytes_received / 1024) / duration;
85       }
86
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);
93       fprintf (stderr,
94           "%c Master [%u] -> Slave [%u]: sent %u KiB/s (%.2f %%), received %u KiB/s (%.2f %%)\n",
95           (mp->pref_partner == p->dest) ? '*' : ' ',
96           mp->no, p->dest->no,
97           kb_sent_sec, kb_sent_percent,
98                   kb_recv_sec, kb_recv_percent);
99       fprintf (stderr,
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);
103     }
104   }
105 }
106
107 static void
108 do_shutdown ()
109 {
110   /* timeout */
111   if (NULL != e)
112   {
113     GNUNET_ATS_TEST_experimentation_stop (e);
114     e = NULL;
115   }
116   GNUNET_ATS_TEST_shutdown_topology ();
117 }
118
119 static void
120 transport_recv_cb (void *cls,
121                    const struct GNUNET_PeerIdentity * peer,
122                    const struct GNUNET_MessageHeader * message)
123 {
124
125 }
126
127 static void
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)
132 {
133   if (NULL != l)
134     GNUNET_ATS_TEST_logging_now (l);
135 }
136
137 static void
138 experiment_done_cb (struct Experiment *e, int success)
139 {
140   if (GNUNET_OK == success)
141     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment `%s' done successful\n", e->name);
142   else
143     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Experiment `%s' failed \n", e->name);
144   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
145   {
146     GNUNET_SCHEDULER_cancel (timeout_task);
147     timeout_task = GNUNET_SCHEDULER_NO_TASK;
148   }
149   /* Stop logging */
150   GNUNET_ATS_TEST_logging_stop (l);
151   evaluate ();
152
153   /* Stop traffic generation */
154   GNUNET_ATS_TEST_generate_traffic_stop_all();
155   /* Clean up experiment */
156   GNUNET_ATS_TEST_experimentation_stop (e);
157   e = NULL;
158
159   /* Shutdown topology */
160   GNUNET_ATS_TEST_shutdown_topology ();
161 }
162
163 static void
164 episode_done_cb (struct Episode *ep)
165 {
166   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Episode %u done\n", ep->id);
167 }
168
169 static void topology_setup_done (void *cls,
170     struct BenchmarkPeer *masters,
171     struct BenchmarkPeer *slaves)
172 {
173   int c_m;
174   int c_s;
175   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Topology setup complete!\n");
176
177   masters_p = masters;
178   slaves_p = slaves;
179
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);
183
184   for (c_m = 0; c_m < e->num_masters; c_m++)
185   {
186       for (c_s = 0; c_s < e->num_slaves; c_s++)
187       {
188         /* Generate maximum traffic to all peers */
189         GNUNET_ATS_TEST_generate_traffic_start (&masters[c_m],
190             &masters[c_m].partners[c_s],
191             10000,
192             GNUNET_TIME_UNIT_FOREVER_REL);
193       }
194   }
195
196   timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add (GNUNET_TIME_UNIT_MINUTES,
197       e->max_duration), &do_shutdown, NULL);
198 }
199
200
201 int
202 main (int argc, char *argv[])
203 {
204   GNUNET_log_setup("gnunet-ats-sim", "INFO", NULL);
205   if (argc < 2)
206   {
207     fprintf (stderr, "No experiment given...\n");
208     return 1;
209   }
210
211   fprintf (stderr, "Loading experiment `%s' \n", argv[1]);
212   e = GNUNET_ATS_TEST_experimentation_load (argv[1]);
213   if (NULL == e)
214   {
215     fprintf (stderr, "Invalid experiment\n");
216     return 1;
217   }
218   if (0 == e->num_episodes)
219   {
220     fprintf (stderr, "No episodes included\n");
221     return 1;
222   }
223
224   /* Setup a topology with */
225   GNUNET_ATS_TEST_create_topology ("gnunet-ats-sim", e->cfg_file,
226       e->num_slaves,
227       e->num_masters,
228       GNUNET_NO,
229       &topology_setup_done,
230       NULL,
231       &transport_recv_cb,
232       &log_request__cb);
233   return 0;
234 }
235 /* end of file gnunet-ats-sim.c */