xref: /illumos-gate/usr/src/lib/liblgrp/common/lgrp.c (revision 1da57d55)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * lgroup interface
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate #include <errno.h>
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <strings.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/pset.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <sys/lgrp_user.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Fast trap for getting home lgroup of current thread
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate extern lgrp_id_t	_lgrp_home_fast(void);
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * lgroup system call
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate extern int		_lgrpsys(int subcode, long arg, void *ap);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static int lgrp_cpus_hier(lgrp_snapshot_header_t *snap, lgrp_id_t lgrp,
53*7c478bd9Sstevel@tonic-gate     processorid_t **cpuids, uint_t *count);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * Get generation ID of lgroup hierarchy given view
58*7c478bd9Sstevel@tonic-gate  * which changes whenever the hierarchy changes (eg. DR or pset contents
59*7c478bd9Sstevel@tonic-gate  * change for caller's view)
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate static lgrp_gen_t
lgrp_generation(lgrp_view_t view)62*7c478bd9Sstevel@tonic-gate lgrp_generation(lgrp_view_t view)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	return (_lgrpsys(LGRP_SYS_GENERATION, view, NULL));
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Get supported revision number of lgroup interface
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate int
lgrp_version(int version)72*7c478bd9Sstevel@tonic-gate lgrp_version(int version)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	return (_lgrpsys(LGRP_SYS_VERSION, version, NULL));
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * Get affinity for given lgroup
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate lgrp_affinity_t
lgrp_affinity_get(idtype_t idtype,id_t id,lgrp_id_t lgrp)82*7c478bd9Sstevel@tonic-gate lgrp_affinity_get(idtype_t idtype, id_t id, lgrp_id_t lgrp)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	lgrp_affinity_args_t	args;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	args.idtype = idtype;
87*7c478bd9Sstevel@tonic-gate 	args.id = id;
88*7c478bd9Sstevel@tonic-gate 	args.lgrp = lgrp;
89*7c478bd9Sstevel@tonic-gate 	return (_lgrpsys(LGRP_SYS_AFFINITY_GET, 0, (void *)&args));
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  * Set affinity for given lgroup
95*7c478bd9Sstevel@tonic-gate  */
96*7c478bd9Sstevel@tonic-gate int
lgrp_affinity_set(idtype_t idtype,id_t id,lgrp_id_t lgrp,lgrp_affinity_t aff)97*7c478bd9Sstevel@tonic-gate lgrp_affinity_set(idtype_t idtype, id_t id, lgrp_id_t lgrp,
98*7c478bd9Sstevel@tonic-gate     lgrp_affinity_t aff)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	lgrp_affinity_args_t	args;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	args.idtype = idtype;
103*7c478bd9Sstevel@tonic-gate 	args.id = id;
104*7c478bd9Sstevel@tonic-gate 	args.lgrp = lgrp;
105*7c478bd9Sstevel@tonic-gate 	args.aff = aff;
106*7c478bd9Sstevel@tonic-gate 	return (_lgrpsys(LGRP_SYS_AFFINITY_SET, 0, (void *)&args));
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /*
111*7c478bd9Sstevel@tonic-gate  * Get home lgroup for given process or thread
112*7c478bd9Sstevel@tonic-gate  */
113*7c478bd9Sstevel@tonic-gate lgrp_id_t
lgrp_home(idtype_t idtype,id_t id)114*7c478bd9Sstevel@tonic-gate lgrp_home(idtype_t idtype, id_t id)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	/*
117*7c478bd9Sstevel@tonic-gate 	 * Use fast trap to get home lgroup of current thread or process
118*7c478bd9Sstevel@tonic-gate 	 * Otherwise, use system call for other process or thread
119*7c478bd9Sstevel@tonic-gate 	 */
120*7c478bd9Sstevel@tonic-gate 	if (id == P_MYID && (idtype == P_LWPID || idtype == P_PID))
121*7c478bd9Sstevel@tonic-gate 		return (_lgrp_home_fast());
122*7c478bd9Sstevel@tonic-gate 	else
123*7c478bd9Sstevel@tonic-gate 		return (_lgrpsys(LGRP_SYS_HOME, idtype, (void *)(intptr_t)id));
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * Get a snapshot of the lgroup hierarchy
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate static int
lgrp_snapshot(void * buf,size_t bufsize)131*7c478bd9Sstevel@tonic-gate lgrp_snapshot(void *buf, size_t bufsize)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	return (_lgrpsys(LGRP_SYS_SNAPSHOT, bufsize, buf));
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /*
138*7c478bd9Sstevel@tonic-gate  * Find any orphan lgroups without parents and make them be children of
139*7c478bd9Sstevel@tonic-gate  * root lgroup
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate static int
parent_orphans(lgrp_snapshot_header_t * snap)142*7c478bd9Sstevel@tonic-gate parent_orphans(lgrp_snapshot_header_t *snap)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	int		i;
145*7c478bd9Sstevel@tonic-gate 	lgrp_info_t	*lgrp_info;
146*7c478bd9Sstevel@tonic-gate 	int		nlgrpsmax;
147*7c478bd9Sstevel@tonic-gate 	int		orphan;
148*7c478bd9Sstevel@tonic-gate 	lgrp_info_t	*root;
149*7c478bd9Sstevel@tonic-gate 	ulong_t		*parents;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_info == NULL ||
152*7c478bd9Sstevel@tonic-gate 	    snap->ss_parents == NULL || snap->ss_root < 0 ||
153*7c478bd9Sstevel@tonic-gate 	    snap->ss_root >= snap->ss_nlgrps_max)
154*7c478bd9Sstevel@tonic-gate 		return (-1);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	nlgrpsmax = snap->ss_nlgrps_max;
157*7c478bd9Sstevel@tonic-gate 	root = &snap->ss_info[snap->ss_root];
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrpsmax; i++) {
160*7c478bd9Sstevel@tonic-gate 		int	j;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		/*
163*7c478bd9Sstevel@tonic-gate 		 * Skip root lgroup
164*7c478bd9Sstevel@tonic-gate 		 */
165*7c478bd9Sstevel@tonic-gate 		if (i == snap->ss_root)
166*7c478bd9Sstevel@tonic-gate 			continue;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		lgrp_info = &snap->ss_info[i];
169*7c478bd9Sstevel@tonic-gate 		if (lgrp_info == NULL || lgrp_info->info_lgrpid == LGRP_NONE)
170*7c478bd9Sstevel@tonic-gate 			continue;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 		/*
173*7c478bd9Sstevel@tonic-gate 		 * Make sure parents bitmap is setup
174*7c478bd9Sstevel@tonic-gate 		 */
175*7c478bd9Sstevel@tonic-gate 		if (lgrp_info->info_parents == NULL)
176*7c478bd9Sstevel@tonic-gate 			lgrp_info->info_parents =
177*7c478bd9Sstevel@tonic-gate 			    (ulong_t *)((uintptr_t)snap->ss_parents +
178*7c478bd9Sstevel@tonic-gate 			    (i * BT_SIZEOFMAP(nlgrpsmax)));
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		/*
181*7c478bd9Sstevel@tonic-gate 		 * Look for orphans (lgroups with no parents)
182*7c478bd9Sstevel@tonic-gate 		 */
183*7c478bd9Sstevel@tonic-gate 		orphan = 1;
184*7c478bd9Sstevel@tonic-gate 		parents = lgrp_info->info_parents;
185*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < BT_BITOUL(nlgrpsmax); j++)
186*7c478bd9Sstevel@tonic-gate 			if (parents[j] != 0) {
187*7c478bd9Sstevel@tonic-gate 				orphan = 0;
188*7c478bd9Sstevel@tonic-gate 				break;
189*7c478bd9Sstevel@tonic-gate 			}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 		/*
192*7c478bd9Sstevel@tonic-gate 		 * Make root be parent of any orphans
193*7c478bd9Sstevel@tonic-gate 		 */
194*7c478bd9Sstevel@tonic-gate 		if (orphan) {
195*7c478bd9Sstevel@tonic-gate 			BT_SET(parents, root->info_lgrpid);
196*7c478bd9Sstevel@tonic-gate 			if (root->info_children) {
197*7c478bd9Sstevel@tonic-gate 				BT_SET(root->info_children, i);
198*7c478bd9Sstevel@tonic-gate 			}
199*7c478bd9Sstevel@tonic-gate 		}
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	return (0);
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate /*
207*7c478bd9Sstevel@tonic-gate  * Remove given lgroup from parent lgroup(s)
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate static void
prune_child(lgrp_snapshot_header_t * snap,lgrp_id_t lgrp)210*7c478bd9Sstevel@tonic-gate prune_child(lgrp_snapshot_header_t *snap, lgrp_id_t lgrp)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	int		i;
213*7c478bd9Sstevel@tonic-gate 	lgrp_info_t	*lgrp_info;
214*7c478bd9Sstevel@tonic-gate 	ulong_t		*parents;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || lgrp < 0 || lgrp > snap->ss_nlgrps_max)
217*7c478bd9Sstevel@tonic-gate 		return;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	lgrp_info = &snap->ss_info[lgrp];
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	parents = lgrp_info->info_parents;
222*7c478bd9Sstevel@tonic-gate 	if (parents == NULL)
223*7c478bd9Sstevel@tonic-gate 		return;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	/*
226*7c478bd9Sstevel@tonic-gate 	 * Update children of parents not to include given lgroup
227*7c478bd9Sstevel@tonic-gate 	 */
228*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < snap->ss_nlgrps_max; i++) {
229*7c478bd9Sstevel@tonic-gate 		if (BT_TEST(parents, i)) {
230*7c478bd9Sstevel@tonic-gate 			lgrp_info = &snap->ss_info[i];
231*7c478bd9Sstevel@tonic-gate 			BT_CLEAR(lgrp_info->info_children, lgrp);
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 	}
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * Prune any CPUs not in given array from specified lgroup
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate static void
prune_cpus(lgrp_snapshot_header_t * snap,lgrp_id_t lgrp,processorid_t * cpus,int ncpus)240*7c478bd9Sstevel@tonic-gate prune_cpus(lgrp_snapshot_header_t *snap, lgrp_id_t lgrp, processorid_t *cpus,
241*7c478bd9Sstevel@tonic-gate     int ncpus)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate 	int		count;
244*7c478bd9Sstevel@tonic-gate 	int		i;
245*7c478bd9Sstevel@tonic-gate 	int		j;
246*7c478bd9Sstevel@tonic-gate 	int		k;
247*7c478bd9Sstevel@tonic-gate 	lgrp_info_t	*lgrp_info;
248*7c478bd9Sstevel@tonic-gate 	uint_t		lgrp_ncpus;
249*7c478bd9Sstevel@tonic-gate 	processorid_t	*lgrp_cpus;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || lgrp < 0 || lgrp > snap->ss_nlgrps_max)
252*7c478bd9Sstevel@tonic-gate 		return;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	lgrp_info = &snap->ss_info[lgrp];
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	/*
257*7c478bd9Sstevel@tonic-gate 	 * No CPUs to remove
258*7c478bd9Sstevel@tonic-gate 	 */
259*7c478bd9Sstevel@tonic-gate 	if (ncpus == 0 || lgrp_info->info_ncpus == 0)
260*7c478bd9Sstevel@tonic-gate 		return;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	/*
263*7c478bd9Sstevel@tonic-gate 	 * Remove all CPUs from lgroup
264*7c478bd9Sstevel@tonic-gate 	 */
265*7c478bd9Sstevel@tonic-gate 	if (cpus == NULL && ncpus == -1) {
266*7c478bd9Sstevel@tonic-gate 		lgrp_info->info_ncpus = 0;
267*7c478bd9Sstevel@tonic-gate 		return;
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	/*
271*7c478bd9Sstevel@tonic-gate 	 * Remove any CPUs from lgroup not in given list of CPUs
272*7c478bd9Sstevel@tonic-gate 	 */
273*7c478bd9Sstevel@tonic-gate 	lgrp_cpus = lgrp_info->info_cpuids;
274*7c478bd9Sstevel@tonic-gate 	lgrp_ncpus = lgrp_info->info_ncpus;
275*7c478bd9Sstevel@tonic-gate 	i = 0;
276*7c478bd9Sstevel@tonic-gate 	for (count = 0; count < lgrp_ncpus; count++) {
277*7c478bd9Sstevel@tonic-gate 		/*
278*7c478bd9Sstevel@tonic-gate 		 * Look for CPU in list
279*7c478bd9Sstevel@tonic-gate 		 */
280*7c478bd9Sstevel@tonic-gate 		for (j = 0; j < ncpus; j++)
281*7c478bd9Sstevel@tonic-gate 			if (lgrp_cpus[i] == cpus[j])
282*7c478bd9Sstevel@tonic-gate 				break;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 		/*
285*7c478bd9Sstevel@tonic-gate 		 * Go to next CPU if found this one in list
286*7c478bd9Sstevel@tonic-gate 		 */
287*7c478bd9Sstevel@tonic-gate 		if (j < ncpus) {
288*7c478bd9Sstevel@tonic-gate 			i++;
289*7c478bd9Sstevel@tonic-gate 			continue;
290*7c478bd9Sstevel@tonic-gate 		}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 		/*
293*7c478bd9Sstevel@tonic-gate 		 * Remove this CPU and shift others into its place
294*7c478bd9Sstevel@tonic-gate 		 * and decrement number of CPUs
295*7c478bd9Sstevel@tonic-gate 		 */
296*7c478bd9Sstevel@tonic-gate 		for (k = i + 1; k < lgrp_info->info_ncpus; k++)
297*7c478bd9Sstevel@tonic-gate 			lgrp_cpus[k - 1] = lgrp_cpus[k];
298*7c478bd9Sstevel@tonic-gate 		lgrp_cpus[k - 1] = -1;
299*7c478bd9Sstevel@tonic-gate 		lgrp_info->info_ncpus--;
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate /*
305*7c478bd9Sstevel@tonic-gate  * Prune lgroup hierarchy for caller's view
306*7c478bd9Sstevel@tonic-gate  */
307*7c478bd9Sstevel@tonic-gate static int
prune_tree(lgrp_snapshot_header_t * snap)308*7c478bd9Sstevel@tonic-gate prune_tree(lgrp_snapshot_header_t *snap)
309*7c478bd9Sstevel@tonic-gate {
310*7c478bd9Sstevel@tonic-gate 	processorid_t	*cpus;
311*7c478bd9Sstevel@tonic-gate 	int		i;
312*7c478bd9Sstevel@tonic-gate 	lgrp_info_t	*lgrp_info;
313*7c478bd9Sstevel@tonic-gate 	lgrp_mem_size_t	nbytes;
314*7c478bd9Sstevel@tonic-gate 	uint_t		ncpus;
315*7c478bd9Sstevel@tonic-gate 	int		nlgrps_max;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_info == NULL)
318*7c478bd9Sstevel@tonic-gate 		return (-1);
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	/*
321*7c478bd9Sstevel@tonic-gate 	 * Get CPUs in caller's pset
322*7c478bd9Sstevel@tonic-gate 	 */
323*7c478bd9Sstevel@tonic-gate 	if (pset_info(PS_MYID, NULL, &ncpus, NULL) == -1)
324*7c478bd9Sstevel@tonic-gate 		return (-1);
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	cpus = NULL;
327*7c478bd9Sstevel@tonic-gate 	if (ncpus > 0) {
328*7c478bd9Sstevel@tonic-gate 		cpus = malloc(ncpus * sizeof (processorid_t));
329*7c478bd9Sstevel@tonic-gate 		if (pset_info(PS_MYID, NULL, &ncpus, cpus) == -1) {
330*7c478bd9Sstevel@tonic-gate 			free(cpus);
331*7c478bd9Sstevel@tonic-gate 			return (-1);
332*7c478bd9Sstevel@tonic-gate 		}
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	/*
336*7c478bd9Sstevel@tonic-gate 	 * Remove any CPUs not in caller's pset from lgroup hierarchy
337*7c478bd9Sstevel@tonic-gate 	 */
338*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
339*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrps_max; i++) {
340*7c478bd9Sstevel@tonic-gate 		lgrp_info = &snap->ss_info[i];
341*7c478bd9Sstevel@tonic-gate 		if (BT_TEST(snap->ss_lgrpset, i))
342*7c478bd9Sstevel@tonic-gate 			prune_cpus(snap, i, cpus, ncpus);
343*7c478bd9Sstevel@tonic-gate 		else if (lgrp_info->info_lgrpid != LGRP_NONE)
344*7c478bd9Sstevel@tonic-gate 			prune_cpus(snap, i, NULL, -1);
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if (ncpus > 0)
348*7c478bd9Sstevel@tonic-gate 		free(cpus);
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	/*
351*7c478bd9Sstevel@tonic-gate 	 * Change lgroup bitmask from just reflecting lgroups overlapping
352*7c478bd9Sstevel@tonic-gate 	 * caller's pset to all lgroups available to caller, starting by
353*7c478bd9Sstevel@tonic-gate 	 * filling in all lgroups and then removing any empty ones below
354*7c478bd9Sstevel@tonic-gate 	 */
355*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrps_max; i++) {
356*7c478bd9Sstevel@tonic-gate 		lgrp_info = &snap->ss_info[i];
357*7c478bd9Sstevel@tonic-gate 		if (lgrp_info->info_lgrpid == LGRP_NONE)
358*7c478bd9Sstevel@tonic-gate 			continue;
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 		BT_SET(snap->ss_lgrpset, i);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	/*
364*7c478bd9Sstevel@tonic-gate 	 * Remove empty lgroups from lgroup hierarchy, removing it from its
365*7c478bd9Sstevel@tonic-gate 	 * parents and decrementing nlgrps
366*7c478bd9Sstevel@tonic-gate 	 */
367*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrps_max; i++) {
368*7c478bd9Sstevel@tonic-gate 		lgrp_info = &snap->ss_info[i];
369*7c478bd9Sstevel@tonic-gate 		if (lgrp_info->info_lgrpid == LGRP_NONE)
370*7c478bd9Sstevel@tonic-gate 			continue;
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 		ncpus = lgrp_cpus_hier(snap, i, NULL, NULL);
373*7c478bd9Sstevel@tonic-gate 		nbytes = lgrp_mem_size((lgrp_cookie_t)snap, i,
374*7c478bd9Sstevel@tonic-gate 		    LGRP_MEM_SZ_INSTALLED, LGRP_CONTENT_HIERARCHY);
375*7c478bd9Sstevel@tonic-gate 		if (ncpus == 0 && nbytes == 0) {
376*7c478bd9Sstevel@tonic-gate 			BT_CLEAR(snap->ss_lgrpset, i);
377*7c478bd9Sstevel@tonic-gate 			prune_child(snap, i);
378*7c478bd9Sstevel@tonic-gate 			snap->ss_nlgrps--;
379*7c478bd9Sstevel@tonic-gate 		}
380*7c478bd9Sstevel@tonic-gate 	}
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	return (0);
383*7c478bd9Sstevel@tonic-gate }
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate /*
387*7c478bd9Sstevel@tonic-gate  * Initialize lgroup interface
388*7c478bd9Sstevel@tonic-gate  */
389*7c478bd9Sstevel@tonic-gate lgrp_cookie_t
lgrp_init(lgrp_view_t view)390*7c478bd9Sstevel@tonic-gate lgrp_init(lgrp_view_t view)
391*7c478bd9Sstevel@tonic-gate {
392*7c478bd9Sstevel@tonic-gate 	ssize_t			bufsize;
393*7c478bd9Sstevel@tonic-gate 	uint_t			gen;
394*7c478bd9Sstevel@tonic-gate 	int			i;
395*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	/*
398*7c478bd9Sstevel@tonic-gate 	 * Check for legal view
399*7c478bd9Sstevel@tonic-gate 	 */
400*7c478bd9Sstevel@tonic-gate 	if (view != LGRP_VIEW_OS && view != LGRP_VIEW_CALLER) {
401*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
402*7c478bd9Sstevel@tonic-gate 		return (LGRP_COOKIE_NONE);
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate 	/*
406*7c478bd9Sstevel@tonic-gate 	 * Try to take a consistent snapshot of lgroup hierarchy
407*7c478bd9Sstevel@tonic-gate 	 */
408*7c478bd9Sstevel@tonic-gate 	snap = NULL;
409*7c478bd9Sstevel@tonic-gate 	while (snap == NULL) {
410*7c478bd9Sstevel@tonic-gate 		/*
411*7c478bd9Sstevel@tonic-gate 		 * Get lgroup generation number before taking snapshot
412*7c478bd9Sstevel@tonic-gate 		 */
413*7c478bd9Sstevel@tonic-gate 		gen = lgrp_generation(view);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 		/*
416*7c478bd9Sstevel@tonic-gate 		 * Get size of buffer needed for snapshot
417*7c478bd9Sstevel@tonic-gate 		 */
418*7c478bd9Sstevel@tonic-gate 		bufsize = lgrp_snapshot(NULL, 0);
419*7c478bd9Sstevel@tonic-gate 		if (bufsize <= 0) {
420*7c478bd9Sstevel@tonic-gate 			if (errno == ENOMEM)
421*7c478bd9Sstevel@tonic-gate 				return (LGRP_COOKIE_NONE);
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 			snap = NULL;
424*7c478bd9Sstevel@tonic-gate 			continue;
425*7c478bd9Sstevel@tonic-gate 		}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 		/*
428*7c478bd9Sstevel@tonic-gate 		 * Allocate buffer
429*7c478bd9Sstevel@tonic-gate 		 */
430*7c478bd9Sstevel@tonic-gate 		snap = malloc(bufsize);
431*7c478bd9Sstevel@tonic-gate 		if (snap == NULL)
432*7c478bd9Sstevel@tonic-gate 			return (LGRP_COOKIE_NONE);
433*7c478bd9Sstevel@tonic-gate 		bzero(snap, bufsize);
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 		/*
436*7c478bd9Sstevel@tonic-gate 		 * Take snapshot of lgroup hierarchy
437*7c478bd9Sstevel@tonic-gate 		 */
438*7c478bd9Sstevel@tonic-gate 		bufsize = lgrp_snapshot(snap, bufsize);
439*7c478bd9Sstevel@tonic-gate 		if (bufsize <= 0) {
440*7c478bd9Sstevel@tonic-gate 			free(snap);
441*7c478bd9Sstevel@tonic-gate 			if (errno == ENOMEM)
442*7c478bd9Sstevel@tonic-gate 				return (LGRP_COOKIE_NONE);
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 			snap = NULL;
445*7c478bd9Sstevel@tonic-gate 			continue;
446*7c478bd9Sstevel@tonic-gate 		}
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 		/*
449*7c478bd9Sstevel@tonic-gate 		 * See whether lgroup generation number changed
450*7c478bd9Sstevel@tonic-gate 		 */
451*7c478bd9Sstevel@tonic-gate 		if (gen == lgrp_generation(view))
452*7c478bd9Sstevel@tonic-gate 			break;
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 		free(snap);
455*7c478bd9Sstevel@tonic-gate 		snap = NULL;
456*7c478bd9Sstevel@tonic-gate 	}
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	/*
459*7c478bd9Sstevel@tonic-gate 	 * Remember generation number and view of this snapshot
460*7c478bd9Sstevel@tonic-gate 	 */
461*7c478bd9Sstevel@tonic-gate 	snap->ss_gen = gen;
462*7c478bd9Sstevel@tonic-gate 	snap->ss_view = view;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	/*
465*7c478bd9Sstevel@tonic-gate 	 * Keep caller's pset ID for caller's view
466*7c478bd9Sstevel@tonic-gate 	 */
467*7c478bd9Sstevel@tonic-gate 	snap->ss_pset = 0;
468*7c478bd9Sstevel@tonic-gate 	if (view == LGRP_VIEW_CALLER) {
469*7c478bd9Sstevel@tonic-gate 		psetid_t	pset;
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 		if (pset_bind(PS_QUERY, P_LWPID, P_MYID, &pset) == -1)
472*7c478bd9Sstevel@tonic-gate 			return ((uintptr_t)-1);
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 		snap->ss_pset = pset;
475*7c478bd9Sstevel@tonic-gate 	}
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	/*
478*7c478bd9Sstevel@tonic-gate 	 * Find any orphan lgroups without parents and make them be children
479*7c478bd9Sstevel@tonic-gate 	 * of the root lgroup
480*7c478bd9Sstevel@tonic-gate 	 */
481*7c478bd9Sstevel@tonic-gate 	if (snap->ss_levels > 1)
482*7c478bd9Sstevel@tonic-gate 		(void) parent_orphans(snap);
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	/*
485*7c478bd9Sstevel@tonic-gate 	 * Prune snapshot of lgroup hierarchy for caller's view
486*7c478bd9Sstevel@tonic-gate 	 */
487*7c478bd9Sstevel@tonic-gate 	if (view == LGRP_VIEW_CALLER)
488*7c478bd9Sstevel@tonic-gate 		(void) prune_tree(snap);
489*7c478bd9Sstevel@tonic-gate 	else {
490*7c478bd9Sstevel@tonic-gate 		/*
491*7c478bd9Sstevel@tonic-gate 		 * Change lgroup bitmask from just reflecting lgroups
492*7c478bd9Sstevel@tonic-gate 		 * overlapping caller's pset to all lgroups available
493*7c478bd9Sstevel@tonic-gate 		 */
494*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < snap->ss_nlgrps_max; i++) {
495*7c478bd9Sstevel@tonic-gate 			lgrp_info_t	*lgrp_info;
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 			lgrp_info = &snap->ss_info[i];
498*7c478bd9Sstevel@tonic-gate 			if (lgrp_info->info_lgrpid == LGRP_NONE)
499*7c478bd9Sstevel@tonic-gate 				continue;
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 			BT_SET(snap->ss_lgrpset, i);
502*7c478bd9Sstevel@tonic-gate 		}
503*7c478bd9Sstevel@tonic-gate 	}
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 	return ((uintptr_t)snap);
506*7c478bd9Sstevel@tonic-gate }
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate /*
510*7c478bd9Sstevel@tonic-gate  * Return whether given cookie is out-of-date (stale) or not
511*7c478bd9Sstevel@tonic-gate  */
512*7c478bd9Sstevel@tonic-gate int
lgrp_cookie_stale(lgrp_cookie_t cookie)513*7c478bd9Sstevel@tonic-gate lgrp_cookie_stale(lgrp_cookie_t cookie)
514*7c478bd9Sstevel@tonic-gate {
515*7c478bd9Sstevel@tonic-gate 	psetid_t		pset;
516*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	/*
519*7c478bd9Sstevel@tonic-gate 	 * Check for bad cookie
520*7c478bd9Sstevel@tonic-gate 	 */
521*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
522*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie) {
523*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
524*7c478bd9Sstevel@tonic-gate 		return (-1);
525*7c478bd9Sstevel@tonic-gate 	}
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	/*
528*7c478bd9Sstevel@tonic-gate 	 * Check generation number which changes when lgroup hierarchy changes
529*7c478bd9Sstevel@tonic-gate 	 * or pset contents change for caller's view
530*7c478bd9Sstevel@tonic-gate 	 */
531*7c478bd9Sstevel@tonic-gate 	if (snap->ss_gen != lgrp_generation(snap->ss_view))
532*7c478bd9Sstevel@tonic-gate 		return (1);
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	/*
535*7c478bd9Sstevel@tonic-gate 	 * See whether pset binding has changed for caller's view
536*7c478bd9Sstevel@tonic-gate 	 */
537*7c478bd9Sstevel@tonic-gate 	if (snap->ss_view == LGRP_VIEW_CALLER) {
538*7c478bd9Sstevel@tonic-gate 		if (pset_bind(PS_QUERY, P_LWPID, P_MYID, &pset) == -1)
539*7c478bd9Sstevel@tonic-gate 			return (-1);
540*7c478bd9Sstevel@tonic-gate 		if (snap->ss_pset != pset)
541*7c478bd9Sstevel@tonic-gate 			return (1);
542*7c478bd9Sstevel@tonic-gate 	}
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	return (0);	/* cookie isn't stale */
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate /*
549*7c478bd9Sstevel@tonic-gate  * Get view of lgroup hierarchy from snapshot represented by given cookie
550*7c478bd9Sstevel@tonic-gate  */
551*7c478bd9Sstevel@tonic-gate lgrp_view_t
lgrp_view(lgrp_cookie_t cookie)552*7c478bd9Sstevel@tonic-gate lgrp_view(lgrp_cookie_t cookie)
553*7c478bd9Sstevel@tonic-gate {
554*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
557*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie) {
558*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
559*7c478bd9Sstevel@tonic-gate 		return (-1);
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 	return (snap->ss_view);
563*7c478bd9Sstevel@tonic-gate }
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate /*
567*7c478bd9Sstevel@tonic-gate  * Get number of lgroups
568*7c478bd9Sstevel@tonic-gate  */
569*7c478bd9Sstevel@tonic-gate int
lgrp_nlgrps(lgrp_cookie_t cookie)570*7c478bd9Sstevel@tonic-gate lgrp_nlgrps(lgrp_cookie_t cookie)
571*7c478bd9Sstevel@tonic-gate {
572*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie) {
577*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
578*7c478bd9Sstevel@tonic-gate 		return (-1);
579*7c478bd9Sstevel@tonic-gate 	}
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 	return (snap->ss_nlgrps);
582*7c478bd9Sstevel@tonic-gate }
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate /*
586*7c478bd9Sstevel@tonic-gate  * Return root lgroup ID
587*7c478bd9Sstevel@tonic-gate  */
588*7c478bd9Sstevel@tonic-gate lgrp_id_t
lgrp_root(lgrp_cookie_t cookie)589*7c478bd9Sstevel@tonic-gate lgrp_root(lgrp_cookie_t cookie)
590*7c478bd9Sstevel@tonic-gate {
591*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
594*7c478bd9Sstevel@tonic-gate 
595*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie) {
596*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
597*7c478bd9Sstevel@tonic-gate 		return (-1);
598*7c478bd9Sstevel@tonic-gate 	}
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate 	return (snap->ss_root);
601*7c478bd9Sstevel@tonic-gate }
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate /*
605*7c478bd9Sstevel@tonic-gate  * Get parent lgroups of given lgroup
606*7c478bd9Sstevel@tonic-gate  */
607*7c478bd9Sstevel@tonic-gate int
lgrp_parents(lgrp_cookie_t cookie,lgrp_id_t lgrp,lgrp_id_t * parents,uint_t count)608*7c478bd9Sstevel@tonic-gate lgrp_parents(lgrp_cookie_t cookie, lgrp_id_t lgrp, lgrp_id_t *parents,
609*7c478bd9Sstevel@tonic-gate     uint_t count)
610*7c478bd9Sstevel@tonic-gate {
611*7c478bd9Sstevel@tonic-gate 	int			i;
612*7c478bd9Sstevel@tonic-gate 	ulong_t			*lgrp_parents;
613*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
614*7c478bd9Sstevel@tonic-gate 	int			nlgrps_max;
615*7c478bd9Sstevel@tonic-gate 	int			nparents;
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	/*
620*7c478bd9Sstevel@tonic-gate 	 * Check for valid arguments
621*7c478bd9Sstevel@tonic-gate 	 */
622*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie ||
623*7c478bd9Sstevel@tonic-gate 	    lgrp < 0 || lgrp == LGRP_NONE) {
624*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
625*7c478bd9Sstevel@tonic-gate 		return (-1);
626*7c478bd9Sstevel@tonic-gate 	}
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	/*
629*7c478bd9Sstevel@tonic-gate 	 * See whether given lgroup exists
630*7c478bd9Sstevel@tonic-gate 	 */
631*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
632*7c478bd9Sstevel@tonic-gate 	if (lgrp >= nlgrps_max || !BT_TEST(snap->ss_lgrpset, lgrp)) {
633*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
634*7c478bd9Sstevel@tonic-gate 		return (-1);
635*7c478bd9Sstevel@tonic-gate 	}
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 	/*
638*7c478bd9Sstevel@tonic-gate 	 * No parents, since given lgroup is root lgroup or
639*7c478bd9Sstevel@tonic-gate 	 * only one level in lgroup hierarchy (ie. SMP)
640*7c478bd9Sstevel@tonic-gate 	 */
641*7c478bd9Sstevel@tonic-gate 	if (lgrp == snap->ss_root || snap->ss_levels == 1) {
642*7c478bd9Sstevel@tonic-gate 		if (parents == NULL || count < 1)
643*7c478bd9Sstevel@tonic-gate 			return (0);
644*7c478bd9Sstevel@tonic-gate 		return (0);
645*7c478bd9Sstevel@tonic-gate 	}
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	/*
648*7c478bd9Sstevel@tonic-gate 	 * Make sure that parents exist
649*7c478bd9Sstevel@tonic-gate 	 */
650*7c478bd9Sstevel@tonic-gate 	if (snap->ss_parents == NULL) {
651*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
652*7c478bd9Sstevel@tonic-gate 		return (-1);
653*7c478bd9Sstevel@tonic-gate 	}
654*7c478bd9Sstevel@tonic-gate 
655*7c478bd9Sstevel@tonic-gate 	/*
656*7c478bd9Sstevel@tonic-gate 	 * Given lgroup should have a parent
657*7c478bd9Sstevel@tonic-gate 	 */
658*7c478bd9Sstevel@tonic-gate 	lgrp_parents = &snap->ss_parents[lgrp * BT_BITOUL(nlgrps_max)];
659*7c478bd9Sstevel@tonic-gate 	if (lgrp_parents == NULL) {
660*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
661*7c478bd9Sstevel@tonic-gate 		return (-1);
662*7c478bd9Sstevel@tonic-gate 	}
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	/*
665*7c478bd9Sstevel@tonic-gate 	 * Check lgroup parents bitmask, fill in parents array, and return
666*7c478bd9Sstevel@tonic-gate 	 * number of parents
667*7c478bd9Sstevel@tonic-gate 	 */
668*7c478bd9Sstevel@tonic-gate 	nparents = 0;
669*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrps_max; i++) {
670*7c478bd9Sstevel@tonic-gate 		if (BT_TEST(lgrp_parents, i)) {
671*7c478bd9Sstevel@tonic-gate 			if (parents != NULL && nparents < count) {
672*7c478bd9Sstevel@tonic-gate 				parents[nparents] = i;
673*7c478bd9Sstevel@tonic-gate 			}
674*7c478bd9Sstevel@tonic-gate 			nparents++;
675*7c478bd9Sstevel@tonic-gate 		}
676*7c478bd9Sstevel@tonic-gate 	}
677*7c478bd9Sstevel@tonic-gate 	return (nparents);
678*7c478bd9Sstevel@tonic-gate }
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate /*
682*7c478bd9Sstevel@tonic-gate  * Get children lgroups of given lgroup
683*7c478bd9Sstevel@tonic-gate  */
684*7c478bd9Sstevel@tonic-gate int
lgrp_children(lgrp_cookie_t cookie,lgrp_id_t lgrp,lgrp_id_t * children,uint_t count)685*7c478bd9Sstevel@tonic-gate lgrp_children(lgrp_cookie_t cookie, lgrp_id_t lgrp, lgrp_id_t *children,
686*7c478bd9Sstevel@tonic-gate     uint_t count)
687*7c478bd9Sstevel@tonic-gate {
688*7c478bd9Sstevel@tonic-gate 	int			i;
689*7c478bd9Sstevel@tonic-gate 	ulong_t			*lgrp_children;
690*7c478bd9Sstevel@tonic-gate 	int			nlgrps_max;
691*7c478bd9Sstevel@tonic-gate 	int			nchildren;
692*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate 	/*
697*7c478bd9Sstevel@tonic-gate 	 * Check for valid arguments
698*7c478bd9Sstevel@tonic-gate 	 */
699*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie ||
700*7c478bd9Sstevel@tonic-gate 	    lgrp < 0 || lgrp == LGRP_NONE) {
701*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
702*7c478bd9Sstevel@tonic-gate 		return (-1);
703*7c478bd9Sstevel@tonic-gate 	}
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	/*
706*7c478bd9Sstevel@tonic-gate 	 * See whether given lgroup exists
707*7c478bd9Sstevel@tonic-gate 	 */
708*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
709*7c478bd9Sstevel@tonic-gate 	if (lgrp >= nlgrps_max || !BT_TEST(snap->ss_lgrpset, lgrp)) {
710*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
711*7c478bd9Sstevel@tonic-gate 		return (-1);
712*7c478bd9Sstevel@tonic-gate 	}
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	/*
715*7c478bd9Sstevel@tonic-gate 	 * No children, since only one level in lgroup hierarchy (ie. SMP)
716*7c478bd9Sstevel@tonic-gate 	 */
717*7c478bd9Sstevel@tonic-gate 	if (snap->ss_levels == 1) {
718*7c478bd9Sstevel@tonic-gate 		if (children == NULL || count < 1)
719*7c478bd9Sstevel@tonic-gate 			return (0);
720*7c478bd9Sstevel@tonic-gate 		return (0);
721*7c478bd9Sstevel@tonic-gate 	}
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 	/*
724*7c478bd9Sstevel@tonic-gate 	 * Make sure that children exist
725*7c478bd9Sstevel@tonic-gate 	 */
726*7c478bd9Sstevel@tonic-gate 	if (snap->ss_children == NULL) {
727*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
728*7c478bd9Sstevel@tonic-gate 		return (-1);
729*7c478bd9Sstevel@tonic-gate 	}
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	/*
732*7c478bd9Sstevel@tonic-gate 	 * Given lgroup may not have any children
733*7c478bd9Sstevel@tonic-gate 	 */
734*7c478bd9Sstevel@tonic-gate 	lgrp_children = &snap->ss_children[lgrp * BT_BITOUL(nlgrps_max)];
735*7c478bd9Sstevel@tonic-gate 
736*7c478bd9Sstevel@tonic-gate 	if (lgrp_children == NULL)
737*7c478bd9Sstevel@tonic-gate 		return (0);
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate 	/*
740*7c478bd9Sstevel@tonic-gate 	 * Check lgroup children bitmask, fill in children array, and return
741*7c478bd9Sstevel@tonic-gate 	 * number of children
742*7c478bd9Sstevel@tonic-gate 	 */
743*7c478bd9Sstevel@tonic-gate 	nchildren = 0;
744*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrps_max; i++) {
745*7c478bd9Sstevel@tonic-gate 		if (BT_TEST(lgrp_children, i)) {
746*7c478bd9Sstevel@tonic-gate 			if (children != NULL && nchildren < count)
747*7c478bd9Sstevel@tonic-gate 				children[nchildren] = i;
748*7c478bd9Sstevel@tonic-gate 			nchildren++;
749*7c478bd9Sstevel@tonic-gate 		}
750*7c478bd9Sstevel@tonic-gate 	}
751*7c478bd9Sstevel@tonic-gate 	return (nchildren);
752*7c478bd9Sstevel@tonic-gate }
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 
755*7c478bd9Sstevel@tonic-gate /*
756*7c478bd9Sstevel@tonic-gate  * Get all CPUs within given lgroup (hierarchy)
757*7c478bd9Sstevel@tonic-gate  */
758*7c478bd9Sstevel@tonic-gate static int
lgrp_cpus_hier(lgrp_snapshot_header_t * snap,lgrp_id_t lgrp,processorid_t ** cpuids,uint_t * count)759*7c478bd9Sstevel@tonic-gate lgrp_cpus_hier(lgrp_snapshot_header_t *snap, lgrp_id_t lgrp,
760*7c478bd9Sstevel@tonic-gate     processorid_t **cpuids, uint_t *count)
761*7c478bd9Sstevel@tonic-gate {
762*7c478bd9Sstevel@tonic-gate 	processorid_t	*cpus;
763*7c478bd9Sstevel@tonic-gate 	int		i;
764*7c478bd9Sstevel@tonic-gate 	int		j;
765*7c478bd9Sstevel@tonic-gate 	lgrp_info_t	*lgrp_info;
766*7c478bd9Sstevel@tonic-gate 	int		ncpus;
767*7c478bd9Sstevel@tonic-gate 	int		nlgrps_max;
768*7c478bd9Sstevel@tonic-gate 	ulong_t		*rset;
769*7c478bd9Sstevel@tonic-gate 	int		total;
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate 	/*
772*7c478bd9Sstevel@tonic-gate 	 * Get lgroup info
773*7c478bd9Sstevel@tonic-gate 	 */
774*7c478bd9Sstevel@tonic-gate 	lgrp_info = &snap->ss_info[lgrp];
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 	if (lgrp_info == NULL) {
777*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
778*7c478bd9Sstevel@tonic-gate 		return (-1);
779*7c478bd9Sstevel@tonic-gate 	}
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate 	/*
782*7c478bd9Sstevel@tonic-gate 	 * Check whether given lgroup contains any lgroups with CPU resources
783*7c478bd9Sstevel@tonic-gate 	 */
784*7c478bd9Sstevel@tonic-gate 	if (lgrp_info->info_rset == NULL)
785*7c478bd9Sstevel@tonic-gate 		return (0);
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
788*7c478bd9Sstevel@tonic-gate 	rset = &lgrp_info->info_rset[LGRP_RSRC_CPU * BT_BITOUL(nlgrps_max)];
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	/*
791*7c478bd9Sstevel@tonic-gate 	 * Get all CPUs within this lgroup
792*7c478bd9Sstevel@tonic-gate 	 */
793*7c478bd9Sstevel@tonic-gate 	total = 0;
794*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlgrps_max; i++) {
795*7c478bd9Sstevel@tonic-gate 		if (!BT_TEST(rset, i))
796*7c478bd9Sstevel@tonic-gate 			continue;
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 		lgrp_info = &snap->ss_info[i];
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate 		/*
801*7c478bd9Sstevel@tonic-gate 		 * Get all CPUs within lgroup
802*7c478bd9Sstevel@tonic-gate 		 */
803*7c478bd9Sstevel@tonic-gate 		cpus = lgrp_info->info_cpuids;
804*7c478bd9Sstevel@tonic-gate 		ncpus = lgrp_info->info_ncpus;
805*7c478bd9Sstevel@tonic-gate 		total += ncpus;
806*7c478bd9Sstevel@tonic-gate 
807*7c478bd9Sstevel@tonic-gate 		/*
808*7c478bd9Sstevel@tonic-gate 		 * Copy as many CPU IDs into array that will fit
809*7c478bd9Sstevel@tonic-gate 		 * and decrement count and increment array pointer
810*7c478bd9Sstevel@tonic-gate 		 * as we go
811*7c478bd9Sstevel@tonic-gate 		 */
812*7c478bd9Sstevel@tonic-gate 		if (cpuids && *cpuids && count) {
813*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < ncpus; j++) {
814*7c478bd9Sstevel@tonic-gate 				if (*count) {
815*7c478bd9Sstevel@tonic-gate 					**cpuids = cpus[j];
816*7c478bd9Sstevel@tonic-gate 					(*cpuids)++;
817*7c478bd9Sstevel@tonic-gate 					(*count)--;
818*7c478bd9Sstevel@tonic-gate 				}
819*7c478bd9Sstevel@tonic-gate 			}
820*7c478bd9Sstevel@tonic-gate 		}
821*7c478bd9Sstevel@tonic-gate 	}
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate 	return (total);
824*7c478bd9Sstevel@tonic-gate }
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate /*
828*7c478bd9Sstevel@tonic-gate  * Get CPUs in given lgroup
829*7c478bd9Sstevel@tonic-gate  */
830*7c478bd9Sstevel@tonic-gate int
lgrp_cpus(lgrp_cookie_t cookie,lgrp_id_t lgrp,processorid_t * cpuids,uint_t count,lgrp_content_t content)831*7c478bd9Sstevel@tonic-gate lgrp_cpus(lgrp_cookie_t cookie, lgrp_id_t lgrp, processorid_t *cpuids,
832*7c478bd9Sstevel@tonic-gate     uint_t count, lgrp_content_t content)
833*7c478bd9Sstevel@tonic-gate {
834*7c478bd9Sstevel@tonic-gate 	int			i;
835*7c478bd9Sstevel@tonic-gate 	processorid_t		*cpus;
836*7c478bd9Sstevel@tonic-gate 	lgrp_info_t		*lgrp_info;
837*7c478bd9Sstevel@tonic-gate 	int			ncpus;
838*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 	/*
843*7c478bd9Sstevel@tonic-gate 	 * Check for valid arguments
844*7c478bd9Sstevel@tonic-gate 	 */
845*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie ||
846*7c478bd9Sstevel@tonic-gate 	    lgrp < 0 || lgrp == LGRP_NONE ||
847*7c478bd9Sstevel@tonic-gate 	    (content != LGRP_CONTENT_DIRECT &&
848*7c478bd9Sstevel@tonic-gate 	    content != LGRP_CONTENT_HIERARCHY)) {
849*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
850*7c478bd9Sstevel@tonic-gate 		return (-1);
851*7c478bd9Sstevel@tonic-gate 	}
852*7c478bd9Sstevel@tonic-gate 
853*7c478bd9Sstevel@tonic-gate 	/*
854*7c478bd9Sstevel@tonic-gate 	 * See whether given lgroup exists
855*7c478bd9Sstevel@tonic-gate 	 */
856*7c478bd9Sstevel@tonic-gate 	if (lgrp >= snap->ss_nlgrps_max || snap->ss_info == NULL ||
857*7c478bd9Sstevel@tonic-gate 	    !BT_TEST(snap->ss_lgrpset, lgrp)) {
858*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
859*7c478bd9Sstevel@tonic-gate 		return (-1);
860*7c478bd9Sstevel@tonic-gate 	}
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 	/*
863*7c478bd9Sstevel@tonic-gate 	 * Get lgroup info
864*7c478bd9Sstevel@tonic-gate 	 */
865*7c478bd9Sstevel@tonic-gate 	lgrp_info = &snap->ss_info[lgrp];
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	/*
868*7c478bd9Sstevel@tonic-gate 	 * Get contents of lgroup
869*7c478bd9Sstevel@tonic-gate 	 */
870*7c478bd9Sstevel@tonic-gate 	switch (content) {
871*7c478bd9Sstevel@tonic-gate 	case LGRP_CONTENT_DIRECT:
872*7c478bd9Sstevel@tonic-gate 		/*
873*7c478bd9Sstevel@tonic-gate 		 * Get CPUs contained directly within given lgroup
874*7c478bd9Sstevel@tonic-gate 		 */
875*7c478bd9Sstevel@tonic-gate 		cpus = lgrp_info->info_cpuids;
876*7c478bd9Sstevel@tonic-gate 		ncpus = lgrp_info->info_ncpus;
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate 		/*
879*7c478bd9Sstevel@tonic-gate 		 * No array to copy CPU IDs into,
880*7c478bd9Sstevel@tonic-gate 		 * so just return number of CPUs.
881*7c478bd9Sstevel@tonic-gate 		 */
882*7c478bd9Sstevel@tonic-gate 		if (cpuids == NULL)
883*7c478bd9Sstevel@tonic-gate 			return (ncpus);
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate 		/*
886*7c478bd9Sstevel@tonic-gate 		 * Copy as many CPU IDs into array that will fit
887*7c478bd9Sstevel@tonic-gate 		 */
888*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncpus; i++)
889*7c478bd9Sstevel@tonic-gate 			if (i < count)
890*7c478bd9Sstevel@tonic-gate 				cpuids[i] = cpus[i];
891*7c478bd9Sstevel@tonic-gate 
892*7c478bd9Sstevel@tonic-gate 		return (ncpus);
893*7c478bd9Sstevel@tonic-gate 
894*7c478bd9Sstevel@tonic-gate 	case LGRP_CONTENT_ALL:
895*7c478bd9Sstevel@tonic-gate 		return (lgrp_cpus_hier(snap, lgrp, &cpuids, &count));
896*7c478bd9Sstevel@tonic-gate 
897*7c478bd9Sstevel@tonic-gate 	default:
898*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
899*7c478bd9Sstevel@tonic-gate 		return (-1);
900*7c478bd9Sstevel@tonic-gate 	}
901*7c478bd9Sstevel@tonic-gate }
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 
904*7c478bd9Sstevel@tonic-gate /*
905*7c478bd9Sstevel@tonic-gate  * Return physical memory size in pages for given lgroup
906*7c478bd9Sstevel@tonic-gate  */
907*7c478bd9Sstevel@tonic-gate lgrp_mem_size_t
lgrp_mem_size(lgrp_cookie_t cookie,lgrp_id_t lgrp,lgrp_mem_size_flag_t type,lgrp_content_t content)908*7c478bd9Sstevel@tonic-gate lgrp_mem_size(lgrp_cookie_t cookie, lgrp_id_t lgrp, lgrp_mem_size_flag_t type,
909*7c478bd9Sstevel@tonic-gate     lgrp_content_t content)
910*7c478bd9Sstevel@tonic-gate {
911*7c478bd9Sstevel@tonic-gate 	int			i;
912*7c478bd9Sstevel@tonic-gate 	lgrp_info_t		*lgrp_info;
913*7c478bd9Sstevel@tonic-gate 	int			nlgrps_max;
914*7c478bd9Sstevel@tonic-gate 	int			pgsz;
915*7c478bd9Sstevel@tonic-gate 	ulong_t			*rset;
916*7c478bd9Sstevel@tonic-gate 	lgrp_mem_size_t		size;
917*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
920*7c478bd9Sstevel@tonic-gate 
921*7c478bd9Sstevel@tonic-gate 	/*
922*7c478bd9Sstevel@tonic-gate 	 * Check for valid arguments
923*7c478bd9Sstevel@tonic-gate 	 */
924*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie ||
925*7c478bd9Sstevel@tonic-gate 	    lgrp < 0 || lgrp == LGRP_NONE) {
926*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
927*7c478bd9Sstevel@tonic-gate 		return (-1);
928*7c478bd9Sstevel@tonic-gate 	}
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate 	/*
931*7c478bd9Sstevel@tonic-gate 	 * See whether given lgroup exists
932*7c478bd9Sstevel@tonic-gate 	 */
933*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
934*7c478bd9Sstevel@tonic-gate 	if (lgrp >= nlgrps_max || snap->ss_info == NULL ||
935*7c478bd9Sstevel@tonic-gate 	    !BT_TEST(snap->ss_lgrpset, lgrp)) {
936*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
937*7c478bd9Sstevel@tonic-gate 		return (-1);
938*7c478bd9Sstevel@tonic-gate 	}
939*7c478bd9Sstevel@tonic-gate 
940*7c478bd9Sstevel@tonic-gate 	pgsz = getpagesize();
941*7c478bd9Sstevel@tonic-gate 
942*7c478bd9Sstevel@tonic-gate 	/*
943*7c478bd9Sstevel@tonic-gate 	 * Get lgroup info
944*7c478bd9Sstevel@tonic-gate 	 */
945*7c478bd9Sstevel@tonic-gate 	lgrp_info = &snap->ss_info[lgrp];
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate 	switch (content) {
948*7c478bd9Sstevel@tonic-gate 	case LGRP_CONTENT_DIRECT:
949*7c478bd9Sstevel@tonic-gate 		/*
950*7c478bd9Sstevel@tonic-gate 		 * Get memory contained directly in this lgroup
951*7c478bd9Sstevel@tonic-gate 		 */
952*7c478bd9Sstevel@tonic-gate 		switch (type) {
953*7c478bd9Sstevel@tonic-gate 		case LGRP_MEM_SZ_FREE:
954*7c478bd9Sstevel@tonic-gate 			size = (lgrp_mem_size_t)pgsz *
955*7c478bd9Sstevel@tonic-gate 			    lgrp_info->info_mem_free;
956*7c478bd9Sstevel@tonic-gate 			return (size);
957*7c478bd9Sstevel@tonic-gate 		case LGRP_MEM_SZ_INSTALLED:
958*7c478bd9Sstevel@tonic-gate 			size = (lgrp_mem_size_t)pgsz *
959*7c478bd9Sstevel@tonic-gate 			    lgrp_info->info_mem_install;
960*7c478bd9Sstevel@tonic-gate 			return (size);
961*7c478bd9Sstevel@tonic-gate 		default:
962*7c478bd9Sstevel@tonic-gate 			errno = EINVAL;
963*7c478bd9Sstevel@tonic-gate 			return (-1);
964*7c478bd9Sstevel@tonic-gate 		}
965*7c478bd9Sstevel@tonic-gate 
966*7c478bd9Sstevel@tonic-gate 	case LGRP_CONTENT_ALL:
967*7c478bd9Sstevel@tonic-gate 		/*
968*7c478bd9Sstevel@tonic-gate 		 * Get memory contained within this lgroup (and its children)
969*7c478bd9Sstevel@tonic-gate 		 */
970*7c478bd9Sstevel@tonic-gate 		/*
971*7c478bd9Sstevel@tonic-gate 		 * Check whether given lgroup contains any lgroups with CPU
972*7c478bd9Sstevel@tonic-gate 		 * resources
973*7c478bd9Sstevel@tonic-gate 		 */
974*7c478bd9Sstevel@tonic-gate 		if (lgrp_info->info_rset == NULL)
975*7c478bd9Sstevel@tonic-gate 			return (0);
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate 		rset = &lgrp_info->info_rset[LGRP_RSRC_MEM *
978*7c478bd9Sstevel@tonic-gate 		    BT_BITOUL(nlgrps_max)];
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 		/*
981*7c478bd9Sstevel@tonic-gate 		 * Add up memory in lgroup resources
982*7c478bd9Sstevel@tonic-gate 		 */
983*7c478bd9Sstevel@tonic-gate 		size = 0;
984*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nlgrps_max; i++) {
985*7c478bd9Sstevel@tonic-gate 			if (!BT_TEST(rset, i))
986*7c478bd9Sstevel@tonic-gate 				continue;
987*7c478bd9Sstevel@tonic-gate 
988*7c478bd9Sstevel@tonic-gate 			lgrp_info = &snap->ss_info[i];
989*7c478bd9Sstevel@tonic-gate 			switch (type) {
990*7c478bd9Sstevel@tonic-gate 			case LGRP_MEM_SZ_FREE:
991*7c478bd9Sstevel@tonic-gate 				size += (lgrp_mem_size_t)pgsz *
992*7c478bd9Sstevel@tonic-gate 				    lgrp_info->info_mem_free;
993*7c478bd9Sstevel@tonic-gate 				break;
994*7c478bd9Sstevel@tonic-gate 			case LGRP_MEM_SZ_INSTALLED:
995*7c478bd9Sstevel@tonic-gate 				size += (lgrp_mem_size_t)pgsz *
996*7c478bd9Sstevel@tonic-gate 				    lgrp_info->info_mem_install;
997*7c478bd9Sstevel@tonic-gate 				break;
998*7c478bd9Sstevel@tonic-gate 			default:
999*7c478bd9Sstevel@tonic-gate 				errno = EINVAL;
1000*7c478bd9Sstevel@tonic-gate 				return (-1);
1001*7c478bd9Sstevel@tonic-gate 			}
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate 		}
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 		return (size);
1006*7c478bd9Sstevel@tonic-gate 
1007*7c478bd9Sstevel@tonic-gate 	default:
1008*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1009*7c478bd9Sstevel@tonic-gate 		return (-1);
1010*7c478bd9Sstevel@tonic-gate 	}
1011*7c478bd9Sstevel@tonic-gate }
1012*7c478bd9Sstevel@tonic-gate 
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate /*
1015*7c478bd9Sstevel@tonic-gate  * Get resources for a particuliar lgroup
1016*7c478bd9Sstevel@tonic-gate  */
1017*7c478bd9Sstevel@tonic-gate int
lgrp_resources(lgrp_cookie_t cookie,lgrp_id_t lgrp,lgrp_id_t * lgrps,uint_t count,lgrp_rsrc_t type)1018*7c478bd9Sstevel@tonic-gate lgrp_resources(lgrp_cookie_t cookie, lgrp_id_t lgrp, lgrp_id_t *lgrps,
1019*7c478bd9Sstevel@tonic-gate     uint_t count, lgrp_rsrc_t type)
1020*7c478bd9Sstevel@tonic-gate {
1021*7c478bd9Sstevel@tonic-gate 	int			i;
1022*7c478bd9Sstevel@tonic-gate 	lgrp_info_t		*lgrp_info;
1023*7c478bd9Sstevel@tonic-gate 	int			nlgrps;
1024*7c478bd9Sstevel@tonic-gate 	int			nlgrps_max;
1025*7c478bd9Sstevel@tonic-gate 	ulong_t			*rset;
1026*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
1027*7c478bd9Sstevel@tonic-gate 
1028*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 	/*
1031*7c478bd9Sstevel@tonic-gate 	 * Check for valid arguments
1032*7c478bd9Sstevel@tonic-gate 	 */
1033*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie ||
1034*7c478bd9Sstevel@tonic-gate 	    lgrp < 0 || lgrp == LGRP_NONE ||
1035*7c478bd9Sstevel@tonic-gate 	    (type != LGRP_RSRC_CPU && type != LGRP_RSRC_MEM)) {
1036*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1037*7c478bd9Sstevel@tonic-gate 		return (-1);
1038*7c478bd9Sstevel@tonic-gate 	}
1039*7c478bd9Sstevel@tonic-gate 
1040*7c478bd9Sstevel@tonic-gate 	/*
1041*7c478bd9Sstevel@tonic-gate 	 * See whether given lgroup exists
1042*7c478bd9Sstevel@tonic-gate 	 */
1043*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
1044*7c478bd9Sstevel@tonic-gate 	if (lgrp >= nlgrps_max || snap->ss_info == NULL ||
1045*7c478bd9Sstevel@tonic-gate 	    !BT_TEST(snap->ss_lgrpset, lgrp)) {
1046*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
1047*7c478bd9Sstevel@tonic-gate 		return (-1);
1048*7c478bd9Sstevel@tonic-gate 	}
1049*7c478bd9Sstevel@tonic-gate 
1050*7c478bd9Sstevel@tonic-gate 	/*
1051*7c478bd9Sstevel@tonic-gate 	 * Get lgroup info
1052*7c478bd9Sstevel@tonic-gate 	 */
1053*7c478bd9Sstevel@tonic-gate 	lgrp_info = &snap->ss_info[lgrp];
1054*7c478bd9Sstevel@tonic-gate 
1055*7c478bd9Sstevel@tonic-gate 	/*
1056*7c478bd9Sstevel@tonic-gate 	 * Count number lgroups contained within this lgroup and
1057*7c478bd9Sstevel@tonic-gate 	 * copy as many lgroup IDs into array that will fit
1058*7c478bd9Sstevel@tonic-gate 	 */
1059*7c478bd9Sstevel@tonic-gate 	rset = &lgrp_info->info_rset[type * BT_BITOUL(nlgrps_max)];
1060*7c478bd9Sstevel@tonic-gate 	nlgrps = 0;
1061*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < snap->ss_nlgrps_max; i++)
1062*7c478bd9Sstevel@tonic-gate 		if (BT_TEST(rset, i)) {
1063*7c478bd9Sstevel@tonic-gate 			if (lgrps != NULL && nlgrps < count)
1064*7c478bd9Sstevel@tonic-gate 				lgrps[nlgrps] = i;
1065*7c478bd9Sstevel@tonic-gate 			nlgrps++;
1066*7c478bd9Sstevel@tonic-gate 		}
1067*7c478bd9Sstevel@tonic-gate 
1068*7c478bd9Sstevel@tonic-gate 	return (nlgrps);
1069*7c478bd9Sstevel@tonic-gate }
1070*7c478bd9Sstevel@tonic-gate 
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate /*
1073*7c478bd9Sstevel@tonic-gate  * Finish using lgroup interface
1074*7c478bd9Sstevel@tonic-gate  */
1075*7c478bd9Sstevel@tonic-gate int
lgrp_fini(lgrp_cookie_t cookie)1076*7c478bd9Sstevel@tonic-gate lgrp_fini(lgrp_cookie_t cookie)
1077*7c478bd9Sstevel@tonic-gate {
1078*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
1079*7c478bd9Sstevel@tonic-gate 
1080*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
1081*7c478bd9Sstevel@tonic-gate 
1082*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie) {
1083*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1084*7c478bd9Sstevel@tonic-gate 		return (-1);
1085*7c478bd9Sstevel@tonic-gate 	}
1086*7c478bd9Sstevel@tonic-gate 
1087*7c478bd9Sstevel@tonic-gate 	bzero(snap, snap->ss_size);
1088*7c478bd9Sstevel@tonic-gate 	free(snap);
1089*7c478bd9Sstevel@tonic-gate 	snap = NULL;
1090*7c478bd9Sstevel@tonic-gate 
1091*7c478bd9Sstevel@tonic-gate 	return (0);
1092*7c478bd9Sstevel@tonic-gate }
1093*7c478bd9Sstevel@tonic-gate 
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate /*
1096*7c478bd9Sstevel@tonic-gate  * Return latency between "from" and "to" lgroups
1097*7c478bd9Sstevel@tonic-gate  *
1098*7c478bd9Sstevel@tonic-gate  * This latency number can only be used for relative comparison
1099*7c478bd9Sstevel@tonic-gate  * between lgroups on the running system, cannot be used across platforms,
1100*7c478bd9Sstevel@tonic-gate  * and may not reflect the actual latency.  It is platform and implementation
1101*7c478bd9Sstevel@tonic-gate  * specific, so platform gets to decide its value.  It would be nice if the
1102*7c478bd9Sstevel@tonic-gate  * number was at least proportional to make comparisons more meaningful though.
1103*7c478bd9Sstevel@tonic-gate  */
1104*7c478bd9Sstevel@tonic-gate int
lgrp_latency(lgrp_id_t from,lgrp_id_t to)1105*7c478bd9Sstevel@tonic-gate lgrp_latency(lgrp_id_t from, lgrp_id_t to)
1106*7c478bd9Sstevel@tonic-gate {
1107*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t		cookie;
1108*7c478bd9Sstevel@tonic-gate 	int			latency;
1109*7c478bd9Sstevel@tonic-gate 
1110*7c478bd9Sstevel@tonic-gate 	cookie = lgrp_init(LGRP_VIEW_OS);
1111*7c478bd9Sstevel@tonic-gate 	latency = lgrp_latency_cookie(cookie, from, to, LGRP_LAT_CPU_TO_MEM);
1112*7c478bd9Sstevel@tonic-gate 	(void) lgrp_fini(cookie);
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate 	return (latency);
1115*7c478bd9Sstevel@tonic-gate }
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate 
1118*7c478bd9Sstevel@tonic-gate /*
1119*7c478bd9Sstevel@tonic-gate  * Return latency between "from" and "to" lgroups
1120*7c478bd9Sstevel@tonic-gate  *
1121*7c478bd9Sstevel@tonic-gate  * This latency number can only be used for relative comparison
1122*7c478bd9Sstevel@tonic-gate  * between lgroups on the running system, cannot be used across platforms,
1123*7c478bd9Sstevel@tonic-gate  * and may not reflect the actual latency.  It is platform and implementation
1124*7c478bd9Sstevel@tonic-gate  * specific, so platform gets to decide its value.  It would be nice if the
1125*7c478bd9Sstevel@tonic-gate  * number was at least proportional to make comparisons more meaningful though.
1126*7c478bd9Sstevel@tonic-gate  */
1127*7c478bd9Sstevel@tonic-gate int
lgrp_latency_cookie(lgrp_cookie_t cookie,lgrp_id_t from,lgrp_id_t to,lgrp_lat_between_t between)1128*7c478bd9Sstevel@tonic-gate lgrp_latency_cookie(lgrp_cookie_t cookie, lgrp_id_t from, lgrp_id_t to,
1129*7c478bd9Sstevel@tonic-gate     lgrp_lat_between_t between)
1130*7c478bd9Sstevel@tonic-gate {
1131*7c478bd9Sstevel@tonic-gate 	lgrp_info_t		*lgrp_info;
1132*7c478bd9Sstevel@tonic-gate 	lgrp_mem_size_t		nbytes;
1133*7c478bd9Sstevel@tonic-gate 	int			ncpus;
1134*7c478bd9Sstevel@tonic-gate 	int			nlgrps_max;
1135*7c478bd9Sstevel@tonic-gate 	lgrp_snapshot_header_t	*snap;
1136*7c478bd9Sstevel@tonic-gate 
1137*7c478bd9Sstevel@tonic-gate 	snap = (lgrp_snapshot_header_t *)cookie;
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate 	/*
1140*7c478bd9Sstevel@tonic-gate 	 * Check for valid snapshot, lgroup, and between flag
1141*7c478bd9Sstevel@tonic-gate 	 */
1142*7c478bd9Sstevel@tonic-gate 	if (snap == NULL || snap->ss_magic != cookie || from < 0 || to < 0 ||
1143*7c478bd9Sstevel@tonic-gate 	    between != LGRP_LAT_CPU_TO_MEM) {
1144*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1145*7c478bd9Sstevel@tonic-gate 		return (-1);
1146*7c478bd9Sstevel@tonic-gate 	}
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 	/*
1149*7c478bd9Sstevel@tonic-gate 	 * Check whether lgroups exist
1150*7c478bd9Sstevel@tonic-gate 	 */
1151*7c478bd9Sstevel@tonic-gate 	nlgrps_max = snap->ss_nlgrps_max;
1152*7c478bd9Sstevel@tonic-gate 	if (from >= nlgrps_max || to >= nlgrps_max) {
1153*7c478bd9Sstevel@tonic-gate 		errno = ESRCH;
1154*7c478bd9Sstevel@tonic-gate 		return (-1);
1155*7c478bd9Sstevel@tonic-gate 	}
1156*7c478bd9Sstevel@tonic-gate 
1157*7c478bd9Sstevel@tonic-gate 	/*
1158*7c478bd9Sstevel@tonic-gate 	 * Check whether "from" lgroup has any CPUs
1159*7c478bd9Sstevel@tonic-gate 	 */
1160*7c478bd9Sstevel@tonic-gate 	ncpus = lgrp_cpus(cookie, from, NULL, 0, LGRP_CONTENT_HIERARCHY);
1161*7c478bd9Sstevel@tonic-gate 	if (ncpus <= 0) {
1162*7c478bd9Sstevel@tonic-gate 		if (ncpus == 0)
1163*7c478bd9Sstevel@tonic-gate 			errno = ESRCH;
1164*7c478bd9Sstevel@tonic-gate 		return (-1);
1165*7c478bd9Sstevel@tonic-gate 	}
1166*7c478bd9Sstevel@tonic-gate 
1167*7c478bd9Sstevel@tonic-gate 	/*
1168*7c478bd9Sstevel@tonic-gate 	 * Check whether "to" lgroup has any memory
1169*7c478bd9Sstevel@tonic-gate 	 */
1170*7c478bd9Sstevel@tonic-gate 	nbytes = lgrp_mem_size(cookie, to, LGRP_MEM_SZ_INSTALLED,
1171*7c478bd9Sstevel@tonic-gate 	    LGRP_CONTENT_HIERARCHY);
1172*7c478bd9Sstevel@tonic-gate 	if (nbytes <= 0) {
1173*7c478bd9Sstevel@tonic-gate 		if (nbytes == 0)
1174*7c478bd9Sstevel@tonic-gate 			errno = ESRCH;
1175*7c478bd9Sstevel@tonic-gate 		return (-1);
1176*7c478bd9Sstevel@tonic-gate 	}
1177*7c478bd9Sstevel@tonic-gate 
1178*7c478bd9Sstevel@tonic-gate 	if (from == to) {
1179*7c478bd9Sstevel@tonic-gate 		lgrp_info = &snap->ss_info[from];
1180*7c478bd9Sstevel@tonic-gate 		return (lgrp_info->info_latency);
1181*7c478bd9Sstevel@tonic-gate 	}
1182*7c478bd9Sstevel@tonic-gate 
1183*7c478bd9Sstevel@tonic-gate 	return (snap->ss_latencies[from][to]);
1184*7c478bd9Sstevel@tonic-gate }
1185