1ba6c22ceSWarner Losh /*- 2ba6c22ceSWarner Losh * CAM IO Scheduler Interface 3ba6c22ceSWarner Losh * 4f24882ecSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 12ba6c22ceSWarner Losh * notice, this list of conditions, and the following disclaimer, 13ba6c22ceSWarner Losh * without modification, immediately at the beginning of the file. 14ba6c22ceSWarner Losh * 2. The name of the author may not be used to endorse or promote products 15ba6c22ceSWarner Losh * derived from this software without specific prior written permission. 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 20ba6c22ceSWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21ba6c22ceSWarner Losh * 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 * $FreeBSD$ 30ba6c22ceSWarner Losh */ 31ba6c22ceSWarner Losh 32ba6c22ceSWarner Losh #include "opt_cam.h" 33ba6c22ceSWarner Losh #include "opt_ddb.h" 34ba6c22ceSWarner Losh 35ba6c22ceSWarner Losh #include <sys/cdefs.h> 36ba6c22ceSWarner Losh __FBSDID("$FreeBSD$"); 37ba6c22ceSWarner Losh 38ba6c22ceSWarner Losh #include <sys/param.h> 39ba6c22ceSWarner Losh 40ba6c22ceSWarner Losh #include <sys/systm.h> 41ba6c22ceSWarner Losh #include <sys/kernel.h> 42ba6c22ceSWarner Losh #include <sys/bio.h> 43ba6c22ceSWarner Losh #include <sys/lock.h> 44ba6c22ceSWarner Losh #include <sys/malloc.h> 45ba6c22ceSWarner Losh #include <sys/mutex.h> 46cf3ec151SWarner Losh #include <sys/sbuf.h> 47ba6c22ceSWarner Losh #include <sys/sysctl.h> 48ba6c22ceSWarner Losh 49ba6c22ceSWarner Losh #include <cam/cam.h> 50ba6c22ceSWarner Losh #include <cam/cam_ccb.h> 51ba6c22ceSWarner Losh #include <cam/cam_periph.h> 52ba6c22ceSWarner Losh #include <cam/cam_xpt_periph.h> 53cf3ec151SWarner Losh #include <cam/cam_xpt_internal.h> 54ba6c22ceSWarner Losh #include <cam/cam_iosched.h> 55ba6c22ceSWarner Losh 56ba6c22ceSWarner Losh #include <ddb/ddb.h> 57ba6c22ceSWarner Losh 58ba6c22ceSWarner Losh static MALLOC_DEFINE(M_CAMSCHED, "CAM I/O Scheduler", 59ba6c22ceSWarner Losh "CAM I/O Scheduler buffers"); 60ba6c22ceSWarner Losh 61ba6c22ceSWarner Losh /* 62ba6c22ceSWarner Losh * Default I/O scheduler for FreeBSD. This implementation is just a thin-vineer 63ba6c22ceSWarner Losh * over the bioq_* interface, with notions of separate calls for normal I/O and 64ba6c22ceSWarner Losh * for trims. 65035ec48eSWarner Losh * 66035ec48eSWarner Losh * When CAM_IOSCHED_DYNAMIC is defined, the scheduler is enhanced to dynamically 67035ec48eSWarner Losh * steer the rate of one type of traffic to help other types of traffic (eg 68035ec48eSWarner Losh * limit writes when read latency deteriorates on SSDs). 69ba6c22ceSWarner Losh */ 70ba6c22ceSWarner Losh 71df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 72ba6c22ceSWarner Losh 73035ec48eSWarner Losh static int do_dynamic_iosched = 1; 74035ec48eSWarner Losh TUNABLE_INT("kern.cam.do_dynamic_iosched", &do_dynamic_iosched); 75035ec48eSWarner Losh SYSCTL_INT(_kern_cam, OID_AUTO, do_dynamic_iosched, CTLFLAG_RD, 76035ec48eSWarner Losh &do_dynamic_iosched, 1, 77035ec48eSWarner Losh "Enable Dynamic I/O scheduler optimizations."); 78ba6c22ceSWarner Losh 79cf3ec151SWarner Losh /* 80cf3ec151SWarner Losh * For an EMA, with an alpha of alpha, we know 81cf3ec151SWarner Losh * alpha = 2 / (N + 1) 82cf3ec151SWarner Losh * or 83cf3ec151SWarner Losh * N = 1 + (2 / alpha) 84cf3ec151SWarner Losh * where N is the number of samples that 86% of the current 85cf3ec151SWarner Losh * EMA is derived from. 86cf3ec151SWarner Losh * 87cf3ec151SWarner Losh * So we invent[*] alpha_bits: 88cf3ec151SWarner Losh * alpha_bits = -log_2(alpha) 89cf3ec151SWarner Losh * alpha = 2^-alpha_bits 90cf3ec151SWarner Losh * So 91cf3ec151SWarner Losh * N = 1 + 2^(alpha_bits + 1) 92cf3ec151SWarner Losh * 93cf3ec151SWarner Losh * The default 9 gives a 1025 lookback for 86% of the data. 94cf3ec151SWarner Losh * For a brief intro: https://en.wikipedia.org/wiki/Moving_average 95cf3ec151SWarner Losh * 96cf3ec151SWarner Losh * [*] Steal from the load average code and many other places. 9779d80af2SWarner Losh * Note: See computation of EMA and EMVAR for acceptable ranges of alpha. 98cf3ec151SWarner Losh */ 99ba6c22ceSWarner Losh static int alpha_bits = 9; 100ba6c22ceSWarner Losh TUNABLE_INT("kern.cam.iosched_alpha_bits", &alpha_bits); 101ba6c22ceSWarner Losh SYSCTL_INT(_kern_cam, OID_AUTO, iosched_alpha_bits, CTLFLAG_RW, 102ba6c22ceSWarner Losh &alpha_bits, 1, 103ba6c22ceSWarner Losh "Bits in EMA's alpha."); 104ba6c22ceSWarner Losh 105ba6c22ceSWarner Losh struct iop_stats; 106ba6c22ceSWarner Losh struct cam_iosched_softc; 107ba6c22ceSWarner Losh 108ba6c22ceSWarner Losh int iosched_debug = 0; 109ba6c22ceSWarner Losh 110ba6c22ceSWarner Losh typedef enum { 111ba6c22ceSWarner Losh none = 0, /* No limits */ 112ba6c22ceSWarner Losh queue_depth, /* Limit how many ops we queue to SIM */ 113ba6c22ceSWarner Losh iops, /* Limit # of IOPS to the drive */ 114ba6c22ceSWarner Losh bandwidth, /* Limit bandwidth to the drive */ 115ba6c22ceSWarner Losh limiter_max 116ba6c22ceSWarner Losh } io_limiter; 117ba6c22ceSWarner Losh 118ba6c22ceSWarner Losh static const char *cam_iosched_limiter_names[] = 119ba6c22ceSWarner Losh { "none", "queue_depth", "iops", "bandwidth" }; 120ba6c22ceSWarner Losh 121ba6c22ceSWarner Losh /* 122ba6c22ceSWarner Losh * Called to initialize the bits of the iop_stats structure relevant to the 123ba6c22ceSWarner Losh * limiter. Called just after the limiter is set. 124ba6c22ceSWarner Losh */ 125ba6c22ceSWarner Losh typedef int l_init_t(struct iop_stats *); 126ba6c22ceSWarner Losh 127ba6c22ceSWarner Losh /* 128ba6c22ceSWarner Losh * Called every tick. 129ba6c22ceSWarner Losh */ 130ba6c22ceSWarner Losh typedef int l_tick_t(struct iop_stats *); 131ba6c22ceSWarner Losh 132ba6c22ceSWarner Losh /* 133ba6c22ceSWarner Losh * Called to see if the limiter thinks this IOP can be allowed to 1340b4060b0SEd Maste * proceed. If so, the limiter assumes that the IOP proceeded 135ba6c22ceSWarner Losh * and makes any accounting of it that's needed. 136ba6c22ceSWarner Losh */ 137ba6c22ceSWarner Losh typedef int l_iop_t(struct iop_stats *, struct bio *); 138ba6c22ceSWarner Losh 139ba6c22ceSWarner Losh /* 1400b4060b0SEd Maste * Called when an I/O completes so the limiter can update its 141ba6c22ceSWarner Losh * accounting. Pending I/Os may complete in any order (even when 142ba6c22ceSWarner Losh * sent to the hardware at the same time), so the limiter may not 143ba6c22ceSWarner Losh * make any assumptions other than this I/O has completed. If it 144ba6c22ceSWarner Losh * returns 1, then xpt_schedule() needs to be called again. 145ba6c22ceSWarner Losh */ 146ba6c22ceSWarner Losh typedef int l_iodone_t(struct iop_stats *, struct bio *); 147ba6c22ceSWarner Losh 148ba6c22ceSWarner Losh static l_iop_t cam_iosched_qd_iop; 149ba6c22ceSWarner Losh static l_iop_t cam_iosched_qd_caniop; 150ba6c22ceSWarner Losh static l_iodone_t cam_iosched_qd_iodone; 151ba6c22ceSWarner Losh 152ba6c22ceSWarner Losh static l_init_t cam_iosched_iops_init; 153ba6c22ceSWarner Losh static l_tick_t cam_iosched_iops_tick; 154ba6c22ceSWarner Losh static l_iop_t cam_iosched_iops_caniop; 155ba6c22ceSWarner Losh static l_iop_t cam_iosched_iops_iop; 156ba6c22ceSWarner Losh 157ba6c22ceSWarner Losh static l_init_t cam_iosched_bw_init; 158ba6c22ceSWarner Losh static l_tick_t cam_iosched_bw_tick; 159ba6c22ceSWarner Losh static l_iop_t cam_iosched_bw_caniop; 160ba6c22ceSWarner Losh static l_iop_t cam_iosched_bw_iop; 161ba6c22ceSWarner Losh 162b20c0a07SWarner Losh struct limswitch { 163ba6c22ceSWarner Losh l_init_t *l_init; 164ba6c22ceSWarner Losh l_tick_t *l_tick; 165ba6c22ceSWarner Losh l_iop_t *l_iop; 166ba6c22ceSWarner Losh l_iop_t *l_caniop; 167ba6c22ceSWarner Losh l_iodone_t *l_iodone; 168ba6c22ceSWarner Losh } limsw[] = 169ba6c22ceSWarner Losh { 170ba6c22ceSWarner Losh { /* none */ 171ba6c22ceSWarner Losh .l_init = NULL, 172ba6c22ceSWarner Losh .l_tick = NULL, 173ba6c22ceSWarner Losh .l_iop = NULL, 174ba6c22ceSWarner Losh .l_iodone= NULL, 175ba6c22ceSWarner Losh }, 176ba6c22ceSWarner Losh { /* queue_depth */ 177ba6c22ceSWarner Losh .l_init = NULL, 178ba6c22ceSWarner Losh .l_tick = NULL, 179ba6c22ceSWarner Losh .l_caniop = cam_iosched_qd_caniop, 180ba6c22ceSWarner Losh .l_iop = cam_iosched_qd_iop, 181ba6c22ceSWarner Losh .l_iodone= cam_iosched_qd_iodone, 182ba6c22ceSWarner Losh }, 183ba6c22ceSWarner Losh { /* iops */ 184ba6c22ceSWarner Losh .l_init = cam_iosched_iops_init, 185ba6c22ceSWarner Losh .l_tick = cam_iosched_iops_tick, 186ba6c22ceSWarner Losh .l_caniop = cam_iosched_iops_caniop, 187ba6c22ceSWarner Losh .l_iop = cam_iosched_iops_iop, 188ba6c22ceSWarner Losh .l_iodone= NULL, 189ba6c22ceSWarner Losh }, 190ba6c22ceSWarner Losh { /* bandwidth */ 191ba6c22ceSWarner Losh .l_init = cam_iosched_bw_init, 192ba6c22ceSWarner Losh .l_tick = cam_iosched_bw_tick, 193ba6c22ceSWarner Losh .l_caniop = cam_iosched_bw_caniop, 194ba6c22ceSWarner Losh .l_iop = cam_iosched_bw_iop, 195ba6c22ceSWarner Losh .l_iodone= NULL, 196ba6c22ceSWarner Losh }, 197ba6c22ceSWarner Losh }; 198ba6c22ceSWarner Losh 199b20c0a07SWarner Losh struct iop_stats { 200ba6c22ceSWarner Losh /* 201ba6c22ceSWarner Losh * sysctl state for this subnode. 202ba6c22ceSWarner Losh */ 203ba6c22ceSWarner Losh struct sysctl_ctx_list sysctl_ctx; 204ba6c22ceSWarner Losh struct sysctl_oid *sysctl_tree; 205ba6c22ceSWarner Losh 206ba6c22ceSWarner Losh /* 207ba6c22ceSWarner Losh * Information about the current rate limiters, if any 208ba6c22ceSWarner Losh */ 209ba6c22ceSWarner Losh io_limiter limiter; /* How are I/Os being limited */ 210ba6c22ceSWarner Losh int min; /* Low range of limit */ 211ba6c22ceSWarner Losh int max; /* High range of limit */ 212ba6c22ceSWarner Losh int current; /* Current rate limiter */ 213ba6c22ceSWarner Losh int l_value1; /* per-limiter scratch value 1. */ 214ba6c22ceSWarner Losh int l_value2; /* per-limiter scratch value 2. */ 215ba6c22ceSWarner Losh 216ba6c22ceSWarner Losh /* 217ba6c22ceSWarner Losh * Debug information about counts of I/Os that have gone through the 218ba6c22ceSWarner Losh * scheduler. 219ba6c22ceSWarner Losh */ 220ba6c22ceSWarner Losh int pending; /* I/Os pending in the hardware */ 221ba6c22ceSWarner Losh int queued; /* number currently in the queue */ 222ba6c22ceSWarner Losh int total; /* Total for all time -- wraps */ 223ba6c22ceSWarner Losh int in; /* number queued all time -- wraps */ 224ba6c22ceSWarner Losh int out; /* number completed all time -- wraps */ 225c4b72d8bSWarner Losh int errs; /* Number of I/Os completed with error -- wraps */ 226ba6c22ceSWarner Losh 227ba6c22ceSWarner Losh /* 228ba6c22ceSWarner Losh * Statistics on different bits of the process. 229ba6c22ceSWarner Losh */ 230cf3ec151SWarner Losh /* Exp Moving Average, see alpha_bits for more details */ 231ba6c22ceSWarner Losh sbintime_t ema; 23279d80af2SWarner Losh sbintime_t emvar; 233ba6c22ceSWarner Losh sbintime_t sd; /* Last computed sd */ 234ba6c22ceSWarner Losh 235cf3ec151SWarner Losh uint32_t state_flags; 236cf3ec151SWarner Losh #define IOP_RATE_LIMITED 1u 237cf3ec151SWarner Losh 23808fc2f23SWarner Losh #define LAT_BUCKETS 15 /* < 1ms < 2ms ... < 2^(n-1)ms >= 2^(n-1)ms*/ 239cf3ec151SWarner Losh uint64_t latencies[LAT_BUCKETS]; 240cf3ec151SWarner Losh 241ba6c22ceSWarner Losh struct cam_iosched_softc *softc; 242ba6c22ceSWarner Losh }; 243ba6c22ceSWarner Losh 244ba6c22ceSWarner Losh 245ba6c22ceSWarner Losh typedef enum { 246ba6c22ceSWarner Losh set_max = 0, /* current = max */ 247ba6c22ceSWarner Losh read_latency, /* Steer read latency by throttling writes */ 248ba6c22ceSWarner Losh cl_max /* Keep last */ 249ba6c22ceSWarner Losh } control_type; 250ba6c22ceSWarner Losh 251ba6c22ceSWarner Losh static const char *cam_iosched_control_type_names[] = 252ba6c22ceSWarner Losh { "set_max", "read_latency" }; 253ba6c22ceSWarner Losh 254b20c0a07SWarner Losh struct control_loop { 255ba6c22ceSWarner Losh /* 256ba6c22ceSWarner Losh * sysctl state for this subnode. 257ba6c22ceSWarner Losh */ 258ba6c22ceSWarner Losh struct sysctl_ctx_list sysctl_ctx; 259ba6c22ceSWarner Losh struct sysctl_oid *sysctl_tree; 260ba6c22ceSWarner Losh 261ba6c22ceSWarner Losh sbintime_t next_steer; /* Time of next steer */ 262ba6c22ceSWarner Losh sbintime_t steer_interval; /* How often do we steer? */ 263ba6c22ceSWarner Losh sbintime_t lolat; 264ba6c22ceSWarner Losh sbintime_t hilat; 265ba6c22ceSWarner Losh int alpha; 266ba6c22ceSWarner Losh control_type type; /* What type of control? */ 267ba6c22ceSWarner Losh int last_count; /* Last I/O count */ 268ba6c22ceSWarner Losh 269ba6c22ceSWarner Losh struct cam_iosched_softc *softc; 270ba6c22ceSWarner Losh }; 271ba6c22ceSWarner Losh 272ba6c22ceSWarner Losh #endif 273ba6c22ceSWarner Losh 274b20c0a07SWarner Losh struct cam_iosched_softc { 275ba6c22ceSWarner Losh struct bio_queue_head bio_queue; 276ba6c22ceSWarner Losh struct bio_queue_head trim_queue; 277ba6c22ceSWarner Losh /* scheduler flags < 16, user flags >= 16 */ 278ba6c22ceSWarner Losh uint32_t flags; 279ba6c22ceSWarner Losh int sort_io_queue; 280d900ade5SWarner Losh int trim_goal; /* # of trims to queue before sending */ 281d900ade5SWarner Losh int trim_ticks; /* Max ticks to hold trims */ 282d900ade5SWarner Losh int last_trim_tick; /* Last 'tick' time ld a trim */ 283d900ade5SWarner Losh int queued_trims; /* Number of trims in the queue */ 284df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 285ba6c22ceSWarner Losh int read_bias; /* Read bias setting */ 286ba6c22ceSWarner Losh int current_read_bias; /* Current read bias state */ 287ba6c22ceSWarner Losh int total_ticks; 288cf3ec151SWarner Losh int load; /* EMA of 'load average' of disk / 2^16 */ 289ba6c22ceSWarner Losh 290ba6c22ceSWarner Losh struct bio_queue_head write_queue; 291ba6c22ceSWarner Losh struct iop_stats read_stats, write_stats, trim_stats; 292ba6c22ceSWarner Losh struct sysctl_ctx_list sysctl_ctx; 293ba6c22ceSWarner Losh struct sysctl_oid *sysctl_tree; 294ba6c22ceSWarner Losh 295ba6c22ceSWarner Losh int quanta; /* Number of quanta per second */ 296ba6c22ceSWarner Losh struct callout ticker; /* Callout for our quota system */ 297ba6c22ceSWarner Losh struct cam_periph *periph; /* cam periph associated with this device */ 298ba6c22ceSWarner Losh uint32_t this_frac; /* Fraction of a second (1024ths) for this tick */ 299ba6c22ceSWarner Losh sbintime_t last_time; /* Last time we ticked */ 300ba6c22ceSWarner Losh struct control_loop cl; 301e5436ab5SWarner Losh sbintime_t max_lat; /* when != 0, if iop latency > max_lat, call max_lat_fcn */ 302e5436ab5SWarner Losh cam_iosched_latfcn_t latfcn; 303e5436ab5SWarner Losh void *latarg; 304ba6c22ceSWarner Losh #endif 305ba6c22ceSWarner Losh }; 306ba6c22ceSWarner Losh 307df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 308ba6c22ceSWarner Losh /* 309ba6c22ceSWarner Losh * helper functions to call the limsw functions. 310ba6c22ceSWarner Losh */ 311ba6c22ceSWarner Losh static int 312ba6c22ceSWarner Losh cam_iosched_limiter_init(struct iop_stats *ios) 313ba6c22ceSWarner Losh { 314ba6c22ceSWarner Losh int lim = ios->limiter; 315ba6c22ceSWarner Losh 316ba6c22ceSWarner Losh /* maybe this should be a kassert */ 317ba6c22ceSWarner Losh if (lim < none || lim >= limiter_max) 318ba6c22ceSWarner Losh return EINVAL; 319ba6c22ceSWarner Losh 320ba6c22ceSWarner Losh if (limsw[lim].l_init) 321ba6c22ceSWarner Losh return limsw[lim].l_init(ios); 322ba6c22ceSWarner Losh 323ba6c22ceSWarner Losh return 0; 324ba6c22ceSWarner Losh } 325ba6c22ceSWarner Losh 326ba6c22ceSWarner Losh static int 327ba6c22ceSWarner Losh cam_iosched_limiter_tick(struct iop_stats *ios) 328ba6c22ceSWarner Losh { 329ba6c22ceSWarner Losh int lim = ios->limiter; 330ba6c22ceSWarner Losh 331ba6c22ceSWarner Losh /* maybe this should be a kassert */ 332ba6c22ceSWarner Losh if (lim < none || lim >= limiter_max) 333ba6c22ceSWarner Losh return EINVAL; 334ba6c22ceSWarner Losh 335ba6c22ceSWarner Losh if (limsw[lim].l_tick) 336ba6c22ceSWarner Losh return limsw[lim].l_tick(ios); 337ba6c22ceSWarner Losh 338ba6c22ceSWarner Losh return 0; 339ba6c22ceSWarner Losh } 340ba6c22ceSWarner Losh 341ba6c22ceSWarner Losh static int 342ba6c22ceSWarner Losh cam_iosched_limiter_iop(struct iop_stats *ios, struct bio *bp) 343ba6c22ceSWarner Losh { 344ba6c22ceSWarner Losh int lim = ios->limiter; 345ba6c22ceSWarner Losh 346ba6c22ceSWarner Losh /* maybe this should be a kassert */ 347ba6c22ceSWarner Losh if (lim < none || lim >= limiter_max) 348ba6c22ceSWarner Losh return EINVAL; 349ba6c22ceSWarner Losh 350ba6c22ceSWarner Losh if (limsw[lim].l_iop) 351ba6c22ceSWarner Losh return limsw[lim].l_iop(ios, bp); 352ba6c22ceSWarner Losh 353ba6c22ceSWarner Losh return 0; 354ba6c22ceSWarner Losh } 355ba6c22ceSWarner Losh 356ba6c22ceSWarner Losh static int 357ba6c22ceSWarner Losh cam_iosched_limiter_caniop(struct iop_stats *ios, struct bio *bp) 358ba6c22ceSWarner Losh { 359ba6c22ceSWarner Losh int lim = ios->limiter; 360ba6c22ceSWarner Losh 361ba6c22ceSWarner Losh /* maybe this should be a kassert */ 362ba6c22ceSWarner Losh if (lim < none || lim >= limiter_max) 363ba6c22ceSWarner Losh return EINVAL; 364ba6c22ceSWarner Losh 365ba6c22ceSWarner Losh if (limsw[lim].l_caniop) 366ba6c22ceSWarner Losh return limsw[lim].l_caniop(ios, bp); 367ba6c22ceSWarner Losh 368ba6c22ceSWarner Losh return 0; 369ba6c22ceSWarner Losh } 370ba6c22ceSWarner Losh 371ba6c22ceSWarner Losh static int 372ba6c22ceSWarner Losh cam_iosched_limiter_iodone(struct iop_stats *ios, struct bio *bp) 373ba6c22ceSWarner Losh { 374ba6c22ceSWarner Losh int lim = ios->limiter; 375ba6c22ceSWarner Losh 376ba6c22ceSWarner Losh /* maybe this should be a kassert */ 377ba6c22ceSWarner Losh if (lim < none || lim >= limiter_max) 378ba6c22ceSWarner Losh return 0; 379ba6c22ceSWarner Losh 380ba6c22ceSWarner Losh if (limsw[lim].l_iodone) 381ba6c22ceSWarner Losh return limsw[lim].l_iodone(ios, bp); 382ba6c22ceSWarner Losh 383ba6c22ceSWarner Losh return 0; 384ba6c22ceSWarner Losh } 385ba6c22ceSWarner Losh 386ba6c22ceSWarner Losh /* 387ba6c22ceSWarner Losh * Functions to implement the different kinds of limiters 388ba6c22ceSWarner Losh */ 389ba6c22ceSWarner Losh 390ba6c22ceSWarner Losh static int 391ba6c22ceSWarner Losh cam_iosched_qd_iop(struct iop_stats *ios, struct bio *bp) 392ba6c22ceSWarner Losh { 393ba6c22ceSWarner Losh 394ba6c22ceSWarner Losh if (ios->current <= 0 || ios->pending < ios->current) 395ba6c22ceSWarner Losh return 0; 396ba6c22ceSWarner Losh 397ba6c22ceSWarner Losh return EAGAIN; 398ba6c22ceSWarner Losh } 399ba6c22ceSWarner Losh 400ba6c22ceSWarner Losh static int 401ba6c22ceSWarner Losh cam_iosched_qd_caniop(struct iop_stats *ios, struct bio *bp) 402ba6c22ceSWarner Losh { 403ba6c22ceSWarner Losh 404ba6c22ceSWarner Losh if (ios->current <= 0 || ios->pending < ios->current) 405ba6c22ceSWarner Losh return 0; 406ba6c22ceSWarner Losh 407ba6c22ceSWarner Losh return EAGAIN; 408ba6c22ceSWarner Losh } 409ba6c22ceSWarner Losh 410ba6c22ceSWarner Losh static int 411ba6c22ceSWarner Losh cam_iosched_qd_iodone(struct iop_stats *ios, struct bio *bp) 412ba6c22ceSWarner Losh { 413ba6c22ceSWarner Losh 414ba6c22ceSWarner Losh if (ios->current <= 0 || ios->pending != ios->current) 415ba6c22ceSWarner Losh return 0; 416ba6c22ceSWarner Losh 417ba6c22ceSWarner Losh return 1; 418ba6c22ceSWarner Losh } 419ba6c22ceSWarner Losh 420ba6c22ceSWarner Losh static int 421ba6c22ceSWarner Losh cam_iosched_iops_init(struct iop_stats *ios) 422ba6c22ceSWarner Losh { 423ba6c22ceSWarner Losh 424ba6c22ceSWarner Losh ios->l_value1 = ios->current / ios->softc->quanta; 425ba6c22ceSWarner Losh if (ios->l_value1 <= 0) 426ba6c22ceSWarner Losh ios->l_value1 = 1; 427f777123bSWarner Losh ios->l_value2 = 0; 428ba6c22ceSWarner Losh 429ba6c22ceSWarner Losh return 0; 430ba6c22ceSWarner Losh } 431ba6c22ceSWarner Losh 432ba6c22ceSWarner Losh static int 433ba6c22ceSWarner Losh cam_iosched_iops_tick(struct iop_stats *ios) 434ba6c22ceSWarner Losh { 43578ed811eSWarner Losh int new_ios; 436ba6c22ceSWarner Losh 437f777123bSWarner Losh /* 438f777123bSWarner Losh * Allow at least one IO per tick until all 439f777123bSWarner Losh * the IOs for this interval have been spent. 440f777123bSWarner Losh */ 44178ed811eSWarner Losh new_ios = (int)((ios->current * (uint64_t)ios->softc->this_frac) >> 16); 44278ed811eSWarner Losh if (new_ios < 1 && ios->l_value2 < ios->current) { 44378ed811eSWarner Losh new_ios = 1; 444f777123bSWarner Losh ios->l_value2++; 445f777123bSWarner Losh } 446ba6c22ceSWarner Losh 44778ed811eSWarner Losh /* 44878ed811eSWarner Losh * If this a new accounting interval, discard any "unspent" ios 44978ed811eSWarner Losh * granted in the previous interval. Otherwise add the new ios to 45078ed811eSWarner Losh * the previously granted ones that haven't been spent yet. 45178ed811eSWarner Losh */ 45278ed811eSWarner Losh if ((ios->softc->total_ticks % ios->softc->quanta) == 0) { 45378ed811eSWarner Losh ios->l_value1 = new_ios; 45478ed811eSWarner Losh ios->l_value2 = 1; 45578ed811eSWarner Losh } else { 45678ed811eSWarner Losh ios->l_value1 += new_ios; 45778ed811eSWarner Losh } 45878ed811eSWarner Losh 45978ed811eSWarner Losh 460ba6c22ceSWarner Losh return 0; 461ba6c22ceSWarner Losh } 462ba6c22ceSWarner Losh 463ba6c22ceSWarner Losh static int 464ba6c22ceSWarner Losh cam_iosched_iops_caniop(struct iop_stats *ios, struct bio *bp) 465ba6c22ceSWarner Losh { 466ba6c22ceSWarner Losh 467ba6c22ceSWarner Losh /* 468ba6c22ceSWarner Losh * So if we have any more IOPs left, allow it, 4696ca2fb66SWarner Losh * otherwise wait. If current iops is 0, treat that 4706ca2fb66SWarner Losh * as unlimited as a failsafe. 471ba6c22ceSWarner Losh */ 4726ca2fb66SWarner Losh if (ios->current > 0 && ios->l_value1 <= 0) 473ba6c22ceSWarner Losh return EAGAIN; 474ba6c22ceSWarner Losh return 0; 475ba6c22ceSWarner Losh } 476ba6c22ceSWarner Losh 477ba6c22ceSWarner Losh static int 478ba6c22ceSWarner Losh cam_iosched_iops_iop(struct iop_stats *ios, struct bio *bp) 479ba6c22ceSWarner Losh { 480ba6c22ceSWarner Losh int rv; 481ba6c22ceSWarner Losh 482ba6c22ceSWarner Losh rv = cam_iosched_limiter_caniop(ios, bp); 483ba6c22ceSWarner Losh if (rv == 0) 484ba6c22ceSWarner Losh ios->l_value1--; 485ba6c22ceSWarner Losh 486ba6c22ceSWarner Losh return rv; 487ba6c22ceSWarner Losh } 488ba6c22ceSWarner Losh 489ba6c22ceSWarner Losh static int 490ba6c22ceSWarner Losh cam_iosched_bw_init(struct iop_stats *ios) 491ba6c22ceSWarner Losh { 492ba6c22ceSWarner Losh 493ba6c22ceSWarner Losh /* ios->current is in kB/s, so scale to bytes */ 494ba6c22ceSWarner Losh ios->l_value1 = ios->current * 1000 / ios->softc->quanta; 495ba6c22ceSWarner Losh 496ba6c22ceSWarner Losh return 0; 497ba6c22ceSWarner Losh } 498ba6c22ceSWarner Losh 499ba6c22ceSWarner Losh static int 500ba6c22ceSWarner Losh cam_iosched_bw_tick(struct iop_stats *ios) 501ba6c22ceSWarner Losh { 502ba6c22ceSWarner Losh int bw; 503ba6c22ceSWarner Losh 504ba6c22ceSWarner Losh /* 505ba6c22ceSWarner Losh * If we're in the hole for available quota from 506ba6c22ceSWarner Losh * the last time, then add the quantum for this. 507ba6c22ceSWarner Losh * If we have any left over from last quantum, 508ba6c22ceSWarner Losh * then too bad, that's lost. Also, ios->current 509ba6c22ceSWarner Losh * is in kB/s, so scale. 510ba6c22ceSWarner Losh * 511ba6c22ceSWarner Losh * We also allow up to 4 quanta of credits to 512ba6c22ceSWarner Losh * accumulate to deal with burstiness. 4 is extremely 513ba6c22ceSWarner Losh * arbitrary. 514ba6c22ceSWarner Losh */ 515ba6c22ceSWarner Losh bw = (int)((ios->current * 1000ull * (uint64_t)ios->softc->this_frac) >> 16); 516ba6c22ceSWarner Losh if (ios->l_value1 < bw * 4) 517ba6c22ceSWarner Losh ios->l_value1 += bw; 518ba6c22ceSWarner Losh 519ba6c22ceSWarner Losh return 0; 520ba6c22ceSWarner Losh } 521ba6c22ceSWarner Losh 522ba6c22ceSWarner Losh static int 523ba6c22ceSWarner Losh cam_iosched_bw_caniop(struct iop_stats *ios, struct bio *bp) 524ba6c22ceSWarner Losh { 525ba6c22ceSWarner Losh /* 526ba6c22ceSWarner Losh * So if we have any more bw quota left, allow it, 5270b4060b0SEd Maste * otherwise wait. Note, we'll go negative and that's 5280b4060b0SEd Maste * OK. We'll just get a little less next quota. 529ba6c22ceSWarner Losh * 530ba6c22ceSWarner Losh * Note on going negative: that allows us to process 531ba6c22ceSWarner Losh * requests in order better, since we won't allow 532ba6c22ceSWarner Losh * shorter reads to get around the long one that we 533ba6c22ceSWarner Losh * don't have the quota to do just yet. It also prevents 534ba6c22ceSWarner Losh * starvation by being a little more permissive about 535ba6c22ceSWarner Losh * what we let through this quantum (to prevent the 536ba6c22ceSWarner Losh * starvation), at the cost of getting a little less 537ba6c22ceSWarner Losh * next quantum. 5386ca2fb66SWarner Losh * 5396ca2fb66SWarner Losh * Also note that if the current limit is <= 0, 5406ca2fb66SWarner Losh * we treat it as unlimited as a failsafe. 541ba6c22ceSWarner Losh */ 5426ca2fb66SWarner Losh if (ios->current > 0 && ios->l_value1 <= 0) 543ba6c22ceSWarner Losh return EAGAIN; 544ba6c22ceSWarner Losh 545ba6c22ceSWarner Losh 546ba6c22ceSWarner Losh return 0; 547ba6c22ceSWarner Losh } 548ba6c22ceSWarner Losh 549ba6c22ceSWarner Losh static int 550ba6c22ceSWarner Losh cam_iosched_bw_iop(struct iop_stats *ios, struct bio *bp) 551ba6c22ceSWarner Losh { 552ba6c22ceSWarner Losh int rv; 553ba6c22ceSWarner Losh 554ba6c22ceSWarner Losh rv = cam_iosched_limiter_caniop(ios, bp); 555ba6c22ceSWarner Losh if (rv == 0) 556ba6c22ceSWarner Losh ios->l_value1 -= bp->bio_length; 557ba6c22ceSWarner Losh 558ba6c22ceSWarner Losh return rv; 559ba6c22ceSWarner Losh } 560ba6c22ceSWarner Losh 561ba6c22ceSWarner Losh static void cam_iosched_cl_maybe_steer(struct control_loop *clp); 562ba6c22ceSWarner Losh 563ba6c22ceSWarner Losh static void 564ba6c22ceSWarner Losh cam_iosched_ticker(void *arg) 565ba6c22ceSWarner Losh { 566ba6c22ceSWarner Losh struct cam_iosched_softc *isc = arg; 567ba6c22ceSWarner Losh sbintime_t now, delta; 568cf3ec151SWarner Losh int pending; 569ba6c22ceSWarner Losh 5703028dd8dSWarner Losh callout_reset(&isc->ticker, hz / isc->quanta, cam_iosched_ticker, isc); 571ba6c22ceSWarner Losh 572ba6c22ceSWarner Losh now = sbinuptime(); 573ba6c22ceSWarner Losh delta = now - isc->last_time; 574ba6c22ceSWarner Losh isc->this_frac = (uint32_t)delta >> 16; /* Note: discards seconds -- should be 0 harmless if not */ 575ba6c22ceSWarner Losh isc->last_time = now; 576ba6c22ceSWarner Losh 577ba6c22ceSWarner Losh cam_iosched_cl_maybe_steer(&isc->cl); 578ba6c22ceSWarner Losh 579ba6c22ceSWarner Losh cam_iosched_limiter_tick(&isc->read_stats); 580ba6c22ceSWarner Losh cam_iosched_limiter_tick(&isc->write_stats); 581ba6c22ceSWarner Losh cam_iosched_limiter_tick(&isc->trim_stats); 582ba6c22ceSWarner Losh 583ba6c22ceSWarner Losh cam_iosched_schedule(isc, isc->periph); 584ba6c22ceSWarner Losh 585cf3ec151SWarner Losh /* 586cf3ec151SWarner Losh * isc->load is an EMA of the pending I/Os at each tick. The number of 587cf3ec151SWarner Losh * pending I/Os is the sum of the I/Os queued to the hardware, and those 588cf3ec151SWarner Losh * in the software queue that could be queued to the hardware if there 589cf3ec151SWarner Losh * were slots. 590cf3ec151SWarner Losh * 591cf3ec151SWarner Losh * ios_stats.pending is a count of requests in the SIM right now for 592cf3ec151SWarner Losh * each of these types of I/O. So the total pending count is the sum of 593cf3ec151SWarner Losh * these I/Os and the sum of the queued I/Os still in the software queue 594cf3ec151SWarner Losh * for those operations that aren't being rate limited at the moment. 595cf3ec151SWarner Losh * 596cf3ec151SWarner Losh * The reason for the rate limiting bit is because those I/Os 597cf3ec151SWarner Losh * aren't part of the software queued load (since we could 598cf3ec151SWarner Losh * give them to hardware, but choose not to). 599cf3ec151SWarner Losh * 600cf3ec151SWarner Losh * Note: due to a bug in counting pending TRIM in the device, we 601cf3ec151SWarner Losh * don't include them in this count. We count each BIO_DELETE in 602cf3ec151SWarner Losh * the pending count, but the periph drivers collapse them down 603cf3ec151SWarner Losh * into one TRIM command. That one trim command gets the completion 604cf3ec151SWarner Losh * so the counts get off. 605cf3ec151SWarner Losh */ 606cf3ec151SWarner Losh pending = isc->read_stats.pending + isc->write_stats.pending /* + isc->trim_stats.pending */; 607cf3ec151SWarner Losh pending += !!(isc->read_stats.state_flags & IOP_RATE_LIMITED) * isc->read_stats.queued + 608cf3ec151SWarner Losh !!(isc->write_stats.state_flags & IOP_RATE_LIMITED) * isc->write_stats.queued /* + 609cf3ec151SWarner Losh !!(isc->trim_stats.state_flags & IOP_RATE_LIMITED) * isc->trim_stats.queued */ ; 610cf3ec151SWarner Losh pending <<= 16; 611cf3ec151SWarner Losh pending /= isc->periph->path->device->ccbq.total_openings; 612cf3ec151SWarner Losh 613cf3ec151SWarner Losh isc->load = (pending + (isc->load << 13) - isc->load) >> 13; /* see above: 13 -> 16139 / 200/s = ~81s ~1 minute */ 614cf3ec151SWarner Losh 615ba6c22ceSWarner Losh isc->total_ticks++; 616ba6c22ceSWarner Losh } 617ba6c22ceSWarner Losh 618ba6c22ceSWarner Losh 619ba6c22ceSWarner Losh static void 620ba6c22ceSWarner Losh cam_iosched_cl_init(struct control_loop *clp, struct cam_iosched_softc *isc) 621ba6c22ceSWarner Losh { 622ba6c22ceSWarner Losh 623ba6c22ceSWarner Losh clp->next_steer = sbinuptime(); 624ba6c22ceSWarner Losh clp->softc = isc; 625ba6c22ceSWarner Losh clp->steer_interval = SBT_1S * 5; /* Let's start out steering every 5s */ 626ba6c22ceSWarner Losh clp->lolat = 5 * SBT_1MS; 627ba6c22ceSWarner Losh clp->hilat = 15 * SBT_1MS; 628ba6c22ceSWarner Losh clp->alpha = 20; /* Alpha == gain. 20 = .2 */ 629ba6c22ceSWarner Losh clp->type = set_max; 630ba6c22ceSWarner Losh } 631ba6c22ceSWarner Losh 632ba6c22ceSWarner Losh static void 633ba6c22ceSWarner Losh cam_iosched_cl_maybe_steer(struct control_loop *clp) 634ba6c22ceSWarner Losh { 635ba6c22ceSWarner Losh struct cam_iosched_softc *isc; 636ba6c22ceSWarner Losh sbintime_t now, lat; 637ba6c22ceSWarner Losh int old; 638ba6c22ceSWarner Losh 639ba6c22ceSWarner Losh isc = clp->softc; 640ba6c22ceSWarner Losh now = isc->last_time; 641ba6c22ceSWarner Losh if (now < clp->next_steer) 642ba6c22ceSWarner Losh return; 643ba6c22ceSWarner Losh 644ba6c22ceSWarner Losh clp->next_steer = now + clp->steer_interval; 645ba6c22ceSWarner Losh switch (clp->type) { 646ba6c22ceSWarner Losh case set_max: 647ba6c22ceSWarner Losh if (isc->write_stats.current != isc->write_stats.max) 648ba6c22ceSWarner Losh printf("Steering write from %d kBps to %d kBps\n", 649ba6c22ceSWarner Losh isc->write_stats.current, isc->write_stats.max); 650ba6c22ceSWarner Losh isc->read_stats.current = isc->read_stats.max; 651ba6c22ceSWarner Losh isc->write_stats.current = isc->write_stats.max; 652ba6c22ceSWarner Losh isc->trim_stats.current = isc->trim_stats.max; 653ba6c22ceSWarner Losh break; 654ba6c22ceSWarner Losh case read_latency: 655ba6c22ceSWarner Losh old = isc->write_stats.current; 656ba6c22ceSWarner Losh lat = isc->read_stats.ema; 657ba6c22ceSWarner Losh /* 658ba6c22ceSWarner Losh * Simple PLL-like engine. Since we're steering to a range for 659ba6c22ceSWarner Losh * the SP (set point) that makes things a little more 660ba6c22ceSWarner Losh * complicated. In addition, we're not directly controlling our 661ba6c22ceSWarner Losh * PV (process variable), the read latency, but instead are 662ba6c22ceSWarner Losh * manipulating the write bandwidth limit for our MV 663ba6c22ceSWarner Losh * (manipulation variable), analysis of this code gets a bit 664ba6c22ceSWarner Losh * messy. Also, the MV is a very noisy control surface for read 665ba6c22ceSWarner Losh * latency since it is affected by many hidden processes inside 666ba6c22ceSWarner Losh * the device which change how responsive read latency will be 667ba6c22ceSWarner Losh * in reaction to changes in write bandwidth. Unlike the classic 668ba6c22ceSWarner Losh * boiler control PLL. this may result in over-steering while 669ba6c22ceSWarner Losh * the SSD takes its time to react to the new, lower load. This 670ba6c22ceSWarner Losh * is why we use a relatively low alpha of between .1 and .25 to 671ba6c22ceSWarner Losh * compensate for this effect. At .1, it takes ~22 steering 672ba6c22ceSWarner Losh * intervals to back off by a factor of 10. At .2 it only takes 673ba6c22ceSWarner Losh * ~10. At .25 it only takes ~8. However some preliminary data 674ba6c22ceSWarner Losh * from the SSD drives suggests a reasponse time in 10's of 675ba6c22ceSWarner Losh * seconds before latency drops regardless of the new write 6760b4060b0SEd Maste * rate. Careful observation will be required to tune this 677ba6c22ceSWarner Losh * effectively. 678ba6c22ceSWarner Losh * 679ba6c22ceSWarner Losh * Also, when there's no read traffic, we jack up the write 680ba6c22ceSWarner Losh * limit too regardless of the last read latency. 10 is 681ba6c22ceSWarner Losh * somewhat arbitrary. 682ba6c22ceSWarner Losh */ 683ba6c22ceSWarner Losh if (lat < clp->lolat || isc->read_stats.total - clp->last_count < 10) 684ba6c22ceSWarner Losh isc->write_stats.current = isc->write_stats.current * 685ba6c22ceSWarner Losh (100 + clp->alpha) / 100; /* Scale up */ 686ba6c22ceSWarner Losh else if (lat > clp->hilat) 687ba6c22ceSWarner Losh isc->write_stats.current = isc->write_stats.current * 688ba6c22ceSWarner Losh (100 - clp->alpha) / 100; /* Scale down */ 689ba6c22ceSWarner Losh clp->last_count = isc->read_stats.total; 690ba6c22ceSWarner Losh 691ba6c22ceSWarner Losh /* 692ba6c22ceSWarner Losh * Even if we don't steer, per se, enforce the min/max limits as 693ba6c22ceSWarner Losh * those may have changed. 694ba6c22ceSWarner Losh */ 695ba6c22ceSWarner Losh if (isc->write_stats.current < isc->write_stats.min) 696ba6c22ceSWarner Losh isc->write_stats.current = isc->write_stats.min; 697ba6c22ceSWarner Losh if (isc->write_stats.current > isc->write_stats.max) 698ba6c22ceSWarner Losh isc->write_stats.current = isc->write_stats.max; 6992b5c19f1SWarner Losh if (old != isc->write_stats.current && iosched_debug) 700cf3ec151SWarner Losh printf("Steering write from %d kBps to %d kBps due to latency of %jdus\n", 701ba6c22ceSWarner Losh old, isc->write_stats.current, 7022b5c19f1SWarner Losh (uintmax_t)((uint64_t)1000000 * (uint32_t)lat) >> 32); 703ba6c22ceSWarner Losh break; 704ba6c22ceSWarner Losh case cl_max: 705ba6c22ceSWarner Losh break; 706ba6c22ceSWarner Losh } 707ba6c22ceSWarner Losh } 708ba6c22ceSWarner Losh #endif 709ba6c22ceSWarner Losh 710ece56614SWarner Losh /* 711ece56614SWarner Losh * Trim or similar currently pending completion. Should only be set for 712ece56614SWarner Losh * those drivers wishing only one Trim active at a time. 713ece56614SWarner Losh */ 714ece56614SWarner Losh #define CAM_IOSCHED_FLAG_TRIM_ACTIVE (1ul << 0) 71507e5967aSWarner Losh /* Callout active, and needs to be torn down */ 71607e5967aSWarner Losh #define CAM_IOSCHED_FLAG_CALLOUT_ACTIVE (1ul << 1) 71707e5967aSWarner Losh 71807e5967aSWarner Losh /* Periph drivers set these flags to indicate work */ 71907e5967aSWarner Losh #define CAM_IOSCHED_FLAG_WORK_FLAGS ((0xffffu) << 16) 72007e5967aSWarner Losh 721df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 722ba6c22ceSWarner Losh static void 723ba6c22ceSWarner Losh cam_iosched_io_metric_update(struct cam_iosched_softc *isc, 724ba6c22ceSWarner Losh sbintime_t sim_latency, int cmd, size_t size); 7255ede5b8cSWarner Losh #endif 726ba6c22ceSWarner Losh 7272d87718fSWarner Losh static inline bool 728ba6c22ceSWarner Losh cam_iosched_has_flagged_work(struct cam_iosched_softc *isc) 729ba6c22ceSWarner Losh { 730ba6c22ceSWarner Losh return !!(isc->flags & CAM_IOSCHED_FLAG_WORK_FLAGS); 731ba6c22ceSWarner Losh } 732ba6c22ceSWarner Losh 7332d87718fSWarner Losh static inline bool 734ba6c22ceSWarner Losh cam_iosched_has_io(struct cam_iosched_softc *isc) 735ba6c22ceSWarner Losh { 736df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 737035ec48eSWarner Losh if (do_dynamic_iosched) { 738ba6c22ceSWarner Losh struct bio *rbp = bioq_first(&isc->bio_queue); 739ba6c22ceSWarner Losh struct bio *wbp = bioq_first(&isc->write_queue); 7402d87718fSWarner Losh bool can_write = wbp != NULL && 741ba6c22ceSWarner Losh cam_iosched_limiter_caniop(&isc->write_stats, wbp) == 0; 7422d87718fSWarner Losh bool can_read = rbp != NULL && 743ba6c22ceSWarner Losh cam_iosched_limiter_caniop(&isc->read_stats, rbp) == 0; 744ba6c22ceSWarner Losh if (iosched_debug > 2) { 745ba6c22ceSWarner Losh printf("can write %d: pending_writes %d max_writes %d\n", can_write, isc->write_stats.pending, isc->write_stats.max); 746ba6c22ceSWarner Losh printf("can read %d: read_stats.pending %d max_reads %d\n", can_read, isc->read_stats.pending, isc->read_stats.max); 747ba6c22ceSWarner Losh printf("Queued reads %d writes %d\n", isc->read_stats.queued, isc->write_stats.queued); 748ba6c22ceSWarner Losh } 749ba6c22ceSWarner Losh return can_read || can_write; 750ba6c22ceSWarner Losh } 751ba6c22ceSWarner Losh #endif 752ba6c22ceSWarner Losh return bioq_first(&isc->bio_queue) != NULL; 753ba6c22ceSWarner Losh } 754ba6c22ceSWarner Losh 7552d87718fSWarner Losh static inline bool 756ba6c22ceSWarner Losh cam_iosched_has_more_trim(struct cam_iosched_softc *isc) 757ba6c22ceSWarner Losh { 758c6171b44SWarner Losh struct bio *bp; 759c6171b44SWarner Losh 760c6171b44SWarner Losh bp = bioq_first(&isc->trim_queue); 761c6171b44SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 762c6171b44SWarner Losh if (do_dynamic_iosched) { 763c6171b44SWarner Losh /* 764c6171b44SWarner Losh * If we're limiting trims, then defer action on trims 765c6171b44SWarner Losh * for a bit. 766c6171b44SWarner Losh */ 767c6171b44SWarner Losh if (bp == NULL || cam_iosched_limiter_caniop(&isc->trim_stats, bp) != 0) 768c6171b44SWarner Losh return false; 769c6171b44SWarner Losh } 770c6171b44SWarner Losh #endif 771d900ade5SWarner Losh 772d900ade5SWarner Losh /* 773d900ade5SWarner Losh * If we've set a trim_goal, then if we exceed that allow trims 774d900ade5SWarner Losh * to be passed back to the driver. If we've also set a tick timeout 775d900ade5SWarner Losh * allow trims back to the driver. Otherwise, don't allow trims yet. 776d900ade5SWarner Losh */ 777d900ade5SWarner Losh if (isc->trim_goal > 0) { 778d900ade5SWarner Losh if (isc->queued_trims >= isc->trim_goal) 779d900ade5SWarner Losh return true; 780d900ade5SWarner Losh if (isc->queued_trims > 0 && 781d900ade5SWarner Losh isc->trim_ticks > 0 && 782d900ade5SWarner Losh ticks - isc->last_trim_tick > isc->trim_ticks) 783d900ade5SWarner Losh return true; 784d900ade5SWarner Losh return false; 785d900ade5SWarner Losh } 786d900ade5SWarner Losh 787ece56614SWarner Losh /* NB: Should perhaps have a max trim active independent of I/O limiters */ 788ece56614SWarner Losh return !(isc->flags & CAM_IOSCHED_FLAG_TRIM_ACTIVE) && bp != NULL; 789ba6c22ceSWarner Losh } 790ba6c22ceSWarner Losh 791ba6c22ceSWarner Losh #define cam_iosched_sort_queue(isc) ((isc)->sort_io_queue >= 0 ? \ 792ba6c22ceSWarner Losh (isc)->sort_io_queue : cam_sort_io_queues) 793ba6c22ceSWarner Losh 794ba6c22ceSWarner Losh 7952d87718fSWarner Losh static inline bool 796ba6c22ceSWarner Losh cam_iosched_has_work(struct cam_iosched_softc *isc) 797ba6c22ceSWarner Losh { 798df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 799ba6c22ceSWarner Losh if (iosched_debug > 2) 800ba6c22ceSWarner Losh printf("has work: %d %d %d\n", cam_iosched_has_io(isc), 801ba6c22ceSWarner Losh cam_iosched_has_more_trim(isc), 802ba6c22ceSWarner Losh cam_iosched_has_flagged_work(isc)); 803ba6c22ceSWarner Losh #endif 804ba6c22ceSWarner Losh 805ba6c22ceSWarner Losh return cam_iosched_has_io(isc) || 806ba6c22ceSWarner Losh cam_iosched_has_more_trim(isc) || 807ba6c22ceSWarner Losh cam_iosched_has_flagged_work(isc); 808ba6c22ceSWarner Losh } 809ba6c22ceSWarner Losh 810df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 811ba6c22ceSWarner Losh static void 812ba6c22ceSWarner Losh cam_iosched_iop_stats_init(struct cam_iosched_softc *isc, struct iop_stats *ios) 813ba6c22ceSWarner Losh { 814ba6c22ceSWarner Losh 815ba6c22ceSWarner Losh ios->limiter = none; 816ba6c22ceSWarner Losh ios->in = 0; 81789d26636SWarner Losh ios->max = ios->current = 300000; 818ba6c22ceSWarner Losh ios->min = 1; 819ba6c22ceSWarner Losh ios->out = 0; 820c4b72d8bSWarner Losh ios->errs = 0; 821ba6c22ceSWarner Losh ios->pending = 0; 822ba6c22ceSWarner Losh ios->queued = 0; 823ba6c22ceSWarner Losh ios->total = 0; 824ba6c22ceSWarner Losh ios->ema = 0; 82579d80af2SWarner Losh ios->emvar = 0; 826ba6c22ceSWarner Losh ios->softc = isc; 82789d26636SWarner Losh cam_iosched_limiter_init(ios); 828ba6c22ceSWarner Losh } 829ba6c22ceSWarner Losh 830ba6c22ceSWarner Losh static int 831ba6c22ceSWarner Losh cam_iosched_limiter_sysctl(SYSCTL_HANDLER_ARGS) 832ba6c22ceSWarner Losh { 833ba6c22ceSWarner Losh char buf[16]; 834ba6c22ceSWarner Losh struct iop_stats *ios; 835ba6c22ceSWarner Losh struct cam_iosched_softc *isc; 836cf3ec151SWarner Losh int value, i, error; 837ba6c22ceSWarner Losh const char *p; 838ba6c22ceSWarner Losh 839ba6c22ceSWarner Losh ios = arg1; 840ba6c22ceSWarner Losh isc = ios->softc; 841ba6c22ceSWarner Losh value = ios->limiter; 842ba6c22ceSWarner Losh if (value < none || value >= limiter_max) 843ba6c22ceSWarner Losh p = "UNKNOWN"; 844ba6c22ceSWarner Losh else 845ba6c22ceSWarner Losh p = cam_iosched_limiter_names[value]; 846ba6c22ceSWarner Losh 847ba6c22ceSWarner Losh strlcpy(buf, p, sizeof(buf)); 848ba6c22ceSWarner Losh error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 849ba6c22ceSWarner Losh if (error != 0 || req->newptr == NULL) 850ba6c22ceSWarner Losh return error; 851ba6c22ceSWarner Losh 852ba6c22ceSWarner Losh cam_periph_lock(isc->periph); 853ba6c22ceSWarner Losh 854ba6c22ceSWarner Losh for (i = none; i < limiter_max; i++) { 855ba6c22ceSWarner Losh if (strcmp(buf, cam_iosched_limiter_names[i]) != 0) 856ba6c22ceSWarner Losh continue; 857ba6c22ceSWarner Losh ios->limiter = i; 858ba6c22ceSWarner Losh error = cam_iosched_limiter_init(ios); 859ba6c22ceSWarner Losh if (error != 0) { 860ba6c22ceSWarner Losh ios->limiter = value; 861ba6c22ceSWarner Losh cam_periph_unlock(isc->periph); 862ba6c22ceSWarner Losh return error; 863ba6c22ceSWarner Losh } 864cf3ec151SWarner Losh /* Note: disk load averate requires ticker to be always running */ 8653028dd8dSWarner Losh callout_reset(&isc->ticker, hz / isc->quanta, cam_iosched_ticker, isc); 866ba6c22ceSWarner Losh isc->flags |= CAM_IOSCHED_FLAG_CALLOUT_ACTIVE; 867ba6c22ceSWarner Losh 868ba6c22ceSWarner Losh cam_periph_unlock(isc->periph); 869ba6c22ceSWarner Losh return 0; 870ba6c22ceSWarner Losh } 871ba6c22ceSWarner Losh 872ba6c22ceSWarner Losh cam_periph_unlock(isc->periph); 873ba6c22ceSWarner Losh return EINVAL; 874ba6c22ceSWarner Losh } 875ba6c22ceSWarner Losh 876ba6c22ceSWarner Losh static int 877ba6c22ceSWarner Losh cam_iosched_control_type_sysctl(SYSCTL_HANDLER_ARGS) 878ba6c22ceSWarner Losh { 879ba6c22ceSWarner Losh char buf[16]; 880ba6c22ceSWarner Losh struct control_loop *clp; 881ba6c22ceSWarner Losh struct cam_iosched_softc *isc; 882ba6c22ceSWarner Losh int value, i, error; 883ba6c22ceSWarner Losh const char *p; 884ba6c22ceSWarner Losh 885ba6c22ceSWarner Losh clp = arg1; 886ba6c22ceSWarner Losh isc = clp->softc; 887ba6c22ceSWarner Losh value = clp->type; 888ba6c22ceSWarner Losh if (value < none || value >= cl_max) 889ba6c22ceSWarner Losh p = "UNKNOWN"; 890ba6c22ceSWarner Losh else 891ba6c22ceSWarner Losh p = cam_iosched_control_type_names[value]; 892ba6c22ceSWarner Losh 893ba6c22ceSWarner Losh strlcpy(buf, p, sizeof(buf)); 894ba6c22ceSWarner Losh error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 895ba6c22ceSWarner Losh if (error != 0 || req->newptr == NULL) 896ba6c22ceSWarner Losh return error; 897ba6c22ceSWarner Losh 898ba6c22ceSWarner Losh for (i = set_max; i < cl_max; i++) { 899ba6c22ceSWarner Losh if (strcmp(buf, cam_iosched_control_type_names[i]) != 0) 900ba6c22ceSWarner Losh continue; 901ba6c22ceSWarner Losh cam_periph_lock(isc->periph); 902ba6c22ceSWarner Losh clp->type = i; 903ba6c22ceSWarner Losh cam_periph_unlock(isc->periph); 904ba6c22ceSWarner Losh return 0; 905ba6c22ceSWarner Losh } 906ba6c22ceSWarner Losh 907ba6c22ceSWarner Losh return EINVAL; 908ba6c22ceSWarner Losh } 909ba6c22ceSWarner Losh 910ba6c22ceSWarner Losh static int 911ba6c22ceSWarner Losh cam_iosched_sbintime_sysctl(SYSCTL_HANDLER_ARGS) 912ba6c22ceSWarner Losh { 913ba6c22ceSWarner Losh char buf[16]; 914ba6c22ceSWarner Losh sbintime_t value; 915ba6c22ceSWarner Losh int error; 916ba6c22ceSWarner Losh uint64_t us; 917ba6c22ceSWarner Losh 918ba6c22ceSWarner Losh value = *(sbintime_t *)arg1; 919ba6c22ceSWarner Losh us = (uint64_t)value / SBT_1US; 920ba6c22ceSWarner Losh snprintf(buf, sizeof(buf), "%ju", (intmax_t)us); 921ba6c22ceSWarner Losh error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 922ba6c22ceSWarner Losh if (error != 0 || req->newptr == NULL) 923ba6c22ceSWarner Losh return error; 924ba6c22ceSWarner Losh us = strtoul(buf, NULL, 10); 925ba6c22ceSWarner Losh if (us == 0) 926ba6c22ceSWarner Losh return EINVAL; 927ba6c22ceSWarner Losh *(sbintime_t *)arg1 = us * SBT_1US; 928ba6c22ceSWarner Losh return 0; 929ba6c22ceSWarner Losh } 930ba6c22ceSWarner Losh 931cf3ec151SWarner Losh static int 932cf3ec151SWarner Losh cam_iosched_sysctl_latencies(SYSCTL_HANDLER_ARGS) 933cf3ec151SWarner Losh { 934cf3ec151SWarner Losh int i, error; 935cf3ec151SWarner Losh struct sbuf sb; 936cf3ec151SWarner Losh uint64_t *latencies; 937cf3ec151SWarner Losh 938cf3ec151SWarner Losh latencies = arg1; 939cf3ec151SWarner Losh sbuf_new_for_sysctl(&sb, NULL, LAT_BUCKETS * 16, req); 940cf3ec151SWarner Losh 941cf3ec151SWarner Losh for (i = 0; i < LAT_BUCKETS - 1; i++) 942cf3ec151SWarner Losh sbuf_printf(&sb, "%jd,", (intmax_t)latencies[i]); 943cf3ec151SWarner Losh sbuf_printf(&sb, "%jd", (intmax_t)latencies[LAT_BUCKETS - 1]); 944cf3ec151SWarner Losh error = sbuf_finish(&sb); 945cf3ec151SWarner Losh sbuf_delete(&sb); 946cf3ec151SWarner Losh 947cf3ec151SWarner Losh return (error); 948cf3ec151SWarner Losh } 949cf3ec151SWarner Losh 9502d22619aSWarner Losh static int 9512d22619aSWarner Losh cam_iosched_quanta_sysctl(SYSCTL_HANDLER_ARGS) 9522d22619aSWarner Losh { 9532d22619aSWarner Losh int *quanta; 9542d22619aSWarner Losh int error, value; 9552d22619aSWarner Losh 9562d22619aSWarner Losh quanta = (unsigned *)arg1; 9572d22619aSWarner Losh value = *quanta; 9582d22619aSWarner Losh 9592d22619aSWarner Losh error = sysctl_handle_int(oidp, (int *)&value, 0, req); 9602d22619aSWarner Losh if ((error != 0) || (req->newptr == NULL)) 9612d22619aSWarner Losh return (error); 9622d22619aSWarner Losh 9632d22619aSWarner Losh if (value < 1 || value > hz) 9642d22619aSWarner Losh return (EINVAL); 9652d22619aSWarner Losh 9662d22619aSWarner Losh *quanta = value; 9672d22619aSWarner Losh 9682d22619aSWarner Losh return (0); 9692d22619aSWarner Losh } 9702d22619aSWarner Losh 971ba6c22ceSWarner Losh static void 972ba6c22ceSWarner Losh cam_iosched_iop_stats_sysctl_init(struct cam_iosched_softc *isc, struct iop_stats *ios, char *name) 973ba6c22ceSWarner Losh { 974ba6c22ceSWarner Losh struct sysctl_oid_list *n; 975ba6c22ceSWarner Losh struct sysctl_ctx_list *ctx; 976ba6c22ceSWarner Losh 977ba6c22ceSWarner Losh ios->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx, 978ba6c22ceSWarner Losh SYSCTL_CHILDREN(isc->sysctl_tree), OID_AUTO, name, 979*7029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, 0, name); 980ba6c22ceSWarner Losh n = SYSCTL_CHILDREN(ios->sysctl_tree); 981ba6c22ceSWarner Losh ctx = &ios->sysctl_ctx; 982ba6c22ceSWarner Losh 983ba6c22ceSWarner Losh SYSCTL_ADD_UQUAD(ctx, n, 984ba6c22ceSWarner Losh OID_AUTO, "ema", CTLFLAG_RD, 985ba6c22ceSWarner Losh &ios->ema, 986ba6c22ceSWarner Losh "Fast Exponentially Weighted Moving Average"); 987ba6c22ceSWarner Losh SYSCTL_ADD_UQUAD(ctx, n, 98879d80af2SWarner Losh OID_AUTO, "emvar", CTLFLAG_RD, 98979d80af2SWarner Losh &ios->emvar, 99079d80af2SWarner Losh "Fast Exponentially Weighted Moving Variance"); 991ba6c22ceSWarner Losh 992ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 993ba6c22ceSWarner Losh OID_AUTO, "pending", CTLFLAG_RD, 994ba6c22ceSWarner Losh &ios->pending, 0, 995ba6c22ceSWarner Losh "Instantaneous # of pending transactions"); 996ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 997ba6c22ceSWarner Losh OID_AUTO, "count", CTLFLAG_RD, 998ba6c22ceSWarner Losh &ios->total, 0, 999ba6c22ceSWarner Losh "# of transactions submitted to hardware"); 1000ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1001ba6c22ceSWarner Losh OID_AUTO, "queued", CTLFLAG_RD, 1002ba6c22ceSWarner Losh &ios->queued, 0, 1003ba6c22ceSWarner Losh "# of transactions in the queue"); 1004ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1005ba6c22ceSWarner Losh OID_AUTO, "in", CTLFLAG_RD, 1006ba6c22ceSWarner Losh &ios->in, 0, 1007ba6c22ceSWarner Losh "# of transactions queued to driver"); 1008ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1009ba6c22ceSWarner Losh OID_AUTO, "out", CTLFLAG_RD, 1010ba6c22ceSWarner Losh &ios->out, 0, 1011c4b72d8bSWarner Losh "# of transactions completed (including with error)"); 1012c4b72d8bSWarner Losh SYSCTL_ADD_INT(ctx, n, 1013c4b72d8bSWarner Losh OID_AUTO, "errs", CTLFLAG_RD, 1014c4b72d8bSWarner Losh &ios->errs, 0, 1015c4b72d8bSWarner Losh "# of transactions completed with an error"); 1016ba6c22ceSWarner Losh 1017ba6c22ceSWarner Losh SYSCTL_ADD_PROC(ctx, n, 1018*7029da5cSPawel Biernacki OID_AUTO, "limiter", 1019*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1020ba6c22ceSWarner Losh ios, 0, cam_iosched_limiter_sysctl, "A", 1021ba6c22ceSWarner Losh "Current limiting type."); 1022ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1023ba6c22ceSWarner Losh OID_AUTO, "min", CTLFLAG_RW, 1024ba6c22ceSWarner Losh &ios->min, 0, 1025ba6c22ceSWarner Losh "min resource"); 1026ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1027ba6c22ceSWarner Losh OID_AUTO, "max", CTLFLAG_RW, 1028ba6c22ceSWarner Losh &ios->max, 0, 1029ba6c22ceSWarner Losh "max resource"); 1030ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1031ba6c22ceSWarner Losh OID_AUTO, "current", CTLFLAG_RW, 1032ba6c22ceSWarner Losh &ios->current, 0, 1033ba6c22ceSWarner Losh "current resource"); 1034ba6c22ceSWarner Losh 1035cf3ec151SWarner Losh SYSCTL_ADD_PROC(ctx, n, 1036*7029da5cSPawel Biernacki OID_AUTO, "latencies", 1037*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 1038cf3ec151SWarner Losh &ios->latencies, 0, 1039cf3ec151SWarner Losh cam_iosched_sysctl_latencies, "A", 1040cf3ec151SWarner Losh "Array of power of 2 latency from 1ms to 1.024s"); 1041ba6c22ceSWarner Losh } 1042ba6c22ceSWarner Losh 1043ba6c22ceSWarner Losh static void 1044ba6c22ceSWarner Losh cam_iosched_iop_stats_fini(struct iop_stats *ios) 1045ba6c22ceSWarner Losh { 1046ba6c22ceSWarner Losh if (ios->sysctl_tree) 1047ba6c22ceSWarner Losh if (sysctl_ctx_free(&ios->sysctl_ctx) != 0) 1048ba6c22ceSWarner Losh printf("can't remove iosched sysctl stats context\n"); 1049ba6c22ceSWarner Losh } 1050ba6c22ceSWarner Losh 1051ba6c22ceSWarner Losh static void 1052ba6c22ceSWarner Losh cam_iosched_cl_sysctl_init(struct cam_iosched_softc *isc) 1053ba6c22ceSWarner Losh { 1054ba6c22ceSWarner Losh struct sysctl_oid_list *n; 1055ba6c22ceSWarner Losh struct sysctl_ctx_list *ctx; 1056ba6c22ceSWarner Losh struct control_loop *clp; 1057ba6c22ceSWarner Losh 1058ba6c22ceSWarner Losh clp = &isc->cl; 1059ba6c22ceSWarner Losh clp->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx, 1060ba6c22ceSWarner Losh SYSCTL_CHILDREN(isc->sysctl_tree), OID_AUTO, "control", 1061*7029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "Control loop info"); 1062ba6c22ceSWarner Losh n = SYSCTL_CHILDREN(clp->sysctl_tree); 1063ba6c22ceSWarner Losh ctx = &clp->sysctl_ctx; 1064ba6c22ceSWarner Losh 1065ba6c22ceSWarner Losh SYSCTL_ADD_PROC(ctx, n, 1066*7029da5cSPawel Biernacki OID_AUTO, "type", 1067*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1068ba6c22ceSWarner Losh clp, 0, cam_iosched_control_type_sysctl, "A", 1069ba6c22ceSWarner Losh "Control loop algorithm"); 1070ba6c22ceSWarner Losh SYSCTL_ADD_PROC(ctx, n, 1071*7029da5cSPawel Biernacki OID_AUTO, "steer_interval", 1072*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1073ba6c22ceSWarner Losh &clp->steer_interval, 0, cam_iosched_sbintime_sysctl, "A", 1074ba6c22ceSWarner Losh "How often to steer (in us)"); 1075ba6c22ceSWarner Losh SYSCTL_ADD_PROC(ctx, n, 1076*7029da5cSPawel Biernacki OID_AUTO, "lolat", 1077*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1078ba6c22ceSWarner Losh &clp->lolat, 0, cam_iosched_sbintime_sysctl, "A", 1079ba6c22ceSWarner Losh "Low water mark for Latency (in us)"); 1080ba6c22ceSWarner Losh SYSCTL_ADD_PROC(ctx, n, 1081*7029da5cSPawel Biernacki OID_AUTO, "hilat", 1082*7029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1083ba6c22ceSWarner Losh &clp->hilat, 0, cam_iosched_sbintime_sysctl, "A", 1084ba6c22ceSWarner Losh "Hi water mark for Latency (in us)"); 1085ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1086ba6c22ceSWarner Losh OID_AUTO, "alpha", CTLFLAG_RW, 1087ba6c22ceSWarner Losh &clp->alpha, 0, 1088ba6c22ceSWarner Losh "Alpha for PLL (x100) aka gain"); 1089ba6c22ceSWarner Losh } 1090ba6c22ceSWarner Losh 1091ba6c22ceSWarner Losh static void 1092ba6c22ceSWarner Losh cam_iosched_cl_sysctl_fini(struct control_loop *clp) 1093ba6c22ceSWarner Losh { 1094ba6c22ceSWarner Losh if (clp->sysctl_tree) 1095ba6c22ceSWarner Losh if (sysctl_ctx_free(&clp->sysctl_ctx) != 0) 1096ba6c22ceSWarner Losh printf("can't remove iosched sysctl control loop context\n"); 1097ba6c22ceSWarner Losh } 1098ba6c22ceSWarner Losh #endif 1099ba6c22ceSWarner Losh 1100ba6c22ceSWarner Losh /* 1101ba6c22ceSWarner Losh * Allocate the iosched structure. This also insulates callers from knowing 1102ba6c22ceSWarner Losh * sizeof struct cam_iosched_softc. 1103ba6c22ceSWarner Losh */ 1104ba6c22ceSWarner Losh int 11050028abe6SWarner Losh cam_iosched_init(struct cam_iosched_softc **iscp, struct cam_periph *periph) 1106ba6c22ceSWarner Losh { 1107ba6c22ceSWarner Losh 1108ba6c22ceSWarner Losh *iscp = malloc(sizeof(**iscp), M_CAMSCHED, M_NOWAIT | M_ZERO); 1109ba6c22ceSWarner Losh if (*iscp == NULL) 1110ba6c22ceSWarner Losh return ENOMEM; 1111df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1112ba6c22ceSWarner Losh if (iosched_debug) 1113ba6c22ceSWarner Losh printf("CAM IOSCHEDULER Allocating entry at %p\n", *iscp); 1114ba6c22ceSWarner Losh #endif 1115ba6c22ceSWarner Losh (*iscp)->sort_io_queue = -1; 1116ba6c22ceSWarner Losh bioq_init(&(*iscp)->bio_queue); 1117ba6c22ceSWarner Losh bioq_init(&(*iscp)->trim_queue); 1118df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1119035ec48eSWarner Losh if (do_dynamic_iosched) { 1120ba6c22ceSWarner Losh bioq_init(&(*iscp)->write_queue); 1121ba6c22ceSWarner Losh (*iscp)->read_bias = 100; 1122ba6c22ceSWarner Losh (*iscp)->current_read_bias = 100; 1123d7fa1ab0SWarner Losh (*iscp)->quanta = min(hz, 200); 1124ba6c22ceSWarner Losh cam_iosched_iop_stats_init(*iscp, &(*iscp)->read_stats); 1125ba6c22ceSWarner Losh cam_iosched_iop_stats_init(*iscp, &(*iscp)->write_stats); 1126ba6c22ceSWarner Losh cam_iosched_iop_stats_init(*iscp, &(*iscp)->trim_stats); 1127ba6c22ceSWarner Losh (*iscp)->trim_stats.max = 1; /* Trims are special: one at a time for now */ 1128ba6c22ceSWarner Losh (*iscp)->last_time = sbinuptime(); 1129ba6c22ceSWarner Losh callout_init_mtx(&(*iscp)->ticker, cam_periph_mtx(periph), 0); 1130ba6c22ceSWarner Losh (*iscp)->periph = periph; 1131ba6c22ceSWarner Losh cam_iosched_cl_init(&(*iscp)->cl, *iscp); 11323028dd8dSWarner Losh callout_reset(&(*iscp)->ticker, hz / (*iscp)->quanta, cam_iosched_ticker, *iscp); 1133ba6c22ceSWarner Losh (*iscp)->flags |= CAM_IOSCHED_FLAG_CALLOUT_ACTIVE; 1134ba6c22ceSWarner Losh } 1135ba6c22ceSWarner Losh #endif 1136ba6c22ceSWarner Losh 1137ba6c22ceSWarner Losh return 0; 1138ba6c22ceSWarner Losh } 1139ba6c22ceSWarner Losh 1140ba6c22ceSWarner Losh /* 1141ba6c22ceSWarner Losh * Reclaim all used resources. This assumes that other folks have 1142ba6c22ceSWarner Losh * drained the requests in the hardware. Maybe an unwise assumption. 1143ba6c22ceSWarner Losh */ 1144ba6c22ceSWarner Losh void 1145ba6c22ceSWarner Losh cam_iosched_fini(struct cam_iosched_softc *isc) 1146ba6c22ceSWarner Losh { 1147ba6c22ceSWarner Losh if (isc) { 1148ba6c22ceSWarner Losh cam_iosched_flush(isc, NULL, ENXIO); 1149df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1150ba6c22ceSWarner Losh cam_iosched_iop_stats_fini(&isc->read_stats); 1151ba6c22ceSWarner Losh cam_iosched_iop_stats_fini(&isc->write_stats); 1152ba6c22ceSWarner Losh cam_iosched_iop_stats_fini(&isc->trim_stats); 1153ba6c22ceSWarner Losh cam_iosched_cl_sysctl_fini(&isc->cl); 1154ba6c22ceSWarner Losh if (isc->sysctl_tree) 1155ba6c22ceSWarner Losh if (sysctl_ctx_free(&isc->sysctl_ctx) != 0) 1156ba6c22ceSWarner Losh printf("can't remove iosched sysctl stats context\n"); 1157ba6c22ceSWarner Losh if (isc->flags & CAM_IOSCHED_FLAG_CALLOUT_ACTIVE) { 1158ba6c22ceSWarner Losh callout_drain(&isc->ticker); 1159ba6c22ceSWarner Losh isc->flags &= ~ CAM_IOSCHED_FLAG_CALLOUT_ACTIVE; 1160ba6c22ceSWarner Losh } 1161ba6c22ceSWarner Losh #endif 1162ba6c22ceSWarner Losh free(isc, M_CAMSCHED); 1163ba6c22ceSWarner Losh } 1164ba6c22ceSWarner Losh } 1165ba6c22ceSWarner Losh 1166ba6c22ceSWarner Losh /* 1167ba6c22ceSWarner Losh * After we're sure we're attaching a device, go ahead and add 1168ba6c22ceSWarner Losh * hooks for any sysctl we may wish to honor. 1169ba6c22ceSWarner Losh */ 1170ba6c22ceSWarner Losh void cam_iosched_sysctl_init(struct cam_iosched_softc *isc, 1171ba6c22ceSWarner Losh struct sysctl_ctx_list *ctx, struct sysctl_oid *node) 1172ba6c22ceSWarner Losh { 1173ba6c22ceSWarner Losh struct sysctl_oid_list *n; 1174ba6c22ceSWarner Losh 1175d900ade5SWarner Losh n = SYSCTL_CHILDREN(node); 1176d900ade5SWarner Losh SYSCTL_ADD_INT(ctx, n, 1177ba6c22ceSWarner Losh OID_AUTO, "sort_io_queue", CTLFLAG_RW | CTLFLAG_MPSAFE, 1178ba6c22ceSWarner Losh &isc->sort_io_queue, 0, 1179ba6c22ceSWarner Losh "Sort IO queue to try and optimise disk access patterns"); 1180d900ade5SWarner Losh SYSCTL_ADD_INT(ctx, n, 1181d900ade5SWarner Losh OID_AUTO, "trim_goal", CTLFLAG_RW, 1182d900ade5SWarner Losh &isc->trim_goal, 0, 1183d900ade5SWarner Losh "Number of trims to try to accumulate before sending to hardware"); 1184d900ade5SWarner Losh SYSCTL_ADD_INT(ctx, n, 1185d900ade5SWarner Losh OID_AUTO, "trim_ticks", CTLFLAG_RW, 1186d900ade5SWarner Losh &isc->trim_goal, 0, 1187d900ade5SWarner Losh "IO Schedul qaunta to hold back trims for when accumulating"); 1188ba6c22ceSWarner Losh 1189df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1190035ec48eSWarner Losh if (!do_dynamic_iosched) 1191ba6c22ceSWarner Losh return; 1192ba6c22ceSWarner Losh 1193ba6c22ceSWarner Losh isc->sysctl_tree = SYSCTL_ADD_NODE(&isc->sysctl_ctx, 1194ba6c22ceSWarner Losh SYSCTL_CHILDREN(node), OID_AUTO, "iosched", 1195*7029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "I/O scheduler statistics"); 1196ba6c22ceSWarner Losh n = SYSCTL_CHILDREN(isc->sysctl_tree); 1197ba6c22ceSWarner Losh ctx = &isc->sysctl_ctx; 1198ba6c22ceSWarner Losh 1199ba6c22ceSWarner Losh cam_iosched_iop_stats_sysctl_init(isc, &isc->read_stats, "read"); 1200ba6c22ceSWarner Losh cam_iosched_iop_stats_sysctl_init(isc, &isc->write_stats, "write"); 1201ba6c22ceSWarner Losh cam_iosched_iop_stats_sysctl_init(isc, &isc->trim_stats, "trim"); 1202ba6c22ceSWarner Losh cam_iosched_cl_sysctl_init(isc); 1203ba6c22ceSWarner Losh 1204ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1205ba6c22ceSWarner Losh OID_AUTO, "read_bias", CTLFLAG_RW, 1206ba6c22ceSWarner Losh &isc->read_bias, 100, 1207ba6c22ceSWarner Losh "How biased towards read should we be independent of limits"); 1208ba6c22ceSWarner Losh 12092d22619aSWarner Losh SYSCTL_ADD_PROC(ctx, n, 1210*7029da5cSPawel Biernacki OID_AUTO, "quanta", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 12112d22619aSWarner Losh &isc->quanta, 0, cam_iosched_quanta_sysctl, "I", 1212ba6c22ceSWarner Losh "How many quanta per second do we slice the I/O up into"); 1213ba6c22ceSWarner Losh 1214ba6c22ceSWarner Losh SYSCTL_ADD_INT(ctx, n, 1215ba6c22ceSWarner Losh OID_AUTO, "total_ticks", CTLFLAG_RD, 1216ba6c22ceSWarner Losh &isc->total_ticks, 0, 1217ba6c22ceSWarner Losh "Total number of ticks we've done"); 1218cf3ec151SWarner Losh 1219cf3ec151SWarner Losh SYSCTL_ADD_INT(ctx, n, 1220cf3ec151SWarner Losh OID_AUTO, "load", CTLFLAG_RD, 1221cf3ec151SWarner Losh &isc->load, 0, 1222cf3ec151SWarner Losh "scaled load average / 100"); 1223e5436ab5SWarner Losh 1224e5436ab5SWarner Losh SYSCTL_ADD_U64(ctx, n, 1225e5436ab5SWarner Losh OID_AUTO, "latency_trigger", CTLFLAG_RW, 1226e5436ab5SWarner Losh &isc->max_lat, 0, 1227e5436ab5SWarner Losh "Latency treshold to trigger callbacks"); 1228e5436ab5SWarner Losh #endif 1229e5436ab5SWarner Losh } 1230e5436ab5SWarner Losh 1231e5436ab5SWarner Losh void 1232e5436ab5SWarner Losh cam_iosched_set_latfcn(struct cam_iosched_softc *isc, 1233e5436ab5SWarner Losh cam_iosched_latfcn_t fnp, void *argp) 1234e5436ab5SWarner Losh { 1235e5436ab5SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1236e5436ab5SWarner Losh isc->latfcn = fnp; 1237e5436ab5SWarner Losh isc->latarg = argp; 1238ba6c22ceSWarner Losh #endif 1239ba6c22ceSWarner Losh } 1240ba6c22ceSWarner Losh 1241ba6c22ceSWarner Losh /* 1242d900ade5SWarner Losh * Client drivers can set two parameters. "goal" is the number of BIO_DELETEs 1243d900ade5SWarner Losh * that will be queued up before iosched will "release" the trims to the client 1244d900ade5SWarner Losh * driver to wo with what they will (usually combine as many as possible). If we 1245d900ade5SWarner Losh * don't get this many, after trim_ticks we'll submit the I/O anyway with 1246d900ade5SWarner Losh * whatever we have. We do need an I/O of some kind of to clock the deferred 1247d900ade5SWarner Losh * trims out to disk. Since we will eventually get a write for the super block 1248d900ade5SWarner Losh * or something before we shutdown, the trims will complete. To be safe, when a 1249d900ade5SWarner Losh * BIO_FLUSH is presented to the iosched work queue, we set the ticks time far 1250d900ade5SWarner Losh * enough in the past so we'll present the BIO_DELETEs to the client driver. 1251d900ade5SWarner Losh * There might be a race if no BIO_DELETESs were queued, a BIO_FLUSH comes in 1252d900ade5SWarner Losh * and then a BIO_DELETE is sent down. No know client does this, and there's 1253d900ade5SWarner Losh * already a race between an ordered BIO_FLUSH and any BIO_DELETEs in flight, 1254d900ade5SWarner Losh * but no client depends on the ordering being honored. 1255d900ade5SWarner Losh * 1256d900ade5SWarner Losh * XXX I'm not sure what the interaction between UFS direct BIOs and the BUF 1257d900ade5SWarner Losh * flushing on shutdown. I think there's bufs that would be dependent on the BIO 1258d900ade5SWarner Losh * finishing to write out at least metadata, so we'll be fine. To be safe, keep 1259d900ade5SWarner Losh * the number of ticks low (less than maybe 10s) to avoid shutdown races. 1260d900ade5SWarner Losh */ 1261d900ade5SWarner Losh 1262d900ade5SWarner Losh void 1263d900ade5SWarner Losh cam_iosched_set_trim_goal(struct cam_iosched_softc *isc, int goal) 1264d900ade5SWarner Losh { 1265d900ade5SWarner Losh 1266d900ade5SWarner Losh isc->trim_goal = goal; 1267d900ade5SWarner Losh } 1268d900ade5SWarner Losh 1269d900ade5SWarner Losh void 1270d900ade5SWarner Losh cam_iosched_set_trim_ticks(struct cam_iosched_softc *isc, int trim_ticks) 1271d900ade5SWarner Losh { 1272d900ade5SWarner Losh 1273d900ade5SWarner Losh isc->trim_ticks = trim_ticks; 1274d900ade5SWarner Losh } 1275d900ade5SWarner Losh 1276d900ade5SWarner Losh /* 1277ba6c22ceSWarner Losh * Flush outstanding I/O. Consumers of this library don't know all the 1278ba6c22ceSWarner Losh * queues we may keep, so this allows all I/O to be flushed in one 1279ba6c22ceSWarner Losh * convenient call. 1280ba6c22ceSWarner Losh */ 1281ba6c22ceSWarner Losh void 1282ba6c22ceSWarner Losh cam_iosched_flush(struct cam_iosched_softc *isc, struct devstat *stp, int err) 1283ba6c22ceSWarner Losh { 1284ba6c22ceSWarner Losh bioq_flush(&isc->bio_queue, stp, err); 1285ba6c22ceSWarner Losh bioq_flush(&isc->trim_queue, stp, err); 1286df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1287035ec48eSWarner Losh if (do_dynamic_iosched) 1288ba6c22ceSWarner Losh bioq_flush(&isc->write_queue, stp, err); 1289ba6c22ceSWarner Losh #endif 1290ba6c22ceSWarner Losh } 1291ba6c22ceSWarner Losh 1292df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1293ba6c22ceSWarner Losh static struct bio * 12940028abe6SWarner Losh cam_iosched_get_write(struct cam_iosched_softc *isc) 1295ba6c22ceSWarner Losh { 1296ba6c22ceSWarner Losh struct bio *bp; 1297ba6c22ceSWarner Losh 1298ba6c22ceSWarner Losh /* 1299ba6c22ceSWarner Losh * We control the write rate by controlling how many requests we send 1300ba6c22ceSWarner Losh * down to the drive at any one time. Fewer requests limits the 1301ba6c22ceSWarner Losh * effects of both starvation when the requests take a while and write 1302ba6c22ceSWarner Losh * amplification when each request is causing more than one write to 1303ba6c22ceSWarner Losh * the NAND media. Limiting the queue depth like this will also limit 1304ba6c22ceSWarner Losh * the write throughput and give and reads that want to compete to 1305ba6c22ceSWarner Losh * compete unfairly. 1306ba6c22ceSWarner Losh */ 1307ba6c22ceSWarner Losh bp = bioq_first(&isc->write_queue); 1308ba6c22ceSWarner Losh if (bp == NULL) { 1309ba6c22ceSWarner Losh if (iosched_debug > 3) 1310ba6c22ceSWarner Losh printf("No writes present in write_queue\n"); 1311ba6c22ceSWarner Losh return NULL; 1312ba6c22ceSWarner Losh } 1313ba6c22ceSWarner Losh 1314ba6c22ceSWarner Losh /* 1315ba6c22ceSWarner Losh * If pending read, prefer that based on current read bias 1316ba6c22ceSWarner Losh * setting. 1317ba6c22ceSWarner Losh */ 1318ba6c22ceSWarner Losh if (bioq_first(&isc->bio_queue) && isc->current_read_bias) { 1319ba6c22ceSWarner Losh if (iosched_debug) 1320f2b98850SWarner Losh printf( 1321f2b98850SWarner Losh "Reads present and current_read_bias is %d queued " 1322f2b98850SWarner Losh "writes %d queued reads %d\n", 1323f2b98850SWarner Losh isc->current_read_bias, isc->write_stats.queued, 1324f2b98850SWarner Losh isc->read_stats.queued); 1325ba6c22ceSWarner Losh isc->current_read_bias--; 1326cf3ec151SWarner Losh /* We're not limiting writes, per se, just doing reads first */ 1327ba6c22ceSWarner Losh return NULL; 1328ba6c22ceSWarner Losh } 1329ba6c22ceSWarner Losh 1330ba6c22ceSWarner Losh /* 1331ba6c22ceSWarner Losh * See if our current limiter allows this I/O. 1332ba6c22ceSWarner Losh */ 1333ba6c22ceSWarner Losh if (cam_iosched_limiter_iop(&isc->write_stats, bp) != 0) { 1334ba6c22ceSWarner Losh if (iosched_debug) 1335ba6c22ceSWarner Losh printf("Can't write because limiter says no.\n"); 1336cf3ec151SWarner Losh isc->write_stats.state_flags |= IOP_RATE_LIMITED; 1337ba6c22ceSWarner Losh return NULL; 1338ba6c22ceSWarner Losh } 1339ba6c22ceSWarner Losh 1340ba6c22ceSWarner Losh /* 1341ba6c22ceSWarner Losh * Let's do this: We've passed all the gates and we're a go 1342ba6c22ceSWarner Losh * to schedule the I/O in the SIM. 1343ba6c22ceSWarner Losh */ 1344ba6c22ceSWarner Losh isc->current_read_bias = isc->read_bias; 1345ba6c22ceSWarner Losh bioq_remove(&isc->write_queue, bp); 1346ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_WRITE) { 1347ba6c22ceSWarner Losh isc->write_stats.queued--; 1348ba6c22ceSWarner Losh isc->write_stats.total++; 1349ba6c22ceSWarner Losh isc->write_stats.pending++; 1350ba6c22ceSWarner Losh } 1351ba6c22ceSWarner Losh if (iosched_debug > 9) 1352ba6c22ceSWarner Losh printf("HWQ : %p %#x\n", bp, bp->bio_cmd); 1353cf3ec151SWarner Losh isc->write_stats.state_flags &= ~IOP_RATE_LIMITED; 1354ba6c22ceSWarner Losh return bp; 1355ba6c22ceSWarner Losh } 1356ba6c22ceSWarner Losh #endif 1357ba6c22ceSWarner Losh 1358ba6c22ceSWarner Losh /* 1359ba6c22ceSWarner Losh * Put back a trim that you weren't able to actually schedule this time. 1360ba6c22ceSWarner Losh */ 1361ba6c22ceSWarner Losh void 1362ba6c22ceSWarner Losh cam_iosched_put_back_trim(struct cam_iosched_softc *isc, struct bio *bp) 1363ba6c22ceSWarner Losh { 1364ba6c22ceSWarner Losh bioq_insert_head(&isc->trim_queue, bp); 1365d900ade5SWarner Losh if (isc->queued_trims == 0) 1366d900ade5SWarner Losh isc->last_trim_tick = ticks; 1367d900ade5SWarner Losh isc->queued_trims++; 1368df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1369ba6c22ceSWarner Losh isc->trim_stats.queued++; 1370ba6c22ceSWarner Losh isc->trim_stats.total--; /* since we put it back, don't double count */ 1371ba6c22ceSWarner Losh isc->trim_stats.pending--; 1372ba6c22ceSWarner Losh #endif 1373ba6c22ceSWarner Losh } 1374ba6c22ceSWarner Losh 1375ba6c22ceSWarner Losh /* 1376ba6c22ceSWarner Losh * gets the next trim from the trim queue. 1377ba6c22ceSWarner Losh * 1378ba6c22ceSWarner Losh * Assumes we're called with the periph lock held. It removes this 13790b4060b0SEd Maste * trim from the queue and the device must explicitly reinsert it 1380ba6c22ceSWarner Losh * should the need arise. 1381ba6c22ceSWarner Losh */ 1382ba6c22ceSWarner Losh struct bio * 1383ba6c22ceSWarner Losh cam_iosched_next_trim(struct cam_iosched_softc *isc) 1384ba6c22ceSWarner Losh { 1385ba6c22ceSWarner Losh struct bio *bp; 1386ba6c22ceSWarner Losh 1387ba6c22ceSWarner Losh bp = bioq_first(&isc->trim_queue); 1388ba6c22ceSWarner Losh if (bp == NULL) 1389ba6c22ceSWarner Losh return NULL; 1390ba6c22ceSWarner Losh bioq_remove(&isc->trim_queue, bp); 1391d900ade5SWarner Losh isc->queued_trims--; 1392d900ade5SWarner Losh isc->last_trim_tick = ticks; /* Reset the tick timer when we take trims */ 1393df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1394ba6c22ceSWarner Losh isc->trim_stats.queued--; 1395ba6c22ceSWarner Losh isc->trim_stats.total++; 1396ba6c22ceSWarner Losh isc->trim_stats.pending++; 1397ba6c22ceSWarner Losh #endif 1398ba6c22ceSWarner Losh return bp; 1399ba6c22ceSWarner Losh } 1400ba6c22ceSWarner Losh 1401ba6c22ceSWarner Losh /* 14020b4060b0SEd Maste * gets an available trim from the trim queue, if there's no trim 1403ba6c22ceSWarner Losh * already pending. It removes this trim from the queue and the device 14040b4060b0SEd Maste * must explicitly reinsert it should the need arise. 1405ba6c22ceSWarner Losh * 1406ba6c22ceSWarner Losh * Assumes we're called with the periph lock held. 1407ba6c22ceSWarner Losh */ 14080028abe6SWarner Losh struct bio * 14090028abe6SWarner Losh cam_iosched_get_trim(struct cam_iosched_softc *isc) 1410ba6c22ceSWarner Losh { 1411c6171b44SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1412c6171b44SWarner Losh struct bio *bp; 1413c6171b44SWarner Losh #endif 1414ba6c22ceSWarner Losh 14150028abe6SWarner Losh if (!cam_iosched_has_more_trim(isc)) 1416ba6c22ceSWarner Losh return NULL; 141762c94a05SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1418c6171b44SWarner Losh bp = bioq_first(&isc->trim_queue); 1419c6171b44SWarner Losh if (bp == NULL) 1420c6171b44SWarner Losh return NULL; 1421c6171b44SWarner Losh 142262c94a05SWarner Losh /* 14231759fd77SWarner Losh * If pending read, prefer that based on current read bias setting. The 14241759fd77SWarner Losh * read bias is shared for both writes and TRIMs, but on TRIMs the bias 14251759fd77SWarner Losh * is for a combined TRIM not a single TRIM request that's come in. 142662c94a05SWarner Losh */ 14271759fd77SWarner Losh if (do_dynamic_iosched) { 142862c94a05SWarner Losh if (bioq_first(&isc->bio_queue) && isc->current_read_bias) { 14291759fd77SWarner Losh if (iosched_debug) 14301759fd77SWarner Losh printf("Reads present and current_read_bias is %d" 14311759fd77SWarner Losh " queued trims %d queued reads %d\n", 14321759fd77SWarner Losh isc->current_read_bias, isc->trim_stats.queued, 14331759fd77SWarner Losh isc->read_stats.queued); 143462c94a05SWarner Losh isc->current_read_bias--; 143562c94a05SWarner Losh /* We're not limiting TRIMS, per se, just doing reads first */ 143662c94a05SWarner Losh return NULL; 143762c94a05SWarner Losh } 143862c94a05SWarner Losh /* 143962c94a05SWarner Losh * We're going to do a trim, so reset the bias. 144062c94a05SWarner Losh */ 144162c94a05SWarner Losh isc->current_read_bias = isc->read_bias; 144262c94a05SWarner Losh } 1443c6171b44SWarner Losh 1444c6171b44SWarner Losh /* 1445c6171b44SWarner Losh * See if our current limiter allows this I/O. Because we only call this 1446c6171b44SWarner Losh * here, and not in next_trim, the 'bandwidth' limits for trims won't 1447c6171b44SWarner Losh * work, while the iops or max queued limits will work. It's tricky 1448c6171b44SWarner Losh * because we want the limits to be from the perspective of the 1449c6171b44SWarner Losh * "commands sent to the device." To make iops work, we need to check 1450c6171b44SWarner Losh * only here (since we want all the ops we combine to count as one). To 1451c6171b44SWarner Losh * make bw limits work, we'd need to check in next_trim, but that would 1452c6171b44SWarner Losh * have the effect of limiting the iops as seen from the upper layers. 1453c6171b44SWarner Losh */ 1454c6171b44SWarner Losh if (cam_iosched_limiter_iop(&isc->trim_stats, bp) != 0) { 1455c6171b44SWarner Losh if (iosched_debug) 1456c6171b44SWarner Losh printf("Can't trim because limiter says no.\n"); 1457c6171b44SWarner Losh isc->trim_stats.state_flags |= IOP_RATE_LIMITED; 1458c6171b44SWarner Losh return NULL; 1459c6171b44SWarner Losh } 1460c6171b44SWarner Losh isc->current_read_bias = isc->read_bias; 1461c6171b44SWarner Losh isc->trim_stats.state_flags &= ~IOP_RATE_LIMITED; 1462c6171b44SWarner Losh /* cam_iosched_next_trim below keeps proper book */ 146362c94a05SWarner Losh #endif 1464ba6c22ceSWarner Losh return cam_iosched_next_trim(isc); 1465ba6c22ceSWarner Losh } 1466ba6c22ceSWarner Losh 1467ba6c22ceSWarner Losh /* 1468ba6c22ceSWarner Losh * Determine what the next bit of work to do is for the periph. The 1469ba6c22ceSWarner Losh * default implementation looks to see if we have trims to do, but no 1470ba6c22ceSWarner Losh * trims outstanding. If so, we do that. Otherwise we see if we have 1471ba6c22ceSWarner Losh * other work. If we do, then we do that. Otherwise why were we called? 1472ba6c22ceSWarner Losh */ 1473ba6c22ceSWarner Losh struct bio * 1474ba6c22ceSWarner Losh cam_iosched_next_bio(struct cam_iosched_softc *isc) 1475ba6c22ceSWarner Losh { 1476ba6c22ceSWarner Losh struct bio *bp; 1477ba6c22ceSWarner Losh 1478ba6c22ceSWarner Losh /* 1479ba6c22ceSWarner Losh * See if we have a trim that can be scheduled. We can only send one 14800028abe6SWarner Losh * at a time down, so this takes that into account. 14810028abe6SWarner Losh * 14820028abe6SWarner Losh * XXX newer TRIM commands are queueable. Revisit this when we 14830028abe6SWarner Losh * implement them. 1484ba6c22ceSWarner Losh */ 14850028abe6SWarner Losh if ((bp = cam_iosched_get_trim(isc)) != NULL) 1486ba6c22ceSWarner Losh return bp; 1487ba6c22ceSWarner Losh 1488df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1489ba6c22ceSWarner Losh /* 1490ba6c22ceSWarner Losh * See if we have any pending writes, and room in the queue for them, 1491ba6c22ceSWarner Losh * and if so, those are next. 1492ba6c22ceSWarner Losh */ 1493035ec48eSWarner Losh if (do_dynamic_iosched) { 14940028abe6SWarner Losh if ((bp = cam_iosched_get_write(isc)) != NULL) 1495ba6c22ceSWarner Losh return bp; 1496ba6c22ceSWarner Losh } 1497ba6c22ceSWarner Losh #endif 1498ba6c22ceSWarner Losh 1499ba6c22ceSWarner Losh /* 1500ba6c22ceSWarner Losh * next, see if there's other, normal I/O waiting. If so return that. 1501ba6c22ceSWarner Losh */ 1502ba6c22ceSWarner Losh if ((bp = bioq_first(&isc->bio_queue)) == NULL) 1503ba6c22ceSWarner Losh return NULL; 1504ba6c22ceSWarner Losh 1505df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1506ba6c22ceSWarner Losh /* 1507cf3ec151SWarner Losh * For the dynamic scheduler, bio_queue is only for reads, so enforce 1508ba6c22ceSWarner Losh * the limits here. Enforce only for reads. 1509ba6c22ceSWarner Losh */ 1510035ec48eSWarner Losh if (do_dynamic_iosched) { 1511ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_READ && 1512cf3ec151SWarner Losh cam_iosched_limiter_iop(&isc->read_stats, bp) != 0) { 1513cf3ec151SWarner Losh isc->read_stats.state_flags |= IOP_RATE_LIMITED; 1514ba6c22ceSWarner Losh return NULL; 1515ba6c22ceSWarner Losh } 1516cf3ec151SWarner Losh } 1517cf3ec151SWarner Losh isc->read_stats.state_flags &= ~IOP_RATE_LIMITED; 1518ba6c22ceSWarner Losh #endif 1519ba6c22ceSWarner Losh bioq_remove(&isc->bio_queue, bp); 1520df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1521035ec48eSWarner Losh if (do_dynamic_iosched) { 1522ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_READ) { 1523ba6c22ceSWarner Losh isc->read_stats.queued--; 1524ba6c22ceSWarner Losh isc->read_stats.total++; 1525ba6c22ceSWarner Losh isc->read_stats.pending++; 1526ba6c22ceSWarner Losh } else 1527ba6c22ceSWarner Losh printf("Found bio_cmd = %#x\n", bp->bio_cmd); 1528ba6c22ceSWarner Losh } 1529ba6c22ceSWarner Losh if (iosched_debug > 9) 1530ba6c22ceSWarner Losh printf("HWQ : %p %#x\n", bp, bp->bio_cmd); 1531ba6c22ceSWarner Losh #endif 1532ba6c22ceSWarner Losh return bp; 1533ba6c22ceSWarner Losh } 1534ba6c22ceSWarner Losh 1535ba6c22ceSWarner Losh /* 1536ba6c22ceSWarner Losh * Driver has been given some work to do by the block layer. Tell the 1537ba6c22ceSWarner Losh * scheduler about it and have it queue the work up. The scheduler module 1538ba6c22ceSWarner Losh * will then return the currently most useful bit of work later, possibly 1539ba6c22ceSWarner Losh * deferring work for various reasons. 1540ba6c22ceSWarner Losh */ 1541ba6c22ceSWarner Losh void 1542ba6c22ceSWarner Losh cam_iosched_queue_work(struct cam_iosched_softc *isc, struct bio *bp) 1543ba6c22ceSWarner Losh { 1544ba6c22ceSWarner Losh 1545ba6c22ceSWarner Losh /* 15460d83f8dcSWarner Losh * A BIO_SPEEDUP from the uppper layers means that they have a block 15470d83f8dcSWarner Losh * shortage. At the present, this is only sent when we're trying to 15480d83f8dcSWarner Losh * allocate blocks, but have a shortage before giving up. bio_length is 15490d83f8dcSWarner Losh * the size of their shortage. We will complete just enough BIO_DELETEs 15500d83f8dcSWarner Losh * in the queue to satisfy the need. If bio_length is 0, we'll complete 15510d83f8dcSWarner Losh * them all. This allows the scheduler to delay BIO_DELETEs to improve 15520d83f8dcSWarner Losh * read/write performance without worrying about the upper layers. When 15530d83f8dcSWarner Losh * it's possibly a problem, we respond by pretending the BIO_DELETEs 15540d83f8dcSWarner Losh * just worked. We can't do anything about the BIO_DELETEs in the 15550d83f8dcSWarner Losh * hardware, though. We have to wait for them to complete. 15560d83f8dcSWarner Losh */ 15570d83f8dcSWarner Losh if (bp->bio_cmd == BIO_SPEEDUP) { 15580d83f8dcSWarner Losh off_t len; 15590d83f8dcSWarner Losh struct bio *nbp; 15600d83f8dcSWarner Losh 15610d83f8dcSWarner Losh len = 0; 15620d83f8dcSWarner Losh while (bioq_first(&isc->trim_queue) && 15630d83f8dcSWarner Losh (bp->bio_length == 0 || len < bp->bio_length)) { 15640d83f8dcSWarner Losh nbp = bioq_takefirst(&isc->trim_queue); 15650d83f8dcSWarner Losh len += nbp->bio_length; 15660d83f8dcSWarner Losh nbp->bio_error = 0; 15670d83f8dcSWarner Losh biodone(nbp); 15680d83f8dcSWarner Losh } 15690d83f8dcSWarner Losh if (bp->bio_length > 0) { 15700d83f8dcSWarner Losh if (bp->bio_length > len) 15710d83f8dcSWarner Losh bp->bio_resid = bp->bio_length - len; 15720d83f8dcSWarner Losh else 15730d83f8dcSWarner Losh bp->bio_resid = 0; 15740d83f8dcSWarner Losh } 15750d83f8dcSWarner Losh bp->bio_error = 0; 15760d83f8dcSWarner Losh biodone(bp); 15770d83f8dcSWarner Losh return; 15780d83f8dcSWarner Losh } 15790d83f8dcSWarner Losh 15800d83f8dcSWarner Losh /* 1581d900ade5SWarner Losh * If we get a BIO_FLUSH, and we're doing delayed BIO_DELETEs then we 1582d900ade5SWarner Losh * set the last tick time to one less than the current ticks minus the 1583d900ade5SWarner Losh * delay to force the BIO_DELETEs to be presented to the client driver. 1584d900ade5SWarner Losh */ 1585d900ade5SWarner Losh if (bp->bio_cmd == BIO_FLUSH && isc->trim_ticks > 0) 1586d900ade5SWarner Losh isc->last_trim_tick = ticks - isc->trim_ticks - 1; 1587d900ade5SWarner Losh 1588d900ade5SWarner Losh /* 1589d900ade5SWarner Losh * Put all trims on the trim queue. Otherwise put the work on the bio 1590d900ade5SWarner Losh * queue. 1591ba6c22ceSWarner Losh */ 1592ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_DELETE) { 159397f8aa05SWarner Losh bioq_insert_tail(&isc->trim_queue, bp); 1594d900ade5SWarner Losh if (isc->queued_trims == 0) 1595d900ade5SWarner Losh isc->last_trim_tick = ticks; 1596d900ade5SWarner Losh isc->queued_trims++; 1597df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1598ba6c22ceSWarner Losh isc->trim_stats.in++; 1599ba6c22ceSWarner Losh isc->trim_stats.queued++; 1600ba6c22ceSWarner Losh #endif 1601ba6c22ceSWarner Losh } 1602df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 160384c12dcdSWarner Losh else if (do_dynamic_iosched && (bp->bio_cmd != BIO_READ)) { 1604ba6c22ceSWarner Losh if (cam_iosched_sort_queue(isc)) 1605ba6c22ceSWarner Losh bioq_disksort(&isc->write_queue, bp); 1606ba6c22ceSWarner Losh else 1607ba6c22ceSWarner Losh bioq_insert_tail(&isc->write_queue, bp); 1608ba6c22ceSWarner Losh if (iosched_debug > 9) 1609ba6c22ceSWarner Losh printf("Qw : %p %#x\n", bp, bp->bio_cmd); 1610ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_WRITE) { 1611ba6c22ceSWarner Losh isc->write_stats.in++; 1612ba6c22ceSWarner Losh isc->write_stats.queued++; 1613ba6c22ceSWarner Losh } 1614ba6c22ceSWarner Losh } 1615ba6c22ceSWarner Losh #endif 1616ba6c22ceSWarner Losh else { 1617ba6c22ceSWarner Losh if (cam_iosched_sort_queue(isc)) 1618ba6c22ceSWarner Losh bioq_disksort(&isc->bio_queue, bp); 1619ba6c22ceSWarner Losh else 1620ba6c22ceSWarner Losh bioq_insert_tail(&isc->bio_queue, bp); 1621df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1622ba6c22ceSWarner Losh if (iosched_debug > 9) 1623ba6c22ceSWarner Losh printf("Qr : %p %#x\n", bp, bp->bio_cmd); 1624ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_READ) { 1625ba6c22ceSWarner Losh isc->read_stats.in++; 1626ba6c22ceSWarner Losh isc->read_stats.queued++; 1627ba6c22ceSWarner Losh } else if (bp->bio_cmd == BIO_WRITE) { 1628ba6c22ceSWarner Losh isc->write_stats.in++; 1629ba6c22ceSWarner Losh isc->write_stats.queued++; 1630ba6c22ceSWarner Losh } 1631ba6c22ceSWarner Losh #endif 1632ba6c22ceSWarner Losh } 1633ba6c22ceSWarner Losh } 1634ba6c22ceSWarner Losh 1635ba6c22ceSWarner Losh /* 1636ba6c22ceSWarner Losh * If we have work, get it scheduled. Called with the periph lock held. 1637ba6c22ceSWarner Losh */ 1638ba6c22ceSWarner Losh void 1639ba6c22ceSWarner Losh cam_iosched_schedule(struct cam_iosched_softc *isc, struct cam_periph *periph) 1640ba6c22ceSWarner Losh { 1641ba6c22ceSWarner Losh 1642ba6c22ceSWarner Losh if (cam_iosched_has_work(isc)) 1643ba6c22ceSWarner Losh xpt_schedule(periph, CAM_PRIORITY_NORMAL); 1644ba6c22ceSWarner Losh } 1645ba6c22ceSWarner Losh 1646ba6c22ceSWarner Losh /* 164755c770b4SWarner Losh * Complete a trim request. Mark that we no longer have one in flight. 1648ba6c22ceSWarner Losh */ 1649ba6c22ceSWarner Losh void 1650ba6c22ceSWarner Losh cam_iosched_trim_done(struct cam_iosched_softc *isc) 1651ba6c22ceSWarner Losh { 1652ba6c22ceSWarner Losh 1653ece56614SWarner Losh isc->flags &= ~CAM_IOSCHED_FLAG_TRIM_ACTIVE; 1654ba6c22ceSWarner Losh } 1655ba6c22ceSWarner Losh 1656ba6c22ceSWarner Losh /* 1657ba6c22ceSWarner Losh * Complete a bio. Called before we release the ccb with xpt_release_ccb so we 1658ba6c22ceSWarner Losh * might use notes in the ccb for statistics. 1659ba6c22ceSWarner Losh */ 1660ba6c22ceSWarner Losh int 1661ba6c22ceSWarner Losh cam_iosched_bio_complete(struct cam_iosched_softc *isc, struct bio *bp, 1662ba6c22ceSWarner Losh union ccb *done_ccb) 1663ba6c22ceSWarner Losh { 1664ba6c22ceSWarner Losh int retval = 0; 1665df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1666035ec48eSWarner Losh if (!do_dynamic_iosched) 1667ba6c22ceSWarner Losh return retval; 1668ba6c22ceSWarner Losh 1669ba6c22ceSWarner Losh if (iosched_debug > 10) 1670ba6c22ceSWarner Losh printf("done: %p %#x\n", bp, bp->bio_cmd); 1671ba6c22ceSWarner Losh if (bp->bio_cmd == BIO_WRITE) { 1672ba6c22ceSWarner Losh retval = cam_iosched_limiter_iodone(&isc->write_stats, bp); 1673157cb465SWarner Losh if ((bp->bio_flags & BIO_ERROR) != 0) 1674c4b72d8bSWarner Losh isc->write_stats.errs++; 1675ba6c22ceSWarner Losh isc->write_stats.out++; 1676ba6c22ceSWarner Losh isc->write_stats.pending--; 1677ba6c22ceSWarner Losh } else if (bp->bio_cmd == BIO_READ) { 1678ba6c22ceSWarner Losh retval = cam_iosched_limiter_iodone(&isc->read_stats, bp); 1679157cb465SWarner Losh if ((bp->bio_flags & BIO_ERROR) != 0) 1680c4b72d8bSWarner Losh isc->read_stats.errs++; 1681ba6c22ceSWarner Losh isc->read_stats.out++; 1682ba6c22ceSWarner Losh isc->read_stats.pending--; 1683ba6c22ceSWarner Losh } else if (bp->bio_cmd == BIO_DELETE) { 1684157cb465SWarner Losh if ((bp->bio_flags & BIO_ERROR) != 0) 1685c4b72d8bSWarner Losh isc->trim_stats.errs++; 1686ba6c22ceSWarner Losh isc->trim_stats.out++; 1687ba6c22ceSWarner Losh isc->trim_stats.pending--; 1688ba6c22ceSWarner Losh } else if (bp->bio_cmd != BIO_FLUSH) { 1689ba6c22ceSWarner Losh if (iosched_debug) 1690ba6c22ceSWarner Losh printf("Completing command with bio_cmd == %#x\n", bp->bio_cmd); 1691ba6c22ceSWarner Losh } 1692ba6c22ceSWarner Losh 1693e5436ab5SWarner Losh if (!(bp->bio_flags & BIO_ERROR) && done_ccb != NULL) { 1694e5436ab5SWarner Losh sbintime_t sim_latency; 1695e5436ab5SWarner Losh 1696e5436ab5SWarner Losh sim_latency = cam_iosched_sbintime_t(done_ccb->ccb_h.qos.periph_data); 1697e5436ab5SWarner Losh 1698e5436ab5SWarner Losh cam_iosched_io_metric_update(isc, sim_latency, 1699ba6c22ceSWarner Losh bp->bio_cmd, bp->bio_bcount); 1700e5436ab5SWarner Losh /* 1701e5436ab5SWarner Losh * Debugging code: allow callbacks to the periph driver when latency max 1702e5436ab5SWarner Losh * is exceeded. This can be useful for triggering external debugging actions. 1703e5436ab5SWarner Losh */ 1704e5436ab5SWarner Losh if (isc->latfcn && isc->max_lat != 0 && sim_latency > isc->max_lat) 1705e5436ab5SWarner Losh isc->latfcn(isc->latarg, sim_latency, bp); 1706e5436ab5SWarner Losh } 1707e5436ab5SWarner Losh 1708ba6c22ceSWarner Losh #endif 1709ba6c22ceSWarner Losh return retval; 1710ba6c22ceSWarner Losh } 1711ba6c22ceSWarner Losh 1712ba6c22ceSWarner Losh /* 1713ba6c22ceSWarner Losh * Tell the io scheduler that you've pushed a trim down into the sim. 171455c770b4SWarner Losh * This also tells the I/O scheduler not to push any more trims down, so 171555c770b4SWarner Losh * some periphs do not call it if they can cope with multiple trims in flight. 1716ba6c22ceSWarner Losh */ 1717ba6c22ceSWarner Losh void 1718ba6c22ceSWarner Losh cam_iosched_submit_trim(struct cam_iosched_softc *isc) 1719ba6c22ceSWarner Losh { 1720ba6c22ceSWarner Losh 1721ece56614SWarner Losh isc->flags |= CAM_IOSCHED_FLAG_TRIM_ACTIVE; 1722ba6c22ceSWarner Losh } 1723ba6c22ceSWarner Losh 1724ba6c22ceSWarner Losh /* 1725ba6c22ceSWarner Losh * Change the sorting policy hint for I/O transactions for this device. 1726ba6c22ceSWarner Losh */ 1727ba6c22ceSWarner Losh void 1728ba6c22ceSWarner Losh cam_iosched_set_sort_queue(struct cam_iosched_softc *isc, int val) 1729ba6c22ceSWarner Losh { 1730ba6c22ceSWarner Losh 1731ba6c22ceSWarner Losh isc->sort_io_queue = val; 1732ba6c22ceSWarner Losh } 1733ba6c22ceSWarner Losh 1734ba6c22ceSWarner Losh int 1735ba6c22ceSWarner Losh cam_iosched_has_work_flags(struct cam_iosched_softc *isc, uint32_t flags) 1736ba6c22ceSWarner Losh { 1737ba6c22ceSWarner Losh return isc->flags & flags; 1738ba6c22ceSWarner Losh } 1739ba6c22ceSWarner Losh 1740ba6c22ceSWarner Losh void 1741ba6c22ceSWarner Losh cam_iosched_set_work_flags(struct cam_iosched_softc *isc, uint32_t flags) 1742ba6c22ceSWarner Losh { 1743ba6c22ceSWarner Losh isc->flags |= flags; 1744ba6c22ceSWarner Losh } 1745ba6c22ceSWarner Losh 1746ba6c22ceSWarner Losh void 1747ba6c22ceSWarner Losh cam_iosched_clr_work_flags(struct cam_iosched_softc *isc, uint32_t flags) 1748ba6c22ceSWarner Losh { 1749ba6c22ceSWarner Losh isc->flags &= ~flags; 1750ba6c22ceSWarner Losh } 1751ba6c22ceSWarner Losh 1752df236247SWarner Losh #ifdef CAM_IOSCHED_DYNAMIC 1753ba6c22ceSWarner Losh /* 1754ba6c22ceSWarner Losh * After the method presented in Jack Crenshaw's 1998 article "Integer 17550b4060b0SEd Maste * Square Roots," reprinted at 1756ba6c22ceSWarner Losh * http://www.embedded.com/electronics-blogs/programmer-s-toolbox/4219659/Integer-Square-Roots 1757ba6c22ceSWarner Losh * and well worth the read. Briefly, we find the power of 4 that's the 1758ba6c22ceSWarner Losh * largest smaller than val. We then check each smaller power of 4 to 1759ba6c22ceSWarner Losh * see if val is still bigger. The right shifts at each step divide 1760ba6c22ceSWarner Losh * the result by 2 which after successive application winds up 1761ba6c22ceSWarner Losh * accumulating the right answer. It could also have been accumulated 1762ba6c22ceSWarner Losh * using a separate root counter, but this code is smaller and faster 1763ba6c22ceSWarner Losh * than that method. This method is also integer size invariant. 17640b4060b0SEd Maste * It returns floor(sqrt((float)val)), or the largest integer less than 1765ba6c22ceSWarner Losh * or equal to the square root. 1766ba6c22ceSWarner Losh */ 1767ba6c22ceSWarner Losh static uint64_t 1768ba6c22ceSWarner Losh isqrt64(uint64_t val) 1769ba6c22ceSWarner Losh { 1770ba6c22ceSWarner Losh uint64_t res = 0; 1771ba6c22ceSWarner Losh uint64_t bit = 1ULL << (sizeof(uint64_t) * NBBY - 2); 1772ba6c22ceSWarner Losh 1773ba6c22ceSWarner Losh /* 1774ba6c22ceSWarner Losh * Find the largest power of 4 smaller than val. 1775ba6c22ceSWarner Losh */ 1776ba6c22ceSWarner Losh while (bit > val) 1777ba6c22ceSWarner Losh bit >>= 2; 1778ba6c22ceSWarner Losh 1779ba6c22ceSWarner Losh /* 1780ba6c22ceSWarner Losh * Accumulate the answer, one bit at a time (we keep moving 1781ba6c22ceSWarner Losh * them over since 2 is the square root of 4 and we test 1782ba6c22ceSWarner Losh * powers of 4). We accumulate where we find the bit, but 1783ba6c22ceSWarner Losh * the successive shifts land the bit in the right place 1784ba6c22ceSWarner Losh * by the end. 1785ba6c22ceSWarner Losh */ 1786ba6c22ceSWarner Losh while (bit != 0) { 1787ba6c22ceSWarner Losh if (val >= res + bit) { 1788ba6c22ceSWarner Losh val -= res + bit; 1789ba6c22ceSWarner Losh res = (res >> 1) + bit; 1790ba6c22ceSWarner Losh } else 1791ba6c22ceSWarner Losh res >>= 1; 1792ba6c22ceSWarner Losh bit >>= 2; 1793ba6c22ceSWarner Losh } 1794ba6c22ceSWarner Losh 1795ba6c22ceSWarner Losh return res; 1796ba6c22ceSWarner Losh } 1797ba6c22ceSWarner Losh 179808fc2f23SWarner Losh static sbintime_t latencies[LAT_BUCKETS - 1] = { 1799cf3ec151SWarner Losh SBT_1MS << 0, 1800cf3ec151SWarner Losh SBT_1MS << 1, 1801cf3ec151SWarner Losh SBT_1MS << 2, 1802cf3ec151SWarner Losh SBT_1MS << 3, 1803cf3ec151SWarner Losh SBT_1MS << 4, 1804cf3ec151SWarner Losh SBT_1MS << 5, 1805cf3ec151SWarner Losh SBT_1MS << 6, 1806cf3ec151SWarner Losh SBT_1MS << 7, 1807cf3ec151SWarner Losh SBT_1MS << 8, 1808cf3ec151SWarner Losh SBT_1MS << 9, 180908fc2f23SWarner Losh SBT_1MS << 10, 181008fc2f23SWarner Losh SBT_1MS << 11, 181108fc2f23SWarner Losh SBT_1MS << 12, 181208fc2f23SWarner Losh SBT_1MS << 13 /* 8.192s */ 1813cf3ec151SWarner Losh }; 1814cf3ec151SWarner Losh 1815ba6c22ceSWarner Losh static void 1816ba6c22ceSWarner Losh cam_iosched_update(struct iop_stats *iop, sbintime_t sim_latency) 1817ba6c22ceSWarner Losh { 181879d80af2SWarner Losh sbintime_t y, deltasq, delta; 1819cf3ec151SWarner Losh int i; 1820cf3ec151SWarner Losh 1821cf3ec151SWarner Losh /* 1822cf3ec151SWarner Losh * Keep counts for latency. We do it by power of two buckets. 1823cf3ec151SWarner Losh * This helps us spot outlier behavior obscured by averages. 1824cf3ec151SWarner Losh */ 1825cf3ec151SWarner Losh for (i = 0; i < LAT_BUCKETS - 1; i++) { 1826cf3ec151SWarner Losh if (sim_latency < latencies[i]) { 1827cf3ec151SWarner Losh iop->latencies[i]++; 1828cf3ec151SWarner Losh break; 1829cf3ec151SWarner Losh } 1830cf3ec151SWarner Losh } 1831cf3ec151SWarner Losh if (i == LAT_BUCKETS - 1) 1832cf3ec151SWarner Losh iop->latencies[i]++; /* Put all > 1024ms values into the last bucket. */ 1833ba6c22ceSWarner Losh 1834ba6c22ceSWarner Losh /* 18350b4060b0SEd Maste * Classic exponentially decaying average with a tiny alpha 1836ba6c22ceSWarner Losh * (2 ^ -alpha_bits). For more info see the NIST statistical 1837ba6c22ceSWarner Losh * handbook. 1838ba6c22ceSWarner Losh * 183979d80af2SWarner Losh * ema_t = y_t * alpha + ema_t-1 * (1 - alpha) [nist] 184079d80af2SWarner Losh * ema_t = y_t * alpha + ema_t-1 - alpha * ema_t-1 184179d80af2SWarner Losh * ema_t = alpha * y_t - alpha * ema_t-1 + ema_t-1 1842ba6c22ceSWarner Losh * alpha = 1 / (1 << alpha_bits) 184379d80af2SWarner Losh * sub e == ema_t-1, b == 1/alpha (== 1 << alpha_bits), d == y_t - ema_t-1 184479d80af2SWarner Losh * = y_t/b - e/b + be/b 184579d80af2SWarner Losh * = (y_t - e + be) / b 184679d80af2SWarner Losh * = (e + d) / b 1847ba6c22ceSWarner Losh * 1848ba6c22ceSWarner Losh * Since alpha is a power of two, we can compute this w/o any mult or 1849ba6c22ceSWarner Losh * division. 185079d80af2SWarner Losh * 185179d80af2SWarner Losh * Variance can also be computed. Usually, it would be expressed as follows: 185279d80af2SWarner Losh * diff_t = y_t - ema_t-1 185379d80af2SWarner Losh * emvar_t = (1 - alpha) * (emavar_t-1 + diff_t^2 * alpha) 185479d80af2SWarner Losh * = emavar_t-1 - alpha * emavar_t-1 + delta_t^2 * alpha - (delta_t * alpha)^2 185579d80af2SWarner Losh * sub b == 1/alpha (== 1 << alpha_bits), e == emavar_t-1, d = delta_t^2 185679d80af2SWarner Losh * = e - e/b + dd/b + dd/bb 185779d80af2SWarner Losh * = (bbe - be + bdd + dd) / bb 185879d80af2SWarner Losh * = (bbe + b(dd-e) + dd) / bb (which is expanded below bb = 1<<(2*alpha_bits)) 185979d80af2SWarner Losh */ 186079d80af2SWarner Losh /* 186179d80af2SWarner Losh * XXX possible numeric issues 186279d80af2SWarner Losh * o We assume right shifted integers do the right thing, since that's 186379d80af2SWarner Losh * implementation defined. You can change the right shifts to / (1LL << alpha). 186479d80af2SWarner Losh * o alpha_bits = 9 gives ema ceiling of 23 bits of seconds for ema and 14 bits 186579d80af2SWarner Losh * for emvar. This puts a ceiling of 13 bits on alpha since we need a 186679d80af2SWarner Losh * few tens of seconds of representation. 186779d80af2SWarner Losh * o We mitigate alpha issues by never setting it too high. 1868ba6c22ceSWarner Losh */ 1869ba6c22ceSWarner Losh y = sim_latency; 187079d80af2SWarner Losh delta = (y - iop->ema); /* d */ 187179d80af2SWarner Losh iop->ema = ((iop->ema << alpha_bits) + delta) >> alpha_bits; 1872ba6c22ceSWarner Losh 1873ba6c22ceSWarner Losh /* 187479d80af2SWarner Losh * Were we to naively plow ahead at this point, we wind up with many numerical 187579d80af2SWarner Losh * issues making any SD > ~3ms unreliable. So, we shift right by 12. This leaves 187679d80af2SWarner Losh * us with microsecond level precision in the input, so the same in the 187779d80af2SWarner Losh * output. It means we can't overflow deltasq unless delta > 4k seconds. It 187879d80af2SWarner Losh * also means that emvar can be up 46 bits 40 of which are fraction, which 187979d80af2SWarner Losh * gives us a way to measure up to ~8s in the SD before the computation goes 188079d80af2SWarner Losh * unstable. Even the worst hard disk rarely has > 1s service time in the 188179d80af2SWarner Losh * drive. It does mean we have to shift left 12 bits after taking the 188279d80af2SWarner Losh * square root to compute the actual standard deviation estimate. This loss of 188379d80af2SWarner Losh * precision is preferable to needing int128 types to work. The above numbers 188479d80af2SWarner Losh * assume alpha=9. 10 or 11 are ok, but we start to run into issues at 12, 188579d80af2SWarner Losh * so 12 or 13 is OK for EMA, EMVAR and SD will be wrong in those cases. 1886ba6c22ceSWarner Losh */ 188779d80af2SWarner Losh delta >>= 12; 188879d80af2SWarner Losh deltasq = delta * delta; /* dd */ 188979d80af2SWarner Losh iop->emvar = ((iop->emvar << (2 * alpha_bits)) + /* bbe */ 189079d80af2SWarner Losh ((deltasq - iop->emvar) << alpha_bits) + /* b(dd-e) */ 189179d80af2SWarner Losh deltasq) /* dd */ 189279d80af2SWarner Losh >> (2 * alpha_bits); /* div bb */ 189379d80af2SWarner Losh iop->sd = (sbintime_t)isqrt64((uint64_t)iop->emvar) << 12; 1894ba6c22ceSWarner Losh } 1895ba6c22ceSWarner Losh 1896ba6c22ceSWarner Losh static void 1897ba6c22ceSWarner Losh cam_iosched_io_metric_update(struct cam_iosched_softc *isc, 1898ba6c22ceSWarner Losh sbintime_t sim_latency, int cmd, size_t size) 1899ba6c22ceSWarner Losh { 1900ba6c22ceSWarner Losh /* xxx Do we need to scale based on the size of the I/O ? */ 1901ba6c22ceSWarner Losh switch (cmd) { 1902ba6c22ceSWarner Losh case BIO_READ: 1903ba6c22ceSWarner Losh cam_iosched_update(&isc->read_stats, sim_latency); 1904ba6c22ceSWarner Losh break; 1905ba6c22ceSWarner Losh case BIO_WRITE: 1906ba6c22ceSWarner Losh cam_iosched_update(&isc->write_stats, sim_latency); 1907ba6c22ceSWarner Losh break; 1908ba6c22ceSWarner Losh case BIO_DELETE: 1909ba6c22ceSWarner Losh cam_iosched_update(&isc->trim_stats, sim_latency); 1910ba6c22ceSWarner Losh break; 1911ba6c22ceSWarner Losh default: 1912ba6c22ceSWarner Losh break; 1913ba6c22ceSWarner Losh } 1914ba6c22ceSWarner Losh } 1915ba6c22ceSWarner Losh 1916ba6c22ceSWarner Losh #ifdef DDB 1917ba6c22ceSWarner Losh static int biolen(struct bio_queue_head *bq) 1918ba6c22ceSWarner Losh { 1919ba6c22ceSWarner Losh int i = 0; 1920ba6c22ceSWarner Losh struct bio *bp; 1921ba6c22ceSWarner Losh 1922ba6c22ceSWarner Losh TAILQ_FOREACH(bp, &bq->queue, bio_queue) { 1923ba6c22ceSWarner Losh i++; 1924ba6c22ceSWarner Losh } 1925ba6c22ceSWarner Losh return i; 1926ba6c22ceSWarner Losh } 1927ba6c22ceSWarner Losh 1928ba6c22ceSWarner Losh /* 1929ba6c22ceSWarner Losh * Show the internal state of the I/O scheduler. 1930ba6c22ceSWarner Losh */ 1931ba6c22ceSWarner Losh DB_SHOW_COMMAND(iosched, cam_iosched_db_show) 1932ba6c22ceSWarner Losh { 1933ba6c22ceSWarner Losh struct cam_iosched_softc *isc; 1934ba6c22ceSWarner Losh 1935ba6c22ceSWarner Losh if (!have_addr) { 1936ba6c22ceSWarner Losh db_printf("Need addr\n"); 1937ba6c22ceSWarner Losh return; 1938ba6c22ceSWarner Losh } 1939ba6c22ceSWarner Losh isc = (struct cam_iosched_softc *)addr; 1940ba6c22ceSWarner Losh db_printf("pending_reads: %d\n", isc->read_stats.pending); 1941ba6c22ceSWarner Losh db_printf("min_reads: %d\n", isc->read_stats.min); 1942ba6c22ceSWarner Losh db_printf("max_reads: %d\n", isc->read_stats.max); 1943ba6c22ceSWarner Losh db_printf("reads: %d\n", isc->read_stats.total); 1944ba6c22ceSWarner Losh db_printf("in_reads: %d\n", isc->read_stats.in); 1945ba6c22ceSWarner Losh db_printf("out_reads: %d\n", isc->read_stats.out); 1946ba6c22ceSWarner Losh db_printf("queued_reads: %d\n", isc->read_stats.queued); 19473aba1d47SWarner Losh db_printf("Read Q len %d\n", biolen(&isc->bio_queue)); 1948ba6c22ceSWarner Losh db_printf("pending_writes: %d\n", isc->write_stats.pending); 1949ba6c22ceSWarner Losh db_printf("min_writes: %d\n", isc->write_stats.min); 1950ba6c22ceSWarner Losh db_printf("max_writes: %d\n", isc->write_stats.max); 1951ba6c22ceSWarner Losh db_printf("writes: %d\n", isc->write_stats.total); 1952ba6c22ceSWarner Losh db_printf("in_writes: %d\n", isc->write_stats.in); 1953ba6c22ceSWarner Losh db_printf("out_writes: %d\n", isc->write_stats.out); 1954ba6c22ceSWarner Losh db_printf("queued_writes: %d\n", isc->write_stats.queued); 19553aba1d47SWarner Losh db_printf("Write Q len %d\n", biolen(&isc->write_queue)); 1956ba6c22ceSWarner Losh db_printf("pending_trims: %d\n", isc->trim_stats.pending); 1957ba6c22ceSWarner Losh db_printf("min_trims: %d\n", isc->trim_stats.min); 1958ba6c22ceSWarner Losh db_printf("max_trims: %d\n", isc->trim_stats.max); 1959ba6c22ceSWarner Losh db_printf("trims: %d\n", isc->trim_stats.total); 1960ba6c22ceSWarner Losh db_printf("in_trims: %d\n", isc->trim_stats.in); 1961ba6c22ceSWarner Losh db_printf("out_trims: %d\n", isc->trim_stats.out); 1962ba6c22ceSWarner Losh db_printf("queued_trims: %d\n", isc->trim_stats.queued); 19633aba1d47SWarner Losh db_printf("Trim Q len %d\n", biolen(&isc->trim_queue)); 1964ba6c22ceSWarner Losh db_printf("read_bias: %d\n", isc->read_bias); 1965ba6c22ceSWarner Losh db_printf("current_read_bias: %d\n", isc->current_read_bias); 1966ece56614SWarner Losh db_printf("Trim active? %s\n", 1967ece56614SWarner Losh (isc->flags & CAM_IOSCHED_FLAG_TRIM_ACTIVE) ? "yes" : "no"); 1968ba6c22ceSWarner Losh } 1969ba6c22ceSWarner Losh #endif 1970ba6c22ceSWarner Losh #endif 1971