1#ifndef __tim_filter_h__
2#define __tim_filter_h__
3/*-
4 * Copyright (c) 2016-9 Netflix, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28/*
29 * Author: Randall Stewart <rrs@netflix.com>
30 */
31
32#include <sys/types.h>
33#include <machine/param.h>
34/*
35 * Do not change the size unless you know what you are
36 * doing, the current size of 5 is designed around
37 * the cache-line size for an amd64 processor. Other processors
38 * may need other sizes.
39 */
40#define NUM_FILTER_ENTRIES 3
41
42struct filter_entry {
43	uint64_t value;		/* Value */
44	uint32_t time_up;	/* Time updated */
45} __packed ;
46
47struct filter_entry_small {
48	uint32_t value;		/* Value */
49	uint32_t time_up;	/* Time updated */
50};
51
52struct time_filter {
53	uint32_t cur_time_limit;
54	struct filter_entry entries[NUM_FILTER_ENTRIES];
55#ifdef _KERNEL
56} __aligned(CACHE_LINE_SIZE);
57#else
58};
59#endif
60struct time_filter_small {
61	uint32_t cur_time_limit;
62	struct filter_entry_small entries[NUM_FILTER_ENTRIES];
63};
64
65/*
66 * To conserve on space there is a code duplication here (this
67 * is where polymophism would be nice in the kernel). Everything
68 * is duplicated to have a filter with a value of uint32_t instead
69 * of a uint64_t. This saves 20 bytes and the structure size
70 * drops to 44 from 64. The bad part about this is you end
71 * up with two sets of functions. The xxx_small() access
72 * the uint32_t value's where the xxx() the uint64_t values.
73 * This forces the user to keep straight which type of structure
74 * they allocated and which call they need to make. crossing
75 * over calls will create either invalid memory references or
76 * very bad results :)
77 */
78
79#define FILTER_TYPE_MIN 1
80#define FILTER_TYPE_MAX 2
81
82#ifdef _KERNEL
83int setup_time_filter(struct time_filter *tf, int fil_type, uint32_t time_len);
84void reset_time(struct time_filter *tf, uint32_t time_len);
85void forward_filter_clock(struct time_filter *tf, uint32_t ticks_forward);
86void tick_filter_clock(struct time_filter *tf, uint32_t now);
87uint32_t apply_filter_min(struct time_filter *tf, uint64_t value, uint32_t now);
88uint32_t apply_filter_max(struct time_filter *tf, uint64_t value, uint32_t now);
89void filter_reduce_by(struct time_filter *tf, uint64_t reduce_by, uint32_t now);
90void filter_increase_by(struct time_filter *tf, uint64_t incr_by, uint32_t now);
91static uint64_t inline
92get_filter_value(struct time_filter *tf)
93{
94	return(tf->entries[0].value);
95}
96
97static uint32_t inline
98get_cur_timelim(struct time_filter *tf)
99{
100	return(tf->cur_time_limit);
101}
102
103int setup_time_filter_small(struct time_filter_small *tf,
104			    int fil_type, uint32_t time_len);
105void reset_time_small(struct time_filter_small *tf, uint32_t time_len);
106void forward_filter_clock_small(struct time_filter_small *tf,
107				uint32_t ticks_forward);
108void tick_filter_clock_small(struct time_filter_small *tf, uint32_t now);
109uint32_t apply_filter_min_small(struct time_filter_small *tf,
110				uint32_t value, uint32_t now);
111uint32_t apply_filter_max_small(struct time_filter_small *tf,
112				uint32_t value, uint32_t now);
113void filter_reduce_by_small(struct time_filter_small *tf,
114			    uint32_t reduce_by, uint32_t now);
115void filter_increase_by_small(struct time_filter_small *tf,
116			      uint32_t incr_by, uint32_t now);
117static uint64_t inline
118get_filter_value_small(struct time_filter_small *tf)
119{
120	return(tf->entries[0].value);
121}
122
123static uint32_t inline
124get_cur_timelim_small(struct time_filter_small *tf)
125{
126	return(tf->cur_time_limit);
127}
128
129#endif
130#endif
131