1 /* 2 * Copyright (c) 2009 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Alex Hornung <ahornung@gmail.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef _SYS_DSCHED_H_ 36 #define _SYS_DSCHED_H_ 37 38 #if defined(_KERNEL) 39 40 #ifndef _SYS_QUEUE_H_ 41 #include <sys/queue.h> 42 #endif 43 #ifndef _SYS_BIO_H_ 44 #include <sys/bio.h> 45 #endif 46 #ifndef _SYS_BIOTRACK_H_ 47 #include <sys/biotrack.h> 48 #endif 49 #ifndef _SYS_LOCK_H_ 50 #include <sys/lock.h> 51 #endif 52 #ifndef _SYS_CONF_H_ 53 #include <sys/conf.h> 54 #endif 55 #ifndef _SYS_MSGPORT_H_ 56 #include <sys/msgport.h> 57 #endif 58 #ifndef _SYS_SYSCTL_H_ 59 #include <sys/sysctl.h> 60 #endif 61 #ifndef _SYS_DISK_H_ 62 #include <sys/disk.h> 63 #endif 64 65 #define DSCHED_POLICY_NAME_LENGTH 64 66 67 #define dsched_set_disk_priv(dp, x) ((dp)->d_dsched_priv1 = (x)) 68 #define dsched_get_disk_priv(dp) ((dp)?((dp)->d_dsched_priv1):NULL) 69 #define dsched_set_proc_priv(pp, x) ((pp)->p_dsched_priv1 = (x)) 70 #define dsched_get_proc_priv(pp) ((pp)?((pp)->p_dsched_priv1):NULL) 71 72 #define dsched_set_thread_priv(td, x) ((td)->td_dsched_priv1 = (x)) 73 #define dsched_get_thread_priv(td) ((td)?((td)->td_dsched_priv1):NULL) 74 75 #define dsched_set_buf_priv(bp, x) ((bp)->b_iosched = (x)) 76 #define dsched_get_buf_priv(bp) ((bp)?((bp)->b_iosched):NULL) 77 #define dsched_clr_buf_priv(bp) ((bp)->b_iosched = NULL) 78 #define dsched_is_clear_buf_priv(bp) ((bp)->b_iosched == NULL) 79 80 81 #define dsched_set_bio_dp(bio, x) ((bio)->bio_caller_info1.ptr = (x)) 82 #define dsched_get_bio_dp(bio) ((bio)?((bio)->bio_caller_info1.ptr):NULL) 83 #define dsched_set_bio_priv(bio, x) ((bio)->bio_caller_info2.ptr = (x)) 84 #define dsched_get_bio_priv(bio) ((bio)?((bio)->bio_caller_info2.ptr):NULL) 85 #define dsched_set_bio_stime(bio, x) ((bio)->bio_caller_info3.lvalue = (x)) 86 #define dsched_get_bio_stime(bio) ((bio)?((bio)->bio_caller_info3.lvalue):0) 87 #define dsched_set_bio_tdio(bio, x) ((bio)->bio_caller_info3.ptr = (x)) 88 #define dsched_get_bio_tdio(bio) ((bio)?((bio)->bio_caller_info3.ptr):0) 89 90 91 struct dsched_thread_ctx { 92 TAILQ_ENTRY(dsched_thread_ctx) link; 93 94 TAILQ_HEAD(, dsched_thread_io) tdio_list; /* list of thread_io */ 95 struct lock lock; 96 97 int32_t refcount; 98 99 struct proc *p; 100 struct thread *td; 101 int32_t dead; 102 }; 103 104 struct dsched_disk_ctx { 105 TAILQ_ENTRY(dsched_disk_ctx) link; 106 107 TAILQ_HEAD(, dsched_thread_io) tdio_list; /* list of thread_io of disk */ 108 struct lock lock; 109 110 int32_t refcount; 111 int32_t flags; 112 113 int max_tag_queue_depth; /* estimated max tag queue depth */ 114 int current_tag_queue_depth; /* estimated current tag queue depth */ 115 116 struct disk *dp; /* back pointer to disk struct */ 117 118 struct sysctl_ctx_list sysctl_ctx; 119 }; 120 121 struct dsched_policy; 122 123 struct dsched_thread_io { 124 TAILQ_ENTRY(dsched_thread_io) link; 125 TAILQ_ENTRY(dsched_thread_io) dlink; 126 127 TAILQ_HEAD(, bio) queue; /* IO queue (bio) */ 128 struct lock lock; 129 int32_t qlength;/* IO queue length */ 130 131 int32_t refcount; 132 133 int32_t flags; 134 135 struct disk *dp; 136 struct dsched_disk_ctx *diskctx; 137 struct dsched_thread_ctx *tdctx; 138 struct proc *p; 139 struct dsched_policy *debug_policy; 140 int debug_inited; 141 int debug_priv; 142 }; 143 144 typedef int dsched_prepare_t(struct dsched_disk_ctx *diskctx); 145 typedef void dsched_teardown_t(struct dsched_disk_ctx *diskctx); 146 typedef void dsched_cancel_t(struct dsched_disk_ctx *diskctx); 147 typedef int dsched_queue_t(struct dsched_disk_ctx *diskctx, 148 struct dsched_thread_io *tdio, struct bio *bio); 149 typedef void dsched_dequeue_t(struct dsched_disk_ctx *diskctx); 150 151 typedef void dsched_new_tdio_t(struct dsched_thread_io *tdio); 152 typedef void dsched_new_diskctx_t(struct dsched_disk_ctx *diskctx); 153 typedef void dsched_destroy_tdio_t(struct dsched_thread_io *tdio); 154 typedef void dsched_destroy_diskctx_t(struct dsched_disk_ctx *diskctx); 155 typedef void dsched_bio_done_t(struct bio *bio); 156 typedef void dsched_polling_func_t(struct dsched_disk_ctx *diskctx); 157 158 struct dsched_policy { 159 char name[DSCHED_POLICY_NAME_LENGTH]; 160 uint64_t uniq_id; 161 int ref_count; 162 163 TAILQ_ENTRY(dsched_policy) link; 164 165 dsched_prepare_t *prepare; 166 dsched_teardown_t *teardown; 167 dsched_cancel_t *cancel_all; 168 dsched_queue_t *bio_queue; 169 170 dsched_new_tdio_t *new_tdio; 171 dsched_new_diskctx_t *new_diskctx; 172 dsched_destroy_tdio_t *destroy_tdio; 173 dsched_destroy_diskctx_t *destroy_diskctx; 174 175 dsched_bio_done_t *bio_done; /* call back when a bio dispatched by dsched_strategy_request_polling() is done */ 176 dsched_polling_func_t *polling_func; /* it gets called when the disk is idle or about to idle */ 177 }; 178 179 TAILQ_HEAD(dsched_policy_head, dsched_policy); 180 181 182 #define DSCHED_THREAD_IO_LOCKINIT(x) \ 183 lockinit(&(x)->lock, "tdiobioq", 0, LK_CANRECURSE) 184 185 #define DSCHED_THREAD_IO_LOCK(x) do { \ 186 dsched_thread_io_ref((x)); \ 187 lockmgr(&(x)->lock, LK_EXCLUSIVE); \ 188 } while(0) 189 190 #define DSCHED_THREAD_IO_UNLOCK(x) do { \ 191 lockmgr(&(x)->lock, LK_RELEASE); \ 192 dsched_thread_io_unref((x)); \ 193 } while(0) 194 195 #define DSCHED_DISK_CTX_LOCKINIT(x) \ 196 lockinit(&(x)->lock, "tdiodiskq", 0, LK_CANRECURSE) 197 198 #define DSCHED_DISK_CTX_LOCK(x) do { \ 199 dsched_disk_ctx_ref((x)); \ 200 lockmgr(&(x)->lock, LK_EXCLUSIVE); \ 201 } while(0) 202 203 #define DSCHED_DISK_CTX_UNLOCK(x) do { \ 204 lockmgr(&(x)->lock, LK_RELEASE); \ 205 dsched_disk_ctx_unref((x)); \ 206 } while(0) 207 208 #define DSCHED_DISK_CTX_LOCK_ASSERT(x) \ 209 KKASSERT(lockstatus(&(x)->lock, curthread) == LK_EXCLUSIVE) 210 211 #define DSCHED_GLOBAL_THREAD_CTX_LOCKINIT(x) \ 212 lockinit(&dsched_tdctx_lock, "tdctxglob", 0, LK_CANRECURSE) 213 #define DSCHED_GLOBAL_THREAD_CTX_LOCK(x) \ 214 lockmgr(&dsched_tdctx_lock, LK_EXCLUSIVE) 215 #define DSCHED_GLOBAL_THREAD_CTX_UNLOCK(x) \ 216 lockmgr(&dsched_tdctx_lock, LK_RELEASE) 217 218 #define DSCHED_THREAD_CTX_LOCKINIT(x) \ 219 lockinit(&(x)->lock, "tdctx", 0, LK_CANRECURSE) 220 221 #define DSCHED_THREAD_CTX_LOCK(x) do { \ 222 dsched_thread_ctx_ref((x)); \ 223 lockmgr(&(x)->lock, LK_EXCLUSIVE); \ 224 } while(0) 225 226 #define DSCHED_THREAD_CTX_UNLOCK(x) do { \ 227 lockmgr(&(x)->lock, LK_RELEASE); \ 228 dsched_thread_ctx_unref((x)); \ 229 } while(0) 230 231 /* flags for thread_io */ 232 #define DSCHED_LINKED_DISK_CTX 0x01 233 #define DSCHED_LINKED_THREAD_CTX 0x02 234 /* flags for disk_ctx */ 235 #define DSCHED_SYSCTL_CTX_INITED 0x01 236 237 #define DSCHED_THREAD_CTX_MAX_SZ sizeof(struct dsched_thread_ctx) 238 #define DSCHED_THREAD_IO_MAX_SZ 384 239 #define DSCHED_DISK_CTX_MAX_SZ 1024 240 241 #define DSCHED_POLICY_MODULE(name, evh, version) \ 242 static moduledata_t name##_mod = { \ 243 #name, \ 244 evh, \ 245 NULL \ 246 }; \ 247 DECLARE_MODULE(name, name##_mod, SI_SUB_PRE_DRIVERS, SI_ORDER_MIDDLE); \ 248 MODULE_VERSION(name, version) 249 250 void dsched_disk_create_callback(struct disk *dp, const char *head_name, int unit); 251 void dsched_disk_update_callback(struct disk *dp, struct disk_info *info); 252 void dsched_disk_destroy_callback(struct disk *dp); 253 void dsched_queue(struct disk *dp, struct bio *bio); 254 int dsched_register(struct dsched_policy *d_policy); 255 int dsched_unregister(struct dsched_policy *d_policy); 256 int dsched_switch(struct disk *dp, struct dsched_policy *new_policy); 257 void dsched_set_policy(struct disk *dp, struct dsched_policy *new_policy); 258 struct dsched_policy *dsched_find_policy(char *search); 259 struct disk *dsched_find_disk(char *search); 260 struct dsched_policy *dsched_policy_enumerate(struct dsched_policy *pol); 261 struct disk *dsched_disk_enumerate(struct disk *marker, struct disk *dp, 262 struct dsched_policy *policy); 263 void dsched_cancel_bio(struct bio *bp); 264 void dsched_strategy_raw(struct disk *dp, struct bio *bp); 265 void dsched_strategy_sync(struct disk *dp, struct bio *bp); 266 void dsched_strategy_async(struct disk *dp, struct bio *bp, biodone_t *done, void *priv); 267 void dsched_strategy_request_polling(struct disk *bp, struct bio *bio, struct dsched_disk_ctx *diskctx); 268 int dsched_debug(int level, char *fmt, ...) __printflike(2, 3); 269 270 void policy_new(struct disk *dp, struct dsched_policy *pol); 271 void policy_destroy(struct disk *dp); 272 273 void dsched_disk_ctx_ref(struct dsched_disk_ctx *diskctx); 274 void dsched_thread_io_ref(struct dsched_thread_io *tdio); 275 void dsched_thread_ctx_ref(struct dsched_thread_ctx *tdctx); 276 void dsched_disk_ctx_unref(struct dsched_disk_ctx *diskctx); 277 void dsched_thread_io_unref(struct dsched_thread_io *tdio); 278 void dsched_thread_ctx_unref(struct dsched_thread_ctx *tdctx); 279 280 void dsched_new_policy_thread_tdio(struct dsched_disk_ctx *diskctx, 281 struct dsched_policy *pol); 282 struct dsched_disk_ctx *dsched_disk_ctx_alloc(struct disk *dp, 283 struct dsched_policy *pol); 284 struct dsched_thread_ctx *dsched_thread_ctx_alloc(struct proc *p); 285 286 typedef void dsched_new_buf_t(struct buf *bp); 287 typedef void dsched_new_proc_t(struct proc *p); 288 typedef void dsched_new_thread_t(struct thread *td); 289 typedef void dsched_exit_buf_t(struct buf *bp); 290 typedef void dsched_exit_proc_t(struct proc *p); 291 typedef void dsched_exit_thread_t(struct thread *td); 292 293 dsched_new_buf_t dsched_new_buf; 294 dsched_new_proc_t dsched_new_proc; 295 dsched_new_thread_t dsched_new_thread; 296 dsched_exit_buf_t dsched_exit_buf; 297 dsched_exit_proc_t dsched_exit_proc; 298 dsched_exit_thread_t dsched_exit_thread; 299 300 #endif /* _KERNEL */ 301 302 303 #define DSCHED_NAME_LENGTH 64 304 #define DSCHED_SET_DEVICE_POLICY _IOWR('d', 1, struct dsched_ioctl) 305 #define DSCHED_LIST_DISKS _IOWR('d', 2, struct dsched_ioctl) 306 #define DSCHED_LIST_DISK _IOWR('d', 3, struct dsched_ioctl) 307 #define DSCHED_LIST_POLICIES _IOWR('d', 4, struct dsched_ioctl) 308 309 struct dsched_ioctl { 310 uint16_t num_elem; 311 char dev_name[DSCHED_NAME_LENGTH]; 312 char pol_name[DSCHED_NAME_LENGTH]; 313 }; 314 315 struct dsched_stats { 316 int32_t tdctx_allocations; 317 int32_t tdio_allocations; 318 int32_t diskctx_allocations; 319 320 int32_t no_tdctx; 321 322 int32_t nthreads; 323 int32_t nprocs; 324 }; 325 326 #endif /* !_SYS_DSCHED_H_ */ 327