2 This file is part of GNUnet.
3 Copyright (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 struct GNUNET_TIME_Relative time_delta;
40 long long int cur_rate;
41 long long int delta_rate;
43 delay.rel_value_us = 0;
45 /* Calculate the current transmission rate based on the type of traffic */
47 case GNUNET_ATS_TEST_TG_CONSTANT:
48 if (UINT32_MAX == tg->base_rate)
49 return GNUNET_TIME_UNIT_ZERO;
50 cur_rate = tg->base_rate;
52 case GNUNET_ATS_TEST_TG_LINEAR:
53 time_delta = GNUNET_TIME_absolute_get_duration(tg->time_start);
54 /* Calculate point of time in the current period */
55 time_delta.rel_value_us = time_delta.rel_value_us % tg->duration_period.rel_value_us;
56 delta_rate = ((double) time_delta.rel_value_us / tg->duration_period.rel_value_us) *
57 (tg->max_rate - tg->base_rate);
58 if ((tg->max_rate < tg->base_rate) && ((tg->max_rate - tg->base_rate) > tg->base_rate))
60 /* This will cause an underflow */
63 cur_rate = tg->base_rate + delta_rate;
65 case GNUNET_ATS_TEST_TG_RANDOM:
66 cur_rate = tg->base_rate + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
67 tg->max_rate - tg->base_rate);
69 case GNUNET_ATS_TEST_TG_SINUS:
70 time_delta = GNUNET_TIME_absolute_get_duration(tg->time_start);
71 /* Calculate point of time in the current period */
72 time_delta.rel_value_us = time_delta.rel_value_us % tg->duration_period.rel_value_us;
73 if ((tg->max_rate - tg->base_rate) > tg->base_rate)
75 /* This will cause an underflow for second half of sinus period,
76 * will be detected in general when experiments are loaded */
79 delta_rate = (tg->max_rate - tg->base_rate) *
80 sin ( (2 * M_PI) / ((double) tg->duration_period.rel_value_us) * time_delta.rel_value_us);
81 cur_rate = tg->base_rate + delta_rate;
92 /* Calculate the delay for the next message based on the current delay */
93 delay.rel_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us * TEST_MESSAGE_SIZE / cur_rate;
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Current rate is %u, calculated delay is %u \n",
97 cur_rate, delay.rel_value_us);
103 send_ping_ready_cb (void *cls, size_t size, void *buf)
105 struct BenchmarkPartner *p = cls;
106 static char msgbuf[TEST_MESSAGE_SIZE];
107 struct GNUNET_MessageHeader *msg;
108 struct GNUNET_TIME_Relative delay;
115 if (size < TEST_MESSAGE_SIZE)
121 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
122 "Master [%u]: Sending PING to [%u]\n",
123 p->me->no, p->dest->no);
141 msg = (struct GNUNET_MessageHeader *) &msgbuf;
142 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
143 msg->type = htons (TEST_MESSAGE_TYPE_PING);
144 msg->size = htons (TEST_MESSAGE_SIZE);
145 memcpy (buf, msg, TEST_MESSAGE_SIZE);
148 p->bytes_sent += TEST_MESSAGE_SIZE;
149 p->me->total_messages_sent++;
150 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
155 return TEST_MESSAGE_SIZE;
157 delay = get_delay (p->tg);
159 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Delay for next transmission %llu ms\n",
160 (long long unsigned int) delay.rel_value_us / 1000);
161 p->tg->next_ping_transmission = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
164 return TEST_MESSAGE_SIZE;
169 comm_schedule_send (void *cls,
170 const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct BenchmarkPartner *p = cls;
174 p->tg->send_task = NULL;
175 p->last_message_sent = GNUNET_TIME_absolute_get();
176 if (GNUNET_YES == top->test_core)
178 p->cth = GNUNET_CORE_notify_transmit_ready (p->me->ch, GNUNET_NO,
179 GNUNET_CORE_PRIO_BEST_EFFORT,
180 GNUNET_TIME_UNIT_MINUTES,
183 &send_ping_ready_cb, p);
187 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (p->me->th,
190 GNUNET_TIME_UNIT_MINUTES,
191 &send_ping_ready_cb, p);
197 comm_send_pong_ready (void *cls, size_t size, void *buf)
199 static char msgbuf[TEST_MESSAGE_SIZE];
200 struct BenchmarkPartner *p = cls;
201 struct GNUNET_MessageHeader *msg;
203 if (GNUNET_YES == top->test_core)
209 p->bytes_sent += TEST_MESSAGE_SIZE;
210 p->me->total_messages_sent++;
211 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
213 msg = (struct GNUNET_MessageHeader *) &msgbuf;
214 memset (&msgbuf, 'a', TEST_MESSAGE_SIZE);
215 msg->type = htons (TEST_MESSAGE_TYPE_PONG);
216 msg->size = htons (TEST_MESSAGE_SIZE);
217 memcpy (buf, msg, TEST_MESSAGE_SIZE);
219 return TEST_MESSAGE_SIZE;
224 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p)
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Slave [%u]: Received PING from [%u], sending PONG\n", p->me->no,
230 p->messages_received++;
231 p->bytes_received += TEST_MESSAGE_SIZE;
232 p->me->total_messages_received++;
233 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
235 if (GNUNET_YES == top->test_core)
237 GNUNET_assert (NULL == p->cth);
240 = GNUNET_CORE_notify_transmit_ready (p->me->ch, GNUNET_NO,
241 GNUNET_CORE_PRIO_BEST_EFFORT,
242 GNUNET_TIME_UNIT_MINUTES,
243 &p->dest->id, TEST_MESSAGE_SIZE,
244 &comm_send_pong_ready, p);
248 GNUNET_assert (NULL == p->tth);
249 p->tth = GNUNET_TRANSPORT_notify_transmit_ready (p->me->th, &p->dest->id,
250 TEST_MESSAGE_SIZE, GNUNET_TIME_UNIT_MINUTES, &comm_send_pong_ready,
257 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p)
259 struct GNUNET_TIME_Relative left;
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Master [%u]: Received PONG from [%u], next message\n", p->me->no,
264 p->messages_received++;
265 p->bytes_received += TEST_MESSAGE_SIZE;
266 p->me->total_messages_received++;
267 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
268 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
269 GNUNET_TIME_absolute_get()).rel_value_us;
271 /* Schedule next send event */
275 left = GNUNET_TIME_absolute_get_remaining(p->tg->next_ping_transmission);
276 if (UINT32_MAX == p->tg->base_rate)
278 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
280 else if (0 == left.rel_value_us)
282 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
286 /* Enforce minimum transmission rate 1 msg / sec */
287 if (GNUNET_TIME_UNIT_SECONDS.rel_value_us == (left = GNUNET_TIME_relative_min (left, GNUNET_TIME_UNIT_SECONDS)).rel_value_us)
288 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
289 "Enforcing minimum send rate between master [%u] and slave [%u]\n",
290 p->me->no, p->dest->no);
291 p->tg->send_task = GNUNET_SCHEDULER_add_delayed (left,
292 &comm_schedule_send, p);
298 * Generate between the source master and the partner and send traffic with a
301 * @param src traffic source
302 * @param dest traffic partner
303 * @param type type of traffic to generate
304 * @param base_rate traffic base rate to send data with
305 * @param max_rate traffic maximum rate to send data with
306 * @param period duration of a period of traffic generation (~ 1/frequency)
307 * @param duration how long to generate traffic
308 * @return the traffic generator
310 struct TrafficGenerator *
311 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
312 struct BenchmarkPartner *dest,
313 enum GeneratorType type,
316 struct GNUNET_TIME_Relative period,
317 struct GNUNET_TIME_Relative duration)
319 struct TrafficGenerator *tg;
321 if (NULL != dest->tg)
327 tg = GNUNET_new (struct TrafficGenerator);
328 GNUNET_CONTAINER_DLL_insert (tg_head, tg_tail, tg);
332 tg->base_rate = base_rate;
333 tg->max_rate = max_rate;
334 tg->duration_period = period;
335 tg->time_start = GNUNET_TIME_absolute_get();
336 tg->next_ping_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
339 case GNUNET_ATS_TEST_TG_CONSTANT:
340 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
341 "Setting up constant traffic generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
342 dest->me->no, GNUNET_i2s (&dest->me->id),
343 dest->dest->no, GNUNET_i2s (&dest->dest->id),
346 case GNUNET_ATS_TEST_TG_LINEAR:
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348 "Setting up linear traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bips\n",
349 dest->me->no, GNUNET_i2s (&dest->me->id),
350 dest->dest->no, GNUNET_i2s (&dest->dest->id),
351 base_rate, max_rate);
353 case GNUNET_ATS_TEST_TG_SINUS:
354 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
355 "Setting up sinus traffic generator master[%u] `%s' and slave [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
356 dest->me->no, GNUNET_i2s (&dest->me->id),
357 dest->dest->no, GNUNET_i2s (&dest->dest->id),
358 base_rate, max_rate);
360 case GNUNET_ATS_TEST_TG_RANDOM:
361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
362 "Setting up random traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bps\n",
363 dest->me->no, GNUNET_i2s (&dest->me->id),
364 dest->dest->no, GNUNET_i2s (&dest->dest->id),
365 base_rate, max_rate);
371 if ( ((GNUNET_YES == top->test_core) && (NULL != dest->cth)) ||
372 ((GNUNET_NO == top->test_core) && (NULL != dest->tth)) )
375 GNUNET_CONTAINER_DLL_remove (tg_head, tg_tail, tg);
381 tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, dest);
387 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg)
389 GNUNET_CONTAINER_DLL_remove (tg_head, tg_tail, tg);
392 if (NULL != tg->send_task)
394 GNUNET_SCHEDULER_cancel (tg->send_task);
395 tg->send_task = NULL;
399 if (NULL != tg->dest->cth)
401 GNUNET_CORE_notify_transmit_ready_cancel (tg->dest->cth);
402 tg->dest->cth = NULL;
407 if (NULL != tg->dest->tth)
409 GNUNET_TRANSPORT_notify_transmit_ready_cancel (tg->dest->tth);
410 tg->dest->tth = NULL;
418 * Stop all traffic generators
421 GNUNET_ATS_TEST_generate_traffic_stop_all ()
423 struct TrafficGenerator *cur;
424 struct TrafficGenerator *next;
426 for (cur = next; NULL != cur; cur = next)
429 GNUNET_ATS_TEST_generate_traffic_stop(cur);
433 /* end of file ats-testing-traffic.c */