2008-11-20 20:01:55 +00:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
/*
|
2010-05-28 20:45:14 +00:00
|
|
|
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
|
2008-11-20 20:01:55 +00:00
|
|
|
* Use is subject to license terms.
|
|
|
|
*/
|
|
|
|
|
Illumos #4045 write throttle & i/o scheduler performance work
4045 zfs write throttle & i/o scheduler performance work
1. The ZFS i/o scheduler (vdev_queue.c) now divides i/os into 5 classes: sync
read, sync write, async read, async write, and scrub/resilver. The scheduler
issues a number of concurrent i/os from each class to the device. Once a class
has been selected, an i/o is selected from this class using either an elevator
algorithem (async, scrub classes) or FIFO (sync classes). The number of
concurrent async write i/os is tuned dynamically based on i/o load, to achieve
good sync i/o latency when there is not a high load of writes, and good write
throughput when there is. See the block comment in vdev_queue.c (reproduced
below) for more details.
2. The write throttle (dsl_pool_tempreserve_space() and
txg_constrain_throughput()) is rewritten to produce much more consistent delays
when under constant load. The new write throttle is based on the amount of
dirty data, rather than guesses about future performance of the system. When
there is a lot of dirty data, each transaction (e.g. write() syscall) will be
delayed by the same small amount. This eliminates the "brick wall of wait"
that the old write throttle could hit, causing all transactions to wait several
seconds until the next txg opens. One of the keys to the new write throttle is
decrementing the amount of dirty data as i/o completes, rather than at the end
of spa_sync(). Note that the write throttle is only applied once the i/o
scheduler is issuing the maximum number of outstanding async writes. See the
block comments in dsl_pool.c and above dmu_tx_delay() (reproduced below) for
more details.
This diff has several other effects, including:
* the commonly-tuned global variable zfs_vdev_max_pending has been removed;
use per-class zfs_vdev_*_max_active values or zfs_vdev_max_active instead.
* the size of each txg (meaning the amount of dirty data written, and thus the
time it takes to write out) is now controlled differently. There is no longer
an explicit time goal; the primary determinant is amount of dirty data.
Systems that are under light or medium load will now often see that a txg is
always syncing, but the impact to performance (e.g. read latency) is minimal.
Tune zfs_dirty_data_max and zfs_dirty_data_sync to control this.
* zio_taskq_batch_pct = 75 -- Only use 75% of all CPUs for compression,
checksum, etc. This improves latency by not allowing these CPU-intensive tasks
to consume all CPU (on machines with at least 4 CPU's; the percentage is
rounded up).
--matt
APPENDIX: problems with the current i/o scheduler
The current ZFS i/o scheduler (vdev_queue.c) is deadline based. The problem
with this is that if there are always i/os pending, then certain classes of
i/os can see very long delays.
For example, if there are always synchronous reads outstanding, then no async
writes will be serviced until they become "past due". One symptom of this
situation is that each pass of the txg sync takes at least several seconds
(typically 3 seconds).
If many i/os become "past due" (their deadline is in the past), then we must
service all of these overdue i/os before any new i/os. This happens when we
enqueue a batch of async writes for the txg sync, with deadlines 2.5 seconds in
the future. If we can't complete all the i/os in 2.5 seconds (e.g. because
there were always reads pending), then these i/os will become past due. Now we
must service all the "async" writes (which could be hundreds of megabytes)
before we service any reads, introducing considerable latency to synchronous
i/os (reads or ZIL writes).
Notes on porting to ZFS on Linux:
- zio_t gained new members io_physdone and io_phys_children. Because
object caches in the Linux port call the constructor only once at
allocation time, objects may contain residual data when retrieved
from the cache. Therefore zio_create() was updated to zero out the two
new fields.
- vdev_mirror_pending() relied on the depth of the per-vdev pending queue
(vq->vq_pending_tree) to select the least-busy leaf vdev to read from.
This tree has been replaced by vq->vq_active_tree which is now used
for the same purpose.
- vdev_queue_init() used the value of zfs_vdev_max_pending to determine
the number of vdev I/O buffers to pre-allocate. That global no longer
exists, so we instead use the sum of the *_max_active values for each of
the five I/O classes described above.
- The Illumos implementation of dmu_tx_delay() delays a transaction by
sleeping in condition variable embedded in the thread
(curthread->t_delay_cv). We do not have an equivalent CV to use in
Linux, so this change replaced the delay logic with a wrapper called
zfs_sleep_until(). This wrapper could be adopted upstream and in other
downstream ports to abstract away operating system-specific delay logic.
- These tunables are added as module parameters, and descriptions added
to the zfs-module-parameters.5 man page.
spa_asize_inflation
zfs_deadman_synctime_ms
zfs_vdev_max_active
zfs_vdev_async_write_active_min_dirty_percent
zfs_vdev_async_write_active_max_dirty_percent
zfs_vdev_async_read_max_active
zfs_vdev_async_read_min_active
zfs_vdev_async_write_max_active
zfs_vdev_async_write_min_active
zfs_vdev_scrub_max_active
zfs_vdev_scrub_min_active
zfs_vdev_sync_read_max_active
zfs_vdev_sync_read_min_active
zfs_vdev_sync_write_max_active
zfs_vdev_sync_write_min_active
zfs_dirty_data_max_percent
zfs_delay_min_dirty_percent
zfs_dirty_data_max_max_percent
zfs_dirty_data_max
zfs_dirty_data_max_max
zfs_dirty_data_sync
zfs_delay_scale
The latter four have type unsigned long, whereas they are uint64_t in
Illumos. This accommodates Linux's module_param() supported types, but
means they may overflow on 32-bit architectures.
The values zfs_dirty_data_max and zfs_dirty_data_max_max are the most
likely to overflow on 32-bit systems, since they express physical RAM
sizes in bytes. In fact, Illumos initializes zfs_dirty_data_max_max to
2^32 which does overflow. To resolve that, this port instead initializes
it in arc_init() to 25% of physical RAM, and adds the tunable
zfs_dirty_data_max_max_percent to override that percentage. While this
solution doesn't completely avoid the overflow issue, it should be a
reasonable default for most systems, and the minority of affected
systems can work around the issue by overriding the defaults.
- Fixed reversed logic in comment above zfs_delay_scale declaration.
- Clarified comments in vdev_queue.c regarding when per-queue minimums take
effect.
- Replaced dmu_tx_write_limit in the dmu_tx kstat file
with dmu_tx_dirty_delay and dmu_tx_dirty_over_max. The first counts
how many times a transaction has been delayed because the pool dirty
data has exceeded zfs_delay_min_dirty_percent. The latter counts how
many times the pool dirty data has exceeded zfs_dirty_data_max (which
we expect to never happen).
- The original patch would have regressed the bug fixed in
zfsonlinux/zfs@c418410, which prevented users from setting the
zfs_vdev_aggregation_limit tuning larger than SPA_MAXBLOCKSIZE.
A similar fix is added to vdev_queue_aggregate().
- In vdev_queue_io_to_issue(), dynamically allocate 'zio_t search' on the
heap instead of the stack. In Linux we can't afford such large
structures on the stack.
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Ned Bass <bass6@llnl.gov>
Reviewed by: Brendan Gregg <brendan.gregg@joyent.com>
Approved by: Robert Mustacchi <rm@joyent.com>
References:
http://www.illumos.org/issues/4045
illumos/illumos-gate@69962b5647e4a8b9b14998733b765925381b727e
Ported-by: Ned Bass <bass6@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1913
2013-08-29 03:01:20 +00:00
|
|
|
/*
|
2016-01-09 16:19:10 +00:00
|
|
|
* Copyright (c) 2013, 2015 by Delphix. All rights reserved.
|
Illumos #4045 write throttle & i/o scheduler performance work
4045 zfs write throttle & i/o scheduler performance work
1. The ZFS i/o scheduler (vdev_queue.c) now divides i/os into 5 classes: sync
read, sync write, async read, async write, and scrub/resilver. The scheduler
issues a number of concurrent i/os from each class to the device. Once a class
has been selected, an i/o is selected from this class using either an elevator
algorithem (async, scrub classes) or FIFO (sync classes). The number of
concurrent async write i/os is tuned dynamically based on i/o load, to achieve
good sync i/o latency when there is not a high load of writes, and good write
throughput when there is. See the block comment in vdev_queue.c (reproduced
below) for more details.
2. The write throttle (dsl_pool_tempreserve_space() and
txg_constrain_throughput()) is rewritten to produce much more consistent delays
when under constant load. The new write throttle is based on the amount of
dirty data, rather than guesses about future performance of the system. When
there is a lot of dirty data, each transaction (e.g. write() syscall) will be
delayed by the same small amount. This eliminates the "brick wall of wait"
that the old write throttle could hit, causing all transactions to wait several
seconds until the next txg opens. One of the keys to the new write throttle is
decrementing the amount of dirty data as i/o completes, rather than at the end
of spa_sync(). Note that the write throttle is only applied once the i/o
scheduler is issuing the maximum number of outstanding async writes. See the
block comments in dsl_pool.c and above dmu_tx_delay() (reproduced below) for
more details.
This diff has several other effects, including:
* the commonly-tuned global variable zfs_vdev_max_pending has been removed;
use per-class zfs_vdev_*_max_active values or zfs_vdev_max_active instead.
* the size of each txg (meaning the amount of dirty data written, and thus the
time it takes to write out) is now controlled differently. There is no longer
an explicit time goal; the primary determinant is amount of dirty data.
Systems that are under light or medium load will now often see that a txg is
always syncing, but the impact to performance (e.g. read latency) is minimal.
Tune zfs_dirty_data_max and zfs_dirty_data_sync to control this.
* zio_taskq_batch_pct = 75 -- Only use 75% of all CPUs for compression,
checksum, etc. This improves latency by not allowing these CPU-intensive tasks
to consume all CPU (on machines with at least 4 CPU's; the percentage is
rounded up).
--matt
APPENDIX: problems with the current i/o scheduler
The current ZFS i/o scheduler (vdev_queue.c) is deadline based. The problem
with this is that if there are always i/os pending, then certain classes of
i/os can see very long delays.
For example, if there are always synchronous reads outstanding, then no async
writes will be serviced until they become "past due". One symptom of this
situation is that each pass of the txg sync takes at least several seconds
(typically 3 seconds).
If many i/os become "past due" (their deadline is in the past), then we must
service all of these overdue i/os before any new i/os. This happens when we
enqueue a batch of async writes for the txg sync, with deadlines 2.5 seconds in
the future. If we can't complete all the i/os in 2.5 seconds (e.g. because
there were always reads pending), then these i/os will become past due. Now we
must service all the "async" writes (which could be hundreds of megabytes)
before we service any reads, introducing considerable latency to synchronous
i/os (reads or ZIL writes).
Notes on porting to ZFS on Linux:
- zio_t gained new members io_physdone and io_phys_children. Because
object caches in the Linux port call the constructor only once at
allocation time, objects may contain residual data when retrieved
from the cache. Therefore zio_create() was updated to zero out the two
new fields.
- vdev_mirror_pending() relied on the depth of the per-vdev pending queue
(vq->vq_pending_tree) to select the least-busy leaf vdev to read from.
This tree has been replaced by vq->vq_active_tree which is now used
for the same purpose.
- vdev_queue_init() used the value of zfs_vdev_max_pending to determine
the number of vdev I/O buffers to pre-allocate. That global no longer
exists, so we instead use the sum of the *_max_active values for each of
the five I/O classes described above.
- The Illumos implementation of dmu_tx_delay() delays a transaction by
sleeping in condition variable embedded in the thread
(curthread->t_delay_cv). We do not have an equivalent CV to use in
Linux, so this change replaced the delay logic with a wrapper called
zfs_sleep_until(). This wrapper could be adopted upstream and in other
downstream ports to abstract away operating system-specific delay logic.
- These tunables are added as module parameters, and descriptions added
to the zfs-module-parameters.5 man page.
spa_asize_inflation
zfs_deadman_synctime_ms
zfs_vdev_max_active
zfs_vdev_async_write_active_min_dirty_percent
zfs_vdev_async_write_active_max_dirty_percent
zfs_vdev_async_read_max_active
zfs_vdev_async_read_min_active
zfs_vdev_async_write_max_active
zfs_vdev_async_write_min_active
zfs_vdev_scrub_max_active
zfs_vdev_scrub_min_active
zfs_vdev_sync_read_max_active
zfs_vdev_sync_read_min_active
zfs_vdev_sync_write_max_active
zfs_vdev_sync_write_min_active
zfs_dirty_data_max_percent
zfs_delay_min_dirty_percent
zfs_dirty_data_max_max_percent
zfs_dirty_data_max
zfs_dirty_data_max_max
zfs_dirty_data_sync
zfs_delay_scale
The latter four have type unsigned long, whereas they are uint64_t in
Illumos. This accommodates Linux's module_param() supported types, but
means they may overflow on 32-bit architectures.
The values zfs_dirty_data_max and zfs_dirty_data_max_max are the most
likely to overflow on 32-bit systems, since they express physical RAM
sizes in bytes. In fact, Illumos initializes zfs_dirty_data_max_max to
2^32 which does overflow. To resolve that, this port instead initializes
it in arc_init() to 25% of physical RAM, and adds the tunable
zfs_dirty_data_max_max_percent to override that percentage. While this
solution doesn't completely avoid the overflow issue, it should be a
reasonable default for most systems, and the minority of affected
systems can work around the issue by overriding the defaults.
- Fixed reversed logic in comment above zfs_delay_scale declaration.
- Clarified comments in vdev_queue.c regarding when per-queue minimums take
effect.
- Replaced dmu_tx_write_limit in the dmu_tx kstat file
with dmu_tx_dirty_delay and dmu_tx_dirty_over_max. The first counts
how many times a transaction has been delayed because the pool dirty
data has exceeded zfs_delay_min_dirty_percent. The latter counts how
many times the pool dirty data has exceeded zfs_dirty_data_max (which
we expect to never happen).
- The original patch would have regressed the bug fixed in
zfsonlinux/zfs@c418410, which prevented users from setting the
zfs_vdev_aggregation_limit tuning larger than SPA_MAXBLOCKSIZE.
A similar fix is added to vdev_queue_aggregate().
- In vdev_queue_io_to_issue(), dynamically allocate 'zio_t search' on the
heap instead of the stack. In Linux we can't afford such large
structures on the stack.
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Ned Bass <bass6@llnl.gov>
Reviewed by: Brendan Gregg <brendan.gregg@joyent.com>
Approved by: Robert Mustacchi <rm@joyent.com>
References:
http://www.illumos.org/issues/4045
illumos/illumos-gate@69962b5647e4a8b9b14998733b765925381b727e
Ported-by: Ned Bass <bass6@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #1913
2013-08-29 03:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2008-11-20 20:01:55 +00:00
|
|
|
#include <sys/zfs_context.h>
|
|
|
|
#include <sys/dnode.h>
|
|
|
|
#include <sys/dmu_objset.h>
|
|
|
|
#include <sys/dmu_zfetch.h>
|
|
|
|
#include <sys/dmu.h>
|
|
|
|
#include <sys/dbuf.h>
|
2010-05-28 20:45:14 +00:00
|
|
|
#include <sys/kstat.h>
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/*
|
2015-12-26 21:10:31 +00:00
|
|
|
* This tunable disables predictive prefetch. Note that it leaves "prescient"
|
|
|
|
* prefetch (e.g. prefetch for zfs send) intact. Unlike predictive prefetch,
|
|
|
|
* prescient prefetch never issues i/os that end up not being needed,
|
|
|
|
* so it can't hurt performance.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
int zfs_prefetch_disable = B_FALSE;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
/* max # of streams per zfetch */
|
2011-05-03 22:09:28 +00:00
|
|
|
unsigned int zfetch_max_streams = 8;
|
2008-11-20 20:01:55 +00:00
|
|
|
/* min time before stream reclaim */
|
2011-05-03 22:09:28 +00:00
|
|
|
unsigned int zfetch_min_sec_reap = 2;
|
2015-12-26 21:10:31 +00:00
|
|
|
/* max bytes to prefetch per stream (default 8MB) */
|
|
|
|
unsigned int zfetch_max_distance = 8 * 1024 * 1024;
|
2016-01-09 16:19:10 +00:00
|
|
|
/* max number of bytes in an array_read in which we allow prefetching (1MB) */
|
2011-05-03 22:09:28 +00:00
|
|
|
unsigned long zfetch_array_rd_sz = 1024 * 1024;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2010-05-28 20:45:14 +00:00
|
|
|
typedef struct zfetch_stats {
|
|
|
|
kstat_named_t zfetchstat_hits;
|
|
|
|
kstat_named_t zfetchstat_misses;
|
2015-12-26 21:10:31 +00:00
|
|
|
kstat_named_t zfetchstat_max_streams;
|
2010-05-28 20:45:14 +00:00
|
|
|
} zfetch_stats_t;
|
|
|
|
|
|
|
|
static zfetch_stats_t zfetch_stats = {
|
|
|
|
{ "hits", KSTAT_DATA_UINT64 },
|
|
|
|
{ "misses", KSTAT_DATA_UINT64 },
|
2015-12-26 21:10:31 +00:00
|
|
|
{ "max_streams", KSTAT_DATA_UINT64 },
|
2010-05-28 20:45:14 +00:00
|
|
|
};
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
#define ZFETCHSTAT_BUMP(stat) \
|
|
|
|
atomic_inc_64(&zfetch_stats.stat.value.ui64);
|
2010-05-28 20:45:14 +00:00
|
|
|
|
|
|
|
kstat_t *zfetch_ksp;
|
|
|
|
|
|
|
|
void
|
|
|
|
zfetch_init(void)
|
|
|
|
{
|
|
|
|
zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
|
|
|
|
KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
|
|
|
|
KSTAT_FLAG_VIRTUAL);
|
|
|
|
|
|
|
|
if (zfetch_ksp != NULL) {
|
|
|
|
zfetch_ksp->ks_data = &zfetch_stats;
|
|
|
|
kstat_install(zfetch_ksp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zfetch_fini(void)
|
|
|
|
{
|
|
|
|
if (zfetch_ksp != NULL) {
|
|
|
|
kstat_delete(zfetch_ksp);
|
|
|
|
zfetch_ksp = NULL;
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This takes a pointer to a zfetch structure and a dnode. It performs the
|
|
|
|
* necessary setup for the zfetch structure, grokking data from the
|
|
|
|
* associated dnode.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
|
|
|
|
{
|
2015-12-26 21:10:31 +00:00
|
|
|
if (zf == NULL)
|
2008-11-20 20:01:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
zf->zf_dnode = dno;
|
|
|
|
|
|
|
|
list_create(&zf->zf_stream, sizeof (zstream_t),
|
2015-12-26 21:10:31 +00:00
|
|
|
offsetof(zstream_t, zs_node));
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
|
|
|
|
}
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
static void
|
|
|
|
dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2015-12-26 21:10:31 +00:00
|
|
|
ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
|
|
|
|
list_remove(&zf->zf_stream, zs);
|
|
|
|
mutex_destroy(&zs->zs_lock);
|
|
|
|
kmem_free(zs, sizeof (*zs));
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-12-26 21:10:31 +00:00
|
|
|
* Clean-up state associated with a zfetch structure (e.g. destroy the
|
|
|
|
* streams). This doesn't free the zfetch_t itself, that's left to the caller.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
|
|
|
void
|
2015-12-26 21:10:31 +00:00
|
|
|
dmu_zfetch_fini(zfetch_t *zf)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2015-12-26 21:10:31 +00:00
|
|
|
zstream_t *zs;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
rw_enter(&zf->zf_rwlock, RW_WRITER);
|
|
|
|
while ((zs = list_head(&zf->zf_stream)) != NULL)
|
|
|
|
dmu_zfetch_stream_remove(zf, zs);
|
|
|
|
rw_exit(&zf->zf_rwlock);
|
2008-11-20 20:01:55 +00:00
|
|
|
list_destroy(&zf->zf_stream);
|
|
|
|
rw_destroy(&zf->zf_rwlock);
|
|
|
|
|
|
|
|
zf->zf_dnode = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-12-26 21:10:31 +00:00
|
|
|
* If there aren't too many streams already, create a new stream.
|
|
|
|
* The "blkid" argument is the next block that we expect this stream to access.
|
|
|
|
* While we're here, clean up old streams (which haven't been
|
|
|
|
* accessed for at least zfetch_min_sec_reap seconds).
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2015-12-26 21:10:31 +00:00
|
|
|
static void
|
|
|
|
dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2015-12-26 21:10:31 +00:00
|
|
|
zstream_t *zs;
|
|
|
|
zstream_t *zs_next;
|
|
|
|
int numstreams = 0;
|
|
|
|
uint32_t max_streams;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* Clean up old streams.
|
|
|
|
*/
|
|
|
|
for (zs = list_head(&zf->zf_stream);
|
|
|
|
zs != NULL; zs = zs_next) {
|
|
|
|
zs_next = list_next(&zf->zf_stream, zs);
|
|
|
|
if (((gethrtime() - zs->zs_atime) / NANOSEC) >
|
|
|
|
zfetch_min_sec_reap)
|
|
|
|
dmu_zfetch_stream_remove(zf, zs);
|
|
|
|
else
|
|
|
|
numstreams++;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* The maximum number of streams is normally zfetch_max_streams,
|
|
|
|
* but for small files we lower it such that it's at least possible
|
|
|
|
* for all the streams to be non-overlapping.
|
|
|
|
*
|
|
|
|
* If we are already at the maximum number of streams for this file,
|
|
|
|
* even after removing old streams, then don't create this stream.
|
|
|
|
*/
|
|
|
|
max_streams = MAX(1, MIN(zfetch_max_streams,
|
|
|
|
zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz /
|
|
|
|
zfetch_max_distance));
|
|
|
|
if (numstreams >= max_streams) {
|
|
|
|
ZFETCHSTAT_BUMP(zfetchstat_max_streams);
|
|
|
|
return;
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
|
|
|
|
zs->zs_blkid = blkid;
|
|
|
|
zs->zs_pf_blkid = blkid;
|
|
|
|
zs->zs_atime = gethrtime();
|
|
|
|
mutex_init(&zs->zs_lock, NULL, MUTEX_DEFAULT, NULL);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
list_insert_head(&zf->zf_stream, zs);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the prefetch entry point. It calls all of the other dmu_zfetch
|
|
|
|
* routines to create, delete, find, or operate upon prefetch streams.
|
|
|
|
*/
|
|
|
|
void
|
2015-12-26 21:10:31 +00:00
|
|
|
dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks)
|
2008-11-20 20:01:55 +00:00
|
|
|
{
|
2015-12-26 21:10:31 +00:00
|
|
|
zstream_t *zs;
|
|
|
|
int64_t pf_start;
|
|
|
|
int pf_nblks;
|
|
|
|
int i;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
|
|
|
if (zfs_prefetch_disable)
|
|
|
|
return;
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* As a fast path for small (single-block) files, ignore access
|
|
|
|
* to the first block.
|
|
|
|
*/
|
|
|
|
if (blkid == 0)
|
2008-11-20 20:01:55 +00:00
|
|
|
return;
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
rw_enter(&zf->zf_rwlock, RW_READER);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
for (zs = list_head(&zf->zf_stream); zs != NULL;
|
|
|
|
zs = list_next(&zf->zf_stream, zs)) {
|
|
|
|
if (blkid == zs->zs_blkid) {
|
|
|
|
mutex_enter(&zs->zs_lock);
|
|
|
|
/*
|
|
|
|
* zs_blkid could have changed before we
|
|
|
|
* acquired zs_lock; re-check them here.
|
|
|
|
*/
|
|
|
|
if (blkid != zs->zs_blkid) {
|
|
|
|
mutex_exit(&zs->zs_lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2010-05-28 20:45:14 +00:00
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
if (zs == NULL) {
|
2008-11-20 20:01:55 +00:00
|
|
|
/*
|
2015-12-26 21:10:31 +00:00
|
|
|
* This access is not part of any existing stream. Create
|
|
|
|
* a new stream for it.
|
2008-11-20 20:01:55 +00:00
|
|
|
*/
|
2015-12-26 21:10:31 +00:00
|
|
|
ZFETCHSTAT_BUMP(zfetchstat_misses);
|
|
|
|
if (rw_tryupgrade(&zf->zf_rwlock))
|
|
|
|
dmu_zfetch_stream_create(zf, blkid + nblks);
|
|
|
|
rw_exit(&zf->zf_rwlock);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* This access was to a block that we issued a prefetch for on
|
|
|
|
* behalf of this stream. Issue further prefetches for this stream.
|
|
|
|
*
|
|
|
|
* Normally, we start prefetching where we stopped
|
|
|
|
* prefetching last (zs_pf_blkid). But when we get our first
|
|
|
|
* hit on this stream, zs_pf_blkid == zs_blkid, we don't
|
|
|
|
* want to prefetch to block we just accessed. In this case,
|
|
|
|
* start just after the block we just accessed.
|
|
|
|
*/
|
|
|
|
pf_start = MAX(zs->zs_pf_blkid, blkid + nblks);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* Double our amount of prefetched data, but don't let the
|
|
|
|
* prefetch get further ahead than zfetch_max_distance.
|
|
|
|
*/
|
|
|
|
pf_nblks =
|
|
|
|
MIN((int64_t)zs->zs_pf_blkid - zs->zs_blkid + nblks,
|
|
|
|
zs->zs_blkid + nblks +
|
|
|
|
(zfetch_max_distance >> zf->zf_dnode->dn_datablkshift) - pf_start);
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
zs->zs_pf_blkid = pf_start + pf_nblks;
|
|
|
|
zs->zs_atime = gethrtime();
|
|
|
|
zs->zs_blkid = blkid + nblks;
|
2008-11-20 20:01:55 +00:00
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
/*
|
|
|
|
* dbuf_prefetch() issues the prefetch i/o
|
|
|
|
* asynchronously, but it may need to wait for an
|
|
|
|
* indirect block to be read from disk. Therefore
|
|
|
|
* we do not want to hold any locks while we call it.
|
|
|
|
*/
|
|
|
|
mutex_exit(&zs->zs_lock);
|
|
|
|
rw_exit(&zf->zf_rwlock);
|
|
|
|
for (i = 0; i < pf_nblks; i++) {
|
|
|
|
dbuf_prefetch(zf->zf_dnode, 0, pf_start + i,
|
|
|
|
ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2015-12-26 21:10:31 +00:00
|
|
|
ZFETCHSTAT_BUMP(zfetchstat_hits);
|
2008-11-20 20:01:55 +00:00
|
|
|
}
|
2010-08-26 18:49:16 +00:00
|
|
|
|
|
|
|
#if defined(_KERNEL) && defined(HAVE_SPL)
|
|
|
|
module_param(zfs_prefetch_disable, int, 0644);
|
|
|
|
MODULE_PARM_DESC(zfs_prefetch_disable, "Disable all ZFS prefetching");
|
2011-05-03 22:09:28 +00:00
|
|
|
|
|
|
|
module_param(zfetch_max_streams, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(zfetch_max_streams, "Max number of streams per zfetch");
|
|
|
|
|
|
|
|
module_param(zfetch_min_sec_reap, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(zfetch_min_sec_reap, "Min time before stream reclaim");
|
|
|
|
|
2015-12-26 21:10:31 +00:00
|
|
|
module_param(zfetch_max_distance, uint, 0644);
|
|
|
|
MODULE_PARM_DESC(zfetch_max_distance,
|
|
|
|
"Max bytes to prefetch per stream (default 8MB)");
|
2011-05-03 22:09:28 +00:00
|
|
|
|
|
|
|
module_param(zfetch_array_rd_sz, ulong, 0644);
|
|
|
|
MODULE_PARM_DESC(zfetch_array_rd_sz, "Number of bytes in a array_read");
|
2010-08-26 18:49:16 +00:00
|
|
|
#endif
|