1794a9a6cSJohn Baldwin.\" Copyright (c) 2008 Yahoo!, Inc. 2794a9a6cSJohn Baldwin.\" All rights reserved. 3794a9a6cSJohn Baldwin.\" Written by: John Baldwin <jhb@FreeBSD.org> 4794a9a6cSJohn Baldwin.\" 5794a9a6cSJohn Baldwin.\" Redistribution and use in source and binary forms, with or without 6794a9a6cSJohn Baldwin.\" modification, are permitted provided that the following conditions 7794a9a6cSJohn Baldwin.\" are met: 8794a9a6cSJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright 9794a9a6cSJohn Baldwin.\" notice, this list of conditions and the following disclaimer. 10794a9a6cSJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright 11794a9a6cSJohn Baldwin.\" notice, this list of conditions and the following disclaimer in the 12794a9a6cSJohn Baldwin.\" documentation and/or other materials provided with the distribution. 13794a9a6cSJohn Baldwin.\" 3. Neither the name of the author nor the names of any co-contributors 14794a9a6cSJohn Baldwin.\" may be used to endorse or promote products derived from this software 15794a9a6cSJohn Baldwin.\" without specific prior written permission. 16794a9a6cSJohn Baldwin.\" 17794a9a6cSJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18794a9a6cSJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19794a9a6cSJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20794a9a6cSJohn Baldwin.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21794a9a6cSJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22794a9a6cSJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23794a9a6cSJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24794a9a6cSJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25794a9a6cSJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26794a9a6cSJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27794a9a6cSJohn Baldwin.\" SUCH DAMAGE. 28794a9a6cSJohn Baldwin.\" 29794a9a6cSJohn Baldwin.\" $FreeBSD$ 30794a9a6cSJohn Baldwin.\" 312a213404SGleb Smirnoff.Dd April 11, 2013 32794a9a6cSJohn Baldwin.Dt KVM_GETPCPU 3 33794a9a6cSJohn Baldwin.Os 34794a9a6cSJohn Baldwin.Sh NAME 35ccd8bad0SRobert Watson.Nm kvm_dpcpu_setcpu 36794a9a6cSJohn Baldwin.Nm kvm_getmaxcpu , 37794a9a6cSJohn Baldwin.Nm kvm_getpcpu 38794a9a6cSJohn Baldwin.Nd access per-CPU data 39794a9a6cSJohn Baldwin.Sh LIBRARY 40794a9a6cSJohn Baldwin.Lb libkvm 41794a9a6cSJohn Baldwin.Sh SYNOPSIS 42794a9a6cSJohn Baldwin.In sys/param.h 43794a9a6cSJohn Baldwin.In sys/pcpu.h 44794a9a6cSJohn Baldwin.In sys/sysctl.h 45794a9a6cSJohn Baldwin.In kvm.h 46794a9a6cSJohn Baldwin.Ft int 47ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu "kvm_t *kd" "u_int cpu" 48ccd8bad0SRobert Watson.Ft int 49794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu "kvm_t *kd" 50794a9a6cSJohn Baldwin.Ft void * 51794a9a6cSJohn Baldwin.Fn kvm_getpcpu "kvm_t *kd" "int cpu" 522a213404SGleb Smirnoff.Ft ssize_t 53*19b5cffeSGleb Smirnoff.Fn kvm_read_zpcpu "kvm_t *kd" "u_long base" "void *buf" "size_t size" "int cpu" 542a213404SGleb Smirnoff.Ft uint64_t 552a213404SGleb Smirnoff.Fn kvm_counter_u64_fetch "kvm_t *kd" "u_long base" 56794a9a6cSJohn Baldwin.Sh DESCRIPTION 57794a9a6cSJohn BaldwinThe 58ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu , 59ccd8bad0SRobert Watson.Fn kvm_getmaxcpu , 60794a9a6cSJohn Baldwinand 61794a9a6cSJohn Baldwin.Fn kvm_getpcpu 62794a9a6cSJohn Baldwinfunctions are used to access the per-CPU data of active processors in the 63794a9a6cSJohn Baldwinkernel indicated by 64794a9a6cSJohn Baldwin.Fa kd . 65ccd8bad0SRobert WatsonPer-CPU storage comes in two flavours: data stored directly in a 66ccd8bad0SRobert Watson.Vt "struct pcpu" 67ccd8bad0SRobert Watsonassociated with each CPU, and dynamic per-CPU storage (DPCPU), in which a 68ccd8bad0SRobert Watsonsingle kernel symbol refers to different data depending on what CPU it is 69ccd8bad0SRobert Watsonaccessed from. 70ccd8bad0SRobert Watson.Pp 71794a9a6cSJohn BaldwinThe 72794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu 73794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel. 74ccd8bad0SRobert Watson.Pp 75794a9a6cSJohn BaldwinThe 76794a9a6cSJohn Baldwin.Fn kvm_getpcpu 77794a9a6cSJohn Baldwinfunction returns a buffer holding the per-CPU data for a single CPU. 78794a9a6cSJohn BaldwinThis buffer is described by the 79794a9a6cSJohn Baldwin.Vt "struct pcpu" 80794a9a6cSJohn Baldwintype. 81794a9a6cSJohn BaldwinThe caller is responsible for releasing the buffer via a call to 82794a9a6cSJohn Baldwin.Xr free 3 83794a9a6cSJohn Baldwinwhen it is no longer needed. 84794a9a6cSJohn BaldwinIf 85794a9a6cSJohn Baldwin.Fa cpu 86794a9a6cSJohn Baldwinis not active, then 87794a9a6cSJohn Baldwin.Dv NULL 88794a9a6cSJohn Baldwinis returned instead. 89ccd8bad0SRobert Watson.Pp 902a213404SGleb SmirnoffThe 912a213404SGleb Smirnoff.Fn kvm_read_zpcpu 922a213404SGleb Smirnofffunction is used to obtain private per-CPU copy from a 932a213404SGleb Smirnoff.Dv UMA_ZONE_PCPU 942a213404SGleb Smirnoff.Xr zone 9 . 952a213404SGleb SmirnoffIt takes 962a213404SGleb Smirnoff.Fa base 972a213404SGleb Smirnoffargument as base address of an allocation and copyies 982a213404SGleb Smirnoff.Fa size 992a213404SGleb Smirnoffbytes into 1002a213404SGleb Smirnoff.Fa buf 1012a213404SGleb Smirnofffrom the part of allocation that is private to 1022a213404SGleb Smirnoff.Fa cpu . 1032a213404SGleb Smirnoff.Pp 1042a213404SGleb SmirnoffThe 1052a213404SGleb Smirnoff.Fn kvm_counter_u64_fetch 1062a213404SGleb Smirnofffunction fetches value of a 1072a213404SGleb Smirnoff.Xr counter 9 1082a213404SGleb Smirnoffpointed by 1092a213404SGleb Smirnoff.Fa base 1102a213404SGleb Smirnoffaddress. 1112a213404SGleb Smirnoff.Pp 112ccd8bad0SRobert WatsonSymbols for dynamic per-CPU data are accessed via 113ccd8bad0SRobert Watson.Xr kvm_nlist 3 114ccd8bad0SRobert Watsonas with other symbols. 115ccd8bad0SRobert Watson.Nm libkvm 116ccd8bad0SRobert Watsonmaintains a notion of the "current CPU", set by 117ccd8bad0SRobert Watson.Xr kvm_dpcpu_setcpu , 118ccd8bad0SRobert Watsonwhich defaults to 0. 119ccd8bad0SRobert WatsonOnce another CPU is selected, 120ccd8bad0SRobert Watson.Xr kvm_nlist 3 121ccd8bad0SRobert Watsonwill return pointers to that data on the appropriate CPU. 122cd11f01eSJohn Baldwin.Sh CACHING 123ccd8bad0SRobert Watson.Fn kvm_getmaxcpu 124ccd8bad0SRobert Watsonand 12563d46d1dSUlrich Spörlein.Fn kvm_getpcpu 126ccd8bad0SRobert Watsoncache the nlist values for various kernel variables which are 127cd11f01eSJohn Baldwinreused in successive calls. 128cd11f01eSJohn BaldwinYou may call either function with 129cd11f01eSJohn Baldwin.Fa kd 130cd11f01eSJohn Baldwinset to 131cd11f01eSJohn Baldwin.Dv NULL 132cd11f01eSJohn Baldwinto clear this cache. 133794a9a6cSJohn Baldwin.Sh RETURN VALUES 134794a9a6cSJohn BaldwinOn success, the 135794a9a6cSJohn Baldwin.Fn kvm_getmaxcpu 136794a9a6cSJohn Baldwinfunction returns the maximum number of CPUs supported by the kernel. 137794a9a6cSJohn BaldwinIf an error occurs, 138794a9a6cSJohn Baldwinit returns -1 instead. 139794a9a6cSJohn Baldwin.Pp 140794a9a6cSJohn BaldwinOn success, the 141794a9a6cSJohn Baldwin.Fn kvm_getpcpu 142794a9a6cSJohn Baldwinfunction returns a pointer to an allocated buffer or 143794a9a6cSJohn Baldwin.Dv NULL . 144794a9a6cSJohn BaldwinIf an error occurs, 145794a9a6cSJohn Baldwinit returns -1 instead. 146794a9a6cSJohn Baldwin.Pp 147ccd8bad0SRobert WatsonOn success, the 148ccd8bad0SRobert Watson.Fn kvm_dpcpu_setcpu 149ccd8bad0SRobert Watsoncall returns 0; if an error occurs, it returns -1 instead. 150ccd8bad0SRobert Watson.Pp 1512a213404SGleb SmirnoffOn success, the 1522a213404SGleb Smirnoff.Fn kvm_read_zpcpu 1532a213404SGleb Smirnofffunction returns number of bytes copied. 1542a213404SGleb SmirnoffIf an error occurs, it returns -1 instead. 1552a213404SGleb Smirnoff.Pp 156ccd8bad0SRobert WatsonIf any function encounters an error, 157794a9a6cSJohn Baldwinthen an error message may be retrieved via 158794a9a6cSJohn Baldwin.Xr kvm_geterr 3 . 159794a9a6cSJohn Baldwin.Sh SEE ALSO 160794a9a6cSJohn Baldwin.Xr free 3 , 1612a213404SGleb Smirnoff.Xr kvm 3 , 1622a213404SGleb Smirnoff.Xr counter 9 , 1632a213404SGleb Smirnoff.Xr zone 9 164