The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
load.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 41c9f9b32174ba17101812477a86fbb1ad6766f8 $
19 *
20 * @brief Load generation algorithms
21 * @file io/load.c
22 *
23 * @copyright 2019 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: 41c9f9b32174ba17101812477a86fbb1ad6766f8 $")
26
27#include <freeradius-devel/io/load.h>
28
35
36struct fr_load_s {
42 void *uctx;
43
44 fr_load_stats_t stats; //!< sending statistics
45 fr_time_t step_start; //!< when the current step started
46 fr_time_t step_end; //!< when the current step will end
48 int sent_base; //!< stats.sent when this run started, so that
49 ///< max_requests counts per-run, not cumulatively
50
52 fr_time_delta_t delta; //!< between packets
53
55 bool header; //!< for printing statistics
56
57 fr_time_t next; //!< The next time we're supposed to send a packet
59};
60
63 void *uctx)
64{
65 fr_load_t *l;
66
67 l = talloc_zero(ctx, fr_load_t);
68 if (!l) return NULL;
69
70 if (!config->start_pps) config->start_pps = 1;
71 if (!config->milliseconds) config->milliseconds = 1000;
72 if (!config->parallel) config->parallel = 1;
73
74 l->el = el;
75 l->config = config;
76 l->callback = callback;
77 l->done = done;
78 l->uctx = uctx;
79
80 return l;
81}
82
83/** Send one or more packets.
84 *
85 */
87{
88 int i;
89
90 /*
91 * Send as many packets as necessary.
92 */
93 l->stats.sent += count;
94 l->stats.last_send = now;
95
96 /*
97 * Run the callback AFTER we set the timer. Which makes
98 * it more likely that the next timer fires on time.
99 */
100 for (i = 0; i < count; i++) {
102 }
103}
104
105/** Stop sending new packets, and wait for outstanding replies.
106 *
107 * If every reply has already arrived then the test is complete right
108 * now, so run the "done" callback. Otherwise completion is signalled
109 * by fr_load_generator_have_reply() when the last reply comes in.
110 */
111static void load_drain(fr_load_t *l, fr_time_t now)
112{
114
115 if (l->stats.received >= l->stats.sent) {
116 l->stats.end = now;
117 if (l->done) l->done(l->uctx);
118 }
119}
120
121static void load_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
122{
123 fr_load_t *l = uctx;
124 fr_time_delta_t delta;
126
127 /*
128 * Keep track of the overall maximum backlog for the
129 * duration of the entire test run.
130 */
131 l->stats.backlog = l->stats.sent - l->stats.received;
133
134 /*
135 * If we're done this step, go to the next one.
136 */
137 if (fr_time_gteq(l->next, l->step_end)) {
138 l->step_start = l->next;
141 l->pps += l->config->step;
142 if (l->pps > (UINT32_MAX / l->config->milliseconds)) l->pps = UINT32_MAX / l->config->milliseconds;
143
144 /*
145 * The ramp-up has passed max_pps. If there's no
146 * reason to keep sending, stop and drain.
147 * Otherwise hold the rate at max_pps, until
148 * max_requests packets have been sent (checked
149 * below), or forever for "unlimited".
150 */
151 if (l->config->max_pps && (l->pps > l->config->max_pps)) {
152 if (!l->config->max_requests && !l->config->unlimited) {
153 load_drain(l, now);
154 return;
155 }
156
157 l->pps = l->config->max_pps;
158 }
159
160 l->stats.pps = l->pps;
161 l->stats.skipped = 0;
163 }
164
165 /*
166 * We don't have "pps" packets in the backlog, go send
167 * some more. We scale the backlog by 1000 milliseconds
168 * per second. Then, multiple the PPS by the number of
169 * milliseconds of backlog we want to keep.
170 *
171 * If the backlog is smaller than packets/s *
172 * milliseconds of backlog, then keep sending.
173 * Otherwise, switch to a gated mode where we only send
174 * new packets once a reply comes in.
175 */
176 if (((size_t) l->stats.backlog * 1000) < ((size_t) l->pps * l->config->milliseconds)) {
177 uint32_t capacity;
178
180 l->stats.blocked = false;
181 count = l->config->parallel;
182 l->stats.skipped = 0;
183
184 capacity = ((l->pps * l->config->milliseconds) / 1000) - l->stats.backlog;
185
186 /*
187 * Limit "count" so that it doesn't overflow.
188 */
189 if (count > capacity) count = capacity;
190
191 } else {
192
193 /*
194 * We have too many packets in the backlog, we're
195 * gated. Don't send more packets until we have
196 * a reply.
197 *
198 * Note that we will send *these* packets.
199 */
201 l->stats.blocked = true;
202 count = 0;
203 l->stats.skipped += l->count;
204 }
205
206 /*
207 * Stop after max_requests packets, if it's set. Packets
208 * sent by a previous run of the generator don't count.
209 */
210 if (l->config->max_requests) {
211 uint32_t sent = (uint32_t) (l->stats.sent - l->sent_base);
212
213 if (sent >= l->config->max_requests) {
214 load_drain(l, now);
215 return;
216 }
217
218 if (count > (l->config->max_requests - sent)) count = l->config->max_requests - sent;
219 }
220
221 /*
222 * Skip timers if we're too busy.
223 */
224 l->next = fr_time_add(l->next, l->delta);
225 if (fr_time_lt(l->next, now)) {
226 while (fr_time_lt(l->next, now)) {
227// l->stats.skipped += l->count;
228 l->next = fr_time_add(l->next, l->delta);
229 }
230 }
231 delta = fr_time_sub(l->next, now);
232
233 /*
234 * Set the timer for the next packet.
235 */
236 if (fr_timer_in(l, tl, &l->ev, delta, false, load_timer, l) < 0) {
237 load_drain(l, now);
238 return;
239 }
240
241 if (count) fr_load_generator_send(l, now, count);
242}
243
244
245/** Start the load generator.
246 *
247 */
249{
251
252 l->stats.start = fr_time();
253 l->step_start = l->stats.start;
255
257 l->sent_base = l->stats.sent;
258
259 l->pps = l->config->start_pps;
260
261 /*
262 * Check for numerical overflow. We later multiply pps*milliseconds, and we don't want overflow.
263 */
264 max = UINT32_MAX / l->config->milliseconds;
265
266 if (l->pps > max) l->pps = max;
267
268 l->stats.pps = l->pps;
269 l->count = l->config->parallel;
270
272 l->next = fr_time_add(l->step_start, l->delta);
273
274 if (fr_timer_in(l, l->el->tl, &l->ev, fr_time_delta_wrap(0), false, load_timer, l) < 0) return -1;
275 return 0;
276}
277
278
279/** Stop the load generation through the simple expedient of deleting
280 * the timer associated with it.
281 *
282 */
284{
285 if (!fr_timer_armed(l->ev)) return 0;
286
288 return 0;
289}
290
291
292/** Tell the load generator that we have a reply to a packet we sent.
293 *
294 */
296{
297 fr_time_t now;
299
300 /*
301 * Note that the replies may come out of order with
302 * respect to the request. So we can't use this reply
303 * for any kind of timing.
304 */
305 now = fr_time();
306 t = fr_time_sub(now, request_time);
307
308 fr_rtt_update(&l->stats.rtt, t);
309
310 l->stats.received++;
311
312 /*
313 * t is in nanoseconds.
314 */
315 if (fr_time_delta_lt(t, fr_time_delta_wrap(1000))) {
316 l->stats.times[0]++; /* < microseconds */
317 } else if (fr_time_delta_lt(t, fr_time_delta_wrap(10000))) {
318 l->stats.times[1]++; /* microseconds */
319 } else if (fr_time_delta_lt(t, fr_time_delta_wrap(100000))) {
320 l->stats.times[2]++; /* 10s of microseconds */
321 } else if (fr_time_delta_lt(t, fr_time_delta_wrap(1000000))) {
322 l->stats.times[3]++; /* 100s of microseconds */
323 } else if (fr_time_delta_lt(t, fr_time_delta_wrap(10000000))) {
324 l->stats.times[4]++; /* milliseconds */
325 } else if (fr_time_delta_lt(t, fr_time_delta_wrap(100000000))) {
326 l->stats.times[5]++; /* 10s of milliseconds */
327 } else if (fr_time_delta_lt(t, fr_time_delta_wrap(NSEC))) {
328 l->stats.times[6]++; /* 100s of milliseconds */
329 } else {
330 l->stats.times[7]++; /* seconds */
331 }
332
333 /*
334 * Still sending packets. Rely on the timer to send more
335 * packets.
336 */
338
339 /*
340 * The send code has decided that the backlog is too
341 * high. New requests are blocked until replies come in.
342 * Since we have a reply, send another request. Unless
343 * we've already sent max_requests packets, in which case
344 * the timer will notice and start draining.
345 */
346 if (l->state == FR_LOAD_STATE_GATED) {
347 if ((l->stats.skipped > 0) &&
348 (!l->config->max_requests ||
349 ((uint32_t) (l->stats.sent - l->sent_base) < l->config->max_requests))) {
350 l->stats.skipped--;
351 fr_load_generator_send(l, now, 1);
352 }
353 return FR_LOAD_CONTINUE;
354 }
355
356 /*
357 * We're still sending or gated, tell the caller to
358 * continue.
359 */
360 if (l->state != FR_LOAD_STATE_DRAINING) {
361 return FR_LOAD_CONTINUE;
362 }
363 /*
364 * Not yet received all replies. Wait until we have all
365 * replies.
366 */
367 if (l->stats.received < l->stats.sent) return FR_LOAD_CONTINUE;
368
369 l->stats.end = now;
370 return FR_LOAD_DONE;
371}
372
373/** Print load generator statistics in CVS format.
374 *
375 */
376size_t fr_load_generator_stats_sprint(fr_load_t *l, fr_time_t now, char *buffer, size_t buflen)
377{
378 double now_f, last_send_f;
379
380 if (!l->header) {
381 l->header = true;
382 return snprintf(buffer, buflen, "\"time\",\"last_packet\",\"rtt\",\"rttvar\",\"pps\",\"pps_accepted\",\"sent\",\"received\",\"backlog\",\"max_backlog\",\"<usec\",\"us\",\"10us\",\"100us\",\"ms\",\"10ms\",\"100ms\",\"s\",\"blocked\"\n");
383 }
384
385
387
389
390 /*
391 * Track packets/s. Since times are in nanoseconds, we
392 * have to scale the counters up by NSEC. And since NSEC
393 * is 1B, the calculations have to be done via 64-bit
394 * numbers, and then converted to a final 32-bit counter.
395 */
396 if (fr_time_gt(now, l->step_start)) {
397 l->stats.pps_accepted = (int)
400 fr_time_sub(now, l->step_start))
401 );
402 }
403
404 return snprintf(buffer, buflen,
405 "%f,%f,"
406 "%" PRId64 ",%" PRId64 ","
407 "%d,%d,"
408 "%d,%d,"
409 "%d,%d,"
410 "%d,%d,%d,%d,%d,%d,%d,%d,"
411 "%d\n",
412 now_f, last_send_f,
415 l->stats.sent, l->stats.received,
417 l->stats.times[0], l->stats.times[1], l->stats.times[2], l->stats.times[3],
418 l->stats.times[4], l->stats.times[5], l->stats.times[6], l->stats.times[7],
419 l->stats.blocked);
420}
421
423{
424 return &l->stats;
425}
static int const char char buffer[256]
Definition acutest.h:641
#define RCSID(id)
Definition build.h:609
#define fr_time()
Definition event.c:61
Stores all information relating to an event list.
Definition event.c:415
int step_received
Definition load.c:47
static void fr_load_generator_send(fr_load_t *l, fr_time_t now, int count)
Send one or more packets.
Definition load.c:86
void * uctx
Definition load.c:42
static void load_timer(fr_timer_list_t *tl, fr_time_t now, void *uctx)
Definition load.c:121
fr_load_stats_t const * fr_load_generator_stats(fr_load_t const *l)
Definition load.c:422
fr_time_t step_start
when the current step started
Definition load.c:45
fr_time_t step_end
when the current step will end
Definition load.c:46
static void load_drain(fr_load_t *l, fr_time_t now)
Stop sending new packets, and wait for outstanding replies.
Definition load.c:111
fr_load_callback_t callback
Definition load.c:40
fr_time_delta_t delta
between packets
Definition load.c:52
fr_load_state_t state
Definition load.c:37
fr_timer_t * ev
Definition load.c:58
fr_time_t next
The next time we're supposed to send a packet.
Definition load.c:57
fr_load_state_t
Definition load.c:29
@ FR_LOAD_STATE_SENDING
Definition load.c:31
@ FR_LOAD_STATE_GATED
Definition load.c:32
@ FR_LOAD_STATE_INIT
Definition load.c:30
@ FR_LOAD_STATE_DRAINING
Definition load.c:33
int fr_load_generator_start(fr_load_t *l)
Start the load generator.
Definition load.c:248
int sent_base
stats.sent when this run started, so that max_requests counts per-run, not cumulatively
Definition load.c:48
int fr_load_generator_stop(fr_load_t *l)
Stop the load generation through the simple expedient of deleting the timer associated with it.
Definition load.c:283
fr_event_list_t * el
Definition load.c:38
fr_load_done_callback_t done
Definition load.c:41
fr_load_stats_t stats
sending statistics
Definition load.c:44
fr_load_config_t const * config
Definition load.c:39
fr_load_t * fr_load_generator_create(TALLOC_CTX *ctx, fr_event_list_t *el, fr_load_config_t *config, fr_load_callback_t callback, fr_load_done_callback_t done, void *uctx)
Definition load.c:61
bool header
for printing statistics
Definition load.c:55
uint32_t pps
Definition load.c:51
uint32_t count
Definition load.c:54
size_t fr_load_generator_stats_sprint(fr_load_t *l, fr_time_t now, char *buffer, size_t buflen)
Print load generator statistics in CVS format.
Definition load.c:376
fr_load_reply_t fr_load_generator_have_reply(fr_load_t *l, fr_time_t request_time)
Tell the load generator that we have a reply to a packet we sent.
Definition load.c:295
int sent
total packets sent
Definition load.h:104
fr_time_t last_send
last packet we sent
Definition load.h:100
int times[8]
response time in microseconds to tens of seconds
Definition load.h:110
int received
total packets received (should be == sent)
Definition load.h:105
fr_rtt_t rtt
smoothed round trip time, and variation
Definition load.h:101
int(* fr_load_callback_t)(fr_time_t now, void *uctx)
Definition load.h:124
uint32_t max_requests
hold at max_pps until this many packets are sent, 0 for "no limit"
Definition load.h:93
int skipped
we skipped sending this number of packets
Definition load.h:106
bool unlimited
hold at max_pps forever, the generator is never done
Definition load.h:94
bool blocked
whether or not we're blocked
Definition load.h:109
fr_time_t end
when the test started
Definition load.h:99
int pps_accepted
Accepted PPS for the last second.
Definition load.h:103
fr_load_reply_t
Whether or not the application should continue.
Definition load.h:118
@ FR_LOAD_DONE
the load generator is done
Definition load.h:120
@ FR_LOAD_CONTINUE
continue sending packets.
Definition load.h:119
uint32_t start_pps
start PPS
Definition load.h:87
int pps
current offered packets/s
Definition load.h:102
void(* fr_load_done_callback_t)(void *uctx)
Called when the generator has stopped sending and every reply has arrived.
Definition load.h:134
fr_time_t start
Definition load.h:98
uint32_t parallel
how many packets in parallel to send
Definition load.h:91
fr_time_delta_t duration
duration of each step
Definition load.h:89
int backlog
current backlog
Definition load.h:107
uint32_t step
how much to increase each load test by
Definition load.h:90
int max_backlog
maximum backlog we saw during the test
Definition load.h:108
uint32_t max_pps
max PPS, 0 for "no limit".
Definition load.h:88
uint32_t milliseconds
how many milliseconds of backlog to top out at
Definition load.h:92
Load generation configuration.
Definition load.h:86
unsigned int uint32_t
unsigned char uint8_t
static const conf_parser_t config[]
Definition base.c:164
static bool done
Definition radclient.c:80
fr_time_delta_t rttvar
RTT variation.
Definition rtt.h:50
fr_time_delta_t rtt
smoothed round trip time
Definition rtt.h:49
static void fr_rtt_update(fr_rtt_t *r, fr_time_delta_t t)
Update the smoothed round trip time and the variation with a new sample.
Definition rtt.h:96
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:173
#define fr_time_gteq(_a, _b)
Definition time.h:238
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
#define fr_time_delta_lt(_a, _b)
Definition time.h:285
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
#define fr_time_delta_wrap(_time)
Definition time.h:152
static double fr_time_delta_to_sec_double(fr_time_delta_t delta)
Seconds as a double, for printing and for arithmetic which need not be exact.
Definition time.h:663
#define NSEC
Definition time.h:379
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
#define fr_time_gt(_a, _b)
Definition time.h:237
static fr_time_delta_t fr_time_delta_from_nsec(int64_t nsec)
Definition time.h:572
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
static fr_time_delta_t fr_time_delta_div(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:267
#define fr_time_lt(_a, _b)
Definition time.h:239
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:65
A timer event.
Definition timer.c:100
#define FR_TIMER_DELETE_RETURN(_ev_p)
Definition timer.h:112
#define fr_timer_in(...)
Definition timer.h:87
static bool fr_timer_armed(fr_timer_t *ev)
Definition timer.h:124
static fr_event_list_t * el
static fr_sbuff_err_t char size_t fr_sbuff_t size_t max
Definition value.h:1061