1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
7 #include <linux/bits.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/workqueue.h>
14 * Number of events between DIM iterations.
15 * Causes a moderation of the algorithm run.
17 #define DIM_NEVENTS 64
20 * Is a difference between values justifies taking an action.
21 * We consider 10% difference as significant.
23 #define IS_SIGNIFICANT_DIFF(val, ref) \
24 ((ref) && (((100UL * abs((val) - (ref))) / (ref)) > 10))
27 * Calculate the gap between two values.
28 * Take wrap-around and variable size into consideration.
30 #define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) \
31 & (BIT_ULL(bits) - 1))
34 * struct dim_cq_moder - Structure for CQ moderation values.
35 * Used for communications between DIM and its consumer.
37 * @usec: CQ timer suggestion (by DIM)
38 * @pkts: CQ packet counter suggestion (by DIM)
39 * @comps: Completion counter
40 * @cq_period_mode: CQ period count mode (from CQE/EQE)
50 * struct dim_sample - Structure for DIM sample data.
51 * Used for communications between DIM and its consumer.
53 * @time: Sample timestamp
54 * @pkt_ctr: Number of packets
55 * @byte_ctr: Number of bytes
56 * @event_ctr: Number of events
57 * @comp_ctr: Current completion counter
68 * struct dim_stats - Structure for DIM stats.
69 * Used for holding current measured rates.
71 * @ppms: Packets per msec
72 * @bpms: Bytes per msec
73 * @epms: Events per msec
74 * @cpms: Completions per msec
75 * @cpe_ratio: Ratio of completions to events
78 int ppms; /* packets per msec */
79 int bpms; /* bytes per msec */
80 int epms; /* events per msec */
81 int cpms; /* completions per msec */
82 int cpe_ratio; /* ratio of completions to events */
86 * struct dim - Main structure for dynamic interrupt moderation (DIM).
87 * Used for holding all information about a specific DIM instance.
89 * @state: Algorithm state (see below)
90 * @prev_stats: Measured rates from previous iteration (for comparison)
91 * @start_sample: Sampled data at start of current iteration
92 * @measuring_sample: A &dim_sample that is used to update the current events
93 * @work: Work to perform on action required
94 * @priv: A pointer to the struct that points to dim
95 * @profile_ix: Current moderation profile
96 * @mode: CQ period count mode
97 * @tune_state: Algorithm tuning state (see below)
98 * @steps_right: Number of steps taken towards higher moderation
99 * @steps_left: Number of steps taken towards lower moderation
100 * @tired: Parking depth counter
104 struct dim_stats prev_stats;
105 struct dim_sample start_sample;
106 struct dim_sample measuring_sample;
107 struct work_struct work;
118 * enum dim_cq_period_mode - Modes for CQ period count
120 * @DIM_CQ_PERIOD_MODE_START_FROM_EQE: Start counting from EQE
121 * @DIM_CQ_PERIOD_MODE_START_FROM_CQE: Start counting from CQE (implies timer reset)
122 * @DIM_CQ_PERIOD_NUM_MODES: Number of modes
124 enum dim_cq_period_mode {
125 DIM_CQ_PERIOD_MODE_START_FROM_EQE = 0x0,
126 DIM_CQ_PERIOD_MODE_START_FROM_CQE = 0x1,
127 DIM_CQ_PERIOD_NUM_MODES
131 * enum dim_state - DIM algorithm states
133 * These will determine if the algorithm is in a valid state to start an iteration.
135 * @DIM_START_MEASURE: This is the first iteration (also after applying a new profile)
136 * @DIM_MEASURE_IN_PROGRESS: Algorithm is already in progress - check if
137 * need to perform an action
138 * @DIM_APPLY_NEW_PROFILE: DIM consumer is currently applying a profile - no need to measure
142 DIM_MEASURE_IN_PROGRESS,
143 DIM_APPLY_NEW_PROFILE,
147 * enum dim_tune_state - DIM algorithm tune states
149 * These will determine which action the algorithm should perform.
151 * @DIM_PARKING_ON_TOP: Algorithm found a local top point - exit on significant difference
152 * @DIM_PARKING_TIRED: Algorithm found a deep top point - don't exit if tired > 0
153 * @DIM_GOING_RIGHT: Algorithm is currently trying higher moderation levels
154 * @DIM_GOING_LEFT: Algorithm is currently trying lower moderation levels
156 enum dim_tune_state {
164 * enum dim_stats_state - DIM algorithm statistics states
166 * These will determine the verdict of current iteration.
168 * @DIM_STATS_WORSE: Current iteration shows worse performance than before
169 * @DIM_STATS_SAME: Current iteration shows same performance than before
170 * @DIM_STATS_BETTER: Current iteration shows better performance than before
172 enum dim_stats_state {
179 * enum dim_step_result - DIM algorithm step results
181 * These describe the result of a step.
183 * @DIM_STEPPED: Performed a regular step
184 * @DIM_TOO_TIRED: Same kind of step was done multiple times - should go to
186 * @DIM_ON_EDGE: Stepped to the most left/right profile
188 enum dim_step_result {
195 * dim_on_top - check if current state is a good place to stop (top location)
198 * Check if current profile is a good place to park at.
199 * This will result in reducing the DIM checks frequency as we assume we
200 * shouldn't probably change profiles, unless traffic pattern wasn't changed.
202 bool dim_on_top(struct dim *dim);
205 * dim_turn - change profile altering direction
208 * Go left if we were going right and vice-versa.
209 * Do nothing if currently parking.
211 void dim_turn(struct dim *dim);
214 * dim_park_on_top - enter a parking state on a top location
217 * Enter parking state.
218 * Clear all movement history.
220 void dim_park_on_top(struct dim *dim);
223 * dim_park_tired - enter a tired parking state
226 * Enter parking state.
227 * Clear all movement history and cause DIM checks frequency to reduce.
229 void dim_park_tired(struct dim *dim);
232 * dim_calc_stats - calculate the difference between two samples
233 * @start: start sample
235 * @curr_stats: delta between samples
237 * Calculate the delta between two samples (in data rates).
238 * Takes into consideration counter wrap-around.
239 * Returned boolean indicates whether curr_stats are reliable.
241 bool dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
242 struct dim_stats *curr_stats);
245 * dim_update_sample - set a sample's fields with given values
246 * @event_ctr: number of events to set
247 * @packets: number of packets to set
248 * @bytes: number of bytes to set
252 dim_update_sample(u16 event_ctr, u64 packets, u64 bytes, struct dim_sample *s)
254 s->time = ktime_get();
255 s->pkt_ctr = packets;
257 s->event_ctr = event_ctr;
261 * dim_update_sample_with_comps - set a sample's fields with given
262 * values including the completion parameter
263 * @event_ctr: number of events to set
264 * @packets: number of packets to set
265 * @bytes: number of bytes to set
266 * @comps: number of completions to set
270 dim_update_sample_with_comps(u16 event_ctr, u64 packets, u64 bytes, u64 comps,
271 struct dim_sample *s)
273 dim_update_sample(event_ctr, packets, bytes, s);
280 * net_dim_get_rx_moderation - provide a CQ moderation object for the given RX profile
281 * @cq_period_mode: CQ period mode
284 struct dim_cq_moder net_dim_get_rx_moderation(u8 cq_period_mode, int ix);
287 * net_dim_get_def_rx_moderation - provide the default RX moderation
288 * @cq_period_mode: CQ period mode
290 struct dim_cq_moder net_dim_get_def_rx_moderation(u8 cq_period_mode);
293 * net_dim_get_tx_moderation - provide a CQ moderation object for the given TX profile
294 * @cq_period_mode: CQ period mode
297 struct dim_cq_moder net_dim_get_tx_moderation(u8 cq_period_mode, int ix);
300 * net_dim_get_def_tx_moderation - provide the default TX moderation
301 * @cq_period_mode: CQ period mode
303 struct dim_cq_moder net_dim_get_def_tx_moderation(u8 cq_period_mode);
306 * net_dim - main DIM algorithm entry point
307 * @dim: DIM instance information
308 * @end_sample: Current data measurement
310 * Called by the consumer.
311 * This is the main logic of the algorithm, where data is processed in order
312 * to decide on next required action.
314 void net_dim(struct dim *dim, struct dim_sample end_sample);
320 * profile size must be of RDMA_DIM_PARAMS_NUM_PROFILES.
322 #define RDMA_DIM_PARAMS_NUM_PROFILES 9
323 #define RDMA_DIM_START_PROFILE 0
326 * rdma_dim - Runs the adaptive moderation.
327 * @dim: The moderation struct.
328 * @completions: The number of completions collected in this round.
330 * Each call to rdma_dim takes the latest amount of completions that
331 * have been collected and counts them as a new event.
332 * Once enough events have been collected the algorithm decides a new
335 void rdma_dim(struct dim *dim, u64 completions);