aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/subr_filter.c
blob: 9b36058e481ca02ba05a3781e84d54871299b62b (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*-
 * Copyright (c) 2016-2019 Netflix, Inc.
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

/*
 * Author: Randall Stewart <rrs@netflix.com>
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/tim_filter.h>

void
reset_time(struct time_filter *tf, uint32_t time_len)
{
	tf->cur_time_limit = time_len;
}

void
reset_time_small(struct time_filter_small *tf, uint32_t time_len)
{
	tf->cur_time_limit = time_len;
}

/*
 * A time filter can be a filter for MIN or MAX. 
 * You call setup_time_filter() with the pointer to
 * the filter structure, the type (FILTER_TYPE_MIN/MAX) and
 * the time length. You can optionally reset the time length
 * later with reset_time().
 *
 * You generally call apply_filter_xxx() to apply the new value
 * to the filter. You also provide a time (now). The filter will
 * age out entries based on the time now and your time limit
 * so that you are always maintaining the min or max in that
 * window of time. Time is a relative thing, it might be ticks
 * in milliseconds, it might be round trip times, its really
 * up to you to decide what it is.
 *
 * To access the current flitered value you can use the macro
 * get_filter_value() which returns the correct entry that
 * has the "current" value in the filter.
 *
 * One thing that used to be here is a single apply_filter(). But
 * this meant that we then had to store the type of filter in
 * the time_filter structure. In order to keep it at a cache
 * line size I split it to two functions. 
 *
 */
int
setup_time_filter(struct time_filter *tf, int fil_type, uint32_t time_len)
{
	uint64_t set_val;
	int i;

	/* 
	 * You must specify either a MIN or MAX filter,
	 * though its up to the user to use the correct
	 * apply.
	 */
	if ((fil_type != FILTER_TYPE_MIN) &&
	    (fil_type != FILTER_TYPE_MAX))
		return(EINVAL);

	if (time_len < NUM_FILTER_ENTRIES)
		return(EINVAL);
		       
	if (fil_type == FILTER_TYPE_MIN)
		set_val = 0xffffffffffffffff;
	else
		set_val = 0;

	for(i=0; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].value = set_val;
		tf->entries[i].time_up = 0;
	}
	tf->cur_time_limit = time_len;
	return(0);
}

int
setup_time_filter_small(struct time_filter_small *tf, int fil_type, uint32_t time_len)
{
	uint32_t set_val;
	int i;

	/* 
	 * You must specify either a MIN or MAX filter,
	 * though its up to the user to use the correct
	 * apply.
	 */
	if ((fil_type != FILTER_TYPE_MIN) &&
	    (fil_type != FILTER_TYPE_MAX))
		return(EINVAL);

	if (time_len < NUM_FILTER_ENTRIES)
		return(EINVAL);
		       
	if (fil_type == FILTER_TYPE_MIN)
		set_val = 0xffffffff;
	else
		set_val = 0;

	for(i=0; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].value = set_val;
		tf->entries[i].time_up = 0;
	}
	tf->cur_time_limit = time_len;
	return(0);
}

static void
check_update_times(struct time_filter *tf, uint64_t value, uint32_t now)
{
	int i, j, fnd;
	uint32_t tim;
	uint32_t time_limit;
	for(i=0; i<(NUM_FILTER_ENTRIES-1); i++) {
		tim = now - tf->entries[i].time_up;
		time_limit = (tf->cur_time_limit * (NUM_FILTER_ENTRIES-i))/NUM_FILTER_ENTRIES;
		if (tim >= time_limit) {
			fnd = 0;
			for(j=(i+1); j<NUM_FILTER_ENTRIES; j++) {
				if (tf->entries[i].time_up < tf->entries[j].time_up) {
					tf->entries[i].value = tf->entries[j].value;
					tf->entries[i].time_up = tf->entries[j].time_up;
					fnd = 1;
					break;
				}
			}
			if (fnd == 0) {
				/* Nothing but the same old entry */
				tf->entries[i].value = value;
				tf->entries[i].time_up = now;
			}
		}
	}
	i = NUM_FILTER_ENTRIES-1;
	tim = now - tf->entries[i].time_up;
	time_limit = (tf->cur_time_limit * (NUM_FILTER_ENTRIES-i))/NUM_FILTER_ENTRIES;
	if (tim >= time_limit) {
		tf->entries[i].value = value;
		tf->entries[i].time_up = now;
	}
}

static void
check_update_times_small(struct time_filter_small *tf, uint32_t value, uint32_t now)
{
	int i, j, fnd;
	uint32_t tim;
	uint32_t time_limit;
	for(i=0; i<(NUM_FILTER_ENTRIES-1); i++) {
		tim = now - tf->entries[i].time_up;
		time_limit = (tf->cur_time_limit * (NUM_FILTER_ENTRIES-i))/NUM_FILTER_ENTRIES;
		if (tim >= time_limit) {
			fnd = 0;
			for(j=(i+1); j<NUM_FILTER_ENTRIES; j++) {
				if (tf->entries[i].time_up < tf->entries[j].time_up) {
					tf->entries[i].value = tf->entries[j].value;
					tf->entries[i].time_up = tf->entries[j].time_up;
					fnd = 1;
					break;
				}
			}
			if (fnd == 0) {
				/* Nothing but the same old entry */
				tf->entries[i].value = value;
				tf->entries[i].time_up = now;
			}
		}
	}
	i = NUM_FILTER_ENTRIES-1;
	tim = now - tf->entries[i].time_up;
	time_limit = (tf->cur_time_limit * (NUM_FILTER_ENTRIES-i))/NUM_FILTER_ENTRIES;
	if (tim >= time_limit) {
		tf->entries[i].value = value;
		tf->entries[i].time_up = now;
	}
}

void
filter_reduce_by(struct time_filter *tf, uint64_t reduce_by, uint32_t now)
{
	int i;
	/* 
	 * Reduce our filter main by reduce_by and
	 * update its time. Then walk other's and
	 * make them the new value too.
	 */
	if (reduce_by < tf->entries[0].value)
		tf->entries[0].value -= reduce_by;
	else
		tf->entries[0].value = 0;
	tf->entries[0].time_up = now;
	for(i=1; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].value = tf->entries[0].value;
		tf->entries[i].time_up = now;
	}
}

void
filter_reduce_by_small(struct time_filter_small *tf, uint32_t reduce_by, uint32_t now)
{
	int i;
	/* 
	 * Reduce our filter main by reduce_by and
	 * update its time. Then walk other's and
	 * make them the new value too.
	 */
	if (reduce_by < tf->entries[0].value)
		tf->entries[0].value -= reduce_by;
	else
		tf->entries[0].value = 0;
	tf->entries[0].time_up = now;
	for(i=1; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].value = tf->entries[0].value;
		tf->entries[i].time_up = now;
	}
}

void
filter_increase_by(struct time_filter *tf, uint64_t incr_by, uint32_t now)
{
	int i;
	/* 
	 * Increase our filter main by incr_by and
	 * update its time. Then walk other's and
	 * make them the new value too.
	 */
	tf->entries[0].value += incr_by;
	tf->entries[0].time_up = now;
	for(i=1; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].value = tf->entries[0].value;
		tf->entries[i].time_up = now;
	}
}

void
filter_increase_by_small(struct time_filter_small *tf, uint32_t incr_by, uint32_t now)
{
	int i;
	/* 
	 * Increase our filter main by incr_by and
	 * update its time. Then walk other's and
	 * make them the new value too.
	 */
	tf->entries[0].value += incr_by;
	tf->entries[0].time_up = now;
	for(i=1; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].value = tf->entries[0].value;
		tf->entries[i].time_up = now;
	}
}

void
forward_filter_clock(struct time_filter *tf, uint32_t ticks_forward)
{
	/*
	 * Bring forward all time values by N ticks. This
	 * postpones expiring slots by that amount.
	 */
	int i;

	for(i=0; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].time_up += ticks_forward;
	}
}

void
forward_filter_clock_small(struct time_filter_small *tf, uint32_t ticks_forward)
{
	/*
	 * Bring forward all time values by N ticks. This
	 * postpones expiring slots by that amount.
	 */
	int i;

	for(i=0; i<NUM_FILTER_ENTRIES; i++) {
		tf->entries[i].time_up += ticks_forward;
	}
}

void
tick_filter_clock(struct time_filter *tf, uint32_t now)
{
	int i;
	uint32_t tim, time_limit;

	/*
	 * We start at two positions back. This
	 * is because the oldest worst value is
	 * preserved always, i.e. it can't expire
	 * due to clock ticking with no updated value.
	 *
	 * The other choice would be to fill it in with
	 * zero, but I don't like that option since
	 * some measurement is better than none (even
	 * if its your oldest measurment).
	 */
	for(i=(NUM_FILTER_ENTRIES-2); i>=0 ; i--) {
		tim = now - tf->entries[i].time_up;
		time_limit = (tf->cur_time_limit * (NUM_FILTER_ENTRIES-i))/NUM_FILTER_ENTRIES;
		if (tim >= time_limit) {
			/* 
			 * This entry is expired, pull down
			 * the next one up.
			 */
			tf->entries[i].value = tf->entries[(i+1)].value;
			tf->entries[i].time_up = tf->entries[(i+1)].time_up;
		}
	}
}

void
tick_filter_clock_small(struct time_filter_small *tf, uint32_t now)
{
	int i;
	uint32_t tim, time_limit;

	/*
	 * We start at two positions back. This
	 * is because the oldest worst value is
	 * preserved always, i.e. it can't expire
	 * due to clock ticking with no updated value.
	 *
	 * The other choice would be to fill it in with
	 * zero, but I don't like that option since
	 * some measurement is better than none (even
	 * if its your oldest measurment).
	 */
	for(i=(NUM_FILTER_ENTRIES-2); i>=0 ; i--) {
		tim = now - tf->entries[i].time_up;
		time_limit = (tf->cur_time_limit * (NUM_FILTER_ENTRIES-i))/NUM_FILTER_ENTRIES;
		if (tim >= time_limit) {
			/* 
			 * This entry is expired, pull down
			 * the next one up.
			 */
			tf->entries[i].value = tf->entries[(i+1)].value;
			tf->entries[i].time_up = tf->entries[(i+1)].time_up;
		}
	}
}

uint32_t
apply_filter_min(struct time_filter *tf, uint64_t value, uint32_t now)
{
	int i, j;

	if (value <= tf->entries[0].value) {
		/* Zap them all */
		for(i=0; i<NUM_FILTER_ENTRIES; i++) {
			tf->entries[i].value = value;
			tf->entries[i].time_up = now;
		}
		return (tf->entries[0].value);
	}
	for (j=1; j<NUM_FILTER_ENTRIES; j++) {
		if (value <= tf->entries[j].value) {
			for(i=j; i<NUM_FILTER_ENTRIES; i++) {
				tf->entries[i].value = value;
				tf->entries[i].time_up = now;
			}
			break;
		}
	}
	check_update_times(tf, value, now);
	return (tf->entries[0].value);
}

uint32_t
apply_filter_min_small(struct time_filter_small *tf,
		       uint32_t value, uint32_t now)
{
	int i, j;

	if (value <= tf->entries[0].value) {
		/* Zap them all */
		for(i=0; i<NUM_FILTER_ENTRIES; i++) {
			tf->entries[i].value = value;
			tf->entries[i].time_up = now;
		}
		return (tf->entries[0].value);
	}
	for (j=1; j<NUM_FILTER_ENTRIES; j++) {
		if (value <= tf->entries[j].value) {
			for(i=j; i<NUM_FILTER_ENTRIES; i++) {
				tf->entries[i].value = value;
				tf->entries[i].time_up = now;
			}
			break;
		}
	}
	check_update_times_small(tf, value, now);
	return (tf->entries[0].value);
}

uint32_t
apply_filter_max(struct time_filter *tf, uint64_t value, uint32_t now)
{
	int i, j;

	if (value >= tf->entries[0].value) {
		/* Zap them all */
		for(i=0; i<NUM_FILTER_ENTRIES; i++) {
			tf->entries[i].value = value;
			tf->entries[i].time_up = now;
		}
		return (tf->entries[0].value);
	}
	for (j=1; j<NUM_FILTER_ENTRIES; j++) {
		if (value >= tf->entries[j].value) {
			for(i=j; i<NUM_FILTER_ENTRIES; i++) {
				tf->entries[i].value = value;
				tf->entries[i].time_up = now;
			}
			break;
		}
	}
	check_update_times(tf, value, now);
	return (tf->entries[0].value);
}

uint32_t
apply_filter_max_small(struct time_filter_small *tf,
		       uint32_t value, uint32_t now)
{
	int i, j;

	if (value >= tf->entries[0].value) {
		/* Zap them all */
		for(i=0; i<NUM_FILTER_ENTRIES; i++) {
			tf->entries[i].value = value;
			tf->entries[i].time_up = now;
		}
		return (tf->entries[0].value);
	}
	for (j=1; j<NUM_FILTER_ENTRIES; j++) {
		if (value >= tf->entries[j].value) {
			for(i=j; i<NUM_FILTER_ENTRIES; i++) {
				tf->entries[i].value = value;
				tf->entries[i].time_up = now;
			}
			break;
		}
	}
	check_update_times_small(tf, value, now);
	return (tf->entries[0].value);
}