1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/zfs_context.h> 29 #include <sys/spa.h> 30 #include <sys/vdev_impl.h> 31 #include <sys/zio.h> 32 #include <sys/avl.h> 33 34 /* 35 * These tunables are for performance analysis. 36 */ 37 /* 38 * zfs_vdev_max_pending is the maximum number of i/os concurrently 39 * pending to each device. zfs_vdev_min_pending is the initial number 40 * of i/os pending to each device (before it starts ramping up to 41 * max_pending). 42 */ 43 int zfs_vdev_max_pending = 35; 44 int zfs_vdev_min_pending = 4; 45 46 /* deadline = pri + (lbolt >> time_shift) */ 47 int zfs_vdev_time_shift = 6; 48 49 /* exponential I/O issue ramp-up rate */ 50 int zfs_vdev_ramp_rate = 2; 51 52 /* 53 * i/os will be aggregated into a single large i/o up to 54 * zfs_vdev_aggregation_limit bytes long. 55 */ 56 int zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE; 57 58 /* 59 * Virtual device vector for disk I/O scheduling. 60 */ 61 int 62 vdev_queue_deadline_compare(const void *x1, const void *x2) 63 { 64 const zio_t *z1 = x1; 65 const zio_t *z2 = x2; 66 67 if (z1->io_deadline < z2->io_deadline) 68 return (-1); 69 if (z1->io_deadline > z2->io_deadline) 70 return (1); 71 72 if (z1->io_offset < z2->io_offset) 73 return (-1); 74 if (z1->io_offset > z2->io_offset) 75 return (1); 76 77 if (z1 < z2) 78 return (-1); 79 if (z1 > z2) 80 return (1); 81 82 return (0); 83 } 84 85 int 86 vdev_queue_offset_compare(const void *x1, const void *x2) 87 { 88 const zio_t *z1 = x1; 89 const zio_t *z2 = x2; 90 91 if (z1->io_offset < z2->io_offset) 92 return (-1); 93 if (z1->io_offset > z2->io_offset) 94 return (1); 95 96 if (z1 < z2) 97 return (-1); 98 if (z1 > z2) 99 return (1); 100 101 return (0); 102 } 103 104 void 105 vdev_queue_init(vdev_t *vd) 106 { 107 vdev_queue_t *vq = &vd->vdev_queue; 108 109 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 110 111 avl_create(&vq->vq_deadline_tree, vdev_queue_deadline_compare, 112 sizeof (zio_t), offsetof(struct zio, io_deadline_node)); 113 114 avl_create(&vq->vq_read_tree, vdev_queue_offset_compare, 115 sizeof (zio_t), offsetof(struct zio, io_offset_node)); 116 117 avl_create(&vq->vq_write_tree, vdev_queue_offset_compare, 118 sizeof (zio_t), offsetof(struct zio, io_offset_node)); 119 120 avl_create(&vq->vq_pending_tree, vdev_queue_offset_compare, 121 sizeof (zio_t), offsetof(struct zio, io_offset_node)); 122 } 123 124 void 125 vdev_queue_fini(vdev_t *vd) 126 { 127 vdev_queue_t *vq = &vd->vdev_queue; 128 129 avl_destroy(&vq->vq_deadline_tree); 130 avl_destroy(&vq->vq_read_tree); 131 avl_destroy(&vq->vq_write_tree); 132 avl_destroy(&vq->vq_pending_tree); 133 134 mutex_destroy(&vq->vq_lock); 135 } 136 137 static void 138 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 139 { 140 avl_add(&vq->vq_deadline_tree, zio); 141 avl_add(zio->io_vdev_tree, zio); 142 } 143 144 static void 145 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 146 { 147 avl_remove(&vq->vq_deadline_tree, zio); 148 avl_remove(zio->io_vdev_tree, zio); 149 } 150 151 static void 152 vdev_queue_agg_io_done(zio_t *aio) 153 { 154 zio_t *dio; 155 uint64_t offset = 0; 156 157 while ((dio = aio->io_delegate_list) != NULL) { 158 if (aio->io_type == ZIO_TYPE_READ) 159 bcopy((char *)aio->io_data + offset, dio->io_data, 160 dio->io_size); 161 offset += dio->io_size; 162 aio->io_delegate_list = dio->io_delegate_next; 163 dio->io_delegate_next = NULL; 164 dio->io_error = aio->io_error; 165 zio_next_stage(dio); 166 } 167 ASSERT3U(offset, ==, aio->io_size); 168 169 zio_buf_free(aio->io_data, aio->io_size); 170 } 171 172 #define IS_ADJACENT(io, nio) \ 173 ((io)->io_offset + (io)->io_size == (nio)->io_offset) 174 175 typedef void zio_issue_func_t(zio_t *); 176 177 static zio_t * 178 vdev_queue_io_to_issue(vdev_queue_t *vq, uint64_t pending_limit, 179 zio_issue_func_t **funcp) 180 { 181 zio_t *fio, *lio, *aio, *dio; 182 avl_tree_t *tree; 183 uint64_t size; 184 185 ASSERT(MUTEX_HELD(&vq->vq_lock)); 186 187 *funcp = NULL; 188 189 if (avl_numnodes(&vq->vq_pending_tree) >= pending_limit || 190 avl_numnodes(&vq->vq_deadline_tree) == 0) 191 return (NULL); 192 193 fio = lio = avl_first(&vq->vq_deadline_tree); 194 195 tree = fio->io_vdev_tree; 196 size = fio->io_size; 197 198 while ((dio = AVL_PREV(tree, fio)) != NULL && IS_ADJACENT(dio, fio) && 199 size + dio->io_size <= zfs_vdev_aggregation_limit) { 200 dio->io_delegate_next = fio; 201 fio = dio; 202 size += dio->io_size; 203 } 204 205 while ((dio = AVL_NEXT(tree, lio)) != NULL && IS_ADJACENT(lio, dio) && 206 size + dio->io_size <= zfs_vdev_aggregation_limit) { 207 lio->io_delegate_next = dio; 208 lio = dio; 209 size += dio->io_size; 210 } 211 212 if (fio != lio) { 213 char *buf = zio_buf_alloc(size); 214 uint64_t offset = 0; 215 int nagg = 0; 216 217 ASSERT(size <= zfs_vdev_aggregation_limit); 218 219 aio = zio_vdev_child_io(fio, NULL, fio->io_vd, 220 fio->io_offset, buf, size, fio->io_type, 221 ZIO_PRIORITY_NOW, ZIO_FLAG_DONT_QUEUE | 222 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_PROPAGATE | 223 ZIO_FLAG_NOBOOKMARK, 224 vdev_queue_agg_io_done, NULL); 225 226 aio->io_delegate_list = fio; 227 228 for (dio = fio; dio != NULL; dio = dio->io_delegate_next) { 229 ASSERT(dio->io_type == aio->io_type); 230 ASSERT(dio->io_vdev_tree == tree); 231 if (dio->io_type == ZIO_TYPE_WRITE) 232 bcopy(dio->io_data, buf + offset, dio->io_size); 233 offset += dio->io_size; 234 vdev_queue_io_remove(vq, dio); 235 zio_vdev_io_bypass(dio); 236 nagg++; 237 } 238 239 ASSERT(offset == size); 240 241 dprintf("%5s T=%llu off=%8llx agg=%3d " 242 "old=%5llx new=%5llx\n", 243 zio_type_name[fio->io_type], 244 fio->io_deadline, fio->io_offset, nagg, fio->io_size, size); 245 246 avl_add(&vq->vq_pending_tree, aio); 247 248 *funcp = zio_nowait; 249 return (aio); 250 } 251 252 ASSERT(fio->io_vdev_tree == tree); 253 vdev_queue_io_remove(vq, fio); 254 255 avl_add(&vq->vq_pending_tree, fio); 256 257 *funcp = zio_next_stage; 258 259 return (fio); 260 } 261 262 zio_t * 263 vdev_queue_io(zio_t *zio) 264 { 265 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 266 zio_t *nio; 267 zio_issue_func_t *func; 268 269 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 270 271 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 272 return (zio); 273 274 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 275 276 if (zio->io_type == ZIO_TYPE_READ) 277 zio->io_vdev_tree = &vq->vq_read_tree; 278 else 279 zio->io_vdev_tree = &vq->vq_write_tree; 280 281 mutex_enter(&vq->vq_lock); 282 283 zio->io_deadline = (zio->io_timestamp >> zfs_vdev_time_shift) + 284 zio->io_priority; 285 286 vdev_queue_io_add(vq, zio); 287 288 nio = vdev_queue_io_to_issue(vq, zfs_vdev_min_pending, &func); 289 290 mutex_exit(&vq->vq_lock); 291 292 if (nio == NULL || func != zio_nowait) 293 return (nio); 294 295 func(nio); 296 return (NULL); 297 } 298 299 void 300 vdev_queue_io_done(zio_t *zio) 301 { 302 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 303 zio_t *nio; 304 zio_issue_func_t *func; 305 int i; 306 307 mutex_enter(&vq->vq_lock); 308 309 avl_remove(&vq->vq_pending_tree, zio); 310 311 for (i = 0; i < zfs_vdev_ramp_rate; i++) { 312 nio = vdev_queue_io_to_issue(vq, zfs_vdev_max_pending, &func); 313 if (nio == NULL) 314 break; 315 mutex_exit(&vq->vq_lock); 316 if (func == zio_next_stage) 317 zio_vdev_io_reissue(nio); 318 func(nio); 319 mutex_enter(&vq->vq_lock); 320 } 321 322 mutex_exit(&vq->vq_lock); 323 } 324