xref: /freebsd/sys/cam/cam_iosched.c (revision dd6123ebf02b86cee90b75e54fc755f6c20caebf)
1ba6c22ceSWarner Losh /*-
2ba6c22ceSWarner Losh  * CAM IO Scheduler Interface
3ba6c22ceSWarner Losh  *
4891c6986SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5f24882ecSPedro F. Giffuni  *
6ba6c22ceSWarner Losh  * Copyright (c) 2015 Netflix, Inc.
7ba6c22ceSWarner Losh  *
8ba6c22ceSWarner Losh  * Redistribution and use in source and binary forms, with or without
9ba6c22ceSWarner Losh  * modification, are permitted provided that the following conditions
10ba6c22ceSWarner Losh  * are met:
11ba6c22ceSWarner Losh  * 1. Redistributions of source code must retain the above copyright
12891c6986SWarner Losh  *    notice, this list of conditions and the following disclaimer.
13891c6986SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
14891c6986SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
15891c6986SWarner Losh  *    documentation and/or other materials provided with the distribution.
16ba6c22ceSWarner Losh  *
17ba6c22ceSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18ba6c22ceSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ba6c22ceSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20891c6986SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21891c6986SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ba6c22ceSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ba6c22ceSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ba6c22ceSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ba6c22ceSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ba6c22ceSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ba6c22ceSWarner Losh  * SUCH DAMAGE.
28ba6c22ceSWarner Losh  */
29ba6c22ceSWarner Losh 
30ba6c22ceSWarner Losh #include "opt_ddb.h"
31ba6c22ceSWarner Losh 
32ba6c22ceSWarner Losh #include <sys/param.h>
33ba6c22ceSWarner Losh #include <sys/systm.h>
34ba6c22ceSWarner Losh #include <sys/kernel.h>
35ba6c22ceSWarner Losh #include <sys/bio.h>
36ba6c22ceSWarner Losh #include <sys/lock.h>
37ba6c22ceSWarner Losh #include <sys/malloc.h>
38ba6c22ceSWarner Losh #include <sys/mutex.h>
39cf3ec151SWarner Losh #include <sys/sbuf.h>
40ba6c22ceSWarner Losh #include <sys/sysctl.h>
41ba6c22ceSWarner Losh 
42ba6c22ceSWarner Losh #include <cam/cam.h>
43ba6c22ceSWarner Losh #include <cam/cam_ccb.h>
44ba6c22ceSWarner Losh #include <cam/cam_periph.h>
45ba6c22ceSWarner Losh #include <cam/cam_xpt_periph.h>
46cf3ec151SWarner Losh #include <cam/cam_xpt_internal.h>
47ba6c22ceSWarner Losh #include <cam/cam_iosched.h>
48ba6c22ceSWarner Losh 
49ba6c22ceSWarner Losh #include <ddb/ddb.h>
50ba6c22ceSWarner Losh 
51ba6c22ceSWarner Losh static MALLOC_DEFINE(M_CAMSCHED, "CAM I/O Scheduler",
52ba6c22ceSWarner Losh     "CAM I/O Scheduler buffers");
53ba6c22ceSWarner Losh 
5422832069SWarner Losh static SYSCTL_NODE(_kern_cam, OID_AUTO, iosched, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
5522832069SWarner Losh     "CAM I/O Scheduler parameters");
5622832069SWarner Losh 
57ba6c22ceSWarner Losh /*
58ba6c22ceSWarner Losh  * Default I/O scheduler for FreeBSD. This implementation is just a thin-vineer
59ba6c22ceSWarner Losh  * over the bioq_* interface, with notions of separate calls for normal I/O and
60ba6c22ceSWarner Losh  * for trims.
61035ec48eSWarner Losh  *
62035ec48eSWarner Losh  * When CAM_IOSCHED_DYNAMIC is defined, the scheduler is enhanced to dynamically
63035ec48eSWarner Losh  * steer the rate of one type of traffic to help other types of traffic (eg
64035ec48eSWarner Losh  * limit writes when read latency deteriorates on SSDs).
65ba6c22ceSWarner Losh  */
66ba6c22ceSWarner Losh 
67df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
68ba6c22ceSWarner Losh 
6922832069SWarner Losh static bool do_dynamic_iosched = true;
7069cb72b8SZhenlei Huang SYSCTL_BOOL(_kern_cam_iosched, OID_AUTO, dynamic, CTLFLAG_RDTUN,
71035ec48eSWarner Losh     &do_dynamic_iosched, 1,
72035ec48eSWarner Losh     "Enable Dynamic I/O scheduler optimizations.");
73ba6c22ceSWarner Losh 
74cf3ec151SWarner Losh /*
75cf3ec151SWarner Losh  * For an EMA, with an alpha of alpha, we know
76cf3ec151SWarner Losh  * 	alpha = 2 / (N + 1)
77cf3ec151SWarner Losh  * or
78cf3ec151SWarner Losh  * 	N = 1 + (2 / alpha)
79cf3ec151SWarner Losh  * where N is the number of samples that 86% of the current
80cf3ec151SWarner Losh  * EMA is derived from.
81cf3ec151SWarner Losh  *
82cf3ec151SWarner Losh  * So we invent[*] alpha_bits:
83cf3ec151SWarner Losh  *	alpha_bits = -log_2(alpha)
84cf3ec151SWarner Losh  *	alpha = 2^-alpha_bits
85cf3ec151SWarner Losh  * So
86cf3ec151SWarner Losh  *	N = 1 + 2^(alpha_bits + 1)
87cf3ec151SWarner Losh  *
88cf3ec151SWarner Losh  * The default 9 gives a 1025 lookback for 86% of the data.
89cf3ec151SWarner Losh  * For a brief intro: https://en.wikipedia.org/wiki/Moving_average
90cf3ec151SWarner Losh  *
91cf3ec151SWarner Losh  * [*] Steal from the load average code and many other places.
9279d80af2SWarner Losh  * Note: See computation of EMA and EMVAR for acceptable ranges of alpha.
93cf3ec151SWarner Losh  */
94ba6c22ceSWarner Losh static int alpha_bits = 9;
9569cb72b8SZhenlei Huang SYSCTL_INT(_kern_cam_iosched, OID_AUTO, alpha_bits, CTLFLAG_RWTUN,
96ba6c22ceSWarner Losh     &alpha_bits, 1,
97ba6c22ceSWarner Losh     "Bits in EMA's alpha.");
98ba6c22ceSWarner Losh 
9922832069SWarner Losh /*
10022832069SWarner Losh  * Different parameters for the buckets of latency we keep track of. These are all
10122832069SWarner Losh  * published read-only since at present they are compile time constants.
10222832069SWarner Losh  *
10322832069SWarner Losh  * Bucket base is the upper bounds of the first latency bucket. It's currently 20us.
10422832069SWarner Losh  * With 20 buckets (see below), that leads to a geometric progression with a max size
10522832069SWarner Losh  * of 5.2s which is safeily larger than 1s to help diagnose extreme outliers better.
10622832069SWarner Losh  */
10722832069SWarner Losh #ifndef BUCKET_BASE
1081d92e81fSWarner Losh #define BUCKET_BASE ((SBT_1S / 50000) + 1)	/* 20us */
10922832069SWarner Losh #endif
11022832069SWarner Losh static sbintime_t bucket_base = BUCKET_BASE;
11122832069SWarner Losh SYSCTL_SBINTIME_USEC(_kern_cam_iosched, OID_AUTO, bucket_base_us, CTLFLAG_RD,
11222832069SWarner Losh     &bucket_base,
11322832069SWarner Losh     "Size of the smallest latency bucket");
11422832069SWarner Losh 
11522832069SWarner Losh /*
11622832069SWarner Losh  * Bucket ratio is the geometric progression for the bucket. For a bucket b_n
11722832069SWarner Losh  * the size of bucket b_n+1 is b_n * bucket_ratio / 100.
11822832069SWarner Losh  */
11922832069SWarner Losh static int bucket_ratio = 200;	/* Rather hard coded at the moment */
12022832069SWarner Losh SYSCTL_INT(_kern_cam_iosched, OID_AUTO, bucket_ratio, CTLFLAG_RD,
12122832069SWarner Losh     &bucket_ratio, 200,
12222832069SWarner Losh     "Latency Bucket Ratio for geometric progression.");
12322832069SWarner Losh 
12422832069SWarner Losh /*
12522832069SWarner Losh  * Number of total buckets. Starting at BUCKET_BASE, each one is a power of 2.
12622832069SWarner Losh  */
12722832069SWarner Losh #ifndef LAT_BUCKETS
12822832069SWarner Losh #define LAT_BUCKETS 20	/* < 20us < 40us ... < 2^(n-1)*20us >= 2^(n-1)*20us */
12922832069SWarner Losh #endif
13022832069SWarner Losh static int lat_buckets = LAT_BUCKETS;
13122832069SWarner Losh SYSCTL_INT(_kern_cam_iosched, OID_AUTO, buckets, CTLFLAG_RD,
13222832069SWarner Losh     &lat_buckets, LAT_BUCKETS,
13322832069SWarner Losh     "Total number of latency buckets published");
13422832069SWarner Losh 
135d592c0dbSWarner Losh /*
136d592c0dbSWarner Losh  * Read bias: how many reads do we favor before scheduling a write
137d592c0dbSWarner Losh  * when we have a choice.
138d592c0dbSWarner Losh  */
139d592c0dbSWarner Losh static int default_read_bias = 0;
140d592c0dbSWarner Losh SYSCTL_INT(_kern_cam_iosched, OID_AUTO, read_bias, CTLFLAG_RWTUN,
141d592c0dbSWarner Losh     &default_read_bias, 0,
142d592c0dbSWarner Losh     "Default read bias for new devices.");
143d592c0dbSWarner Losh 
144ba6c22ceSWarner Losh struct iop_stats;
145ba6c22ceSWarner Losh struct cam_iosched_softc;
146ba6c22ceSWarner Losh 
147ba6c22ceSWarner Losh int iosched_debug = 0;
148ba6c22ceSWarner Losh 
149ba6c22ceSWarner Losh typedef enum {
150ba6c22ceSWarner Losh 	none = 0,				/* No limits */
151ba6c22ceSWarner Losh 	queue_depth,			/* Limit how many ops we queue to SIM */
152ba6c22ceSWarner Losh 	iops,				/* Limit # of IOPS to the drive */
153ba6c22ceSWarner Losh 	bandwidth,			/* Limit bandwidth to the drive */
154ba6c22ceSWarner Losh 	limiter_max
155ba6c22ceSWarner Losh } io_limiter;
156ba6c22ceSWarner Losh 
157ba6c22ceSWarner Losh static const char *cam_iosched_limiter_names[] =
158ba6c22ceSWarner Losh     { "none", "queue_depth", "iops", "bandwidth" };
159ba6c22ceSWarner Losh 
160ba6c22ceSWarner Losh /*
161ba6c22ceSWarner Losh  * Called to initialize the bits of the iop_stats structure relevant to the
162ba6c22ceSWarner Losh  * limiter. Called just after the limiter is set.
163ba6c22ceSWarner Losh  */
164ba6c22ceSWarner Losh typedef int l_init_t(struct iop_stats *);
165ba6c22ceSWarner Losh 
166ba6c22ceSWarner Losh /*
167ba6c22ceSWarner Losh  * Called every tick.
168ba6c22ceSWarner Losh  */
169ba6c22ceSWarner Losh typedef int l_tick_t(struct iop_stats *);
170ba6c22ceSWarner Losh 
171ba6c22ceSWarner Losh /*
172ba6c22ceSWarner Losh  * Called to see if the limiter thinks this IOP can be allowed to
1730b4060b0SEd Maste  * proceed. If so, the limiter assumes that the IOP proceeded
174ba6c22ceSWarner Losh  * and makes any accounting of it that's needed.
175ba6c22ceSWarner Losh  */
176ba6c22ceSWarner Losh typedef int l_iop_t(struct iop_stats *, struct bio *);
177ba6c22ceSWarner Losh 
178ba6c22ceSWarner Losh /*
1790b4060b0SEd Maste  * Called when an I/O completes so the limiter can update its
180ba6c22ceSWarner Losh  * accounting. Pending I/Os may complete in any order (even when
181ba6c22ceSWarner Losh  * sent to the hardware at the same time), so the limiter may not
182ba6c22ceSWarner Losh  * make any assumptions other than this I/O has completed. If it
183ba6c22ceSWarner Losh  * returns 1, then xpt_schedule() needs to be called again.
184ba6c22ceSWarner Losh  */
185ba6c22ceSWarner Losh typedef int l_iodone_t(struct iop_stats *, struct bio *);
186ba6c22ceSWarner Losh 
187ba6c22ceSWarner Losh static l_iop_t cam_iosched_qd_iop;
188ba6c22ceSWarner Losh static l_iop_t cam_iosched_qd_caniop;
189ba6c22ceSWarner Losh static l_iodone_t cam_iosched_qd_iodone;
190ba6c22ceSWarner Losh 
191ba6c22ceSWarner Losh static l_init_t cam_iosched_iops_init;
192ba6c22ceSWarner Losh static l_tick_t cam_iosched_iops_tick;
193ba6c22ceSWarner Losh static l_iop_t cam_iosched_iops_caniop;
194ba6c22ceSWarner Losh static l_iop_t cam_iosched_iops_iop;
195ba6c22ceSWarner Losh 
196ba6c22ceSWarner Losh static l_init_t cam_iosched_bw_init;
197ba6c22ceSWarner Losh static l_tick_t cam_iosched_bw_tick;
198ba6c22ceSWarner Losh static l_iop_t cam_iosched_bw_caniop;
199ba6c22ceSWarner Losh static l_iop_t cam_iosched_bw_iop;
200ba6c22ceSWarner Losh 
201b20c0a07SWarner Losh struct limswitch {
202ba6c22ceSWarner Losh 	l_init_t	*l_init;
203ba6c22ceSWarner Losh 	l_tick_t	*l_tick;
204ba6c22ceSWarner Losh 	l_iop_t		*l_iop;
205ba6c22ceSWarner Losh 	l_iop_t		*l_caniop;
206ba6c22ceSWarner Losh 	l_iodone_t	*l_iodone;
207ba6c22ceSWarner Losh } limsw[] =
208ba6c22ceSWarner Losh {
209ba6c22ceSWarner Losh 	{	/* none */
210ba6c22ceSWarner Losh 		.l_init = NULL,
211ba6c22ceSWarner Losh 		.l_tick = NULL,
212ba6c22ceSWarner Losh 		.l_iop = NULL,
213ba6c22ceSWarner Losh 		.l_iodone= NULL,
214ba6c22ceSWarner Losh 	},
215ba6c22ceSWarner Losh 	{	/* queue_depth */
216ba6c22ceSWarner Losh 		.l_init = NULL,
217ba6c22ceSWarner Losh 		.l_tick = NULL,
218ba6c22ceSWarner Losh 		.l_caniop = cam_iosched_qd_caniop,
219ba6c22ceSWarner Losh 		.l_iop = cam_iosched_qd_iop,
220ba6c22ceSWarner Losh 		.l_iodone= cam_iosched_qd_iodone,
221ba6c22ceSWarner Losh 	},
222ba6c22ceSWarner Losh 	{	/* iops */
223ba6c22ceSWarner Losh 		.l_init = cam_iosched_iops_init,
224ba6c22ceSWarner Losh 		.l_tick = cam_iosched_iops_tick,
225ba6c22ceSWarner Losh 		.l_caniop = cam_iosched_iops_caniop,
226ba6c22ceSWarner Losh 		.l_iop = cam_iosched_iops_iop,
227ba6c22ceSWarner Losh 		.l_iodone= NULL,
228ba6c22ceSWarner Losh 	},
229ba6c22ceSWarner Losh 	{	/* bandwidth */
230ba6c22ceSWarner Losh 		.l_init = cam_iosched_bw_init,
231ba6c22ceSWarner Losh 		.l_tick = cam_iosched_bw_tick,
232ba6c22ceSWarner Losh 		.l_caniop = cam_iosched_bw_caniop,
233ba6c22ceSWarner Losh 		.l_iop = cam_iosched_bw_iop,
234ba6c22ceSWarner Losh 		.l_iodone= NULL,
235ba6c22ceSWarner Losh 	},
236ba6c22ceSWarner Losh };
237ba6c22ceSWarner Losh 
238b20c0a07SWarner Losh struct iop_stats {
239ba6c22ceSWarner Losh 	/*
240ba6c22ceSWarner Losh 	 * sysctl state for this subnode.
241ba6c22ceSWarner Losh 	 */
242ba6c22ceSWarner Losh 	struct sysctl_ctx_list	sysctl_ctx;
243ba6c22ceSWarner Losh 	struct sysctl_oid	*sysctl_tree;
244ba6c22ceSWarner Losh 
245ba6c22ceSWarner Losh 	/*
246ba6c22ceSWarner Losh 	 * Information about the current rate limiters, if any
247ba6c22ceSWarner Losh 	 */
248ba6c22ceSWarner Losh 	io_limiter	limiter;	/* How are I/Os being limited */
249ba6c22ceSWarner Losh 	int		min;		/* Low range of limit */
250ba6c22ceSWarner Losh 	int		max;		/* High range of limit */
251ba6c22ceSWarner Losh 	int		current;	/* Current rate limiter */
252ba6c22ceSWarner Losh 	int		l_value1;	/* per-limiter scratch value 1. */
253ba6c22ceSWarner Losh 	int		l_value2;	/* per-limiter scratch value 2. */
254ba6c22ceSWarner Losh 
255ba6c22ceSWarner Losh 	/*
256ba6c22ceSWarner Losh 	 * Debug information about counts of I/Os that have gone through the
257ba6c22ceSWarner Losh 	 * scheduler.
258ba6c22ceSWarner Losh 	 */
259ba6c22ceSWarner Losh 	int		pending;	/* I/Os pending in the hardware */
260ba6c22ceSWarner Losh 	int		queued;		/* number currently in the queue */
261ba6c22ceSWarner Losh 	int		total;		/* Total for all time -- wraps */
262ba6c22ceSWarner Losh 	int		in;		/* number queued all time -- wraps */
263ba6c22ceSWarner Losh 	int		out;		/* number completed all time -- wraps */
264c4b72d8bSWarner Losh 	int		errs;		/* Number of I/Os completed with error --  wraps */
265ba6c22ceSWarner Losh 
266ba6c22ceSWarner Losh 	/*
267ba6c22ceSWarner Losh 	 * Statistics on different bits of the process.
268ba6c22ceSWarner Losh 	 */
269cf3ec151SWarner Losh 		/* Exp Moving Average, see alpha_bits for more details */
270ba6c22ceSWarner Losh 	sbintime_t      ema;
27179d80af2SWarner Losh 	sbintime_t      emvar;
272ba6c22ceSWarner Losh 	sbintime_t      sd;		/* Last computed sd */
273ba6c22ceSWarner Losh 
274e82644e5SWarner Losh 	uint64_t	too_long;	/* Number of I/Os greater than bad lat threshold */
275e82644e5SWarner Losh 	sbintime_t	bad_latency;	/* Latency threshold */
276e82644e5SWarner Losh 
277cf3ec151SWarner Losh 	uint32_t	state_flags;
278cf3ec151SWarner Losh #define IOP_RATE_LIMITED		1u
279cf3ec151SWarner Losh 
280cf3ec151SWarner Losh 	uint64_t	latencies[LAT_BUCKETS];
281cf3ec151SWarner Losh 
282ba6c22ceSWarner Losh 	struct cam_iosched_softc *softc;
283ba6c22ceSWarner Losh };
284ba6c22ceSWarner Losh 
285ba6c22ceSWarner Losh typedef enum {
286ba6c22ceSWarner Losh 	set_max = 0,			/* current = max */
287ba6c22ceSWarner Losh 	read_latency,			/* Steer read latency by throttling writes */
288ba6c22ceSWarner Losh 	cl_max				/* Keep last */
289ba6c22ceSWarner Losh } control_type;
290ba6c22ceSWarner Losh 
291ba6c22ceSWarner Losh static const char *cam_iosched_control_type_names[] =
292ba6c22ceSWarner Losh     { "set_max", "read_latency" };
293ba6c22ceSWarner Losh 
294b20c0a07SWarner Losh struct control_loop {
295ba6c22ceSWarner Losh 	/*
296ba6c22ceSWarner Losh 	 * sysctl state for this subnode.
297ba6c22ceSWarner Losh 	 */
298ba6c22ceSWarner Losh 	struct sysctl_ctx_list	sysctl_ctx;
299ba6c22ceSWarner Losh 	struct sysctl_oid	*sysctl_tree;
300ba6c22ceSWarner Losh 
301ba6c22ceSWarner Losh 	sbintime_t	next_steer;		/* Time of next steer */
302ba6c22ceSWarner Losh 	sbintime_t	steer_interval;		/* How often do we steer? */
303ba6c22ceSWarner Losh 	sbintime_t	lolat;
304ba6c22ceSWarner Losh 	sbintime_t	hilat;
305ba6c22ceSWarner Losh 	int		alpha;
306ba6c22ceSWarner Losh 	control_type	type;			/* What type of control? */
307ba6c22ceSWarner Losh 	int		last_count;		/* Last I/O count */
308ba6c22ceSWarner Losh 
309ba6c22ceSWarner Losh 	struct cam_iosched_softc *softc;
310ba6c22ceSWarner Losh };
311ba6c22ceSWarner Losh 
312ba6c22ceSWarner Losh #endif
313ba6c22ceSWarner Losh 
314b20c0a07SWarner Losh struct cam_iosched_softc {
315ba6c22ceSWarner Losh 	struct bio_queue_head bio_queue;
316ba6c22ceSWarner Losh 	struct bio_queue_head trim_queue;
3176f1dd607SWarner Losh 	const struct disk *disk;
318ba6c22ceSWarner Losh 				/* scheduler flags < 16, user flags >= 16 */
319ba6c22ceSWarner Losh 	uint32_t	flags;
320ba6c22ceSWarner Losh 	int		sort_io_queue;
321d900ade5SWarner Losh 	int		trim_goal;		/* # of trims to queue before sending */
322d900ade5SWarner Losh 	int		trim_ticks;		/* Max ticks to hold trims */
323d900ade5SWarner Losh 	int		last_trim_tick;		/* Last 'tick' time ld a trim */
324d900ade5SWarner Losh 	int		queued_trims;		/* Number of trims in the queue */
325df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
326ba6c22ceSWarner Losh 	int		read_bias;		/* Read bias setting */
327ba6c22ceSWarner Losh 	int		current_read_bias;	/* Current read bias state */
328ba6c22ceSWarner Losh 	int		total_ticks;
329cf3ec151SWarner Losh 	int		load;			/* EMA of 'load average' of disk / 2^16 */
330ba6c22ceSWarner Losh 
331ba6c22ceSWarner Losh 	struct bio_queue_head write_queue;
332ba6c22ceSWarner Losh 	struct iop_stats read_stats, write_stats, trim_stats;
333ba6c22ceSWarner Losh 	struct sysctl_ctx_list	sysctl_ctx;
334ba6c22ceSWarner Losh 	struct sysctl_oid	*sysctl_tree;
335ba6c22ceSWarner Losh 
336ba6c22ceSWarner Losh 	int		quanta;			/* Number of quanta per second */
337ba6c22ceSWarner Losh 	struct callout	ticker;			/* Callout for our quota system */
338ba6c22ceSWarner Losh 	struct cam_periph *periph;		/* cam periph associated with this device */
339ba6c22ceSWarner Losh 	uint32_t	this_frac;		/* Fraction of a second (1024ths) for this tick */
340ba6c22ceSWarner Losh 	sbintime_t	last_time;		/* Last time we ticked */
341ba6c22ceSWarner Losh 	struct control_loop cl;
342e5436ab5SWarner Losh 	sbintime_t	max_lat;		/* when != 0, if iop latency > max_lat, call max_lat_fcn */
343e5436ab5SWarner Losh 	cam_iosched_latfcn_t	latfcn;
344e5436ab5SWarner Losh 	void		*latarg;
345ba6c22ceSWarner Losh #endif
346ba6c22ceSWarner Losh };
347ba6c22ceSWarner Losh 
348df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
349ba6c22ceSWarner Losh /*
350ba6c22ceSWarner Losh  * helper functions to call the limsw functions.
351ba6c22ceSWarner Losh  */
352ba6c22ceSWarner Losh static int
353ba6c22ceSWarner Losh cam_iosched_limiter_init(struct iop_stats *ios)
354ba6c22ceSWarner Losh {
355ba6c22ceSWarner Losh 	int lim = ios->limiter;
356ba6c22ceSWarner Losh 
357ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
358ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
359ba6c22ceSWarner Losh 		return EINVAL;
360ba6c22ceSWarner Losh 
361ba6c22ceSWarner Losh 	if (limsw[lim].l_init)
362ba6c22ceSWarner Losh 		return limsw[lim].l_init(ios);
363ba6c22ceSWarner Losh 
364ba6c22ceSWarner Losh 	return 0;
365ba6c22ceSWarner Losh }
366ba6c22ceSWarner Losh 
367ba6c22ceSWarner Losh static int
368ba6c22ceSWarner Losh cam_iosched_limiter_tick(struct iop_stats *ios)
369ba6c22ceSWarner Losh {
370ba6c22ceSWarner Losh 	int lim = ios->limiter;
371ba6c22ceSWarner Losh 
372ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
373ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
374ba6c22ceSWarner Losh 		return EINVAL;
375ba6c22ceSWarner Losh 
376ba6c22ceSWarner Losh 	if (limsw[lim].l_tick)
377ba6c22ceSWarner Losh 		return limsw[lim].l_tick(ios);
378ba6c22ceSWarner Losh 
379ba6c22ceSWarner Losh 	return 0;
380ba6c22ceSWarner Losh }
381ba6c22ceSWarner Losh 
382ba6c22ceSWarner Losh static int
383ba6c22ceSWarner Losh cam_iosched_limiter_iop(struct iop_stats *ios, struct bio *bp)
384ba6c22ceSWarner Losh {
385ba6c22ceSWarner Losh 	int lim = ios->limiter;
386ba6c22ceSWarner Losh 
387ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
388ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
389ba6c22ceSWarner Losh 		return EINVAL;
390ba6c22ceSWarner Losh 
391ba6c22ceSWarner Losh 	if (limsw[lim].l_iop)
392ba6c22ceSWarner Losh 		return limsw[lim].l_iop(ios, bp);
393ba6c22ceSWarner Losh 
394ba6c22ceSWarner Losh 	return 0;
395ba6c22ceSWarner Losh }
396ba6c22ceSWarner Losh 
397ba6c22ceSWarner Losh static int
398ba6c22ceSWarner Losh cam_iosched_limiter_caniop(struct iop_stats *ios, struct bio *bp)
399ba6c22ceSWarner Losh {
400ba6c22ceSWarner Losh 	int lim = ios->limiter;
401ba6c22ceSWarner Losh 
402ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
403ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
404ba6c22ceSWarner Losh 		return EINVAL;
405ba6c22ceSWarner Losh 
406ba6c22ceSWarner Losh 	if (limsw[lim].l_caniop)
407ba6c22ceSWarner Losh 		return limsw[lim].l_caniop(ios, bp);
408ba6c22ceSWarner Losh 
409ba6c22ceSWarner Losh 	return 0;
410ba6c22ceSWarner Losh }
411ba6c22ceSWarner Losh 
412ba6c22ceSWarner Losh static int
413ba6c22ceSWarner Losh cam_iosched_limiter_iodone(struct iop_stats *ios, struct bio *bp)
414ba6c22ceSWarner Losh {
415ba6c22ceSWarner Losh 	int lim = ios->limiter;
416ba6c22ceSWarner Losh 
417ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
418ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
419ba6c22ceSWarner Losh 		return 0;
420ba6c22ceSWarner Losh 
421ba6c22ceSWarner Losh 	if (limsw[lim].l_iodone)
422ba6c22ceSWarner Losh 		return limsw[lim].l_iodone(ios, bp);
423ba6c22ceSWarner Losh 
424ba6c22ceSWarner Losh 	return 0;
425ba6c22ceSWarner Losh }
426ba6c22ceSWarner Losh 
427ba6c22ceSWarner Losh /*
428ba6c22ceSWarner Losh  * Functions to implement the different kinds of limiters
429ba6c22ceSWarner Losh  */
430ba6c22ceSWarner Losh 
431ba6c22ceSWarner Losh static int
432ba6c22ceSWarner Losh cam_iosched_qd_iop(struct iop_stats *ios, struct bio *bp)
433ba6c22ceSWarner Losh {
434ba6c22ceSWarner Losh 
435ba6c22ceSWarner Losh 	if (ios->current <= 0 || ios->pending < ios->current)
436ba6c22ceSWarner Losh 		return 0;
437ba6c22ceSWarner Losh 
438ba6c22ceSWarner Losh 	return EAGAIN;
439ba6c22ceSWarner Losh }
440ba6c22ceSWarner Losh 
441ba6c22ceSWarner Losh static int
442ba6c22ceSWarner Losh cam_iosched_qd_caniop(struct iop_stats *ios, struct bio *bp)
443ba6c22ceSWarner Losh {
444ba6c22ceSWarner Losh 
445ba6c22ceSWarner Losh 	if (ios->current <= 0 || ios->pending < ios->current)
446ba6c22ceSWarner Losh 		return 0;
447ba6c22ceSWarner Losh 
448ba6c22ceSWarner Losh 	return EAGAIN;
449ba6c22ceSWarner Losh }
450ba6c22ceSWarner Losh 
451ba6c22ceSWarner Losh static int
452ba6c22ceSWarner Losh cam_iosched_qd_iodone(struct iop_stats *ios, struct bio *bp)
453ba6c22ceSWarner Losh {
454ba6c22ceSWarner Losh 
455ba6c22ceSWarner Losh 	if (ios->current <= 0 || ios->pending != ios->current)
456ba6c22ceSWarner Losh 		return 0;
457ba6c22ceSWarner Losh 
458ba6c22ceSWarner Losh 	return 1;
459ba6c22ceSWarner Losh }
460ba6c22ceSWarner Losh 
461ba6c22ceSWarner Losh static int
462ba6c22ceSWarner Losh cam_iosched_iops_init(struct iop_stats *ios)
463ba6c22ceSWarner Losh {
464ba6c22ceSWarner Losh 
465ba6c22ceSWarner Losh 	ios->l_value1 = ios->current / ios->softc->quanta;
466ba6c22ceSWarner Losh 	if (ios->l_value1 <= 0)
467ba6c22ceSWarner Losh 		ios->l_value1 = 1;
468f777123bSWarner Losh 	ios->l_value2 = 0;
469ba6c22ceSWarner Losh 
470ba6c22ceSWarner Losh 	return 0;
471ba6c22ceSWarner Losh }
472ba6c22ceSWarner Losh 
473ba6c22ceSWarner Losh static int
474ba6c22ceSWarner Losh cam_iosched_iops_tick(struct iop_stats *ios)
475ba6c22ceSWarner Losh {
47678ed811eSWarner Losh 	int new_ios;
477ba6c22ceSWarner Losh 
478f777123bSWarner Losh 	/*
479f777123bSWarner Losh 	 * Allow at least one IO per tick until all
480f777123bSWarner Losh 	 * the IOs for this interval have been spent.
481f777123bSWarner Losh 	 */
48278ed811eSWarner Losh 	new_ios = (int)((ios->current * (uint64_t)ios->softc->this_frac) >> 16);
48378ed811eSWarner Losh 	if (new_ios < 1 && ios->l_value2 < ios->current) {
48478ed811eSWarner Losh 		new_ios = 1;
485f777123bSWarner Losh 		ios->l_value2++;
486f777123bSWarner Losh 	}
487ba6c22ceSWarner Losh 
48878ed811eSWarner Losh 	/*
48978ed811eSWarner Losh 	 * If this a new accounting interval, discard any "unspent" ios
49078ed811eSWarner Losh 	 * granted in the previous interval.  Otherwise add the new ios to
49178ed811eSWarner Losh 	 * the previously granted ones that haven't been spent yet.
49278ed811eSWarner Losh 	 */
49378ed811eSWarner Losh 	if ((ios->softc->total_ticks % ios->softc->quanta) == 0) {
49478ed811eSWarner Losh 		ios->l_value1 = new_ios;
49578ed811eSWarner Losh 		ios->l_value2 = 1;
49678ed811eSWarner Losh 	} else {
49778ed811eSWarner Losh 		ios->l_value1 += new_ios;
49878ed811eSWarner Losh 	}
49978ed811eSWarner Losh 
500ba6c22ceSWarner Losh 	return 0;
501ba6c22ceSWarner Losh }
502ba6c22ceSWarner Losh 
503ba6c22ceSWarner Losh static int
504ba6c22ceSWarner Losh cam_iosched_iops_caniop(struct iop_stats *ios, struct bio *bp)
505ba6c22ceSWarner Losh {
506ba6c22ceSWarner Losh 
507ba6c22ceSWarner Losh 	/*
508ba6c22ceSWarner Losh 	 * So if we have any more IOPs left, allow it,
5096ca2fb66SWarner Losh 	 * otherwise wait. If current iops is 0, treat that
5106ca2fb66SWarner Losh 	 * as unlimited as a failsafe.
511ba6c22ceSWarner Losh 	 */
5126ca2fb66SWarner Losh 	if (ios->current > 0 && ios->l_value1 <= 0)
513ba6c22ceSWarner Losh 		return EAGAIN;
514ba6c22ceSWarner Losh 	return 0;
515ba6c22ceSWarner Losh }
516ba6c22ceSWarner Losh 
517ba6c22ceSWarner Losh static int
518ba6c22ceSWarner Losh cam_iosched_iops_iop(struct iop_stats *ios, struct bio *bp)
519ba6c22ceSWarner Losh {
520ba6c22ceSWarner Losh 	int rv;
521ba6c22ceSWarner Losh 
522ba6c22ceSWarner Losh 	rv = cam_iosched_limiter_caniop(ios, bp);
523ba6c22ceSWarner Losh 	if (rv == 0)
524ba6c22ceSWarner Losh 		ios->l_value1--;
525ba6c22ceSWarner Losh 
526ba6c22ceSWarner Losh 	return rv;
527ba6c22ceSWarner Losh }
528ba6c22ceSWarner Losh 
529ba6c22ceSWarner Losh static int
530ba6c22ceSWarner Losh cam_iosched_bw_init(struct iop_stats *ios)
531ba6c22ceSWarner Losh {
532ba6c22ceSWarner Losh 
533ba6c22ceSWarner Losh 	/* ios->current is in kB/s, so scale to bytes */
534ba6c22ceSWarner Losh 	ios->l_value1 = ios->current * 1000 / ios->softc->quanta;
535ba6c22ceSWarner Losh 
536ba6c22ceSWarner Losh 	return 0;
537ba6c22ceSWarner Losh }
538ba6c22ceSWarner Losh 
539ba6c22ceSWarner Losh static int
540ba6c22ceSWarner Losh cam_iosched_bw_tick(struct iop_stats *ios)
541ba6c22ceSWarner Losh {
542ba6c22ceSWarner Losh 	int bw;
543ba6c22ceSWarner Losh 
544ba6c22ceSWarner Losh 	/*
545ba6c22ceSWarner Losh 	 * If we're in the hole for available quota from
546ba6c22ceSWarner Losh 	 * the last time, then add the quantum for this.
547ba6c22ceSWarner Losh 	 * If we have any left over from last quantum,
548ba6c22ceSWarner Losh 	 * then too bad, that's lost. Also, ios->current
549ba6c22ceSWarner Losh 	 * is in kB/s, so scale.
550ba6c22ceSWarner Losh 	 *
551ba6c22ceSWarner Losh 	 * We also allow up to 4 quanta of credits to
552ba6c22ceSWarner Losh 	 * accumulate to deal with burstiness. 4 is extremely
553ba6c22ceSWarner Losh 	 * arbitrary.
554ba6c22ceSWarner Losh 	 */
555ba6c22ceSWarner Losh 	bw = (int)((ios->current * 1000ull * (uint64_t)ios->softc->this_frac) >> 16);
556ba6c22ceSWarner Losh 	if (ios->l_value1 < bw * 4)
557ba6c22ceSWarner Losh 		ios->l_value1 += bw;
558ba6c22ceSWarner Losh 
559ba6c22ceSWarner Losh 	return 0;
560ba6c22ceSWarner Losh }
561ba6c22ceSWarner Losh 
562ba6c22ceSWarner Losh static int
563ba6c22ceSWarner Losh cam_iosched_bw_caniop(struct iop_stats *ios, struct bio *bp)
564ba6c22ceSWarner Losh {
565ba6c22ceSWarner Losh 	/*
566ba6c22ceSWarner Losh 	 * So if we have any more bw quota left, allow it,
5670b4060b0SEd Maste 	 * otherwise wait. Note, we'll go negative and that's
5680b4060b0SEd Maste 	 * OK. We'll just get a little less next quota.
569ba6c22ceSWarner Losh 	 *
570ba6c22ceSWarner Losh 	 * Note on going negative: that allows us to process
571ba6c22ceSWarner Losh 	 * requests in order better, since we won't allow
572ba6c22ceSWarner Losh 	 * shorter reads to get around the long one that we
573ba6c22ceSWarner Losh 	 * don't have the quota to do just yet. It also prevents
574ba6c22ceSWarner Losh 	 * starvation by being a little more permissive about
575ba6c22ceSWarner Losh 	 * what we let through this quantum (to prevent the
576ba6c22ceSWarner Losh 	 * starvation), at the cost of getting a little less
577ba6c22ceSWarner Losh 	 * next quantum.
5786ca2fb66SWarner Losh 	 *
5796ca2fb66SWarner Losh 	 * Also note that if the current limit is <= 0,
5806ca2fb66SWarner Losh 	 * we treat it as unlimited as a failsafe.
581ba6c22ceSWarner Losh 	 */
5826ca2fb66SWarner Losh 	if (ios->current > 0 && ios->l_value1 <= 0)
583ba6c22ceSWarner Losh 		return EAGAIN;
584ba6c22ceSWarner Losh 
585ba6c22ceSWarner Losh 	return 0;
586ba6c22ceSWarner Losh }
587ba6c22ceSWarner Losh 
588ba6c22ceSWarner Losh static int
589ba6c22ceSWarner Losh cam_iosched_bw_iop(struct iop_stats *ios, struct bio *bp)
590ba6c22ceSWarner Losh {
591ba6c22ceSWarner Losh 	int rv;
592ba6c22ceSWarner Losh 
593ba6c22ceSWarner Losh 	rv = cam_iosched_limiter_caniop(ios, bp);
594ba6c22ceSWarner Losh 	if (rv == 0)
595ba6c22ceSWarner Losh 		ios->l_value1 -= bp->bio_length;
596ba6c22ceSWarner Losh 
597ba6c22ceSWarner Losh 	return rv;
598ba6c22ceSWarner Losh }
599ba6c22ceSWarner Losh 
600ba6c22ceSWarner Losh static void cam_iosched_cl_maybe_steer(struct control_loop *clp);
601ba6c22ceSWarner Losh 
602ba6c22ceSWarner Losh static void
603ba6c22ceSWarner Losh cam_iosched_ticker(void *arg)
604ba6c22ceSWarner Losh {
605ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc = arg;
606ba6c22ceSWarner Losh 	sbintime_t now, delta;
607cf3ec151SWarner Losh 	int pending;
608ba6c22ceSWarner Losh 
6093028dd8dSWarner Losh 	callout_reset(&isc->ticker, hz / isc->quanta, cam_iosched_ticker, isc);
610ba6c22ceSWarner Losh 
611ba6c22ceSWarner Losh 	now = sbinuptime();
612ba6c22ceSWarner Losh 	delta = now - isc->last_time;
613ba6c22ceSWarner Losh 	isc->this_frac = (uint32_t)delta >> 16;		/* Note: discards seconds -- should be 0 harmless if not */
614ba6c22ceSWarner Losh 	isc->last_time = now;
615ba6c22ceSWarner Losh 
616ba6c22ceSWarner Losh 	cam_iosched_cl_maybe_steer(&isc->cl);
617ba6c22ceSWarner Losh 
618ba6c22ceSWarner Losh 	cam_iosched_limiter_tick(&isc->read_stats);
619ba6c22ceSWarner Losh 	cam_iosched_limiter_tick(&isc->write_stats);
620ba6c22ceSWarner Losh 	cam_iosched_limiter_tick(&isc->trim_stats);
621ba6c22ceSWarner Losh 
622ba6c22ceSWarner Losh 	cam_iosched_schedule(isc, isc->periph);
623ba6c22ceSWarner Losh 
624cf3ec151SWarner Losh 	/*
625cf3ec151SWarner Losh 	 * isc->load is an EMA of the pending I/Os at each tick. The number of
626cf3ec151SWarner Losh 	 * pending I/Os is the sum of the I/Os queued to the hardware, and those
627cf3ec151SWarner Losh 	 * in the software queue that could be queued to the hardware if there
628cf3ec151SWarner Losh 	 * were slots.
629cf3ec151SWarner Losh 	 *
630cf3ec151SWarner Losh 	 * ios_stats.pending is a count of requests in the SIM right now for
631cf3ec151SWarner Losh 	 * each of these types of I/O. So the total pending count is the sum of
632cf3ec151SWarner Losh 	 * these I/Os and the sum of the queued I/Os still in the software queue
633cf3ec151SWarner Losh 	 * for those operations that aren't being rate limited at the moment.
634cf3ec151SWarner Losh 	 *
635cf3ec151SWarner Losh 	 * The reason for the rate limiting bit is because those I/Os
636cf3ec151SWarner Losh 	 * aren't part of the software queued load (since we could
637cf3ec151SWarner Losh 	 * give them to hardware, but choose not to).
638cf3ec151SWarner Losh 	 *
639cf3ec151SWarner Losh 	 * Note: due to a bug in counting pending TRIM in the device, we
640cf3ec151SWarner Losh 	 * don't include them in this count. We count each BIO_DELETE in
641cf3ec151SWarner Losh 	 * the pending count, but the periph drivers collapse them down
642cf3ec151SWarner Losh 	 * into one TRIM command. That one trim command gets the completion
643cf3ec151SWarner Losh 	 * so the counts get off.
644cf3ec151SWarner Losh 	 */
645cf3ec151SWarner Losh 	pending = isc->read_stats.pending + isc->write_stats.pending /* + isc->trim_stats.pending */;
646cf3ec151SWarner Losh 	pending += !!(isc->read_stats.state_flags & IOP_RATE_LIMITED) * isc->read_stats.queued +
647cf3ec151SWarner Losh 	    !!(isc->write_stats.state_flags & IOP_RATE_LIMITED) * isc->write_stats.queued /* +
648cf3ec151SWarner Losh 	    !!(isc->trim_stats.state_flags & IOP_RATE_LIMITED) * isc->trim_stats.queued */ ;
649cf3ec151SWarner Losh 	pending <<= 16;
650cf3ec151SWarner Losh 	pending /= isc->periph->path->device->ccbq.total_openings;
651cf3ec151SWarner Losh 
652cf3ec151SWarner Losh 	isc->load = (pending + (isc->load << 13) - isc->load) >> 13; /* see above: 13 -> 16139 / 200/s = ~81s ~1 minute */
653cf3ec151SWarner Losh 
654ba6c22ceSWarner Losh 	isc->total_ticks++;
655ba6c22ceSWarner Losh }
656ba6c22ceSWarner Losh 
657ba6c22ceSWarner Losh static void
658ba6c22ceSWarner Losh cam_iosched_cl_init(struct control_loop *clp, struct cam_iosched_softc *isc)
659ba6c22ceSWarner Losh {
660ba6c22ceSWarner Losh 
661ba6c22ceSWarner Losh 	clp->next_steer = sbinuptime();
662ba6c22ceSWarner Losh 	clp->softc = isc;
663ba6c22ceSWarner Losh 	clp->steer_interval = SBT_1S * 5;	/* Let's start out steering every 5s */
664ba6c22ceSWarner Losh 	clp->lolat = 5 * SBT_1MS;
665ba6c22ceSWarner Losh 	clp->hilat = 15 * SBT_1MS;
666ba6c22ceSWarner Losh 	clp->alpha = 20;			/* Alpha == gain. 20 = .2 */
667ba6c22ceSWarner Losh 	clp->type = set_max;
668ba6c22ceSWarner Losh }
669ba6c22ceSWarner Losh 
670ba6c22ceSWarner Losh static void
671ba6c22ceSWarner Losh cam_iosched_cl_maybe_steer(struct control_loop *clp)
672ba6c22ceSWarner Losh {
673ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
674ba6c22ceSWarner Losh 	sbintime_t now, lat;
675ba6c22ceSWarner Losh 	int old;
676ba6c22ceSWarner Losh 
677ba6c22ceSWarner Losh 	isc = clp->softc;
678ba6c22ceSWarner Losh 	now = isc->last_time;
679ba6c22ceSWarner Losh 	if (now < clp->next_steer)
680ba6c22ceSWarner Losh 		return;
681ba6c22ceSWarner Losh 
682ba6c22ceSWarner Losh 	clp->next_steer = now + clp->steer_interval;
683ba6c22ceSWarner Losh 	switch (clp->type) {
684ba6c22ceSWarner Losh 	case set_max:
685ba6c22ceSWarner Losh 		if (isc->write_stats.current != isc->write_stats.max)
686ba6c22ceSWarner Losh 			printf("Steering write from %d kBps to %d kBps\n",
687ba6c22ceSWarner Losh 			    isc->write_stats.current, isc->write_stats.max);
688ba6c22ceSWarner Losh 		isc->read_stats.current = isc->read_stats.max;
689ba6c22ceSWarner Losh 		isc->write_stats.current = isc->write_stats.max;
690ba6c22ceSWarner Losh 		isc->trim_stats.current = isc->trim_stats.max;
691ba6c22ceSWarner Losh 		break;
692ba6c22ceSWarner Losh 	case read_latency:
693ba6c22ceSWarner Losh 		old = isc->write_stats.current;
694ba6c22ceSWarner Losh 		lat = isc->read_stats.ema;
695ba6c22ceSWarner Losh 		/*
696ba6c22ceSWarner Losh 		 * Simple PLL-like engine. Since we're steering to a range for
697ba6c22ceSWarner Losh 		 * the SP (set point) that makes things a little more
698ba6c22ceSWarner Losh 		 * complicated. In addition, we're not directly controlling our
699ba6c22ceSWarner Losh 		 * PV (process variable), the read latency, but instead are
700ba6c22ceSWarner Losh 		 * manipulating the write bandwidth limit for our MV
701ba6c22ceSWarner Losh 		 * (manipulation variable), analysis of this code gets a bit
702ba6c22ceSWarner Losh 		 * messy. Also, the MV is a very noisy control surface for read
703ba6c22ceSWarner Losh 		 * latency since it is affected by many hidden processes inside
704ba6c22ceSWarner Losh 		 * the device which change how responsive read latency will be
705ba6c22ceSWarner Losh 		 * in reaction to changes in write bandwidth. Unlike the classic
706ba6c22ceSWarner Losh 		 * boiler control PLL. this may result in over-steering while
707ba6c22ceSWarner Losh 		 * the SSD takes its time to react to the new, lower load. This
708ba6c22ceSWarner Losh 		 * is why we use a relatively low alpha of between .1 and .25 to
709ba6c22ceSWarner Losh 		 * compensate for this effect. At .1, it takes ~22 steering
710ba6c22ceSWarner Losh 		 * intervals to back off by a factor of 10. At .2 it only takes
711ba6c22ceSWarner Losh 		 * ~10. At .25 it only takes ~8. However some preliminary data
712ba6c22ceSWarner Losh 		 * from the SSD drives suggests a reasponse time in 10's of
713ba6c22ceSWarner Losh 		 * seconds before latency drops regardless of the new write
7140b4060b0SEd Maste 		 * rate. Careful observation will be required to tune this
715ba6c22ceSWarner Losh 		 * effectively.
716ba6c22ceSWarner Losh 		 *
717ba6c22ceSWarner Losh 		 * Also, when there's no read traffic, we jack up the write
718ba6c22ceSWarner Losh 		 * limit too regardless of the last read latency.  10 is
719ba6c22ceSWarner Losh 		 * somewhat arbitrary.
720ba6c22ceSWarner Losh 		 */
721ba6c22ceSWarner Losh 		if (lat < clp->lolat || isc->read_stats.total - clp->last_count < 10)
722ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.current *
723ba6c22ceSWarner Losh 			    (100 + clp->alpha) / 100;	/* Scale up */
724ba6c22ceSWarner Losh 		else if (lat > clp->hilat)
725ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.current *
726ba6c22ceSWarner Losh 			    (100 - clp->alpha) / 100;	/* Scale down */
727ba6c22ceSWarner Losh 		clp->last_count = isc->read_stats.total;
728ba6c22ceSWarner Losh 
729ba6c22ceSWarner Losh 		/*
730ba6c22ceSWarner Losh 		 * Even if we don't steer, per se, enforce the min/max limits as
731ba6c22ceSWarner Losh 		 * those may have changed.
732ba6c22ceSWarner Losh 		 */
733ba6c22ceSWarner Losh 		if (isc->write_stats.current < isc->write_stats.min)
734ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.min;
735ba6c22ceSWarner Losh 		if (isc->write_stats.current > isc->write_stats.max)
736ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.max;
7372b5c19f1SWarner Losh 		if (old != isc->write_stats.current && 	iosched_debug)
738cf3ec151SWarner Losh 			printf("Steering write from %d kBps to %d kBps due to latency of %jdus\n",
739ba6c22ceSWarner Losh 			    old, isc->write_stats.current,
7402b5c19f1SWarner Losh 			    (uintmax_t)((uint64_t)1000000 * (uint32_t)lat) >> 32);
741ba6c22ceSWarner Losh 		break;
742ba6c22ceSWarner Losh 	case cl_max:
743ba6c22ceSWarner Losh 		break;
744ba6c22ceSWarner Losh 	}
745ba6c22ceSWarner Losh }
746ba6c22ceSWarner Losh #endif
747ba6c22ceSWarner Losh 
748ece56614SWarner Losh /*
749ece56614SWarner Losh  * Trim or similar currently pending completion. Should only be set for
750ece56614SWarner Losh  * those drivers wishing only one Trim active at a time.
751ece56614SWarner Losh  */
752ece56614SWarner Losh #define CAM_IOSCHED_FLAG_TRIM_ACTIVE	(1ul << 0)
75307e5967aSWarner Losh 			/* Callout active, and needs to be torn down */
75407e5967aSWarner Losh #define CAM_IOSCHED_FLAG_CALLOUT_ACTIVE (1ul << 1)
75507e5967aSWarner Losh 
75607e5967aSWarner Losh 			/* Periph drivers set these flags to indicate work */
75707e5967aSWarner Losh #define CAM_IOSCHED_FLAG_WORK_FLAGS	((0xffffu) << 16)
75807e5967aSWarner Losh 
759df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
760ba6c22ceSWarner Losh static void
761ba6c22ceSWarner Losh cam_iosched_io_metric_update(struct cam_iosched_softc *isc,
762148c1734SWarner Losh     sbintime_t sim_latency, const struct bio *bp);
7635ede5b8cSWarner Losh #endif
764ba6c22ceSWarner Losh 
7652d87718fSWarner Losh static inline bool
766ba6c22ceSWarner Losh cam_iosched_has_flagged_work(struct cam_iosched_softc *isc)
767ba6c22ceSWarner Losh {
768ba6c22ceSWarner Losh 	return !!(isc->flags & CAM_IOSCHED_FLAG_WORK_FLAGS);
769ba6c22ceSWarner Losh }
770ba6c22ceSWarner Losh 
7712d87718fSWarner Losh static inline bool
772ba6c22ceSWarner Losh cam_iosched_has_io(struct cam_iosched_softc *isc)
773ba6c22ceSWarner Losh {
774df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
775035ec48eSWarner Losh 	if (do_dynamic_iosched) {
776ba6c22ceSWarner Losh 		struct bio *rbp = bioq_first(&isc->bio_queue);
777ba6c22ceSWarner Losh 		struct bio *wbp = bioq_first(&isc->write_queue);
7782d87718fSWarner Losh 		bool can_write = wbp != NULL &&
779ba6c22ceSWarner Losh 		    cam_iosched_limiter_caniop(&isc->write_stats, wbp) == 0;
7802d87718fSWarner Losh 		bool can_read = rbp != NULL &&
781ba6c22ceSWarner Losh 		    cam_iosched_limiter_caniop(&isc->read_stats, rbp) == 0;
782ba6c22ceSWarner Losh 		if (iosched_debug > 2) {
783ba6c22ceSWarner Losh 			printf("can write %d: pending_writes %d max_writes %d\n", can_write, isc->write_stats.pending, isc->write_stats.max);
784ba6c22ceSWarner Losh 			printf("can read %d: read_stats.pending %d max_reads %d\n", can_read, isc->read_stats.pending, isc->read_stats.max);
785ba6c22ceSWarner Losh 			printf("Queued reads %d writes %d\n", isc->read_stats.queued, isc->write_stats.queued);
786ba6c22ceSWarner Losh 		}
787ba6c22ceSWarner Losh 		return can_read || can_write;
788ba6c22ceSWarner Losh 	}
789ba6c22ceSWarner Losh #endif
790ba6c22ceSWarner Losh 	return bioq_first(&isc->bio_queue) != NULL;
791ba6c22ceSWarner Losh }
792ba6c22ceSWarner Losh 
7932d87718fSWarner Losh static inline bool
794ba6c22ceSWarner Losh cam_iosched_has_more_trim(struct cam_iosched_softc *isc)
795ba6c22ceSWarner Losh {
796c6171b44SWarner Losh 	struct bio *bp;
797c6171b44SWarner Losh 
798c6171b44SWarner Losh 	bp = bioq_first(&isc->trim_queue);
799c6171b44SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
800c6171b44SWarner Losh 	if (do_dynamic_iosched) {
801c6171b44SWarner Losh 		/*
802c6171b44SWarner Losh 		 * If we're limiting trims, then defer action on trims
803c6171b44SWarner Losh 		 * for a bit.
804c6171b44SWarner Losh 		 */
805c6171b44SWarner Losh 		if (bp == NULL || cam_iosched_limiter_caniop(&isc->trim_stats, bp) != 0)
806c6171b44SWarner Losh 			return false;
807c6171b44SWarner Losh 	}
808c6171b44SWarner Losh #endif
809d900ade5SWarner Losh 
810d900ade5SWarner Losh 	/*
811d900ade5SWarner Losh 	 * If we've set a trim_goal, then if we exceed that allow trims
812d900ade5SWarner Losh 	 * to be passed back to the driver. If we've also set a tick timeout
813d900ade5SWarner Losh 	 * allow trims back to the driver. Otherwise, don't allow trims yet.
814d900ade5SWarner Losh 	 */
815d900ade5SWarner Losh 	if (isc->trim_goal > 0) {
816d900ade5SWarner Losh 		if (isc->queued_trims >= isc->trim_goal)
817d900ade5SWarner Losh 			return true;
818d900ade5SWarner Losh 		if (isc->queued_trims > 0 &&
819d900ade5SWarner Losh 		    isc->trim_ticks > 0 &&
820d900ade5SWarner Losh 		    ticks - isc->last_trim_tick > isc->trim_ticks)
821d900ade5SWarner Losh 			return true;
822d900ade5SWarner Losh 		return false;
823d900ade5SWarner Losh 	}
824d900ade5SWarner Losh 
825ece56614SWarner Losh 	/* NB: Should perhaps have a max trim active independent of I/O limiters */
826ece56614SWarner Losh 	return !(isc->flags & CAM_IOSCHED_FLAG_TRIM_ACTIVE) && bp != NULL;
827ba6c22ceSWarner Losh }
828ba6c22ceSWarner Losh 
829ba6c22ceSWarner Losh #define cam_iosched_sort_queue(isc)	((isc)->sort_io_queue >= 0 ?	\
830ba6c22ceSWarner Losh     (isc)->sort_io_queue : cam_sort_io_queues)
831ba6c22ceSWarner Losh 
8322d87718fSWarner Losh static inline bool
833ba6c22ceSWarner Losh cam_iosched_has_work(struct cam_iosched_softc *isc)
834ba6c22ceSWarner Losh {
835df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
836ba6c22ceSWarner Losh 	if (iosched_debug > 2)
837ba6c22ceSWarner Losh 		printf("has work: %d %d %d\n", cam_iosched_has_io(isc),
838ba6c22ceSWarner Losh 		    cam_iosched_has_more_trim(isc),
839ba6c22ceSWarner Losh 		    cam_iosched_has_flagged_work(isc));
840ba6c22ceSWarner Losh #endif
841ba6c22ceSWarner Losh 
842ba6c22ceSWarner Losh 	return cam_iosched_has_io(isc) ||
843ba6c22ceSWarner Losh 		cam_iosched_has_more_trim(isc) ||
844ba6c22ceSWarner Losh 		cam_iosched_has_flagged_work(isc);
845ba6c22ceSWarner Losh }
846ba6c22ceSWarner Losh 
847df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
848ba6c22ceSWarner Losh static void
849ba6c22ceSWarner Losh cam_iosched_iop_stats_init(struct cam_iosched_softc *isc, struct iop_stats *ios)
850ba6c22ceSWarner Losh {
851ba6c22ceSWarner Losh 
852ba6c22ceSWarner Losh 	ios->limiter = none;
853ba6c22ceSWarner Losh 	ios->in = 0;
85489d26636SWarner Losh 	ios->max = ios->current = 300000;
855ba6c22ceSWarner Losh 	ios->min = 1;
856ba6c22ceSWarner Losh 	ios->out = 0;
857c4b72d8bSWarner Losh 	ios->errs = 0;
858ba6c22ceSWarner Losh 	ios->pending = 0;
859ba6c22ceSWarner Losh 	ios->queued = 0;
860ba6c22ceSWarner Losh 	ios->total = 0;
861ba6c22ceSWarner Losh 	ios->ema = 0;
86279d80af2SWarner Losh 	ios->emvar = 0;
863e82644e5SWarner Losh 	ios->bad_latency = SBT_1S / 2;	/* Default to 500ms */
864ba6c22ceSWarner Losh 	ios->softc = isc;
86589d26636SWarner Losh 	cam_iosched_limiter_init(ios);
866ba6c22ceSWarner Losh }
867ba6c22ceSWarner Losh 
868ba6c22ceSWarner Losh static int
869ba6c22ceSWarner Losh cam_iosched_limiter_sysctl(SYSCTL_HANDLER_ARGS)
870ba6c22ceSWarner Losh {
871ba6c22ceSWarner Losh 	char buf[16];
872ba6c22ceSWarner Losh 	struct iop_stats *ios;
873ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
874cf3ec151SWarner Losh 	int value, i, error;
875ba6c22ceSWarner Losh 	const char *p;
876ba6c22ceSWarner Losh 
877ba6c22ceSWarner Losh 	ios = arg1;
878ba6c22ceSWarner Losh 	isc = ios->softc;
879ba6c22ceSWarner Losh 	value = ios->limiter;
880ba6c22ceSWarner Losh 	if (value < none || value >= limiter_max)
881ba6c22ceSWarner Losh 		p = "UNKNOWN";
882ba6c22ceSWarner Losh 	else
883ba6c22ceSWarner Losh 		p = cam_iosched_limiter_names[value];
884ba6c22ceSWarner Losh 
885ba6c22ceSWarner Losh 	strlcpy(buf, p, sizeof(buf));
886ba6c22ceSWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
887ba6c22ceSWarner Losh 	if (error != 0 || req->newptr == NULL)
888ba6c22ceSWarner Losh 		return error;
889ba6c22ceSWarner Losh 
890ba6c22ceSWarner Losh 	cam_periph_lock(isc->periph);
891ba6c22ceSWarner Losh 
892ba6c22ceSWarner Losh 	for (i = none; i < limiter_max; i++) {
893ba6c22ceSWarner Losh 		if (strcmp(buf, cam_iosched_limiter_names[i]) != 0)
894ba6c22ceSWarner Losh 			continue;
895ba6c22ceSWarner Losh 		ios->limiter = i;
896ba6c22ceSWarner Losh 		error = cam_iosched_limiter_init(ios);
897ba6c22ceSWarner Losh 		if (error != 0) {
898ba6c22ceSWarner Losh 			ios->limiter = value;
899ba6c22ceSWarner Losh 			cam_periph_unlock(isc->periph);
900ba6c22ceSWarner Losh 			return error;
901ba6c22ceSWarner Losh 		}
902cf3ec151SWarner Losh 		/* Note: disk load averate requires ticker to be always running */
9033028dd8dSWarner Losh 		callout_reset(&isc->ticker, hz / isc->quanta, cam_iosched_ticker, isc);
904ba6c22ceSWarner Losh 		isc->flags |= CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
905ba6c22ceSWarner Losh 
906ba6c22ceSWarner Losh 		cam_periph_unlock(isc->periph);
907ba6c22ceSWarner Losh 		return 0;
908ba6c22ceSWarner Losh 	}
909ba6c22ceSWarner Losh 
910ba6c22ceSWarner Losh 	cam_periph_unlock(isc->periph);
911ba6c22ceSWarner Losh 	return EINVAL;
912ba6c22ceSWarner Losh }
913ba6c22ceSWarner Losh 
914ba6c22ceSWarner Losh static int
915ba6c22ceSWarner Losh cam_iosched_control_type_sysctl(SYSCTL_HANDLER_ARGS)
916ba6c22ceSWarner Losh {
917ba6c22ceSWarner Losh 	char buf[16];
918ba6c22ceSWarner Losh 	struct control_loop *clp;
919ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
920ba6c22ceSWarner Losh 	int value, i, error;
921ba6c22ceSWarner Losh 	const char *p;
922ba6c22ceSWarner Losh 
923ba6c22ceSWarner Losh 	clp = arg1;
924ba6c22ceSWarner Losh 	isc = clp->softc;
925ba6c22ceSWarner Losh 	value = clp->type;
926ba6c22ceSWarner Losh 	if (value < none || value >= cl_max)
927ba6c22ceSWarner Losh 		p = "UNKNOWN";
928ba6c22ceSWarner Losh 	else
929ba6c22ceSWarner Losh 		p = cam_iosched_control_type_names[value];
930ba6c22ceSWarner Losh 
931ba6c22ceSWarner Losh 	strlcpy(buf, p, sizeof(buf));
932ba6c22ceSWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
933ba6c22ceSWarner Losh 	if (error != 0 || req->newptr == NULL)
934ba6c22ceSWarner Losh 		return error;
935ba6c22ceSWarner Losh 
936ba6c22ceSWarner Losh 	for (i = set_max; i < cl_max; i++) {
937ba6c22ceSWarner Losh 		if (strcmp(buf, cam_iosched_control_type_names[i]) != 0)
938ba6c22ceSWarner Losh 			continue;
939ba6c22ceSWarner Losh 		cam_periph_lock(isc->periph);
940ba6c22ceSWarner Losh 		clp->type = i;
941ba6c22ceSWarner Losh 		cam_periph_unlock(isc->periph);
942ba6c22ceSWarner Losh 		return 0;
943ba6c22ceSWarner Losh 	}
944ba6c22ceSWarner Losh 
945ba6c22ceSWarner Losh 	return EINVAL;
946ba6c22ceSWarner Losh }
947ba6c22ceSWarner Losh 
948ba6c22ceSWarner Losh static int
949ba6c22ceSWarner Losh cam_iosched_sbintime_sysctl(SYSCTL_HANDLER_ARGS)
950ba6c22ceSWarner Losh {
951ba6c22ceSWarner Losh 	char buf[16];
952ba6c22ceSWarner Losh 	sbintime_t value;
953ba6c22ceSWarner Losh 	int error;
954ba6c22ceSWarner Losh 	uint64_t us;
955ba6c22ceSWarner Losh 
956ba6c22ceSWarner Losh 	value = *(sbintime_t *)arg1;
957ba6c22ceSWarner Losh 	us = (uint64_t)value / SBT_1US;
958ba6c22ceSWarner Losh 	snprintf(buf, sizeof(buf), "%ju", (intmax_t)us);
959ba6c22ceSWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
960ba6c22ceSWarner Losh 	if (error != 0 || req->newptr == NULL)
961ba6c22ceSWarner Losh 		return error;
962ba6c22ceSWarner Losh 	us = strtoul(buf, NULL, 10);
963ba6c22ceSWarner Losh 	if (us == 0)
964ba6c22ceSWarner Losh 		return EINVAL;
965ba6c22ceSWarner Losh 	*(sbintime_t *)arg1 = us * SBT_1US;
966ba6c22ceSWarner Losh 	return 0;
967ba6c22ceSWarner Losh }
968ba6c22ceSWarner Losh 
969cf3ec151SWarner Losh static int
970cf3ec151SWarner Losh cam_iosched_sysctl_latencies(SYSCTL_HANDLER_ARGS)
971cf3ec151SWarner Losh {
972cf3ec151SWarner Losh 	int i, error;
973cf3ec151SWarner Losh 	struct sbuf sb;
974cf3ec151SWarner Losh 	uint64_t *latencies;
975cf3ec151SWarner Losh 
976cf3ec151SWarner Losh 	latencies = arg1;
977cf3ec151SWarner Losh 	sbuf_new_for_sysctl(&sb, NULL, LAT_BUCKETS * 16, req);
978cf3ec151SWarner Losh 
979cf3ec151SWarner Losh 	for (i = 0; i < LAT_BUCKETS - 1; i++)
980cf3ec151SWarner Losh 		sbuf_printf(&sb, "%jd,", (intmax_t)latencies[i]);
981cf3ec151SWarner Losh 	sbuf_printf(&sb, "%jd", (intmax_t)latencies[LAT_BUCKETS - 1]);
982cf3ec151SWarner Losh 	error = sbuf_finish(&sb);
983cf3ec151SWarner Losh 	sbuf_delete(&sb);
984cf3ec151SWarner Losh 
985cf3ec151SWarner Losh 	return (error);
986cf3ec151SWarner Losh }
987cf3ec151SWarner Losh 
9882d22619aSWarner Losh static int
9892d22619aSWarner Losh cam_iosched_quanta_sysctl(SYSCTL_HANDLER_ARGS)
9902d22619aSWarner Losh {
9912d22619aSWarner Losh 	int *quanta;
9922d22619aSWarner Losh 	int error, value;
9932d22619aSWarner Losh 
9942d22619aSWarner Losh 	quanta = (unsigned *)arg1;
9952d22619aSWarner Losh 	value = *quanta;
9962d22619aSWarner Losh 
9972d22619aSWarner Losh 	error = sysctl_handle_int(oidp, (int *)&value, 0, req);
9982d22619aSWarner Losh 	if ((error != 0) || (req->newptr == NULL))
9992d22619aSWarner Losh 		return (error);
10002d22619aSWarner Losh 
10012d22619aSWarner Losh 	if (value < 1 || value > hz)
10022d22619aSWarner Losh 		return (EINVAL);
10032d22619aSWarner Losh 
10042d22619aSWarner Losh 	*quanta = value;
10052d22619aSWarner Losh 
10062d22619aSWarner Losh 	return (0);
10072d22619aSWarner Losh }
10082d22619aSWarner Losh 
1009ba6c22ceSWarner Losh static void
1010ba6c22ceSWarner Losh cam_iosched_iop_stats_sysctl_init(struct cam_iosched_softc *isc, struct iop_stats *ios, char *name)
1011ba6c22ceSWarner Losh {
1012ba6c22ceSWarner Losh 	struct sysctl_oid_list *n;
1013ba6c22ceSWarner Losh 	struct sysctl_ctx_list *ctx;
1014ba6c22ceSWarner Losh 
1015ba6c22ceSWarner Losh 	ios->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx,
1016ba6c22ceSWarner Losh 	    SYSCTL_CHILDREN(isc->sysctl_tree), OID_AUTO, name,
10177029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, name);
1018ba6c22ceSWarner Losh 	n = SYSCTL_CHILDREN(ios->sysctl_tree);
1019ba6c22ceSWarner Losh 	ctx = &ios->sysctl_ctx;
1020ba6c22ceSWarner Losh 
1021ba6c22ceSWarner Losh 	SYSCTL_ADD_UQUAD(ctx, n,
1022ba6c22ceSWarner Losh 	    OID_AUTO, "ema", CTLFLAG_RD,
1023ba6c22ceSWarner Losh 	    &ios->ema,
1024ba6c22ceSWarner Losh 	    "Fast Exponentially Weighted Moving Average");
1025ba6c22ceSWarner Losh 	SYSCTL_ADD_UQUAD(ctx, n,
102679d80af2SWarner Losh 	    OID_AUTO, "emvar", CTLFLAG_RD,
102779d80af2SWarner Losh 	    &ios->emvar,
102879d80af2SWarner Losh 	    "Fast Exponentially Weighted Moving Variance");
1029ba6c22ceSWarner Losh 
1030ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1031ba6c22ceSWarner Losh 	    OID_AUTO, "pending", CTLFLAG_RD,
1032ba6c22ceSWarner Losh 	    &ios->pending, 0,
1033ba6c22ceSWarner Losh 	    "Instantaneous # of pending transactions");
1034ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1035ba6c22ceSWarner Losh 	    OID_AUTO, "count", CTLFLAG_RD,
1036ba6c22ceSWarner Losh 	    &ios->total, 0,
1037ba6c22ceSWarner Losh 	    "# of transactions submitted to hardware");
1038ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1039ba6c22ceSWarner Losh 	    OID_AUTO, "queued", CTLFLAG_RD,
1040ba6c22ceSWarner Losh 	    &ios->queued, 0,
1041ba6c22ceSWarner Losh 	    "# of transactions in the queue");
1042ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1043ba6c22ceSWarner Losh 	    OID_AUTO, "in", CTLFLAG_RD,
1044ba6c22ceSWarner Losh 	    &ios->in, 0,
1045ba6c22ceSWarner Losh 	    "# of transactions queued to driver");
1046ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1047ba6c22ceSWarner Losh 	    OID_AUTO, "out", CTLFLAG_RD,
1048ba6c22ceSWarner Losh 	    &ios->out, 0,
1049c4b72d8bSWarner Losh 	    "# of transactions completed (including with error)");
1050c4b72d8bSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1051c4b72d8bSWarner Losh 	    OID_AUTO, "errs", CTLFLAG_RD,
1052c4b72d8bSWarner Losh 	    &ios->errs, 0,
1053c4b72d8bSWarner Losh 	    "# of transactions completed with an error");
1054e82644e5SWarner Losh 	SYSCTL_ADD_U64(ctx, n,
1055e82644e5SWarner Losh 	    OID_AUTO, "too_long", CTLFLAG_RD,
1056e82644e5SWarner Losh 	    &ios->too_long, 0,
1057e82644e5SWarner Losh 	    "# of transactions completed took too long");
1058e82644e5SWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
1059e82644e5SWarner Losh 	    OID_AUTO, "bad_latency",
1060e82644e5SWarner Losh 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1061e82644e5SWarner Losh 	    &ios->bad_latency, 0, cam_iosched_sbintime_sysctl, "A",
1062e82644e5SWarner Losh 	    "Threshold for counting transactions that took too long (in us)");
1063ba6c22ceSWarner Losh 
1064ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
10657029da5cSPawel Biernacki 	    OID_AUTO, "limiter",
1066303477d3SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1067ba6c22ceSWarner Losh 	    ios, 0, cam_iosched_limiter_sysctl, "A",
1068ba6c22ceSWarner Losh 	    "Current limiting type.");
1069ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1070ba6c22ceSWarner Losh 	    OID_AUTO, "min", CTLFLAG_RW,
1071ba6c22ceSWarner Losh 	    &ios->min, 0,
1072ba6c22ceSWarner Losh 	    "min resource");
1073ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1074ba6c22ceSWarner Losh 	    OID_AUTO, "max", CTLFLAG_RW,
1075ba6c22ceSWarner Losh 	    &ios->max, 0,
1076ba6c22ceSWarner Losh 	    "max resource");
1077ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1078ba6c22ceSWarner Losh 	    OID_AUTO, "current", CTLFLAG_RW,
1079ba6c22ceSWarner Losh 	    &ios->current, 0,
1080ba6c22ceSWarner Losh 	    "current resource");
1081ba6c22ceSWarner Losh 
1082cf3ec151SWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
10837029da5cSPawel Biernacki 	    OID_AUTO, "latencies",
1084303477d3SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1085cf3ec151SWarner Losh 	    &ios->latencies, 0,
1086cf3ec151SWarner Losh 	    cam_iosched_sysctl_latencies, "A",
1087d21c19d4SWarner Losh 	    "Array of latencies, a geometric progresson from\n"
1088d21c19d4SWarner Losh 	    "kern.cam.iosched.bucket_base_us with a ratio of\n"
1089d21c19d4SWarner Losh 	    "kern.cam.iosched.bucket_ration / 100 from one to\n"
1090d21c19d4SWarner Losh 	    "the next. By default 20 steps from 20us to 10.485s\n"
1091d21c19d4SWarner Losh 	    "by doubling.");
1092d21c19d4SWarner Losh 
1093ba6c22ceSWarner Losh }
1094ba6c22ceSWarner Losh 
1095ba6c22ceSWarner Losh static void
1096ba6c22ceSWarner Losh cam_iosched_iop_stats_fini(struct iop_stats *ios)
1097ba6c22ceSWarner Losh {
1098ba6c22ceSWarner Losh 	if (ios->sysctl_tree)
1099ba6c22ceSWarner Losh 		if (sysctl_ctx_free(&ios->sysctl_ctx) != 0)
1100ba6c22ceSWarner Losh 			printf("can't remove iosched sysctl stats context\n");
1101ba6c22ceSWarner Losh }
1102ba6c22ceSWarner Losh 
1103ba6c22ceSWarner Losh static void
1104ba6c22ceSWarner Losh cam_iosched_cl_sysctl_init(struct cam_iosched_softc *isc)
1105ba6c22ceSWarner Losh {
1106ba6c22ceSWarner Losh 	struct sysctl_oid_list *n;
1107ba6c22ceSWarner Losh 	struct sysctl_ctx_list *ctx;
1108ba6c22ceSWarner Losh 	struct control_loop *clp;
1109ba6c22ceSWarner Losh 
1110ba6c22ceSWarner Losh 	clp = &isc->cl;
1111ba6c22ceSWarner Losh 	clp->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx,
1112ba6c22ceSWarner Losh 	    SYSCTL_CHILDREN(isc->sysctl_tree), OID_AUTO, "control",
11137029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Control loop info");
1114ba6c22ceSWarner Losh 	n = SYSCTL_CHILDREN(clp->sysctl_tree);
1115ba6c22ceSWarner Losh 	ctx = &clp->sysctl_ctx;
1116ba6c22ceSWarner Losh 
1117ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
11187029da5cSPawel Biernacki 	    OID_AUTO, "type",
1119303477d3SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1120ba6c22ceSWarner Losh 	    clp, 0, cam_iosched_control_type_sysctl, "A",
1121ba6c22ceSWarner Losh 	    "Control loop algorithm");
1122ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
11237029da5cSPawel Biernacki 	    OID_AUTO, "steer_interval",
1124303477d3SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1125ba6c22ceSWarner Losh 	    &clp->steer_interval, 0, cam_iosched_sbintime_sysctl, "A",
1126ba6c22ceSWarner Losh 	    "How often to steer (in us)");
1127ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
11287029da5cSPawel Biernacki 	    OID_AUTO, "lolat",
1129303477d3SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1130ba6c22ceSWarner Losh 	    &clp->lolat, 0, cam_iosched_sbintime_sysctl, "A",
1131ba6c22ceSWarner Losh 	    "Low water mark for Latency (in us)");
1132ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
11337029da5cSPawel Biernacki 	    OID_AUTO, "hilat",
1134303477d3SAlexander Motin 	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_MPSAFE,
1135ba6c22ceSWarner Losh 	    &clp->hilat, 0, cam_iosched_sbintime_sysctl, "A",
1136ba6c22ceSWarner Losh 	    "Hi water mark for Latency (in us)");
1137ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1138ba6c22ceSWarner Losh 	    OID_AUTO, "alpha", CTLFLAG_RW,
1139ba6c22ceSWarner Losh 	    &clp->alpha, 0,
1140ba6c22ceSWarner Losh 	    "Alpha for PLL (x100) aka gain");
1141ba6c22ceSWarner Losh }
1142ba6c22ceSWarner Losh 
1143ba6c22ceSWarner Losh static void
1144ba6c22ceSWarner Losh cam_iosched_cl_sysctl_fini(struct control_loop *clp)
1145ba6c22ceSWarner Losh {
1146ba6c22ceSWarner Losh 	if (clp->sysctl_tree)
1147ba6c22ceSWarner Losh 		if (sysctl_ctx_free(&clp->sysctl_ctx) != 0)
1148ba6c22ceSWarner Losh 			printf("can't remove iosched sysctl control loop context\n");
1149ba6c22ceSWarner Losh }
1150ba6c22ceSWarner Losh #endif
1151ba6c22ceSWarner Losh 
1152ba6c22ceSWarner Losh /*
1153ba6c22ceSWarner Losh  * Allocate the iosched structure. This also insulates callers from knowing
1154ba6c22ceSWarner Losh  * sizeof struct cam_iosched_softc.
1155ba6c22ceSWarner Losh  */
1156ba6c22ceSWarner Losh int
11576f1dd607SWarner Losh cam_iosched_init(struct cam_iosched_softc **iscp, struct cam_periph *periph,
11586f1dd607SWarner Losh     const struct disk *dp)
1159ba6c22ceSWarner Losh {
1160ba6c22ceSWarner Losh 
1161ba6c22ceSWarner Losh 	*iscp = malloc(sizeof(**iscp), M_CAMSCHED, M_NOWAIT | M_ZERO);
1162ba6c22ceSWarner Losh 	if (*iscp == NULL)
1163ba6c22ceSWarner Losh 		return ENOMEM;
11646f1dd607SWarner Losh 	(*iscp)->disk = dp;
1165df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1166ba6c22ceSWarner Losh 	if (iosched_debug)
1167ba6c22ceSWarner Losh 		printf("CAM IOSCHEDULER Allocating entry at %p\n", *iscp);
1168ba6c22ceSWarner Losh #endif
1169ba6c22ceSWarner Losh 	(*iscp)->sort_io_queue = -1;
1170ba6c22ceSWarner Losh 	bioq_init(&(*iscp)->bio_queue);
1171ba6c22ceSWarner Losh 	bioq_init(&(*iscp)->trim_queue);
1172df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1173035ec48eSWarner Losh 	if (do_dynamic_iosched) {
1174ba6c22ceSWarner Losh 		bioq_init(&(*iscp)->write_queue);
1175d592c0dbSWarner Losh 		(*iscp)->read_bias = default_read_bias;
1176b65803baSWarner Losh 		(*iscp)->current_read_bias = 0;
1177d7fa1ab0SWarner Losh 		(*iscp)->quanta = min(hz, 200);
1178ba6c22ceSWarner Losh 		cam_iosched_iop_stats_init(*iscp, &(*iscp)->read_stats);
1179ba6c22ceSWarner Losh 		cam_iosched_iop_stats_init(*iscp, &(*iscp)->write_stats);
1180ba6c22ceSWarner Losh 		cam_iosched_iop_stats_init(*iscp, &(*iscp)->trim_stats);
1181ba6c22ceSWarner Losh 		(*iscp)->trim_stats.max = 1;	/* Trims are special: one at a time for now */
1182ba6c22ceSWarner Losh 		(*iscp)->last_time = sbinuptime();
1183ba6c22ceSWarner Losh 		callout_init_mtx(&(*iscp)->ticker, cam_periph_mtx(periph), 0);
1184ba6c22ceSWarner Losh 		(*iscp)->periph = periph;
1185ba6c22ceSWarner Losh 		cam_iosched_cl_init(&(*iscp)->cl, *iscp);
11863028dd8dSWarner Losh 		callout_reset(&(*iscp)->ticker, hz / (*iscp)->quanta, cam_iosched_ticker, *iscp);
1187ba6c22ceSWarner Losh 		(*iscp)->flags |= CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
1188ba6c22ceSWarner Losh 	}
1189ba6c22ceSWarner Losh #endif
1190ba6c22ceSWarner Losh 
1191ba6c22ceSWarner Losh 	return 0;
1192ba6c22ceSWarner Losh }
1193ba6c22ceSWarner Losh 
1194ba6c22ceSWarner Losh /*
1195ba6c22ceSWarner Losh  * Reclaim all used resources. This assumes that other folks have
1196ba6c22ceSWarner Losh  * drained the requests in the hardware. Maybe an unwise assumption.
1197ba6c22ceSWarner Losh  */
1198ba6c22ceSWarner Losh void
1199ba6c22ceSWarner Losh cam_iosched_fini(struct cam_iosched_softc *isc)
1200ba6c22ceSWarner Losh {
1201ba6c22ceSWarner Losh 	if (isc) {
1202ba6c22ceSWarner Losh 		cam_iosched_flush(isc, NULL, ENXIO);
1203df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1204ba6c22ceSWarner Losh 		cam_iosched_iop_stats_fini(&isc->read_stats);
1205ba6c22ceSWarner Losh 		cam_iosched_iop_stats_fini(&isc->write_stats);
1206ba6c22ceSWarner Losh 		cam_iosched_iop_stats_fini(&isc->trim_stats);
1207ba6c22ceSWarner Losh 		cam_iosched_cl_sysctl_fini(&isc->cl);
1208ba6c22ceSWarner Losh 		if (isc->sysctl_tree)
1209ba6c22ceSWarner Losh 			if (sysctl_ctx_free(&isc->sysctl_ctx) != 0)
1210ba6c22ceSWarner Losh 				printf("can't remove iosched sysctl stats context\n");
1211ba6c22ceSWarner Losh 		if (isc->flags & CAM_IOSCHED_FLAG_CALLOUT_ACTIVE) {
1212ba6c22ceSWarner Losh 			callout_drain(&isc->ticker);
1213ba6c22ceSWarner Losh 			isc->flags &= ~ CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
1214ba6c22ceSWarner Losh 		}
1215ba6c22ceSWarner Losh #endif
1216ba6c22ceSWarner Losh 		free(isc, M_CAMSCHED);
1217ba6c22ceSWarner Losh 	}
1218ba6c22ceSWarner Losh }
1219ba6c22ceSWarner Losh 
1220ba6c22ceSWarner Losh /*
1221ba6c22ceSWarner Losh  * After we're sure we're attaching a device, go ahead and add
1222ba6c22ceSWarner Losh  * hooks for any sysctl we may wish to honor.
1223ba6c22ceSWarner Losh  */
1224ba6c22ceSWarner Losh void cam_iosched_sysctl_init(struct cam_iosched_softc *isc,
1225ba6c22ceSWarner Losh     struct sysctl_ctx_list *ctx, struct sysctl_oid *node)
1226ba6c22ceSWarner Losh {
1227ba6c22ceSWarner Losh 	struct sysctl_oid_list *n;
1228ba6c22ceSWarner Losh 
1229d900ade5SWarner Losh 	n = SYSCTL_CHILDREN(node);
1230d900ade5SWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1231ba6c22ceSWarner Losh 		OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1232ba6c22ceSWarner Losh 		&isc->sort_io_queue, 0,
1233ba6c22ceSWarner Losh 		"Sort IO queue to try and optimise disk access patterns");
1234d900ade5SWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1235d900ade5SWarner Losh 	    OID_AUTO, "trim_goal", CTLFLAG_RW,
1236d900ade5SWarner Losh 	    &isc->trim_goal, 0,
1237d900ade5SWarner Losh 	    "Number of trims to try to accumulate before sending to hardware");
1238d900ade5SWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1239d900ade5SWarner Losh 	    OID_AUTO, "trim_ticks", CTLFLAG_RW,
1240d900ade5SWarner Losh 	    &isc->trim_goal, 0,
1241d900ade5SWarner Losh 	    "IO Schedul qaunta to hold back trims for when accumulating");
1242ba6c22ceSWarner Losh 
1243df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1244035ec48eSWarner Losh 	if (!do_dynamic_iosched)
1245ba6c22ceSWarner Losh 		return;
1246ba6c22ceSWarner Losh 
1247ba6c22ceSWarner Losh 	isc->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx,
1248ba6c22ceSWarner Losh 	    SYSCTL_CHILDREN(node), OID_AUTO, "iosched",
12497029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "I/O scheduler statistics");
1250ba6c22ceSWarner Losh 	n = SYSCTL_CHILDREN(isc->sysctl_tree);
1251ba6c22ceSWarner Losh 	ctx = &isc->sysctl_ctx;
1252ba6c22ceSWarner Losh 
1253ba6c22ceSWarner Losh 	cam_iosched_iop_stats_sysctl_init(isc, &isc->read_stats, "read");
1254ba6c22ceSWarner Losh 	cam_iosched_iop_stats_sysctl_init(isc, &isc->write_stats, "write");
1255ba6c22ceSWarner Losh 	cam_iosched_iop_stats_sysctl_init(isc, &isc->trim_stats, "trim");
1256ba6c22ceSWarner Losh 	cam_iosched_cl_sysctl_init(isc);
1257ba6c22ceSWarner Losh 
1258ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1259ba6c22ceSWarner Losh 	    OID_AUTO, "read_bias", CTLFLAG_RW,
1260d592c0dbSWarner Losh 	    &isc->read_bias, default_read_bias,
1261ba6c22ceSWarner Losh 	    "How biased towards read should we be independent of limits");
1262ba6c22ceSWarner Losh 
12632d22619aSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
1264303477d3SAlexander Motin 	    OID_AUTO, "quanta", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
12652d22619aSWarner Losh 	    &isc->quanta, 0, cam_iosched_quanta_sysctl, "I",
1266ba6c22ceSWarner Losh 	    "How many quanta per second do we slice the I/O up into");
1267ba6c22ceSWarner Losh 
1268ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1269ba6c22ceSWarner Losh 	    OID_AUTO, "total_ticks", CTLFLAG_RD,
1270ba6c22ceSWarner Losh 	    &isc->total_ticks, 0,
1271ba6c22ceSWarner Losh 	    "Total number of ticks we've done");
1272cf3ec151SWarner Losh 
1273cf3ec151SWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1274cf3ec151SWarner Losh 	    OID_AUTO, "load", CTLFLAG_RD,
1275cf3ec151SWarner Losh 	    &isc->load, 0,
1276cf3ec151SWarner Losh 	    "scaled load average / 100");
1277e5436ab5SWarner Losh 
1278e5436ab5SWarner Losh 	SYSCTL_ADD_U64(ctx, n,
1279e5436ab5SWarner Losh 	    OID_AUTO, "latency_trigger", CTLFLAG_RW,
1280e5436ab5SWarner Losh 	    &isc->max_lat, 0,
1281e5436ab5SWarner Losh 	    "Latency treshold to trigger callbacks");
1282e5436ab5SWarner Losh #endif
1283e5436ab5SWarner Losh }
1284e5436ab5SWarner Losh 
1285e5436ab5SWarner Losh void
1286e5436ab5SWarner Losh cam_iosched_set_latfcn(struct cam_iosched_softc *isc,
1287e5436ab5SWarner Losh     cam_iosched_latfcn_t fnp, void *argp)
1288e5436ab5SWarner Losh {
1289e5436ab5SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1290e5436ab5SWarner Losh 	isc->latfcn = fnp;
1291e5436ab5SWarner Losh 	isc->latarg = argp;
1292ba6c22ceSWarner Losh #endif
1293ba6c22ceSWarner Losh }
1294ba6c22ceSWarner Losh 
1295ba6c22ceSWarner Losh /*
1296d900ade5SWarner Losh  * Client drivers can set two parameters. "goal" is the number of BIO_DELETEs
1297d900ade5SWarner Losh  * that will be queued up before iosched will "release" the trims to the client
1298d900ade5SWarner Losh  * driver to wo with what they will (usually combine as many as possible). If we
1299d900ade5SWarner Losh  * don't get this many, after trim_ticks we'll submit the I/O anyway with
1300d900ade5SWarner Losh  * whatever we have.  We do need an I/O of some kind of to clock the deferred
1301d900ade5SWarner Losh  * trims out to disk. Since we will eventually get a write for the super block
1302d900ade5SWarner Losh  * or something before we shutdown, the trims will complete. To be safe, when a
1303d900ade5SWarner Losh  * BIO_FLUSH is presented to the iosched work queue, we set the ticks time far
1304d900ade5SWarner Losh  * enough in the past so we'll present the BIO_DELETEs to the client driver.
1305d900ade5SWarner Losh  * There might be a race if no BIO_DELETESs were queued, a BIO_FLUSH comes in
1306d900ade5SWarner Losh  * and then a BIO_DELETE is sent down. No know client does this, and there's
1307d900ade5SWarner Losh  * already a race between an ordered BIO_FLUSH and any BIO_DELETEs in flight,
1308d900ade5SWarner Losh  * but no client depends on the ordering being honored.
1309d900ade5SWarner Losh  *
1310d900ade5SWarner Losh  * XXX I'm not sure what the interaction between UFS direct BIOs and the BUF
1311d900ade5SWarner Losh  * flushing on shutdown. I think there's bufs that would be dependent on the BIO
1312d900ade5SWarner Losh  * finishing to write out at least metadata, so we'll be fine. To be safe, keep
1313d900ade5SWarner Losh  * the number of ticks low (less than maybe 10s) to avoid shutdown races.
1314d900ade5SWarner Losh  */
1315d900ade5SWarner Losh 
1316d900ade5SWarner Losh void
1317d900ade5SWarner Losh cam_iosched_set_trim_goal(struct cam_iosched_softc *isc, int goal)
1318d900ade5SWarner Losh {
1319d900ade5SWarner Losh 
1320d900ade5SWarner Losh 	isc->trim_goal = goal;
1321d900ade5SWarner Losh }
1322d900ade5SWarner Losh 
1323d900ade5SWarner Losh void
1324d900ade5SWarner Losh cam_iosched_set_trim_ticks(struct cam_iosched_softc *isc, int trim_ticks)
1325d900ade5SWarner Losh {
1326d900ade5SWarner Losh 
1327d900ade5SWarner Losh 	isc->trim_ticks = trim_ticks;
1328d900ade5SWarner Losh }
1329d900ade5SWarner Losh 
1330d900ade5SWarner Losh /*
1331ba6c22ceSWarner Losh  * Flush outstanding I/O. Consumers of this library don't know all the
1332ba6c22ceSWarner Losh  * queues we may keep, so this allows all I/O to be flushed in one
1333ba6c22ceSWarner Losh  * convenient call.
1334ba6c22ceSWarner Losh  */
1335ba6c22ceSWarner Losh void
1336ba6c22ceSWarner Losh cam_iosched_flush(struct cam_iosched_softc *isc, struct devstat *stp, int err)
1337ba6c22ceSWarner Losh {
1338ba6c22ceSWarner Losh 	bioq_flush(&isc->bio_queue, stp, err);
1339ba6c22ceSWarner Losh 	bioq_flush(&isc->trim_queue, stp, err);
1340df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1341035ec48eSWarner Losh 	if (do_dynamic_iosched)
1342ba6c22ceSWarner Losh 		bioq_flush(&isc->write_queue, stp, err);
1343ba6c22ceSWarner Losh #endif
1344ba6c22ceSWarner Losh }
1345ba6c22ceSWarner Losh 
1346df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1347ba6c22ceSWarner Losh static struct bio *
13480028abe6SWarner Losh cam_iosched_get_write(struct cam_iosched_softc *isc)
1349ba6c22ceSWarner Losh {
1350ba6c22ceSWarner Losh 	struct bio *bp;
1351ba6c22ceSWarner Losh 
1352ba6c22ceSWarner Losh 	/*
1353ba6c22ceSWarner Losh 	 * We control the write rate by controlling how many requests we send
1354ba6c22ceSWarner Losh 	 * down to the drive at any one time. Fewer requests limits the
1355ba6c22ceSWarner Losh 	 * effects of both starvation when the requests take a while and write
1356ba6c22ceSWarner Losh 	 * amplification when each request is causing more than one write to
1357ba6c22ceSWarner Losh 	 * the NAND media. Limiting the queue depth like this will also limit
1358ba6c22ceSWarner Losh 	 * the write throughput and give and reads that want to compete to
1359ba6c22ceSWarner Losh 	 * compete unfairly.
1360ba6c22ceSWarner Losh 	 */
1361ba6c22ceSWarner Losh 	bp = bioq_first(&isc->write_queue);
1362ba6c22ceSWarner Losh 	if (bp == NULL) {
1363ba6c22ceSWarner Losh 		if (iosched_debug > 3)
1364ba6c22ceSWarner Losh 			printf("No writes present in write_queue\n");
1365ba6c22ceSWarner Losh 		return NULL;
1366ba6c22ceSWarner Losh 	}
1367ba6c22ceSWarner Losh 
1368ba6c22ceSWarner Losh 	/*
1369ba6c22ceSWarner Losh 	 * If pending read, prefer that based on current read bias
1370ba6c22ceSWarner Losh 	 * setting.
1371ba6c22ceSWarner Losh 	 */
1372ba6c22ceSWarner Losh 	if (bioq_first(&isc->bio_queue) && isc->current_read_bias) {
1373ba6c22ceSWarner Losh 		if (iosched_debug)
1374f2b98850SWarner Losh 			printf(
1375f2b98850SWarner Losh 			    "Reads present and current_read_bias is %d queued "
1376f2b98850SWarner Losh 			    "writes %d queued reads %d\n",
1377f2b98850SWarner Losh 			    isc->current_read_bias, isc->write_stats.queued,
1378f2b98850SWarner Losh 			    isc->read_stats.queued);
1379ba6c22ceSWarner Losh 		isc->current_read_bias--;
1380cf3ec151SWarner Losh 		/* We're not limiting writes, per se, just doing reads first */
1381ba6c22ceSWarner Losh 		return NULL;
1382ba6c22ceSWarner Losh 	}
1383ba6c22ceSWarner Losh 
1384ba6c22ceSWarner Losh 	/*
1385ba6c22ceSWarner Losh 	 * See if our current limiter allows this I/O.
1386ba6c22ceSWarner Losh 	 */
1387ba6c22ceSWarner Losh 	if (cam_iosched_limiter_iop(&isc->write_stats, bp) != 0) {
1388ba6c22ceSWarner Losh 		if (iosched_debug)
1389ba6c22ceSWarner Losh 			printf("Can't write because limiter says no.\n");
1390cf3ec151SWarner Losh 		isc->write_stats.state_flags |= IOP_RATE_LIMITED;
1391ba6c22ceSWarner Losh 		return NULL;
1392ba6c22ceSWarner Losh 	}
1393ba6c22ceSWarner Losh 
1394ba6c22ceSWarner Losh 	/*
1395ba6c22ceSWarner Losh 	 * Let's do this: We've passed all the gates and we're a go
1396ba6c22ceSWarner Losh 	 * to schedule the I/O in the SIM.
1397ba6c22ceSWarner Losh 	 */
1398ba6c22ceSWarner Losh 	isc->current_read_bias = isc->read_bias;
1399ba6c22ceSWarner Losh 	bioq_remove(&isc->write_queue, bp);
1400ba6c22ceSWarner Losh 	if (bp->bio_cmd == BIO_WRITE) {
1401ba6c22ceSWarner Losh 		isc->write_stats.queued--;
1402ba6c22ceSWarner Losh 		isc->write_stats.total++;
1403ba6c22ceSWarner Losh 		isc->write_stats.pending++;
1404ba6c22ceSWarner Losh 	}
1405ba6c22ceSWarner Losh 	if (iosched_debug > 9)
1406ba6c22ceSWarner Losh 		printf("HWQ : %p %#x\n", bp, bp->bio_cmd);
1407cf3ec151SWarner Losh 	isc->write_stats.state_flags &= ~IOP_RATE_LIMITED;
1408ba6c22ceSWarner Losh 	return bp;
1409ba6c22ceSWarner Losh }
1410ba6c22ceSWarner Losh #endif
1411ba6c22ceSWarner Losh 
1412ba6c22ceSWarner Losh /*
1413ba6c22ceSWarner Losh  * Put back a trim that you weren't able to actually schedule this time.
1414ba6c22ceSWarner Losh  */
1415ba6c22ceSWarner Losh void
1416ba6c22ceSWarner Losh cam_iosched_put_back_trim(struct cam_iosched_softc *isc, struct bio *bp)
1417ba6c22ceSWarner Losh {
1418ba6c22ceSWarner Losh 	bioq_insert_head(&isc->trim_queue, bp);
1419d900ade5SWarner Losh 	if (isc->queued_trims == 0)
1420d900ade5SWarner Losh 		isc->last_trim_tick = ticks;
1421d900ade5SWarner Losh 	isc->queued_trims++;
1422df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1423ba6c22ceSWarner Losh 	isc->trim_stats.queued++;
1424ba6c22ceSWarner Losh 	isc->trim_stats.total--;		/* since we put it back, don't double count */
1425ba6c22ceSWarner Losh 	isc->trim_stats.pending--;
1426ba6c22ceSWarner Losh #endif
1427ba6c22ceSWarner Losh }
1428ba6c22ceSWarner Losh 
1429ba6c22ceSWarner Losh /*
1430ba6c22ceSWarner Losh  * gets the next trim from the trim queue.
1431ba6c22ceSWarner Losh  *
1432ba6c22ceSWarner Losh  * Assumes we're called with the periph lock held.  It removes this
14330b4060b0SEd Maste  * trim from the queue and the device must explicitly reinsert it
1434ba6c22ceSWarner Losh  * should the need arise.
1435ba6c22ceSWarner Losh  */
1436ba6c22ceSWarner Losh struct bio *
1437ba6c22ceSWarner Losh cam_iosched_next_trim(struct cam_iosched_softc *isc)
1438ba6c22ceSWarner Losh {
1439ba6c22ceSWarner Losh 	struct bio *bp;
1440ba6c22ceSWarner Losh 
1441ba6c22ceSWarner Losh 	bp  = bioq_first(&isc->trim_queue);
1442ba6c22ceSWarner Losh 	if (bp == NULL)
1443ba6c22ceSWarner Losh 		return NULL;
1444ba6c22ceSWarner Losh 	bioq_remove(&isc->trim_queue, bp);
1445d900ade5SWarner Losh 	isc->queued_trims--;
1446d900ade5SWarner Losh 	isc->last_trim_tick = ticks;	/* Reset the tick timer when we take trims */
1447df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1448ba6c22ceSWarner Losh 	isc->trim_stats.queued--;
1449ba6c22ceSWarner Losh 	isc->trim_stats.total++;
1450ba6c22ceSWarner Losh 	isc->trim_stats.pending++;
1451ba6c22ceSWarner Losh #endif
1452ba6c22ceSWarner Losh 	return bp;
1453ba6c22ceSWarner Losh }
1454ba6c22ceSWarner Losh 
1455ba6c22ceSWarner Losh /*
14560b4060b0SEd Maste  * gets an available trim from the trim queue, if there's no trim
1457ba6c22ceSWarner Losh  * already pending. It removes this trim from the queue and the device
14580b4060b0SEd Maste  * must explicitly reinsert it should the need arise.
1459ba6c22ceSWarner Losh  *
1460ba6c22ceSWarner Losh  * Assumes we're called with the periph lock held.
1461ba6c22ceSWarner Losh  */
14620028abe6SWarner Losh struct bio *
14630028abe6SWarner Losh cam_iosched_get_trim(struct cam_iosched_softc *isc)
1464ba6c22ceSWarner Losh {
1465c6171b44SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1466c6171b44SWarner Losh 	struct bio *bp;
1467c6171b44SWarner Losh #endif
1468ba6c22ceSWarner Losh 
14690028abe6SWarner Losh 	if (!cam_iosched_has_more_trim(isc))
1470ba6c22ceSWarner Losh 		return NULL;
147162c94a05SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1472c6171b44SWarner Losh 	bp  = bioq_first(&isc->trim_queue);
1473c6171b44SWarner Losh 	if (bp == NULL)
1474c6171b44SWarner Losh 		return NULL;
1475c6171b44SWarner Losh 
147662c94a05SWarner Losh 	/*
14771759fd77SWarner Losh 	 * If pending read, prefer that based on current read bias setting. The
14781759fd77SWarner Losh 	 * read bias is shared for both writes and TRIMs, but on TRIMs the bias
14791759fd77SWarner Losh 	 * is for a combined TRIM not a single TRIM request that's come in.
148062c94a05SWarner Losh 	 */
14811759fd77SWarner Losh 	if (do_dynamic_iosched) {
148262c94a05SWarner Losh 		if (bioq_first(&isc->bio_queue) && isc->current_read_bias) {
14831759fd77SWarner Losh 			if (iosched_debug)
14841759fd77SWarner Losh 				printf("Reads present and current_read_bias is %d"
14851759fd77SWarner Losh 				    " queued trims %d queued reads %d\n",
14861759fd77SWarner Losh 				    isc->current_read_bias, isc->trim_stats.queued,
14871759fd77SWarner Losh 				    isc->read_stats.queued);
148862c94a05SWarner Losh 			isc->current_read_bias--;
148962c94a05SWarner Losh 			/* We're not limiting TRIMS, per se, just doing reads first */
149062c94a05SWarner Losh 			return NULL;
149162c94a05SWarner Losh 		}
149262c94a05SWarner Losh 		/*
149362c94a05SWarner Losh 		 * We're going to do a trim, so reset the bias.
149462c94a05SWarner Losh 		 */
149562c94a05SWarner Losh 		isc->current_read_bias = isc->read_bias;
149662c94a05SWarner Losh 	}
1497c6171b44SWarner Losh 
1498c6171b44SWarner Losh 	/*
1499c6171b44SWarner Losh 	 * See if our current limiter allows this I/O. Because we only call this
1500c6171b44SWarner Losh 	 * here, and not in next_trim, the 'bandwidth' limits for trims won't
1501c6171b44SWarner Losh 	 * work, while the iops or max queued limits will work. It's tricky
1502c6171b44SWarner Losh 	 * because we want the limits to be from the perspective of the
1503c6171b44SWarner Losh 	 * "commands sent to the device." To make iops work, we need to check
1504c6171b44SWarner Losh 	 * only here (since we want all the ops we combine to count as one). To
1505c6171b44SWarner Losh 	 * make bw limits work, we'd need to check in next_trim, but that would
1506c6171b44SWarner Losh 	 * have the effect of limiting the iops as seen from the upper layers.
1507c6171b44SWarner Losh 	 */
1508c6171b44SWarner Losh 	if (cam_iosched_limiter_iop(&isc->trim_stats, bp) != 0) {
1509c6171b44SWarner Losh 		if (iosched_debug)
1510c6171b44SWarner Losh 			printf("Can't trim because limiter says no.\n");
1511c6171b44SWarner Losh 		isc->trim_stats.state_flags |= IOP_RATE_LIMITED;
1512c6171b44SWarner Losh 		return NULL;
1513c6171b44SWarner Losh 	}
1514c6171b44SWarner Losh 	isc->current_read_bias = isc->read_bias;
1515c6171b44SWarner Losh 	isc->trim_stats.state_flags &= ~IOP_RATE_LIMITED;
1516c6171b44SWarner Losh 	/* cam_iosched_next_trim below keeps proper book */
151762c94a05SWarner Losh #endif
1518ba6c22ceSWarner Losh 	return cam_iosched_next_trim(isc);
1519ba6c22ceSWarner Losh }
1520ba6c22ceSWarner Losh 
1521cc1572ddSWarner Losh 
15221599fc90SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1523cc1572ddSWarner Losh static struct bio *
1524cc1572ddSWarner Losh bio_next(struct bio *bp)
1525cc1572ddSWarner Losh {
1526cc1572ddSWarner Losh 	bp = TAILQ_NEXT(bp, bio_queue);
1527cc1572ddSWarner Losh 	/*
1528cc1572ddSWarner Losh 	 * After the first commands, the ordered bit terminates
1529cc1572ddSWarner Losh 	 * our search because BIO_ORDERED acts like a barrier.
1530cc1572ddSWarner Losh 	 */
1531cc1572ddSWarner Losh 	if (bp == NULL || bp->bio_flags & BIO_ORDERED)
1532cc1572ddSWarner Losh 		return NULL;
1533cc1572ddSWarner Losh 	return bp;
1534cc1572ddSWarner Losh }
1535cc1572ddSWarner Losh 
1536cc1572ddSWarner Losh static bool
1537cc1572ddSWarner Losh cam_iosched_rate_limited(struct iop_stats *ios)
1538cc1572ddSWarner Losh {
1539cc1572ddSWarner Losh 	return ios->state_flags & IOP_RATE_LIMITED;
1540cc1572ddSWarner Losh }
1541cc1572ddSWarner Losh #endif
1542cc1572ddSWarner Losh 
1543ba6c22ceSWarner Losh /*
1544ba6c22ceSWarner Losh  * Determine what the next bit of work to do is for the periph. The
1545ba6c22ceSWarner Losh  * default implementation looks to see if we have trims to do, but no
1546ba6c22ceSWarner Losh  * trims outstanding. If so, we do that. Otherwise we see if we have
1547ba6c22ceSWarner Losh  * other work. If we do, then we do that. Otherwise why were we called?
1548ba6c22ceSWarner Losh  */
1549ba6c22ceSWarner Losh struct bio *
1550ba6c22ceSWarner Losh cam_iosched_next_bio(struct cam_iosched_softc *isc)
1551ba6c22ceSWarner Losh {
1552ba6c22ceSWarner Losh 	struct bio *bp;
1553ba6c22ceSWarner Losh 
1554ba6c22ceSWarner Losh 	/*
1555ba6c22ceSWarner Losh 	 * See if we have a trim that can be scheduled. We can only send one
15560028abe6SWarner Losh 	 * at a time down, so this takes that into account.
15570028abe6SWarner Losh 	 *
15580028abe6SWarner Losh 	 * XXX newer TRIM commands are queueable. Revisit this when we
15590028abe6SWarner Losh 	 * implement them.
1560ba6c22ceSWarner Losh 	 */
15610028abe6SWarner Losh 	if ((bp = cam_iosched_get_trim(isc)) != NULL)
1562ba6c22ceSWarner Losh 		return bp;
1563ba6c22ceSWarner Losh 
1564df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1565ba6c22ceSWarner Losh 	/*
1566e3d92d4cSWarner Losh 	 * See if we have any pending writes, room in the queue for them,
1567e3d92d4cSWarner Losh 	 * and no pending reads (unless we've scheduled too many).
1568e3d92d4cSWarner Losh 	 * if so, those are next.
1569ba6c22ceSWarner Losh 	 */
1570035ec48eSWarner Losh 	if (do_dynamic_iosched) {
15710028abe6SWarner Losh 		if ((bp = cam_iosched_get_write(isc)) != NULL)
1572ba6c22ceSWarner Losh 			return bp;
1573ba6c22ceSWarner Losh 	}
1574ba6c22ceSWarner Losh #endif
1575ba6c22ceSWarner Losh 	/*
1576ba6c22ceSWarner Losh 	 * next, see if there's other, normal I/O waiting. If so return that.
1577ba6c22ceSWarner Losh 	 */
1578df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1579035ec48eSWarner Losh 	if (do_dynamic_iosched) {
1580cc1572ddSWarner Losh 		for (bp = bioq_first(&isc->bio_queue); bp != NULL;
1581cc1572ddSWarner Losh 		     bp = bio_next(bp)) {
1582cc1572ddSWarner Losh 			/*
1583cc1572ddSWarner Losh 			 * For the dynamic scheduler with a read bias, bio_queue
1584cc1572ddSWarner Losh 			 * is only for reads. However, without one, all
1585cc1572ddSWarner Losh 			 * operations are queued. Enforce limits here for any
1586cc1572ddSWarner Losh 			 * operation we find here.
1587cc1572ddSWarner Losh 			 */
1588cc1572ddSWarner Losh 			if (bp->bio_cmd == BIO_READ) {
1589cc1572ddSWarner Losh 				if (cam_iosched_rate_limited(&isc->read_stats) ||
1590cf3ec151SWarner Losh 				    cam_iosched_limiter_iop(&isc->read_stats, bp) != 0) {
1591cf3ec151SWarner Losh 					isc->read_stats.state_flags |= IOP_RATE_LIMITED;
1592cc1572ddSWarner Losh 					continue;
1593cf3ec151SWarner Losh 				}
1594cf3ec151SWarner Losh 				isc->read_stats.state_flags &= ~IOP_RATE_LIMITED;
1595cc1572ddSWarner Losh 			}
1596cc1572ddSWarner Losh 			/*
1597cc1572ddSWarner Losh 			 * There can only be write requests on the queue when
1598cc1572ddSWarner Losh 			 * the read bias is 0, but we need to process them
1599cc1572ddSWarner Losh 			 * here. We do not assert for read bias == 0, however,
1600cc1572ddSWarner Losh 			 * since it is dynamic and we can have WRITE operations
1601cc1572ddSWarner Losh 			 * in the queue after we transition from 0 to non-zero.
1602cc1572ddSWarner Losh 			 */
1603cc1572ddSWarner Losh 			if (bp->bio_cmd == BIO_WRITE) {
1604cc1572ddSWarner Losh 				if (cam_iosched_rate_limited(&isc->write_stats) ||
1605cc1572ddSWarner Losh 				    cam_iosched_limiter_iop(&isc->write_stats, bp) != 0) {
1606cc1572ddSWarner Losh 					isc->write_stats.state_flags |= IOP_RATE_LIMITED;
1607cc1572ddSWarner Losh 					continue;
1608cc1572ddSWarner Losh 				}
1609cc1572ddSWarner Losh 				isc->write_stats.state_flags &= ~IOP_RATE_LIMITED;
1610cc1572ddSWarner Losh 			}
1611cc1572ddSWarner Losh 			/*
1612cc1572ddSWarner Losh 			 * here we know we have a bp that's != NULL, that's not rate limited
1613cc1572ddSWarner Losh 			 * and can be the next I/O.
1614cc1572ddSWarner Losh 			 */
1615cc1572ddSWarner Losh 			break;
1616cc1572ddSWarner Losh 		}
1617cc1572ddSWarner Losh 	} else
1618ba6c22ceSWarner Losh #endif
1619cc1572ddSWarner Losh 		bp = bioq_first(&isc->bio_queue);
1620cc1572ddSWarner Losh 
1621cc1572ddSWarner Losh 	if (bp == NULL)
1622cc1572ddSWarner Losh 		return (NULL);
1623ba6c22ceSWarner Losh 	bioq_remove(&isc->bio_queue, bp);
1624df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1625035ec48eSWarner Losh 	if (do_dynamic_iosched) {
1626ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_READ) {
1627ba6c22ceSWarner Losh 			isc->read_stats.queued--;
1628ba6c22ceSWarner Losh 			isc->read_stats.total++;
1629ba6c22ceSWarner Losh 			isc->read_stats.pending++;
1630cc1572ddSWarner Losh 		} else if (bp->bio_cmd == BIO_WRITE) {
1631cc1572ddSWarner Losh 			isc->write_stats.queued--;
1632cc1572ddSWarner Losh 			isc->write_stats.total++;
1633cc1572ddSWarner Losh 			isc->write_stats.pending++;
1634a85fea31SWarner Losh 		}
1635ba6c22ceSWarner Losh 	}
1636ba6c22ceSWarner Losh 	if (iosched_debug > 9)
1637ba6c22ceSWarner Losh 		printf("HWQ : %p %#x\n", bp, bp->bio_cmd);
1638ba6c22ceSWarner Losh #endif
1639ba6c22ceSWarner Losh 	return bp;
1640ba6c22ceSWarner Losh }
1641ba6c22ceSWarner Losh 
1642ba6c22ceSWarner Losh /*
1643ba6c22ceSWarner Losh  * Driver has been given some work to do by the block layer. Tell the
1644ba6c22ceSWarner Losh  * scheduler about it and have it queue the work up. The scheduler module
1645ba6c22ceSWarner Losh  * will then return the currently most useful bit of work later, possibly
1646ba6c22ceSWarner Losh  * deferring work for various reasons.
1647ba6c22ceSWarner Losh  */
1648ba6c22ceSWarner Losh void
1649ba6c22ceSWarner Losh cam_iosched_queue_work(struct cam_iosched_softc *isc, struct bio *bp)
1650ba6c22ceSWarner Losh {
1651ba6c22ceSWarner Losh 
1652ba6c22ceSWarner Losh 	/*
1653586eda6bSGordon Bergling 	 * A BIO_SPEEDUP from the upper layers means that they have a block
16540d83f8dcSWarner Losh 	 * shortage. At the present, this is only sent when we're trying to
16550d83f8dcSWarner Losh 	 * allocate blocks, but have a shortage before giving up. bio_length is
16560d83f8dcSWarner Losh 	 * the size of their shortage. We will complete just enough BIO_DELETEs
16570d83f8dcSWarner Losh 	 * in the queue to satisfy the need. If bio_length is 0, we'll complete
16580d83f8dcSWarner Losh 	 * them all. This allows the scheduler to delay BIO_DELETEs to improve
16590d83f8dcSWarner Losh 	 * read/write performance without worrying about the upper layers. When
16600d83f8dcSWarner Losh 	 * it's possibly a problem, we respond by pretending the BIO_DELETEs
16610d83f8dcSWarner Losh 	 * just worked. We can't do anything about the BIO_DELETEs in the
16620d83f8dcSWarner Losh 	 * hardware, though. We have to wait for them to complete.
16630d83f8dcSWarner Losh 	 */
16640d83f8dcSWarner Losh 	if (bp->bio_cmd == BIO_SPEEDUP) {
16650d83f8dcSWarner Losh 		off_t len;
16660d83f8dcSWarner Losh 		struct bio *nbp;
16670d83f8dcSWarner Losh 
16680d83f8dcSWarner Losh 		len = 0;
16690d83f8dcSWarner Losh 		while (bioq_first(&isc->trim_queue) &&
16700d83f8dcSWarner Losh 		    (bp->bio_length == 0 || len < bp->bio_length)) {
16710d83f8dcSWarner Losh 			nbp = bioq_takefirst(&isc->trim_queue);
16720d83f8dcSWarner Losh 			len += nbp->bio_length;
16730d83f8dcSWarner Losh 			nbp->bio_error = 0;
16740d83f8dcSWarner Losh 			biodone(nbp);
16750d83f8dcSWarner Losh 		}
16760d83f8dcSWarner Losh 		if (bp->bio_length > 0) {
16770d83f8dcSWarner Losh 			if (bp->bio_length > len)
16780d83f8dcSWarner Losh 				bp->bio_resid = bp->bio_length - len;
16790d83f8dcSWarner Losh 			else
16800d83f8dcSWarner Losh 				bp->bio_resid = 0;
16810d83f8dcSWarner Losh 		}
16820d83f8dcSWarner Losh 		bp->bio_error = 0;
16830d83f8dcSWarner Losh 		biodone(bp);
16840d83f8dcSWarner Losh 		return;
16850d83f8dcSWarner Losh 	}
16860d83f8dcSWarner Losh 
16870d83f8dcSWarner Losh 	/*
1688d900ade5SWarner Losh 	 * If we get a BIO_FLUSH, and we're doing delayed BIO_DELETEs then we
1689d900ade5SWarner Losh 	 * set the last tick time to one less than the current ticks minus the
1690d900ade5SWarner Losh 	 * delay to force the BIO_DELETEs to be presented to the client driver.
1691d900ade5SWarner Losh 	 */
1692d900ade5SWarner Losh 	if (bp->bio_cmd == BIO_FLUSH && isc->trim_ticks > 0)
1693d900ade5SWarner Losh 		isc->last_trim_tick = ticks - isc->trim_ticks - 1;
1694d900ade5SWarner Losh 
1695d900ade5SWarner Losh 	/*
1696d900ade5SWarner Losh 	 * Put all trims on the trim queue. Otherwise put the work on the bio
1697d900ade5SWarner Losh 	 * queue.
1698ba6c22ceSWarner Losh 	 */
1699ba6c22ceSWarner Losh 	if (bp->bio_cmd == BIO_DELETE) {
170097f8aa05SWarner Losh 		bioq_insert_tail(&isc->trim_queue, bp);
1701d900ade5SWarner Losh 		if (isc->queued_trims == 0)
1702d900ade5SWarner Losh 			isc->last_trim_tick = ticks;
1703d900ade5SWarner Losh 		isc->queued_trims++;
1704df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1705ba6c22ceSWarner Losh 		isc->trim_stats.in++;
1706ba6c22ceSWarner Losh 		isc->trim_stats.queued++;
1707ba6c22ceSWarner Losh #endif
1708ba6c22ceSWarner Losh 	}
1709df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1710cc1572ddSWarner Losh 	else if (do_dynamic_iosched && isc->read_bias != 0 &&
1711cc1572ddSWarner Losh 	    (bp->bio_cmd != BIO_READ)) {
1712ba6c22ceSWarner Losh 		if (cam_iosched_sort_queue(isc))
1713ba6c22ceSWarner Losh 			bioq_disksort(&isc->write_queue, bp);
1714ba6c22ceSWarner Losh 		else
1715ba6c22ceSWarner Losh 			bioq_insert_tail(&isc->write_queue, bp);
1716ba6c22ceSWarner Losh 		if (iosched_debug > 9)
1717ba6c22ceSWarner Losh 			printf("Qw  : %p %#x\n", bp, bp->bio_cmd);
1718ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_WRITE) {
1719ba6c22ceSWarner Losh 			isc->write_stats.in++;
1720ba6c22ceSWarner Losh 			isc->write_stats.queued++;
1721ba6c22ceSWarner Losh 		}
1722ba6c22ceSWarner Losh 	}
1723ba6c22ceSWarner Losh #endif
1724ba6c22ceSWarner Losh 	else {
1725ba6c22ceSWarner Losh 		if (cam_iosched_sort_queue(isc))
1726ba6c22ceSWarner Losh 			bioq_disksort(&isc->bio_queue, bp);
1727ba6c22ceSWarner Losh 		else
1728ba6c22ceSWarner Losh 			bioq_insert_tail(&isc->bio_queue, bp);
1729df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1730ba6c22ceSWarner Losh 		if (iosched_debug > 9)
1731ba6c22ceSWarner Losh 			printf("Qr  : %p %#x\n", bp, bp->bio_cmd);
1732ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_READ) {
1733ba6c22ceSWarner Losh 			isc->read_stats.in++;
1734ba6c22ceSWarner Losh 			isc->read_stats.queued++;
1735ba6c22ceSWarner Losh 		} else if (bp->bio_cmd == BIO_WRITE) {
1736ba6c22ceSWarner Losh 			isc->write_stats.in++;
1737ba6c22ceSWarner Losh 			isc->write_stats.queued++;
1738ba6c22ceSWarner Losh 		}
1739ba6c22ceSWarner Losh #endif
1740ba6c22ceSWarner Losh 	}
1741ba6c22ceSWarner Losh }
1742ba6c22ceSWarner Losh 
1743ba6c22ceSWarner Losh /*
1744ba6c22ceSWarner Losh  * If we have work, get it scheduled. Called with the periph lock held.
1745ba6c22ceSWarner Losh  */
1746ba6c22ceSWarner Losh void
1747ba6c22ceSWarner Losh cam_iosched_schedule(struct cam_iosched_softc *isc, struct cam_periph *periph)
1748ba6c22ceSWarner Losh {
1749ba6c22ceSWarner Losh 
1750ba6c22ceSWarner Losh 	if (cam_iosched_has_work(isc))
1751ba6c22ceSWarner Losh 		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1752ba6c22ceSWarner Losh }
1753ba6c22ceSWarner Losh 
1754ba6c22ceSWarner Losh /*
175555c770b4SWarner Losh  * Complete a trim request. Mark that we no longer have one in flight.
1756ba6c22ceSWarner Losh  */
1757ba6c22ceSWarner Losh void
1758ba6c22ceSWarner Losh cam_iosched_trim_done(struct cam_iosched_softc *isc)
1759ba6c22ceSWarner Losh {
1760ba6c22ceSWarner Losh 
1761ece56614SWarner Losh 	isc->flags &= ~CAM_IOSCHED_FLAG_TRIM_ACTIVE;
1762ba6c22ceSWarner Losh }
1763ba6c22ceSWarner Losh 
1764ba6c22ceSWarner Losh /*
1765ba6c22ceSWarner Losh  * Complete a bio. Called before we release the ccb with xpt_release_ccb so we
1766ba6c22ceSWarner Losh  * might use notes in the ccb for statistics.
1767ba6c22ceSWarner Losh  */
1768ba6c22ceSWarner Losh int
1769ba6c22ceSWarner Losh cam_iosched_bio_complete(struct cam_iosched_softc *isc, struct bio *bp,
1770ba6c22ceSWarner Losh     union ccb *done_ccb)
1771ba6c22ceSWarner Losh {
1772ba6c22ceSWarner Losh 	int retval = 0;
1773df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1774035ec48eSWarner Losh 	if (!do_dynamic_iosched)
1775ba6c22ceSWarner Losh 		return retval;
1776ba6c22ceSWarner Losh 
1777ba6c22ceSWarner Losh 	if (iosched_debug > 10)
1778ba6c22ceSWarner Losh 		printf("done: %p %#x\n", bp, bp->bio_cmd);
1779ba6c22ceSWarner Losh 	if (bp->bio_cmd == BIO_WRITE) {
1780ba6c22ceSWarner Losh 		retval = cam_iosched_limiter_iodone(&isc->write_stats, bp);
1781157cb465SWarner Losh 		if ((bp->bio_flags & BIO_ERROR) != 0)
1782c4b72d8bSWarner Losh 			isc->write_stats.errs++;
1783ba6c22ceSWarner Losh 		isc->write_stats.out++;
1784ba6c22ceSWarner Losh 		isc->write_stats.pending--;
1785ba6c22ceSWarner Losh 	} else if (bp->bio_cmd == BIO_READ) {
1786ba6c22ceSWarner Losh 		retval = cam_iosched_limiter_iodone(&isc->read_stats, bp);
1787157cb465SWarner Losh 		if ((bp->bio_flags & BIO_ERROR) != 0)
1788c4b72d8bSWarner Losh 			isc->read_stats.errs++;
1789ba6c22ceSWarner Losh 		isc->read_stats.out++;
1790ba6c22ceSWarner Losh 		isc->read_stats.pending--;
1791ba6c22ceSWarner Losh 	} else if (bp->bio_cmd == BIO_DELETE) {
1792157cb465SWarner Losh 		if ((bp->bio_flags & BIO_ERROR) != 0)
1793c4b72d8bSWarner Losh 			isc->trim_stats.errs++;
1794ba6c22ceSWarner Losh 		isc->trim_stats.out++;
1795ba6c22ceSWarner Losh 		isc->trim_stats.pending--;
1796ba6c22ceSWarner Losh 	} else if (bp->bio_cmd != BIO_FLUSH) {
1797ba6c22ceSWarner Losh 		if (iosched_debug)
1798ba6c22ceSWarner Losh 			printf("Completing command with bio_cmd == %#x\n", bp->bio_cmd);
1799ba6c22ceSWarner Losh 	}
1800ba6c22ceSWarner Losh 
18014afa62beSWarner Losh 	if ((bp->bio_flags & BIO_ERROR) == 0 && done_ccb != NULL &&
18024afa62beSWarner Losh 	    (done_ccb->ccb_h.status & CAM_QOS_VALID) != 0) {
1803e5436ab5SWarner Losh 		sbintime_t sim_latency;
1804e5436ab5SWarner Losh 
1805e5436ab5SWarner Losh 		sim_latency = cam_iosched_sbintime_t(done_ccb->ccb_h.qos.periph_data);
1806e5436ab5SWarner Losh 
1807148c1734SWarner Losh 		cam_iosched_io_metric_update(isc, sim_latency, bp);
1808148c1734SWarner Losh 
1809e5436ab5SWarner Losh 		/*
1810e5436ab5SWarner Losh 		 * Debugging code: allow callbacks to the periph driver when latency max
1811e5436ab5SWarner Losh 		 * is exceeded. This can be useful for triggering external debugging actions.
1812e5436ab5SWarner Losh 		 */
1813e5436ab5SWarner Losh 		if (isc->latfcn && isc->max_lat != 0 && sim_latency > isc->max_lat)
1814e5436ab5SWarner Losh 			isc->latfcn(isc->latarg, sim_latency, bp);
1815e5436ab5SWarner Losh 	}
1816ba6c22ceSWarner Losh #endif
1817ba6c22ceSWarner Losh 	return retval;
1818ba6c22ceSWarner Losh }
1819ba6c22ceSWarner Losh 
1820ba6c22ceSWarner Losh /*
1821ba6c22ceSWarner Losh  * Tell the io scheduler that you've pushed a trim down into the sim.
182255c770b4SWarner Losh  * This also tells the I/O scheduler not to push any more trims down, so
182355c770b4SWarner Losh  * some periphs do not call it if they can cope with multiple trims in flight.
1824ba6c22ceSWarner Losh  */
1825ba6c22ceSWarner Losh void
1826ba6c22ceSWarner Losh cam_iosched_submit_trim(struct cam_iosched_softc *isc)
1827ba6c22ceSWarner Losh {
1828ba6c22ceSWarner Losh 
1829ece56614SWarner Losh 	isc->flags |= CAM_IOSCHED_FLAG_TRIM_ACTIVE;
1830ba6c22ceSWarner Losh }
1831ba6c22ceSWarner Losh 
1832ba6c22ceSWarner Losh /*
1833ba6c22ceSWarner Losh  * Change the sorting policy hint for I/O transactions for this device.
1834ba6c22ceSWarner Losh  */
1835ba6c22ceSWarner Losh void
1836ba6c22ceSWarner Losh cam_iosched_set_sort_queue(struct cam_iosched_softc *isc, int val)
1837ba6c22ceSWarner Losh {
1838ba6c22ceSWarner Losh 
1839ba6c22ceSWarner Losh 	isc->sort_io_queue = val;
1840ba6c22ceSWarner Losh }
1841ba6c22ceSWarner Losh 
1842ba6c22ceSWarner Losh int
1843ba6c22ceSWarner Losh cam_iosched_has_work_flags(struct cam_iosched_softc *isc, uint32_t flags)
1844ba6c22ceSWarner Losh {
1845ba6c22ceSWarner Losh 	return isc->flags & flags;
1846ba6c22ceSWarner Losh }
1847ba6c22ceSWarner Losh 
1848ba6c22ceSWarner Losh void
1849ba6c22ceSWarner Losh cam_iosched_set_work_flags(struct cam_iosched_softc *isc, uint32_t flags)
1850ba6c22ceSWarner Losh {
1851ba6c22ceSWarner Losh 	isc->flags |= flags;
1852ba6c22ceSWarner Losh }
1853ba6c22ceSWarner Losh 
1854ba6c22ceSWarner Losh void
1855ba6c22ceSWarner Losh cam_iosched_clr_work_flags(struct cam_iosched_softc *isc, uint32_t flags)
1856ba6c22ceSWarner Losh {
1857ba6c22ceSWarner Losh 	isc->flags &= ~flags;
1858ba6c22ceSWarner Losh }
1859ba6c22ceSWarner Losh 
1860df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC
1861ba6c22ceSWarner Losh /*
1862ba6c22ceSWarner Losh  * After the method presented in Jack Crenshaw's 1998 article "Integer
18630b4060b0SEd Maste  * Square Roots," reprinted at
1864ba6c22ceSWarner Losh  * http://www.embedded.com/electronics-blogs/programmer-s-toolbox/4219659/Integer-Square-Roots
1865ba6c22ceSWarner Losh  * and well worth the read. Briefly, we find the power of 4 that's the
1866ba6c22ceSWarner Losh  * largest smaller than val. We then check each smaller power of 4 to
1867ba6c22ceSWarner Losh  * see if val is still bigger. The right shifts at each step divide
1868ba6c22ceSWarner Losh  * the result by 2 which after successive application winds up
1869ba6c22ceSWarner Losh  * accumulating the right answer. It could also have been accumulated
1870ba6c22ceSWarner Losh  * using a separate root counter, but this code is smaller and faster
1871ba6c22ceSWarner Losh  * than that method. This method is also integer size invariant.
18720b4060b0SEd Maste  * It returns floor(sqrt((float)val)), or the largest integer less than
1873ba6c22ceSWarner Losh  * or equal to the square root.
1874ba6c22ceSWarner Losh  */
1875ba6c22ceSWarner Losh static uint64_t
1876ba6c22ceSWarner Losh isqrt64(uint64_t val)
1877ba6c22ceSWarner Losh {
1878ba6c22ceSWarner Losh 	uint64_t res = 0;
1879ba6c22ceSWarner Losh 	uint64_t bit = 1ULL << (sizeof(uint64_t) * NBBY - 2);
1880ba6c22ceSWarner Losh 
1881ba6c22ceSWarner Losh 	/*
1882ba6c22ceSWarner Losh 	 * Find the largest power of 4 smaller than val.
1883ba6c22ceSWarner Losh 	 */
1884ba6c22ceSWarner Losh 	while (bit > val)
1885ba6c22ceSWarner Losh 		bit >>= 2;
1886ba6c22ceSWarner Losh 
1887ba6c22ceSWarner Losh 	/*
1888ba6c22ceSWarner Losh 	 * Accumulate the answer, one bit at a time (we keep moving
1889ba6c22ceSWarner Losh 	 * them over since 2 is the square root of 4 and we test
1890ba6c22ceSWarner Losh 	 * powers of 4). We accumulate where we find the bit, but
1891ba6c22ceSWarner Losh 	 * the successive shifts land the bit in the right place
1892ba6c22ceSWarner Losh 	 * by the end.
1893ba6c22ceSWarner Losh 	 */
1894ba6c22ceSWarner Losh 	while (bit != 0) {
1895ba6c22ceSWarner Losh 		if (val >= res + bit) {
1896ba6c22ceSWarner Losh 			val -= res + bit;
1897ba6c22ceSWarner Losh 			res = (res >> 1) + bit;
1898ba6c22ceSWarner Losh 		} else
1899ba6c22ceSWarner Losh 			res >>= 1;
1900ba6c22ceSWarner Losh 		bit >>= 2;
1901ba6c22ceSWarner Losh 	}
1902ba6c22ceSWarner Losh 
1903ba6c22ceSWarner Losh 	return res;
1904ba6c22ceSWarner Losh }
1905ba6c22ceSWarner Losh 
190608fc2f23SWarner Losh static sbintime_t latencies[LAT_BUCKETS - 1] = {
190722832069SWarner Losh 	BUCKET_BASE <<  0,	/* 20us */
190822832069SWarner Losh 	BUCKET_BASE <<  1,
190922832069SWarner Losh 	BUCKET_BASE <<  2,
191022832069SWarner Losh 	BUCKET_BASE <<  3,
191122832069SWarner Losh 	BUCKET_BASE <<  4,
191222832069SWarner Losh 	BUCKET_BASE <<  5,
191322832069SWarner Losh 	BUCKET_BASE <<  6,
191422832069SWarner Losh 	BUCKET_BASE <<  7,
191522832069SWarner Losh 	BUCKET_BASE <<  8,
191622832069SWarner Losh 	BUCKET_BASE <<  9,
191722832069SWarner Losh 	BUCKET_BASE << 10,
191822832069SWarner Losh 	BUCKET_BASE << 11,
191922832069SWarner Losh 	BUCKET_BASE << 12,
192022832069SWarner Losh 	BUCKET_BASE << 13,
192122832069SWarner Losh 	BUCKET_BASE << 14,
192222832069SWarner Losh 	BUCKET_BASE << 15,
192322832069SWarner Losh 	BUCKET_BASE << 16,
192422832069SWarner Losh 	BUCKET_BASE << 17,
192522832069SWarner Losh 	BUCKET_BASE << 18	/* 5,242,880us */
1926cf3ec151SWarner Losh };
1927cf3ec151SWarner Losh 
1928*dd6123ebSWarner Losh #define CAM_IOSCHED_DEVD_MSG_SIZE	256
1929*dd6123ebSWarner Losh 
1930*dd6123ebSWarner Losh static void
1931*dd6123ebSWarner Losh cam_iosched_devctl_outlier(struct iop_stats *iop, sbintime_t sim_latency,
1932*dd6123ebSWarner Losh     const struct bio *bp)
1933*dd6123ebSWarner Losh {
1934*dd6123ebSWarner Losh 	daddr_t lba = bp->bio_pblkno;
1935*dd6123ebSWarner Losh 	daddr_t cnt = bp->bio_bcount / iop->softc->disk->d_sectorsize;
1936*dd6123ebSWarner Losh 	char *sbmsg;
1937*dd6123ebSWarner Losh 	struct sbuf sb;
1938*dd6123ebSWarner Losh 
1939*dd6123ebSWarner Losh 	sbmsg = malloc(CAM_IOSCHED_DEVD_MSG_SIZE, M_CAMSCHED, M_NOWAIT);
1940*dd6123ebSWarner Losh 	if (sbmsg == NULL)
1941*dd6123ebSWarner Losh 		return;
1942*dd6123ebSWarner Losh 	sbuf_new(&sb, sbmsg, CAM_IOSCHED_DEVD_MSG_SIZE, SBUF_FIXEDLEN);
1943*dd6123ebSWarner Losh 
1944*dd6123ebSWarner Losh 	sbuf_printf(&sb, "device=%s%d lba=%jd blocks=%jd latency=%jd",
1945*dd6123ebSWarner Losh 	    iop->softc->periph->periph_name,
1946*dd6123ebSWarner Losh 	    iop->softc->periph->unit_number,
1947*dd6123ebSWarner Losh 	    lba, cnt, sbttons(sim_latency));
1948*dd6123ebSWarner Losh 	if (sbuf_finish(&sb) == 0)
1949*dd6123ebSWarner Losh 		devctl_notify("CAM", "iosched", "latency", sbuf_data(&sb));
1950*dd6123ebSWarner Losh 	sbuf_delete(&sb);
1951*dd6123ebSWarner Losh 	free(sbmsg, M_CAMSCHED);
1952*dd6123ebSWarner Losh }
1953*dd6123ebSWarner Losh 
1954ba6c22ceSWarner Losh static void
1955148c1734SWarner Losh cam_iosched_update(struct iop_stats *iop, sbintime_t sim_latency,
1956*dd6123ebSWarner Losh     const struct bio *bp)
1957ba6c22ceSWarner Losh {
195879d80af2SWarner Losh 	sbintime_t y, deltasq, delta;
1959cf3ec151SWarner Losh 	int i;
1960cf3ec151SWarner Losh 
1961cf3ec151SWarner Losh 	/*
1962e82644e5SWarner Losh 	 * Simple threshold: count the number of events that excede the
1963e82644e5SWarner Losh 	 * configured threshold.
1964e82644e5SWarner Losh 	 */
1965e82644e5SWarner Losh 	if (sim_latency > iop->bad_latency) {
1966*dd6123ebSWarner Losh 		cam_iosched_devctl_outlier(iop, sim_latency, bp);
1967e82644e5SWarner Losh 		iop->too_long++;
1968e82644e5SWarner Losh 	}
1969e82644e5SWarner Losh 
1970e82644e5SWarner Losh 	/*
1971cf3ec151SWarner Losh 	 * Keep counts for latency. We do it by power of two buckets.
1972cf3ec151SWarner Losh 	 * This helps us spot outlier behavior obscured by averages.
1973cf3ec151SWarner Losh 	 */
1974cf3ec151SWarner Losh 	for (i = 0; i < LAT_BUCKETS - 1; i++) {
1975cf3ec151SWarner Losh 		if (sim_latency < latencies[i]) {
1976cf3ec151SWarner Losh 			iop->latencies[i]++;
1977cf3ec151SWarner Losh 			break;
1978cf3ec151SWarner Losh 		}
1979cf3ec151SWarner Losh 	}
1980cf3ec151SWarner Losh 	if (i == LAT_BUCKETS - 1)
1981c048ac62SWarner Losh 		iop->latencies[i]++; 	 /* Put all > 8192ms values into the last bucket. */
1982ba6c22ceSWarner Losh 
1983ba6c22ceSWarner Losh 	/*
19840b4060b0SEd Maste 	 * Classic exponentially decaying average with a tiny alpha
1985ba6c22ceSWarner Losh 	 * (2 ^ -alpha_bits). For more info see the NIST statistical
1986ba6c22ceSWarner Losh 	 * handbook.
1987ba6c22ceSWarner Losh 	 *
198879d80af2SWarner Losh 	 * ema_t = y_t * alpha + ema_t-1 * (1 - alpha)		[nist]
198979d80af2SWarner Losh 	 * ema_t = y_t * alpha + ema_t-1 - alpha * ema_t-1
199079d80af2SWarner Losh 	 * ema_t = alpha * y_t - alpha * ema_t-1 + ema_t-1
1991ba6c22ceSWarner Losh 	 * alpha = 1 / (1 << alpha_bits)
199279d80af2SWarner Losh 	 * sub e == ema_t-1, b == 1/alpha (== 1 << alpha_bits), d == y_t - ema_t-1
199379d80af2SWarner Losh 	 *	= y_t/b - e/b + be/b
199479d80af2SWarner Losh 	 *      = (y_t - e + be) / b
199579d80af2SWarner Losh 	 *	= (e + d) / b
1996ba6c22ceSWarner Losh 	 *
1997ba6c22ceSWarner Losh 	 * Since alpha is a power of two, we can compute this w/o any mult or
1998ba6c22ceSWarner Losh 	 * division.
199979d80af2SWarner Losh 	 *
200079d80af2SWarner Losh 	 * Variance can also be computed. Usually, it would be expressed as follows:
200179d80af2SWarner Losh 	 *	diff_t = y_t - ema_t-1
200279d80af2SWarner Losh 	 *	emvar_t = (1 - alpha) * (emavar_t-1 + diff_t^2 * alpha)
200379d80af2SWarner Losh 	 *	  = emavar_t-1 - alpha * emavar_t-1 + delta_t^2 * alpha - (delta_t * alpha)^2
200479d80af2SWarner Losh 	 * sub b == 1/alpha (== 1 << alpha_bits), e == emavar_t-1, d = delta_t^2
200579d80af2SWarner Losh 	 *	  = e - e/b + dd/b + dd/bb
200679d80af2SWarner Losh 	 *	  = (bbe - be + bdd + dd) / bb
200779d80af2SWarner Losh 	 *	  = (bbe + b(dd-e) + dd) / bb (which is expanded below bb = 1<<(2*alpha_bits))
200879d80af2SWarner Losh 	 */
200979d80af2SWarner Losh 	/*
201079d80af2SWarner Losh 	 * XXX possible numeric issues
201179d80af2SWarner Losh 	 *	o We assume right shifted integers do the right thing, since that's
201279d80af2SWarner Losh 	 *	  implementation defined. You can change the right shifts to / (1LL << alpha).
201379d80af2SWarner Losh 	 *	o alpha_bits = 9 gives ema ceiling of 23 bits of seconds for ema and 14 bits
201479d80af2SWarner Losh 	 *	  for emvar. This puts a ceiling of 13 bits on alpha since we need a
201579d80af2SWarner Losh 	 *	  few tens of seconds of representation.
201679d80af2SWarner Losh 	 *	o We mitigate alpha issues by never setting it too high.
2017ba6c22ceSWarner Losh 	 */
2018ba6c22ceSWarner Losh 	y = sim_latency;
201979d80af2SWarner Losh 	delta = (y - iop->ema);					/* d */
202079d80af2SWarner Losh 	iop->ema = ((iop->ema << alpha_bits) + delta) >> alpha_bits;
2021ba6c22ceSWarner Losh 
2022ba6c22ceSWarner Losh 	/*
202379d80af2SWarner Losh 	 * Were we to naively plow ahead at this point, we wind up with many numerical
202479d80af2SWarner Losh 	 * issues making any SD > ~3ms unreliable. So, we shift right by 12. This leaves
202579d80af2SWarner Losh 	 * us with microsecond level precision in the input, so the same in the
202679d80af2SWarner Losh 	 * output. It means we can't overflow deltasq unless delta > 4k seconds. It
202779d80af2SWarner Losh 	 * also means that emvar can be up 46 bits 40 of which are fraction, which
202879d80af2SWarner Losh 	 * gives us a way to measure up to ~8s in the SD before the computation goes
202979d80af2SWarner Losh 	 * unstable. Even the worst hard disk rarely has > 1s service time in the
203079d80af2SWarner Losh 	 * drive. It does mean we have to shift left 12 bits after taking the
203179d80af2SWarner Losh 	 * square root to compute the actual standard deviation estimate. This loss of
203279d80af2SWarner Losh 	 * precision is preferable to needing int128 types to work. The above numbers
203379d80af2SWarner Losh 	 * assume alpha=9. 10 or 11 are ok, but we start to run into issues at 12,
203479d80af2SWarner Losh 	 * so 12 or 13 is OK for EMA, EMVAR and SD will be wrong in those cases.
2035ba6c22ceSWarner Losh 	 */
203679d80af2SWarner Losh 	delta >>= 12;
203779d80af2SWarner Losh 	deltasq = delta * delta;				/* dd */
203879d80af2SWarner Losh 	iop->emvar = ((iop->emvar << (2 * alpha_bits)) +	/* bbe */
203979d80af2SWarner Losh 	    ((deltasq - iop->emvar) << alpha_bits) +		/* b(dd-e) */
204079d80af2SWarner Losh 	    deltasq)						/* dd */
204179d80af2SWarner Losh 	    >> (2 * alpha_bits);				/* div bb */
204279d80af2SWarner Losh 	iop->sd = (sbintime_t)isqrt64((uint64_t)iop->emvar) << 12;
2043ba6c22ceSWarner Losh }
2044ba6c22ceSWarner Losh 
2045ba6c22ceSWarner Losh static void
2046ba6c22ceSWarner Losh cam_iosched_io_metric_update(struct cam_iosched_softc *isc,
2047148c1734SWarner Losh     sbintime_t sim_latency, const struct bio *bp)
2048ba6c22ceSWarner Losh {
2049148c1734SWarner Losh 	switch (bp->bio_cmd) {
2050ba6c22ceSWarner Losh 	case BIO_READ:
2051148c1734SWarner Losh 		cam_iosched_update(&isc->read_stats, sim_latency, bp);
2052ba6c22ceSWarner Losh 		break;
2053ba6c22ceSWarner Losh 	case BIO_WRITE:
2054148c1734SWarner Losh 		cam_iosched_update(&isc->write_stats, sim_latency, bp);
2055ba6c22ceSWarner Losh 		break;
2056ba6c22ceSWarner Losh 	case BIO_DELETE:
2057148c1734SWarner Losh 		cam_iosched_update(&isc->trim_stats, sim_latency, bp);
2058ba6c22ceSWarner Losh 		break;
2059ba6c22ceSWarner Losh 	default:
2060ba6c22ceSWarner Losh 		break;
2061ba6c22ceSWarner Losh 	}
2062ba6c22ceSWarner Losh }
2063ba6c22ceSWarner Losh 
2064ba6c22ceSWarner Losh #ifdef DDB
2065ba6c22ceSWarner Losh static int biolen(struct bio_queue_head *bq)
2066ba6c22ceSWarner Losh {
2067ba6c22ceSWarner Losh 	int i = 0;
2068ba6c22ceSWarner Losh 	struct bio *bp;
2069ba6c22ceSWarner Losh 
2070ba6c22ceSWarner Losh 	TAILQ_FOREACH(bp, &bq->queue, bio_queue) {
2071ba6c22ceSWarner Losh 		i++;
2072ba6c22ceSWarner Losh 	}
2073ba6c22ceSWarner Losh 	return i;
2074ba6c22ceSWarner Losh }
2075ba6c22ceSWarner Losh 
2076ba6c22ceSWarner Losh /*
2077ba6c22ceSWarner Losh  * Show the internal state of the I/O scheduler.
2078ba6c22ceSWarner Losh  */
2079ba6c22ceSWarner Losh DB_SHOW_COMMAND(iosched, cam_iosched_db_show)
2080ba6c22ceSWarner Losh {
2081ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
2082ba6c22ceSWarner Losh 
2083ba6c22ceSWarner Losh 	if (!have_addr) {
2084ba6c22ceSWarner Losh 		db_printf("Need addr\n");
2085ba6c22ceSWarner Losh 		return;
2086ba6c22ceSWarner Losh 	}
2087ba6c22ceSWarner Losh 	isc = (struct cam_iosched_softc *)addr;
2088ba6c22ceSWarner Losh 	db_printf("pending_reads:     %d\n", isc->read_stats.pending);
2089ba6c22ceSWarner Losh 	db_printf("min_reads:         %d\n", isc->read_stats.min);
2090ba6c22ceSWarner Losh 	db_printf("max_reads:         %d\n", isc->read_stats.max);
2091ba6c22ceSWarner Losh 	db_printf("reads:             %d\n", isc->read_stats.total);
2092ba6c22ceSWarner Losh 	db_printf("in_reads:          %d\n", isc->read_stats.in);
2093ba6c22ceSWarner Losh 	db_printf("out_reads:         %d\n", isc->read_stats.out);
2094ba6c22ceSWarner Losh 	db_printf("queued_reads:      %d\n", isc->read_stats.queued);
20953aba1d47SWarner Losh 	db_printf("Read Q len         %d\n", biolen(&isc->bio_queue));
2096ba6c22ceSWarner Losh 	db_printf("pending_writes:    %d\n", isc->write_stats.pending);
2097ba6c22ceSWarner Losh 	db_printf("min_writes:        %d\n", isc->write_stats.min);
2098ba6c22ceSWarner Losh 	db_printf("max_writes:        %d\n", isc->write_stats.max);
2099ba6c22ceSWarner Losh 	db_printf("writes:            %d\n", isc->write_stats.total);
2100ba6c22ceSWarner Losh 	db_printf("in_writes:         %d\n", isc->write_stats.in);
2101ba6c22ceSWarner Losh 	db_printf("out_writes:        %d\n", isc->write_stats.out);
2102ba6c22ceSWarner Losh 	db_printf("queued_writes:     %d\n", isc->write_stats.queued);
21033aba1d47SWarner Losh 	db_printf("Write Q len        %d\n", biolen(&isc->write_queue));
2104ba6c22ceSWarner Losh 	db_printf("pending_trims:     %d\n", isc->trim_stats.pending);
2105ba6c22ceSWarner Losh 	db_printf("min_trims:         %d\n", isc->trim_stats.min);
2106ba6c22ceSWarner Losh 	db_printf("max_trims:         %d\n", isc->trim_stats.max);
2107ba6c22ceSWarner Losh 	db_printf("trims:             %d\n", isc->trim_stats.total);
2108ba6c22ceSWarner Losh 	db_printf("in_trims:          %d\n", isc->trim_stats.in);
2109ba6c22ceSWarner Losh 	db_printf("out_trims:         %d\n", isc->trim_stats.out);
2110ba6c22ceSWarner Losh 	db_printf("queued_trims:      %d\n", isc->trim_stats.queued);
21113aba1d47SWarner Losh 	db_printf("Trim Q len         %d\n", biolen(&isc->trim_queue));
2112ba6c22ceSWarner Losh 	db_printf("read_bias:         %d\n", isc->read_bias);
2113ba6c22ceSWarner Losh 	db_printf("current_read_bias: %d\n", isc->current_read_bias);
2114ece56614SWarner Losh 	db_printf("Trim active?       %s\n",
2115ece56614SWarner Losh 	    (isc->flags & CAM_IOSCHED_FLAG_TRIM_ACTIVE) ? "yes" : "no");
2116ba6c22ceSWarner Losh }
2117ba6c22ceSWarner Losh #endif
2118ba6c22ceSWarner Losh #endif
2119