1 #ifndef __tim_filter_h__
2 #define __tim_filter_h__
4 * Copyright (c) 2016-9 Netflix, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * Author: Randall Stewart <rrs@netflix.com>
34 #include <sys/types.h>
35 #include <machine/param.h>
37 * Do not change the size unless you know what you are
38 * doing, the current size of 5 is designed around
39 * the cache-line size for an amd64 processor. Other processors
40 * may need other sizes.
42 #define NUM_FILTER_ENTRIES 3
45 uint64_t value; /* Value */
46 uint32_t time_up; /* Time updated */
49 struct filter_entry_small {
50 uint32_t value; /* Value */
51 uint32_t time_up; /* Time updated */
55 uint32_t cur_time_limit;
56 struct filter_entry entries[NUM_FILTER_ENTRIES];
58 } __aligned(CACHE_LINE_SIZE);
62 struct time_filter_small {
63 uint32_t cur_time_limit;
64 struct filter_entry_small entries[NUM_FILTER_ENTRIES];
68 * To conserve on space there is a code duplication here (this
69 * is where polymophism would be nice in the kernel). Everything
70 * is duplicated to have a filter with a value of uint32_t instead
71 * of a uint64_t. This saves 20 bytes and the structure size
72 * drops to 44 from 64. The bad part about this is you end
73 * up with two sets of functions. The xxx_small() access
74 * the uint32_t value's where the xxx() the uint64_t values.
75 * This forces the user to keep straight which type of structure
76 * they allocated and which call they need to make. crossing
77 * over calls will create either invalid memory references or
81 #define FILTER_TYPE_MIN 1
82 #define FILTER_TYPE_MAX 2
85 int setup_time_filter(struct time_filter *tf, int fil_type, uint32_t time_len);
86 void reset_time(struct time_filter *tf, uint32_t time_len);
87 void forward_filter_clock(struct time_filter *tf, uint32_t ticks_forward);
88 void tick_filter_clock(struct time_filter *tf, uint32_t now);
89 uint32_t apply_filter_min(struct time_filter *tf, uint64_t value, uint32_t now);
90 uint32_t apply_filter_max(struct time_filter *tf, uint64_t value, uint32_t now);
91 void filter_reduce_by(struct time_filter *tf, uint64_t reduce_by, uint32_t now);
92 void filter_increase_by(struct time_filter *tf, uint64_t incr_by, uint32_t now);
93 static uint64_t inline
94 get_filter_value(struct time_filter *tf)
96 return(tf->entries[0].value);
99 static uint32_t inline
100 get_cur_timelim(struct time_filter *tf)
102 return(tf->cur_time_limit);
105 int setup_time_filter_small(struct time_filter_small *tf,
106 int fil_type, uint32_t time_len);
107 void reset_time_small(struct time_filter_small *tf, uint32_t time_len);
108 void forward_filter_clock_small(struct time_filter_small *tf,
109 uint32_t ticks_forward);
110 void tick_filter_clock_small(struct time_filter_small *tf, uint32_t now);
111 uint32_t apply_filter_min_small(struct time_filter_small *tf,
112 uint32_t value, uint32_t now);
113 uint32_t apply_filter_max_small(struct time_filter_small *tf,
114 uint32_t value, uint32_t now);
115 void filter_reduce_by_small(struct time_filter_small *tf,
116 uint32_t reduce_by, uint32_t now);
117 void filter_increase_by_small(struct time_filter_small *tf,
118 uint32_t incr_by, uint32_t now);
119 static uint64_t inline
120 get_filter_value_small(struct time_filter_small *tf)
122 return(tf->entries[0].value);
125 static uint32_t inline
126 get_cur_timelim_small(struct time_filter_small *tf)
128 return(tf->cur_time_limit);