Thu Apr 1 06:25:46 2021 UTC ()
Add a sysctl hashstat collector for uihash.


(simonb)
diff -r1.11 -r1.12 src/sys/kern/kern_uidinfo.c

cvs diff -r1.11 -r1.12 src/sys/kern/kern_uidinfo.c (expand / switch to unified diff)

--- src/sys/kern/kern_uidinfo.c 2019/03/01 03:03:19 1.11
+++ src/sys/kern/kern_uidinfo.c 2021/04/01 06:25:45 1.12
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_uidinfo.c,v 1.11 2019/03/01 03:03:19 christos Exp $ */ 1/* $NetBSD: kern_uidinfo.c,v 1.12 2021/04/01 06:25:45 simonb Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1982, 1986, 1991, 1993 4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc. 6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed 7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph 8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc. 10 * the permission of UNIX System Laboratories, Inc.
11 * 11 *
12 * Redistribution and use in source and binary forms, with or without 12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions 13 * modification, are permitted provided that the following conditions
14 * are met: 14 * are met:
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE. 34 * SUCH DAMAGE.
35 */ 35 */
36 36
37#include <sys/cdefs.h> 37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.11 2019/03/01 03:03:19 christos Exp $"); 38__KERNEL_RCSID(0, "$NetBSD: kern_uidinfo.c,v 1.12 2021/04/01 06:25:45 simonb Exp $");
39 39
40#include <sys/param.h> 40#include <sys/param.h>
41#include <sys/systm.h> 41#include <sys/systm.h>
42#include <sys/kmem.h> 42#include <sys/kmem.h>
43#include <sys/proc.h> 43#include <sys/proc.h>
44#include <sys/atomic.h> 44#include <sys/atomic.h>
45#include <sys/uidinfo.h> 45#include <sys/uidinfo.h>
46#include <sys/sysctl.h> 46#include <sys/sysctl.h>
47#include <sys/kauth.h> 47#include <sys/kauth.h>
48#include <sys/cpu.h> 48#include <sys/cpu.h>
49 49
50static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl; 50static SLIST_HEAD(uihashhead, uidinfo) *uihashtbl;
51static u_long uihash; 51static u_long uihash;
@@ -122,45 +122,77 @@ sysctl_kern_uidinfo_setup(void) @@ -122,45 +122,77 @@ sysctl_kern_uidinfo_setup(void)
122 CTLFLAG_PERMANENT, 122 CTLFLAG_PERMANENT,
123 CTLTYPE_QUAD, "semcnt", 123 CTLTYPE_QUAD, "semcnt",
124 SYSCTL_DESCR("Number of semaphores used for the current user"), 124 SYSCTL_DESCR("Number of semaphores used for the current user"),
125 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 125 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
126 CTL_CREATE, CTL_EOL); 126 CTL_CREATE, CTL_EOL);
127 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode, 127 sysctl_createv(&kern_uidinfo_sysctllog, 0, &rnode, &cnode,
128 CTLFLAG_PERMANENT, 128 CTLFLAG_PERMANENT,
129 CTLTYPE_QUAD, "sbsize", 129 CTLTYPE_QUAD, "sbsize",
130 SYSCTL_DESCR("Socket buffers used for the current user"), 130 SYSCTL_DESCR("Socket buffers used for the current user"),
131 sysctl_kern_uidinfo_cnt, 0, NULL, 0, 131 sysctl_kern_uidinfo_cnt, 0, NULL, 0,
132 CTL_CREATE, CTL_EOL); 132 CTL_CREATE, CTL_EOL);
133} 133}
134 134
 135static int
 136uid_stats(struct hashstat_sysctl *hs, bool fill)
 137{
 138 struct uidinfo *uip;
 139 uint64_t chain;
 140
 141 strlcpy(hs->hash_name, "uihash", sizeof(hs->hash_name));
 142 strlcpy(hs->hash_desc, "user info (uid->used proc) hash",
 143 sizeof(hs->hash_desc));
 144 if (!fill)
 145 return 0;
 146
 147 hs->hash_size = uihash + 1;
 148
 149 for (size_t i = 0; i < hs->hash_size; i++) {
 150 chain = 0;
 151 SLIST_FOREACH(uip, &uihashtbl[i], ui_hash) {
 152 membar_datadep_consumer();
 153 chain++;
 154 }
 155 if (chain > 0) {
 156 hs->hash_used++;
 157 hs->hash_items += chain;
 158 if (chain > hs->hash_maxchain)
 159 hs->hash_maxchain = chain;
 160 }
 161 }
 162
 163 return 0;
 164}
 165
135void 166void
136uid_init(void) 167uid_init(void)
137{ 168{
138 169
139 /* 170 /*
140 * In case of MP system, SLIST_FOREACH would force a cache line 171 * In case of MP system, SLIST_FOREACH would force a cache line
141 * write-back for every modified 'uidinfo', thus we try to keep the 172 * write-back for every modified 'uidinfo', thus we try to keep the
142 * lists short. 173 * lists short.
143 */ 174 */
144 const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64); 175 const u_int uihash_sz = (maxcpus > 1 ? 1024 : 64);
145 176
146 uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash); 177 uihashtbl = hashinit(uihash_sz, HASH_SLIST, true, &uihash);
147 178
148 /* 179 /*
149 * Ensure that uid 0 is always in the user hash table, as 180 * Ensure that uid 0 is always in the user hash table, as
150 * sbreserve() expects it available from interrupt context. 181 * sbreserve() expects it available from interrupt context.
151 */ 182 */
152 (void)uid_find(0); 183 (void)uid_find(0);
153 sysctl_kern_uidinfo_setup(); 184 sysctl_kern_uidinfo_setup();
 185 hashstat_register("uihash", uid_stats);
154} 186}
155 187
156struct uidinfo * 188struct uidinfo *
157uid_find(uid_t uid) 189uid_find(uid_t uid)
158{ 190{
159 struct uidinfo *uip, *uip_first, *newuip; 191 struct uidinfo *uip, *uip_first, *newuip;
160 struct uihashhead *uipp; 192 struct uihashhead *uipp;
161 193
162 uipp = UIHASH(uid); 194 uipp = UIHASH(uid);
163 newuip = NULL; 195 newuip = NULL;
164 196
165 /* 197 /*
166 * To make insertion atomic, abstraction of SLIST will be violated. 198 * To make insertion atomic, abstraction of SLIST will be violated.