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