xref: /openbmc/linux/include/linux/dim.h (revision 4e95bc26)
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
3 
4 #ifndef DIM_H
5 #define DIM_H
6 
7 #include <linux/module.h>
8 
9 /**
10  * Number of events between DIM iterations.
11  * Causes a moderation of the algorithm run.
12  */
13 #define DIM_NEVENTS 64
14 
15 /**
16  * Is a difference between values justifies taking an action.
17  * We consider 10% difference as significant.
18  */
19 #define IS_SIGNIFICANT_DIFF(val, ref) \
20 	(((100UL * abs((val) - (ref))) / (ref)) > 10)
21 
22 /**
23  * Calculate the gap between two values.
24  * Take wrap-around and variable size into consideration.
25  */
26 #define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) \
27 		& (BIT_ULL(bits) - 1))
28 
29 /**
30  * Structure for CQ moderation values.
31  * Used for communications between DIM and its consumer.
32  *
33  * @usec: CQ timer suggestion (by DIM)
34  * @pkts: CQ packet counter suggestion (by DIM)
35  * @cq_period_mode: CQ priod count mode (from CQE/EQE)
36  */
37 struct dim_cq_moder {
38 	u16 usec;
39 	u16 pkts;
40 	u16 comps;
41 	u8 cq_period_mode;
42 };
43 
44 /**
45  * Structure for DIM sample data.
46  * Used for communications between DIM and its consumer.
47  *
48  * @time: Sample timestamp
49  * @pkt_ctr: Number of packets
50  * @byte_ctr: Number of bytes
51  * @event_ctr: Number of events
52  */
53 struct dim_sample {
54 	ktime_t time;
55 	u32 pkt_ctr;
56 	u32 byte_ctr;
57 	u16 event_ctr;
58 	u32 comp_ctr;
59 };
60 
61 /**
62  * Structure for DIM stats.
63  * Used for holding current measured rates.
64  *
65  * @ppms: Packets per msec
66  * @bpms: Bytes per msec
67  * @epms: Events per msec
68  */
69 struct dim_stats {
70 	int ppms; /* packets per msec */
71 	int bpms; /* bytes per msec */
72 	int epms; /* events per msec */
73 	int cpms; /* completions per msec */
74 	int cpe_ratio; /* ratio of completions to events */
75 };
76 
77 /**
78  * Main structure for dynamic interrupt moderation (DIM).
79  * Used for holding all information about a specific DIM instance.
80  *
81  * @state: Algorithm state (see below)
82  * @prev_stats: Measured rates from previous iteration (for comparison)
83  * @start_sample: Sampled data at start of current iteration
84  * @work: Work to perform on action required
85  * @profile_ix: Current moderation profile
86  * @mode: CQ period count mode
87  * @tune_state: Algorithm tuning state (see below)
88  * @steps_right: Number of steps taken towards higher moderation
89  * @steps_left: Number of steps taken towards lower moderation
90  * @tired: Parking depth counter
91  */
92 struct dim {
93 	u8 state;
94 	struct dim_stats prev_stats;
95 	struct dim_sample start_sample;
96 	struct dim_sample measuring_sample;
97 	struct work_struct work;
98 	u8 profile_ix;
99 	u8 mode;
100 	u8 tune_state;
101 	u8 steps_right;
102 	u8 steps_left;
103 	u8 tired;
104 };
105 
106 /**
107  * enum dim_cq_period_mode
108  *
109  * These are the modes for CQ period count.
110  *
111  * @DIM_CQ_PERIOD_MODE_START_FROM_EQE: Start counting from EQE
112  * @DIM_CQ_PERIOD_MODE_START_FROM_CQE: Start counting from CQE (implies timer reset)
113  * @DIM_CQ_PERIOD_NUM_MODES: Number of modes
114  */
115 enum {
116 	DIM_CQ_PERIOD_MODE_START_FROM_EQE = 0x0,
117 	DIM_CQ_PERIOD_MODE_START_FROM_CQE = 0x1,
118 	DIM_CQ_PERIOD_NUM_MODES
119 };
120 
121 /**
122  * enum dim_state
123  *
124  * These are the DIM algorithm states.
125  * These will determine if the algorithm is in a valid state to start an iteration.
126  *
127  * @DIM_START_MEASURE: This is the first iteration (also after applying a new profile)
128  * @DIM_MEASURE_IN_PROGRESS: Algorithm is already in progress - check if
129  * need to perform an action
130  * @DIM_APPLY_NEW_PROFILE: DIM consumer is currently applying a profile - no need to measure
131  */
132 enum {
133 	DIM_START_MEASURE,
134 	DIM_MEASURE_IN_PROGRESS,
135 	DIM_APPLY_NEW_PROFILE,
136 };
137 
138 /**
139  * enum dim_tune_state
140  *
141  * These are the DIM algorithm tune states.
142  * These will determine which action the algorithm should perform.
143  *
144  * @DIM_PARKING_ON_TOP: Algorithm found a local top point - exit on significant difference
145  * @DIM_PARKING_TIRED: Algorithm found a deep top point - don't exit if tired > 0
146  * @DIM_GOING_RIGHT: Algorithm is currently trying higher moderation levels
147  * @DIM_GOING_LEFT: Algorithm is currently trying lower moderation levels
148  */
149 enum {
150 	DIM_PARKING_ON_TOP,
151 	DIM_PARKING_TIRED,
152 	DIM_GOING_RIGHT,
153 	DIM_GOING_LEFT,
154 };
155 
156 /**
157  * enum dim_stats_state
158  *
159  * These are the DIM algorithm statistics states.
160  * These will determine the verdict of current iteration.
161  *
162  * @DIM_STATS_WORSE: Current iteration shows worse performance than before
163  * @DIM_STATS_WORSE: Current iteration shows same performance than before
164  * @DIM_STATS_WORSE: Current iteration shows better performance than before
165  */
166 enum {
167 	DIM_STATS_WORSE,
168 	DIM_STATS_SAME,
169 	DIM_STATS_BETTER,
170 };
171 
172 /**
173  * enum dim_step_result
174  *
175  * These are the DIM algorithm step results.
176  * These describe the result of a step.
177  *
178  * @DIM_STEPPED: Performed a regular step
179  * @DIM_TOO_TIRED: Same kind of step was done multiple times - should go to
180  * tired parking
181  * @DIM_ON_EDGE: Stepped to the most left/right profile
182  */
183 enum {
184 	DIM_STEPPED,
185 	DIM_TOO_TIRED,
186 	DIM_ON_EDGE,
187 };
188 
189 /**
190  *	dim_on_top - check if current state is a good place to stop (top location)
191  *	@dim: DIM context
192  *
193  * Check if current profile is a good place to park at.
194  * This will result in reducing the DIM checks frequency as we assume we
195  * shouldn't probably change profiles, unless traffic pattern wasn't changed.
196  */
197 bool dim_on_top(struct dim *dim);
198 
199 /**
200  *	dim_turn - change profile alterning direction
201  *	@dim: DIM context
202  *
203  * Go left if we were going right and vice-versa.
204  * Do nothing if currently parking.
205  */
206 void dim_turn(struct dim *dim);
207 
208 /**
209  *	dim_park_on_top - enter a parking state on a top location
210  *	@dim: DIM context
211  *
212  * Enter parking state.
213  * Clear all movement history.
214  */
215 void dim_park_on_top(struct dim *dim);
216 
217 /**
218  *	dim_park_tired - enter a tired parking state
219  *	@dim: DIM context
220  *
221  * Enter parking state.
222  * Clear all movement history and cause DIM checks frequency to reduce.
223  */
224 void dim_park_tired(struct dim *dim);
225 
226 /**
227  *	dim_calc_stats - calculate the difference between two samples
228  *	@start: start sample
229  *	@end: end sample
230  *	@curr_stats: delta between samples
231  *
232  * Calculate the delta between two samples (in data rates).
233  * Takes into consideration counter wrap-around.
234  */
235 void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
236 		    struct dim_stats *curr_stats);
237 
238 /**
239  *	dim_update_sample - set a sample's fields with give values
240  *	@event_ctr: number of events to set
241  *	@packets: number of packets to set
242  *	@bytes: number of bytes to set
243  *	@s: DIM sample
244  */
245 static inline void
246 dim_update_sample(u16 event_ctr, u64 packets, u64 bytes, struct dim_sample *s)
247 {
248 	s->time	     = ktime_get();
249 	s->pkt_ctr   = packets;
250 	s->byte_ctr  = bytes;
251 	s->event_ctr = event_ctr;
252 }
253 
254 /**
255  *	dim_update_sample_with_comps - set a sample's fields with given
256  *	values including the completion parameter
257  *	@event_ctr: number of events to set
258  *	@packets: number of packets to set
259  *	@bytes: number of bytes to set
260  *	@comps: number of completions to set
261  *	@s: DIM sample
262  */
263 static inline void
264 dim_update_sample_with_comps(u16 event_ctr, u64 packets, u64 bytes, u64 comps,
265 			     struct dim_sample *s)
266 {
267 	dim_update_sample(event_ctr, packets, bytes, s);
268 	s->comp_ctr = comps;
269 }
270 
271 /* Net DIM */
272 
273 /*
274  * Net DIM profiles:
275  *        There are different set of profiles for each CQ period mode.
276  *        There are different set of profiles for RX/TX CQs.
277  *        Each profile size must be of NET_DIM_PARAMS_NUM_PROFILES
278  */
279 #define NET_DIM_PARAMS_NUM_PROFILES 5
280 #define NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE 256
281 #define NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE 128
282 #define NET_DIM_DEF_PROFILE_CQE 1
283 #define NET_DIM_DEF_PROFILE_EQE 1
284 
285 #define NET_DIM_RX_EQE_PROFILES { \
286 	{1,   NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
287 	{8,   NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
288 	{64,  NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
289 	{128, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
290 	{256, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
291 }
292 
293 #define NET_DIM_RX_CQE_PROFILES { \
294 	{2,  256},             \
295 	{8,  128},             \
296 	{16, 64},              \
297 	{32, 64},              \
298 	{64, 64}               \
299 }
300 
301 #define NET_DIM_TX_EQE_PROFILES { \
302 	{1,   NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE},  \
303 	{8,   NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE},  \
304 	{32,  NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE},  \
305 	{64,  NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE},  \
306 	{128, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}   \
307 }
308 
309 #define NET_DIM_TX_CQE_PROFILES { \
310 	{5,  128},  \
311 	{8,  64},  \
312 	{16, 32},  \
313 	{32, 32},  \
314 	{64, 32}   \
315 }
316 
317 static const struct dim_cq_moder
318 rx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
319 	NET_DIM_RX_EQE_PROFILES,
320 	NET_DIM_RX_CQE_PROFILES,
321 };
322 
323 static const struct dim_cq_moder
324 tx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
325 	NET_DIM_TX_EQE_PROFILES,
326 	NET_DIM_TX_CQE_PROFILES,
327 };
328 
329 /**
330  *	net_dim_get_rx_moderation - provide a CQ moderation object for the given RX profile
331  *	@cq_period_mode: CQ period mode
332  *	@ix: Profile index
333  */
334 struct dim_cq_moder net_dim_get_rx_moderation(u8 cq_period_mode, int ix);
335 
336 /**
337  *	net_dim_get_def_rx_moderation - provide the default RX moderation
338  *	@cq_period_mode: CQ period mode
339  */
340 struct dim_cq_moder net_dim_get_def_rx_moderation(u8 cq_period_mode);
341 
342 /**
343  *	net_dim_get_tx_moderation - provide a CQ moderation object for the given TX profile
344  *	@cq_period_mode: CQ period mode
345  *	@ix: Profile index
346  */
347 struct dim_cq_moder net_dim_get_tx_moderation(u8 cq_period_mode, int ix);
348 
349 /**
350  *	net_dim_get_def_tx_moderation - provide the default TX moderation
351  *	@cq_period_mode: CQ period mode
352  */
353 struct dim_cq_moder net_dim_get_def_tx_moderation(u8 cq_period_mode);
354 
355 /**
356  *	net_dim - main DIM algorithm entry point
357  *	@dim: DIM instance information
358  *	@end_sample: Current data measurement
359  *
360  * Called by the consumer.
361  * This is the main logic of the algorithm, where data is processed in order to decide on next
362  * required action.
363  */
364 void net_dim(struct dim *dim, struct dim_sample end_sample);
365 
366 #endif /* DIM_H */
367