Path: blob/main/sys/contrib/openzfs/module/zfs/aggsum.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/15/*16* Copyright (c) 2017, 2018 by Delphix. All rights reserved.17*/1819#include <sys/zfs_context.h>20#include <sys/aggsum.h>2122/*23* Aggregate-sum counters are a form of fanned-out counter, used when atomic24* instructions on a single field cause enough CPU cache line contention to25* slow system performance. Due to their increased overhead and the expense26* involved with precisely reading from them, they should only be used in cases27* where the write rate (increment/decrement) is much higher than the read rate28* (get value).29*30* Aggregate sum counters are comprised of two basic parts, the core and the31* buckets. The core counter contains a lock for the entire counter, as well32* as the current upper and lower bounds on the value of the counter. The33* aggsum_bucket structure contains a per-bucket lock to protect the contents of34* the bucket, the current amount that this bucket has changed from the global35* counter (called the delta), and the amount of increment and decrement we have36* "borrowed" from the core counter.37*38* The basic operation of an aggsum is simple. Threads that wish to modify the39* counter will modify one bucket's counter (determined by their current CPU, to40* help minimize lock and cache contention). If the bucket already has41* sufficient capacity borrowed from the core structure to handle their request,42* they simply modify the delta and return. If the bucket does not, we clear43* the bucket's current state (to prevent the borrowed amounts from getting too44* large), and borrow more from the core counter. Borrowing is done by adding to45* the upper bound (or subtracting from the lower bound) of the core counter,46* and setting the borrow value for the bucket to the amount added (or47* subtracted). Clearing the bucket is the opposite; we add the current delta48* to both the lower and upper bounds of the core counter, subtract the borrowed49* incremental from the upper bound, and add the borrowed decrement from the50* lower bound. Note that only borrowing and clearing require access to the51* core counter; since all other operations access CPU-local resources,52* performance can be much higher than a traditional counter.53*54* Threads that wish to read from the counter have a slightly more challenging55* task. It is fast to determine the upper and lower bounds of the aggum; this56* does not require grabbing any locks. This suffices for cases where an57* approximation of the aggsum's value is acceptable. However, if one needs to58* know whether some specific value is above or below the current value in the59* aggsum, they invoke aggsum_compare(). This function operates by repeatedly60* comparing the target value to the upper and lower bounds of the aggsum, and61* then clearing a bucket. This proceeds until the target is outside of the62* upper and lower bounds and we return a response, or the last bucket has been63* cleared and we know that the target is equal to the aggsum's value. Finally,64* the most expensive operation is determining the precise value of the aggsum.65* To do this, we clear every bucket and then return the upper bound (which must66* be equal to the lower bound). What makes aggsum_compare() and aggsum_value()67* expensive is clearing buckets. This involves grabbing the global lock68* (serializing against themselves and borrow operations), grabbing a bucket's69* lock (preventing threads on those CPUs from modifying their delta), and70* zeroing out the borrowed value (forcing that thread to borrow on its next71* request, which will also be expensive). This is what makes aggsums well72* suited for write-many read-rarely operations.73*74* Note that the aggsums do not expand if more CPUs are hot-added. In that75* case, we will have less fanout than boot_ncpus, but we don't want to always76* reserve the RAM necessary to create the extra slots for additional CPUs up77* front, and dynamically adding them is a complex task.78*/7980/*81* We will borrow 2^aggsum_borrow_shift times the current request, so we will82* have to get the as_lock approximately every 2^aggsum_borrow_shift calls to83* aggsum_add().84*/85static uint_t aggsum_borrow_shift = 4;8687void88aggsum_init(aggsum_t *as, uint64_t value)89{90memset(as, 0, sizeof (*as));91as->as_lower_bound = as->as_upper_bound = value;92mutex_init(&as->as_lock, NULL, MUTEX_DEFAULT, NULL);93/*94* Too many buckets may hurt read performance without improving95* write. From 12 CPUs use bucket per 2 CPUs, from 48 per 4, etc.96*/97as->as_bucketshift = highbit64(boot_ncpus / 6) / 2;98as->as_numbuckets = ((boot_ncpus - 1) >> as->as_bucketshift) + 1;99as->as_buckets = kmem_zalloc(as->as_numbuckets *100sizeof (aggsum_bucket_t), KM_SLEEP);101for (int i = 0; i < as->as_numbuckets; i++) {102mutex_init(&as->as_buckets[i].asc_lock,103NULL, MUTEX_DEFAULT, NULL);104}105}106107void108aggsum_fini(aggsum_t *as)109{110for (int i = 0; i < as->as_numbuckets; i++)111mutex_destroy(&as->as_buckets[i].asc_lock);112kmem_free(as->as_buckets, as->as_numbuckets * sizeof (aggsum_bucket_t));113mutex_destroy(&as->as_lock);114}115116int64_t117aggsum_lower_bound(aggsum_t *as)118{119return (atomic_load_64((volatile uint64_t *)&as->as_lower_bound));120}121122uint64_t123aggsum_upper_bound(aggsum_t *as)124{125return (atomic_load_64(&as->as_upper_bound));126}127128uint64_t129aggsum_value(aggsum_t *as)130{131int64_t lb;132uint64_t ub;133134mutex_enter(&as->as_lock);135lb = as->as_lower_bound;136ub = as->as_upper_bound;137if (lb == ub) {138for (int i = 0; i < as->as_numbuckets; i++) {139ASSERT0(as->as_buckets[i].asc_delta);140ASSERT0(as->as_buckets[i].asc_borrowed);141}142mutex_exit(&as->as_lock);143return (lb);144}145for (int i = 0; i < as->as_numbuckets; i++) {146struct aggsum_bucket *asb = &as->as_buckets[i];147if (asb->asc_borrowed == 0)148continue;149mutex_enter(&asb->asc_lock);150lb += asb->asc_delta + asb->asc_borrowed;151ub += asb->asc_delta - asb->asc_borrowed;152asb->asc_delta = 0;153asb->asc_borrowed = 0;154mutex_exit(&asb->asc_lock);155}156ASSERT3U(lb, ==, ub);157atomic_store_64((volatile uint64_t *)&as->as_lower_bound, lb);158atomic_store_64(&as->as_upper_bound, lb);159mutex_exit(&as->as_lock);160161return (lb);162}163164void165aggsum_add(aggsum_t *as, int64_t delta)166{167struct aggsum_bucket *asb;168int64_t borrow;169170asb = &as->as_buckets[(CPU_SEQID_UNSTABLE >> as->as_bucketshift) %171as->as_numbuckets];172173/* Try fast path if we already borrowed enough before. */174mutex_enter(&asb->asc_lock);175if (asb->asc_delta + delta <= (int64_t)asb->asc_borrowed &&176asb->asc_delta + delta >= -(int64_t)asb->asc_borrowed) {177asb->asc_delta += delta;178mutex_exit(&asb->asc_lock);179return;180}181mutex_exit(&asb->asc_lock);182183/*184* We haven't borrowed enough. Take the global lock and borrow185* considering what is requested now and what we borrowed before.186*/187borrow = (delta < 0 ? -delta : delta);188borrow <<= aggsum_borrow_shift + as->as_bucketshift;189mutex_enter(&as->as_lock);190if (borrow >= asb->asc_borrowed)191borrow -= asb->asc_borrowed;192else193borrow = (borrow - (int64_t)asb->asc_borrowed) / 4;194mutex_enter(&asb->asc_lock);195delta += asb->asc_delta;196asb->asc_delta = 0;197asb->asc_borrowed += borrow;198mutex_exit(&asb->asc_lock);199atomic_store_64((volatile uint64_t *)&as->as_lower_bound,200as->as_lower_bound + delta - borrow);201atomic_store_64(&as->as_upper_bound,202as->as_upper_bound + delta + borrow);203mutex_exit(&as->as_lock);204}205206/*207* Compare the aggsum value to target efficiently. Returns -1 if the value208* represented by the aggsum is less than target, 1 if it's greater, and 0 if209* they are equal.210*/211int212aggsum_compare(aggsum_t *as, uint64_t target)213{214int64_t lb;215uint64_t ub;216int i;217218if (atomic_load_64(&as->as_upper_bound) < target)219return (-1);220lb = atomic_load_64((volatile uint64_t *)&as->as_lower_bound);221if (lb > 0 && (uint64_t)lb > target)222return (1);223mutex_enter(&as->as_lock);224lb = as->as_lower_bound;225ub = as->as_upper_bound;226for (i = 0; i < as->as_numbuckets; i++) {227struct aggsum_bucket *asb = &as->as_buckets[i];228if (asb->asc_borrowed == 0)229continue;230mutex_enter(&asb->asc_lock);231lb += asb->asc_delta + asb->asc_borrowed;232ub += asb->asc_delta - asb->asc_borrowed;233asb->asc_delta = 0;234asb->asc_borrowed = 0;235mutex_exit(&asb->asc_lock);236if (ub < target || (lb > 0 && (uint64_t)lb > target))237break;238}239if (i >= as->as_numbuckets)240ASSERT3U(lb, ==, ub);241atomic_store_64((volatile uint64_t *)&as->as_lower_bound, lb);242atomic_store_64(&as->as_upper_bound, ub);243mutex_exit(&as->as_lock);244return (ub < target ? -1 : (uint64_t)lb > target ? 1 : 0);245}246247248