2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 */
48 case GNUNET_ATS_TEST_TG_CONSTANT:
49 if (UINT32_MAX == tg->base_rate)
50 return GNUNET_TIME_UNIT_ZERO;
51 cur_rate = tg->base_rate;
54 case GNUNET_ATS_TEST_TG_LINEAR:
55 time_delta = GNUNET_TIME_absolute_get_duration (tg->time_start);
56 /* Calculate point of time in the current period */
57 time_delta.rel_value_us = time_delta.rel_value_us
58 % tg->duration_period.rel_value_us;
59 delta_rate = ((double) time_delta.rel_value_us
60 / tg->duration_period.rel_value_us)
61 * (tg->max_rate - tg->base_rate);
62 if ((tg->max_rate < tg->base_rate) && ((tg->max_rate - tg->base_rate) >
65 /* This will cause an underflow */
68 cur_rate = tg->base_rate + delta_rate;
71 case GNUNET_ATS_TEST_TG_RANDOM:
72 cur_rate = tg->base_rate + GNUNET_CRYPTO_random_u32 (
73 GNUNET_CRYPTO_QUALITY_WEAK,
78 case GNUNET_ATS_TEST_TG_SINUS:
79 time_delta = GNUNET_TIME_absolute_get_duration (tg->time_start);
80 /* Calculate point of time in the current period */
81 time_delta.rel_value_us = time_delta.rel_value_us
82 % tg->duration_period.rel_value_us;
83 if ((tg->max_rate - tg->base_rate) > tg->base_rate)
85 /* This will cause an underflow for second half of sinus period,
86 * will be detected in general when experiments are loaded */
89 delta_rate = (tg->max_rate - tg->base_rate)
91 / ((double) tg->duration_period.rel_value_us)
92 * time_delta.rel_value_us);
93 cur_rate = tg->base_rate + delta_rate;
105 /* Calculate the delay for the next message based on the current delay */
106 delay.rel_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us
107 * TEST_MESSAGE_SIZE / cur_rate;
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Current rate is %lld, calculated delay is %llu\n",
112 (unsigned long long) delay.rel_value_us);
118 update_ping_data (void *cls)
120 struct BenchmarkPartner *p = cls;
121 struct GNUNET_TIME_Relative delay;
124 p->bytes_sent += TEST_MESSAGE_SIZE;
125 p->me->total_messages_sent++;
126 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
133 delay = get_delay (p->tg);
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Delay for next transmission %s\n",
136 GNUNET_STRINGS_relative_time_to_string (delay,
138 p->tg->next_ping_transmission
139 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
145 comm_schedule_send (void *cls)
147 struct BenchmarkPartner *p = cls;
148 struct TestMessage *msg;
149 struct GNUNET_MQ_Envelope *env;
151 p->tg->send_task = NULL;
152 p->last_message_sent = GNUNET_TIME_absolute_get ();
153 env = GNUNET_MQ_msg (msg,
154 TEST_MESSAGE_TYPE_PING);
155 memset (msg->padding,
157 sizeof(msg->padding));
158 GNUNET_MQ_notify_sent (env,
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "Master [%u]: Sending PING to [%u]\n",
165 GNUNET_MQ_send (p->mq,
171 update_pong_data (void *cls)
173 struct BenchmarkPartner *p = cls;
176 p->bytes_sent += TEST_MESSAGE_SIZE;
177 p->me->total_messages_sent++;
178 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
183 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p)
185 struct TestMessage *msg;
186 struct GNUNET_MQ_Envelope *env;
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Slave [%u]: Received PING from [%u], sending PONG\n",
192 p->messages_received++;
193 p->bytes_received += TEST_MESSAGE_SIZE;
194 p->me->total_messages_received++;
195 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
198 env = GNUNET_MQ_msg (msg,
199 TEST_MESSAGE_TYPE_PING);
200 memset (msg->padding,
202 sizeof(msg->padding));
203 GNUNET_MQ_notify_sent (env,
206 GNUNET_MQ_send (p->mq,
212 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p)
214 struct GNUNET_TIME_Relative left;
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Master [%u]: Received PONG from [%u], next message\n",
221 p->messages_received++;
222 p->bytes_received += TEST_MESSAGE_SIZE;
223 p->me->total_messages_received++;
224 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
225 p->total_app_rtt += GNUNET_TIME_absolute_get_difference (p->last_message_sent,
226 GNUNET_TIME_absolute_get ())
229 /* Schedule next send event */
233 left = GNUNET_TIME_absolute_get_remaining (p->tg->next_ping_transmission);
234 if (UINT32_MAX == p->tg->base_rate)
236 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
238 else if (0 == left.rel_value_us)
240 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
244 /* Enforce minimum transmission rate 1 msg / sec */
245 if (GNUNET_TIME_UNIT_SECONDS.rel_value_us == (left =
246 GNUNET_TIME_relative_min (
248 GNUNET_TIME_UNIT_SECONDS))
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 "Enforcing minimum send rate between master [%u] and slave [%u]\n",
252 p->me->no, p->dest->no);
253 p->tg->send_task = GNUNET_SCHEDULER_add_delayed (left,
254 &comm_schedule_send, p);
260 * Generate between the source master and the partner and send traffic with a
263 * @param src traffic source
264 * @param dest traffic partner
265 * @param type type of traffic to generate
266 * @param base_rate traffic base rate to send data with
267 * @param max_rate traffic maximum rate to send data with
268 * @param period duration of a period of traffic generation (~ 1/frequency)
269 * @param duration how long to generate traffic
270 * @return the traffic generator
272 struct TrafficGenerator *
273 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
274 struct BenchmarkPartner *dest,
275 enum GeneratorType type,
276 unsigned int base_rate,
277 unsigned int max_rate,
278 struct GNUNET_TIME_Relative period,
279 struct GNUNET_TIME_Relative duration)
281 struct TrafficGenerator *tg;
283 if (NULL != dest->tg)
289 tg = GNUNET_new (struct TrafficGenerator);
290 GNUNET_CONTAINER_DLL_insert (tg_head,
296 tg->base_rate = base_rate;
297 tg->max_rate = max_rate;
298 tg->duration_period = period;
299 tg->time_start = GNUNET_TIME_absolute_get ();
300 tg->next_ping_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
304 case GNUNET_ATS_TEST_TG_CONSTANT:
305 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
306 "Setting up constant traffic generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
308 GNUNET_i2s (&dest->me->id),
310 GNUNET_i2s (&dest->dest->id),
314 case GNUNET_ATS_TEST_TG_LINEAR:
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 "Setting up linear traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bips\n",
318 GNUNET_i2s (&dest->me->id),
320 GNUNET_i2s (&dest->dest->id),
325 case GNUNET_ATS_TEST_TG_SINUS:
326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
327 "Setting up sinus traffic generator master[%u] `%s' and slave [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
329 GNUNET_i2s (&dest->me->id),
331 GNUNET_i2s (&dest->dest->id),
336 case GNUNET_ATS_TEST_TG_RANDOM:
337 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
338 "Setting up random traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bps\n",
340 GNUNET_i2s (&dest->me->id),
342 GNUNET_i2s (&dest->dest->id),
353 = GNUNET_SCHEDULER_add_now (&comm_schedule_send,
360 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg)
362 GNUNET_CONTAINER_DLL_remove (tg_head,
366 if (NULL != tg->send_task)
368 GNUNET_SCHEDULER_cancel (tg->send_task);
369 tg->send_task = NULL;
376 * Stop all traffic generators
379 GNUNET_ATS_TEST_generate_traffic_stop_all ()
381 struct TrafficGenerator *cur;
382 struct TrafficGenerator *next;
385 for (cur = next; NULL != cur; cur = next)
388 GNUNET_ATS_TEST_generate_traffic_stop (cur);
392 /* end of file ats-testing-traffic.c */