1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * DVA-based Adjustable Relpacement Cache 30 * 31 * While much of the theory of operation used here is 32 * based on the self-tuning, low overhead replacement cache 33 * presented by Megiddo and Modha at FAST 2003, there are some 34 * significant differences: 35 * 36 * 1. The Megiddo and Modha model assumes any page is evictable. 37 * Pages in its cache cannot be "locked" into memory. This makes 38 * the eviction algorithm simple: evict the last page in the list. 39 * This also make the performance characteristics easy to reason 40 * about. Our cache is not so simple. At any given moment, some 41 * subset of the blocks in the cache are un-evictable because we 42 * have handed out a reference to them. Blocks are only evictable 43 * when there are no external references active. This makes 44 * eviction far more problematic: we choose to evict the evictable 45 * blocks that are the "lowest" in the list. 46 * 47 * There are times when it is not possible to evict the requested 48 * space. In these circumstances we are unable to adjust the cache 49 * size. To prevent the cache growing unbounded at these times we 50 * implement a "cache throttle" that slowes the flow of new data 51 * into the cache until we can make space avaiable. 52 * 53 * 2. The Megiddo and Modha model assumes a fixed cache size. 54 * Pages are evicted when the cache is full and there is a cache 55 * miss. Our model has a variable sized cache. It grows with 56 * high use, but also tries to react to memory preasure from the 57 * operating system: decreasing its size when system memory is 58 * tight. 59 * 60 * 3. The Megiddo and Modha model assumes a fixed page size. All 61 * elements of the cache are therefor exactly the same size. So 62 * when adjusting the cache size following a cache miss, its simply 63 * a matter of choosing a single page to evict. In our model, we 64 * have variable sized cache blocks (rangeing from 512 bytes to 65 * 128K bytes). We therefor choose a set of blocks to evict to make 66 * space for a cache miss that approximates as closely as possible 67 * the space used by the new block. 68 * 69 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 70 * by N. Megiddo & D. Modha, FAST 2003 71 */ 72 73 /* 74 * The locking model: 75 * 76 * A new reference to a cache buffer can be obtained in two 77 * ways: 1) via a hash table lookup using the DVA as a key, 78 * or 2) via one of the ARC lists. The arc_read() inerface 79 * uses method 1, while the internal arc algorithms for 80 * adjusting the cache use method 2. We therefor provide two 81 * types of locks: 1) the hash table lock array, and 2) the 82 * arc list locks. 83 * 84 * Buffers do not have their own mutexs, rather they rely on the 85 * hash table mutexs for the bulk of their protection (i.e. most 86 * fields in the arc_buf_hdr_t are protected by these mutexs). 87 * 88 * buf_hash_find() returns the appropriate mutex (held) when it 89 * locates the requested buffer in the hash table. It returns 90 * NULL for the mutex if the buffer was not in the table. 91 * 92 * buf_hash_remove() expects the appropriate hash mutex to be 93 * already held before it is invoked. 94 * 95 * Each arc state also has a mutex which is used to protect the 96 * buffer list associated with the state. When attempting to 97 * obtain a hash table lock while holding an arc list lock you 98 * must use: mutex_tryenter() to avoid deadlock. Also note that 99 * the "top" state mutex must be held before the "bot" state mutex. 100 * 101 * Arc buffers may have an associated eviction callback function. 102 * This function will be invoked prior to removing the buffer (e.g. 103 * in arc_do_user_evicts()). Note however that the data associated 104 * with the buffer may be evicted prior to the callback. The callback 105 * must be made with *no locks held* (to prevent deadlock). Additionally, 106 * the users of callbacks must ensure that their private data is 107 * protected from simultaneous callbacks from arc_buf_evict() 108 * and arc_do_user_evicts(). 109 * 110 * Note that the majority of the performance stats are manipulated 111 * with atomic operations. 112 */ 113 114 #include <sys/spa.h> 115 #include <sys/zio.h> 116 #include <sys/zfs_context.h> 117 #include <sys/arc.h> 118 #include <sys/refcount.h> 119 #ifdef _KERNEL 120 #include <sys/vmsystm.h> 121 #include <vm/anon.h> 122 #include <sys/fs/swapnode.h> 123 #include <sys/dnlc.h> 124 #endif 125 #include <sys/callb.h> 126 127 static kmutex_t arc_reclaim_thr_lock; 128 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */ 129 static uint8_t arc_thread_exit; 130 131 #define ARC_REDUCE_DNLC_PERCENT 3 132 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT; 133 134 typedef enum arc_reclaim_strategy { 135 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */ 136 ARC_RECLAIM_CONS /* Conservative reclaim strategy */ 137 } arc_reclaim_strategy_t; 138 139 /* number of seconds before growing cache again */ 140 static int arc_grow_retry = 60; 141 142 static kmutex_t arc_reclaim_lock; 143 static int arc_dead; 144 145 /* 146 * Note that buffers can be on one of 5 states: 147 * ARC_anon - anonymous (discussed below) 148 * ARC_mru - recently used, currently cached 149 * ARC_mru_ghost - recentely used, no longer in cache 150 * ARC_mfu - frequently used, currently cached 151 * ARC_mfu_ghost - frequently used, no longer in cache 152 * When there are no active references to the buffer, they 153 * are linked onto one of the lists in arc. These are the 154 * only buffers that can be evicted or deleted. 155 * 156 * Anonymous buffers are buffers that are not associated with 157 * a DVA. These are buffers that hold dirty block copies 158 * before they are written to stable storage. By definition, 159 * they are "ref'd" and are considered part of arc_mru 160 * that cannot be freed. Generally, they will aquire a DVA 161 * as they are written and migrate onto the arc_mru list. 162 */ 163 164 typedef struct arc_state { 165 list_t list; /* linked list of evictable buffer in state */ 166 uint64_t lsize; /* total size of buffers in the linked list */ 167 uint64_t size; /* total size of all buffers in this state */ 168 uint64_t hits; 169 kmutex_t mtx; 170 } arc_state_t; 171 172 /* The 5 states: */ 173 static arc_state_t ARC_anon; 174 static arc_state_t ARC_mru; 175 static arc_state_t ARC_mru_ghost; 176 static arc_state_t ARC_mfu; 177 static arc_state_t ARC_mfu_ghost; 178 179 static struct arc { 180 arc_state_t *anon; 181 arc_state_t *mru; 182 arc_state_t *mru_ghost; 183 arc_state_t *mfu; 184 arc_state_t *mfu_ghost; 185 uint64_t size; /* Actual total arc size */ 186 uint64_t p; /* Target size (in bytes) of mru */ 187 uint64_t c; /* Target size of cache (in bytes) */ 188 uint64_t c_min; /* Minimum target cache size */ 189 uint64_t c_max; /* Maximum target cache size */ 190 191 /* performance stats */ 192 uint64_t hits; 193 uint64_t misses; 194 uint64_t deleted; 195 uint64_t skipped; 196 uint64_t hash_elements; 197 uint64_t hash_elements_max; 198 uint64_t hash_collisions; 199 uint64_t hash_chains; 200 uint32_t hash_chain_max; 201 202 int no_grow; /* Don't try to grow cache size */ 203 } arc; 204 205 static uint64_t arc_tempreserve; 206 207 typedef struct arc_callback arc_callback_t; 208 209 struct arc_callback { 210 arc_done_func_t *acb_done; 211 void *acb_private; 212 arc_byteswap_func_t *acb_byteswap; 213 arc_buf_t *acb_buf; 214 zio_t *acb_zio_dummy; 215 arc_callback_t *acb_next; 216 }; 217 218 struct arc_buf_hdr { 219 /* immutable */ 220 uint64_t b_size; 221 spa_t *b_spa; 222 223 /* protected by hash lock */ 224 dva_t b_dva; 225 uint64_t b_birth; 226 uint64_t b_cksum0; 227 228 arc_buf_hdr_t *b_hash_next; 229 arc_buf_t *b_buf; 230 uint32_t b_flags; 231 uint32_t b_datacnt; 232 233 kcondvar_t b_cv; 234 arc_callback_t *b_acb; 235 236 /* protected by arc state mutex */ 237 arc_state_t *b_state; 238 list_node_t b_arc_node; 239 240 /* updated atomically */ 241 clock_t b_arc_access; 242 243 /* self protecting */ 244 refcount_t b_refcnt; 245 }; 246 247 static arc_buf_t *arc_eviction_list; 248 static kmutex_t arc_eviction_mtx; 249 static void arc_access_and_exit(arc_buf_hdr_t *buf, kmutex_t *hash_lock); 250 251 #define GHOST_STATE(state) \ 252 ((state) == arc.mru_ghost || (state) == arc.mfu_ghost) 253 254 /* 255 * Private ARC flags. These flags are private ARC only flags that will show up 256 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can 257 * be passed in as arc_flags in things like arc_read. However, these flags 258 * should never be passed and should only be set by ARC code. When adding new 259 * public flags, make sure not to smash the private ones. 260 */ 261 262 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */ 263 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */ 264 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */ 265 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */ 266 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */ 267 268 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE) 269 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS) 270 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR) 271 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ) 272 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE) 273 274 /* 275 * Hash table routines 276 */ 277 278 #define HT_LOCK_PAD 64 279 280 struct ht_lock { 281 kmutex_t ht_lock; 282 #ifdef _KERNEL 283 unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))]; 284 #endif 285 }; 286 287 #define BUF_LOCKS 256 288 typedef struct buf_hash_table { 289 uint64_t ht_mask; 290 arc_buf_hdr_t **ht_table; 291 struct ht_lock ht_locks[BUF_LOCKS]; 292 } buf_hash_table_t; 293 294 static buf_hash_table_t buf_hash_table; 295 296 #define BUF_HASH_INDEX(spa, dva, birth) \ 297 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 298 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 299 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock)) 300 #define HDR_LOCK(buf) \ 301 (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth))) 302 303 uint64_t zfs_crc64_table[256]; 304 305 static uint64_t 306 buf_hash(spa_t *spa, dva_t *dva, uint64_t birth) 307 { 308 uintptr_t spav = (uintptr_t)spa; 309 uint8_t *vdva = (uint8_t *)dva; 310 uint64_t crc = -1ULL; 311 int i; 312 313 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY); 314 315 for (i = 0; i < sizeof (dva_t); i++) 316 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF]; 317 318 crc ^= (spav>>8) ^ birth; 319 320 return (crc); 321 } 322 323 #define BUF_EMPTY(buf) \ 324 ((buf)->b_dva.dva_word[0] == 0 && \ 325 (buf)->b_dva.dva_word[1] == 0 && \ 326 (buf)->b_birth == 0) 327 328 #define BUF_EQUAL(spa, dva, birth, buf) \ 329 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 330 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 331 ((buf)->b_birth == birth) && ((buf)->b_spa == spa) 332 333 static arc_buf_hdr_t * 334 buf_hash_find(spa_t *spa, dva_t *dva, uint64_t birth, kmutex_t **lockp) 335 { 336 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 337 kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 338 arc_buf_hdr_t *buf; 339 340 mutex_enter(hash_lock); 341 for (buf = buf_hash_table.ht_table[idx]; buf != NULL; 342 buf = buf->b_hash_next) { 343 if (BUF_EQUAL(spa, dva, birth, buf)) { 344 *lockp = hash_lock; 345 return (buf); 346 } 347 } 348 mutex_exit(hash_lock); 349 *lockp = NULL; 350 return (NULL); 351 } 352 353 /* 354 * Insert an entry into the hash table. If there is already an element 355 * equal to elem in the hash table, then the already existing element 356 * will be returned and the new element will not be inserted. 357 * Otherwise returns NULL. 358 */ 359 static arc_buf_hdr_t *fbufs[4]; /* XXX to find 6341326 */ 360 static kthread_t *fbufs_lastthread; 361 static arc_buf_hdr_t * 362 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp) 363 { 364 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 365 kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 366 arc_buf_hdr_t *fbuf; 367 uint32_t max, i; 368 369 ASSERT(!HDR_IN_HASH_TABLE(buf)); 370 fbufs_lastthread = curthread; 371 *lockp = hash_lock; 372 mutex_enter(hash_lock); 373 for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL; 374 fbuf = fbuf->b_hash_next, i++) { 375 if (i < sizeof (fbufs) / sizeof (fbufs[0])) 376 fbufs[i] = fbuf; 377 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf)) 378 return (fbuf); 379 } 380 381 buf->b_hash_next = buf_hash_table.ht_table[idx]; 382 buf_hash_table.ht_table[idx] = buf; 383 buf->b_flags |= ARC_IN_HASH_TABLE; 384 385 /* collect some hash table performance data */ 386 if (i > 0) { 387 atomic_add_64(&arc.hash_collisions, 1); 388 if (i == 1) 389 atomic_add_64(&arc.hash_chains, 1); 390 } 391 while (i > (max = arc.hash_chain_max) && 392 max != atomic_cas_32(&arc.hash_chain_max, max, i)) { 393 continue; 394 } 395 atomic_add_64(&arc.hash_elements, 1); 396 if (arc.hash_elements > arc.hash_elements_max) 397 atomic_add_64(&arc.hash_elements_max, 1); 398 399 return (NULL); 400 } 401 402 static void 403 buf_hash_remove(arc_buf_hdr_t *buf) 404 { 405 arc_buf_hdr_t *fbuf, **bufp; 406 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth); 407 408 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 409 ASSERT(HDR_IN_HASH_TABLE(buf)); 410 411 bufp = &buf_hash_table.ht_table[idx]; 412 while ((fbuf = *bufp) != buf) { 413 ASSERT(fbuf != NULL); 414 bufp = &fbuf->b_hash_next; 415 } 416 *bufp = buf->b_hash_next; 417 buf->b_hash_next = NULL; 418 buf->b_flags &= ~ARC_IN_HASH_TABLE; 419 420 /* collect some hash table performance data */ 421 atomic_add_64(&arc.hash_elements, -1); 422 if (buf_hash_table.ht_table[idx] && 423 buf_hash_table.ht_table[idx]->b_hash_next == NULL) 424 atomic_add_64(&arc.hash_chains, -1); 425 } 426 427 /* 428 * Global data structures and functions for the buf kmem cache. 429 */ 430 static kmem_cache_t *hdr_cache; 431 static kmem_cache_t *buf_cache; 432 433 static void 434 buf_fini(void) 435 { 436 int i; 437 438 kmem_free(buf_hash_table.ht_table, 439 (buf_hash_table.ht_mask + 1) * sizeof (void *)); 440 for (i = 0; i < BUF_LOCKS; i++) 441 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock); 442 kmem_cache_destroy(hdr_cache); 443 kmem_cache_destroy(buf_cache); 444 } 445 446 /* 447 * Constructor callback - called when the cache is empty 448 * and a new buf is requested. 449 */ 450 /* ARGSUSED */ 451 static int 452 hdr_cons(void *vbuf, void *unused, int kmflag) 453 { 454 arc_buf_hdr_t *buf = vbuf; 455 456 bzero(buf, sizeof (arc_buf_hdr_t)); 457 refcount_create(&buf->b_refcnt); 458 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL); 459 return (0); 460 } 461 462 /* 463 * Destructor callback - called when a cached buf is 464 * no longer required. 465 */ 466 /* ARGSUSED */ 467 static void 468 hdr_dest(void *vbuf, void *unused) 469 { 470 arc_buf_hdr_t *buf = vbuf; 471 472 refcount_destroy(&buf->b_refcnt); 473 cv_destroy(&buf->b_cv); 474 } 475 476 static int arc_reclaim_needed(void); 477 void arc_kmem_reclaim(void); 478 479 /* 480 * Reclaim callback -- invoked when memory is low. 481 */ 482 /* ARGSUSED */ 483 static void 484 hdr_recl(void *unused) 485 { 486 dprintf("hdr_recl called\n"); 487 if (arc_reclaim_needed()) 488 arc_kmem_reclaim(); 489 } 490 491 static void 492 buf_init(void) 493 { 494 uint64_t *ct; 495 uint64_t hsize = 1ULL << 12; 496 int i, j; 497 498 /* 499 * The hash table is big enough to fill all of physical memory 500 * with an average 64K block size. The table will take up 501 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers). 502 */ 503 while (hsize * 65536 < physmem * PAGESIZE) 504 hsize <<= 1; 505 retry: 506 buf_hash_table.ht_mask = hsize - 1; 507 buf_hash_table.ht_table = 508 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 509 if (buf_hash_table.ht_table == NULL) { 510 ASSERT(hsize > (1ULL << 8)); 511 hsize >>= 1; 512 goto retry; 513 } 514 515 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t), 516 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0); 517 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 518 0, NULL, NULL, NULL, NULL, NULL, 0); 519 520 for (i = 0; i < 256; i++) 521 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 522 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 523 524 for (i = 0; i < BUF_LOCKS; i++) { 525 mutex_init(&buf_hash_table.ht_locks[i].ht_lock, 526 NULL, MUTEX_DEFAULT, NULL); 527 } 528 } 529 530 #define ARC_MINTIME (hz>>4) /* 62 ms */ 531 532 static void 533 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 534 { 535 ASSERT(MUTEX_HELD(hash_lock)); 536 537 if ((refcount_add(&ab->b_refcnt, tag) == 1) && 538 (ab->b_state != arc.anon)) { 539 int delta = ab->b_size * ab->b_datacnt; 540 541 ASSERT(!MUTEX_HELD(&ab->b_state->mtx)); 542 mutex_enter(&ab->b_state->mtx); 543 ASSERT(refcount_count(&ab->b_refcnt) > 0); 544 ASSERT(list_link_active(&ab->b_arc_node)); 545 list_remove(&ab->b_state->list, ab); 546 if (GHOST_STATE(ab->b_state)) { 547 ASSERT3U(ab->b_datacnt, ==, 0); 548 ASSERT3P(ab->b_buf, ==, NULL); 549 delta = ab->b_size; 550 } 551 ASSERT(delta > 0); 552 ASSERT3U(ab->b_state->lsize, >=, delta); 553 atomic_add_64(&ab->b_state->lsize, -delta); 554 mutex_exit(&ab->b_state->mtx); 555 } 556 } 557 558 static int 559 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag) 560 { 561 int cnt; 562 563 ASSERT(ab->b_state == arc.anon || MUTEX_HELD(hash_lock)); 564 ASSERT(!GHOST_STATE(ab->b_state)); 565 566 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) && 567 (ab->b_state != arc.anon)) { 568 569 ASSERT(!MUTEX_HELD(&ab->b_state->mtx)); 570 mutex_enter(&ab->b_state->mtx); 571 ASSERT(!list_link_active(&ab->b_arc_node)); 572 list_insert_head(&ab->b_state->list, ab); 573 ASSERT(ab->b_datacnt > 0); 574 atomic_add_64(&ab->b_state->lsize, ab->b_size * ab->b_datacnt); 575 ASSERT3U(ab->b_state->size, >=, ab->b_state->lsize); 576 mutex_exit(&ab->b_state->mtx); 577 } 578 return (cnt); 579 } 580 581 /* 582 * Move the supplied buffer to the indicated state. The mutex 583 * for the buffer must be held by the caller. 584 */ 585 static void 586 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock) 587 { 588 arc_state_t *old_state = ab->b_state; 589 int refcnt = refcount_count(&ab->b_refcnt); 590 int from_delta, to_delta; 591 592 ASSERT(MUTEX_HELD(hash_lock)); 593 ASSERT(new_state != old_state); 594 ASSERT(refcnt == 0 || ab->b_datacnt > 0); 595 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state)); 596 597 from_delta = to_delta = ab->b_datacnt * ab->b_size; 598 599 /* 600 * If this buffer is evictable, transfer it from the 601 * old state list to the new state list. 602 */ 603 if (refcnt == 0) { 604 if (old_state != arc.anon) { 605 int use_mutex = !MUTEX_HELD(&old_state->mtx); 606 607 if (use_mutex) 608 mutex_enter(&old_state->mtx); 609 610 ASSERT(list_link_active(&ab->b_arc_node)); 611 list_remove(&old_state->list, ab); 612 613 /* ghost elements have a ghost size */ 614 if (GHOST_STATE(old_state)) { 615 ASSERT(ab->b_datacnt == 0); 616 ASSERT(ab->b_buf == NULL); 617 from_delta = ab->b_size; 618 } 619 ASSERT3U(old_state->lsize, >=, from_delta); 620 atomic_add_64(&old_state->lsize, -from_delta); 621 622 if (use_mutex) 623 mutex_exit(&old_state->mtx); 624 } 625 if (new_state != arc.anon) { 626 int use_mutex = !MUTEX_HELD(&new_state->mtx); 627 628 if (use_mutex) 629 mutex_enter(&new_state->mtx); 630 631 list_insert_head(&new_state->list, ab); 632 633 /* ghost elements have a ghost size */ 634 if (GHOST_STATE(new_state)) { 635 ASSERT(ab->b_datacnt == 0); 636 ASSERT(ab->b_buf == NULL); 637 to_delta = ab->b_size; 638 } 639 atomic_add_64(&new_state->lsize, to_delta); 640 ASSERT3U(new_state->size + to_delta, >=, 641 new_state->lsize); 642 643 if (use_mutex) 644 mutex_exit(&new_state->mtx); 645 } 646 } 647 648 ASSERT(!BUF_EMPTY(ab)); 649 if (new_state == arc.anon && old_state != arc.anon) { 650 buf_hash_remove(ab); 651 } 652 653 /* 654 * If this buffer isn't being transferred to the MRU-top 655 * state, it's safe to clear its prefetch flag 656 */ 657 if ((new_state != arc.mru) && (new_state != arc.mru_ghost)) { 658 ab->b_flags &= ~ARC_PREFETCH; 659 } 660 661 /* adjust state sizes */ 662 if (to_delta) 663 atomic_add_64(&new_state->size, to_delta); 664 if (from_delta) { 665 ASSERT3U(old_state->size, >=, from_delta); 666 atomic_add_64(&old_state->size, -from_delta); 667 } 668 ab->b_state = new_state; 669 } 670 671 arc_buf_t * 672 arc_buf_alloc(spa_t *spa, int size, void *tag) 673 { 674 arc_buf_hdr_t *hdr; 675 arc_buf_t *buf; 676 677 ASSERT3U(size, >, 0); 678 hdr = kmem_cache_alloc(hdr_cache, KM_SLEEP); 679 ASSERT(BUF_EMPTY(hdr)); 680 hdr->b_size = size; 681 hdr->b_spa = spa; 682 hdr->b_state = arc.anon; 683 hdr->b_arc_access = 0; 684 buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 685 buf->b_hdr = hdr; 686 buf->b_efunc = NULL; 687 buf->b_private = NULL; 688 buf->b_next = NULL; 689 buf->b_data = zio_buf_alloc(size); 690 hdr->b_buf = buf; 691 hdr->b_datacnt = 1; 692 hdr->b_flags = 0; 693 ASSERT(refcount_is_zero(&hdr->b_refcnt)); 694 (void) refcount_add(&hdr->b_refcnt, tag); 695 696 atomic_add_64(&arc.size, size); 697 atomic_add_64(&arc.anon->size, size); 698 699 return (buf); 700 } 701 702 static void * 703 arc_data_copy(arc_buf_hdr_t *hdr, void *old_data) 704 { 705 void *new_data = zio_buf_alloc(hdr->b_size); 706 707 atomic_add_64(&arc.size, hdr->b_size); 708 bcopy(old_data, new_data, hdr->b_size); 709 atomic_add_64(&hdr->b_state->size, hdr->b_size); 710 if (list_link_active(&hdr->b_arc_node)) { 711 ASSERT(refcount_is_zero(&hdr->b_refcnt)); 712 atomic_add_64(&hdr->b_state->lsize, hdr->b_size); 713 } 714 return (new_data); 715 } 716 717 void 718 arc_buf_add_ref(arc_buf_t *buf, void* tag) 719 { 720 arc_buf_hdr_t *hdr; 721 kmutex_t *hash_lock; 722 723 mutex_enter(&arc_eviction_mtx); 724 hdr = buf->b_hdr; 725 if (buf->b_data == NULL) { 726 /* 727 * This buffer is evicted. 728 */ 729 mutex_exit(&arc_eviction_mtx); 730 return; 731 } else { 732 /* 733 * Prevent this buffer from being evicted 734 * while we add a reference. 735 */ 736 buf->b_hdr = NULL; 737 } 738 mutex_exit(&arc_eviction_mtx); 739 740 ASSERT(hdr->b_state != arc.anon); 741 hash_lock = HDR_LOCK(hdr); 742 mutex_enter(hash_lock); 743 ASSERT(!GHOST_STATE(hdr->b_state)); 744 buf->b_hdr = hdr; 745 add_reference(hdr, hash_lock, tag); 746 arc_access_and_exit(hdr, hash_lock); 747 atomic_add_64(&arc.hits, 1); 748 } 749 750 static void 751 arc_buf_destroy(arc_buf_t *buf, boolean_t all) 752 { 753 arc_buf_t **bufp; 754 755 /* free up data associated with the buf */ 756 if (buf->b_data) { 757 arc_state_t *state = buf->b_hdr->b_state; 758 uint64_t size = buf->b_hdr->b_size; 759 760 zio_buf_free(buf->b_data, size); 761 atomic_add_64(&arc.size, -size); 762 if (list_link_active(&buf->b_hdr->b_arc_node)) { 763 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt)); 764 ASSERT(state != arc.anon); 765 ASSERT3U(state->lsize, >=, size); 766 atomic_add_64(&state->lsize, -size); 767 } 768 ASSERT3U(state->size, >=, size); 769 atomic_add_64(&state->size, -size); 770 buf->b_data = NULL; 771 ASSERT(buf->b_hdr->b_datacnt > 0); 772 buf->b_hdr->b_datacnt -= 1; 773 } 774 775 /* only remove the buf if requested */ 776 if (!all) 777 return; 778 779 /* remove the buf from the hdr list */ 780 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next) 781 continue; 782 *bufp = buf->b_next; 783 784 ASSERT(buf->b_efunc == NULL); 785 786 /* clean up the buf */ 787 buf->b_hdr = NULL; 788 kmem_cache_free(buf_cache, buf); 789 } 790 791 static void 792 arc_hdr_destroy(arc_buf_hdr_t *hdr) 793 { 794 ASSERT(refcount_is_zero(&hdr->b_refcnt)); 795 ASSERT3P(hdr->b_state, ==, arc.anon); 796 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 797 798 if (!BUF_EMPTY(hdr)) { 799 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 800 bzero(&hdr->b_dva, sizeof (dva_t)); 801 hdr->b_birth = 0; 802 hdr->b_cksum0 = 0; 803 } 804 while (hdr->b_buf) { 805 arc_buf_t *buf = hdr->b_buf; 806 807 if (buf->b_efunc) { 808 mutex_enter(&arc_eviction_mtx); 809 ASSERT(buf->b_hdr != NULL); 810 arc_buf_destroy(hdr->b_buf, FALSE); 811 hdr->b_buf = buf->b_next; 812 buf->b_next = arc_eviction_list; 813 arc_eviction_list = buf; 814 mutex_exit(&arc_eviction_mtx); 815 } else { 816 arc_buf_destroy(hdr->b_buf, TRUE); 817 } 818 } 819 820 ASSERT(!list_link_active(&hdr->b_arc_node)); 821 ASSERT3P(hdr->b_hash_next, ==, NULL); 822 ASSERT3P(hdr->b_acb, ==, NULL); 823 kmem_cache_free(hdr_cache, hdr); 824 } 825 826 void 827 arc_buf_free(arc_buf_t *buf, void *tag) 828 { 829 arc_buf_hdr_t *hdr = buf->b_hdr; 830 int hashed = hdr->b_state != arc.anon; 831 832 ASSERT(buf->b_efunc == NULL); 833 ASSERT(buf->b_data != NULL); 834 835 if (hashed) { 836 kmutex_t *hash_lock = HDR_LOCK(hdr); 837 838 mutex_enter(hash_lock); 839 (void) remove_reference(hdr, hash_lock, tag); 840 if (hdr->b_datacnt > 1) 841 arc_buf_destroy(buf, TRUE); 842 else 843 hdr->b_flags |= ARC_BUF_AVAILABLE; 844 mutex_exit(hash_lock); 845 } else if (HDR_IO_IN_PROGRESS(hdr)) { 846 int destroy_hdr; 847 /* 848 * We are in the middle of an async write. Don't destroy 849 * this buffer unless the write completes before we finish 850 * decrementing the reference count. 851 */ 852 mutex_enter(&arc_eviction_mtx); 853 (void) remove_reference(hdr, NULL, tag); 854 ASSERT(refcount_is_zero(&hdr->b_refcnt)); 855 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr); 856 mutex_exit(&arc_eviction_mtx); 857 if (destroy_hdr) 858 arc_hdr_destroy(hdr); 859 } else { 860 if (remove_reference(hdr, NULL, tag) > 0) { 861 ASSERT(HDR_IO_ERROR(hdr)); 862 arc_buf_destroy(buf, TRUE); 863 } else { 864 arc_hdr_destroy(hdr); 865 } 866 } 867 } 868 869 int 870 arc_buf_remove_ref(arc_buf_t *buf, void* tag) 871 { 872 arc_buf_hdr_t *hdr = buf->b_hdr; 873 kmutex_t *hash_lock = HDR_LOCK(hdr); 874 int no_callback = (buf->b_efunc == NULL); 875 876 if (hdr->b_state == arc.anon) { 877 arc_buf_free(buf, tag); 878 return (no_callback); 879 } 880 881 mutex_enter(hash_lock); 882 ASSERT(hdr->b_state != arc.anon); 883 ASSERT(buf->b_data != NULL); 884 885 (void) remove_reference(hdr, hash_lock, tag); 886 if (hdr->b_datacnt > 1) { 887 if (no_callback) 888 arc_buf_destroy(buf, TRUE); 889 } else if (no_callback) { 890 ASSERT(hdr->b_buf == buf && buf->b_next == NULL); 891 hdr->b_flags |= ARC_BUF_AVAILABLE; 892 } 893 ASSERT(no_callback || hdr->b_datacnt > 1 || 894 refcount_is_zero(&hdr->b_refcnt)); 895 mutex_exit(hash_lock); 896 return (no_callback); 897 } 898 899 int 900 arc_buf_size(arc_buf_t *buf) 901 { 902 return (buf->b_hdr->b_size); 903 } 904 905 /* 906 * Evict buffers from list until we've removed the specified number of 907 * bytes. Move the removed buffers to the appropriate evict state. 908 */ 909 static uint64_t 910 arc_evict(arc_state_t *state, int64_t bytes) 911 { 912 arc_state_t *evicted_state; 913 uint64_t bytes_evicted = 0, skipped = 0; 914 arc_buf_hdr_t *ab, *ab_prev; 915 kmutex_t *hash_lock; 916 917 ASSERT(state == arc.mru || state == arc.mfu); 918 919 evicted_state = (state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost; 920 921 mutex_enter(&state->mtx); 922 mutex_enter(&evicted_state->mtx); 923 924 for (ab = list_tail(&state->list); ab; ab = ab_prev) { 925 ab_prev = list_prev(&state->list, ab); 926 hash_lock = HDR_LOCK(ab); 927 if (mutex_tryenter(hash_lock)) { 928 ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0); 929 ASSERT(ab->b_datacnt > 0); 930 while (ab->b_buf) { 931 arc_buf_t *buf = ab->b_buf; 932 if (buf->b_data) 933 bytes_evicted += ab->b_size; 934 if (buf->b_efunc) { 935 mutex_enter(&arc_eviction_mtx); 936 /* 937 * arc_buf_add_ref() could derail 938 * this eviction. 939 */ 940 if (buf->b_hdr == NULL) { 941 mutex_exit(&arc_eviction_mtx); 942 mutex_exit(hash_lock); 943 goto skip; 944 } 945 arc_buf_destroy(buf, FALSE); 946 ab->b_buf = buf->b_next; 947 buf->b_next = arc_eviction_list; 948 arc_eviction_list = buf; 949 mutex_exit(&arc_eviction_mtx); 950 } else { 951 arc_buf_destroy(buf, TRUE); 952 } 953 } 954 ASSERT(ab->b_datacnt == 0); 955 arc_change_state(evicted_state, ab, hash_lock); 956 ASSERT(HDR_IN_HASH_TABLE(ab)); 957 ab->b_flags = ARC_IN_HASH_TABLE; 958 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab); 959 mutex_exit(hash_lock); 960 if (bytes >= 0 && bytes_evicted >= bytes) 961 break; 962 } else { 963 skip: 964 skipped += 1; 965 } 966 } 967 mutex_exit(&evicted_state->mtx); 968 mutex_exit(&state->mtx); 969 970 if (bytes_evicted < bytes) 971 dprintf("only evicted %lld bytes from %x", 972 (longlong_t)bytes_evicted, state); 973 974 atomic_add_64(&arc.skipped, skipped); 975 if (bytes < 0) 976 return (skipped); 977 return (bytes_evicted); 978 } 979 980 /* 981 * Remove buffers from list until we've removed the specified number of 982 * bytes. Destroy the buffers that are removed. 983 */ 984 static void 985 arc_evict_ghost(arc_state_t *state, int64_t bytes) 986 { 987 arc_buf_hdr_t *ab, *ab_prev; 988 kmutex_t *hash_lock; 989 uint64_t bytes_deleted = 0; 990 uint_t bufs_skipped = 0; 991 992 ASSERT(GHOST_STATE(state)); 993 top: 994 mutex_enter(&state->mtx); 995 for (ab = list_tail(&state->list); ab; ab = ab_prev) { 996 ab_prev = list_prev(&state->list, ab); 997 hash_lock = HDR_LOCK(ab); 998 if (mutex_tryenter(hash_lock)) { 999 ASSERT(ab->b_buf == NULL); 1000 arc_change_state(arc.anon, ab, hash_lock); 1001 mutex_exit(hash_lock); 1002 atomic_add_64(&arc.deleted, 1); 1003 bytes_deleted += ab->b_size; 1004 arc_hdr_destroy(ab); 1005 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab); 1006 if (bytes >= 0 && bytes_deleted >= bytes) 1007 break; 1008 } else { 1009 if (bytes < 0) { 1010 mutex_exit(&state->mtx); 1011 mutex_enter(hash_lock); 1012 mutex_exit(hash_lock); 1013 goto top; 1014 } 1015 bufs_skipped += 1; 1016 } 1017 } 1018 mutex_exit(&state->mtx); 1019 1020 if (bufs_skipped) { 1021 atomic_add_64(&arc.skipped, bufs_skipped); 1022 ASSERT(bytes >= 0); 1023 } 1024 1025 if (bytes_deleted < bytes) 1026 dprintf("only deleted %lld bytes from %p", 1027 (longlong_t)bytes_deleted, state); 1028 } 1029 1030 static void 1031 arc_adjust(void) 1032 { 1033 int64_t top_sz, mru_over, arc_over; 1034 1035 top_sz = arc.anon->size + arc.mru->size; 1036 1037 if (top_sz > arc.p && arc.mru->lsize > 0) { 1038 int64_t toevict = MIN(arc.mru->lsize, top_sz-arc.p); 1039 (void) arc_evict(arc.mru, toevict); 1040 top_sz = arc.anon->size + arc.mru->size; 1041 } 1042 1043 mru_over = top_sz + arc.mru_ghost->size - arc.c; 1044 1045 if (mru_over > 0) { 1046 if (arc.mru_ghost->lsize > 0) { 1047 int64_t todelete = MIN(arc.mru_ghost->lsize, mru_over); 1048 arc_evict_ghost(arc.mru_ghost, todelete); 1049 } 1050 } 1051 1052 if ((arc_over = arc.size - arc.c) > 0) { 1053 int64_t tbl_over; 1054 1055 if (arc.mfu->lsize > 0) { 1056 int64_t toevict = MIN(arc.mfu->lsize, arc_over); 1057 (void) arc_evict(arc.mfu, toevict); 1058 } 1059 1060 tbl_over = arc.size + arc.mru_ghost->lsize + 1061 arc.mfu_ghost->lsize - arc.c*2; 1062 1063 if (tbl_over > 0 && arc.mfu_ghost->lsize > 0) { 1064 int64_t todelete = MIN(arc.mfu_ghost->lsize, tbl_over); 1065 arc_evict_ghost(arc.mfu_ghost, todelete); 1066 } 1067 } 1068 } 1069 1070 static void 1071 arc_do_user_evicts(void) 1072 { 1073 mutex_enter(&arc_eviction_mtx); 1074 while (arc_eviction_list != NULL) { 1075 arc_buf_t *buf = arc_eviction_list; 1076 arc_eviction_list = buf->b_next; 1077 buf->b_hdr = NULL; 1078 mutex_exit(&arc_eviction_mtx); 1079 1080 ASSERT(buf->b_efunc != NULL); 1081 VERIFY(buf->b_efunc(buf) == 0); 1082 1083 buf->b_efunc = NULL; 1084 buf->b_private = NULL; 1085 kmem_cache_free(buf_cache, buf); 1086 mutex_enter(&arc_eviction_mtx); 1087 } 1088 mutex_exit(&arc_eviction_mtx); 1089 } 1090 1091 /* 1092 * Flush all *evictable* data from the cache. 1093 * NOTE: this will not touch "active" (i.e. referenced) data. 1094 */ 1095 void 1096 arc_flush(void) 1097 { 1098 while (arc_evict(arc.mru, -1)); 1099 while (arc_evict(arc.mfu, -1)); 1100 1101 arc_evict_ghost(arc.mru_ghost, -1); 1102 arc_evict_ghost(arc.mfu_ghost, -1); 1103 1104 mutex_enter(&arc_reclaim_thr_lock); 1105 arc_do_user_evicts(); 1106 mutex_exit(&arc_reclaim_thr_lock); 1107 ASSERT(arc_eviction_list == NULL); 1108 } 1109 1110 void 1111 arc_kmem_reclaim(void) 1112 { 1113 /* Remove 12.5% */ 1114 /* 1115 * We need arc_reclaim_lock because we don't want multiple 1116 * threads trying to reclaim concurrently. 1117 */ 1118 1119 /* 1120 * umem calls the reclaim func when we destroy the buf cache, 1121 * which is after we do arc_fini(). So we set a flag to prevent 1122 * accessing the destroyed mutexes and lists. 1123 */ 1124 if (arc_dead) 1125 return; 1126 1127 if (arc.c <= arc.c_min) 1128 return; 1129 1130 mutex_enter(&arc_reclaim_lock); 1131 1132 atomic_add_64(&arc.c, -(arc.c >> 3)); 1133 atomic_add_64(&arc.p, -(arc.p >> 3)); 1134 if (arc.c > arc.size) 1135 arc.c = arc.size; 1136 if (arc.c < arc.c_min) 1137 arc.c = arc.c_min; 1138 if (arc.p > arc.c) 1139 arc.p = (arc.c >> 1); 1140 ASSERT((int64_t)arc.p >= 0); 1141 1142 arc_adjust(); 1143 1144 mutex_exit(&arc_reclaim_lock); 1145 } 1146 1147 static int 1148 arc_reclaim_needed(void) 1149 { 1150 uint64_t extra; 1151 1152 #ifdef _KERNEL 1153 /* 1154 * take 'desfree' extra pages, so we reclaim sooner, rather than later 1155 */ 1156 extra = desfree; 1157 1158 /* 1159 * check that we're out of range of the pageout scanner. It starts to 1160 * schedule paging if freemem is less than lotsfree and needfree. 1161 * lotsfree is the high-water mark for pageout, and needfree is the 1162 * number of needed free pages. We add extra pages here to make sure 1163 * the scanner doesn't start up while we're freeing memory. 1164 */ 1165 if (freemem < lotsfree + needfree + extra) 1166 return (1); 1167 1168 /* 1169 * check to make sure that swapfs has enough space so that anon 1170 * reservations can still succeeed. anon_resvmem() checks that the 1171 * availrmem is greater than swapfs_minfree, and the number of reserved 1172 * swap pages. We also add a bit of extra here just to prevent 1173 * circumstances from getting really dire. 1174 */ 1175 if (availrmem < swapfs_minfree + swapfs_reserve + extra) 1176 return (1); 1177 1178 /* 1179 * If we're on an i386 platform, it's possible that we'll exhaust the 1180 * kernel heap space before we ever run out of available physical 1181 * memory. Most checks of the size of the heap_area compare against 1182 * tune.t_minarmem, which is the minimum available real memory that we 1183 * can have in the system. However, this is generally fixed at 25 pages 1184 * which is so low that it's useless. In this comparison, we seek to 1185 * calculate the total heap-size, and reclaim if more than 3/4ths of the 1186 * heap is allocated. (Or, in the caclulation, if less than 1/4th is 1187 * free) 1188 */ 1189 #if defined(__i386) 1190 if (btop(vmem_size(heap_arena, VMEM_FREE)) < 1191 (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2)) 1192 return (1); 1193 #endif 1194 1195 #else 1196 if (spa_get_random(100) == 0) 1197 return (1); 1198 #endif 1199 return (0); 1200 } 1201 1202 static void 1203 arc_kmem_reap_now(arc_reclaim_strategy_t strat) 1204 { 1205 size_t i; 1206 kmem_cache_t *prev_cache = NULL; 1207 extern kmem_cache_t *zio_buf_cache[]; 1208 1209 #ifdef _KERNEL 1210 /* 1211 * First purge some DNLC entries, in case the DNLC is using 1212 * up too much memory. 1213 */ 1214 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent); 1215 #endif 1216 1217 /* 1218 * An agressive reclamation will shrink the cache size as well as 1219 * reap free buffers from the arc kmem caches. 1220 */ 1221 if (strat == ARC_RECLAIM_AGGR) 1222 arc_kmem_reclaim(); 1223 1224 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 1225 if (zio_buf_cache[i] != prev_cache) { 1226 prev_cache = zio_buf_cache[i]; 1227 kmem_cache_reap_now(zio_buf_cache[i]); 1228 } 1229 } 1230 kmem_cache_reap_now(buf_cache); 1231 kmem_cache_reap_now(hdr_cache); 1232 } 1233 1234 static void 1235 arc_reclaim_thread(void) 1236 { 1237 clock_t growtime = 0; 1238 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS; 1239 callb_cpr_t cpr; 1240 1241 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG); 1242 1243 mutex_enter(&arc_reclaim_thr_lock); 1244 while (arc_thread_exit == 0) { 1245 if (arc_reclaim_needed()) { 1246 1247 if (arc.no_grow) { 1248 if (last_reclaim == ARC_RECLAIM_CONS) { 1249 last_reclaim = ARC_RECLAIM_AGGR; 1250 } else { 1251 last_reclaim = ARC_RECLAIM_CONS; 1252 } 1253 } else { 1254 arc.no_grow = TRUE; 1255 last_reclaim = ARC_RECLAIM_AGGR; 1256 membar_producer(); 1257 } 1258 1259 /* reset the growth delay for every reclaim */ 1260 growtime = lbolt + (arc_grow_retry * hz); 1261 1262 arc_kmem_reap_now(last_reclaim); 1263 1264 } else if ((growtime > 0) && ((growtime - lbolt) <= 0)) { 1265 arc.no_grow = FALSE; 1266 } 1267 1268 if (arc_eviction_list != NULL) 1269 arc_do_user_evicts(); 1270 1271 /* block until needed, or one second, whichever is shorter */ 1272 CALLB_CPR_SAFE_BEGIN(&cpr); 1273 (void) cv_timedwait(&arc_reclaim_thr_cv, 1274 &arc_reclaim_thr_lock, (lbolt + hz)); 1275 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock); 1276 } 1277 1278 arc_thread_exit = 0; 1279 cv_broadcast(&arc_reclaim_thr_cv); 1280 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */ 1281 thread_exit(); 1282 } 1283 1284 /* 1285 * Adapt arc info given the number of bytes we are trying to add and 1286 * the state that we are comming from. This function is only called 1287 * when we are adding new content to the cache. 1288 */ 1289 static void 1290 arc_adapt(int bytes, arc_state_t *state) 1291 { 1292 int mult; 1293 1294 ASSERT(bytes > 0); 1295 /* 1296 * Adapt the target size of the MRU list: 1297 * - if we just hit in the MRU ghost list, then increase 1298 * the target size of the MRU list. 1299 * - if we just hit in the MFU ghost list, then increase 1300 * the target size of the MFU list by decreasing the 1301 * target size of the MRU list. 1302 */ 1303 if (state == arc.mru_ghost) { 1304 mult = ((arc.mru_ghost->size >= arc.mfu_ghost->size) ? 1305 1 : (arc.mfu_ghost->size/arc.mru_ghost->size)); 1306 1307 arc.p = MIN(arc.c, arc.p + bytes * mult); 1308 } else if (state == arc.mfu_ghost) { 1309 mult = ((arc.mfu_ghost->size >= arc.mru_ghost->size) ? 1310 1 : (arc.mru_ghost->size/arc.mfu_ghost->size)); 1311 1312 arc.p = MAX(0, (int64_t)arc.p - bytes * mult); 1313 } 1314 ASSERT((int64_t)arc.p >= 0); 1315 1316 if (arc_reclaim_needed()) { 1317 cv_signal(&arc_reclaim_thr_cv); 1318 return; 1319 } 1320 1321 if (arc.no_grow) 1322 return; 1323 1324 if (arc.c >= arc.c_max) 1325 return; 1326 1327 /* 1328 * If we're within (2 * maxblocksize) bytes of the target 1329 * cache size, increment the target cache size 1330 */ 1331 if (arc.size > arc.c - (2ULL << SPA_MAXBLOCKSHIFT)) { 1332 atomic_add_64(&arc.c, (int64_t)bytes); 1333 if (arc.c > arc.c_max) 1334 arc.c = arc.c_max; 1335 else if (state == arc.anon) 1336 atomic_add_64(&arc.p, (int64_t)bytes); 1337 if (arc.p > arc.c) 1338 arc.p = arc.c; 1339 } 1340 ASSERT((int64_t)arc.p >= 0); 1341 } 1342 1343 /* 1344 * Check if the cache has reached its limits and eviction is required 1345 * prior to insert. 1346 */ 1347 static int 1348 arc_evict_needed() 1349 { 1350 if (arc_reclaim_needed()) 1351 return (1); 1352 1353 return (arc.size > arc.c); 1354 } 1355 1356 /* 1357 * The state, supplied as the first argument, is going to have something 1358 * inserted on its behalf. So, determine which cache must be victimized to 1359 * satisfy an insertion for this state. We have the following cases: 1360 * 1361 * 1. Insert for MRU, p > sizeof(arc.anon + arc.mru) -> 1362 * In this situation if we're out of space, but the resident size of the MFU is 1363 * under the limit, victimize the MFU cache to satisfy this insertion request. 1364 * 1365 * 2. Insert for MRU, p <= sizeof(arc.anon + arc.mru) -> 1366 * Here, we've used up all of the available space for the MRU, so we need to 1367 * evict from our own cache instead. Evict from the set of resident MRU 1368 * entries. 1369 * 1370 * 3. Insert for MFU (c - p) > sizeof(arc.mfu) -> 1371 * c minus p represents the MFU space in the cache, since p is the size of the 1372 * cache that is dedicated to the MRU. In this situation there's still space on 1373 * the MFU side, so the MRU side needs to be victimized. 1374 * 1375 * 4. Insert for MFU (c - p) < sizeof(arc.mfu) -> 1376 * MFU's resident set is consuming more space than it has been allotted. In 1377 * this situation, we must victimize our own cache, the MFU, for this insertion. 1378 */ 1379 static void 1380 arc_evict_for_state(arc_state_t *state, uint64_t bytes) 1381 { 1382 uint64_t mru_used; 1383 uint64_t mfu_space; 1384 uint64_t evicted; 1385 1386 ASSERT(state == arc.mru || state == arc.mfu); 1387 1388 if (state == arc.mru) { 1389 mru_used = arc.anon->size + arc.mru->size; 1390 if (arc.p > mru_used) { 1391 /* case 1 */ 1392 evicted = arc_evict(arc.mfu, bytes); 1393 if (evicted < bytes) { 1394 arc_adjust(); 1395 } 1396 } else { 1397 /* case 2 */ 1398 evicted = arc_evict(arc.mru, bytes); 1399 if (evicted < bytes) { 1400 arc_adjust(); 1401 } 1402 } 1403 } else { 1404 /* MFU case */ 1405 mfu_space = arc.c - arc.p; 1406 if (mfu_space > arc.mfu->size) { 1407 /* case 3 */ 1408 evicted = arc_evict(arc.mru, bytes); 1409 if (evicted < bytes) { 1410 arc_adjust(); 1411 } 1412 } else { 1413 /* case 4 */ 1414 evicted = arc_evict(arc.mfu, bytes); 1415 if (evicted < bytes) { 1416 arc_adjust(); 1417 } 1418 } 1419 } 1420 } 1421 1422 /* 1423 * This routine is called whenever a buffer is accessed. 1424 * NOTE: the hash lock is dropped in this function. 1425 */ 1426 static void 1427 arc_access_and_exit(arc_buf_hdr_t *buf, kmutex_t *hash_lock) 1428 { 1429 arc_state_t *evict_state = NULL; 1430 int blksz; 1431 1432 ASSERT(MUTEX_HELD(hash_lock)); 1433 1434 blksz = buf->b_size; 1435 1436 if (buf->b_state == arc.anon) { 1437 /* 1438 * This buffer is not in the cache, and does not 1439 * appear in our "ghost" list. Add the new buffer 1440 * to the MRU state. 1441 */ 1442 1443 arc_adapt(blksz, arc.anon); 1444 if (arc_evict_needed()) 1445 evict_state = arc.mru; 1446 1447 ASSERT(buf->b_arc_access == 0); 1448 buf->b_arc_access = lbolt; 1449 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 1450 arc_change_state(arc.mru, buf, hash_lock); 1451 1452 } else if (buf->b_state == arc.mru) { 1453 /* 1454 * If this buffer is in the MRU-top state and has the prefetch 1455 * flag, the first read was actually part of a prefetch. In 1456 * this situation, we simply want to clear the flag and return. 1457 * A subsequent access should bump this into the MFU state. 1458 */ 1459 if ((buf->b_flags & ARC_PREFETCH) != 0) { 1460 buf->b_flags &= ~ARC_PREFETCH; 1461 atomic_add_64(&arc.mru->hits, 1); 1462 mutex_exit(hash_lock); 1463 return; 1464 } 1465 1466 /* 1467 * This buffer has been "accessed" only once so far, 1468 * but it is still in the cache. Move it to the MFU 1469 * state. 1470 */ 1471 if (lbolt > buf->b_arc_access + ARC_MINTIME) { 1472 /* 1473 * More than 125ms have passed since we 1474 * instantiated this buffer. Move it to the 1475 * most frequently used state. 1476 */ 1477 buf->b_arc_access = lbolt; 1478 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 1479 arc_change_state(arc.mfu, buf, hash_lock); 1480 } 1481 atomic_add_64(&arc.mru->hits, 1); 1482 } else if (buf->b_state == arc.mru_ghost) { 1483 arc_state_t *new_state; 1484 /* 1485 * This buffer has been "accessed" recently, but 1486 * was evicted from the cache. Move it to the 1487 * MFU state. 1488 */ 1489 1490 if (buf->b_flags & ARC_PREFETCH) { 1491 new_state = arc.mru; 1492 buf->b_flags &= ~ARC_PREFETCH; 1493 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf); 1494 } else { 1495 new_state = arc.mfu; 1496 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 1497 } 1498 1499 arc_adapt(blksz, arc.mru_ghost); 1500 if (arc_evict_needed()) 1501 evict_state = new_state; 1502 1503 buf->b_arc_access = lbolt; 1504 arc_change_state(new_state, buf, hash_lock); 1505 1506 atomic_add_64(&arc.mru_ghost->hits, 1); 1507 } else if (buf->b_state == arc.mfu) { 1508 /* 1509 * This buffer has been accessed more than once and is 1510 * still in the cache. Keep it in the MFU state. 1511 * 1512 * NOTE: the add_reference() that occurred when we did 1513 * the arc_read() should have kicked this off the list, 1514 * so even if it was a prefetch, it will be put back at 1515 * the head of the list when we remove_reference(). 1516 */ 1517 atomic_add_64(&arc.mfu->hits, 1); 1518 } else if (buf->b_state == arc.mfu_ghost) { 1519 /* 1520 * This buffer has been accessed more than once but has 1521 * been evicted from the cache. Move it back to the 1522 * MFU state. 1523 */ 1524 1525 arc_adapt(blksz, arc.mfu_ghost); 1526 if (arc_evict_needed()) 1527 evict_state = arc.mfu; 1528 1529 buf->b_arc_access = lbolt; 1530 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf); 1531 arc_change_state(arc.mfu, buf, hash_lock); 1532 1533 atomic_add_64(&arc.mfu_ghost->hits, 1); 1534 } else { 1535 ASSERT(!"invalid arc state"); 1536 } 1537 1538 mutex_exit(hash_lock); 1539 if (evict_state) 1540 arc_evict_for_state(evict_state, blksz); 1541 } 1542 1543 /* a generic arc_done_func_t which you can use */ 1544 /* ARGSUSED */ 1545 void 1546 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg) 1547 { 1548 bcopy(buf->b_data, arg, buf->b_hdr->b_size); 1549 VERIFY(arc_buf_remove_ref(buf, arg) == 1); 1550 } 1551 1552 /* a generic arc_done_func_t which you can use */ 1553 void 1554 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg) 1555 { 1556 arc_buf_t **bufp = arg; 1557 if (zio && zio->io_error) { 1558 VERIFY(arc_buf_remove_ref(buf, arg) == 1); 1559 *bufp = NULL; 1560 } else { 1561 *bufp = buf; 1562 } 1563 } 1564 1565 static void 1566 arc_read_done(zio_t *zio) 1567 { 1568 arc_buf_hdr_t *hdr; 1569 arc_buf_t *buf; 1570 arc_buf_t *abuf; /* buffer we're assigning to callback */ 1571 kmutex_t *hash_lock; 1572 arc_callback_t *callback_list, *acb; 1573 int freeable = FALSE; 1574 1575 buf = zio->io_private; 1576 hdr = buf->b_hdr; 1577 1578 if (!HDR_FREED_IN_READ(hdr)) { 1579 arc_buf_hdr_t *found; 1580 1581 found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth, 1582 &hash_lock); 1583 1584 /* 1585 * Buffer was inserted into hash-table and removed from lists 1586 * prior to starting I/O. We should find this header, since 1587 * it's in the hash table, and it should be legit since it's 1588 * not possible to evict it during the I/O. 1589 */ 1590 1591 ASSERT(found); 1592 ASSERT(DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))); 1593 } 1594 1595 /* byteswap if necessary */ 1596 callback_list = hdr->b_acb; 1597 ASSERT(callback_list != NULL); 1598 if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap) 1599 callback_list->acb_byteswap(buf->b_data, hdr->b_size); 1600 1601 /* create copies of the data buffer for the callers */ 1602 abuf = buf; 1603 for (acb = callback_list; acb; acb = acb->acb_next) { 1604 if (acb->acb_done) { 1605 if (abuf == NULL) { 1606 abuf = kmem_cache_alloc(buf_cache, KM_SLEEP); 1607 abuf->b_data = arc_data_copy(hdr, buf->b_data); 1608 abuf->b_hdr = hdr; 1609 abuf->b_efunc = NULL; 1610 abuf->b_private = NULL; 1611 abuf->b_next = hdr->b_buf; 1612 hdr->b_buf = abuf; 1613 hdr->b_datacnt += 1; 1614 } 1615 acb->acb_buf = abuf; 1616 abuf = NULL; 1617 } else { 1618 /* 1619 * The caller did not provide a callback function. 1620 * In this case, we should just remove the reference. 1621 */ 1622 if (HDR_FREED_IN_READ(hdr)) { 1623 ASSERT3P(hdr->b_state, ==, arc.anon); 1624 (void) refcount_remove(&hdr->b_refcnt, 1625 acb->acb_private); 1626 } else { 1627 (void) remove_reference(hdr, hash_lock, 1628 acb->acb_private); 1629 } 1630 } 1631 } 1632 hdr->b_acb = NULL; 1633 hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 1634 ASSERT(!HDR_BUF_AVAILABLE(hdr)); 1635 if (abuf == buf) 1636 hdr->b_flags |= ARC_BUF_AVAILABLE; 1637 1638 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL); 1639 1640 if (zio->io_error != 0) { 1641 hdr->b_flags |= ARC_IO_ERROR; 1642 if (hdr->b_state != arc.anon) 1643 arc_change_state(arc.anon, hdr, hash_lock); 1644 if (HDR_IN_HASH_TABLE(hdr)) 1645 buf_hash_remove(hdr); 1646 freeable = refcount_is_zero(&hdr->b_refcnt); 1647 /* translate checksum errors into IO errors */ 1648 if (zio->io_error == ECKSUM) 1649 zio->io_error = EIO; 1650 } 1651 1652 /* 1653 * Broadcast before we drop the hash_lock. This is less efficient, 1654 * but avoids the possibility that the hdr (and hence the cv) might 1655 * be freed before we get to the cv_broadcast(). 1656 */ 1657 cv_broadcast(&hdr->b_cv); 1658 1659 if (!HDR_FREED_IN_READ(hdr)) { 1660 /* 1661 * Only call arc_access on anonymous buffers. This is because 1662 * if we've issued an I/O for an evicted buffer, we've already 1663 * called arc_access (to prevent any simultaneous readers from 1664 * getting confused). 1665 */ 1666 if (zio->io_error == 0 && hdr->b_state == arc.anon) 1667 arc_access_and_exit(hdr, hash_lock); 1668 else 1669 mutex_exit(hash_lock); 1670 } else { 1671 /* 1672 * This block was freed while we waited for the read to 1673 * complete. It has been removed from the hash table and 1674 * moved to the anonymous state (so that it won't show up 1675 * in the cache). 1676 */ 1677 ASSERT3P(hdr->b_state, ==, arc.anon); 1678 freeable = refcount_is_zero(&hdr->b_refcnt); 1679 } 1680 1681 /* execute each callback and free its structure */ 1682 while ((acb = callback_list) != NULL) { 1683 if (acb->acb_done) 1684 acb->acb_done(zio, acb->acb_buf, acb->acb_private); 1685 1686 if (acb->acb_zio_dummy != NULL) { 1687 acb->acb_zio_dummy->io_error = zio->io_error; 1688 zio_nowait(acb->acb_zio_dummy); 1689 } 1690 1691 callback_list = acb->acb_next; 1692 kmem_free(acb, sizeof (arc_callback_t)); 1693 } 1694 1695 if (freeable) 1696 arc_hdr_destroy(hdr); 1697 } 1698 1699 /* 1700 * "Read" the block block at the specified DVA (in bp) via the 1701 * cache. If the block is found in the cache, invoke the provided 1702 * callback immediately and return. Note that the `zio' parameter 1703 * in the callback will be NULL in this case, since no IO was 1704 * required. If the block is not in the cache pass the read request 1705 * on to the spa with a substitute callback function, so that the 1706 * requested block will be added to the cache. 1707 * 1708 * If a read request arrives for a block that has a read in-progress, 1709 * either wait for the in-progress read to complete (and return the 1710 * results); or, if this is a read with a "done" func, add a record 1711 * to the read to invoke the "done" func when the read completes, 1712 * and return; or just return. 1713 * 1714 * arc_read_done() will invoke all the requested "done" functions 1715 * for readers of this block. 1716 */ 1717 int 1718 arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap, 1719 arc_done_func_t *done, void *private, int priority, int flags, 1720 uint32_t arc_flags, zbookmark_t *zb) 1721 { 1722 arc_buf_hdr_t *hdr; 1723 arc_buf_t *buf; 1724 kmutex_t *hash_lock; 1725 zio_t *rzio; 1726 1727 top: 1728 hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 1729 if (hdr && hdr->b_datacnt > 0) { 1730 1731 if (HDR_IO_IN_PROGRESS(hdr)) { 1732 if ((arc_flags & ARC_NOWAIT) && done) { 1733 arc_callback_t *acb = NULL; 1734 1735 acb = kmem_zalloc(sizeof (arc_callback_t), 1736 KM_SLEEP); 1737 acb->acb_done = done; 1738 acb->acb_private = private; 1739 acb->acb_byteswap = swap; 1740 if (pio != NULL) 1741 acb->acb_zio_dummy = zio_null(pio, 1742 spa, NULL, NULL, flags); 1743 1744 ASSERT(acb->acb_done != NULL); 1745 acb->acb_next = hdr->b_acb; 1746 hdr->b_acb = acb; 1747 add_reference(hdr, hash_lock, private); 1748 mutex_exit(hash_lock); 1749 return (0); 1750 } else if (arc_flags & ARC_WAIT) { 1751 cv_wait(&hdr->b_cv, hash_lock); 1752 mutex_exit(hash_lock); 1753 goto top; 1754 } 1755 mutex_exit(hash_lock); 1756 return (0); 1757 } 1758 1759 ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu); 1760 1761 if (done) { 1762 /* 1763 * If this block is already in use, create a new 1764 * copy of the data so that we will be guaranteed 1765 * that arc_release() will always succeed. 1766 */ 1767 buf = hdr->b_buf; 1768 ASSERT(buf); 1769 ASSERT(buf->b_data); 1770 if (!HDR_BUF_AVAILABLE(hdr)) { 1771 void *data = arc_data_copy(hdr, buf->b_data); 1772 buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 1773 buf->b_hdr = hdr; 1774 buf->b_data = data; 1775 buf->b_efunc = NULL; 1776 buf->b_private = NULL; 1777 buf->b_next = hdr->b_buf; 1778 hdr->b_buf = buf; 1779 hdr->b_datacnt += 1; 1780 } else { 1781 ASSERT(buf->b_efunc == NULL); 1782 hdr->b_flags &= ~ARC_BUF_AVAILABLE; 1783 } 1784 add_reference(hdr, hash_lock, private); 1785 } 1786 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 1787 arc_access_and_exit(hdr, hash_lock); 1788 atomic_add_64(&arc.hits, 1); 1789 if (done) 1790 done(NULL, buf, private); 1791 } else { 1792 uint64_t size = BP_GET_LSIZE(bp); 1793 arc_callback_t *acb; 1794 1795 if (hdr == NULL) { 1796 /* this block is not in the cache */ 1797 arc_buf_hdr_t *exists; 1798 1799 buf = arc_buf_alloc(spa, size, private); 1800 hdr = buf->b_hdr; 1801 hdr->b_dva = *BP_IDENTITY(bp); 1802 hdr->b_birth = bp->blk_birth; 1803 hdr->b_cksum0 = bp->blk_cksum.zc_word[0]; 1804 exists = buf_hash_insert(hdr, &hash_lock); 1805 if (exists) { 1806 /* somebody beat us to the hash insert */ 1807 mutex_exit(hash_lock); 1808 bzero(&hdr->b_dva, sizeof (dva_t)); 1809 hdr->b_birth = 0; 1810 hdr->b_cksum0 = 0; 1811 (void) arc_buf_remove_ref(buf, private); 1812 goto top; /* restart the IO request */ 1813 } 1814 1815 } else { 1816 /* this block is in the ghost cache */ 1817 ASSERT(GHOST_STATE(hdr->b_state)); 1818 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1819 add_reference(hdr, hash_lock, private); 1820 ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 1821 1822 ASSERT(hdr->b_buf == NULL); 1823 buf = kmem_cache_alloc(buf_cache, KM_SLEEP); 1824 buf->b_hdr = hdr; 1825 buf->b_efunc = NULL; 1826 buf->b_private = NULL; 1827 buf->b_next = NULL; 1828 hdr->b_buf = buf; 1829 buf->b_data = zio_buf_alloc(hdr->b_size); 1830 atomic_add_64(&arc.size, hdr->b_size); 1831 ASSERT(hdr->b_datacnt == 0); 1832 hdr->b_datacnt = 1; 1833 } 1834 1835 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 1836 acb->acb_done = done; 1837 acb->acb_private = private; 1838 acb->acb_byteswap = swap; 1839 1840 ASSERT(hdr->b_acb == NULL); 1841 hdr->b_acb = acb; 1842 1843 /* 1844 * If this DVA is part of a prefetch, mark the buf 1845 * header with the prefetch flag 1846 */ 1847 if (arc_flags & ARC_PREFETCH) 1848 hdr->b_flags |= ARC_PREFETCH; 1849 hdr->b_flags |= ARC_IO_IN_PROGRESS; 1850 1851 /* 1852 * If the buffer has been evicted, migrate it to a present state 1853 * before issuing the I/O. Once we drop the hash-table lock, 1854 * the header will be marked as I/O in progress and have an 1855 * attached buffer. At this point, anybody who finds this 1856 * buffer ought to notice that it's legit but has a pending I/O. 1857 */ 1858 1859 if (GHOST_STATE(hdr->b_state)) 1860 arc_access_and_exit(hdr, hash_lock); 1861 else 1862 mutex_exit(hash_lock); 1863 1864 ASSERT3U(hdr->b_size, ==, size); 1865 DTRACE_PROBE2(arc__miss, blkptr_t *, bp, uint64_t, size); 1866 atomic_add_64(&arc.misses, 1); 1867 1868 rzio = zio_read(pio, spa, bp, buf->b_data, size, 1869 arc_read_done, buf, priority, flags, zb); 1870 1871 if (arc_flags & ARC_WAIT) 1872 return (zio_wait(rzio)); 1873 1874 ASSERT(arc_flags & ARC_NOWAIT); 1875 zio_nowait(rzio); 1876 } 1877 return (0); 1878 } 1879 1880 /* 1881 * arc_read() variant to support pool traversal. If the block is already 1882 * in the ARC, make a copy of it; otherwise, the caller will do the I/O. 1883 * The idea is that we don't want pool traversal filling up memory, but 1884 * if the ARC already has the data anyway, we shouldn't pay for the I/O. 1885 */ 1886 int 1887 arc_tryread(spa_t *spa, blkptr_t *bp, void *data) 1888 { 1889 arc_buf_hdr_t *hdr; 1890 kmutex_t *hash_mtx; 1891 int rc = 0; 1892 1893 hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx); 1894 1895 if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) { 1896 arc_buf_t *buf = hdr->b_buf; 1897 1898 ASSERT(buf); 1899 while (buf->b_data == NULL) { 1900 buf = buf->b_next; 1901 ASSERT(buf); 1902 } 1903 bcopy(buf->b_data, data, hdr->b_size); 1904 } else { 1905 rc = ENOENT; 1906 } 1907 1908 if (hash_mtx) 1909 mutex_exit(hash_mtx); 1910 1911 return (rc); 1912 } 1913 1914 void 1915 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private) 1916 { 1917 ASSERT(buf->b_hdr != NULL); 1918 ASSERT(buf->b_hdr->b_state != arc.anon); 1919 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL); 1920 buf->b_efunc = func; 1921 buf->b_private = private; 1922 } 1923 1924 /* 1925 * This is used by the DMU to let the ARC know that a buffer is 1926 * being evicted, so the ARC should clean up. If this arc buf 1927 * is not yet in the evicted state, it will be put there. 1928 */ 1929 int 1930 arc_buf_evict(arc_buf_t *buf) 1931 { 1932 arc_buf_hdr_t *hdr; 1933 kmutex_t *hash_lock; 1934 arc_buf_t **bufp; 1935 1936 mutex_enter(&arc_eviction_mtx); 1937 hdr = buf->b_hdr; 1938 if (hdr == NULL) { 1939 /* 1940 * We are in arc_do_user_evicts(). 1941 * NOTE: We can't be in arc_buf_add_ref() because 1942 * that would violate the interface rules. 1943 */ 1944 ASSERT(buf->b_data == NULL); 1945 mutex_exit(&arc_eviction_mtx); 1946 return (0); 1947 } else if (buf->b_data == NULL) { 1948 /* 1949 * We are on the eviction list, pull us off. 1950 */ 1951 bufp = &arc_eviction_list; 1952 while (*bufp != buf) 1953 bufp = &(*bufp)->b_next; 1954 *bufp = buf->b_next; 1955 mutex_exit(&arc_eviction_mtx); 1956 goto out; 1957 } else { 1958 /* 1959 * Prevent a race with arc_evict() 1960 */ 1961 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt); 1962 buf->b_hdr = NULL; 1963 } 1964 mutex_exit(&arc_eviction_mtx); 1965 1966 hash_lock = HDR_LOCK(hdr); 1967 mutex_enter(hash_lock); 1968 1969 ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu); 1970 1971 /* 1972 * Pull this buffer off of the hdr 1973 */ 1974 bufp = &hdr->b_buf; 1975 while (*bufp != buf) 1976 bufp = &(*bufp)->b_next; 1977 *bufp = buf->b_next; 1978 1979 ASSERT(buf->b_data != NULL); 1980 buf->b_hdr = hdr; 1981 arc_buf_destroy(buf, FALSE); 1982 1983 if (hdr->b_datacnt == 0) { 1984 arc_state_t *old_state = hdr->b_state; 1985 arc_state_t *evicted_state; 1986 1987 ASSERT(refcount_is_zero(&hdr->b_refcnt)); 1988 1989 evicted_state = 1990 (old_state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost; 1991 1992 mutex_enter(&old_state->mtx); 1993 mutex_enter(&evicted_state->mtx); 1994 1995 arc_change_state(evicted_state, hdr, hash_lock); 1996 ASSERT(HDR_IN_HASH_TABLE(hdr)); 1997 hdr->b_flags = ARC_IN_HASH_TABLE; 1998 1999 mutex_exit(&evicted_state->mtx); 2000 mutex_exit(&old_state->mtx); 2001 } 2002 mutex_exit(hash_lock); 2003 out: 2004 VERIFY(buf->b_efunc(buf) == 0); 2005 buf->b_efunc = NULL; 2006 buf->b_private = NULL; 2007 buf->b_hdr = NULL; 2008 kmem_cache_free(buf_cache, buf); 2009 return (1); 2010 } 2011 2012 /* 2013 * Release this buffer from the cache. This must be done 2014 * after a read and prior to modifying the buffer contents. 2015 * If the buffer has more than one reference, we must make 2016 * make a new hdr for the buffer. 2017 */ 2018 void 2019 arc_release(arc_buf_t *buf, void *tag) 2020 { 2021 arc_buf_hdr_t *hdr = buf->b_hdr; 2022 kmutex_t *hash_lock = HDR_LOCK(hdr); 2023 2024 /* this buffer is not on any list */ 2025 ASSERT(refcount_count(&hdr->b_refcnt) > 0); 2026 2027 if (hdr->b_state == arc.anon) { 2028 /* this buffer is already released */ 2029 ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1); 2030 ASSERT(BUF_EMPTY(hdr)); 2031 ASSERT(buf->b_efunc == NULL); 2032 return; 2033 } 2034 2035 mutex_enter(hash_lock); 2036 2037 /* 2038 * Do we have more than one buf? 2039 */ 2040 if (hdr->b_buf != buf || buf->b_next != NULL) { 2041 arc_buf_hdr_t *nhdr; 2042 arc_buf_t **bufp; 2043 uint64_t blksz = hdr->b_size; 2044 spa_t *spa = hdr->b_spa; 2045 2046 ASSERT(hdr->b_datacnt > 1); 2047 /* 2048 * Pull the data off of this buf and attach it to 2049 * a new anonymous buf. 2050 */ 2051 (void) remove_reference(hdr, hash_lock, tag); 2052 bufp = &hdr->b_buf; 2053 while (*bufp != buf) 2054 bufp = &(*bufp)->b_next; 2055 *bufp = (*bufp)->b_next; 2056 2057 ASSERT3U(hdr->b_state->size, >=, hdr->b_size); 2058 atomic_add_64(&hdr->b_state->size, -hdr->b_size); 2059 if (refcount_is_zero(&hdr->b_refcnt)) { 2060 ASSERT3U(hdr->b_state->lsize, >=, hdr->b_size); 2061 atomic_add_64(&hdr->b_state->lsize, -hdr->b_size); 2062 } 2063 hdr->b_datacnt -= 1; 2064 2065 mutex_exit(hash_lock); 2066 2067 nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP); 2068 nhdr->b_size = blksz; 2069 nhdr->b_spa = spa; 2070 nhdr->b_buf = buf; 2071 nhdr->b_state = arc.anon; 2072 nhdr->b_arc_access = 0; 2073 nhdr->b_flags = 0; 2074 nhdr->b_datacnt = 1; 2075 buf->b_hdr = nhdr; 2076 buf->b_next = NULL; 2077 (void) refcount_add(&nhdr->b_refcnt, tag); 2078 atomic_add_64(&arc.anon->size, blksz); 2079 2080 hdr = nhdr; 2081 } else { 2082 ASSERT(refcount_count(&hdr->b_refcnt) == 1); 2083 ASSERT(!list_link_active(&hdr->b_arc_node)); 2084 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 2085 arc_change_state(arc.anon, hdr, hash_lock); 2086 hdr->b_arc_access = 0; 2087 mutex_exit(hash_lock); 2088 bzero(&hdr->b_dva, sizeof (dva_t)); 2089 hdr->b_birth = 0; 2090 hdr->b_cksum0 = 0; 2091 } 2092 buf->b_efunc = NULL; 2093 buf->b_private = NULL; 2094 } 2095 2096 int 2097 arc_released(arc_buf_t *buf) 2098 { 2099 return (buf->b_data != NULL && buf->b_hdr->b_state == arc.anon); 2100 } 2101 2102 int 2103 arc_has_callback(arc_buf_t *buf) 2104 { 2105 return (buf->b_efunc != NULL); 2106 } 2107 2108 #ifdef ZFS_DEBUG 2109 int 2110 arc_referenced(arc_buf_t *buf) 2111 { 2112 return (refcount_count(&buf->b_hdr->b_refcnt)); 2113 } 2114 #endif 2115 2116 static void 2117 arc_write_done(zio_t *zio) 2118 { 2119 arc_buf_t *buf; 2120 arc_buf_hdr_t *hdr; 2121 arc_callback_t *acb; 2122 2123 buf = zio->io_private; 2124 hdr = buf->b_hdr; 2125 acb = hdr->b_acb; 2126 hdr->b_acb = NULL; 2127 ASSERT(acb != NULL); 2128 2129 /* this buffer is on no lists and is not in the hash table */ 2130 ASSERT3P(hdr->b_state, ==, arc.anon); 2131 2132 hdr->b_dva = *BP_IDENTITY(zio->io_bp); 2133 hdr->b_birth = zio->io_bp->blk_birth; 2134 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0]; 2135 /* 2136 * If the block to be written was all-zero, we may have 2137 * compressed it away. In this case no write was performed 2138 * so there will be no dva/birth-date/checksum. The buffer 2139 * must therefor remain anonymous (and uncached). 2140 */ 2141 if (!BUF_EMPTY(hdr)) { 2142 arc_buf_hdr_t *exists; 2143 kmutex_t *hash_lock; 2144 2145 exists = buf_hash_insert(hdr, &hash_lock); 2146 if (exists) { 2147 /* 2148 * This can only happen if we overwrite for 2149 * sync-to-convergence, because we remove 2150 * buffers from the hash table when we arc_free(). 2151 */ 2152 ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig), 2153 BP_IDENTITY(zio->io_bp))); 2154 ASSERT3U(zio->io_bp_orig.blk_birth, ==, 2155 zio->io_bp->blk_birth); 2156 2157 ASSERT(refcount_is_zero(&exists->b_refcnt)); 2158 arc_change_state(arc.anon, exists, hash_lock); 2159 mutex_exit(hash_lock); 2160 arc_hdr_destroy(exists); 2161 exists = buf_hash_insert(hdr, &hash_lock); 2162 ASSERT3P(exists, ==, NULL); 2163 } 2164 hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2165 arc_access_and_exit(hdr, hash_lock); 2166 } else if (acb->acb_done == NULL) { 2167 int destroy_hdr; 2168 /* 2169 * This is an anonymous buffer with no user callback, 2170 * destroy it if there are no active references. 2171 */ 2172 mutex_enter(&arc_eviction_mtx); 2173 destroy_hdr = refcount_is_zero(&hdr->b_refcnt); 2174 hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2175 mutex_exit(&arc_eviction_mtx); 2176 if (destroy_hdr) 2177 arc_hdr_destroy(hdr); 2178 } else { 2179 hdr->b_flags &= ~ARC_IO_IN_PROGRESS; 2180 } 2181 2182 if (acb->acb_done) { 2183 ASSERT(!refcount_is_zero(&hdr->b_refcnt)); 2184 acb->acb_done(zio, buf, acb->acb_private); 2185 } 2186 2187 kmem_free(acb, sizeof (arc_callback_t)); 2188 } 2189 2190 int 2191 arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, 2192 uint64_t txg, blkptr_t *bp, arc_buf_t *buf, 2193 arc_done_func_t *done, void *private, int priority, int flags, 2194 uint32_t arc_flags, zbookmark_t *zb) 2195 { 2196 arc_buf_hdr_t *hdr = buf->b_hdr; 2197 arc_callback_t *acb; 2198 zio_t *rzio; 2199 2200 /* this is a private buffer - no locking required */ 2201 ASSERT3P(hdr->b_state, ==, arc.anon); 2202 ASSERT(BUF_EMPTY(hdr)); 2203 ASSERT(!HDR_IO_ERROR(hdr)); 2204 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 2205 acb->acb_done = done; 2206 acb->acb_private = private; 2207 acb->acb_byteswap = (arc_byteswap_func_t *)-1; 2208 hdr->b_acb = acb; 2209 hdr->b_flags |= ARC_IO_IN_PROGRESS; 2210 rzio = zio_write(pio, spa, checksum, compress, txg, bp, 2211 buf->b_data, hdr->b_size, arc_write_done, buf, priority, flags, zb); 2212 2213 if (arc_flags & ARC_WAIT) 2214 return (zio_wait(rzio)); 2215 2216 ASSERT(arc_flags & ARC_NOWAIT); 2217 zio_nowait(rzio); 2218 2219 return (0); 2220 } 2221 2222 int 2223 arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 2224 zio_done_func_t *done, void *private, uint32_t arc_flags) 2225 { 2226 arc_buf_hdr_t *ab; 2227 kmutex_t *hash_lock; 2228 zio_t *zio; 2229 2230 /* 2231 * If this buffer is in the cache, release it, so it 2232 * can be re-used. 2233 */ 2234 ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock); 2235 if (ab != NULL) { 2236 /* 2237 * The checksum of blocks to free is not always 2238 * preserved (eg. on the deadlist). However, if it is 2239 * nonzero, it should match what we have in the cache. 2240 */ 2241 ASSERT(bp->blk_cksum.zc_word[0] == 0 || 2242 ab->b_cksum0 == bp->blk_cksum.zc_word[0]); 2243 arc_change_state(arc.anon, ab, hash_lock); 2244 if (refcount_is_zero(&ab->b_refcnt)) { 2245 mutex_exit(hash_lock); 2246 arc_hdr_destroy(ab); 2247 atomic_add_64(&arc.deleted, 1); 2248 } else { 2249 ASSERT3U(refcount_count(&ab->b_refcnt), ==, 1); 2250 ASSERT3U(ab->b_datacnt, ==, 1); 2251 if (HDR_IO_IN_PROGRESS(ab)) 2252 ab->b_flags |= ARC_FREED_IN_READ; 2253 if (HDR_IN_HASH_TABLE(ab)) 2254 buf_hash_remove(ab); 2255 ab->b_arc_access = 0; 2256 bzero(&ab->b_dva, sizeof (dva_t)); 2257 ab->b_birth = 0; 2258 ab->b_cksum0 = 0; 2259 ab->b_buf->b_efunc = NULL; 2260 ab->b_buf->b_private = NULL; 2261 mutex_exit(hash_lock); 2262 } 2263 } 2264 2265 zio = zio_free(pio, spa, txg, bp, done, private); 2266 2267 if (arc_flags & ARC_WAIT) 2268 return (zio_wait(zio)); 2269 2270 ASSERT(arc_flags & ARC_NOWAIT); 2271 zio_nowait(zio); 2272 2273 return (0); 2274 } 2275 2276 void 2277 arc_tempreserve_clear(uint64_t tempreserve) 2278 { 2279 atomic_add_64(&arc_tempreserve, -tempreserve); 2280 ASSERT((int64_t)arc_tempreserve >= 0); 2281 } 2282 2283 int 2284 arc_tempreserve_space(uint64_t tempreserve) 2285 { 2286 #ifdef ZFS_DEBUG 2287 /* 2288 * Once in a while, fail for no reason. Everything should cope. 2289 */ 2290 if (spa_get_random(10000) == 0) { 2291 dprintf("forcing random failure\n"); 2292 return (ERESTART); 2293 } 2294 #endif 2295 if (tempreserve > arc.c/4 && !arc.no_grow) 2296 arc.c = MIN(arc.c_max, tempreserve * 4); 2297 if (tempreserve > arc.c) 2298 return (ENOMEM); 2299 2300 /* 2301 * Throttle writes when the amount of dirty data in the cache 2302 * gets too large. We try to keep the cache less than half full 2303 * of dirty blocks so that our sync times don't grow too large. 2304 * Note: if two requests come in concurrently, we might let them 2305 * both succeed, when one of them should fail. Not a huge deal. 2306 * 2307 * XXX The limit should be adjusted dynamically to keep the time 2308 * to sync a dataset fixed (around 1-5 seconds?). 2309 */ 2310 2311 if (tempreserve + arc_tempreserve + arc.anon->size > arc.c / 2 && 2312 arc_tempreserve + arc.anon->size > arc.c / 4) { 2313 dprintf("failing, arc_tempreserve=%lluK anon=%lluK " 2314 "tempreserve=%lluK arc.c=%lluK\n", 2315 arc_tempreserve>>10, arc.anon->lsize>>10, 2316 tempreserve>>10, arc.c>>10); 2317 return (ERESTART); 2318 } 2319 atomic_add_64(&arc_tempreserve, tempreserve); 2320 return (0); 2321 } 2322 2323 void 2324 arc_init(void) 2325 { 2326 mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL); 2327 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL); 2328 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL); 2329 2330 /* Start out with 1/8 of all memory */ 2331 arc.c = physmem * PAGESIZE / 8; 2332 2333 #ifdef _KERNEL 2334 /* 2335 * On architectures where the physical memory can be larger 2336 * than the addressable space (intel in 32-bit mode), we may 2337 * need to limit the cache to 1/8 of VM size. 2338 */ 2339 arc.c = MIN(arc.c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8); 2340 #endif 2341 2342 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */ 2343 arc.c_min = MAX(arc.c / 4, 64<<20); 2344 /* set max to 3/4 of all memory, or all but 1GB, whichever is more */ 2345 if (arc.c * 8 >= 1<<30) 2346 arc.c_max = (arc.c * 8) - (1<<30); 2347 else 2348 arc.c_max = arc.c_min; 2349 arc.c_max = MAX(arc.c * 6, arc.c_max); 2350 arc.c = arc.c_max; 2351 arc.p = (arc.c >> 1); 2352 2353 /* if kmem_flags are set, lets try to use less memory */ 2354 if (kmem_debugging()) 2355 arc.c = arc.c / 2; 2356 if (arc.c < arc.c_min) 2357 arc.c = arc.c_min; 2358 2359 arc.anon = &ARC_anon; 2360 arc.mru = &ARC_mru; 2361 arc.mru_ghost = &ARC_mru_ghost; 2362 arc.mfu = &ARC_mfu; 2363 arc.mfu_ghost = &ARC_mfu_ghost; 2364 arc.size = 0; 2365 2366 list_create(&arc.mru->list, sizeof (arc_buf_hdr_t), 2367 offsetof(arc_buf_hdr_t, b_arc_node)); 2368 list_create(&arc.mru_ghost->list, sizeof (arc_buf_hdr_t), 2369 offsetof(arc_buf_hdr_t, b_arc_node)); 2370 list_create(&arc.mfu->list, sizeof (arc_buf_hdr_t), 2371 offsetof(arc_buf_hdr_t, b_arc_node)); 2372 list_create(&arc.mfu_ghost->list, sizeof (arc_buf_hdr_t), 2373 offsetof(arc_buf_hdr_t, b_arc_node)); 2374 2375 buf_init(); 2376 2377 arc_thread_exit = 0; 2378 arc_eviction_list = NULL; 2379 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL); 2380 2381 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0, 2382 TS_RUN, minclsyspri); 2383 } 2384 2385 void 2386 arc_fini(void) 2387 { 2388 mutex_enter(&arc_reclaim_thr_lock); 2389 arc_thread_exit = 1; 2390 while (arc_thread_exit != 0) 2391 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock); 2392 mutex_exit(&arc_reclaim_thr_lock); 2393 2394 arc_flush(); 2395 2396 arc_dead = TRUE; 2397 2398 mutex_destroy(&arc_eviction_mtx); 2399 mutex_destroy(&arc_reclaim_lock); 2400 mutex_destroy(&arc_reclaim_thr_lock); 2401 cv_destroy(&arc_reclaim_thr_cv); 2402 2403 list_destroy(&arc.mru->list); 2404 list_destroy(&arc.mru_ghost->list); 2405 list_destroy(&arc.mfu->list); 2406 list_destroy(&arc.mfu_ghost->list); 2407 2408 buf_fini(); 2409 } 2410