Skip to content

Commit 536edd6

Browse files
edumazetdavem330
authored andcommitted
codel: use Newton method instead of sqrt() and divides
As Van pointed out, interval/sqrt(count) can be implemented using multiplies only. http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots This patch implements the Newton method and reciprocal divide. Total cost is 15 cycles instead of 120 on my Corei5 machine (64bit kernel). There is a small 'error' for count values < 5, but we don't really care. I reuse a hole in struct codel_vars : - pack the dropping boolean into one bit - use 31bit to store the reciprocal value of sqrt(count). Suggested-by: Van Jacobson <van@pollere.net> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Dave Taht <dave.taht@bufferbloat.net> Cc: Kathleen Nichols <nichols@pollere.com> Cc: Tom Herbert <therbert@google.com> Cc: Matt Mathis <mattmathis@google.com> Cc: Yuchung Cheng <ycheng@google.com> Cc: Nandita Dukkipati <nanditad@google.com> Cc: Stephen Hemminger <shemminger@vyatta.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 470f16c commit 536edd6

1 file changed

Lines changed: 37 additions & 31 deletions

File tree

‎include/net/codel.h‎

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <linux/skbuff.h>
4747
#include <net/pkt_sched.h>
4848
#include <net/inet_ecn.h>
49+
#include <linux/reciprocal_div.h>
4950

5051
/* Controlling Queue Delay (CoDel) algorithm
5152
* =========================================
@@ -123,6 +124,7 @@ struct codel_params {
123124
* entered dropping state
124125
* @lastcount: count at entry to dropping state
125126
* @dropping: set to true if in dropping state
127+
* @rec_inv_sqrt: reciprocal value of sqrt(count) >> 1
126128
* @first_above_time: when we went (or will go) continuously above target
127129
* for interval
128130
* @drop_next: time to drop next packet, or when we dropped last
@@ -131,7 +133,8 @@ struct codel_params {
131133
struct codel_vars {
132134
u32 count;
133135
u32 lastcount;
134-
bool dropping;
136+
bool dropping:1;
137+
u32 rec_inv_sqrt:31;
135138
codel_time_t first_above_time;
136139
codel_time_t drop_next;
137140
codel_time_t ldelay;
@@ -158,50 +161,45 @@ static void codel_params_init(struct codel_params *params)
158161

159162
static void codel_vars_init(struct codel_vars *vars)
160163
{
161-
vars->drop_next = 0;
162-
vars->first_above_time = 0;
163-
vars->dropping = false; /* exit dropping state */
164-
vars->count = 0;
165-
vars->lastcount = 0;
164+
memset(vars, 0, sizeof(*vars));
166165
}
167166

168167
static void codel_stats_init(struct codel_stats *stats)
169168
{
170169
stats->maxpacket = 256;
171170
}
172171

173-
/* return interval/sqrt(x) with good precision
174-
* relies on int_sqrt(unsigned long x) kernel implementation
172+
/*
173+
* http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots
174+
* new_invsqrt = (invsqrt / 2) * (3 - count * invsqrt^2)
175+
*
176+
* Here, invsqrt is a fixed point number (< 1.0), 31bit mantissa)
175177
*/
176-
static u32 codel_inv_sqrt(u32 _interval, u32 _x)
178+
static void codel_Newton_step(struct codel_vars *vars)
177179
{
178-
u64 interval = _interval;
179-
unsigned long x = _x;
180+
u32 invsqrt = vars->rec_inv_sqrt;
181+
u32 invsqrt2 = ((u64)invsqrt * invsqrt) >> 31;
182+
u64 val = (3LL << 31) - ((u64)vars->count * invsqrt2);
180183

181-
/* Scale operands for max precision */
182-
183-
#if BITS_PER_LONG == 64
184-
x <<= 32; /* On 64bit arches, we can prescale x by 32bits */
185-
interval <<= 16;
186-
#endif
184+
val = (val * invsqrt) >> 32;
187185

188-
while (x < (1UL << (BITS_PER_LONG - 2))) {
189-
x <<= 2;
190-
interval <<= 1;
191-
}
192-
do_div(interval, int_sqrt(x));
193-
return (u32)interval;
186+
vars->rec_inv_sqrt = val;
194187
}
195188

189+
/*
190+
* CoDel control_law is t + interval/sqrt(count)
191+
* We maintain in rec_inv_sqrt the reciprocal value of sqrt(count) to avoid
192+
* both sqrt() and divide operation.
193+
*/
196194
static codel_time_t codel_control_law(codel_time_t t,
197195
codel_time_t interval,
198-
u32 count)
196+
u32 rec_inv_sqrt)
199197
{
200-
return t + codel_inv_sqrt(interval, count);
198+
return t + reciprocal_divide(interval, rec_inv_sqrt << 1);
201199
}
202200

203201

204-
static bool codel_should_drop(struct sk_buff *skb,
202+
static bool codel_should_drop(const struct sk_buff *skb,
205203
unsigned int *backlog,
206204
struct codel_vars *vars,
207205
struct codel_params *params,
@@ -274,14 +272,16 @@ static struct sk_buff *codel_dequeue(struct Qdisc *sch,
274272
*/
275273
while (vars->dropping &&
276274
codel_time_after_eq(now, vars->drop_next)) {
277-
if (++vars->count == 0) /* avoid zero divides */
278-
vars->count = ~0U;
275+
vars->count++; /* dont care of possible wrap
276+
* since there is no more divide
277+
*/
278+
codel_Newton_step(vars);
279279
if (params->ecn && INET_ECN_set_ce(skb)) {
280280
stats->ecn_mark++;
281281
vars->drop_next =
282282
codel_control_law(vars->drop_next,
283283
params->interval,
284-
vars->count);
284+
vars->rec_inv_sqrt);
285285
goto end;
286286
}
287287
qdisc_drop(skb, sch);
@@ -296,7 +296,7 @@ static struct sk_buff *codel_dequeue(struct Qdisc *sch,
296296
vars->drop_next =
297297
codel_control_law(vars->drop_next,
298298
params->interval,
299-
vars->count);
299+
vars->rec_inv_sqrt);
300300
}
301301
}
302302
}
@@ -319,12 +319,18 @@ static struct sk_buff *codel_dequeue(struct Qdisc *sch,
319319
if (codel_time_before(now - vars->drop_next,
320320
16 * params->interval)) {
321321
vars->count = (vars->count - vars->lastcount) | 1;
322+
/* we dont care if rec_inv_sqrt approximation
323+
* is not very precise :
324+
* Next Newton steps will correct it quadratically.
325+
*/
326+
codel_Newton_step(vars);
322327
} else {
323328
vars->count = 1;
329+
vars->rec_inv_sqrt = 0x7fffffff;
324330
}
325331
vars->lastcount = vars->count;
326332
vars->drop_next = codel_control_law(now, params->interval,
327-
vars->count);
333+
vars->rec_inv_sqrt);
328334
}
329335
end:
330336
return skb;

0 commit comments

Comments
 (0)