/* $NetBSD: test-ratelim.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $ */
/*
* Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../util-internal.h"
static int cfg_verbose = 0;
static int cfg_help = 0;
static int cfg_n_connections = 30;
static int cfg_duration = 5;
static int cfg_connlimit = 0;
static int cfg_grouplimit = 0;
static int cfg_tick_msec = 1000;
static int cfg_min_share = -1;
static int cfg_group_drain = 0;
static int cfg_connlimit_tolerance = -1;
static int cfg_grouplimit_tolerance = -1;
static int cfg_stddev_tolerance = -1;
#ifdef _WIN32
static int cfg_enable_iocp = 0;
#endif
/* Timers bias for slow CPUs, affects:
* - cfg_connlimit_tolerance (--check-connlimit)
* - cfg_grouplimit_tolerance (--check-grouplimit)
* - cfg_stddev_tolerance (--check-stddev)
*/
static int timer_bias_events;
static struct timeval timer_bias_start;
double timer_bias_spend;
/* Real cost is less (approximately ~5 usec),
* this macros adjusted to make the bias less */
#define TIMER_MAX_COST_USEC 10
/* info from check_bucket_levels_cb */
static int total_n_bev_checks = 0;
static ev_int64_t total_rbucket_level=0;
static ev_int64_t total_wbucket_level=0;
static ev_int64_t total_max_to_read=0;
static ev_int64_t total_max_to_write=0;
static ev_int64_t max_bucket_level=EV_INT64_MIN;
static ev_int64_t min_bucket_level=EV_INT64_MAX;
/* from check_group_bucket_levels_cb */
static int total_n_group_bev_checks = 0;
static ev_int64_t total_group_rbucket_level = 0;
static ev_int64_t total_group_wbucket_level = 0;
static int n_echo_conns_open = 0;
/* Info on the open connections */
struct bufferevent **bevs;
struct client_state *states;
struct bufferevent_rate_limit_group *group = NULL;
static void check_bucket_levels_cb(evutil_socket_t fd, short events, void *arg);
/* Called periodically to check up on how full the buckets are */
static void
check_bucket_levels_cb(evutil_socket_t fd, short events, void *arg)
{
struct bufferevent *bev = arg;
ev_ssize_t r = bufferevent_get_read_limit(bev);
ev_ssize_t w = bufferevent_get_write_limit(bev);
ev_ssize_t rm = bufferevent_get_max_to_read(bev);
ev_ssize_t wm = bufferevent_get_max_to_write(bev);
/* XXXX check that no value is above the cofigured burst
* limit */
total_rbucket_level += r;
total_wbucket_level += w;
total_max_to_read += rm;
total_max_to_write += wm;
#define B(x) \
if ((x) > max_bucket_level) \
max_bucket_level = (x); \
if ((x) < min_bucket_level) \
min_bucket_level = (x)
B(r);
B(w);
#undef B
cfg = event_config_new();
if (!cfg)
goto err;
if (event_config_set_flag(cfg, EVENT_BASE_FLAG_PRECISE_TIMER))
goto err;
base = event_base_new_with_config(cfg);
if (!base)
goto err;
ratelim_group = NULL; /* So no more responders get added */
event_free(periodic_level_check);
if (group_drain_event)
event_free(group_drain_event);
for (i = 0; i < cfg_n_connections; ++i) {
bufferevent_free(bevs[i]);
}
evconnlistener_free(listener);
/* Make sure no new echo_conns get added to the group. */
ratelim_group = NULL;
/* This should get _everybody_ freed */
while (n_echo_conns_open) {
printf("waiting for %d conns\n", n_echo_conns_open);
tv.tv_sec = 0;
tv.tv_usec = 300000;
event_base_loopexit(base, &tv);
event_base_dispatch(base);
}
if (group)
bufferevent_rate_limit_group_free(group);
printf(" stddev: %f per second\n", sqrt(variance));
if (cfg_stddev_tolerance > 0 &&
sqrt(variance) > cfg_stddev_tolerance) {
fprintf(stderr, "Connection variance out of bounds\n");
ok = 0;
}
static int
handle_option(int argc, char **argv, int *i, const struct option *opt)
{
long val;
char *endptr = NULL;
if (opt->isbool) {
*opt->ptr = 1;
return 0;
}
if (*i + 1 == argc) {
fprintf(stderr, "Too few arguments to '%s'\n",argv[*i]);
return -1;
}
val = strtol(argv[*i+1], &endptr, 10);
if (*argv[*i+1] == '\0' || !endptr || *endptr != '\0') {
fprintf(stderr, "Couldn't parse numeric value '%s'\n",
argv[*i+1]);
return -1;
}
if (val < opt->min || val > 0x7fffffff) {
fprintf(stderr, "Value '%s' is out-of-range'\n",
argv[*i+1]);
return -1;
}
*opt->ptr = (int)val;
++*i;
return 0;
}
static void
usage(void)
{
fprintf(stderr,
"test-ratelim [-v] [-n INT] [-d INT] [-c INT] [-g INT] [-t INT]\n\n"
"Pushes bytes through a number of possibly rate-limited connections, and\n"
"displays average throughput.\n\n"
" -n INT: Number of connections to open (default: 30)\n"
" -d INT: Duration of the test in seconds (default: 5 sec)\n");
fprintf(stderr,
" -c INT: Connection-rate limit applied to each connection in bytes per second\n"
" (default: None.)\n"
" -g INT: Group-rate limit applied to sum of all usage in bytes per second\n"
" (default: None.)\n"
" -G INT: drain INT bytes from the group limit every tick. (default: 0)\n"
" -t INT: Granularity of timing, in milliseconds (default: 1000 msec)\n");
}
int
main(int argc, char **argv)
{
int i,j;
double ratio;
#ifdef _WIN32
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA wsaData;