Print this page
4334 Improve ZFS N-way mirror read performance
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/vdev_queue.c
+++ new/usr/src/uts/common/fs/zfs/vdev_queue.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 27 * Copyright (c) 2013 by Delphix. All rights reserved.
28 + * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 29 */
29 30
30 31 #include <sys/zfs_context.h>
31 32 #include <sys/vdev_impl.h>
32 33 #include <sys/spa_impl.h>
33 34 #include <sys/zio.h>
34 35 #include <sys/avl.h>
35 36 #include <sys/dsl_pool.h>
36 37
37 38 /*
38 39 * ZFS I/O Scheduler
39 40 * ---------------
40 41 *
41 42 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
42 43 * I/O scheduler determines when and in what order those operations are
43 44 * issued. The I/O scheduler divides operations into five I/O classes
44 45 * prioritized in the following order: sync read, sync write, async read,
45 46 * async write, and scrub/resilver. Each queue defines the minimum and
46 47 * maximum number of concurrent operations that may be issued to the device.
47 48 * In addition, the device has an aggregate maximum. Note that the sum of the
48 49 * per-queue minimums must not exceed the aggregate maximum, and if the
49 50 * aggregate maximum is equal to or greater than the sum of the per-queue
50 51 * maximums, the per-queue minimum has no effect.
51 52 *
52 53 * For many physical devices, throughput increases with the number of
53 54 * concurrent operations, but latency typically suffers. Further, physical
54 55 * devices typically have a limit at which more concurrent operations have no
55 56 * effect on throughput or can actually cause it to decrease.
56 57 *
57 58 * The scheduler selects the next operation to issue by first looking for an
58 59 * I/O class whose minimum has not been satisfied. Once all are satisfied and
59 60 * the aggregate maximum has not been hit, the scheduler looks for classes
60 61 * whose maximum has not been satisfied. Iteration through the I/O classes is
61 62 * done in the order specified above. No further operations are issued if the
62 63 * aggregate maximum number of concurrent operations has been hit or if there
63 64 * are no operations queued for an I/O class that has not hit its maximum.
64 65 * Every time an i/o is queued or an operation completes, the I/O scheduler
65 66 * looks for new operations to issue.
66 67 *
67 68 * All I/O classes have a fixed maximum number of outstanding operations
68 69 * except for the async write class. Asynchronous writes represent the data
69 70 * that is committed to stable storage during the syncing stage for
70 71 * transaction groups (see txg.c). Transaction groups enter the syncing state
71 72 * periodically so the number of queued async writes will quickly burst up and
72 73 * then bleed down to zero. Rather than servicing them as quickly as possible,
73 74 * the I/O scheduler changes the maximum number of active async write i/os
74 75 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
75 76 * both throughput and latency typically increase with the number of
76 77 * concurrent operations issued to physical devices, reducing the burstiness
77 78 * in the number of concurrent operations also stabilizes the response time of
78 79 * operations from other -- and in particular synchronous -- queues. In broad
79 80 * strokes, the I/O scheduler will issue more concurrent operations from the
80 81 * async write queue as there's more dirty data in the pool.
81 82 *
82 83 * Async Writes
83 84 *
84 85 * The number of concurrent operations issued for the async write I/O class
85 86 * follows a piece-wise linear function defined by a few adjustable points.
86 87 *
87 88 * | o---------| <-- zfs_vdev_async_write_max_active
88 89 * ^ | /^ |
89 90 * | | / | |
90 91 * active | / | |
91 92 * I/O | / | |
92 93 * count | / | |
93 94 * | / | |
94 95 * |------------o | | <-- zfs_vdev_async_write_min_active
95 96 * 0|____________^______|_________|
96 97 * 0% | | 100% of zfs_dirty_data_max
97 98 * | |
98 99 * | `-- zfs_vdev_async_write_active_max_dirty_percent
99 100 * `--------- zfs_vdev_async_write_active_min_dirty_percent
100 101 *
101 102 * Until the amount of dirty data exceeds a minimum percentage of the dirty
102 103 * data allowed in the pool, the I/O scheduler will limit the number of
103 104 * concurrent operations to the minimum. As that threshold is crossed, the
104 105 * number of concurrent operations issued increases linearly to the maximum at
105 106 * the specified maximum percentage of the dirty data allowed in the pool.
106 107 *
107 108 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
108 109 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
109 110 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
110 111 * maximum percentage, this indicates that the rate of incoming data is
111 112 * greater than the rate that the backend storage can handle. In this case, we
112 113 * must further throttle incoming writes (see dmu_tx_delay() for details).
113 114 */
114 115
115 116 /*
116 117 * The maximum number of i/os active to each device. Ideally, this will be >=
117 118 * the sum of each queue's max_active. It must be at least the sum of each
118 119 * queue's min_active.
119 120 */
120 121 uint32_t zfs_vdev_max_active = 1000;
121 122
122 123 /*
123 124 * Per-queue limits on the number of i/os active to each device. If the
124 125 * sum of the queue's max_active is < zfs_vdev_max_active, then the
125 126 * min_active comes into play. We will send min_active from each queue,
126 127 * and then select from queues in the order defined by zio_priority_t.
127 128 *
128 129 * In general, smaller max_active's will lead to lower latency of synchronous
129 130 * operations. Larger max_active's may lead to higher overall throughput,
130 131 * depending on underlying storage.
131 132 *
132 133 * The ratio of the queues' max_actives determines the balance of performance
133 134 * between reads, writes, and scrubs. E.g., increasing
134 135 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
135 136 * more quickly, but reads and writes to have higher latency and lower
136 137 * throughput.
137 138 */
138 139 uint32_t zfs_vdev_sync_read_min_active = 10;
139 140 uint32_t zfs_vdev_sync_read_max_active = 10;
140 141 uint32_t zfs_vdev_sync_write_min_active = 10;
141 142 uint32_t zfs_vdev_sync_write_max_active = 10;
142 143 uint32_t zfs_vdev_async_read_min_active = 1;
143 144 uint32_t zfs_vdev_async_read_max_active = 3;
144 145 uint32_t zfs_vdev_async_write_min_active = 1;
145 146 uint32_t zfs_vdev_async_write_max_active = 10;
146 147 uint32_t zfs_vdev_scrub_min_active = 1;
147 148 uint32_t zfs_vdev_scrub_max_active = 2;
148 149
149 150 /*
150 151 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
151 152 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
152 153 * zfs_vdev_async_write_active_max_dirty_percent, use
153 154 * zfs_vdev_async_write_max_active. The value is linearly interpolated
154 155 * between min and max.
155 156 */
156 157 int zfs_vdev_async_write_active_min_dirty_percent = 30;
157 158 int zfs_vdev_async_write_active_max_dirty_percent = 60;
158 159
159 160 /*
160 161 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
161 162 * For read I/Os, we also aggregate across small adjacency gaps; for writes
162 163 * we include spans of optional I/Os to aid aggregation at the disk even when
163 164 * they aren't able to help us aggregate at this level.
164 165 */
165 166 int zfs_vdev_aggregation_limit = SPA_MAXBLOCKSIZE;
166 167 int zfs_vdev_read_gap_limit = 32 << 10;
167 168 int zfs_vdev_write_gap_limit = 4 << 10;
168 169
169 170 int
170 171 vdev_queue_offset_compare(const void *x1, const void *x2)
171 172 {
172 173 const zio_t *z1 = x1;
173 174 const zio_t *z2 = x2;
174 175
175 176 if (z1->io_offset < z2->io_offset)
176 177 return (-1);
177 178 if (z1->io_offset > z2->io_offset)
178 179 return (1);
179 180
180 181 if (z1 < z2)
181 182 return (-1);
182 183 if (z1 > z2)
183 184 return (1);
184 185
185 186 return (0);
186 187 }
187 188
188 189 int
189 190 vdev_queue_timestamp_compare(const void *x1, const void *x2)
190 191 {
191 192 const zio_t *z1 = x1;
192 193 const zio_t *z2 = x2;
193 194
194 195 if (z1->io_timestamp < z2->io_timestamp)
195 196 return (-1);
196 197 if (z1->io_timestamp > z2->io_timestamp)
197 198 return (1);
198 199
199 200 if (z1 < z2)
200 201 return (-1);
201 202 if (z1 > z2)
202 203 return (1);
203 204
204 205 return (0);
205 206 }
206 207
207 208 void
208 209 vdev_queue_init(vdev_t *vd)
209 210 {
210 211 vdev_queue_t *vq = &vd->vdev_queue;
211 212
212 213 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
213 214 vq->vq_vdev = vd;
214 215
215 216 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
216 217 sizeof (zio_t), offsetof(struct zio, io_queue_node));
217 218
218 219 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
219 220 /*
220 221 * The synchronous i/o queues are FIFO rather than LBA ordered.
221 222 * This provides more consistent latency for these i/os, and
↓ open down ↓ |
184 lines elided |
↑ open up ↑ |
222 223 * they tend to not be tightly clustered anyway so there is
223 224 * little to no throughput loss.
224 225 */
225 226 boolean_t fifo = (p == ZIO_PRIORITY_SYNC_READ ||
226 227 p == ZIO_PRIORITY_SYNC_WRITE);
227 228 avl_create(&vq->vq_class[p].vqc_queued_tree,
228 229 fifo ? vdev_queue_timestamp_compare :
229 230 vdev_queue_offset_compare,
230 231 sizeof (zio_t), offsetof(struct zio, io_queue_node));
231 232 }
233 +
234 + vq->vq_lastoffset = 0;
232 235 }
233 236
234 237 void
235 238 vdev_queue_fini(vdev_t *vd)
236 239 {
237 240 vdev_queue_t *vq = &vd->vdev_queue;
238 241
239 242 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
240 243 avl_destroy(&vq->vq_class[p].vqc_queued_tree);
241 244 avl_destroy(&vq->vq_active_tree);
242 245
243 246 mutex_destroy(&vq->vq_lock);
244 247 }
245 248
246 249 static void
247 250 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
248 251 {
249 252 spa_t *spa = zio->io_spa;
250 253 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
251 254 avl_add(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio);
252 255
253 256 mutex_enter(&spa->spa_iokstat_lock);
254 257 spa->spa_queue_stats[zio->io_priority].spa_queued++;
255 258 if (spa->spa_iokstat != NULL)
256 259 kstat_waitq_enter(spa->spa_iokstat->ks_data);
257 260 mutex_exit(&spa->spa_iokstat_lock);
258 261 }
259 262
260 263 static void
261 264 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
262 265 {
263 266 spa_t *spa = zio->io_spa;
264 267 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
265 268 avl_remove(&vq->vq_class[zio->io_priority].vqc_queued_tree, zio);
266 269
267 270 mutex_enter(&spa->spa_iokstat_lock);
268 271 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
269 272 spa->spa_queue_stats[zio->io_priority].spa_queued--;
270 273 if (spa->spa_iokstat != NULL)
271 274 kstat_waitq_exit(spa->spa_iokstat->ks_data);
272 275 mutex_exit(&spa->spa_iokstat_lock);
273 276 }
274 277
275 278 static void
276 279 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
277 280 {
278 281 spa_t *spa = zio->io_spa;
279 282 ASSERT(MUTEX_HELD(&vq->vq_lock));
280 283 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
281 284 vq->vq_class[zio->io_priority].vqc_active++;
282 285 avl_add(&vq->vq_active_tree, zio);
283 286
284 287 mutex_enter(&spa->spa_iokstat_lock);
285 288 spa->spa_queue_stats[zio->io_priority].spa_active++;
286 289 if (spa->spa_iokstat != NULL)
287 290 kstat_runq_enter(spa->spa_iokstat->ks_data);
288 291 mutex_exit(&spa->spa_iokstat_lock);
289 292 }
290 293
291 294 static void
292 295 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
293 296 {
294 297 spa_t *spa = zio->io_spa;
295 298 ASSERT(MUTEX_HELD(&vq->vq_lock));
296 299 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
297 300 vq->vq_class[zio->io_priority].vqc_active--;
298 301 avl_remove(&vq->vq_active_tree, zio);
299 302
300 303 mutex_enter(&spa->spa_iokstat_lock);
301 304 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
302 305 spa->spa_queue_stats[zio->io_priority].spa_active--;
303 306 if (spa->spa_iokstat != NULL) {
304 307 kstat_io_t *ksio = spa->spa_iokstat->ks_data;
305 308
306 309 kstat_runq_exit(spa->spa_iokstat->ks_data);
307 310 if (zio->io_type == ZIO_TYPE_READ) {
308 311 ksio->reads++;
309 312 ksio->nread += zio->io_size;
310 313 } else if (zio->io_type == ZIO_TYPE_WRITE) {
311 314 ksio->writes++;
312 315 ksio->nwritten += zio->io_size;
313 316 }
314 317 }
315 318 mutex_exit(&spa->spa_iokstat_lock);
316 319 }
317 320
318 321 static void
319 322 vdev_queue_agg_io_done(zio_t *aio)
320 323 {
321 324 if (aio->io_type == ZIO_TYPE_READ) {
322 325 zio_t *pio;
323 326 while ((pio = zio_walk_parents(aio)) != NULL) {
324 327 bcopy((char *)aio->io_data + (pio->io_offset -
325 328 aio->io_offset), pio->io_data, pio->io_size);
326 329 }
327 330 }
328 331
329 332 zio_buf_free(aio->io_data, aio->io_size);
330 333 }
331 334
332 335 static int
333 336 vdev_queue_class_min_active(zio_priority_t p)
334 337 {
335 338 switch (p) {
336 339 case ZIO_PRIORITY_SYNC_READ:
337 340 return (zfs_vdev_sync_read_min_active);
338 341 case ZIO_PRIORITY_SYNC_WRITE:
339 342 return (zfs_vdev_sync_write_min_active);
340 343 case ZIO_PRIORITY_ASYNC_READ:
341 344 return (zfs_vdev_async_read_min_active);
342 345 case ZIO_PRIORITY_ASYNC_WRITE:
343 346 return (zfs_vdev_async_write_min_active);
344 347 case ZIO_PRIORITY_SCRUB:
345 348 return (zfs_vdev_scrub_min_active);
346 349 default:
347 350 panic("invalid priority %u", p);
348 351 return (0);
349 352 }
350 353 }
351 354
352 355 static int
353 356 vdev_queue_max_async_writes(uint64_t dirty)
354 357 {
355 358 int writes;
356 359 uint64_t min_bytes = zfs_dirty_data_max *
357 360 zfs_vdev_async_write_active_min_dirty_percent / 100;
358 361 uint64_t max_bytes = zfs_dirty_data_max *
359 362 zfs_vdev_async_write_active_max_dirty_percent / 100;
360 363
361 364 if (dirty < min_bytes)
362 365 return (zfs_vdev_async_write_min_active);
363 366 if (dirty > max_bytes)
364 367 return (zfs_vdev_async_write_max_active);
365 368
366 369 /*
367 370 * linear interpolation:
368 371 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
369 372 * move right by min_bytes
370 373 * move up by min_writes
371 374 */
372 375 writes = (dirty - min_bytes) *
373 376 (zfs_vdev_async_write_max_active -
374 377 zfs_vdev_async_write_min_active) /
375 378 (max_bytes - min_bytes) +
376 379 zfs_vdev_async_write_min_active;
377 380 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
378 381 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
379 382 return (writes);
380 383 }
381 384
382 385 static int
383 386 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
384 387 {
385 388 switch (p) {
386 389 case ZIO_PRIORITY_SYNC_READ:
387 390 return (zfs_vdev_sync_read_max_active);
388 391 case ZIO_PRIORITY_SYNC_WRITE:
389 392 return (zfs_vdev_sync_write_max_active);
390 393 case ZIO_PRIORITY_ASYNC_READ:
391 394 return (zfs_vdev_async_read_max_active);
392 395 case ZIO_PRIORITY_ASYNC_WRITE:
393 396 return (vdev_queue_max_async_writes(
394 397 spa->spa_dsl_pool->dp_dirty_total));
395 398 case ZIO_PRIORITY_SCRUB:
396 399 return (zfs_vdev_scrub_max_active);
397 400 default:
398 401 panic("invalid priority %u", p);
399 402 return (0);
400 403 }
401 404 }
402 405
403 406 /*
404 407 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
405 408 * there is no eligible class.
406 409 */
407 410 static zio_priority_t
408 411 vdev_queue_class_to_issue(vdev_queue_t *vq)
409 412 {
410 413 spa_t *spa = vq->vq_vdev->vdev_spa;
411 414 zio_priority_t p;
412 415
413 416 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
414 417 return (ZIO_PRIORITY_NUM_QUEUEABLE);
415 418
416 419 /* find a queue that has not reached its minimum # outstanding i/os */
417 420 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
418 421 if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 &&
419 422 vq->vq_class[p].vqc_active <
420 423 vdev_queue_class_min_active(p))
421 424 return (p);
422 425 }
423 426
424 427 /*
425 428 * If we haven't found a queue, look for one that hasn't reached its
426 429 * maximum # outstanding i/os.
427 430 */
428 431 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
429 432 if (avl_numnodes(&vq->vq_class[p].vqc_queued_tree) > 0 &&
430 433 vq->vq_class[p].vqc_active <
431 434 vdev_queue_class_max_active(spa, p))
432 435 return (p);
433 436 }
434 437
435 438 /* No eligible queued i/os */
436 439 return (ZIO_PRIORITY_NUM_QUEUEABLE);
437 440 }
438 441
439 442 /*
440 443 * Compute the range spanned by two i/os, which is the endpoint of the last
441 444 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
442 445 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
443 446 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
444 447 */
445 448 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
446 449 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
447 450
448 451 static zio_t *
449 452 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
450 453 {
451 454 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
452 455 uint64_t maxgap = 0;
453 456 uint64_t size;
454 457 boolean_t stretch = B_FALSE;
455 458 vdev_queue_class_t *vqc = &vq->vq_class[zio->io_priority];
456 459 avl_tree_t *t = &vqc->vqc_queued_tree;
457 460 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
458 461
459 462 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
460 463 return (NULL);
461 464
462 465 /*
463 466 * The synchronous i/o queues are not sorted by LBA, so we can't
464 467 * find adjacent i/os. These i/os tend to not be tightly clustered,
465 468 * or too large to aggregate, so this has little impact on performance.
466 469 */
467 470 if (zio->io_priority == ZIO_PRIORITY_SYNC_READ ||
468 471 zio->io_priority == ZIO_PRIORITY_SYNC_WRITE)
469 472 return (NULL);
470 473
471 474 first = last = zio;
472 475
473 476 if (zio->io_type == ZIO_TYPE_READ)
474 477 maxgap = zfs_vdev_read_gap_limit;
475 478
476 479 /*
477 480 * We can aggregate I/Os that are sufficiently adjacent and of
478 481 * the same flavor, as expressed by the AGG_INHERIT flags.
479 482 * The latter requirement is necessary so that certain
480 483 * attributes of the I/O, such as whether it's a normal I/O
481 484 * or a scrub/resilver, can be preserved in the aggregate.
482 485 * We can include optional I/Os, but don't allow them
483 486 * to begin a range as they add no benefit in that situation.
484 487 */
485 488
486 489 /*
487 490 * We keep track of the last non-optional I/O.
488 491 */
489 492 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
490 493
491 494 /*
492 495 * Walk backwards through sufficiently contiguous I/Os
493 496 * recording the last non-option I/O.
494 497 */
495 498 while ((dio = AVL_PREV(t, first)) != NULL &&
496 499 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
497 500 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
498 501 IO_GAP(dio, first) <= maxgap) {
499 502 first = dio;
500 503 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
501 504 mandatory = first;
502 505 }
503 506
504 507 /*
505 508 * Skip any initial optional I/Os.
506 509 */
507 510 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
508 511 first = AVL_NEXT(t, first);
509 512 ASSERT(first != NULL);
510 513 }
511 514
512 515 /*
513 516 * Walk forward through sufficiently contiguous I/Os.
514 517 */
515 518 while ((dio = AVL_NEXT(t, last)) != NULL &&
516 519 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
517 520 IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
518 521 IO_GAP(last, dio) <= maxgap) {
519 522 last = dio;
520 523 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
521 524 mandatory = last;
522 525 }
523 526
524 527 /*
525 528 * Now that we've established the range of the I/O aggregation
526 529 * we must decide what to do with trailing optional I/Os.
527 530 * For reads, there's nothing to do. While we are unable to
528 531 * aggregate further, it's possible that a trailing optional
529 532 * I/O would allow the underlying device to aggregate with
530 533 * subsequent I/Os. We must therefore determine if the next
531 534 * non-optional I/O is close enough to make aggregation
532 535 * worthwhile.
533 536 */
534 537 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
535 538 zio_t *nio = last;
536 539 while ((dio = AVL_NEXT(t, nio)) != NULL &&
537 540 IO_GAP(nio, dio) == 0 &&
538 541 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
539 542 nio = dio;
540 543 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
541 544 stretch = B_TRUE;
542 545 break;
543 546 }
544 547 }
545 548 }
546 549
547 550 if (stretch) {
548 551 /* This may be a no-op. */
549 552 dio = AVL_NEXT(t, last);
550 553 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
551 554 } else {
552 555 while (last != mandatory && last != first) {
553 556 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
554 557 last = AVL_PREV(t, last);
555 558 ASSERT(last != NULL);
556 559 }
557 560 }
558 561
559 562 if (first == last)
560 563 return (NULL);
561 564
562 565 size = IO_SPAN(first, last);
563 566 ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
564 567
565 568 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
566 569 zio_buf_alloc(size), size, first->io_type, zio->io_priority,
567 570 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
568 571 vdev_queue_agg_io_done, NULL);
569 572 aio->io_timestamp = first->io_timestamp;
570 573
571 574 nio = first;
572 575 do {
573 576 dio = nio;
574 577 nio = AVL_NEXT(t, dio);
575 578 ASSERT3U(dio->io_type, ==, aio->io_type);
576 579
577 580 if (dio->io_flags & ZIO_FLAG_NODATA) {
578 581 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
579 582 bzero((char *)aio->io_data + (dio->io_offset -
580 583 aio->io_offset), dio->io_size);
581 584 } else if (dio->io_type == ZIO_TYPE_WRITE) {
582 585 bcopy(dio->io_data, (char *)aio->io_data +
583 586 (dio->io_offset - aio->io_offset),
584 587 dio->io_size);
585 588 }
586 589
587 590 zio_add_child(dio, aio);
588 591 vdev_queue_io_remove(vq, dio);
589 592 zio_vdev_io_bypass(dio);
590 593 zio_execute(dio);
591 594 } while (dio != last);
592 595
593 596 return (aio);
594 597 }
595 598
596 599 static zio_t *
597 600 vdev_queue_io_to_issue(vdev_queue_t *vq)
598 601 {
599 602 zio_t *zio, *aio;
600 603 zio_priority_t p;
601 604 avl_index_t idx;
602 605 vdev_queue_class_t *vqc;
603 606 zio_t search;
604 607
605 608 again:
606 609 ASSERT(MUTEX_HELD(&vq->vq_lock));
607 610
608 611 p = vdev_queue_class_to_issue(vq);
609 612
610 613 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
611 614 /* No eligible queued i/os */
612 615 return (NULL);
613 616 }
614 617
615 618 /*
616 619 * For LBA-ordered queues (async / scrub), issue the i/o which follows
617 620 * the most recently issued i/o in LBA (offset) order.
618 621 *
619 622 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
620 623 */
621 624 vqc = &vq->vq_class[p];
622 625 search.io_timestamp = 0;
623 626 search.io_offset = vq->vq_last_offset + 1;
624 627 VERIFY3P(avl_find(&vqc->vqc_queued_tree, &search, &idx), ==, NULL);
625 628 zio = avl_nearest(&vqc->vqc_queued_tree, idx, AVL_AFTER);
626 629 if (zio == NULL)
627 630 zio = avl_first(&vqc->vqc_queued_tree);
628 631 ASSERT3U(zio->io_priority, ==, p);
629 632
630 633 aio = vdev_queue_aggregate(vq, zio);
631 634 if (aio != NULL)
632 635 zio = aio;
633 636 else
634 637 vdev_queue_io_remove(vq, zio);
635 638
636 639 /*
637 640 * If the I/O is or was optional and therefore has no data, we need to
638 641 * simply discard it. We need to drop the vdev queue's lock to avoid a
639 642 * deadlock that we could encounter since this I/O will complete
640 643 * immediately.
641 644 */
642 645 if (zio->io_flags & ZIO_FLAG_NODATA) {
643 646 mutex_exit(&vq->vq_lock);
644 647 zio_vdev_io_bypass(zio);
645 648 zio_execute(zio);
646 649 mutex_enter(&vq->vq_lock);
647 650 goto again;
648 651 }
649 652
650 653 vdev_queue_pending_add(vq, zio);
651 654 vq->vq_last_offset = zio->io_offset;
652 655
653 656 return (zio);
654 657 }
655 658
656 659 zio_t *
657 660 vdev_queue_io(zio_t *zio)
658 661 {
659 662 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
660 663 zio_t *nio;
661 664
662 665 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
663 666 return (zio);
664 667
665 668 /*
666 669 * Children i/os inherent their parent's priority, which might
667 670 * not match the child's i/o type. Fix it up here.
668 671 */
669 672 if (zio->io_type == ZIO_TYPE_READ) {
670 673 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
671 674 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
672 675 zio->io_priority != ZIO_PRIORITY_SCRUB)
673 676 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
674 677 } else {
675 678 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
676 679 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
677 680 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
678 681 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
679 682 }
680 683
681 684 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
682 685
683 686 mutex_enter(&vq->vq_lock);
684 687 zio->io_timestamp = gethrtime();
685 688 vdev_queue_io_add(vq, zio);
686 689 nio = vdev_queue_io_to_issue(vq);
687 690 mutex_exit(&vq->vq_lock);
688 691
689 692 if (nio == NULL)
690 693 return (NULL);
691 694
692 695 if (nio->io_done == vdev_queue_agg_io_done) {
693 696 zio_nowait(nio);
694 697 return (NULL);
695 698 }
696 699
697 700 return (nio);
698 701 }
699 702
700 703 void
701 704 vdev_queue_io_done(zio_t *zio)
702 705 {
703 706 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
704 707 zio_t *nio;
705 708
706 709 if (zio_injection_enabled)
707 710 delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
708 711
709 712 mutex_enter(&vq->vq_lock);
710 713
711 714 vdev_queue_pending_remove(vq, zio);
712 715
713 716 vq->vq_io_complete_ts = gethrtime();
714 717
715 718 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
716 719 mutex_exit(&vq->vq_lock);
717 720 if (nio->io_done == vdev_queue_agg_io_done) {
↓ open down ↓ |
476 lines elided |
↑ open up ↑ |
718 721 zio_nowait(nio);
719 722 } else {
720 723 zio_vdev_io_reissue(nio);
721 724 zio_execute(nio);
722 725 }
723 726 mutex_enter(&vq->vq_lock);
724 727 }
725 728
726 729 mutex_exit(&vq->vq_lock);
727 730 }
731 +
732 +/*
733 + * As these three methods are only used for load calculations we're not
734 + * concerned if we get an incorrect value on 32bit platforms due to lack of
735 + * vq_lock mutex use here, instead we prefer to keep it lock free for
736 + * performance.
737 + */
738 +int
739 +vdev_queue_length(vdev_t *vd)
740 +{
741 + return (avl_numnodes(&vd->vdev_queue.vq_pending_tree));
742 +}
743 +
744 +uint64_t
745 +vdev_queue_lastoffset(vdev_t *vd)
746 +{
747 + return (vd->vdev_queue.vq_lastoffset);
748 +}
749 +
750 +void
751 +vdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio)
752 +{
753 + vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size;
754 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX