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 * $FreeBSD$ 29 */ 30 /* 31 * Author: Randall Stewart <rrs@netflix.com> 32 */ 33 34 #include <sys/types.h> 35 #include <machine/param.h> 36 /* 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. 41 */ 42 #define NUM_FILTER_ENTRIES 3 43 44 struct filter_entry { 45 uint64_t value; /* Value */ 46 uint32_t time_up; /* Time updated */ 47 } __packed ; 48 49 struct filter_entry_small { 50 uint32_t value; /* Value */ 51 uint32_t time_up; /* Time updated */ 52 }; 53 54 struct time_filter { 55 uint32_t cur_time_limit; 56 struct filter_entry entries[NUM_FILTER_ENTRIES]; 57 #ifdef _KERNEL 58 } __aligned(CACHE_LINE_SIZE); 59 #else 60 }; 61 #endif 62 struct time_filter_small { 63 uint32_t cur_time_limit; 64 struct filter_entry_small entries[NUM_FILTER_ENTRIES]; 65 }; 66 67 /* 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 78 * very bad results :) 79 */ 80 81 #define FILTER_TYPE_MIN 1 82 #define FILTER_TYPE_MAX 2 83 84 #ifdef _KERNEL 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) 95 { 96 return(tf->entries[0].value); 97 } 98 99 static uint32_t inline 100 get_cur_timelim(struct time_filter *tf) 101 { 102 return(tf->cur_time_limit); 103 } 104 105 106 int setup_time_filter_small(struct time_filter_small *tf, 107 int fil_type, uint32_t time_len); 108 void reset_time_small(struct time_filter_small *tf, uint32_t time_len); 109 void forward_filter_clock_small(struct time_filter_small *tf, 110 uint32_t ticks_forward); 111 void tick_filter_clock_small(struct time_filter_small *tf, uint32_t now); 112 uint32_t apply_filter_min_small(struct time_filter_small *tf, 113 uint32_t value, uint32_t now); 114 uint32_t apply_filter_max_small(struct time_filter_small *tf, 115 uint32_t value, uint32_t now); 116 void filter_reduce_by_small(struct time_filter_small *tf, 117 uint32_t reduce_by, uint32_t now); 118 void filter_increase_by_small(struct time_filter_small *tf, 119 uint32_t incr_by, uint32_t now); 120 static uint64_t inline 121 get_filter_value_small(struct time_filter_small *tf) 122 { 123 return(tf->entries[0].value); 124 } 125 126 static uint32_t inline 127 get_cur_timelim_small(struct time_filter_small *tf) 128 { 129 return(tf->cur_time_limit); 130 } 131 132 133 #endif 134 #endif 135