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/ats-testing-traffic.c
22 * @brief ats benchmark: traffic generator
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "ats-testing.h"
30 static struct TrafficGenerator *tg_head;
31 static struct TrafficGenerator *tg_tail;
33 extern struct GNUNET_ATS_TEST_Topology *top;
35 static struct GNUNET_TIME_Relative
36 get_delay (struct TrafficGenerator *tg)
38 struct GNUNET_TIME_Relative delay;
39 delay.rel_value_us = 0;
42 case GNUNET_ATS_TEST_TG_CONSTANT:
43 if (UINT32_MAX == tg->rate)
44 delay.rel_value_us = 0;
45 else if (tg->rate <= TEST_MESSAGE_SIZE)
46 delay.rel_value_us = (GNUNET_TIME_UNIT_SECONDS.rel_value_us);
48 delay.rel_value_us = (GNUNET_TIME_UNIT_SECONDS.rel_value_us / (tg->rate / TEST_MESSAGE_SIZE));
58 send_ping_ready_cb (void *cls, size_t size, void *buf)
60 struct BenchmarkPartner *p = cls;
61 static char msgbuf[TEST_MESSAGE_SIZE];
62 struct GNUNET_MessageHeader *msg;
69 if (size < TEST_MESSAGE_SIZE)
75 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Master [%u]: Sending PING to [%u]\n",
76 p->me->no, p->dest->no);
95 msg = (struct GNUNET_MessageHeader *) &msgbuf;
96 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
97 msg->type = htons (TEST_MESSAGE_TYPE_PING);
98 msg->size = htons (TEST_MESSAGE_SIZE);
99 memcpy (buf, msg, TEST_MESSAGE_SIZE);
102 p->bytes_sent += TEST_MESSAGE_SIZE;
103 p->me->total_messages_sent++;
104 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
109 return TEST_MESSAGE_SIZE;
111 p->tg->next_ping_transmission = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
114 return TEST_MESSAGE_SIZE;
119 comm_schedule_send (void *cls,
120 const struct GNUNET_SCHEDULER_TaskContext* tc)
122 struct BenchmarkPartner *p = cls;
124 p->tg->send_task = GNUNET_SCHEDULER_NO_TASK;
126 p->last_message_sent = GNUNET_TIME_absolute_get();
127 if (GNUNET_YES == top->test_core)
129 p->cth = GNUNET_CORE_notify_transmit_ready (
130 p->me->ch, GNUNET_NO,
131 GNUNET_CORE_PRIO_BEST_EFFORT,
132 GNUNET_TIME_UNIT_MINUTES, &p->dest->id,
133 TEST_MESSAGE_SIZE, &send_ping_ready_cb, p);
137 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (
138 p->me->th, &p->dest->id, TEST_MESSAGE_SIZE, 0,GNUNET_TIME_UNIT_MINUTES,
139 &send_ping_ready_cb, p);
145 comm_send_pong_ready (void *cls, size_t size, void *buf)
147 static char msgbuf[TEST_MESSAGE_SIZE];
148 struct BenchmarkPartner *p = cls;
149 struct GNUNET_MessageHeader *msg;
151 if (GNUNET_YES == top->test_core)
157 p->bytes_sent += TEST_MESSAGE_SIZE;
158 p->me->total_messages_sent++;
159 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
161 msg = (struct GNUNET_MessageHeader *) &msgbuf;
162 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
163 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
164 msg->size = htons (TEST_MESSAGE_SIZE);
165 memcpy (buf, msg, TEST_MESSAGE_SIZE);
167 return TEST_MESSAGE_SIZE;
172 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p)
174 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
175 "Slave [%u]: Received PING from [%u], sending PONG\n", p->me->no,
178 p->messages_received++;
179 p->bytes_received += TEST_MESSAGE_SIZE;
180 p->me->total_messages_received++;
181 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
183 if (GNUNET_YES == top->test_core)
185 GNUNET_assert (NULL == p->cth);
188 = GNUNET_CORE_notify_transmit_ready (p->me->ch, GNUNET_NO,
189 GNUNET_CORE_PRIO_BEST_EFFORT,
190 GNUNET_TIME_UNIT_MINUTES,
191 &p->dest->id, TEST_MESSAGE_SIZE,
192 &comm_send_pong_ready, p);
196 GNUNET_assert (NULL == p->tth);
197 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (p->me->th, &p->dest->id,
198 TEST_MESSAGE_SIZE, 0, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
204 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p)
206 struct GNUNET_TIME_Relative left;
207 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
208 "Master [%u]: Received PONG from [%u], next message\n", p->me->no,
211 p->messages_received++;
212 p->bytes_received += TEST_MESSAGE_SIZE;
213 p->me->total_messages_received++;
214 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
215 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
216 GNUNET_TIME_absolute_get()).rel_value_us;
218 /* Schedule next send event */
219 left = GNUNET_TIME_absolute_get_remaining(p->tg->next_ping_transmission);
220 if (UINT32_MAX == p->tg->rate)
222 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
224 else if (0 == left.rel_value_us)
226 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
230 p->tg->send_task = GNUNET_SCHEDULER_add_delayed (left,
231 &comm_schedule_send, p);
237 * Generate between the source master and the partner and send traffic with a
241 struct TrafficGenerator *
242 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
243 struct BenchmarkPartner *dest,
244 enum TrafficGeneratorType type,
246 struct GNUNET_TIME_Relative duration)
248 struct TrafficGenerator * tg;
251 if (NULL != dest->tg)
257 tg = GNUNET_new (struct TrafficGenerator);
258 GNUNET_CONTAINER_DLL_insert (tg_head, tg_tail, tg);
263 tg->next_ping_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
265 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
266 "Setting up traffic generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
267 dest->me->no, GNUNET_i2s (&dest->me->id),
268 dest->dest->no, GNUNET_i2s (&dest->dest->id),
271 if ( ((GNUNET_YES == top->test_core) && (NULL != dest->cth)) ||
272 ((GNUNET_NO == top->test_core) && (NULL != dest->tth)) )
275 GNUNET_CONTAINER_DLL_remove (tg_head, tg_tail, tg);
281 tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, dest);
286 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg)
289 GNUNET_CONTAINER_DLL_remove (tg_head, tg_tail, tg);
292 if (GNUNET_SCHEDULER_NO_TASK != tg->send_task)
294 GNUNET_SCHEDULER_cancel (tg->send_task);
295 tg->send_task = GNUNET_SCHEDULER_NO_TASK;
299 if (NULL != tg->dest->cth)
301 GNUNET_CORE_notify_transmit_ready_cancel (tg->dest->cth);
302 tg->dest->cth = NULL;
307 if (NULL != tg->dest->tth)
309 GNUNET_TRANSPORT_notify_transmit_ready_cancel (tg->dest->tth);
310 tg->dest->tth = NULL;
317 * Stop all traffic generators
320 GNUNET_ATS_TEST_generate_traffic_stop_all ()
322 struct TrafficGenerator *cur;
323 struct TrafficGenerator *next;
325 for (cur = next; NULL != cur; cur = next)
328 GNUNET_ATS_TEST_generate_traffic_stop(cur);
332 /* end of file ats-testing-traffic.c */