From owner-freebsd-hackers@freebsd.org Wed Mar 8 22:54:28 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B1ABAD0324C for ; Wed, 8 Mar 2017 22:54:28 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from mail.baldwin.cx (bigwig.baldwin.cx [96.47.65.170]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 8C5A51AE8 for ; Wed, 8 Mar 2017 22:54:28 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from ralph.baldwin.cx (c-73-231-226-104.hsd1.ca.comcast.net [73.231.226.104]) by mail.baldwin.cx (Postfix) with ESMTPSA id E758C10A7FA; Wed, 8 Mar 2017 17:54:21 -0500 (EST) From: John Baldwin To: freebsd-hackers@freebsd.org Cc: Karl Denninger Subject: Re: Kernel UMA current occupancy statistics Date: Wed, 08 Mar 2017 14:27:15 -0800 Message-ID: <2800890.fCNZHq2Y8P@ralph.baldwin.cx> User-Agent: KMail/4.14.10 (FreeBSD/11.0-STABLE; KDE/4.14.10; amd64; ; ) In-Reply-To: <18fdca3b-9002-db60-504b-388c628fab0e@denninger.net> References: <18fdca3b-9002-db60-504b-388c628fab0e@denninger.net> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.4.3 (mail.baldwin.cx); Wed, 08 Mar 2017 17:54:22 -0500 (EST) X-Virus-Scanned: clamav-milter 0.99.2 at mail.baldwin.cx X-Virus-Status: Clean X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Mar 2017 22:54:28 -0000 On Wednesday, August 31, 2016 08:02:06 AM Karl Denninger wrote: > Working on the ZFS ARC code I'm trying to find documentation on the > means by which I can determine the occupancy of a UMA slab. > > There is a userland set of calls but I assume those are not the correct > way to approach this in the kernel context. include/sys/vm/uma.h > declares that the returned structure is to be opaque to users of the > facility, and the only occupancy-related function I can find is > uma_zone_get_cur, which gives me the number of items allocated but > uma_zone_get_max states that it will return "0" if no limit on > allocations has been set. > > Any hints on how to determine, if for example there are 50,000 "units" > of memory that are currently held out of kmem in a given slab how many > are actually allocated and how many are free and reusable without a > further kernel memory allocation? > > What I'm trying to determine is this (from vmstat -z): > > ITEM SIZE LIMIT USED FREE REQ FAIL SLEEP > zio_buf_512: 512, 0, 79865, 199359, 6495950, 0, 0 > > In other words how do I programmatically, inside a kernel routine (in > this case zfs/arc.c) retrieve the "used" and "free" values if I have a > given slab's pointer (which I can use to call kmem_cache_reap_now)? > > Thanks in advance; vmstat -z retrieves these using memstat_sysctl_uma() from libmemstat which in turn uses the vm.zone_stats sysctl. Looking in that function, it seems that the used field (mt_count) is computed this way: mtp->mt_count = mtp->mt_numallocs - mtp->mt_numfrees; Where numallocs and numfrees are computed by summing a set of per-CPU stats: for (j = 0; j < maxcpus; j++) { mtp->mt_numallocs += upsp->ups_allocs; mtp->mt_numfrees += upsp->ups_frees; } The free field (mt_free) is calculated similarly. First with a global count followed by per-CPU counts: mtp->mt_numfrees = uthp->uth_frees; for (j = 0; j < maxcpus; j++) { mtp->mt_free += upsp->ups_cache_free; } The vm.zone_stats sysctl is implemented by sysctl_vm_zone_stats() in sys/vm/uma_core.c. It seems like 'uma_zone_get_cur()' might give you USED. You would need to add a new function to let you calculate FREE. You can probably use 'uma_zone_get_cur()' as a template for implementing that other function. uma_zone_sumstat() might also be useful as a reference. -- John Baldwin