Mattias Nissler | 12446c6 | 2007-12-19 01:27:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #ifndef RC80211_PID_H |
| 10 | #define RC80211_PID_H |
| 11 | |
| 12 | /* Sampling period for measuring percentage of failed frames. */ |
| 13 | #define RC_PID_INTERVAL (HZ / 8) |
| 14 | |
| 15 | /* Exponential averaging smoothness (used for I part of PID controller) */ |
| 16 | #define RC_PID_SMOOTHING_SHIFT 3 |
| 17 | #define RC_PID_SMOOTHING (1 << RC_PID_SMOOTHING_SHIFT) |
| 18 | |
| 19 | /* Sharpening factor (used for D part of PID controller) */ |
| 20 | #define RC_PID_SHARPENING_FACTOR 0 |
| 21 | #define RC_PID_SHARPENING_DURATION 0 |
| 22 | |
| 23 | /* Fixed point arithmetic shifting amount. */ |
| 24 | #define RC_PID_ARITH_SHIFT 8 |
| 25 | |
| 26 | /* Fixed point arithmetic factor. */ |
| 27 | #define RC_PID_ARITH_FACTOR (1 << RC_PID_ARITH_SHIFT) |
| 28 | |
| 29 | /* Proportional PID component coefficient. */ |
| 30 | #define RC_PID_COEFF_P 15 |
| 31 | /* Integral PID component coefficient. */ |
| 32 | #define RC_PID_COEFF_I 9 |
| 33 | /* Derivative PID component coefficient. */ |
| 34 | #define RC_PID_COEFF_D 15 |
| 35 | |
| 36 | /* Target failed frames rate for the PID controller. NB: This effectively gives |
| 37 | * maximum failed frames percentage we're willing to accept. If the wireless |
| 38 | * link quality is good, the controller will fail to adjust failed frames |
| 39 | * percentage to the target. This is intentional. |
| 40 | */ |
Stefano Brivio | 426706c | 2007-12-23 04:39:17 +0100 | [diff] [blame^] | 41 | #define RC_PID_TARGET_PF 11 |
Mattias Nissler | 12446c6 | 2007-12-19 01:27:18 +0100 | [diff] [blame] | 42 | |
| 43 | /* Rate behaviour normalization quantity over time. */ |
| 44 | #define RC_PID_NORM_OFFSET 3 |
| 45 | |
| 46 | /* Push high rates right after loading. */ |
| 47 | #define RC_PID_FAST_START 0 |
| 48 | |
| 49 | /* Arithmetic right shift for positive and negative values for ISO C. */ |
| 50 | #define RC_PID_DO_ARITH_RIGHT_SHIFT(x, y) \ |
| 51 | (x) < 0 ? -((-(x)) >> (y)) : (x) >> (y) |
| 52 | |
| 53 | enum rc_pid_event_type { |
| 54 | RC_PID_EVENT_TYPE_TX_STATUS, |
| 55 | RC_PID_EVENT_TYPE_RATE_CHANGE, |
| 56 | RC_PID_EVENT_TYPE_TX_RATE, |
| 57 | RC_PID_EVENT_TYPE_PF_SAMPLE, |
| 58 | }; |
| 59 | |
| 60 | union rc_pid_event_data { |
| 61 | /* RC_PID_EVENT_TX_STATUS */ |
| 62 | struct { |
| 63 | struct ieee80211_tx_status tx_status; |
| 64 | }; |
| 65 | /* RC_PID_EVENT_TYPE_RATE_CHANGE */ |
| 66 | /* RC_PID_EVENT_TYPE_TX_RATE */ |
| 67 | struct { |
| 68 | int index; |
| 69 | int rate; |
| 70 | }; |
| 71 | /* RC_PID_EVENT_TYPE_PF_SAMPLE */ |
| 72 | struct { |
| 73 | s32 pf_sample; |
| 74 | s32 prop_err; |
| 75 | s32 int_err; |
| 76 | s32 der_err; |
| 77 | }; |
| 78 | }; |
| 79 | |
| 80 | struct rc_pid_event { |
| 81 | /* The time when the event occured */ |
| 82 | unsigned long timestamp; |
| 83 | |
| 84 | /* Event ID number */ |
| 85 | unsigned int id; |
| 86 | |
| 87 | /* Type of event */ |
| 88 | enum rc_pid_event_type type; |
| 89 | |
| 90 | /* type specific data */ |
| 91 | union rc_pid_event_data data; |
| 92 | }; |
| 93 | |
| 94 | /* Size of the event ring buffer. */ |
| 95 | #define RC_PID_EVENT_RING_SIZE 32 |
| 96 | |
| 97 | struct rc_pid_event_buffer { |
| 98 | /* Counter that generates event IDs */ |
| 99 | unsigned int ev_count; |
| 100 | |
| 101 | /* Ring buffer of events */ |
| 102 | struct rc_pid_event ring[RC_PID_EVENT_RING_SIZE]; |
| 103 | |
| 104 | /* Index to the entry in events_buf to be reused */ |
| 105 | unsigned int next_entry; |
| 106 | |
| 107 | /* Lock that guards against concurrent access to this buffer struct */ |
| 108 | spinlock_t lock; |
| 109 | |
| 110 | /* Wait queue for poll/select and blocking I/O */ |
| 111 | wait_queue_head_t waitqueue; |
| 112 | }; |
| 113 | |
| 114 | struct rc_pid_events_file_info { |
| 115 | /* The event buffer we read */ |
| 116 | struct rc_pid_event_buffer *events; |
| 117 | |
| 118 | /* The entry we have should read next */ |
| 119 | unsigned int next_entry; |
| 120 | }; |
| 121 | |
Mattias Nissler | 1946b74 | 2007-12-20 13:27:26 +0100 | [diff] [blame] | 122 | struct rc_pid_debugfs_entries { |
| 123 | struct dentry *dir; |
| 124 | struct dentry *target; |
| 125 | struct dentry *sampling_period; |
| 126 | struct dentry *coeff_p; |
| 127 | struct dentry *coeff_i; |
| 128 | struct dentry *coeff_d; |
| 129 | struct dentry *smoothing_shift; |
| 130 | struct dentry *sharpen_factor; |
| 131 | struct dentry *sharpen_duration; |
| 132 | struct dentry *norm_offset; |
| 133 | struct dentry *fast_start; |
| 134 | }; |
| 135 | |
Mattias Nissler | 12446c6 | 2007-12-19 01:27:18 +0100 | [diff] [blame] | 136 | void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf, |
| 137 | struct ieee80211_tx_status *stat); |
| 138 | |
| 139 | void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf, |
| 140 | int index, int rate); |
| 141 | |
| 142 | void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf, |
| 143 | int index, int rate); |
| 144 | |
| 145 | void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf, |
| 146 | s32 pf_sample, s32 prop_err, |
| 147 | s32 int_err, s32 der_err); |
| 148 | |
| 149 | void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta, |
| 150 | struct dentry *dir); |
| 151 | |
| 152 | void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta); |
| 153 | |
| 154 | struct rc_pid_sta_info { |
| 155 | unsigned long last_change; |
| 156 | unsigned long last_sample; |
| 157 | |
| 158 | u32 tx_num_failed; |
| 159 | u32 tx_num_xmit; |
| 160 | |
| 161 | /* Average failed frames percentage error (i.e. actual vs. target |
| 162 | * percentage), scaled by RC_PID_SMOOTHING. This value is computed |
| 163 | * using using an exponential weighted average technique: |
| 164 | * |
| 165 | * (RC_PID_SMOOTHING - 1) * err_avg_old + err |
| 166 | * err_avg = ------------------------------------------ |
| 167 | * RC_PID_SMOOTHING |
| 168 | * |
| 169 | * where err_avg is the new approximation, err_avg_old the previous one |
| 170 | * and err is the error w.r.t. to the current failed frames percentage |
| 171 | * sample. Note that the bigger RC_PID_SMOOTHING the more weight is |
| 172 | * given to the previous estimate, resulting in smoother behavior (i.e. |
| 173 | * corresponding to a longer integration window). |
| 174 | * |
| 175 | * For computation, we actually don't use the above formula, but this |
| 176 | * one: |
| 177 | * |
| 178 | * err_avg_scaled = err_avg_old_scaled - err_avg_old + err |
| 179 | * |
| 180 | * where: |
| 181 | * err_avg_scaled = err * RC_PID_SMOOTHING |
| 182 | * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING |
| 183 | * |
| 184 | * This avoids floating point numbers and the per_failed_old value can |
| 185 | * easily be obtained by shifting per_failed_old_scaled right by |
| 186 | * RC_PID_SMOOTHING_SHIFT. |
| 187 | */ |
| 188 | s32 err_avg_sc; |
| 189 | |
| 190 | /* Last framed failes percentage sample. */ |
| 191 | u32 last_pf; |
| 192 | |
| 193 | /* Sharpening needed. */ |
| 194 | u8 sharp_cnt; |
| 195 | |
| 196 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 197 | /* Event buffer */ |
| 198 | struct rc_pid_event_buffer events; |
| 199 | |
| 200 | /* Events debugfs file entry */ |
| 201 | struct dentry *events_entry; |
| 202 | #endif |
| 203 | }; |
| 204 | |
| 205 | /* Algorithm parameters. We keep them on a per-algorithm approach, so they can |
| 206 | * be tuned individually for each interface. |
| 207 | */ |
| 208 | struct rc_pid_rateinfo { |
| 209 | |
| 210 | /* Map sorted rates to rates in ieee80211_hw_mode. */ |
| 211 | int index; |
| 212 | |
| 213 | /* Map rates in ieee80211_hw_mode to sorted rates. */ |
| 214 | int rev_index; |
| 215 | |
| 216 | /* Did we do any measurement on this rate? */ |
| 217 | bool valid; |
| 218 | |
| 219 | /* Comparison with the lowest rate. */ |
| 220 | int diff; |
| 221 | }; |
| 222 | |
| 223 | struct rc_pid_info { |
| 224 | |
| 225 | /* The failed frames percentage target. */ |
| 226 | unsigned int target; |
| 227 | |
| 228 | /* Rate at which failed frames percentage is sampled in 0.001s. */ |
| 229 | unsigned int sampling_period; |
| 230 | |
| 231 | /* P, I and D coefficients. */ |
| 232 | int coeff_p; |
| 233 | int coeff_i; |
| 234 | int coeff_d; |
| 235 | |
| 236 | /* Exponential averaging shift. */ |
| 237 | unsigned int smoothing_shift; |
| 238 | |
Mattias Nissler | 1946b74 | 2007-12-20 13:27:26 +0100 | [diff] [blame] | 239 | /* Sharpening factor and duration. */ |
| 240 | unsigned int sharpen_factor; |
Mattias Nissler | 12446c6 | 2007-12-19 01:27:18 +0100 | [diff] [blame] | 241 | unsigned int sharpen_duration; |
| 242 | |
| 243 | /* Normalization offset. */ |
| 244 | unsigned int norm_offset; |
| 245 | |
| 246 | /* Fast starst parameter. */ |
| 247 | unsigned int fast_start; |
| 248 | |
| 249 | /* Rates information. */ |
| 250 | struct rc_pid_rateinfo *rinfo; |
| 251 | |
| 252 | /* Index of the last used rate. */ |
| 253 | int oldrate; |
Mattias Nissler | 1946b74 | 2007-12-20 13:27:26 +0100 | [diff] [blame] | 254 | |
| 255 | #ifdef CONFIG_MAC80211_DEBUGFS |
| 256 | /* Debugfs entries created for the parameters above. */ |
| 257 | struct rc_pid_debugfs_entries dentries; |
| 258 | #endif |
Mattias Nissler | 12446c6 | 2007-12-19 01:27:18 +0100 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | #endif /* RC80211_PID_H */ |