arc.c revision 3a737e0dbe1535527c59ef625c9a252897b0b12a
/*
* 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
* 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
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* DVA-based Adjustable Replacement Cache
*
* While much of the theory of operation used here is
* based on the self-tuning, low overhead replacement cache
* presented by Megiddo and Modha at FAST 2003, there are some
* significant differences:
*
* 1. The Megiddo and Modha model assumes any page is evictable.
* Pages in its cache cannot be "locked" into memory. This makes
* the eviction algorithm simple: evict the last page in the list.
* This also make the performance characteristics easy to reason
* about. Our cache is not so simple. At any given moment, some
* subset of the blocks in the cache are un-evictable because we
* have handed out a reference to them. Blocks are only evictable
* when there are no external references active. This makes
* eviction far more problematic: we choose to evict the evictable
* blocks that are the "lowest" in the list.
*
* There are times when it is not possible to evict the requested
* space. In these circumstances we are unable to adjust the cache
* size. To prevent the cache growing unbounded at these times we
* implement a "cache throttle" that slows the flow of new data
* into the cache until we can make space available.
*
* 2. The Megiddo and Modha model assumes a fixed cache size.
* Pages are evicted when the cache is full and there is a cache
* miss. Our model has a variable sized cache. It grows with
* high use, but also tries to react to memory pressure from the
* operating system: decreasing its size when system memory is
* tight.
*
* 3. The Megiddo and Modha model assumes a fixed page size. All
* elements of the cache are therefor exactly the same size. So
* when adjusting the cache size following a cache miss, its simply
* a matter of choosing a single page to evict. In our model, we
* have variable sized cache blocks (rangeing from 512 bytes to
* 128K bytes). We therefor choose a set of blocks to evict to make
* space for a cache miss that approximates as closely as possible
* the space used by the new block.
*
* See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
* by N. Megiddo & D. Modha, FAST 2003
*/
/*
* The locking model:
*
* A new reference to a cache buffer can be obtained in two
* ways: 1) via a hash table lookup using the DVA as a key,
* or 2) via one of the ARC lists. The arc_read() interface
* uses method 1, while the internal arc algorithms for
* adjusting the cache use method 2. We therefor provide two
* types of locks: 1) the hash table lock array, and 2) the
* arc list locks.
*
* Buffers do not have their own mutexs, rather they rely on the
* hash table mutexs for the bulk of their protection (i.e. most
* fields in the arc_buf_hdr_t are protected by these mutexs).
*
* buf_hash_find() returns the appropriate mutex (held) when it
* locates the requested buffer in the hash table. It returns
* NULL for the mutex if the buffer was not in the table.
*
* buf_hash_remove() expects the appropriate hash mutex to be
* already held before it is invoked.
*
* Each arc state also has a mutex which is used to protect the
* buffer list associated with the state. When attempting to
* obtain a hash table lock while holding an arc list lock you
* must use: mutex_tryenter() to avoid deadlock. Also note that
* the active state mutex must be held before the ghost state mutex.
*
* Arc buffers may have an associated eviction callback function.
* This function will be invoked prior to removing the buffer (e.g.
* in arc_do_user_evicts()). Note however that the data associated
* with the buffer may be evicted prior to the callback. The callback
* must be made with *no locks held* (to prevent deadlock). Additionally,
* the users of callbacks must ensure that their private data is
* protected from simultaneous callbacks from arc_buf_evict()
* and arc_do_user_evicts().
*
* Note that the majority of the performance stats are manipulated
* with atomic operations.
*
* The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
*
* - L2ARC buflist creation
* - L2ARC buflist eviction
* - L2ARC write completion, which walks L2ARC buflists
* - ARC header destruction, as it removes from L2ARC buflists
* - ARC header release, as it removes from L2ARC buflists
*/
#include <sys/zio_checksum.h>
#include <sys/zfs_context.h>
#include <sys/refcount.h>
#ifdef _KERNEL
#endif
static kmutex_t arc_reclaim_thr_lock;
static uint8_t arc_thread_exit;
extern int zfs_write_limit_shift;
extern uint64_t zfs_write_limit_max;
extern uint64_t zfs_write_limit_inflated;
#define ARC_REDUCE_DNLC_PERCENT 3
typedef enum arc_reclaim_strategy {
ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
ARC_RECLAIM_CONS /* Conservative reclaim strategy */
/* number of seconds before growing cache again */
static int arc_grow_retry = 60;
/*
* minimum lifespan of a prefetch block in clock ticks
* (initialized in arc_init())
*/
static int arc_min_prefetch_lifespan;
static int arc_dead;
/*
* The arc has filled available memory and has now warmed up.
*/
/*
* These tunables are for performance analysis.
*/
/*
* Note that buffers can be in one of 6 states:
* ARC_anon - anonymous (discussed below)
* ARC_mru - recently used, currently cached
* ARC_mru_ghost - recentely used, no longer in cache
* ARC_mfu - frequently used, currently cached
* ARC_mfu_ghost - frequently used, no longer in cache
* ARC_l2c_only - exists in L2ARC but not other states
* When there are no active references to the buffer, they are
* are linked onto a list in one of these arc states. These are
* the only buffers that can be evicted or deleted. Within each
* state there are multiple lists, one for meta-data and one for
* non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
* etc.) is tracked separately so that it can be managed more
* explicitly: favored over data, limited explicitly.
*
* Anonymous buffers are buffers that are not associated with
* a DVA. These are buffers that hold dirty block copies
* before they are written to stable storage. By definition,
* they are "ref'd" and are considered part of arc_mru
* that cannot be freed. Generally, they will aquire a DVA
* as they are written and migrate onto the arc_mru list.
*
* The ARC_l2c_only state is for buffers that are in the second
* level ARC but no longer in any of the ARC_m* lists. The second
* level ARC itself may also contain buffers that are in any of
* the ARC_m* states - meaning that a buffer can exist in two
* places. The reason for the ARC_l2c_only state is to keep the
* buffer header in the hash table, so that reads that hit the
* second level ARC benefit from these fast lookups.
*/
typedef struct arc_state {
} arc_state_t;
/* The 6 states: */
static arc_state_t ARC_anon;
static arc_state_t ARC_mru;
static arc_state_t ARC_mru_ghost;
static arc_state_t ARC_mfu;
static arc_state_t ARC_mfu_ghost;
static arc_state_t ARC_l2c_only;
typedef struct arc_stats {
} arc_stats_t;
static arc_stats_t arc_stats = {
{ "hits", KSTAT_DATA_UINT64 },
{ "misses", KSTAT_DATA_UINT64 },
{ "demand_data_hits", KSTAT_DATA_UINT64 },
{ "demand_data_misses", KSTAT_DATA_UINT64 },
{ "demand_metadata_hits", KSTAT_DATA_UINT64 },
{ "demand_metadata_misses", KSTAT_DATA_UINT64 },
{ "prefetch_data_hits", KSTAT_DATA_UINT64 },
{ "prefetch_data_misses", KSTAT_DATA_UINT64 },
{ "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
{ "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
{ "mru_hits", KSTAT_DATA_UINT64 },
{ "mru_ghost_hits", KSTAT_DATA_UINT64 },
{ "mfu_hits", KSTAT_DATA_UINT64 },
{ "mfu_ghost_hits", KSTAT_DATA_UINT64 },
{ "deleted", KSTAT_DATA_UINT64 },
{ "recycle_miss", KSTAT_DATA_UINT64 },
{ "mutex_miss", KSTAT_DATA_UINT64 },
{ "evict_skip", KSTAT_DATA_UINT64 },
{ "hash_elements", KSTAT_DATA_UINT64 },
{ "hash_elements_max", KSTAT_DATA_UINT64 },
{ "hash_collisions", KSTAT_DATA_UINT64 },
{ "hash_chains", KSTAT_DATA_UINT64 },
{ "hash_chain_max", KSTAT_DATA_UINT64 },
{ "p", KSTAT_DATA_UINT64 },
{ "c", KSTAT_DATA_UINT64 },
{ "c_min", KSTAT_DATA_UINT64 },
{ "c_max", KSTAT_DATA_UINT64 },
{ "size", KSTAT_DATA_UINT64 },
{ "hdr_size", KSTAT_DATA_UINT64 },
{ "l2_hits", KSTAT_DATA_UINT64 },
{ "l2_misses", KSTAT_DATA_UINT64 },
{ "l2_feeds", KSTAT_DATA_UINT64 },
{ "l2_rw_clash", KSTAT_DATA_UINT64 },
{ "l2_writes_sent", KSTAT_DATA_UINT64 },
{ "l2_writes_done", KSTAT_DATA_UINT64 },
{ "l2_writes_error", KSTAT_DATA_UINT64 },
{ "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
{ "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
{ "l2_evict_reading", KSTAT_DATA_UINT64 },
{ "l2_free_on_write", KSTAT_DATA_UINT64 },
{ "l2_abort_lowmem", KSTAT_DATA_UINT64 },
{ "l2_cksum_bad", KSTAT_DATA_UINT64 },
{ "l2_io_error", KSTAT_DATA_UINT64 },
{ "l2_size", KSTAT_DATA_UINT64 },
{ "l2_hdr_size", KSTAT_DATA_UINT64 },
{ "memory_throttle_count", KSTAT_DATA_UINT64 }
};
uint64_t m; \
continue; \
}
#define ARCSTAT_MAXSTAT(stat) \
/*
* two separate conditions, giving a total of four different subtypes for
* each of hits and misses (so eight statistics total).
*/
if (cond1) { \
if (cond2) { \
} else { \
} \
} else { \
if (cond2) { \
} else { \
} \
}
static arc_state_t *arc_anon;
static arc_state_t *arc_mru;
static arc_state_t *arc_mru_ghost;
static arc_state_t *arc_mfu;
static arc_state_t *arc_mfu_ghost;
static arc_state_t *arc_l2c_only;
/*
* There are several ARC variables that are critical to export as kstats --
* but we don't want to have to grovel around in the kstat whenever we wish to
* manipulate them. For these variables, we therefore define them to be in
* terms of the statistic variable. This assures that we are not introducing
* the possibility of inconsistency by having shadow copies of the variables,
* while still allowing the code to be readable.
*/
static int arc_no_grow; /* Don't try to grow cache size */
static uint64_t arc_tempreserve;
static uint64_t arc_meta_used;
static uint64_t arc_meta_limit;
static uint64_t arc_meta_max = 0;
typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
typedef struct arc_callback arc_callback_t;
struct arc_callback {
void *acb_private;
};
typedef struct arc_write_callback arc_write_callback_t;
struct arc_write_callback {
void *awcb_private;
};
struct arc_buf_hdr {
/* protected by hash lock */
/* immutable */
/* protected by arc state mutex */
/* updated atomically */
/* self protecting */
};
static arc_buf_t *arc_eviction_list;
static kmutex_t arc_eviction_mtx;
static arc_buf_hdr_t arc_eviction_hdr;
#define GHOST_STATE(state) \
(state) == arc_l2c_only)
/*
* Private ARC flags. These flags are private ARC only flags that will show up
* in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
* be passed in as arc_flags in things like arc_read. However, these flags
* should never be passed and should only be set by ARC code. When adding new
* public flags, make sure not to smash the private ones.
*/
/*
* Other sizes
*/
/*
* Hash table routines
*/
#define HT_LOCK_PAD 64
struct ht_lock {
#ifdef _KERNEL
#endif
};
#define BUF_LOCKS 256
typedef struct buf_hash_table {
static buf_hash_table_t buf_hash_table;
/*
* Level 2 ARC
*/
/*
* L2ARC Performance Tunables
*/
/*
* L2ARC Internals
*/
typedef struct l2arc_dev {
} l2arc_dev_t;
typedef struct l2arc_read_callback {
int l2rcb_flags; /* original flags */
typedef struct l2arc_write_callback {
struct l2arc_buf_hdr {
/* protected by arc_buf_hdr mutex */
};
typedef struct l2arc_data_free {
/* protected by l2arc_free_on_write_mtx */
void *l2df_data;
static kmutex_t l2arc_feed_thr_lock;
static kcondvar_t l2arc_feed_thr_cv;
static uint8_t l2arc_thread_exit;
static void l2arc_hdr_stat_add(void);
static void l2arc_hdr_stat_remove(void);
static uint64_t
{
int i;
for (i = 0; i < sizeof (dva_t); i++)
return (crc);
}
static arc_buf_hdr_t *
{
return (buf);
}
}
return (NULL);
}
/*
* Insert an entry into the hash table. If there is already an element
* equal to elem in the hash table, then the already existing element
* will be returned and the new element will not be inserted.
* Otherwise returns NULL.
*/
static arc_buf_hdr_t *
{
uint32_t i;
return (fbuf);
}
/* collect some hash table performance data */
if (i > 0) {
if (i == 1)
}
return (NULL);
}
static void
{
}
/* collect some hash table performance data */
}
/*
* Global data structures and functions for the buf kmem cache.
*/
static kmem_cache_t *hdr_cache;
static kmem_cache_t *buf_cache;
static void
buf_fini(void)
{
int i;
for (i = 0; i < BUF_LOCKS; i++)
}
/*
* Constructor callback - called when the cache is empty
* and a new buf is requested.
*/
/* ARGSUSED */
static int
{
return (0);
}
/*
* Destructor callback - called when a cached buf is
* no longer required.
*/
/* ARGSUSED */
static void
{
}
/*
* Reclaim callback -- invoked when memory is low.
*/
/* ARGSUSED */
static void
{
dprintf("hdr_recl called\n");
/*
* umem calls the reclaim func when we destroy the buf cache,
* which is after we do arc_fini().
*/
if (!arc_dead)
}
static void
buf_init(void)
{
int i, j;
/*
* The hash table is big enough to fill all of physical memory
* with an average 64K block size. The table will take up
*/
hsize <<= 1;
hsize >>= 1;
goto retry;
}
for (i = 0; i < 256; i++)
for (i = 0; i < BUF_LOCKS; i++) {
}
}
static void
{
if (!(zfs_flags & ZFS_DEBUG_MODIFY))
return;
return;
}
panic("buffer modified while frozen!");
}
static int
{
int equal;
return (equal);
}
static void
{
return;
return;
}
}
void
{
if (zfs_flags & ZFS_DEBUG_MODIFY) {
panic("modifying non-anon buffer!");
panic("modifying buffer while i/o in progress!");
}
}
}
void
{
if (!(zfs_flags & ZFS_DEBUG_MODIFY))
return;
}
static void
{
}
/* remove the prefetch flag is we get a reference */
}
}
static int
{
int cnt;
}
return (cnt);
}
/*
* Move the supplied buffer to the indicated state. The mutex
* for the buffer must be held by the caller.
*/
static void
{
/*
* If this buffer is evictable, transfer it from the
* old state list to the new state list.
*/
if (refcnt == 0) {
if (use_mutex)
/*
* If prefetching out of the ghost cache,
* we will have a non-null datacnt.
*/
/* ghost elements have a ghost size */
}
if (use_mutex)
}
if (use_mutex)
/* ghost elements have a ghost size */
if (GHOST_STATE(new_state)) {
}
if (use_mutex)
}
}
}
/* adjust state sizes */
if (to_delta)
if (from_delta) {
}
/* adjust l2arc hdr stats */
if (new_state == arc_l2c_only)
else if (old_state == arc_l2c_only)
}
void
{
}
void
{
if (arc_meta_max < arc_meta_used)
}
void *
{
return (zio_data_buf_alloc(size));
}
void
{
}
{
hdr->b_arc_access = 0;
return (buf);
}
static arc_buf_t *
{
return (buf);
}
void
{
/*
* Check to see if this buffer is currently being evicted via
* arc_do_user_evicts().
*/
return;
}
/*
* This buffer is evicted.
*/
return;
}
}
/*
* Free the arc data buffer. If it is an l2arc write in progress,
* the buffer is placed on l2arc_free_on_write to be freed later.
*/
static void
{
if (HDR_L2_WRITING(hdr)) {
} else {
}
}
static void
{
/* free up data associated with the buf */
if (!recycle) {
if (type == ARC_BUFC_METADATA) {
} else {
}
}
}
}
/* only remove the buf if requested */
if (!all)
return;
/* remove the buf from the hdr list */
continue;
/* clean up the buf */
}
static void
{
if (!MUTEX_HELD(&l2arc_buflist_mtx)) {
/*
* To prevent arc_free() and l2arc_evict() from
* attempting to free the same buffer at the same time,
* a FREE_IN_PROGRESS flag is given to arc_free() to
* give it priority. l2arc_evict() can't destroy this
* header while we are waiting on l2arc_buflist_mtx.
*/
} else {
}
}
}
} else {
}
}
}
}
void
{
if (hashed) {
else
} else if (HDR_IO_IN_PROGRESS(hdr)) {
int destroy_hdr;
/*
* We are in the middle of an async write. Don't destroy
* this buffer unless the write completes before we finish
* decrementing the reference count.
*/
if (destroy_hdr)
} else {
} else {
}
}
}
int
{
return (no_callback);
}
if (no_callback)
} else if (no_callback) {
}
return (no_callback);
}
int
{
}
/*
* Evict buffers from list until we've removed the specified number of
* bytes. Move the removed buffers to the appropriate evict state.
* If the recycle flag is set, then attempt to "recycle" a buffer:
* - look for a buffer to evict that is `bytes' long.
* - return the data block from this buffer rather than freeing it.
* This flag is used by callers that are trying to make space for a
* new buffer in a full arc cache.
*
* This function makes a "best effort". It skips over any buffers
* it can't get a hash_lock on, and so may not catch all candidates.
* It may also return without evicting as much space as requested.
*/
static void *
{
/* prefetch buffers have a minimum lifespan */
if (HDR_IO_IN_PROGRESS(ab) ||
skipped++;
continue;
}
/* "lookahead" for better eviction candidate */
continue;
!HDR_L2_WRITING(ab)) {
}
}
} else {
}
}
if (!have_lock)
break;
} else {
missed += 1;
}
}
if (bytes_evicted < bytes)
dprintf("only evicted %lld bytes from %x",
if (skipped)
if (missed)
/*
* We have just evicted some date into the ghost state, make
* sure we also adjust the ghost state size if necessary.
*/
if (arc_no_grow &&
}
}
return (stolen);
}
/*
* Remove buffers from list until we've removed the specified number of
* bytes. Destroy the buffers that are removed.
*/
static void
{
uint64_t bytes_deleted = 0;
uint64_t bufs_skipped = 0;
top:
continue;
if (mutex_tryenter(hash_lock)) {
/*
* This buffer is cached on the 2nd Level ARC;
* don't destroy the header.
*/
} else {
}
break;
} else {
if (bytes < 0) {
goto top;
}
bufs_skipped += 1;
}
}
goto top;
}
if (bufs_skipped) {
}
if (bytes_deleted < bytes)
dprintf("only deleted %lld bytes from %p",
}
static void
arc_adjust(void)
{
}
}
if (mru_over > 0) {
if (arc_mru_ghost->arcs_size > 0) {
}
}
}
if (arc_over > 0 &&
arc_over);
}
}
}
}
static void
arc_do_user_evicts(void)
{
while (arc_eviction_list != NULL) {
}
}
/*
* Flush all *evictable* data from the cache for the given spa.
* NOTE: this will not touch "active" (i.e. referenced) data.
*/
void
{
if (spa)
break;
}
if (spa)
break;
}
if (spa)
break;
}
if (spa)
break;
}
}
void
arc_shrink(void)
{
#ifdef _KERNEL
#else
#endif
else
}
arc_adjust();
}
static int
arc_reclaim_needed(void)
{
#ifdef _KERNEL
if (needfree)
return (1);
/*
* take 'desfree' extra pages, so we reclaim sooner, rather than later
*/
/*
* check that we're out of range of the pageout scanner. It starts to
* schedule paging if freemem is less than lotsfree and needfree.
* lotsfree is the high-water mark for pageout, and needfree is the
* number of needed free pages. We add extra pages here to make sure
* the scanner doesn't start up while we're freeing memory.
*/
return (1);
/*
* check to make sure that swapfs has enough space so that anon
* reservations can still succeed. anon_resvmem() checks that the
* availrmem is greater than swapfs_minfree, and the number of reserved
* swap pages. We also add a bit of extra here just to prevent
* circumstances from getting really dire.
*/
return (1);
#if defined(__i386)
/*
* If we're on an i386 platform, it's possible that we'll exhaust the
* kernel heap space before we ever run out of available physical
* memory. Most checks of the size of the heap_area compare against
* tune.t_minarmem, which is the minimum available real memory that we
* can have in the system. However, this is generally fixed at 25 pages
* which is so low that it's useless. In this comparison, we seek to
* calculate the total heap-size, and reclaim if more than 3/4ths of the
* heap is allocated. (Or, in the calculation, if less than 1/4th is
* free)
*/
return (1);
#endif
#else
if (spa_get_random(100) == 0)
return (1);
#endif
return (0);
}
static void
{
size_t i;
extern kmem_cache_t *zio_buf_cache[];
extern kmem_cache_t *zio_data_buf_cache[];
#ifdef _KERNEL
if (arc_meta_used >= arc_meta_limit) {
/*
* We are exceeding our meta-data cache limit.
* Purge some DNLC entries to release holds on meta-data.
*/
}
#if defined(__i386)
/*
* Reclaim unused memory from all kmem caches.
*/
kmem_reap();
#endif
#endif
/*
* An aggressive reclamation will shrink the cache size as well as
* reap free buffers from the arc kmem caches.
*/
if (strat == ARC_RECLAIM_AGGR)
arc_shrink();
for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
if (zio_buf_cache[i] != prev_cache) {
prev_cache = zio_buf_cache[i];
}
if (zio_data_buf_cache[i] != prev_data_cache) {
}
}
}
static void
arc_reclaim_thread(void)
{
while (arc_thread_exit == 0) {
if (arc_reclaim_needed()) {
if (arc_no_grow) {
if (last_reclaim == ARC_RECLAIM_CONS) {
} else {
}
} else {
arc_no_grow = TRUE;
}
/* reset the growth delay for every reclaim */
arc_no_grow = FALSE;
}
arc_adjust();
if (arc_eviction_list != NULL)
/* block until needed, or one second, whichever is shorter */
(void) cv_timedwait(&arc_reclaim_thr_cv,
}
arc_thread_exit = 0;
thread_exit();
}
/*
* Adapt arc info given the number of bytes we are trying to add and
* the state that we are comming from. This function is only called
* when we are adding new content to the cache.
*/
static void
{
int mult;
if (state == arc_l2c_only)
return;
/*
* Adapt the target size of the MRU list:
* - if we just hit in the MRU ghost list, then increase
* the target size of the MRU list.
* - if we just hit in the MFU ghost list, then increase
* the target size of the MFU list by decreasing the
* target size of the MRU list.
*/
if (state == arc_mru_ghost) {
} else if (state == arc_mfu_ghost) {
}
if (arc_reclaim_needed()) {
return;
}
if (arc_no_grow)
return;
return;
/*
* If we're within (2 * maxblocksize) bytes of the target
* cache size, increment the target cache size
*/
}
}
/*
* Check if the cache has reached its limits and eviction is required
* prior to insert.
*/
static int
{
return (1);
#ifdef _KERNEL
/*
* If zio data pages are being allocated out of a separate heap segment,
* then enforce that the size of available vmem for this area remains
* above about 1/32nd free.
*/
return (1);
#endif
if (arc_reclaim_needed())
return (1);
}
/*
* The buffer, supplied as the first argument, needs a data block.
* So, if we are at cache max, determine which cache should be victimized.
* We have the following cases:
*
* 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
* In this situation if we're out of space, but the resident size of the MFU is
* under the limit, victimize the MFU cache to satisfy this insertion request.
*
* 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
* Here, we've used up all of the available space for the MRU, so we need to
* evict from our own cache instead. Evict from the set of resident MRU
* entries.
*
* 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
* c minus p represents the MFU space in the cache, since p is the size of the
* cache that is dedicated to the MRU. In this situation there's still space on
* the MFU side, so the MRU side needs to be victimized.
*
* 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
* MFU's resident set is consuming more space than it has been allotted. In
* this situation, we must victimize our own cache, the MFU, for this insertion.
*/
static void
{
/*
* We have not yet reached cache maximum size,
* just allocate a new buffer.
*/
if (!arc_evict_needed(type)) {
if (type == ARC_BUFC_METADATA) {
} else {
}
goto out;
}
/*
* If we are prefetching from the mfu ghost list, this buffer
* will end up on the mru list; so steal space from there.
*/
if (state == arc_mfu_ghost)
else if (state == arc_mru_ghost)
} else {
/* MFU cases */
}
if (type == ARC_BUFC_METADATA) {
} else {
}
}
out:
/*
* Update the state size. Note that ghost states have a
* "ghost size" and so don't need to be updated.
*/
}
/*
* If we are growing the cache, and we are adding anonymous
* data, and we have outgrown arc_p, update arc_p
*/
}
}
/*
* This routine is called whenever a buffer is accessed.
* NOTE: the hash lock is dropped in this function.
*/
static void
{
/*
* This buffer is not in the cache, and does not
* appear in our "ghost" list. Add the new buffer
* to the MRU state.
*/
/*
* If this buffer is here because of a prefetch, then either:
* - clear the flag if this is a "referencing" read
* (any subsequent access will bump this into the MFU state).
* or
* - move the buffer to the head of the list if this is
* another prefetch (to make it less likely to be evicted).
*/
} else {
}
return;
}
/*
* This buffer has been "accessed" only once so far,
* but it is still in the cache. Move it to the MFU
* state.
*/
/*
* More than 125ms have passed since we
* instantiated this buffer. Move it to the
* most frequently used state.
*/
}
/*
* This buffer has been "accessed" recently, but
* was evicted from the cache. Move it to the
* MFU state.
*/
} else {
}
/*
* This buffer has been accessed more than once and is
* still in the cache. Keep it in the MFU state.
*
* NOTE: an add_reference() that occurred when we did
* the arc_read() will have kicked this off the list.
* If it was a prefetch, we will explicitly move it to
* the head of the list now.
*/
}
/*
* This buffer has been accessed more than once but has
* been evicted from the cache. Move it back to the
* MFU state.
*/
/*
* This is a prefetch access...
* move this block back to the MRU state.
*/
}
/*
* This buffer is on the 2nd Level ARC.
*/
} else {
ASSERT(!"invalid arc state");
}
}
/* a generic arc_done_func_t which you can use */
/* ARGSUSED */
void
{
}
/* a generic arc_done_func_t */
void
{
} else {
}
}
static void
{
/*
* The hdr was inserted into hash-table and removed from lists
* prior to starting I/O. We should find this header, since
* it's in the hash table, and it should be legit since it's
* not possible to evict it during the I/O. The only possible
* reason for it not to be found is if we were freed during the
* read.
*/
&hash_lock);
/* byteswap if necessary */
/* create copies of the data buffer for the callers */
}
}
if (HDR_IN_HASH_TABLE(hdr))
/* convert checksum errors into IO errors */
}
/*
* Broadcast before we drop the hash_lock to avoid the possibility
* that the hdr (and hence the cv) might be freed before we get to
* the cv_broadcast().
*/
if (hash_lock) {
/*
* Only call arc_access on anonymous buffers. This is because
* if we've issued an I/O for an evicted buffer, we've already
* called arc_access (to prevent any simultaneous readers from
* getting confused).
*/
} else {
/*
* This block was freed while we waited for the read to
* complete. It has been removed from the hash table and
* moved to the anonymous state (so that it won't show up
* in the cache).
*/
}
/* execute each callback and free its structure */
}
}
if (freeable)
}
/*
* "Read" the block block at the specified DVA (in bp) via the
* cache. If the block is found in the cache, invoke the provided
* callback immediately and return. Note that the `zio' parameter
* in the callback will be NULL in this case, since no IO was
* required. If the block is not in the cache pass the read request
* on to the spa with a substitute callback function, so that the
* requested block will be added to the cache.
*
* If a read request arrives for a block that has a read in-progress,
* either wait for the in-progress read to complete (and return the
* results); or, if this is a read with a "done" func, add a record
* to the read to invoke the "done" func when the read completes,
* and return; or just return.
*
* arc_read_done() will invoke all the requested "done" functions
* for readers of this block.
*/
int
{
top:
*arc_flags |= ARC_CACHED;
if (HDR_IO_IN_PROGRESS(hdr)) {
goto top;
}
if (done) {
KM_SLEEP);
return (0);
}
return (0);
}
if (done) {
/*
* If this block is already in use, create a new
* copy of the data so that we will be guaranteed
* that arc_release() will always succeed.
*/
if (HDR_BUF_AVAILABLE(hdr)) {
} else {
}
} else if (*arc_flags & ARC_PREFETCH &&
}
if (done)
} else {
/* this block is not in the cache */
if (exists) {
/* somebody beat us to the hash insert */
goto top; /* restart the IO request */
}
/* if this is a prefetch, we don't have a reference */
if (*arc_flags & ARC_PREFETCH) {
private);
}
if (BP_GET_LEVEL(bp) > 0)
} else {
/* this block is in the ghost cache */
/* if this is a prefetch, we don't have a reference */
if (*arc_flags & ARC_PREFETCH)
else
}
/*
* If the buffer has been evicted, migrate it to a present state
* before issuing the I/O. Once we drop the hash-table lock,
* the header will be marked as I/O in progress and have an
* attached buffer. At this point, anybody who finds this
* buffer ought to notice that it's legit but has a pending I/O.
*/
}
zbookmark_t *, zb);
if (l2arc_ndev != 0) {
/*
* Lock out device removal.
*/
/*
* Read from the L2ARC if the following are true:
* 1. The L2ARC vdev was previously cached.
* 2. This buffer still has L2ARC metadata.
* 3. This buffer isn't currently writing to the L2ARC.
* 4. The L2ARC entry wasn't evicted, which may
* also have invalidated the vdev.
*/
if (vdev_is_dead(vd))
goto l2skip;
KM_SLEEP);
/*
* l2arc read.
*/
B_FALSE);
if (*arc_flags & ARC_NOWAIT) {
return (0);
}
return (0);
/* l2arc read error; goto zio_read() */
} else {
arc_buf_hdr_t *, hdr);
if (HDR_L2_WRITING(hdr))
}
}
}
return (0);
}
/*
* arc_read() variant to support pool traversal. If the block is already
* in the ARC, make a copy of it; otherwise, the caller will do the I/O.
* The idea is that we don't want pool traversal filling up memory, but
* if the ARC already has the data anyway, we shouldn't pay for the I/O.
*/
int
{
int rc = 0;
}
} else {
}
if (hash_mtx)
return (rc);
}
void
{
}
/*
* This is used by the DMU to let the ARC know that a buffer is
* being evicted, so the ARC should clean up. If this arc buf
* is not yet in the evicted state, it will be put there.
*/
int
{
/*
* We are in arc_do_user_evicts().
*/
return (0);
}
/*
* We are on the eviction list.
*/
/*
* We are already in arc_do_user_evicts().
*/
return (0);
} else {
/*
* Process this buffer now
* but let arc_do_user_evicts() do the reaping.
*/
return (1);
}
}
/*
* Pull this buffer off of the hdr
*/
}
return (1);
}
/*
* Release this buffer from the cache. This must be done
* after a read and prior to modifying the buffer contents.
* If the buffer has more than one reference, we must make
* make a new hdr for the buffer.
*/
void
{
/* this buffer is not on any list */
/* this buffer is already released */
return;
}
/*
* Do we have more than one buf?
*/
/*
* Pull the data off of this buf and attach it to
* a new anonymous buf.
*/
}
}
nhdr->b_arc_access = 0;
} else {
hdr->b_arc_access = 0;
}
}
if (l2hdr) {
}
if (MUTEX_HELD(&l2arc_buflist_mtx))
}
int
{
}
int
{
}
#ifdef ZFS_DEBUG
int
{
}
#endif
static void
{
}
/*
* If the IO is already in progress, then this is a re-write
* attempt, so we need to thaw and re-compute the cksum. It is
* the responsibility of the callback to handle the freeing
* and accounting for any re-write attempt. If we don't have a
* callback registered then simply free the block here.
*/
if (HDR_IO_IN_PROGRESS(hdr)) {
}
}
}
}
static void
{
/* this buffer is on no lists and is not in the hash table */
/*
* If the block to be written was all-zero, we may have
* compressed it away. In this case no write was performed
* so there will be no dva/birth-date/checksum. The buffer
* must therefor remain anonymous (and uncached).
*/
if (exists) {
/*
* This can only happen if we overwrite for
* sync-to-convergence, because we remove
* buffers from the hash table when we arc_free().
*/
}
int destroy_hdr;
/*
* This is an anonymous buffer with no user callback,
* destroy it if there are no active references.
*/
if (destroy_hdr)
} else {
}
}
}
zio_t *
{
/* this is a private buffer - no locking required */
return (zio);
}
int
{
/*
* If this buffer is in the cache, release it, so it
* can be re-used.
*/
/*
* The checksum of blocks to free is not always
* preserved (eg. on the deadlist). However, if it is
* nonzero, it should match what we have in the cache.
*/
if (HDR_IO_IN_PROGRESS(ab)) {
/*
* This should only happen when we prefetch.
*/
if (HDR_IN_HASH_TABLE(ab))
ab->b_arc_access = 0;
} else {
/*
* We still have an active reference on this
* buffer. This can happen, e.g., from
* dbuf_unoverride().
*/
ab->b_arc_access = 0;
}
}
return (0);
}
static int
{
#ifdef _KERNEL
#if defined(__i386)
#endif
if (available_memory >= zfs_write_limit_max)
return (0);
page_load = 0;
}
/*
* If we are in pageout, we know that memory is already tight,
* the arc is already going to be evicting, so we just want to
* continue to let page writes occur as quickly as possible.
*/
if (curproc == proc_pageout) {
return (ERESTART);
/* Note: reserve is inflated, so we deflate */
return (0);
} else if (page_load > 0 && arc_reclaim_needed()) {
/* memory is low, delay before restarting */
return (EAGAIN);
}
page_load = 0;
}
return (ERESTART);
}
#endif
return (0);
}
void
{
}
int
{
int error;
#ifdef ZFS_DEBUG
/*
* Once in a while, fail for no reason. Everything should cope.
*/
if (spa_get_random(10000) == 0) {
dprintf("forcing random failure\n");
return (ERESTART);
}
#endif
return (ENOMEM);
/*
* Writes will, almost always, require additional memory allocations
* make sure that there is sufficient available memory for this.
*/
return (error);
/*
* Throttle writes when the amount of dirty data in the cache
* gets too large. We try to keep the cache less than half full
* of dirty blocks so that our sync times don't grow too large.
* Note: if two requests come in concurrently, we might let them
* both succeed, when one of them should fail. Not a huge deal.
*/
dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
"anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
arc_tempreserve>>10,
return (ERESTART);
}
return (0);
}
void
arc_init(void)
{
/* Convert seconds to clock ticks */
/* Start out with 1/8 of all memory */
#ifdef _KERNEL
/*
* On architectures where the physical memory can be larger
* than the addressable space (intel in 32-bit mode), we may
* need to limit the cache to 1/8 of VM size.
*/
#endif
/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
else
/*
* Allow the tunables to override our calculations if they are
* reasonable (ie. over 64MB)
*/
/* limit meta-data to 1/4 of the arc capacity */
/* Allow the tunable to override if it is reasonable */
/* if kmem_flags are set, lets try to use less memory */
if (kmem_debugging())
arc_size = 0;
buf_init();
arc_thread_exit = 0;
}
if (zfs_write_limit_max == 0)
else
}
void
arc_fini(void)
{
arc_thread_exit = 1;
while (arc_thread_exit != 0)
}
buf_fini();
}
/*
* Level 2 ARC
*
* The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
* It uses dedicated storage devices to hold cached data, which are populated
* using large infrequent writes. The main role of this cache is to boost
* the performance of random read workloads. The intended L2ARC devices
* include short-stroked disks, solid state disks, and other media with
* substantially faster read latency than disk.
*
* +-----------------------+
* | ARC |
* +-----------------------+
* | ^ ^
* | | |
* l2arc_feed_thread() arc_read()
* | | |
* | l2arc read |
* V | |
* +---------------+ |
* | L2ARC | |
* +---------------+ |
* | ^ |
* l2arc_write() | |
* | | |
* V | |
* +-------+ +-------+
* | vdev | | vdev |
* | cache | | cache |
* +-------+ +-------+
* +=========+ .-----.
* : L2ARC : |-_____-|
* : devices : | Disks |
* +=========+ `-_____-'
*
* Read requests are satisfied from the following sources, in order:
*
* 1) ARC
* 2) vdev cache of L2ARC devices
* 3) L2ARC devices
* 4) vdev cache of disks
* 5) disks
*
* Some L2ARC device types exhibit extremely slow write performance.
* To accommodate for this there are some significant differences between
* the L2ARC and traditional cache design:
*
* 1. There is no eviction path from the ARC to the L2ARC. Evictions from
* the ARC behave as usual, freeing buffers and placing headers on ghost
* lists. The ARC does not send buffers to the L2ARC during eviction as
* this would add inflated write latencies for all ARC memory pressure.
*
* 2. The L2ARC attempts to cache data from the ARC before it is evicted.
* It does this by periodically scanning buffers from the eviction-end of
* the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
* not already there. It scans until a headroom of buffers is satisfied,
* which itself is a buffer for ARC eviction. The thread that does this is
* l2arc_feed_thread(), illustrated below; example sizes are included to
* provide a better sense of ratio than this diagram:
*
* head --> tail
* +---------------------+----------+
* ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
* +---------------------+----------+ | o L2ARC eligible
* ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
* +---------------------+----------+ |
* 15.9 Gbytes ^ 32 Mbytes |
* headroom |
* l2arc_feed_thread()
* |
* l2arc write hand <--[oooo]--'
* | 8 Mbyte
* | write max
* V
* +==============================+
* L2ARC dev |####|#|###|###| |####| ... |
* +==============================+
* 32 Gbytes
*
* 3. If an ARC buffer is copied to the L2ARC but then hit instead of
* evicted, then the L2ARC has cached a buffer much sooner than it probably
* needed to, potentially wasting L2ARC device bandwidth and storage. It is
* safe to say that this is an uncommon case, since buffers at the end of
* the ARC lists have moved there due to inactivity.
*
* 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
* then the L2ARC simply misses copying some buffers. This serves as a
* pressure valve to prevent heavy read workloads from both stalling the ARC
* with waits and clogging the L2ARC with writes. This also helps prevent
* the potential for the L2ARC to churn if it attempts to cache content too
* quickly, such as during backups of the entire pool.
*
* 5. After system boot and before the ARC has filled main memory, there are
* no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
* lists can remain mostly static. Instead of searching from tail of these
* lists as pictured, the l2arc_feed_thread() will search from the list heads
* for eligible buffers, greatly increasing its chance of finding them.
*
* The L2ARC device write speed is also boosted during this time so that
* the L2ARC warms up faster. Since there have been no ARC evictions yet,
* there are no L2ARC reads, and no fear of degrading read performance
* through increased writes.
*
* 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
* the vdev queue can aggregate them into larger and fewer writes. Each
* device is written to in a rotor fashion, sweeping writes through
* available space then repeating.
*
* 7. The L2ARC does not store dirty content. It never needs to flush
* write buffers back to disk based storage.
*
* 8. If an ARC buffer is written (and dirtied) which also exists in the
* L2ARC, the now stale L2ARC buffer is immediately dropped.
*
* The performance of the L2ARC can be tweaked by a number of tunables, which
* may be necessary for different workloads:
*
* l2arc_write_max max write bytes per interval
* l2arc_write_boost extra write bytes during device warmup
* l2arc_noprefetch skip caching prefetched buffers
* l2arc_headroom number of max device writes to precache
* l2arc_feed_secs seconds between L2ARC writing
*
* Tunables may be removed or added as future performance improvements are
* integrated, and also may become zpool properties.
*/
static void
l2arc_hdr_stat_add(void)
{
}
static void
l2arc_hdr_stat_remove(void)
{
}
/*
* Cycle through L2ARC devices. This is how L2ARC load balances.
* If a device is returned, this also returns holding the spa config lock.
*/
static l2arc_dev_t *
l2arc_dev_get_next(void)
{
/*
* Lock out the removal of spas (spa_namespace_lock), then removal
* of cache devices (l2arc_dev_mtx). Once a device has been selected,
* both locks will be dropped and a spa config lock held instead.
*/
/* if there are no vdevs, there is nothing to do */
if (l2arc_ndev == 0)
goto out;
do {
/* loop around the list looking for a non-faulted vdev */
} else {
}
/* if we have come back to the start, bail out */
break;
/* if we were unable to find any usable vdevs, return NULL */
out:
/*
* Grab the config lock to prevent the 'next' device from being
* removed while we are writing to it.
*/
return (next);
}
/*
* Free buffers that were tagged for destruction.
*/
static void
{
}
}
/*
* A write to a cache device has completed. Update all headers to allow
* reads from these buffers to begin.
*/
static void
{
l2arc_write_callback_t *, cb);
/*
* All writes completed, or an error was hit.
*/
if (!mutex_tryenter(hash_lock)) {
/*
* This buffer misses out. It may be in a stage
* of eviction. Its ARC_L2_WRITING flag will be
* left set, denying reads to this buffer.
*/
continue;
}
/*
* Error - drop L2ARC entry.
*/
}
/*
* Allow ARC to begin reads to this L2ARC entry.
*/
}
}
/*
* A read to a cache device completed. Validate buffer contents before
* handing over to the regular ARC routines.
*/
static void
{
int equal;
/*
* Check this survived the L2ARC journey.
*/
} else {
/*
* Buffer didn't survive caching. Increment stats and
* reissue to the original storage device.
*/
} else {
}
if (!equal)
/*
* Let the resent I/O call arc_read_done() instead.
*/
(void) zio_nowait(rzio);
}
}
}
/*
* This is the list priority from which the L2ARC will search for pages to
* cache. This is used within loops (0..3) to cycle through lists in the
* desired order. This order can have a significant effect on cache
* performance.
*
* Currently the metadata lists are hit first, MFU then MRU, followed by
* the data lists. This function returns a locked list, and also returns
* the lock pointer.
*/
static list_t *
{
switch (list_num) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
mutex_enter(*lock);
return (list);
}
/*
* Evict buffers from the device write hand to the distance specified in
* bytes. This distance may span populated buffers, it may span nothing.
* This is clearing a region on the L2ARC device ready for writing.
* If the 'all' boolean is set, every buffer is evicted.
*/
static void
{
return;
/*
* This is the first sweep through the device. There is
* nothing to evict.
*/
return;
}
/*
* When nearing the end of the device, evict to the end
* before the device write hand jumps to the start.
*/
} else {
}
top:
if (!mutex_tryenter(hash_lock)) {
/*
* Missed the hash lock. Retry.
*/
goto top;
}
if (HDR_L2_WRITE_HEAD(ab)) {
/*
* We hit a write head node. Leave it for
* l2arc_write_done().
*/
continue;
}
/*
* We've evicted to the target address,
* or the end of the device.
*/
break;
}
if (HDR_FREE_IN_PROGRESS(ab)) {
/*
* Already on the path to destruction.
*/
continue;
}
/*
* This doesn't exist in the ARC. Destroy.
* arc_hdr_destroy() will call list_remove()
* and decrement arcstat_l2_size.
*/
} else {
/*
* Invalidate issued or about to be issued
* reads, since we may be about to write
* over this location.
*/
if (HDR_L2_READING(ab)) {
}
/*
* Tell ARC this no longer exists in L2ARC.
*/
}
/*
* This may have been leftover after a
* failed write.
*/
}
}
}
/*
* Find and write ARC buffers to the L2ARC device.
*
* An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
* for reading until they have completed writing.
*/
static void
{
void *buf_data;
write_sz = 0;
/*
* Copy buffers for L2ARC writing.
*/
passed_sz = 0;
/*
* L2ARC fast warmup.
*
* Until the ARC is warm and starts to evict, read from the
* head of the ARC lists rather than the tail.
*/
else
else
/*
* Skip this buffer rather than waiting.
*/
continue;
}
/*
* Searched too far.
*/
break;
}
continue;
}
/*
* Already in L2ARC.
*/
continue;
}
continue;
}
break;
}
continue;
}
/*
* Insert a dummy header on the buflist so
* l2arc_write_done() can find where the
* write buffers begin without searching.
*/
cb = kmem_alloc(
sizeof (l2arc_write_callback_t), KM_SLEEP);
}
/*
* Create and add a new L2ARC header.
*/
/*
* Compute and store the buffer cksum before
* writing. On debug the cksum is verified first.
*/
(void) zio_nowait(wzio);
}
break;
}
return;
}
/*
* Bump device hand to the device start if it is approaching the end.
* l2arc_evict() will already have evicted ahead for this case.
*/
}
}
/*
* This thread feeds the L2ARC at regular intervals. This is the beating
* heart of the L2ARC.
*/
static void
l2arc_feed_thread(void)
{
while (l2arc_thread_exit == 0) {
/*
* Pause for l2arc_feed_secs seconds between writes.
*/
/*
* Quick check for L2ARC devices.
*/
if (l2arc_ndev == 0) {
continue;
}
/*
* This selects the next l2arc device to write to, and in
* doing so the next spa to feed from: dev->l2ad_spa. This
* will return NULL if there are now no l2arc devices or if
* they are all faulted.
*
* If a device is returned, its spa's config lock is also
* held to prevent device removal. l2arc_dev_get_next()
* will grab and release l2arc_dev_mtx.
*/
continue;
/*
* Avoid contributing to memory pressure.
*/
if (arc_reclaim_needed()) {
continue;
}
/*
* Evict L2ARC buffers that will be overwritten.
*/
/*
* Write ARC buffers.
*/
}
l2arc_thread_exit = 0;
thread_exit();
}
{
break;
}
}
/*
* Add a vdev for use by the L2ARC. By this point the spa has already
* validated the vdev and opened it.
*/
void
{
/*
* Create a new l2arc device entry.
*/
/*
* This is a list of all ARC buffers that are still valid on the
* device.
*/
/*
* Add device to global list
*/
}
/*
* Remove a vdev from the L2ARC.
*/
void
{
/*
* Find the device by vdev
*/
break;
}
}
/*
* Remove device from global list
*/
/*
* Clear all buflists and ARC references. L2ARC device flush.
*/
}
void
{
l2arc_thread_exit = 0;
l2arc_ndev = 0;
l2arc_writes_sent = 0;
l2arc_writes_done = 0;
}
void
{
/*
* This is called from dmu_fini(), which is called from spa_fini();
* Because of this, we can assume that all l2arc devices have
* already been removed when the pools themselves were removed.
*/
l2arc_thread_exit = 1;
while (l2arc_thread_exit != 0)
}