xref: /freebsd/sys/cam/cam_iosched.c (revision 2b5c19f196d1446dcb4c5078a110d587a1918f28)
1ba6c22ceSWarner Losh /*-
2ba6c22ceSWarner Losh  * CAM IO Scheduler Interface
3ba6c22ceSWarner Losh  *
4ba6c22ceSWarner Losh  * Copyright (c) 2015 Netflix, Inc.
5ba6c22ceSWarner Losh  * All rights reserved.
6ba6c22ceSWarner Losh  *
7ba6c22ceSWarner Losh  * Redistribution and use in source and binary forms, with or without
8ba6c22ceSWarner Losh  * modification, are permitted provided that the following conditions
9ba6c22ceSWarner Losh  * are met:
10ba6c22ceSWarner Losh  * 1. Redistributions of source code must retain the above copyright
11ba6c22ceSWarner Losh  *    notice, this list of conditions, and the following disclaimer,
12ba6c22ceSWarner Losh  *    without modification, immediately at the beginning of the file.
13ba6c22ceSWarner Losh  * 2. The name of the author may not be used to endorse or promote products
14ba6c22ceSWarner Losh  *    derived from this software without specific prior written permission.
15ba6c22ceSWarner Losh  *
16ba6c22ceSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ba6c22ceSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ba6c22ceSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ba6c22ceSWarner Losh  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20ba6c22ceSWarner Losh  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ba6c22ceSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ba6c22ceSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ba6c22ceSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ba6c22ceSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ba6c22ceSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ba6c22ceSWarner Losh  * SUCH DAMAGE.
27ba6c22ceSWarner Losh  *
28ba6c22ceSWarner Losh  * $FreeBSD$
29ba6c22ceSWarner Losh  */
30ba6c22ceSWarner Losh 
31ba6c22ceSWarner Losh #include "opt_cam.h"
32ba6c22ceSWarner Losh #include "opt_ddb.h"
33ba6c22ceSWarner Losh 
34ba6c22ceSWarner Losh #include <sys/cdefs.h>
35ba6c22ceSWarner Losh __FBSDID("$FreeBSD$");
36ba6c22ceSWarner Losh 
37ba6c22ceSWarner Losh #include <sys/param.h>
38ba6c22ceSWarner Losh 
39ba6c22ceSWarner Losh #include <sys/systm.h>
40ba6c22ceSWarner Losh #include <sys/kernel.h>
41ba6c22ceSWarner Losh #include <sys/bio.h>
42ba6c22ceSWarner Losh #include <sys/lock.h>
43ba6c22ceSWarner Losh #include <sys/malloc.h>
44ba6c22ceSWarner Losh #include <sys/mutex.h>
45ba6c22ceSWarner Losh #include <sys/sysctl.h>
46ba6c22ceSWarner Losh 
47ba6c22ceSWarner Losh #include <cam/cam.h>
48ba6c22ceSWarner Losh #include <cam/cam_ccb.h>
49ba6c22ceSWarner Losh #include <cam/cam_periph.h>
50ba6c22ceSWarner Losh #include <cam/cam_xpt_periph.h>
51ba6c22ceSWarner Losh #include <cam/cam_iosched.h>
52ba6c22ceSWarner Losh 
53ba6c22ceSWarner Losh #include <ddb/ddb.h>
54ba6c22ceSWarner Losh 
55ba6c22ceSWarner Losh static MALLOC_DEFINE(M_CAMSCHED, "CAM I/O Scheduler",
56ba6c22ceSWarner Losh     "CAM I/O Scheduler buffers");
57ba6c22ceSWarner Losh 
58ba6c22ceSWarner Losh /*
59ba6c22ceSWarner Losh  * Default I/O scheduler for FreeBSD. This implementation is just a thin-vineer
60ba6c22ceSWarner Losh  * over the bioq_* interface, with notions of separate calls for normal I/O and
61ba6c22ceSWarner Losh  * for trims.
62ba6c22ceSWarner Losh  */
63ba6c22ceSWarner Losh 
64ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
65ba6c22ceSWarner Losh 
66ba6c22ceSWarner Losh SYSCTL_DECL(_kern_cam);
67ba6c22ceSWarner Losh static int do_netflix_iosched = 1;
68ba6c22ceSWarner Losh TUNABLE_INT("kern.cam.do_netflix_iosched", &do_netflix_iosched);
69ba6c22ceSWarner Losh SYSCTL_INT(_kern_cam, OID_AUTO, do_netflix_iosched, CTLFLAG_RD,
70ba6c22ceSWarner Losh     &do_netflix_iosched, 1,
71ba6c22ceSWarner Losh     "Enable Netflix I/O scheduler optimizations.");
72ba6c22ceSWarner Losh 
73ba6c22ceSWarner Losh static int alpha_bits = 9;
74ba6c22ceSWarner Losh TUNABLE_INT("kern.cam.iosched_alpha_bits", &alpha_bits);
75ba6c22ceSWarner Losh SYSCTL_INT(_kern_cam, OID_AUTO, iosched_alpha_bits, CTLFLAG_RW,
76ba6c22ceSWarner Losh     &alpha_bits, 1,
77ba6c22ceSWarner Losh     "Bits in EMA's alpha.");
78ba6c22ceSWarner Losh 
79ba6c22ceSWarner Losh 
80ba6c22ceSWarner Losh 
81ba6c22ceSWarner Losh struct iop_stats;
82ba6c22ceSWarner Losh struct cam_iosched_softc;
83ba6c22ceSWarner Losh 
84ba6c22ceSWarner Losh int iosched_debug = 0;
85ba6c22ceSWarner Losh 
86ba6c22ceSWarner Losh typedef enum {
87ba6c22ceSWarner Losh 	none = 0,				/* No limits */
88ba6c22ceSWarner Losh 	queue_depth,			/* Limit how many ops we queue to SIM */
89ba6c22ceSWarner Losh 	iops,				/* Limit # of IOPS to the drive */
90ba6c22ceSWarner Losh 	bandwidth,			/* Limit bandwidth to the drive */
91ba6c22ceSWarner Losh 	limiter_max
92ba6c22ceSWarner Losh } io_limiter;
93ba6c22ceSWarner Losh 
94ba6c22ceSWarner Losh static const char *cam_iosched_limiter_names[] =
95ba6c22ceSWarner Losh     { "none", "queue_depth", "iops", "bandwidth" };
96ba6c22ceSWarner Losh 
97ba6c22ceSWarner Losh /*
98ba6c22ceSWarner Losh  * Called to initialize the bits of the iop_stats structure relevant to the
99ba6c22ceSWarner Losh  * limiter. Called just after the limiter is set.
100ba6c22ceSWarner Losh  */
101ba6c22ceSWarner Losh typedef int l_init_t(struct iop_stats *);
102ba6c22ceSWarner Losh 
103ba6c22ceSWarner Losh /*
104ba6c22ceSWarner Losh  * Called every tick.
105ba6c22ceSWarner Losh  */
106ba6c22ceSWarner Losh typedef int l_tick_t(struct iop_stats *);
107ba6c22ceSWarner Losh 
108ba6c22ceSWarner Losh /*
109ba6c22ceSWarner Losh  * Called to see if the limiter thinks this IOP can be allowed to
110ba6c22ceSWarner Losh  * proceed. If so, the limiter assumes that the while IOP proceeded
111ba6c22ceSWarner Losh  * and makes any accounting of it that's needed.
112ba6c22ceSWarner Losh  */
113ba6c22ceSWarner Losh typedef int l_iop_t(struct iop_stats *, struct bio *);
114ba6c22ceSWarner Losh 
115ba6c22ceSWarner Losh /*
116ba6c22ceSWarner Losh  * Called when an I/O completes so the limiter can updates its
117ba6c22ceSWarner Losh  * accounting. Pending I/Os may complete in any order (even when
118ba6c22ceSWarner Losh  * sent to the hardware at the same time), so the limiter may not
119ba6c22ceSWarner Losh  * make any assumptions other than this I/O has completed. If it
120ba6c22ceSWarner Losh  * returns 1, then xpt_schedule() needs to be called again.
121ba6c22ceSWarner Losh  */
122ba6c22ceSWarner Losh typedef int l_iodone_t(struct iop_stats *, struct bio *);
123ba6c22ceSWarner Losh 
124ba6c22ceSWarner Losh static l_iop_t cam_iosched_qd_iop;
125ba6c22ceSWarner Losh static l_iop_t cam_iosched_qd_caniop;
126ba6c22ceSWarner Losh static l_iodone_t cam_iosched_qd_iodone;
127ba6c22ceSWarner Losh 
128ba6c22ceSWarner Losh static l_init_t cam_iosched_iops_init;
129ba6c22ceSWarner Losh static l_tick_t cam_iosched_iops_tick;
130ba6c22ceSWarner Losh static l_iop_t cam_iosched_iops_caniop;
131ba6c22ceSWarner Losh static l_iop_t cam_iosched_iops_iop;
132ba6c22ceSWarner Losh 
133ba6c22ceSWarner Losh static l_init_t cam_iosched_bw_init;
134ba6c22ceSWarner Losh static l_tick_t cam_iosched_bw_tick;
135ba6c22ceSWarner Losh static l_iop_t cam_iosched_bw_caniop;
136ba6c22ceSWarner Losh static l_iop_t cam_iosched_bw_iop;
137ba6c22ceSWarner Losh 
138ba6c22ceSWarner Losh struct limswitch
139ba6c22ceSWarner Losh {
140ba6c22ceSWarner Losh 	l_init_t	*l_init;
141ba6c22ceSWarner Losh 	l_tick_t	*l_tick;
142ba6c22ceSWarner Losh 	l_iop_t		*l_iop;
143ba6c22ceSWarner Losh 	l_iop_t		*l_caniop;
144ba6c22ceSWarner Losh 	l_iodone_t	*l_iodone;
145ba6c22ceSWarner Losh } limsw[] =
146ba6c22ceSWarner Losh {
147ba6c22ceSWarner Losh 	{	/* none */
148ba6c22ceSWarner Losh 		.l_init = NULL,
149ba6c22ceSWarner Losh 		.l_tick = NULL,
150ba6c22ceSWarner Losh 		.l_iop = NULL,
151ba6c22ceSWarner Losh 		.l_iodone= NULL,
152ba6c22ceSWarner Losh 	},
153ba6c22ceSWarner Losh 	{	/* queue_depth */
154ba6c22ceSWarner Losh 		.l_init = NULL,
155ba6c22ceSWarner Losh 		.l_tick = NULL,
156ba6c22ceSWarner Losh 		.l_caniop = cam_iosched_qd_caniop,
157ba6c22ceSWarner Losh 		.l_iop = cam_iosched_qd_iop,
158ba6c22ceSWarner Losh 		.l_iodone= cam_iosched_qd_iodone,
159ba6c22ceSWarner Losh 	},
160ba6c22ceSWarner Losh 	{	/* iops */
161ba6c22ceSWarner Losh 		.l_init = cam_iosched_iops_init,
162ba6c22ceSWarner Losh 		.l_tick = cam_iosched_iops_tick,
163ba6c22ceSWarner Losh 		.l_caniop = cam_iosched_iops_caniop,
164ba6c22ceSWarner Losh 		.l_iop = cam_iosched_iops_iop,
165ba6c22ceSWarner Losh 		.l_iodone= NULL,
166ba6c22ceSWarner Losh 	},
167ba6c22ceSWarner Losh 	{	/* bandwidth */
168ba6c22ceSWarner Losh 		.l_init = cam_iosched_bw_init,
169ba6c22ceSWarner Losh 		.l_tick = cam_iosched_bw_tick,
170ba6c22ceSWarner Losh 		.l_caniop = cam_iosched_bw_caniop,
171ba6c22ceSWarner Losh 		.l_iop = cam_iosched_bw_iop,
172ba6c22ceSWarner Losh 		.l_iodone= NULL,
173ba6c22ceSWarner Losh 	},
174ba6c22ceSWarner Losh };
175ba6c22ceSWarner Losh 
176ba6c22ceSWarner Losh struct iop_stats
177ba6c22ceSWarner Losh {
178ba6c22ceSWarner Losh 	/*
179ba6c22ceSWarner Losh 	 * sysctl state for this subnode.
180ba6c22ceSWarner Losh 	 */
181ba6c22ceSWarner Losh 	struct sysctl_ctx_list	sysctl_ctx;
182ba6c22ceSWarner Losh 	struct sysctl_oid	*sysctl_tree;
183ba6c22ceSWarner Losh 
184ba6c22ceSWarner Losh 	/*
185ba6c22ceSWarner Losh 	 * Information about the current rate limiters, if any
186ba6c22ceSWarner Losh 	 */
187ba6c22ceSWarner Losh 	io_limiter	limiter;	/* How are I/Os being limited */
188ba6c22ceSWarner Losh 	int		min;		/* Low range of limit */
189ba6c22ceSWarner Losh 	int		max;		/* High range of limit */
190ba6c22ceSWarner Losh 	int		current;	/* Current rate limiter */
191ba6c22ceSWarner Losh 	int		l_value1;	/* per-limiter scratch value 1. */
192ba6c22ceSWarner Losh 	int		l_value2;	/* per-limiter scratch value 2. */
193ba6c22ceSWarner Losh 
194ba6c22ceSWarner Losh 
195ba6c22ceSWarner Losh 	/*
196ba6c22ceSWarner Losh 	 * Debug information about counts of I/Os that have gone through the
197ba6c22ceSWarner Losh 	 * scheduler.
198ba6c22ceSWarner Losh 	 */
199ba6c22ceSWarner Losh 	int		pending;	/* I/Os pending in the hardware */
200ba6c22ceSWarner Losh 	int		queued;		/* number currently in the queue */
201ba6c22ceSWarner Losh 	int		total;		/* Total for all time -- wraps */
202ba6c22ceSWarner Losh 	int		in;		/* number queued all time -- wraps */
203ba6c22ceSWarner Losh 	int		out;		/* number completed all time -- wraps */
204ba6c22ceSWarner Losh 
205ba6c22ceSWarner Losh 	/*
206ba6c22ceSWarner Losh 	 * Statistics on different bits of the process.
207ba6c22ceSWarner Losh 	 */
208ba6c22ceSWarner Losh 		/* Exp Moving Average, alpha = 1 / (1 << alpha_bits) */
209ba6c22ceSWarner Losh 	sbintime_t      ema;
210ba6c22ceSWarner Losh 	sbintime_t      emss;		/* Exp Moving sum of the squares */
211ba6c22ceSWarner Losh 	sbintime_t      sd;		/* Last computed sd */
212ba6c22ceSWarner Losh 
213ba6c22ceSWarner Losh 	struct cam_iosched_softc *softc;
214ba6c22ceSWarner Losh };
215ba6c22ceSWarner Losh 
216ba6c22ceSWarner Losh 
217ba6c22ceSWarner Losh typedef enum {
218ba6c22ceSWarner Losh 	set_max = 0,			/* current = max */
219ba6c22ceSWarner Losh 	read_latency,			/* Steer read latency by throttling writes */
220ba6c22ceSWarner Losh 	cl_max				/* Keep last */
221ba6c22ceSWarner Losh } control_type;
222ba6c22ceSWarner Losh 
223ba6c22ceSWarner Losh static const char *cam_iosched_control_type_names[] =
224ba6c22ceSWarner Losh     { "set_max", "read_latency" };
225ba6c22ceSWarner Losh 
226ba6c22ceSWarner Losh struct control_loop
227ba6c22ceSWarner Losh {
228ba6c22ceSWarner Losh 	/*
229ba6c22ceSWarner Losh 	 * sysctl state for this subnode.
230ba6c22ceSWarner Losh 	 */
231ba6c22ceSWarner Losh 	struct sysctl_ctx_list	sysctl_ctx;
232ba6c22ceSWarner Losh 	struct sysctl_oid	*sysctl_tree;
233ba6c22ceSWarner Losh 
234ba6c22ceSWarner Losh 	sbintime_t	next_steer;		/* Time of next steer */
235ba6c22ceSWarner Losh 	sbintime_t	steer_interval;		/* How often do we steer? */
236ba6c22ceSWarner Losh 	sbintime_t	lolat;
237ba6c22ceSWarner Losh 	sbintime_t	hilat;
238ba6c22ceSWarner Losh 	int		alpha;
239ba6c22ceSWarner Losh 	control_type	type;			/* What type of control? */
240ba6c22ceSWarner Losh 	int		last_count;		/* Last I/O count */
241ba6c22ceSWarner Losh 
242ba6c22ceSWarner Losh 	struct cam_iosched_softc *softc;
243ba6c22ceSWarner Losh };
244ba6c22ceSWarner Losh 
245ba6c22ceSWarner Losh #endif
246ba6c22ceSWarner Losh 
247ba6c22ceSWarner Losh struct cam_iosched_softc
248ba6c22ceSWarner Losh {
249ba6c22ceSWarner Losh 	struct bio_queue_head bio_queue;
250ba6c22ceSWarner Losh 	struct bio_queue_head trim_queue;
251ba6c22ceSWarner Losh 				/* scheduler flags < 16, user flags >= 16 */
252ba6c22ceSWarner Losh 	uint32_t	flags;
253ba6c22ceSWarner Losh 	int		sort_io_queue;
254ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
255ba6c22ceSWarner Losh 	int		read_bias;		/* Read bias setting */
256ba6c22ceSWarner Losh 	int		current_read_bias;	/* Current read bias state */
257ba6c22ceSWarner Losh 	int		total_ticks;
258ba6c22ceSWarner Losh 
259ba6c22ceSWarner Losh 	struct bio_queue_head write_queue;
260ba6c22ceSWarner Losh 	struct iop_stats read_stats, write_stats, trim_stats;
261ba6c22ceSWarner Losh 	struct sysctl_ctx_list	sysctl_ctx;
262ba6c22ceSWarner Losh 	struct sysctl_oid	*sysctl_tree;
263ba6c22ceSWarner Losh 
264ba6c22ceSWarner Losh 	int		quanta;			/* Number of quanta per second */
265ba6c22ceSWarner Losh 	struct callout	ticker;			/* Callout for our quota system */
266ba6c22ceSWarner Losh 	struct cam_periph *periph;		/* cam periph associated with this device */
267ba6c22ceSWarner Losh 	uint32_t	this_frac;		/* Fraction of a second (1024ths) for this tick */
268ba6c22ceSWarner Losh 	sbintime_t	last_time;		/* Last time we ticked */
269ba6c22ceSWarner Losh 	struct control_loop cl;
270ba6c22ceSWarner Losh #endif
271ba6c22ceSWarner Losh };
272ba6c22ceSWarner Losh 
273ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
274ba6c22ceSWarner Losh /*
275ba6c22ceSWarner Losh  * helper functions to call the limsw functions.
276ba6c22ceSWarner Losh  */
277ba6c22ceSWarner Losh static int
278ba6c22ceSWarner Losh cam_iosched_limiter_init(struct iop_stats *ios)
279ba6c22ceSWarner Losh {
280ba6c22ceSWarner Losh 	int lim = ios->limiter;
281ba6c22ceSWarner Losh 
282ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
283ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
284ba6c22ceSWarner Losh 		return EINVAL;
285ba6c22ceSWarner Losh 
286ba6c22ceSWarner Losh 	if (limsw[lim].l_init)
287ba6c22ceSWarner Losh 		return limsw[lim].l_init(ios);
288ba6c22ceSWarner Losh 
289ba6c22ceSWarner Losh 	return 0;
290ba6c22ceSWarner Losh }
291ba6c22ceSWarner Losh 
292ba6c22ceSWarner Losh static int
293ba6c22ceSWarner Losh cam_iosched_limiter_tick(struct iop_stats *ios)
294ba6c22ceSWarner Losh {
295ba6c22ceSWarner Losh 	int lim = ios->limiter;
296ba6c22ceSWarner Losh 
297ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
298ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
299ba6c22ceSWarner Losh 		return EINVAL;
300ba6c22ceSWarner Losh 
301ba6c22ceSWarner Losh 	if (limsw[lim].l_tick)
302ba6c22ceSWarner Losh 		return limsw[lim].l_tick(ios);
303ba6c22ceSWarner Losh 
304ba6c22ceSWarner Losh 	return 0;
305ba6c22ceSWarner Losh }
306ba6c22ceSWarner Losh 
307ba6c22ceSWarner Losh static int
308ba6c22ceSWarner Losh cam_iosched_limiter_iop(struct iop_stats *ios, struct bio *bp)
309ba6c22ceSWarner Losh {
310ba6c22ceSWarner Losh 	int lim = ios->limiter;
311ba6c22ceSWarner Losh 
312ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
313ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
314ba6c22ceSWarner Losh 		return EINVAL;
315ba6c22ceSWarner Losh 
316ba6c22ceSWarner Losh 	if (limsw[lim].l_iop)
317ba6c22ceSWarner Losh 		return limsw[lim].l_iop(ios, bp);
318ba6c22ceSWarner Losh 
319ba6c22ceSWarner Losh 	return 0;
320ba6c22ceSWarner Losh }
321ba6c22ceSWarner Losh 
322ba6c22ceSWarner Losh static int
323ba6c22ceSWarner Losh cam_iosched_limiter_caniop(struct iop_stats *ios, struct bio *bp)
324ba6c22ceSWarner Losh {
325ba6c22ceSWarner Losh 	int lim = ios->limiter;
326ba6c22ceSWarner Losh 
327ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
328ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
329ba6c22ceSWarner Losh 		return EINVAL;
330ba6c22ceSWarner Losh 
331ba6c22ceSWarner Losh 	if (limsw[lim].l_caniop)
332ba6c22ceSWarner Losh 		return limsw[lim].l_caniop(ios, bp);
333ba6c22ceSWarner Losh 
334ba6c22ceSWarner Losh 	return 0;
335ba6c22ceSWarner Losh }
336ba6c22ceSWarner Losh 
337ba6c22ceSWarner Losh static int
338ba6c22ceSWarner Losh cam_iosched_limiter_iodone(struct iop_stats *ios, struct bio *bp)
339ba6c22ceSWarner Losh {
340ba6c22ceSWarner Losh 	int lim = ios->limiter;
341ba6c22ceSWarner Losh 
342ba6c22ceSWarner Losh 	/* maybe this should be a kassert */
343ba6c22ceSWarner Losh 	if (lim < none || lim >= limiter_max)
344ba6c22ceSWarner Losh 		return 0;
345ba6c22ceSWarner Losh 
346ba6c22ceSWarner Losh 	if (limsw[lim].l_iodone)
347ba6c22ceSWarner Losh 		return limsw[lim].l_iodone(ios, bp);
348ba6c22ceSWarner Losh 
349ba6c22ceSWarner Losh 	return 0;
350ba6c22ceSWarner Losh }
351ba6c22ceSWarner Losh 
352ba6c22ceSWarner Losh /*
353ba6c22ceSWarner Losh  * Functions to implement the different kinds of limiters
354ba6c22ceSWarner Losh  */
355ba6c22ceSWarner Losh 
356ba6c22ceSWarner Losh static int
357ba6c22ceSWarner Losh cam_iosched_qd_iop(struct iop_stats *ios, struct bio *bp)
358ba6c22ceSWarner Losh {
359ba6c22ceSWarner Losh 
360ba6c22ceSWarner Losh 	if (ios->current <= 0 || ios->pending < ios->current)
361ba6c22ceSWarner Losh 		return 0;
362ba6c22ceSWarner Losh 
363ba6c22ceSWarner Losh 	return EAGAIN;
364ba6c22ceSWarner Losh }
365ba6c22ceSWarner Losh 
366ba6c22ceSWarner Losh static int
367ba6c22ceSWarner Losh cam_iosched_qd_caniop(struct iop_stats *ios, struct bio *bp)
368ba6c22ceSWarner Losh {
369ba6c22ceSWarner Losh 
370ba6c22ceSWarner Losh 	if (ios->current <= 0 || ios->pending < ios->current)
371ba6c22ceSWarner Losh 		return 0;
372ba6c22ceSWarner Losh 
373ba6c22ceSWarner Losh 	return EAGAIN;
374ba6c22ceSWarner Losh }
375ba6c22ceSWarner Losh 
376ba6c22ceSWarner Losh static int
377ba6c22ceSWarner Losh cam_iosched_qd_iodone(struct iop_stats *ios, struct bio *bp)
378ba6c22ceSWarner Losh {
379ba6c22ceSWarner Losh 
380ba6c22ceSWarner Losh 	if (ios->current <= 0 || ios->pending != ios->current)
381ba6c22ceSWarner Losh 		return 0;
382ba6c22ceSWarner Losh 
383ba6c22ceSWarner Losh 	return 1;
384ba6c22ceSWarner Losh }
385ba6c22ceSWarner Losh 
386ba6c22ceSWarner Losh static int
387ba6c22ceSWarner Losh cam_iosched_iops_init(struct iop_stats *ios)
388ba6c22ceSWarner Losh {
389ba6c22ceSWarner Losh 
390ba6c22ceSWarner Losh 	ios->l_value1 = ios->current / ios->softc->quanta;
391ba6c22ceSWarner Losh 	if (ios->l_value1 <= 0)
392ba6c22ceSWarner Losh 		ios->l_value1 = 1;
393ba6c22ceSWarner Losh 
394ba6c22ceSWarner Losh 	return 0;
395ba6c22ceSWarner Losh }
396ba6c22ceSWarner Losh 
397ba6c22ceSWarner Losh static int
398ba6c22ceSWarner Losh cam_iosched_iops_tick(struct iop_stats *ios)
399ba6c22ceSWarner Losh {
400ba6c22ceSWarner Losh 
401ba6c22ceSWarner Losh 	ios->l_value1 = (int)((ios->current * (uint64_t)ios->softc->this_frac) >> 16);
402ba6c22ceSWarner Losh 	if (ios->l_value1 <= 0)
403ba6c22ceSWarner Losh 		ios->l_value1 = 1;
404ba6c22ceSWarner Losh 
405ba6c22ceSWarner Losh 	return 0;
406ba6c22ceSWarner Losh }
407ba6c22ceSWarner Losh 
408ba6c22ceSWarner Losh static int
409ba6c22ceSWarner Losh cam_iosched_iops_caniop(struct iop_stats *ios, struct bio *bp)
410ba6c22ceSWarner Losh {
411ba6c22ceSWarner Losh 
412ba6c22ceSWarner Losh 	/*
413ba6c22ceSWarner Losh 	 * So if we have any more IOPs left, allow it,
414ba6c22ceSWarner Losh 	 * otherwise wait.
415ba6c22ceSWarner Losh 	 */
416ba6c22ceSWarner Losh 	if (ios->l_value1 <= 0)
417ba6c22ceSWarner Losh 		return EAGAIN;
418ba6c22ceSWarner Losh 	return 0;
419ba6c22ceSWarner Losh }
420ba6c22ceSWarner Losh 
421ba6c22ceSWarner Losh static int
422ba6c22ceSWarner Losh cam_iosched_iops_iop(struct iop_stats *ios, struct bio *bp)
423ba6c22ceSWarner Losh {
424ba6c22ceSWarner Losh 	int rv;
425ba6c22ceSWarner Losh 
426ba6c22ceSWarner Losh 	rv = cam_iosched_limiter_caniop(ios, bp);
427ba6c22ceSWarner Losh 	if (rv == 0)
428ba6c22ceSWarner Losh 		ios->l_value1--;
429ba6c22ceSWarner Losh 
430ba6c22ceSWarner Losh 	return rv;
431ba6c22ceSWarner Losh }
432ba6c22ceSWarner Losh 
433ba6c22ceSWarner Losh static int
434ba6c22ceSWarner Losh cam_iosched_bw_init(struct iop_stats *ios)
435ba6c22ceSWarner Losh {
436ba6c22ceSWarner Losh 
437ba6c22ceSWarner Losh 	/* ios->current is in kB/s, so scale to bytes */
438ba6c22ceSWarner Losh 	ios->l_value1 = ios->current * 1000 / ios->softc->quanta;
439ba6c22ceSWarner Losh 
440ba6c22ceSWarner Losh 	return 0;
441ba6c22ceSWarner Losh }
442ba6c22ceSWarner Losh 
443ba6c22ceSWarner Losh static int
444ba6c22ceSWarner Losh cam_iosched_bw_tick(struct iop_stats *ios)
445ba6c22ceSWarner Losh {
446ba6c22ceSWarner Losh 	int bw;
447ba6c22ceSWarner Losh 
448ba6c22ceSWarner Losh 	/*
449ba6c22ceSWarner Losh 	 * If we're in the hole for available quota from
450ba6c22ceSWarner Losh 	 * the last time, then add the quantum for this.
451ba6c22ceSWarner Losh 	 * If we have any left over from last quantum,
452ba6c22ceSWarner Losh 	 * then too bad, that's lost. Also, ios->current
453ba6c22ceSWarner Losh 	 * is in kB/s, so scale.
454ba6c22ceSWarner Losh 	 *
455ba6c22ceSWarner Losh 	 * We also allow up to 4 quanta of credits to
456ba6c22ceSWarner Losh 	 * accumulate to deal with burstiness. 4 is extremely
457ba6c22ceSWarner Losh 	 * arbitrary.
458ba6c22ceSWarner Losh 	 */
459ba6c22ceSWarner Losh 	bw = (int)((ios->current * 1000ull * (uint64_t)ios->softc->this_frac) >> 16);
460ba6c22ceSWarner Losh 	if (ios->l_value1 < bw * 4)
461ba6c22ceSWarner Losh 		ios->l_value1 += bw;
462ba6c22ceSWarner Losh 
463ba6c22ceSWarner Losh 	return 0;
464ba6c22ceSWarner Losh }
465ba6c22ceSWarner Losh 
466ba6c22ceSWarner Losh static int
467ba6c22ceSWarner Losh cam_iosched_bw_caniop(struct iop_stats *ios, struct bio *bp)
468ba6c22ceSWarner Losh {
469ba6c22ceSWarner Losh 	/*
470ba6c22ceSWarner Losh 	 * So if we have any more bw quota left, allow it,
471ba6c22ceSWarner Losh 	 * otherwise wait. Not, we'll go negative and that's
472ba6c22ceSWarner Losh 	 * OK. We'll just get a lettle less next quota.
473ba6c22ceSWarner Losh 	 *
474ba6c22ceSWarner Losh 	 * Note on going negative: that allows us to process
475ba6c22ceSWarner Losh 	 * requests in order better, since we won't allow
476ba6c22ceSWarner Losh 	 * shorter reads to get around the long one that we
477ba6c22ceSWarner Losh 	 * don't have the quota to do just yet. It also prevents
478ba6c22ceSWarner Losh 	 * starvation by being a little more permissive about
479ba6c22ceSWarner Losh 	 * what we let through this quantum (to prevent the
480ba6c22ceSWarner Losh 	 * starvation), at the cost of getting a little less
481ba6c22ceSWarner Losh 	 * next quantum.
482ba6c22ceSWarner Losh 	 */
483ba6c22ceSWarner Losh 	if (ios->l_value1 <= 0)
484ba6c22ceSWarner Losh 		return EAGAIN;
485ba6c22ceSWarner Losh 
486ba6c22ceSWarner Losh 
487ba6c22ceSWarner Losh 	return 0;
488ba6c22ceSWarner Losh }
489ba6c22ceSWarner Losh 
490ba6c22ceSWarner Losh static int
491ba6c22ceSWarner Losh cam_iosched_bw_iop(struct iop_stats *ios, struct bio *bp)
492ba6c22ceSWarner Losh {
493ba6c22ceSWarner Losh 	int rv;
494ba6c22ceSWarner Losh 
495ba6c22ceSWarner Losh 	rv = cam_iosched_limiter_caniop(ios, bp);
496ba6c22ceSWarner Losh 	if (rv == 0)
497ba6c22ceSWarner Losh 		ios->l_value1 -= bp->bio_length;
498ba6c22ceSWarner Losh 
499ba6c22ceSWarner Losh 	return rv;
500ba6c22ceSWarner Losh }
501ba6c22ceSWarner Losh 
502ba6c22ceSWarner Losh static void cam_iosched_cl_maybe_steer(struct control_loop *clp);
503ba6c22ceSWarner Losh 
504ba6c22ceSWarner Losh static void
505ba6c22ceSWarner Losh cam_iosched_ticker(void *arg)
506ba6c22ceSWarner Losh {
507ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc = arg;
508ba6c22ceSWarner Losh 	sbintime_t now, delta;
509ba6c22ceSWarner Losh 
510ba6c22ceSWarner Losh 	callout_reset(&isc->ticker, hz / isc->quanta - 1, cam_iosched_ticker, isc);
511ba6c22ceSWarner Losh 
512ba6c22ceSWarner Losh 	now = sbinuptime();
513ba6c22ceSWarner Losh 	delta = now - isc->last_time;
514ba6c22ceSWarner Losh 	isc->this_frac = (uint32_t)delta >> 16;		/* Note: discards seconds -- should be 0 harmless if not */
515ba6c22ceSWarner Losh 	isc->last_time = now;
516ba6c22ceSWarner Losh 
517ba6c22ceSWarner Losh 	cam_iosched_cl_maybe_steer(&isc->cl);
518ba6c22ceSWarner Losh 
519ba6c22ceSWarner Losh 	cam_iosched_limiter_tick(&isc->read_stats);
520ba6c22ceSWarner Losh 	cam_iosched_limiter_tick(&isc->write_stats);
521ba6c22ceSWarner Losh 	cam_iosched_limiter_tick(&isc->trim_stats);
522ba6c22ceSWarner Losh 
523ba6c22ceSWarner Losh 	cam_iosched_schedule(isc, isc->periph);
524ba6c22ceSWarner Losh 
525ba6c22ceSWarner Losh 	isc->total_ticks++;
526ba6c22ceSWarner Losh }
527ba6c22ceSWarner Losh 
528ba6c22ceSWarner Losh 
529ba6c22ceSWarner Losh static void
530ba6c22ceSWarner Losh cam_iosched_cl_init(struct control_loop *clp, struct cam_iosched_softc *isc)
531ba6c22ceSWarner Losh {
532ba6c22ceSWarner Losh 
533ba6c22ceSWarner Losh 	clp->next_steer = sbinuptime();
534ba6c22ceSWarner Losh 	clp->softc = isc;
535ba6c22ceSWarner Losh 	clp->steer_interval = SBT_1S * 5;	/* Let's start out steering every 5s */
536ba6c22ceSWarner Losh 	clp->lolat = 5 * SBT_1MS;
537ba6c22ceSWarner Losh 	clp->hilat = 15 * SBT_1MS;
538ba6c22ceSWarner Losh 	clp->alpha = 20;			/* Alpha == gain. 20 = .2 */
539ba6c22ceSWarner Losh 	clp->type = set_max;
540ba6c22ceSWarner Losh }
541ba6c22ceSWarner Losh 
542ba6c22ceSWarner Losh static void
543ba6c22ceSWarner Losh cam_iosched_cl_maybe_steer(struct control_loop *clp)
544ba6c22ceSWarner Losh {
545ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
546ba6c22ceSWarner Losh 	sbintime_t now, lat;
547ba6c22ceSWarner Losh 	int old;
548ba6c22ceSWarner Losh 
549ba6c22ceSWarner Losh 	isc = clp->softc;
550ba6c22ceSWarner Losh 	now = isc->last_time;
551ba6c22ceSWarner Losh 	if (now < clp->next_steer)
552ba6c22ceSWarner Losh 		return;
553ba6c22ceSWarner Losh 
554ba6c22ceSWarner Losh 	clp->next_steer = now + clp->steer_interval;
555ba6c22ceSWarner Losh 	switch (clp->type) {
556ba6c22ceSWarner Losh 	case set_max:
557ba6c22ceSWarner Losh 		if (isc->write_stats.current != isc->write_stats.max)
558ba6c22ceSWarner Losh 			printf("Steering write from %d kBps to %d kBps\n",
559ba6c22ceSWarner Losh 			    isc->write_stats.current, isc->write_stats.max);
560ba6c22ceSWarner Losh 		isc->read_stats.current = isc->read_stats.max;
561ba6c22ceSWarner Losh 		isc->write_stats.current = isc->write_stats.max;
562ba6c22ceSWarner Losh 		isc->trim_stats.current = isc->trim_stats.max;
563ba6c22ceSWarner Losh 		break;
564ba6c22ceSWarner Losh 	case read_latency:
565ba6c22ceSWarner Losh 		old = isc->write_stats.current;
566ba6c22ceSWarner Losh 		lat = isc->read_stats.ema;
567ba6c22ceSWarner Losh 		/*
568ba6c22ceSWarner Losh 		 * Simple PLL-like engine. Since we're steering to a range for
569ba6c22ceSWarner Losh 		 * the SP (set point) that makes things a little more
570ba6c22ceSWarner Losh 		 * complicated. In addition, we're not directly controlling our
571ba6c22ceSWarner Losh 		 * PV (process variable), the read latency, but instead are
572ba6c22ceSWarner Losh 		 * manipulating the write bandwidth limit for our MV
573ba6c22ceSWarner Losh 		 * (manipulation variable), analysis of this code gets a bit
574ba6c22ceSWarner Losh 		 * messy. Also, the MV is a very noisy control surface for read
575ba6c22ceSWarner Losh 		 * latency since it is affected by many hidden processes inside
576ba6c22ceSWarner Losh 		 * the device which change how responsive read latency will be
577ba6c22ceSWarner Losh 		 * in reaction to changes in write bandwidth. Unlike the classic
578ba6c22ceSWarner Losh 		 * boiler control PLL. this may result in over-steering while
579ba6c22ceSWarner Losh 		 * the SSD takes its time to react to the new, lower load. This
580ba6c22ceSWarner Losh 		 * is why we use a relatively low alpha of between .1 and .25 to
581ba6c22ceSWarner Losh 		 * compensate for this effect. At .1, it takes ~22 steering
582ba6c22ceSWarner Losh 		 * intervals to back off by a factor of 10. At .2 it only takes
583ba6c22ceSWarner Losh 		 * ~10. At .25 it only takes ~8. However some preliminary data
584ba6c22ceSWarner Losh 		 * from the SSD drives suggests a reasponse time in 10's of
585ba6c22ceSWarner Losh 		 * seconds before latency drops regardless of the new write
586ba6c22ceSWarner Losh 		 * rate. Careful observation will be reqiured to tune this
587ba6c22ceSWarner Losh 		 * effectively.
588ba6c22ceSWarner Losh 		 *
589ba6c22ceSWarner Losh 		 * Also, when there's no read traffic, we jack up the write
590ba6c22ceSWarner Losh 		 * limit too regardless of the last read latency.  10 is
591ba6c22ceSWarner Losh 		 * somewhat arbitrary.
592ba6c22ceSWarner Losh 		 */
593ba6c22ceSWarner Losh 		if (lat < clp->lolat || isc->read_stats.total - clp->last_count < 10)
594ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.current *
595ba6c22ceSWarner Losh 			    (100 + clp->alpha) / 100;	/* Scale up */
596ba6c22ceSWarner Losh 		else if (lat > clp->hilat)
597ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.current *
598ba6c22ceSWarner Losh 			    (100 - clp->alpha) / 100;	/* Scale down */
599ba6c22ceSWarner Losh 		clp->last_count = isc->read_stats.total;
600ba6c22ceSWarner Losh 
601ba6c22ceSWarner Losh 		/*
602ba6c22ceSWarner Losh 		 * Even if we don't steer, per se, enforce the min/max limits as
603ba6c22ceSWarner Losh 		 * those may have changed.
604ba6c22ceSWarner Losh 		 */
605ba6c22ceSWarner Losh 		if (isc->write_stats.current < isc->write_stats.min)
606ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.min;
607ba6c22ceSWarner Losh 		if (isc->write_stats.current > isc->write_stats.max)
608ba6c22ceSWarner Losh 			isc->write_stats.current = isc->write_stats.max;
609*2b5c19f1SWarner Losh 		if (old != isc->write_stats.current && 	iosched_debug)
610*2b5c19f1SWarner Losh 			printf("Steering write from %d kBps to %d kBps due to latency of %jdms\n",
611ba6c22ceSWarner Losh 			    old, isc->write_stats.current,
612*2b5c19f1SWarner Losh 			    (uintmax_t)((uint64_t)1000000 * (uint32_t)lat) >> 32);
613ba6c22ceSWarner Losh 		break;
614ba6c22ceSWarner Losh 	case cl_max:
615ba6c22ceSWarner Losh 		break;
616ba6c22ceSWarner Losh 	}
617ba6c22ceSWarner Losh }
618ba6c22ceSWarner Losh #endif
619ba6c22ceSWarner Losh 
620ba6c22ceSWarner Losh 			/* Trim or similar currently pending completion */
621ba6c22ceSWarner Losh #define CAM_IOSCHED_FLAG_TRIM_ACTIVE	(1ul << 0)
622ba6c22ceSWarner Losh 			/* Callout active, and needs to be torn down */
623ba6c22ceSWarner Losh #define CAM_IOSCHED_FLAG_CALLOUT_ACTIVE (1ul << 1)
624ba6c22ceSWarner Losh 
625ba6c22ceSWarner Losh 			/* Periph drivers set these flags to indicate work */
626ba6c22ceSWarner Losh #define CAM_IOSCHED_FLAG_WORK_FLAGS	((0xffffu) << 16)
627ba6c22ceSWarner Losh 
6285ede5b8cSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
629ba6c22ceSWarner Losh static void
630ba6c22ceSWarner Losh cam_iosched_io_metric_update(struct cam_iosched_softc *isc,
631ba6c22ceSWarner Losh     sbintime_t sim_latency, int cmd, size_t size);
6325ede5b8cSWarner Losh #endif
633ba6c22ceSWarner Losh 
634ba6c22ceSWarner Losh static inline int
635ba6c22ceSWarner Losh cam_iosched_has_flagged_work(struct cam_iosched_softc *isc)
636ba6c22ceSWarner Losh {
637ba6c22ceSWarner Losh 	return !!(isc->flags & CAM_IOSCHED_FLAG_WORK_FLAGS);
638ba6c22ceSWarner Losh }
639ba6c22ceSWarner Losh 
640ba6c22ceSWarner Losh static inline int
641ba6c22ceSWarner Losh cam_iosched_has_io(struct cam_iosched_softc *isc)
642ba6c22ceSWarner Losh {
643ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
644ba6c22ceSWarner Losh 	if (do_netflix_iosched) {
645ba6c22ceSWarner Losh 		struct bio *rbp = bioq_first(&isc->bio_queue);
646ba6c22ceSWarner Losh 		struct bio *wbp = bioq_first(&isc->write_queue);
647ba6c22ceSWarner Losh 		int can_write = wbp != NULL &&
648ba6c22ceSWarner Losh 		    cam_iosched_limiter_caniop(&isc->write_stats, wbp) == 0;
649ba6c22ceSWarner Losh 		int can_read = rbp != NULL &&
650ba6c22ceSWarner Losh 		    cam_iosched_limiter_caniop(&isc->read_stats, rbp) == 0;
651ba6c22ceSWarner Losh 		if (iosched_debug > 2) {
652ba6c22ceSWarner Losh 			printf("can write %d: pending_writes %d max_writes %d\n", can_write, isc->write_stats.pending, isc->write_stats.max);
653ba6c22ceSWarner Losh 			printf("can read %d: read_stats.pending %d max_reads %d\n", can_read, isc->read_stats.pending, isc->read_stats.max);
654ba6c22ceSWarner Losh 			printf("Queued reads %d writes %d\n", isc->read_stats.queued, isc->write_stats.queued);
655ba6c22ceSWarner Losh 		}
656ba6c22ceSWarner Losh 		return can_read || can_write;
657ba6c22ceSWarner Losh 	}
658ba6c22ceSWarner Losh #endif
659ba6c22ceSWarner Losh 	return bioq_first(&isc->bio_queue) != NULL;
660ba6c22ceSWarner Losh }
661ba6c22ceSWarner Losh 
662ba6c22ceSWarner Losh static inline int
663ba6c22ceSWarner Losh cam_iosched_has_more_trim(struct cam_iosched_softc *isc)
664ba6c22ceSWarner Losh {
665ba6c22ceSWarner Losh 	return !(isc->flags & CAM_IOSCHED_FLAG_TRIM_ACTIVE) &&
666ba6c22ceSWarner Losh 	    bioq_first(&isc->trim_queue);
667ba6c22ceSWarner Losh }
668ba6c22ceSWarner Losh 
669ba6c22ceSWarner Losh #define cam_iosched_sort_queue(isc)	((isc)->sort_io_queue >= 0 ?	\
670ba6c22ceSWarner Losh     (isc)->sort_io_queue : cam_sort_io_queues)
671ba6c22ceSWarner Losh 
672ba6c22ceSWarner Losh 
673ba6c22ceSWarner Losh static inline int
674ba6c22ceSWarner Losh cam_iosched_has_work(struct cam_iosched_softc *isc)
675ba6c22ceSWarner Losh {
676ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
677ba6c22ceSWarner Losh 	if (iosched_debug > 2)
678ba6c22ceSWarner Losh 		printf("has work: %d %d %d\n", cam_iosched_has_io(isc),
679ba6c22ceSWarner Losh 		    cam_iosched_has_more_trim(isc),
680ba6c22ceSWarner Losh 		    cam_iosched_has_flagged_work(isc));
681ba6c22ceSWarner Losh #endif
682ba6c22ceSWarner Losh 
683ba6c22ceSWarner Losh 	return cam_iosched_has_io(isc) ||
684ba6c22ceSWarner Losh 		cam_iosched_has_more_trim(isc) ||
685ba6c22ceSWarner Losh 		cam_iosched_has_flagged_work(isc);
686ba6c22ceSWarner Losh }
687ba6c22ceSWarner Losh 
688ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
689ba6c22ceSWarner Losh static void
690ba6c22ceSWarner Losh cam_iosched_iop_stats_init(struct cam_iosched_softc *isc, struct iop_stats *ios)
691ba6c22ceSWarner Losh {
692ba6c22ceSWarner Losh 
693ba6c22ceSWarner Losh 	ios->limiter = none;
694ba6c22ceSWarner Losh 	cam_iosched_limiter_init(ios);
695ba6c22ceSWarner Losh 	ios->in = 0;
696ba6c22ceSWarner Losh 	ios->max = 300000;
697ba6c22ceSWarner Losh 	ios->min = 1;
698ba6c22ceSWarner Losh 	ios->out = 0;
699ba6c22ceSWarner Losh 	ios->pending = 0;
700ba6c22ceSWarner Losh 	ios->queued = 0;
701ba6c22ceSWarner Losh 	ios->total = 0;
702ba6c22ceSWarner Losh 	ios->ema = 0;
703ba6c22ceSWarner Losh 	ios->emss = 0;
704ba6c22ceSWarner Losh 	ios->sd = 0;
705ba6c22ceSWarner Losh 	ios->softc = isc;
706ba6c22ceSWarner Losh }
707ba6c22ceSWarner Losh 
708ba6c22ceSWarner Losh static int
709ba6c22ceSWarner Losh cam_iosched_limiter_sysctl(SYSCTL_HANDLER_ARGS)
710ba6c22ceSWarner Losh {
711ba6c22ceSWarner Losh 	char buf[16];
712ba6c22ceSWarner Losh 	struct iop_stats *ios;
713ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
714ba6c22ceSWarner Losh 	int value, i, error, cantick;
715ba6c22ceSWarner Losh 	const char *p;
716ba6c22ceSWarner Losh 
717ba6c22ceSWarner Losh 	ios = arg1;
718ba6c22ceSWarner Losh 	isc = ios->softc;
719ba6c22ceSWarner Losh 	value = ios->limiter;
720ba6c22ceSWarner Losh 	if (value < none || value >= limiter_max)
721ba6c22ceSWarner Losh 		p = "UNKNOWN";
722ba6c22ceSWarner Losh 	else
723ba6c22ceSWarner Losh 		p = cam_iosched_limiter_names[value];
724ba6c22ceSWarner Losh 
725ba6c22ceSWarner Losh 	strlcpy(buf, p, sizeof(buf));
726ba6c22ceSWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
727ba6c22ceSWarner Losh 	if (error != 0 || req->newptr == NULL)
728ba6c22ceSWarner Losh 		return error;
729ba6c22ceSWarner Losh 
730ba6c22ceSWarner Losh 	cam_periph_lock(isc->periph);
731ba6c22ceSWarner Losh 
732ba6c22ceSWarner Losh 	for (i = none; i < limiter_max; i++) {
733ba6c22ceSWarner Losh 		if (strcmp(buf, cam_iosched_limiter_names[i]) != 0)
734ba6c22ceSWarner Losh 			continue;
735ba6c22ceSWarner Losh 		ios->limiter = i;
736ba6c22ceSWarner Losh 		error = cam_iosched_limiter_init(ios);
737ba6c22ceSWarner Losh 		if (error != 0) {
738ba6c22ceSWarner Losh 			ios->limiter = value;
739ba6c22ceSWarner Losh 			cam_periph_unlock(isc->periph);
740ba6c22ceSWarner Losh 			return error;
741ba6c22ceSWarner Losh 		}
742ba6c22ceSWarner Losh 		cantick = !!limsw[isc->read_stats.limiter].l_tick +
743ba6c22ceSWarner Losh 		    !!limsw[isc->write_stats.limiter].l_tick +
744ba6c22ceSWarner Losh 		    !!limsw[isc->trim_stats.limiter].l_tick +
745ba6c22ceSWarner Losh 		    1;	/* Control loop requires it */
746ba6c22ceSWarner Losh 		if (isc->flags & CAM_IOSCHED_FLAG_CALLOUT_ACTIVE) {
747ba6c22ceSWarner Losh 			if (cantick == 0) {
748ba6c22ceSWarner Losh 				callout_stop(&isc->ticker);
749ba6c22ceSWarner Losh 				isc->flags &= ~CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
750ba6c22ceSWarner Losh 			}
751ba6c22ceSWarner Losh 		} else {
752ba6c22ceSWarner Losh 			if (cantick != 0) {
753ba6c22ceSWarner Losh 				callout_reset(&isc->ticker, hz / isc->quanta - 1, cam_iosched_ticker, isc);
754ba6c22ceSWarner Losh 				isc->flags |= CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
755ba6c22ceSWarner Losh 			}
756ba6c22ceSWarner Losh 		}
757ba6c22ceSWarner Losh 
758ba6c22ceSWarner Losh 		cam_periph_unlock(isc->periph);
759ba6c22ceSWarner Losh 		return 0;
760ba6c22ceSWarner Losh 	}
761ba6c22ceSWarner Losh 
762ba6c22ceSWarner Losh 	cam_periph_unlock(isc->periph);
763ba6c22ceSWarner Losh 	return EINVAL;
764ba6c22ceSWarner Losh }
765ba6c22ceSWarner Losh 
766ba6c22ceSWarner Losh static int
767ba6c22ceSWarner Losh cam_iosched_control_type_sysctl(SYSCTL_HANDLER_ARGS)
768ba6c22ceSWarner Losh {
769ba6c22ceSWarner Losh 	char buf[16];
770ba6c22ceSWarner Losh 	struct control_loop *clp;
771ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
772ba6c22ceSWarner Losh 	int value, i, error;
773ba6c22ceSWarner Losh 	const char *p;
774ba6c22ceSWarner Losh 
775ba6c22ceSWarner Losh 	clp = arg1;
776ba6c22ceSWarner Losh 	isc = clp->softc;
777ba6c22ceSWarner Losh 	value = clp->type;
778ba6c22ceSWarner Losh 	if (value < none || value >= cl_max)
779ba6c22ceSWarner Losh 		p = "UNKNOWN";
780ba6c22ceSWarner Losh 	else
781ba6c22ceSWarner Losh 		p = cam_iosched_control_type_names[value];
782ba6c22ceSWarner Losh 
783ba6c22ceSWarner Losh 	strlcpy(buf, p, sizeof(buf));
784ba6c22ceSWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
785ba6c22ceSWarner Losh 	if (error != 0 || req->newptr == NULL)
786ba6c22ceSWarner Losh 		return error;
787ba6c22ceSWarner Losh 
788ba6c22ceSWarner Losh 	for (i = set_max; i < cl_max; i++) {
789ba6c22ceSWarner Losh 		if (strcmp(buf, cam_iosched_control_type_names[i]) != 0)
790ba6c22ceSWarner Losh 			continue;
791ba6c22ceSWarner Losh 		cam_periph_lock(isc->periph);
792ba6c22ceSWarner Losh 		clp->type = i;
793ba6c22ceSWarner Losh 		cam_periph_unlock(isc->periph);
794ba6c22ceSWarner Losh 		return 0;
795ba6c22ceSWarner Losh 	}
796ba6c22ceSWarner Losh 
797ba6c22ceSWarner Losh 	return EINVAL;
798ba6c22ceSWarner Losh }
799ba6c22ceSWarner Losh 
800ba6c22ceSWarner Losh static int
801ba6c22ceSWarner Losh cam_iosched_sbintime_sysctl(SYSCTL_HANDLER_ARGS)
802ba6c22ceSWarner Losh {
803ba6c22ceSWarner Losh 	char buf[16];
804ba6c22ceSWarner Losh 	sbintime_t value;
805ba6c22ceSWarner Losh 	int error;
806ba6c22ceSWarner Losh 	uint64_t us;
807ba6c22ceSWarner Losh 
808ba6c22ceSWarner Losh 	value = *(sbintime_t *)arg1;
809ba6c22ceSWarner Losh 	us = (uint64_t)value / SBT_1US;
810ba6c22ceSWarner Losh 	snprintf(buf, sizeof(buf), "%ju", (intmax_t)us);
811ba6c22ceSWarner Losh 	error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
812ba6c22ceSWarner Losh 	if (error != 0 || req->newptr == NULL)
813ba6c22ceSWarner Losh 		return error;
814ba6c22ceSWarner Losh 	us = strtoul(buf, NULL, 10);
815ba6c22ceSWarner Losh 	if (us == 0)
816ba6c22ceSWarner Losh 		return EINVAL;
817ba6c22ceSWarner Losh 	*(sbintime_t *)arg1 = us * SBT_1US;
818ba6c22ceSWarner Losh 	return 0;
819ba6c22ceSWarner Losh }
820ba6c22ceSWarner Losh 
821ba6c22ceSWarner Losh static void
822ba6c22ceSWarner Losh cam_iosched_iop_stats_sysctl_init(struct cam_iosched_softc *isc, struct iop_stats *ios, char *name)
823ba6c22ceSWarner Losh {
824ba6c22ceSWarner Losh 	struct sysctl_oid_list *n;
825ba6c22ceSWarner Losh 	struct sysctl_ctx_list *ctx;
826ba6c22ceSWarner Losh 
827ba6c22ceSWarner Losh 	ios->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx,
828ba6c22ceSWarner Losh 	    SYSCTL_CHILDREN(isc->sysctl_tree), OID_AUTO, name,
829ba6c22ceSWarner Losh 	    CTLFLAG_RD, 0, name);
830ba6c22ceSWarner Losh 	n = SYSCTL_CHILDREN(ios->sysctl_tree);
831ba6c22ceSWarner Losh 	ctx = &ios->sysctl_ctx;
832ba6c22ceSWarner Losh 
833ba6c22ceSWarner Losh 	SYSCTL_ADD_UQUAD(ctx, n,
834ba6c22ceSWarner Losh 	    OID_AUTO, "ema", CTLFLAG_RD,
835ba6c22ceSWarner Losh 	    &ios->ema,
836ba6c22ceSWarner Losh 	    "Fast Exponentially Weighted Moving Average");
837ba6c22ceSWarner Losh 	SYSCTL_ADD_UQUAD(ctx, n,
838ba6c22ceSWarner Losh 	    OID_AUTO, "emss", CTLFLAG_RD,
839ba6c22ceSWarner Losh 	    &ios->emss,
840ba6c22ceSWarner Losh 	    "Fast Exponentially Weighted Moving Sum of Squares (maybe wrong)");
841ba6c22ceSWarner Losh 	SYSCTL_ADD_UQUAD(ctx, n,
842ba6c22ceSWarner Losh 	    OID_AUTO, "sd", CTLFLAG_RD,
843ba6c22ceSWarner Losh 	    &ios->sd,
844ba6c22ceSWarner Losh 	    "Estimated SD for fast ema (may be wrong)");
845ba6c22ceSWarner Losh 
846ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
847ba6c22ceSWarner Losh 	    OID_AUTO, "pending", CTLFLAG_RD,
848ba6c22ceSWarner Losh 	    &ios->pending, 0,
849ba6c22ceSWarner Losh 	    "Instantaneous # of pending transactions");
850ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
851ba6c22ceSWarner Losh 	    OID_AUTO, "count", CTLFLAG_RD,
852ba6c22ceSWarner Losh 	    &ios->total, 0,
853ba6c22ceSWarner Losh 	    "# of transactions submitted to hardware");
854ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
855ba6c22ceSWarner Losh 	    OID_AUTO, "queued", CTLFLAG_RD,
856ba6c22ceSWarner Losh 	    &ios->queued, 0,
857ba6c22ceSWarner Losh 	    "# of transactions in the queue");
858ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
859ba6c22ceSWarner Losh 	    OID_AUTO, "in", CTLFLAG_RD,
860ba6c22ceSWarner Losh 	    &ios->in, 0,
861ba6c22ceSWarner Losh 	    "# of transactions queued to driver");
862ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
863ba6c22ceSWarner Losh 	    OID_AUTO, "out", CTLFLAG_RD,
864ba6c22ceSWarner Losh 	    &ios->out, 0,
865ba6c22ceSWarner Losh 	    "# of transactions completed");
866ba6c22ceSWarner Losh 
867ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
868ba6c22ceSWarner Losh 	    OID_AUTO, "limiter", CTLTYPE_STRING | CTLFLAG_RW,
869ba6c22ceSWarner Losh 	    ios, 0, cam_iosched_limiter_sysctl, "A",
870ba6c22ceSWarner Losh 	    "Current limiting type.");
871ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
872ba6c22ceSWarner Losh 	    OID_AUTO, "min", CTLFLAG_RW,
873ba6c22ceSWarner Losh 	    &ios->min, 0,
874ba6c22ceSWarner Losh 	    "min resource");
875ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
876ba6c22ceSWarner Losh 	    OID_AUTO, "max", CTLFLAG_RW,
877ba6c22ceSWarner Losh 	    &ios->max, 0,
878ba6c22ceSWarner Losh 	    "max resource");
879ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
880ba6c22ceSWarner Losh 	    OID_AUTO, "current", CTLFLAG_RW,
881ba6c22ceSWarner Losh 	    &ios->current, 0,
882ba6c22ceSWarner Losh 	    "current resource");
883ba6c22ceSWarner Losh 
884ba6c22ceSWarner Losh }
885ba6c22ceSWarner Losh 
886ba6c22ceSWarner Losh static void
887ba6c22ceSWarner Losh cam_iosched_iop_stats_fini(struct iop_stats *ios)
888ba6c22ceSWarner Losh {
889ba6c22ceSWarner Losh 	if (ios->sysctl_tree)
890ba6c22ceSWarner Losh 		if (sysctl_ctx_free(&ios->sysctl_ctx) != 0)
891ba6c22ceSWarner Losh 			printf("can't remove iosched sysctl stats context\n");
892ba6c22ceSWarner Losh }
893ba6c22ceSWarner Losh 
894ba6c22ceSWarner Losh static void
895ba6c22ceSWarner Losh cam_iosched_cl_sysctl_init(struct cam_iosched_softc *isc)
896ba6c22ceSWarner Losh {
897ba6c22ceSWarner Losh 	struct sysctl_oid_list *n;
898ba6c22ceSWarner Losh 	struct sysctl_ctx_list *ctx;
899ba6c22ceSWarner Losh 	struct control_loop *clp;
900ba6c22ceSWarner Losh 
901ba6c22ceSWarner Losh 	clp = &isc->cl;
902ba6c22ceSWarner Losh 	clp->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx,
903ba6c22ceSWarner Losh 	    SYSCTL_CHILDREN(isc->sysctl_tree), OID_AUTO, "control",
904ba6c22ceSWarner Losh 	    CTLFLAG_RD, 0, "Control loop info");
905ba6c22ceSWarner Losh 	n = SYSCTL_CHILDREN(clp->sysctl_tree);
906ba6c22ceSWarner Losh 	ctx = &clp->sysctl_ctx;
907ba6c22ceSWarner Losh 
908ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
909ba6c22ceSWarner Losh 	    OID_AUTO, "type", CTLTYPE_STRING | CTLFLAG_RW,
910ba6c22ceSWarner Losh 	    clp, 0, cam_iosched_control_type_sysctl, "A",
911ba6c22ceSWarner Losh 	    "Control loop algorithm");
912ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
913ba6c22ceSWarner Losh 	    OID_AUTO, "steer_interval", CTLTYPE_STRING | CTLFLAG_RW,
914ba6c22ceSWarner Losh 	    &clp->steer_interval, 0, cam_iosched_sbintime_sysctl, "A",
915ba6c22ceSWarner Losh 	    "How often to steer (in us)");
916ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
917ba6c22ceSWarner Losh 	    OID_AUTO, "lolat", CTLTYPE_STRING | CTLFLAG_RW,
918ba6c22ceSWarner Losh 	    &clp->lolat, 0, cam_iosched_sbintime_sysctl, "A",
919ba6c22ceSWarner Losh 	    "Low water mark for Latency (in us)");
920ba6c22ceSWarner Losh 	SYSCTL_ADD_PROC(ctx, n,
921ba6c22ceSWarner Losh 	    OID_AUTO, "hilat", CTLTYPE_STRING | CTLFLAG_RW,
922ba6c22ceSWarner Losh 	    &clp->hilat, 0, cam_iosched_sbintime_sysctl, "A",
923ba6c22ceSWarner Losh 	    "Hi water mark for Latency (in us)");
924ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
925ba6c22ceSWarner Losh 	    OID_AUTO, "alpha", CTLFLAG_RW,
926ba6c22ceSWarner Losh 	    &clp->alpha, 0,
927ba6c22ceSWarner Losh 	    "Alpha for PLL (x100) aka gain");
928ba6c22ceSWarner Losh }
929ba6c22ceSWarner Losh 
930ba6c22ceSWarner Losh static void
931ba6c22ceSWarner Losh cam_iosched_cl_sysctl_fini(struct control_loop *clp)
932ba6c22ceSWarner Losh {
933ba6c22ceSWarner Losh 	if (clp->sysctl_tree)
934ba6c22ceSWarner Losh 		if (sysctl_ctx_free(&clp->sysctl_ctx) != 0)
935ba6c22ceSWarner Losh 			printf("can't remove iosched sysctl control loop context\n");
936ba6c22ceSWarner Losh }
937ba6c22ceSWarner Losh #endif
938ba6c22ceSWarner Losh 
939ba6c22ceSWarner Losh /*
940ba6c22ceSWarner Losh  * Allocate the iosched structure. This also insulates callers from knowing
941ba6c22ceSWarner Losh  * sizeof struct cam_iosched_softc.
942ba6c22ceSWarner Losh  */
943ba6c22ceSWarner Losh int
944ba6c22ceSWarner Losh cam_iosched_init(struct cam_iosched_softc **iscp, struct cam_periph *periph)
945ba6c22ceSWarner Losh {
946ba6c22ceSWarner Losh 
947ba6c22ceSWarner Losh 	*iscp = malloc(sizeof(**iscp), M_CAMSCHED, M_NOWAIT | M_ZERO);
948ba6c22ceSWarner Losh 	if (*iscp == NULL)
949ba6c22ceSWarner Losh 		return ENOMEM;
950ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
951ba6c22ceSWarner Losh 	if (iosched_debug)
952ba6c22ceSWarner Losh 		printf("CAM IOSCHEDULER Allocating entry at %p\n", *iscp);
953ba6c22ceSWarner Losh #endif
954ba6c22ceSWarner Losh 	(*iscp)->sort_io_queue = -1;
955ba6c22ceSWarner Losh 	bioq_init(&(*iscp)->bio_queue);
956ba6c22ceSWarner Losh 	bioq_init(&(*iscp)->trim_queue);
957ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
958ba6c22ceSWarner Losh 	if (do_netflix_iosched) {
959ba6c22ceSWarner Losh 		bioq_init(&(*iscp)->write_queue);
960ba6c22ceSWarner Losh 		(*iscp)->read_bias = 100;
961ba6c22ceSWarner Losh 		(*iscp)->current_read_bias = 100;
962ba6c22ceSWarner Losh 		(*iscp)->quanta = 200;
963ba6c22ceSWarner Losh 		cam_iosched_iop_stats_init(*iscp, &(*iscp)->read_stats);
964ba6c22ceSWarner Losh 		cam_iosched_iop_stats_init(*iscp, &(*iscp)->write_stats);
965ba6c22ceSWarner Losh 		cam_iosched_iop_stats_init(*iscp, &(*iscp)->trim_stats);
966ba6c22ceSWarner Losh 		(*iscp)->trim_stats.max = 1;	/* Trims are special: one at a time for now */
967ba6c22ceSWarner Losh 		(*iscp)->last_time = sbinuptime();
968ba6c22ceSWarner Losh 		callout_init_mtx(&(*iscp)->ticker, cam_periph_mtx(periph), 0);
969ba6c22ceSWarner Losh 		(*iscp)->periph = periph;
970ba6c22ceSWarner Losh 		cam_iosched_cl_init(&(*iscp)->cl, *iscp);
971ba6c22ceSWarner Losh 		callout_reset(&(*iscp)->ticker, hz / (*iscp)->quanta - 1, cam_iosched_ticker, *iscp);
972ba6c22ceSWarner Losh 		(*iscp)->flags |= CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
973ba6c22ceSWarner Losh 	}
974ba6c22ceSWarner Losh #endif
975ba6c22ceSWarner Losh 
976ba6c22ceSWarner Losh 	return 0;
977ba6c22ceSWarner Losh }
978ba6c22ceSWarner Losh 
979ba6c22ceSWarner Losh /*
980ba6c22ceSWarner Losh  * Reclaim all used resources. This assumes that other folks have
981ba6c22ceSWarner Losh  * drained the requests in the hardware. Maybe an unwise assumption.
982ba6c22ceSWarner Losh  */
983ba6c22ceSWarner Losh void
984ba6c22ceSWarner Losh cam_iosched_fini(struct cam_iosched_softc *isc)
985ba6c22ceSWarner Losh {
986ba6c22ceSWarner Losh 	if (isc) {
987ba6c22ceSWarner Losh 		cam_iosched_flush(isc, NULL, ENXIO);
988ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
989ba6c22ceSWarner Losh 		cam_iosched_iop_stats_fini(&isc->read_stats);
990ba6c22ceSWarner Losh 		cam_iosched_iop_stats_fini(&isc->write_stats);
991ba6c22ceSWarner Losh 		cam_iosched_iop_stats_fini(&isc->trim_stats);
992ba6c22ceSWarner Losh 		cam_iosched_cl_sysctl_fini(&isc->cl);
993ba6c22ceSWarner Losh 		if (isc->sysctl_tree)
994ba6c22ceSWarner Losh 			if (sysctl_ctx_free(&isc->sysctl_ctx) != 0)
995ba6c22ceSWarner Losh 				printf("can't remove iosched sysctl stats context\n");
996ba6c22ceSWarner Losh 		if (isc->flags & CAM_IOSCHED_FLAG_CALLOUT_ACTIVE) {
997ba6c22ceSWarner Losh 			callout_drain(&isc->ticker);
998ba6c22ceSWarner Losh 			isc->flags &= ~ CAM_IOSCHED_FLAG_CALLOUT_ACTIVE;
999ba6c22ceSWarner Losh 		}
1000ba6c22ceSWarner Losh 
1001ba6c22ceSWarner Losh #endif
1002ba6c22ceSWarner Losh 		free(isc, M_CAMSCHED);
1003ba6c22ceSWarner Losh 	}
1004ba6c22ceSWarner Losh }
1005ba6c22ceSWarner Losh 
1006ba6c22ceSWarner Losh /*
1007ba6c22ceSWarner Losh  * After we're sure we're attaching a device, go ahead and add
1008ba6c22ceSWarner Losh  * hooks for any sysctl we may wish to honor.
1009ba6c22ceSWarner Losh  */
1010ba6c22ceSWarner Losh void cam_iosched_sysctl_init(struct cam_iosched_softc *isc,
1011ba6c22ceSWarner Losh     struct sysctl_ctx_list *ctx, struct sysctl_oid *node)
1012ba6c22ceSWarner Losh {
1013ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1014ba6c22ceSWarner Losh 	struct sysctl_oid_list *n;
1015ba6c22ceSWarner Losh #endif
1016ba6c22ceSWarner Losh 
1017ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(node),
1018ba6c22ceSWarner Losh 		OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE,
1019ba6c22ceSWarner Losh 		&isc->sort_io_queue, 0,
1020ba6c22ceSWarner Losh 		"Sort IO queue to try and optimise disk access patterns");
1021ba6c22ceSWarner Losh 
1022ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1023ba6c22ceSWarner Losh 	if (!do_netflix_iosched)
1024ba6c22ceSWarner Losh 		return;
1025ba6c22ceSWarner Losh 
1026ba6c22ceSWarner Losh 	isc->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx,
1027ba6c22ceSWarner Losh 	    SYSCTL_CHILDREN(node), OID_AUTO, "iosched",
1028ba6c22ceSWarner Losh 	    CTLFLAG_RD, 0, "I/O scheduler statistics");
1029ba6c22ceSWarner Losh 	n = SYSCTL_CHILDREN(isc->sysctl_tree);
1030ba6c22ceSWarner Losh 	ctx = &isc->sysctl_ctx;
1031ba6c22ceSWarner Losh 
1032ba6c22ceSWarner Losh 	cam_iosched_iop_stats_sysctl_init(isc, &isc->read_stats, "read");
1033ba6c22ceSWarner Losh 	cam_iosched_iop_stats_sysctl_init(isc, &isc->write_stats, "write");
1034ba6c22ceSWarner Losh 	cam_iosched_iop_stats_sysctl_init(isc, &isc->trim_stats, "trim");
1035ba6c22ceSWarner Losh 	cam_iosched_cl_sysctl_init(isc);
1036ba6c22ceSWarner Losh 
1037ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1038ba6c22ceSWarner Losh 	    OID_AUTO, "read_bias", CTLFLAG_RW,
1039ba6c22ceSWarner Losh 	    &isc->read_bias, 100,
1040ba6c22ceSWarner Losh 	    "How biased towards read should we be independent of limits");
1041ba6c22ceSWarner Losh 
1042ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1043ba6c22ceSWarner Losh 	    OID_AUTO, "quanta", CTLFLAG_RW,
1044ba6c22ceSWarner Losh 	    &isc->quanta, 200,
1045ba6c22ceSWarner Losh 	    "How many quanta per second do we slice the I/O up into");
1046ba6c22ceSWarner Losh 
1047ba6c22ceSWarner Losh 	SYSCTL_ADD_INT(ctx, n,
1048ba6c22ceSWarner Losh 	    OID_AUTO, "total_ticks", CTLFLAG_RD,
1049ba6c22ceSWarner Losh 	    &isc->total_ticks, 0,
1050ba6c22ceSWarner Losh 	    "Total number of ticks we've done");
1051ba6c22ceSWarner Losh #endif
1052ba6c22ceSWarner Losh }
1053ba6c22ceSWarner Losh 
1054ba6c22ceSWarner Losh /*
1055ba6c22ceSWarner Losh  * Flush outstanding I/O. Consumers of this library don't know all the
1056ba6c22ceSWarner Losh  * queues we may keep, so this allows all I/O to be flushed in one
1057ba6c22ceSWarner Losh  * convenient call.
1058ba6c22ceSWarner Losh  */
1059ba6c22ceSWarner Losh void
1060ba6c22ceSWarner Losh cam_iosched_flush(struct cam_iosched_softc *isc, struct devstat *stp, int err)
1061ba6c22ceSWarner Losh {
1062ba6c22ceSWarner Losh 	bioq_flush(&isc->bio_queue, stp, err);
1063ba6c22ceSWarner Losh 	bioq_flush(&isc->trim_queue, stp, err);
1064ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1065ba6c22ceSWarner Losh 	if (do_netflix_iosched)
1066ba6c22ceSWarner Losh 		bioq_flush(&isc->write_queue, stp, err);
1067ba6c22ceSWarner Losh #endif
1068ba6c22ceSWarner Losh }
1069ba6c22ceSWarner Losh 
1070ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1071ba6c22ceSWarner Losh static struct bio *
1072ba6c22ceSWarner Losh cam_iosched_get_write(struct cam_iosched_softc *isc)
1073ba6c22ceSWarner Losh {
1074ba6c22ceSWarner Losh 	struct bio *bp;
1075ba6c22ceSWarner Losh 
1076ba6c22ceSWarner Losh 	/*
1077ba6c22ceSWarner Losh 	 * We control the write rate by controlling how many requests we send
1078ba6c22ceSWarner Losh 	 * down to the drive at any one time. Fewer requests limits the
1079ba6c22ceSWarner Losh 	 * effects of both starvation when the requests take a while and write
1080ba6c22ceSWarner Losh 	 * amplification when each request is causing more than one write to
1081ba6c22ceSWarner Losh 	 * the NAND media. Limiting the queue depth like this will also limit
1082ba6c22ceSWarner Losh 	 * the write throughput and give and reads that want to compete to
1083ba6c22ceSWarner Losh 	 * compete unfairly.
1084ba6c22ceSWarner Losh 	 */
1085ba6c22ceSWarner Losh 	bp = bioq_first(&isc->write_queue);
1086ba6c22ceSWarner Losh 	if (bp == NULL) {
1087ba6c22ceSWarner Losh 		if (iosched_debug > 3)
1088ba6c22ceSWarner Losh 			printf("No writes present in write_queue\n");
1089ba6c22ceSWarner Losh 		return NULL;
1090ba6c22ceSWarner Losh 	}
1091ba6c22ceSWarner Losh 
1092ba6c22ceSWarner Losh 	/*
1093ba6c22ceSWarner Losh 	 * If pending read, prefer that based on current read bias
1094ba6c22ceSWarner Losh 	 * setting.
1095ba6c22ceSWarner Losh 	 */
1096ba6c22ceSWarner Losh 	if (bioq_first(&isc->bio_queue) && isc->current_read_bias) {
1097ba6c22ceSWarner Losh 		if (iosched_debug)
1098ba6c22ceSWarner Losh 			printf("Reads present and current_read_bias is %d queued writes %d queued reads %d\n", isc->current_read_bias, isc->write_stats.queued, isc->read_stats.queued);
1099ba6c22ceSWarner Losh 		isc->current_read_bias--;
1100ba6c22ceSWarner Losh 		return NULL;
1101ba6c22ceSWarner Losh 	}
1102ba6c22ceSWarner Losh 
1103ba6c22ceSWarner Losh 	/*
1104ba6c22ceSWarner Losh 	 * See if our current limiter allows this I/O.
1105ba6c22ceSWarner Losh 	 */
1106ba6c22ceSWarner Losh 	if (cam_iosched_limiter_iop(&isc->write_stats, bp) != 0) {
1107ba6c22ceSWarner Losh 		if (iosched_debug)
1108ba6c22ceSWarner Losh 			printf("Can't write because limiter says no.\n");
1109ba6c22ceSWarner Losh 		return NULL;
1110ba6c22ceSWarner Losh 	}
1111ba6c22ceSWarner Losh 
1112ba6c22ceSWarner Losh 	/*
1113ba6c22ceSWarner Losh 	 * Let's do this: We've passed all the gates and we're a go
1114ba6c22ceSWarner Losh 	 * to schedule the I/O in the SIM.
1115ba6c22ceSWarner Losh 	 */
1116ba6c22ceSWarner Losh 	isc->current_read_bias = isc->read_bias;
1117ba6c22ceSWarner Losh 	bioq_remove(&isc->write_queue, bp);
1118ba6c22ceSWarner Losh 	if (bp->bio_cmd == BIO_WRITE) {
1119ba6c22ceSWarner Losh 		isc->write_stats.queued--;
1120ba6c22ceSWarner Losh 		isc->write_stats.total++;
1121ba6c22ceSWarner Losh 		isc->write_stats.pending++;
1122ba6c22ceSWarner Losh 	}
1123ba6c22ceSWarner Losh 	if (iosched_debug > 9)
1124ba6c22ceSWarner Losh 		printf("HWQ : %p %#x\n", bp, bp->bio_cmd);
1125ba6c22ceSWarner Losh 	return bp;
1126ba6c22ceSWarner Losh }
1127ba6c22ceSWarner Losh #endif
1128ba6c22ceSWarner Losh 
1129ba6c22ceSWarner Losh /*
1130ba6c22ceSWarner Losh  * Put back a trim that you weren't able to actually schedule this time.
1131ba6c22ceSWarner Losh  */
1132ba6c22ceSWarner Losh void
1133ba6c22ceSWarner Losh cam_iosched_put_back_trim(struct cam_iosched_softc *isc, struct bio *bp)
1134ba6c22ceSWarner Losh {
1135ba6c22ceSWarner Losh 	bioq_insert_head(&isc->trim_queue, bp);
1136ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1137ba6c22ceSWarner Losh 	isc->trim_stats.queued++;
1138ba6c22ceSWarner Losh 	isc->trim_stats.total--;		/* since we put it back, don't double count */
1139ba6c22ceSWarner Losh 	isc->trim_stats.pending--;
1140ba6c22ceSWarner Losh #endif
1141ba6c22ceSWarner Losh }
1142ba6c22ceSWarner Losh 
1143ba6c22ceSWarner Losh /*
1144ba6c22ceSWarner Losh  * gets the next trim from the trim queue.
1145ba6c22ceSWarner Losh  *
1146ba6c22ceSWarner Losh  * Assumes we're called with the periph lock held.  It removes this
1147ba6c22ceSWarner Losh  * trim from the queue and the device must explicitly reinstert it
1148ba6c22ceSWarner Losh  * should the need arise.
1149ba6c22ceSWarner Losh  */
1150ba6c22ceSWarner Losh struct bio *
1151ba6c22ceSWarner Losh cam_iosched_next_trim(struct cam_iosched_softc *isc)
1152ba6c22ceSWarner Losh {
1153ba6c22ceSWarner Losh 	struct bio *bp;
1154ba6c22ceSWarner Losh 
1155ba6c22ceSWarner Losh 	bp  = bioq_first(&isc->trim_queue);
1156ba6c22ceSWarner Losh 	if (bp == NULL)
1157ba6c22ceSWarner Losh 		return NULL;
1158ba6c22ceSWarner Losh 	bioq_remove(&isc->trim_queue, bp);
1159ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1160ba6c22ceSWarner Losh 	isc->trim_stats.queued--;
1161ba6c22ceSWarner Losh 	isc->trim_stats.total++;
1162ba6c22ceSWarner Losh 	isc->trim_stats.pending++;
1163ba6c22ceSWarner Losh #endif
1164ba6c22ceSWarner Losh 	return bp;
1165ba6c22ceSWarner Losh }
1166ba6c22ceSWarner Losh 
1167ba6c22ceSWarner Losh /*
1168ba6c22ceSWarner Losh  * gets the an available trim from the trim queue, if there's no trim
1169ba6c22ceSWarner Losh  * already pending. It removes this trim from the queue and the device
1170ba6c22ceSWarner Losh  * must explicitly reinstert it should the need arise.
1171ba6c22ceSWarner Losh  *
1172ba6c22ceSWarner Losh  * Assumes we're called with the periph lock held.
1173ba6c22ceSWarner Losh  */
1174ba6c22ceSWarner Losh struct bio *
1175ba6c22ceSWarner Losh cam_iosched_get_trim(struct cam_iosched_softc *isc)
1176ba6c22ceSWarner Losh {
1177ba6c22ceSWarner Losh 
1178ba6c22ceSWarner Losh 	if (!cam_iosched_has_more_trim(isc))
1179ba6c22ceSWarner Losh 		return NULL;
1180ba6c22ceSWarner Losh 
1181ba6c22ceSWarner Losh 	return cam_iosched_next_trim(isc);
1182ba6c22ceSWarner Losh }
1183ba6c22ceSWarner Losh 
1184ba6c22ceSWarner Losh /*
1185ba6c22ceSWarner Losh  * Determine what the next bit of work to do is for the periph. The
1186ba6c22ceSWarner Losh  * default implementation looks to see if we have trims to do, but no
1187ba6c22ceSWarner Losh  * trims outstanding. If so, we do that. Otherwise we see if we have
1188ba6c22ceSWarner Losh  * other work. If we do, then we do that. Otherwise why were we called?
1189ba6c22ceSWarner Losh  */
1190ba6c22ceSWarner Losh struct bio *
1191ba6c22ceSWarner Losh cam_iosched_next_bio(struct cam_iosched_softc *isc)
1192ba6c22ceSWarner Losh {
1193ba6c22ceSWarner Losh 	struct bio *bp;
1194ba6c22ceSWarner Losh 
1195ba6c22ceSWarner Losh 	/*
1196ba6c22ceSWarner Losh 	 * See if we have a trim that can be scheduled. We can only send one
1197ba6c22ceSWarner Losh 	 * at a time down, so this takes that into account.
1198ba6c22ceSWarner Losh 	 *
1199ba6c22ceSWarner Losh 	 * XXX newer TRIM commands are queueable. Revisit this when we
1200ba6c22ceSWarner Losh 	 * implement them.
1201ba6c22ceSWarner Losh 	 */
1202ba6c22ceSWarner Losh 	if ((bp = cam_iosched_get_trim(isc)) != NULL)
1203ba6c22ceSWarner Losh 		return bp;
1204ba6c22ceSWarner Losh 
1205ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1206ba6c22ceSWarner Losh 	/*
1207ba6c22ceSWarner Losh 	 * See if we have any pending writes, and room in the queue for them,
1208ba6c22ceSWarner Losh 	 * and if so, those are next.
1209ba6c22ceSWarner Losh 	 */
1210ba6c22ceSWarner Losh 	if (do_netflix_iosched) {
1211ba6c22ceSWarner Losh 		if ((bp = cam_iosched_get_write(isc)) != NULL)
1212ba6c22ceSWarner Losh 			return bp;
1213ba6c22ceSWarner Losh 	}
1214ba6c22ceSWarner Losh #endif
1215ba6c22ceSWarner Losh 
1216ba6c22ceSWarner Losh 	/*
1217ba6c22ceSWarner Losh 	 * next, see if there's other, normal I/O waiting. If so return that.
1218ba6c22ceSWarner Losh 	 */
1219ba6c22ceSWarner Losh 	if ((bp = bioq_first(&isc->bio_queue)) == NULL)
1220ba6c22ceSWarner Losh 		return NULL;
1221ba6c22ceSWarner Losh 
1222ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1223ba6c22ceSWarner Losh 	/*
1224ba6c22ceSWarner Losh 	 * For the netflix scheduler, bio_queue is only for reads, so enforce
1225ba6c22ceSWarner Losh 	 * the limits here. Enforce only for reads.
1226ba6c22ceSWarner Losh 	 */
1227ba6c22ceSWarner Losh 	if (do_netflix_iosched) {
1228ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_READ &&
1229ba6c22ceSWarner Losh 		    cam_iosched_limiter_iop(&isc->read_stats, bp) != 0)
1230ba6c22ceSWarner Losh 			return NULL;
1231ba6c22ceSWarner Losh 	}
1232ba6c22ceSWarner Losh #endif
1233ba6c22ceSWarner Losh 	bioq_remove(&isc->bio_queue, bp);
1234ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1235ba6c22ceSWarner Losh 	if (do_netflix_iosched) {
1236ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_READ) {
1237ba6c22ceSWarner Losh 			isc->read_stats.queued--;
1238ba6c22ceSWarner Losh 			isc->read_stats.total++;
1239ba6c22ceSWarner Losh 			isc->read_stats.pending++;
1240ba6c22ceSWarner Losh 		} else
1241ba6c22ceSWarner Losh 			printf("Found bio_cmd = %#x\n", bp->bio_cmd);
1242ba6c22ceSWarner Losh 	}
1243ba6c22ceSWarner Losh 	if (iosched_debug > 9)
1244ba6c22ceSWarner Losh 		printf("HWQ : %p %#x\n", bp, bp->bio_cmd);
1245ba6c22ceSWarner Losh #endif
1246ba6c22ceSWarner Losh 	return bp;
1247ba6c22ceSWarner Losh }
1248ba6c22ceSWarner Losh 
1249ba6c22ceSWarner Losh /*
1250ba6c22ceSWarner Losh  * Driver has been given some work to do by the block layer. Tell the
1251ba6c22ceSWarner Losh  * scheduler about it and have it queue the work up. The scheduler module
1252ba6c22ceSWarner Losh  * will then return the currently most useful bit of work later, possibly
1253ba6c22ceSWarner Losh  * deferring work for various reasons.
1254ba6c22ceSWarner Losh  */
1255ba6c22ceSWarner Losh void
1256ba6c22ceSWarner Losh cam_iosched_queue_work(struct cam_iosched_softc *isc, struct bio *bp)
1257ba6c22ceSWarner Losh {
1258ba6c22ceSWarner Losh 
1259ba6c22ceSWarner Losh 	/*
1260ba6c22ceSWarner Losh 	 * Put all trims on the trim queue sorted, since we know
1261ba6c22ceSWarner Losh 	 * that the collapsing code requires this. Otherwise put
1262ba6c22ceSWarner Losh 	 * the work on the bio queue.
1263ba6c22ceSWarner Losh 	 */
1264ba6c22ceSWarner Losh 	if (bp->bio_cmd == BIO_DELETE) {
1265ba6c22ceSWarner Losh 		bioq_disksort(&isc->trim_queue, bp);
1266ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1267ba6c22ceSWarner Losh 		isc->trim_stats.in++;
1268ba6c22ceSWarner Losh 		isc->trim_stats.queued++;
1269ba6c22ceSWarner Losh #endif
1270ba6c22ceSWarner Losh 	}
1271ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1272ba6c22ceSWarner Losh 	else if (do_netflix_iosched &&
1273ba6c22ceSWarner Losh 	    (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_FLUSH)) {
1274ba6c22ceSWarner Losh 		if (cam_iosched_sort_queue(isc))
1275ba6c22ceSWarner Losh 			bioq_disksort(&isc->write_queue, bp);
1276ba6c22ceSWarner Losh 		else
1277ba6c22ceSWarner Losh 			bioq_insert_tail(&isc->write_queue, bp);
1278ba6c22ceSWarner Losh 		if (iosched_debug > 9)
1279ba6c22ceSWarner Losh 			printf("Qw  : %p %#x\n", bp, bp->bio_cmd);
1280ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_WRITE) {
1281ba6c22ceSWarner Losh 			isc->write_stats.in++;
1282ba6c22ceSWarner Losh 			isc->write_stats.queued++;
1283ba6c22ceSWarner Losh 		}
1284ba6c22ceSWarner Losh 	}
1285ba6c22ceSWarner Losh #endif
1286ba6c22ceSWarner Losh 	else {
1287ba6c22ceSWarner Losh 		if (cam_iosched_sort_queue(isc))
1288ba6c22ceSWarner Losh 			bioq_disksort(&isc->bio_queue, bp);
1289ba6c22ceSWarner Losh 		else
1290ba6c22ceSWarner Losh 			bioq_insert_tail(&isc->bio_queue, bp);
1291ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1292ba6c22ceSWarner Losh 		if (iosched_debug > 9)
1293ba6c22ceSWarner Losh 			printf("Qr  : %p %#x\n", bp, bp->bio_cmd);
1294ba6c22ceSWarner Losh 		if (bp->bio_cmd == BIO_READ) {
1295ba6c22ceSWarner Losh 			isc->read_stats.in++;
1296ba6c22ceSWarner Losh 			isc->read_stats.queued++;
1297ba6c22ceSWarner Losh 		} else if (bp->bio_cmd == BIO_WRITE) {
1298ba6c22ceSWarner Losh 			isc->write_stats.in++;
1299ba6c22ceSWarner Losh 			isc->write_stats.queued++;
1300ba6c22ceSWarner Losh 		}
1301ba6c22ceSWarner Losh #endif
1302ba6c22ceSWarner Losh 	}
1303ba6c22ceSWarner Losh }
1304ba6c22ceSWarner Losh 
1305ba6c22ceSWarner Losh /*
1306ba6c22ceSWarner Losh  * If we have work, get it scheduled. Called with the periph lock held.
1307ba6c22ceSWarner Losh  */
1308ba6c22ceSWarner Losh void
1309ba6c22ceSWarner Losh cam_iosched_schedule(struct cam_iosched_softc *isc, struct cam_periph *periph)
1310ba6c22ceSWarner Losh {
1311ba6c22ceSWarner Losh 
1312ba6c22ceSWarner Losh 	if (cam_iosched_has_work(isc))
1313ba6c22ceSWarner Losh 		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1314ba6c22ceSWarner Losh }
1315ba6c22ceSWarner Losh 
1316ba6c22ceSWarner Losh /*
1317ba6c22ceSWarner Losh  * Complete a trim request
1318ba6c22ceSWarner Losh  */
1319ba6c22ceSWarner Losh void
1320ba6c22ceSWarner Losh cam_iosched_trim_done(struct cam_iosched_softc *isc)
1321ba6c22ceSWarner Losh {
1322ba6c22ceSWarner Losh 
1323ba6c22ceSWarner Losh 	isc->flags &= ~CAM_IOSCHED_FLAG_TRIM_ACTIVE;
1324ba6c22ceSWarner Losh }
1325ba6c22ceSWarner Losh 
1326ba6c22ceSWarner Losh /*
1327ba6c22ceSWarner Losh  * Complete a bio. Called before we release the ccb with xpt_release_ccb so we
1328ba6c22ceSWarner Losh  * might use notes in the ccb for statistics.
1329ba6c22ceSWarner Losh  */
1330ba6c22ceSWarner Losh int
1331ba6c22ceSWarner Losh cam_iosched_bio_complete(struct cam_iosched_softc *isc, struct bio *bp,
1332ba6c22ceSWarner Losh     union ccb *done_ccb)
1333ba6c22ceSWarner Losh {
1334ba6c22ceSWarner Losh 	int retval = 0;
1335ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1336ba6c22ceSWarner Losh 	if (!do_netflix_iosched)
1337ba6c22ceSWarner Losh 		return retval;
1338ba6c22ceSWarner Losh 
1339ba6c22ceSWarner Losh 	if (iosched_debug > 10)
1340ba6c22ceSWarner Losh 		printf("done: %p %#x\n", bp, bp->bio_cmd);
1341ba6c22ceSWarner Losh 	if (bp->bio_cmd == BIO_WRITE) {
1342ba6c22ceSWarner Losh 		retval = cam_iosched_limiter_iodone(&isc->write_stats, bp);
1343ba6c22ceSWarner Losh 		isc->write_stats.out++;
1344ba6c22ceSWarner Losh 		isc->write_stats.pending--;
1345ba6c22ceSWarner Losh 	} else if (bp->bio_cmd == BIO_READ) {
1346ba6c22ceSWarner Losh 		retval = cam_iosched_limiter_iodone(&isc->read_stats, bp);
1347ba6c22ceSWarner Losh 		isc->read_stats.out++;
1348ba6c22ceSWarner Losh 		isc->read_stats.pending--;
1349ba6c22ceSWarner Losh 	} else if (bp->bio_cmd == BIO_DELETE) {
1350ba6c22ceSWarner Losh 		isc->trim_stats.out++;
1351ba6c22ceSWarner Losh 		isc->trim_stats.pending--;
1352ba6c22ceSWarner Losh 	} else if (bp->bio_cmd != BIO_FLUSH) {
1353ba6c22ceSWarner Losh 		if (iosched_debug)
1354ba6c22ceSWarner Losh 			printf("Completing command with bio_cmd == %#x\n", bp->bio_cmd);
1355ba6c22ceSWarner Losh 	}
1356ba6c22ceSWarner Losh 
1357ba6c22ceSWarner Losh 	if (!(bp->bio_flags & BIO_ERROR))
1358ba6c22ceSWarner Losh 		cam_iosched_io_metric_update(isc, done_ccb->ccb_h.qos.sim_data,
1359ba6c22ceSWarner Losh 		    bp->bio_cmd, bp->bio_bcount);
1360ba6c22ceSWarner Losh #endif
1361ba6c22ceSWarner Losh 	return retval;
1362ba6c22ceSWarner Losh }
1363ba6c22ceSWarner Losh 
1364ba6c22ceSWarner Losh /*
1365ba6c22ceSWarner Losh  * Tell the io scheduler that you've pushed a trim down into the sim.
1366ba6c22ceSWarner Losh  * xxx better place for this?
1367ba6c22ceSWarner Losh  */
1368ba6c22ceSWarner Losh void
1369ba6c22ceSWarner Losh cam_iosched_submit_trim(struct cam_iosched_softc *isc)
1370ba6c22ceSWarner Losh {
1371ba6c22ceSWarner Losh 
1372ba6c22ceSWarner Losh 	isc->flags |= CAM_IOSCHED_FLAG_TRIM_ACTIVE;
1373ba6c22ceSWarner Losh }
1374ba6c22ceSWarner Losh 
1375ba6c22ceSWarner Losh /*
1376ba6c22ceSWarner Losh  * Change the sorting policy hint for I/O transactions for this device.
1377ba6c22ceSWarner Losh  */
1378ba6c22ceSWarner Losh void
1379ba6c22ceSWarner Losh cam_iosched_set_sort_queue(struct cam_iosched_softc *isc, int val)
1380ba6c22ceSWarner Losh {
1381ba6c22ceSWarner Losh 
1382ba6c22ceSWarner Losh 	isc->sort_io_queue = val;
1383ba6c22ceSWarner Losh }
1384ba6c22ceSWarner Losh 
1385ba6c22ceSWarner Losh int
1386ba6c22ceSWarner Losh cam_iosched_has_work_flags(struct cam_iosched_softc *isc, uint32_t flags)
1387ba6c22ceSWarner Losh {
1388ba6c22ceSWarner Losh 	return isc->flags & flags;
1389ba6c22ceSWarner Losh }
1390ba6c22ceSWarner Losh 
1391ba6c22ceSWarner Losh void
1392ba6c22ceSWarner Losh cam_iosched_set_work_flags(struct cam_iosched_softc *isc, uint32_t flags)
1393ba6c22ceSWarner Losh {
1394ba6c22ceSWarner Losh 	isc->flags |= flags;
1395ba6c22ceSWarner Losh }
1396ba6c22ceSWarner Losh 
1397ba6c22ceSWarner Losh void
1398ba6c22ceSWarner Losh cam_iosched_clr_work_flags(struct cam_iosched_softc *isc, uint32_t flags)
1399ba6c22ceSWarner Losh {
1400ba6c22ceSWarner Losh 	isc->flags &= ~flags;
1401ba6c22ceSWarner Losh }
1402ba6c22ceSWarner Losh 
1403ba6c22ceSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1404ba6c22ceSWarner Losh /*
1405ba6c22ceSWarner Losh  * After the method presented in Jack Crenshaw's 1998 article "Integer
1406ba6c22ceSWarner Losh  * Suqare Roots," reprinted at
1407ba6c22ceSWarner Losh  * http://www.embedded.com/electronics-blogs/programmer-s-toolbox/4219659/Integer-Square-Roots
1408ba6c22ceSWarner Losh  * and well worth the read. Briefly, we find the power of 4 that's the
1409ba6c22ceSWarner Losh  * largest smaller than val. We then check each smaller power of 4 to
1410ba6c22ceSWarner Losh  * see if val is still bigger. The right shifts at each step divide
1411ba6c22ceSWarner Losh  * the result by 2 which after successive application winds up
1412ba6c22ceSWarner Losh  * accumulating the right answer. It could also have been accumulated
1413ba6c22ceSWarner Losh  * using a separate root counter, but this code is smaller and faster
1414ba6c22ceSWarner Losh  * than that method. This method is also integer size invariant.
1415ba6c22ceSWarner Losh  * It returns floor(sqrt((float)val)), or the larget integer less than
1416ba6c22ceSWarner Losh  * or equal to the square root.
1417ba6c22ceSWarner Losh  */
1418ba6c22ceSWarner Losh static uint64_t
1419ba6c22ceSWarner Losh isqrt64(uint64_t val)
1420ba6c22ceSWarner Losh {
1421ba6c22ceSWarner Losh 	uint64_t res = 0;
1422ba6c22ceSWarner Losh 	uint64_t bit = 1ULL << (sizeof(uint64_t) * NBBY - 2);
1423ba6c22ceSWarner Losh 
1424ba6c22ceSWarner Losh 	/*
1425ba6c22ceSWarner Losh 	 * Find the largest power of 4 smaller than val.
1426ba6c22ceSWarner Losh 	 */
1427ba6c22ceSWarner Losh 	while (bit > val)
1428ba6c22ceSWarner Losh 		bit >>= 2;
1429ba6c22ceSWarner Losh 
1430ba6c22ceSWarner Losh 	/*
1431ba6c22ceSWarner Losh 	 * Accumulate the answer, one bit at a time (we keep moving
1432ba6c22ceSWarner Losh 	 * them over since 2 is the square root of 4 and we test
1433ba6c22ceSWarner Losh 	 * powers of 4). We accumulate where we find the bit, but
1434ba6c22ceSWarner Losh 	 * the successive shifts land the bit in the right place
1435ba6c22ceSWarner Losh 	 * by the end.
1436ba6c22ceSWarner Losh 	 */
1437ba6c22ceSWarner Losh 	while (bit != 0) {
1438ba6c22ceSWarner Losh 		if (val >= res + bit) {
1439ba6c22ceSWarner Losh 			val -= res + bit;
1440ba6c22ceSWarner Losh 			res = (res >> 1) + bit;
1441ba6c22ceSWarner Losh 		} else
1442ba6c22ceSWarner Losh 			res >>= 1;
1443ba6c22ceSWarner Losh 		bit >>= 2;
1444ba6c22ceSWarner Losh 	}
1445ba6c22ceSWarner Losh 
1446ba6c22ceSWarner Losh 	return res;
1447ba6c22ceSWarner Losh }
1448ba6c22ceSWarner Losh 
1449ba6c22ceSWarner Losh /*
1450ba6c22ceSWarner Losh  * a and b are 32.32 fixed point stored in a 64-bit word.
1451ba6c22ceSWarner Losh  * Let al and bl be the .32 part of a and b.
1452ba6c22ceSWarner Losh  * Let ah and bh be the 32 part of a and b.
1453ba6c22ceSWarner Losh  * R is the radix and is 1 << 32
1454ba6c22ceSWarner Losh  *
1455ba6c22ceSWarner Losh  * a * b
1456ba6c22ceSWarner Losh  * (ah + al / R) * (bh + bl / R)
1457ba6c22ceSWarner Losh  * ah * bh + (al * bh + ah * bl) / R + al * bl / R^2
1458ba6c22ceSWarner Losh  *
1459ba6c22ceSWarner Losh  * After multiplicaiton, we have to renormalize by multiply by
1460ba6c22ceSWarner Losh  * R, so we wind up with
1461ba6c22ceSWarner Losh  *	ah * bh * R + al * bh + ah * bl + al * bl / R
1462ba6c22ceSWarner Losh  * which turns out to be a very nice way to compute this value
1463ba6c22ceSWarner Losh  * so long as ah and bh are < 65536 there's no loss of high bits
1464ba6c22ceSWarner Losh  * and the low order bits are below the threshold of caring for
1465ba6c22ceSWarner Losh  * this application.
1466ba6c22ceSWarner Losh  */
1467ba6c22ceSWarner Losh static uint64_t
1468ba6c22ceSWarner Losh mul(uint64_t a, uint64_t b)
1469ba6c22ceSWarner Losh {
1470ba6c22ceSWarner Losh 	uint64_t al, ah, bl, bh;
1471ba6c22ceSWarner Losh 	al = a & 0xffffffff;
1472ba6c22ceSWarner Losh 	ah = a >> 32;
1473ba6c22ceSWarner Losh 	bl = b & 0xffffffff;
1474ba6c22ceSWarner Losh 	bh = b >> 32;
1475ba6c22ceSWarner Losh 	return ((ah * bh) << 32) + al * bh + ah * bl + ((al * bl) >> 32);
1476ba6c22ceSWarner Losh }
1477ba6c22ceSWarner Losh 
1478ba6c22ceSWarner Losh static void
1479ba6c22ceSWarner Losh cam_iosched_update(struct iop_stats *iop, sbintime_t sim_latency)
1480ba6c22ceSWarner Losh {
1481ba6c22ceSWarner Losh 	sbintime_t y, yy;
1482ba6c22ceSWarner Losh 	uint64_t var;
1483ba6c22ceSWarner Losh 
1484ba6c22ceSWarner Losh 	/*
1485ba6c22ceSWarner Losh 	 * Classic expoentially decaying average with a tiny alpha
1486ba6c22ceSWarner Losh 	 * (2 ^ -alpha_bits). For more info see the NIST statistical
1487ba6c22ceSWarner Losh 	 * handbook.
1488ba6c22ceSWarner Losh 	 *
1489ba6c22ceSWarner Losh 	 * ema_t = y_t * alpha + ema_t-1 * (1 - alpha)
1490ba6c22ceSWarner Losh 	 * alpha = 1 / (1 << alpha_bits)
1491ba6c22ceSWarner Losh 	 *
1492ba6c22ceSWarner Losh 	 * Since alpha is a power of two, we can compute this w/o any mult or
1493ba6c22ceSWarner Losh 	 * division.
1494ba6c22ceSWarner Losh 	 */
1495ba6c22ceSWarner Losh 	y = sim_latency;
1496ba6c22ceSWarner Losh 	iop->ema = (y + (iop->ema << alpha_bits) - iop->ema) >> alpha_bits;
1497ba6c22ceSWarner Losh 
1498ba6c22ceSWarner Losh 	yy = mul(y, y);
1499ba6c22ceSWarner Losh 	iop->emss = (yy + (iop->emss << alpha_bits) - iop->emss) >> alpha_bits;
1500ba6c22ceSWarner Losh 
1501ba6c22ceSWarner Losh 	/*
1502ba6c22ceSWarner Losh          * s_1 = sum of data
1503ba6c22ceSWarner Losh 	 * s_2 = sum of data * data
1504ba6c22ceSWarner Losh 	 * ema ~ mean (or s_1 / N)
1505ba6c22ceSWarner Losh 	 * emss ~ s_2 / N
1506ba6c22ceSWarner Losh 	 *
1507ba6c22ceSWarner Losh 	 * sd = sqrt((N * s_2 - s_1 ^ 2) / (N * (N - 1)))
1508ba6c22ceSWarner Losh 	 * sd = sqrt((N * s_2 / N * (N - 1)) - (s_1 ^ 2 / (N * (N - 1))))
1509ba6c22ceSWarner Losh 	 *
1510ba6c22ceSWarner Losh 	 * N ~ 2 / alpha - 1
1511ba6c22ceSWarner Losh 	 * alpha < 1 / 16 (typically much less)
1512ba6c22ceSWarner Losh 	 * N > 31 --> N large so N * (N - 1) is approx N * N
1513ba6c22ceSWarner Losh 	 *
1514ba6c22ceSWarner Losh 	 * substituting and rearranging:
1515ba6c22ceSWarner Losh 	 * sd ~ sqrt(s_2 / N - (s_1 / N) ^ 2)
1516ba6c22ceSWarner Losh 	 *    ~ sqrt(emss - ema ^ 2);
1517ba6c22ceSWarner Losh 	 * which is the formula used here to get a decent estimate of sd which
1518ba6c22ceSWarner Losh 	 * we use to detect outliers. Note that when first starting up, it
1519ba6c22ceSWarner Losh 	 * takes a while for emss sum of squares estimator to converge on a
1520ba6c22ceSWarner Losh 	 * good value.  during this time, it can be less than ema^2. We
1521ba6c22ceSWarner Losh 	 * compute a sd of 0 in that case, and ignore outliers.
1522ba6c22ceSWarner Losh 	 */
1523ba6c22ceSWarner Losh 	var = iop->emss - mul(iop->ema, iop->ema);
1524ba6c22ceSWarner Losh 	iop->sd = (int64_t)var < 0 ? 0 : isqrt64(var);
1525ba6c22ceSWarner Losh }
1526ba6c22ceSWarner Losh 
15275ede5b8cSWarner Losh #ifdef CAM_NETFLIX_IOSCHED
1528ba6c22ceSWarner Losh static void
1529ba6c22ceSWarner Losh cam_iosched_io_metric_update(struct cam_iosched_softc *isc,
1530ba6c22ceSWarner Losh     sbintime_t sim_latency, int cmd, size_t size)
1531ba6c22ceSWarner Losh {
1532ba6c22ceSWarner Losh 	/* xxx Do we need to scale based on the size of the I/O ? */
1533ba6c22ceSWarner Losh 	switch (cmd) {
1534ba6c22ceSWarner Losh 	case BIO_READ:
1535ba6c22ceSWarner Losh 		cam_iosched_update(&isc->read_stats, sim_latency);
1536ba6c22ceSWarner Losh 		break;
1537ba6c22ceSWarner Losh 	case BIO_WRITE:
1538ba6c22ceSWarner Losh 		cam_iosched_update(&isc->write_stats, sim_latency);
1539ba6c22ceSWarner Losh 		break;
1540ba6c22ceSWarner Losh 	case BIO_DELETE:
1541ba6c22ceSWarner Losh 		cam_iosched_update(&isc->trim_stats, sim_latency);
1542ba6c22ceSWarner Losh 		break;
1543ba6c22ceSWarner Losh 	default:
1544ba6c22ceSWarner Losh 		break;
1545ba6c22ceSWarner Losh 	}
1546ba6c22ceSWarner Losh }
15475ede5b8cSWarner Losh #endif
1548ba6c22ceSWarner Losh 
1549ba6c22ceSWarner Losh #ifdef DDB
1550ba6c22ceSWarner Losh static int biolen(struct bio_queue_head *bq)
1551ba6c22ceSWarner Losh {
1552ba6c22ceSWarner Losh 	int i = 0;
1553ba6c22ceSWarner Losh 	struct bio *bp;
1554ba6c22ceSWarner Losh 
1555ba6c22ceSWarner Losh 	TAILQ_FOREACH(bp, &bq->queue, bio_queue) {
1556ba6c22ceSWarner Losh 		i++;
1557ba6c22ceSWarner Losh 	}
1558ba6c22ceSWarner Losh 	return i;
1559ba6c22ceSWarner Losh }
1560ba6c22ceSWarner Losh 
1561ba6c22ceSWarner Losh /*
1562ba6c22ceSWarner Losh  * Show the internal state of the I/O scheduler.
1563ba6c22ceSWarner Losh  */
1564ba6c22ceSWarner Losh DB_SHOW_COMMAND(iosched, cam_iosched_db_show)
1565ba6c22ceSWarner Losh {
1566ba6c22ceSWarner Losh 	struct cam_iosched_softc *isc;
1567ba6c22ceSWarner Losh 
1568ba6c22ceSWarner Losh 	if (!have_addr) {
1569ba6c22ceSWarner Losh 		db_printf("Need addr\n");
1570ba6c22ceSWarner Losh 		return;
1571ba6c22ceSWarner Losh 	}
1572ba6c22ceSWarner Losh 	isc = (struct cam_iosched_softc *)addr;
1573ba6c22ceSWarner Losh 	db_printf("pending_reads:     %d\n", isc->read_stats.pending);
1574ba6c22ceSWarner Losh 	db_printf("min_reads:         %d\n", isc->read_stats.min);
1575ba6c22ceSWarner Losh 	db_printf("max_reads:         %d\n", isc->read_stats.max);
1576ba6c22ceSWarner Losh 	db_printf("reads:             %d\n", isc->read_stats.total);
1577ba6c22ceSWarner Losh 	db_printf("in_reads:          %d\n", isc->read_stats.in);
1578ba6c22ceSWarner Losh 	db_printf("out_reads:         %d\n", isc->read_stats.out);
1579ba6c22ceSWarner Losh 	db_printf("queued_reads:      %d\n", isc->read_stats.queued);
1580ba6c22ceSWarner Losh 	db_printf("Current Q len      %d\n", biolen(&isc->bio_queue));
1581ba6c22ceSWarner Losh 	db_printf("pending_writes:    %d\n", isc->write_stats.pending);
1582ba6c22ceSWarner Losh 	db_printf("min_writes:        %d\n", isc->write_stats.min);
1583ba6c22ceSWarner Losh 	db_printf("max_writes:        %d\n", isc->write_stats.max);
1584ba6c22ceSWarner Losh 	db_printf("writes:            %d\n", isc->write_stats.total);
1585ba6c22ceSWarner Losh 	db_printf("in_writes:         %d\n", isc->write_stats.in);
1586ba6c22ceSWarner Losh 	db_printf("out_writes:        %d\n", isc->write_stats.out);
1587ba6c22ceSWarner Losh 	db_printf("queued_writes:     %d\n", isc->write_stats.queued);
1588ba6c22ceSWarner Losh 	db_printf("Current Q len      %d\n", biolen(&isc->write_queue));
1589ba6c22ceSWarner Losh 	db_printf("pending_trims:     %d\n", isc->trim_stats.pending);
1590ba6c22ceSWarner Losh 	db_printf("min_trims:         %d\n", isc->trim_stats.min);
1591ba6c22ceSWarner Losh 	db_printf("max_trims:         %d\n", isc->trim_stats.max);
1592ba6c22ceSWarner Losh 	db_printf("trims:             %d\n", isc->trim_stats.total);
1593ba6c22ceSWarner Losh 	db_printf("in_trims:          %d\n", isc->trim_stats.in);
1594ba6c22ceSWarner Losh 	db_printf("out_trims:         %d\n", isc->trim_stats.out);
1595ba6c22ceSWarner Losh 	db_printf("queued_trims:      %d\n", isc->trim_stats.queued);
1596ba6c22ceSWarner Losh 	db_printf("Current Q len      %d\n", biolen(&isc->trim_queue));
1597ba6c22ceSWarner Losh 	db_printf("read_bias:         %d\n", isc->read_bias);
1598ba6c22ceSWarner Losh 	db_printf("current_read_bias: %d\n", isc->current_read_bias);
1599ba6c22ceSWarner Losh 	db_printf("Trim active?       %s\n",
1600ba6c22ceSWarner Losh 	    (isc->flags & CAM_IOSCHED_FLAG_TRIM_ACTIVE) ? "yes" : "no");
1601ba6c22ceSWarner Losh }
1602ba6c22ceSWarner Losh #endif
1603ba6c22ceSWarner Losh #endif
1604