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/>.
19 * @file ats-tests/ats-testing-traffic.c
20 * @brief ats benchmark: traffic generator
21 * @author Christian Grothoff
22 * @author Matthias Wachs
25 #include "gnunet_util_lib.h"
26 #include "ats-testing.h"
28 static struct TrafficGenerator *tg_head;
29 static struct TrafficGenerator *tg_tail;
31 extern struct GNUNET_ATS_TEST_Topology *top;
33 static struct GNUNET_TIME_Relative
34 get_delay (struct TrafficGenerator *tg)
36 struct GNUNET_TIME_Relative delay;
37 struct GNUNET_TIME_Relative time_delta;
38 long long int cur_rate;
39 long long int delta_rate;
41 delay.rel_value_us = 0;
43 /* Calculate the current transmission rate based on the type of traffic */
45 case GNUNET_ATS_TEST_TG_CONSTANT:
46 if (UINT32_MAX == tg->base_rate)
47 return GNUNET_TIME_UNIT_ZERO;
48 cur_rate = tg->base_rate;
50 case GNUNET_ATS_TEST_TG_LINEAR:
51 time_delta = GNUNET_TIME_absolute_get_duration(tg->time_start);
52 /* Calculate point of time in the current period */
53 time_delta.rel_value_us = time_delta.rel_value_us % tg->duration_period.rel_value_us;
54 delta_rate = ((double) time_delta.rel_value_us / tg->duration_period.rel_value_us) *
55 (tg->max_rate - tg->base_rate);
56 if ((tg->max_rate < tg->base_rate) && ((tg->max_rate - tg->base_rate) > tg->base_rate))
58 /* This will cause an underflow */
61 cur_rate = tg->base_rate + delta_rate;
63 case GNUNET_ATS_TEST_TG_RANDOM:
64 cur_rate = tg->base_rate + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
65 tg->max_rate - tg->base_rate);
67 case GNUNET_ATS_TEST_TG_SINUS:
68 time_delta = GNUNET_TIME_absolute_get_duration(tg->time_start);
69 /* Calculate point of time in the current period */
70 time_delta.rel_value_us = time_delta.rel_value_us % tg->duration_period.rel_value_us;
71 if ((tg->max_rate - tg->base_rate) > tg->base_rate)
73 /* This will cause an underflow for second half of sinus period,
74 * will be detected in general when experiments are loaded */
77 delta_rate = (tg->max_rate - tg->base_rate) *
78 sin ( (2 * M_PI) / ((double) tg->duration_period.rel_value_us) * time_delta.rel_value_us);
79 cur_rate = tg->base_rate + delta_rate;
90 /* Calculate the delay for the next message based on the current delay */
91 delay.rel_value_us = GNUNET_TIME_UNIT_SECONDS.rel_value_us * TEST_MESSAGE_SIZE / cur_rate;
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94 "Current rate is %lld, calculated delay is %llu\n",
96 (unsigned long long) delay.rel_value_us);
102 update_ping_data (void *cls)
104 struct BenchmarkPartner *p = cls;
105 struct GNUNET_TIME_Relative delay;
108 p->bytes_sent += TEST_MESSAGE_SIZE;
109 p->me->total_messages_sent++;
110 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
117 delay = get_delay (p->tg);
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Delay for next transmission %s\n",
120 GNUNET_STRINGS_relative_time_to_string (delay,
122 p->tg->next_ping_transmission
123 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
129 comm_schedule_send (void *cls)
131 struct BenchmarkPartner *p = cls;
132 struct TestMessage *msg;
133 struct GNUNET_MQ_Envelope *env;
135 p->tg->send_task = NULL;
136 p->last_message_sent = GNUNET_TIME_absolute_get();
137 env = GNUNET_MQ_msg (msg,
138 TEST_MESSAGE_TYPE_PING);
139 memset (msg->padding,
141 sizeof (msg->padding));
142 GNUNET_MQ_notify_sent (env,
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Master [%u]: Sending PING to [%u]\n",
149 GNUNET_MQ_send (p->mq,
155 update_pong_data (void *cls)
157 struct BenchmarkPartner *p = cls;
160 p->bytes_sent += TEST_MESSAGE_SIZE;
161 p->me->total_messages_sent++;
162 p->me->total_bytes_sent += TEST_MESSAGE_SIZE;
167 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p)
169 struct TestMessage *msg;
170 struct GNUNET_MQ_Envelope *env;
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Slave [%u]: Received PING from [%u], sending PONG\n",
176 p->messages_received++;
177 p->bytes_received += TEST_MESSAGE_SIZE;
178 p->me->total_messages_received++;
179 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
182 env = GNUNET_MQ_msg (msg,
183 TEST_MESSAGE_TYPE_PING);
184 memset (msg->padding,
186 sizeof (msg->padding));
187 GNUNET_MQ_notify_sent (env,
190 GNUNET_MQ_send (p->mq,
196 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p)
198 struct GNUNET_TIME_Relative left;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Master [%u]: Received PONG from [%u], next message\n",
204 p->messages_received++;
205 p->bytes_received += TEST_MESSAGE_SIZE;
206 p->me->total_messages_received++;
207 p->me->total_bytes_received += TEST_MESSAGE_SIZE;
208 p->total_app_rtt += GNUNET_TIME_absolute_get_difference(p->last_message_sent,
209 GNUNET_TIME_absolute_get()).rel_value_us;
211 /* Schedule next send event */
215 left = GNUNET_TIME_absolute_get_remaining(p->tg->next_ping_transmission);
216 if (UINT32_MAX == p->tg->base_rate)
218 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
220 else if (0 == left.rel_value_us)
222 p->tg->send_task = GNUNET_SCHEDULER_add_now (&comm_schedule_send, p);
226 /* Enforce minimum transmission rate 1 msg / sec */
227 if (GNUNET_TIME_UNIT_SECONDS.rel_value_us == (left = GNUNET_TIME_relative_min (left, GNUNET_TIME_UNIT_SECONDS)).rel_value_us)
228 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
229 "Enforcing minimum send rate between master [%u] and slave [%u]\n",
230 p->me->no, p->dest->no);
231 p->tg->send_task = GNUNET_SCHEDULER_add_delayed (left,
232 &comm_schedule_send, p);
238 * Generate between the source master and the partner and send traffic with a
241 * @param src traffic source
242 * @param dest traffic partner
243 * @param type type of traffic to generate
244 * @param base_rate traffic base rate to send data with
245 * @param max_rate traffic maximum rate to send data with
246 * @param period duration of a period of traffic generation (~ 1/frequency)
247 * @param duration how long to generate traffic
248 * @return the traffic generator
250 struct TrafficGenerator *
251 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
252 struct BenchmarkPartner *dest,
253 enum GeneratorType type,
254 unsigned int base_rate,
255 unsigned int max_rate,
256 struct GNUNET_TIME_Relative period,
257 struct GNUNET_TIME_Relative duration)
259 struct TrafficGenerator *tg;
261 if (NULL != dest->tg)
267 tg = GNUNET_new (struct TrafficGenerator);
268 GNUNET_CONTAINER_DLL_insert (tg_head,
274 tg->base_rate = base_rate;
275 tg->max_rate = max_rate;
276 tg->duration_period = period;
277 tg->time_start = GNUNET_TIME_absolute_get();
278 tg->next_ping_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
281 case GNUNET_ATS_TEST_TG_CONSTANT:
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283 "Setting up constant traffic generator master[%u] `%s' and slave [%u] `%s' max %u Bips\n",
285 GNUNET_i2s (&dest->me->id),
287 GNUNET_i2s (&dest->dest->id),
290 case GNUNET_ATS_TEST_TG_LINEAR:
291 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
292 "Setting up linear traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bips\n",
294 GNUNET_i2s (&dest->me->id),
296 GNUNET_i2s (&dest->dest->id),
300 case GNUNET_ATS_TEST_TG_SINUS:
301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
302 "Setting up sinus traffic generator master[%u] `%s' and slave [%u] `%s' baserate %u Bips, amplitude %u Bps\n",
304 GNUNET_i2s (&dest->me->id),
306 GNUNET_i2s (&dest->dest->id),
310 case GNUNET_ATS_TEST_TG_RANDOM:
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
312 "Setting up random traffic generator master[%u] `%s' and slave [%u] `%s' min %u Bips max %u Bps\n",
314 GNUNET_i2s (&dest->me->id),
316 GNUNET_i2s (&dest->dest->id),
326 = GNUNET_SCHEDULER_add_now (&comm_schedule_send,
333 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg)
335 GNUNET_CONTAINER_DLL_remove (tg_head,
339 if (NULL != tg->send_task)
341 GNUNET_SCHEDULER_cancel (tg->send_task);
342 tg->send_task = NULL;
349 * Stop all traffic generators
352 GNUNET_ATS_TEST_generate_traffic_stop_all ()
354 struct TrafficGenerator *cur;
355 struct TrafficGenerator *next;
358 for (cur = next; NULL != cur; cur = next)
361 GNUNET_ATS_TEST_generate_traffic_stop(cur);
365 /* end of file ats-testing-traffic.c */