xref: /titanic_41/usr/src/cmd/pools/poolstat/sa_kstat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * sa_kstat - kstat statistic adapter, collects statistic data provided
31*7c478bd9Sstevel@tonic-gate  * by kstat.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <locale.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <assert.h>
37*7c478bd9Sstevel@tonic-gate #include <kstat.h>
38*7c478bd9Sstevel@tonic-gate #include <pool.h>
39*7c478bd9Sstevel@tonic-gate #include "utils.h"
40*7c478bd9Sstevel@tonic-gate #include <sys/pset.h>
41*7c478bd9Sstevel@tonic-gate #include "poolstat.h"
42*7c478bd9Sstevel@tonic-gate #include "poolstat_utils.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /* marks 'sdata_t' element as updated	*/
45*7c478bd9Sstevel@tonic-gate #define	SD_UPDATED	1
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /* Specified value for an invalid set.	*/
48*7c478bd9Sstevel@tonic-gate #define	INVALID_SET	-2
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /* statistic data	*/
51*7c478bd9Sstevel@tonic-gate typedef struct sdata {
52*7c478bd9Sstevel@tonic-gate 	kstat_t		sd_oks;		/* old kstat	*/
53*7c478bd9Sstevel@tonic-gate 	kstat_t		sd_nks;		/* new kstat	*/
54*7c478bd9Sstevel@tonic-gate 	void		*sd_udata; 	/* user data	*/
55*7c478bd9Sstevel@tonic-gate 	uint_t		sd_state;	/* state of this data (UPDATED)	*/
56*7c478bd9Sstevel@tonic-gate 	struct sdata	*sd_next;
57*7c478bd9Sstevel@tonic-gate } sdata_t;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /* pset user data	*/
60*7c478bd9Sstevel@tonic-gate typedef struct {
61*7c478bd9Sstevel@tonic-gate 	psetid_t opset;		/* old pset sysid	*/
62*7c478bd9Sstevel@tonic-gate 	psetid_t npset;		/* new pset sysid	*/
63*7c478bd9Sstevel@tonic-gate } pset_ud_t;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate /* shortcuts to access set's id in 'pset_ud_t'		*/
66*7c478bd9Sstevel@tonic-gate #define	SD_OPSET(p)	(((pset_ud_t *)(p)->sd_udata)->opset)
67*7c478bd9Sstevel@tonic-gate #define	SD_NPSET(p)	(((pset_ud_t *)(p)->sd_udata)->npset)
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static	kstat_ctl_t	*ks_ctl;	/* libkstat handle		*/
70*7c478bd9Sstevel@tonic-gate static  sdata_t		*cpu_list;	/* list with cpu statistics	*/
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static sdata_t *update_sdata_list(sdata_t *, kstat_ctl_t *, char *, int,
73*7c478bd9Sstevel@tonic-gate 	char *, int *);
74*7c478bd9Sstevel@tonic-gate static void update_cpu_list(sdata_t *list);
75*7c478bd9Sstevel@tonic-gate static void update_pset_stats(statistic_bag_t *, sdata_t *);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
78*7c478bd9Sstevel@tonic-gate void
sa_kstat_init(void * unused)79*7c478bd9Sstevel@tonic-gate sa_kstat_init(void *unused)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	if ((ks_ctl = kstat_open()) == NULL)
82*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_KSTAT_OPEN), get_errstr());
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate void
sa_kstat_update(statistic_bag_t * sbag,int flags)86*7c478bd9Sstevel@tonic-gate sa_kstat_update(statistic_bag_t *sbag, int flags)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 	/* The SA_REFRESH flag forces the update of local data structures. */
89*7c478bd9Sstevel@tonic-gate 	if (flags & SA_REFRESH) {
90*7c478bd9Sstevel@tonic-gate 		int	ks_error = 0;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		if (kstat_chain_update(ks_ctl) == -1)
93*7c478bd9Sstevel@tonic-gate 			die(gettext(ERR_KSTAT_DATA), get_errstr());
94*7c478bd9Sstevel@tonic-gate 		cpu_list = update_sdata_list(cpu_list, ks_ctl, "cpu",
95*7c478bd9Sstevel@tonic-gate 							-1, "sys", &ks_error);
96*7c478bd9Sstevel@tonic-gate 		if (ks_error)
97*7c478bd9Sstevel@tonic-gate 			die(gettext(ERR_KSTAT_DATA), get_errstr());
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 		/* update info about cpu binding to processor sets	*/
100*7c478bd9Sstevel@tonic-gate 		update_cpu_list(cpu_list);
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if (strcmp(sbag->sb_type, PSET_TYPE_NAME) == 0) {
104*7c478bd9Sstevel@tonic-gate 		update_pset_stats(sbag, cpu_list);
105*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(sbag->sb_type, POOL_TYPE_NAME) == 0) {
106*7c478bd9Sstevel@tonic-gate 		return;
107*7c478bd9Sstevel@tonic-gate 	} else {
108*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_UNSUPP_STYPE), sbag->sb_type);
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate static void *
safe_kstat_data_lookup(kstat_t * ksp,char * name)114*7c478bd9Sstevel@tonic-gate safe_kstat_data_lookup(kstat_t *ksp, char *name)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	void *dp;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if ((dp = kstat_data_lookup(ksp, name)) == NULL)
119*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_KSTAT_DLOOKUP),
120*7c478bd9Sstevel@tonic-gate 			ksp->ks_name, name, get_errstr());
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	return (dp);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * Find the delta over the interval between new_ksp and old_ksp.
127*7c478bd9Sstevel@tonic-gate  * If old_ksp->ks_data is NULL and 'oz' is set then pretend
128*7c478bd9Sstevel@tonic-gate  * that old_ksp is zero otherwise return 0.
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate static uint64_t
delta(kstat_t * new_ksp,kstat_t * old_ksp,char * name,int oz)131*7c478bd9Sstevel@tonic-gate delta(kstat_t *new_ksp, kstat_t *old_ksp, char *name, int oz)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	kstat_named_t *new_ksn;
134*7c478bd9Sstevel@tonic-gate 	kstat_named_t *old_ksn;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	new_ksn = (kstat_named_t *)safe_kstat_data_lookup(new_ksp, name);
137*7c478bd9Sstevel@tonic-gate 	if (old_ksp == NULL || old_ksp->ks_data == NULL)
138*7c478bd9Sstevel@tonic-gate 		return ((oz == 1) ? new_ksn->value.ui64 : 0);
139*7c478bd9Sstevel@tonic-gate 	old_ksn = (kstat_named_t *)safe_kstat_data_lookup(old_ksp, name);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	return (new_ksn->value.ui64 - old_ksn->value.ui64);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate  * Create a clone of the passed kstat_t structure 'kstat_t'. If
146*7c478bd9Sstevel@tonic-gate  * 'fr' flag is set free the old ks_data structure in 'dst'.
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate static void
kstat_clone(kstat_t * src,kstat_t * dst,int fr)149*7c478bd9Sstevel@tonic-gate kstat_clone(kstat_t *src, kstat_t *dst, int fr)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	if (fr)
152*7c478bd9Sstevel@tonic-gate 		FREE(dst->ks_data);
153*7c478bd9Sstevel@tonic-gate 	*dst = *src;
154*7c478bd9Sstevel@tonic-gate 	if (src->ks_data != NULL) {
155*7c478bd9Sstevel@tonic-gate 		dst->ks_data = ZALLOC(src->ks_data_size);
156*7c478bd9Sstevel@tonic-gate 		(void) memcpy(dst->ks_data, src->ks_data, src->ks_data_size);
157*7c478bd9Sstevel@tonic-gate 	} else {
158*7c478bd9Sstevel@tonic-gate 		dst->ks_data = NULL;
159*7c478bd9Sstevel@tonic-gate 		dst->ks_data_size = 0;
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate /*
164*7c478bd9Sstevel@tonic-gate  * Erase the data from 'src'.
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate static void
kstat_erase(kstat_t * src)167*7c478bd9Sstevel@tonic-gate kstat_erase(kstat_t *src)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	FREE(src->ks_data);
170*7c478bd9Sstevel@tonic-gate 	(void) memset(src, 0, sizeof (*src));
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * Create a new statistic data object with its own copy of the passed
175*7c478bd9Sstevel@tonic-gate  * kstat.
176*7c478bd9Sstevel@tonic-gate  */
177*7c478bd9Sstevel@tonic-gate static sdata_t *
sdata_new(kstat_t * ksp)178*7c478bd9Sstevel@tonic-gate sdata_new(kstat_t *ksp)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	sdata_t *sdp;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	NEW0(sdp);
183*7c478bd9Sstevel@tonic-gate 	kstat_clone(ksp, &sdp->sd_nks, 0);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	return (sdp);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate static void
sdata_free(sdata_t * sdp)189*7c478bd9Sstevel@tonic-gate sdata_free(sdata_t *sdp)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	FREE(sdp->sd_oks.ks_data);
192*7c478bd9Sstevel@tonic-gate 	FREE(sdp->sd_nks.ks_data);
193*7c478bd9Sstevel@tonic-gate 	FREE(sdp->sd_udata);
194*7c478bd9Sstevel@tonic-gate 	FREE(sdp);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate /*
198*7c478bd9Sstevel@tonic-gate  * Create new or update an existing list of cpu statistics. For each
199*7c478bd9Sstevel@tonic-gate  * cpu two kstats are kept. One old kstat which contains the data from
200*7c478bd9Sstevel@tonic-gate  * the previous scan, and new with the current data. The old and the new
201*7c478bd9Sstevel@tonic-gate  * kstats *must* be for the same instance and have the same kid.
202*7c478bd9Sstevel@tonic-gate  * If 'instance' argument is set to -1 don't use it as a filter.
203*7c478bd9Sstevel@tonic-gate  */
204*7c478bd9Sstevel@tonic-gate static sdata_t *
update_sdata_list(sdata_t * list,kstat_ctl_t * kc,char * module,int instance,char * name,int * errp)205*7c478bd9Sstevel@tonic-gate update_sdata_list(sdata_t *list, kstat_ctl_t *kc, char *module,
206*7c478bd9Sstevel@tonic-gate 		int instance, char *name, int *errp)
207*7c478bd9Sstevel@tonic-gate {
208*7c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
209*7c478bd9Sstevel@tonic-gate 	sdata_t	*sdp, *sdpp; /* kstat instance pointer/previous-pointer */
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
212*7c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, module) == 0 &&
213*7c478bd9Sstevel@tonic-gate 			(name == NULL || strcmp(ksp->ks_name, name) == 0) &&
214*7c478bd9Sstevel@tonic-gate 			(instance == -1 || ksp->ks_instance == instance)) {
215*7c478bd9Sstevel@tonic-gate 			if (kstat_read(kc, ksp, NULL) == -1) {
216*7c478bd9Sstevel@tonic-gate 				*errp = -1;
217*7c478bd9Sstevel@tonic-gate 				return (list);
218*7c478bd9Sstevel@tonic-gate 			}
219*7c478bd9Sstevel@tonic-gate 			/*
220*7c478bd9Sstevel@tonic-gate 			 * Find the kstat in the existing list:
221*7c478bd9Sstevel@tonic-gate 			 * If we find one for the same instance and with the
222*7c478bd9Sstevel@tonic-gate 			 * same ks_kid we'll save it as old_kstat.
223*7c478bd9Sstevel@tonic-gate 			 * If we find one for the same instance but with a
224*7c478bd9Sstevel@tonic-gate 			 * different ks_kid we'll removed it.
225*7c478bd9Sstevel@tonic-gate 			 */
226*7c478bd9Sstevel@tonic-gate 			for (sdpp = sdp = list; sdp; sdp = sdp->sd_next) {
227*7c478bd9Sstevel@tonic-gate 				if (ksp->ks_instance ==
228*7c478bd9Sstevel@tonic-gate 						sdp->sd_nks.ks_instance) {
229*7c478bd9Sstevel@tonic-gate 					if (ksp->ks_kid == sdp->sd_nks.ks_kid) {
230*7c478bd9Sstevel@tonic-gate 						kstat_clone(&sdp->sd_nks,
231*7c478bd9Sstevel@tonic-gate 							&sdp->sd_oks, 1);
232*7c478bd9Sstevel@tonic-gate 					} else {
233*7c478bd9Sstevel@tonic-gate 						kstat_erase(&sdp->sd_oks);
234*7c478bd9Sstevel@tonic-gate 					}
235*7c478bd9Sstevel@tonic-gate 					kstat_clone(ksp, &sdp->sd_nks, 1);
236*7c478bd9Sstevel@tonic-gate 					sdp->sd_state |= SD_UPDATED;
237*7c478bd9Sstevel@tonic-gate 					break;
238*7c478bd9Sstevel@tonic-gate 				}
239*7c478bd9Sstevel@tonic-gate 				sdpp = sdp;
240*7c478bd9Sstevel@tonic-gate 			}
241*7c478bd9Sstevel@tonic-gate 			/* add a new kstat instance	*/
242*7c478bd9Sstevel@tonic-gate 			if (!sdp) {
243*7c478bd9Sstevel@tonic-gate 				/* first instance	*/
244*7c478bd9Sstevel@tonic-gate 				if (!list) {
245*7c478bd9Sstevel@tonic-gate 					list = sdata_new(ksp);
246*7c478bd9Sstevel@tonic-gate 					list->sd_state |= SD_UPDATED;
247*7c478bd9Sstevel@tonic-gate 				} else {
248*7c478bd9Sstevel@tonic-gate 					sdpp->sd_next = sdata_new(ksp);
249*7c478bd9Sstevel@tonic-gate 					sdpp->sd_next->sd_state |= SD_UPDATED;
250*7c478bd9Sstevel@tonic-gate 				}
251*7c478bd9Sstevel@tonic-gate 			}
252*7c478bd9Sstevel@tonic-gate 		}
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	/* remove untouched statistics	*/
256*7c478bd9Sstevel@tonic-gate 	sdp = list;
257*7c478bd9Sstevel@tonic-gate 	sdpp = NULL;
258*7c478bd9Sstevel@tonic-gate 	while (sdp != NULL) {
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 		if (sdp->sd_state & SD_UPDATED) {
261*7c478bd9Sstevel@tonic-gate 			sdp->sd_state &= ~SD_UPDATED;
262*7c478bd9Sstevel@tonic-gate 			sdpp = sdp;
263*7c478bd9Sstevel@tonic-gate 			sdp = sdp->sd_next;
264*7c478bd9Sstevel@tonic-gate 		} else {
265*7c478bd9Sstevel@tonic-gate 			sdata_t *tmp;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 			if (sdpp == NULL)
268*7c478bd9Sstevel@tonic-gate 				list = sdp->sd_next;
269*7c478bd9Sstevel@tonic-gate 			else
270*7c478bd9Sstevel@tonic-gate 				sdpp->sd_next = sdp->sd_next;
271*7c478bd9Sstevel@tonic-gate 			tmp = sdp->sd_next;
272*7c478bd9Sstevel@tonic-gate 			sdata_free(sdp);
273*7c478bd9Sstevel@tonic-gate 			sdp = tmp;
274*7c478bd9Sstevel@tonic-gate 		}
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	*errp = 0;
278*7c478bd9Sstevel@tonic-gate 	return (list);
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate /*
282*7c478bd9Sstevel@tonic-gate  * Update the pset assignment information for each cpu in the statistic
283*7c478bd9Sstevel@tonic-gate  * data list.
284*7c478bd9Sstevel@tonic-gate  */
285*7c478bd9Sstevel@tonic-gate static void
update_cpu_list(sdata_t * list)286*7c478bd9Sstevel@tonic-gate update_cpu_list(sdata_t *list)
287*7c478bd9Sstevel@tonic-gate {
288*7c478bd9Sstevel@tonic-gate 	sdata_t	*sdp;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	for (sdp = list; sdp; sdp = sdp->sd_next) {
291*7c478bd9Sstevel@tonic-gate 		/* for new CPU create a new user data object	*/
292*7c478bd9Sstevel@tonic-gate 		if (sdp->sd_udata == NULL) {
293*7c478bd9Sstevel@tonic-gate 			sdp->sd_udata = ZALLOC(sizeof (pset_ud_t));
294*7c478bd9Sstevel@tonic-gate 			/*
295*7c478bd9Sstevel@tonic-gate 			 * set its pset to invalid, so it will not be
296*7c478bd9Sstevel@tonic-gate 			 * used in statistics calculation.
297*7c478bd9Sstevel@tonic-gate 			 */
298*7c478bd9Sstevel@tonic-gate 			SD_NPSET(sdp) = INVALID_SET;
299*7c478bd9Sstevel@tonic-gate 		}
300*7c478bd9Sstevel@tonic-gate 		/* copy the pset assignment information to the previous stat */
301*7c478bd9Sstevel@tonic-gate 		SD_OPSET(sdp) = SD_NPSET(sdp);
302*7c478bd9Sstevel@tonic-gate 		/* set the current assignment	*/
303*7c478bd9Sstevel@tonic-gate 		if (pset_assign(PS_QUERY, sdp->sd_nks.ks_instance,
304*7c478bd9Sstevel@tonic-gate 			&(SD_NPSET(sdp))) == -1)
305*7c478bd9Sstevel@tonic-gate 			SD_NPSET(sdp) = INVALID_SET;
306*7c478bd9Sstevel@tonic-gate 	}
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate  * Update statistic data for pset. Calculate the CPU usage in a pset.
311*7c478bd9Sstevel@tonic-gate  */
312*7c478bd9Sstevel@tonic-gate static void
update_pset_stats(statistic_bag_t * sbag,sdata_t * list)313*7c478bd9Sstevel@tonic-gate update_pset_stats(statistic_bag_t *sbag, sdata_t *list)
314*7c478bd9Sstevel@tonic-gate {
315*7c478bd9Sstevel@tonic-gate 	sdata_t	*sdp;
316*7c478bd9Sstevel@tonic-gate 	pset_statistic_bag_t *bag = (pset_statistic_bag_t *)sbag->bag;
317*7c478bd9Sstevel@tonic-gate 	uint64_t allticks, ust, kst, ist, wst;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	ust = kst = ist = wst = 0;
320*7c478bd9Sstevel@tonic-gate 	for (sdp = list; sdp; sdp = sdp->sd_next) {
321*7c478bd9Sstevel@tonic-gate 		/*
322*7c478bd9Sstevel@tonic-gate 		 * only calculate for the asked pset id and if the cpu belongs
323*7c478bd9Sstevel@tonic-gate 		 * to the same set in the previous and in the current snapshot.
324*7c478bd9Sstevel@tonic-gate 		 * It means that the usage for CPUs that were rebound during
325*7c478bd9Sstevel@tonic-gate 		 * the sampling interval are not charged to any set.
326*7c478bd9Sstevel@tonic-gate 		 */
327*7c478bd9Sstevel@tonic-gate 		if ((SD_OPSET(sdp) == SD_NPSET(sdp)) &&
328*7c478bd9Sstevel@tonic-gate 			(SD_NPSET(sdp) == bag->pset_sb_sysid)) {
329*7c478bd9Sstevel@tonic-gate 			ust += delta(&sdp->sd_nks, &sdp->sd_oks,
330*7c478bd9Sstevel@tonic-gate 				"cpu_ticks_user", 0);
331*7c478bd9Sstevel@tonic-gate 			kst += delta(&sdp->sd_nks, &sdp->sd_oks,
332*7c478bd9Sstevel@tonic-gate 				"cpu_ticks_kernel", 0);
333*7c478bd9Sstevel@tonic-gate 			ist += delta(&sdp->sd_nks, &sdp->sd_oks,
334*7c478bd9Sstevel@tonic-gate 				"cpu_ticks_idle", 0);
335*7c478bd9Sstevel@tonic-gate 			wst += delta(&sdp->sd_nks, &sdp->sd_oks,
336*7c478bd9Sstevel@tonic-gate 				"cpu_ticks_wait", 0);
337*7c478bd9Sstevel@tonic-gate 		}
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	if ((allticks = ust + kst + wst + ist) != 0) {
341*7c478bd9Sstevel@tonic-gate 		bag->pset_sb_used =
342*7c478bd9Sstevel@tonic-gate 			(double)(ust + kst) / allticks * bag->pset_sb_size;
343*7c478bd9Sstevel@tonic-gate 	} else {
344*7c478bd9Sstevel@tonic-gate 		bag->pset_sb_used = 0.0;
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate }
347