forked from mariusae/trickle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwstat.c
260 lines (203 loc) · 4.84 KB
/
bwstat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* bwstat.c
*
* Copyright (c) 2003 Marius Aamodt Eriksen <[email protected]>
* All rights reserved.
*
* $Id: bwstat.c,v 1.15 2003/07/29 05:58:58 marius Exp $
*/
#include <sys/types.h>
#include <sys/queue.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */
#if defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME)
#include <time.h>
#endif /* defined(HAVE_TIME_H) && defined(TIME_WITH_SYS_TIME) */
#include <string.h>
#include "util.h"
#include "bwstat.h"
static TAILQ_HEAD(bwstathead, bwstat) statq;
static uint winsz;
static double difftv(struct timeval *, struct timeval *);
static void _bwstat_update(struct bwstat_data *, size_t);
#define INITTV(tv, curtv) do { \
if (!timerisset(&(tv))) \
(tv) = (curtv); \
} while (0)
/*
* XXX should winsz be per-update?
*/
int
bwstat_init(uint xwinsz)
{
struct bwstat *bs;
winsz = xwinsz;
TAILQ_INIT(&statq);
/* First entry is the totals. */
if ((bs = bwstat_new()) == NULL)
return (-1);
return (0);
}
struct bwstat *
bwstat_new(void)
{
struct bwstat *bs;
if ((bs = calloc(1, sizeof(*bs))) == NULL)
return (NULL);
bs->pts = 1;
TAILQ_INSERT_TAIL(&statq, bs, next);
return (bs);
}
struct bwstat *
bwstat_free(struct bwstat *bs)
{
TAILQ_REMOVE(&statq, bs, next);
return (NULL);
}
struct bwstat *
bwstat_gettot(void)
{
return (TAILQ_FIRST(&statq));
}
void
bwstat_update(struct bwstat *bs, size_t len, short which)
{
struct bwstat *bstot = TAILQ_FIRST(&statq);
if (bs != NULL)
_bwstat_update(&bs->data[which], len);
_bwstat_update(&bstot->data[which], len);
}
static void
_bwstat_update(struct bwstat_data *bsd, size_t len)
{
struct timeval curtv;
double elap, elapwin;
gettimeofday(&curtv, NULL);
INITTV(bsd->tv, curtv);
INITTV(bsd->wintv, curtv);
elap = difftv(&curtv, &bsd->tv);
elapwin = difftv(&curtv, &bsd->wintv);
/* XXX Window follows. */
if (bsd->winbytes == 0 && bsd->winrate > 0)
bsd->winbytes = (1.0 * bsd->winrate) * elapwin;
bsd->bytes += len;
bsd->winbytes += len;
if (elap == 0.0 || elapwin == 0.0)
return;
bsd->rate = (1.0 * bsd->bytes) / elap;
bsd->winrate = (1.0 * bsd->winbytes) / elapwin;
/*
* Reset window; XXX make this a sliding window wrt a
* timeline; keep average, but reduce #bytes?
*/
if (bsd->winbytes >= winsz) {
gettimeofday(&bsd->wintv, NULL);
bsd->winbytes = 0;
}
}
/*
* Return required delay for bs for direction which.
*/
struct timeval *
bwstat_getdelay(struct bwstat *bs, size_t *len, uint lim, short which)
{
uint rate = 0, npts = 0, ent;
int ncli = 0, pool = 0, xent;
double delay;
static struct timeval tv;
struct bwstathead poolq;
struct bwstat *xbs, *bstot = TAILQ_FIRST(&statq);
uint initent;
size_t xlen = *len;
if (*len == 0)
return (NULL);
memset(&tv, 0, sizeof(tv));
TAILQ_INIT(&poolq);
rate = bstot->data[which].winrate;
if (rate <= lim)
return (NULL);
xbs = bstot;
while ((xbs = TAILQ_NEXT(xbs, next)) != NULL) {
ncli++;
npts += xbs->pts;
TAILQ_INSERT_TAIL(&poolq, xbs, qnext);
}
if (ncli == 0)
return (NULL);
/* Entitlement per point */
initent = ent = lim / npts;
if (ent == 0)
; /*
* XXX we have surpassed our
* granularity.
*/
/*
* Sprinkle some bandwidth: increase the value of a point
* until everyone is satisfied.
*/
do {
/* Take from the poor ... */
TAILQ_FOREACH(xbs, &poolq, qnext)
if (xbs->data[which].winrate < ent * xbs->pts) {
pool += ent * xbs->pts -
xbs->data[which].winrate;
ncli--;
npts -= xbs->pts;
TAILQ_REMOVE(&poolq, xbs, qnext);
}
/* And give to the rich ... */
if (ncli > 0) {
xent = pool / npts;
if (xent <= 0)
break;
TAILQ_FOREACH(xbs, &poolq, qnext)
if (xbs->data[which].winrate > ent * xbs->pts)
pool -= xent * xbs->pts;
ent += xent;
}
} while (pool > 0 && ncli > 0);
/*
* This is the case of a client that is not using its limit.
* We reset ent in this case. The rest will adjust itself
* over time.
*/
if (ent * bs->pts > lim)
ent = lim / bs->pts;
if (bs->data[which].winrate > ent * bs->pts)
delay = (1.0 * *len) / (1.0 * ent * bs->pts);
else
delay = 0.0;
/* if (delay > bs->tsmooth) { */
if ((*len = ent * bs->pts * bs->tsmooth) == 0) {
*len = bs->lsmooth;
delay = (1.0 * *len) / (1.0 * ent * bs->pts);
} else
delay = bs->tsmooth;
/* } */
if (*len > xlen) {
*len = xlen;
delay = (1.0 * *len) / (1.0 * ent * bs->pts);
}
/* XXX */
if (delay < 0.0)
return (NULL);
tv.tv_sec = delay;
tv.tv_usec = (delay - tv.tv_sec) * 1000000L;
return (&tv);
}
static double
difftv(struct timeval *tv0, struct timeval *tv1)
{
struct timeval diff_tv;
timersub(tv0, tv1, &diff_tv);
return (diff_tv.tv_sec + (diff_tv.tv_usec / 1000000.0));
}