xref: /illumos-gate/usr/src/cmd/cpc/common/setgrp.c (revision 2a8bcb4e)
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 #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <strings.h>
31*7c478bd9Sstevel@tonic-gate #include <libintl.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <libcpc.h>
36*7c478bd9Sstevel@tonic-gate #include "cpucmds.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	CHARS_PER_REQ 11	/* space required for printing column headers */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * These routines are solely used to manage a list of request sets.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate struct __cpc_setgrp {
45*7c478bd9Sstevel@tonic-gate 	struct setgrp_elem {
46*7c478bd9Sstevel@tonic-gate 		cpc_set_t	*set;
47*7c478bd9Sstevel@tonic-gate 		uint8_t		sysonly;	/* All reqs sys-mode only ? */
48*7c478bd9Sstevel@tonic-gate 		int		nreqs;
49*7c478bd9Sstevel@tonic-gate 		int		*picnums;	/* picnum used per req */
50*7c478bd9Sstevel@tonic-gate 		cpc_buf_t	*data1;
51*7c478bd9Sstevel@tonic-gate 		cpc_buf_t	*data2;
52*7c478bd9Sstevel@tonic-gate 		cpc_buf_t	*scratch;
53*7c478bd9Sstevel@tonic-gate 		char *name;
54*7c478bd9Sstevel@tonic-gate 		char *hdr;
55*7c478bd9Sstevel@tonic-gate 	} *sets;		/* array of events and names */
56*7c478bd9Sstevel@tonic-gate 	int nelem;		/* size of array */
57*7c478bd9Sstevel@tonic-gate 	int current;		/* currently bound event in eventset */
58*7c478bd9Sstevel@tonic-gate 	int smt;		/* Measures physical events on SMT CPU */
59*7c478bd9Sstevel@tonic-gate 	int has_sysonly_set;	/* Does this group have a system-only set? */
60*7c478bd9Sstevel@tonic-gate 	cpc_t *cpc;		/* library handle */
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static void *emalloc(size_t n);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate cpc_setgrp_t *
cpc_setgrp_new(cpc_t * cpc,int smt)66*7c478bd9Sstevel@tonic-gate cpc_setgrp_new(cpc_t *cpc, int smt)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	sgrp = emalloc(sizeof (*sgrp));
71*7c478bd9Sstevel@tonic-gate 	sgrp->current = -1;
72*7c478bd9Sstevel@tonic-gate 	sgrp->cpc = cpc;
73*7c478bd9Sstevel@tonic-gate 	sgrp->smt = smt;
74*7c478bd9Sstevel@tonic-gate 	sgrp->has_sysonly_set = 0;
75*7c478bd9Sstevel@tonic-gate 	return (sgrp);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * Walker to count the number of requests in a set, and check if any requests
80*7c478bd9Sstevel@tonic-gate  * count user-mode events.
81*7c478bd9Sstevel@tonic-gate  */
82*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
83*7c478bd9Sstevel@tonic-gate static void
cpc_setgrp_walker(void * arg,int index,const char * event,uint64_t preset,uint_t flags,int nattrs,const cpc_attr_t * attrs)84*7c478bd9Sstevel@tonic-gate cpc_setgrp_walker(void *arg, int index, const char *event, uint64_t preset,
85*7c478bd9Sstevel@tonic-gate     uint_t flags, int nattrs, const cpc_attr_t *attrs)
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	struct setgrp_elem *se = arg;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	se->nreqs++;
90*7c478bd9Sstevel@tonic-gate 	if (flags & CPC_COUNT_USER)
91*7c478bd9Sstevel@tonic-gate 		se->sysonly = 0;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * Walker to discover the picnums used by the requests in a set.
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
98*7c478bd9Sstevel@tonic-gate static void
cpc_setgrp_picwalker(void * arg,int index,const char * event,uint64_t preset,uint_t flags,int nattrs,const cpc_attr_t * attrs)99*7c478bd9Sstevel@tonic-gate cpc_setgrp_picwalker(void *arg, int index, const char *event, uint64_t preset,
100*7c478bd9Sstevel@tonic-gate     uint_t flags, int nattrs, const cpc_attr_t *attrs)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	int *picnums = arg;
103*7c478bd9Sstevel@tonic-gate 	int i;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nattrs; i++) {
106*7c478bd9Sstevel@tonic-gate 		if (strncmp(attrs[i].ca_name, "picnum", 7) == 0)
107*7c478bd9Sstevel@tonic-gate 			break;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	if (i == nattrs)
110*7c478bd9Sstevel@tonic-gate 		picnums[index] = -1;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	picnums[index] = (int)attrs[i].ca_val;
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate cpc_setgrp_t *
cpc_setgrp_newset(cpc_setgrp_t * sgrp,const char * spec,int * errcnt)116*7c478bd9Sstevel@tonic-gate cpc_setgrp_newset(cpc_setgrp_t *sgrp, const char *spec, int *errcnt)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	cpc_set_t		*set;
119*7c478bd9Sstevel@tonic-gate 	struct setgrp_elem	*new;
120*7c478bd9Sstevel@tonic-gate 	char			hdr[CHARS_PER_REQ+1];
121*7c478bd9Sstevel@tonic-gate 	int			i;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if ((set = cpc_strtoset(sgrp->cpc, spec, sgrp->smt)) == NULL) {
124*7c478bd9Sstevel@tonic-gate 		*errcnt += 1;
125*7c478bd9Sstevel@tonic-gate 		return (NULL);
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if ((new = realloc(sgrp->sets, (1 + sgrp->nelem) * sizeof (*new)))
129*7c478bd9Sstevel@tonic-gate 	    == NULL) {
130*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
131*7c478bd9Sstevel@tonic-gate 		    gettext("cpc_setgrp: no re memory available\n"));
132*7c478bd9Sstevel@tonic-gate 		exit(0);
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	sgrp->sets = new;
136*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].set = set;
137*7c478bd9Sstevel@tonic-gate 	/*
138*7c478bd9Sstevel@tonic-gate 	 * Count the number of requests in the set we just made. If any requests
139*7c478bd9Sstevel@tonic-gate 	 * in the set have CPC_COUNT_USER in the flags, the sysonly flag will
140*7c478bd9Sstevel@tonic-gate 	 * be cleared.
141*7c478bd9Sstevel@tonic-gate 	 */
142*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].nreqs = 0;
143*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].sysonly = 1;
144*7c478bd9Sstevel@tonic-gate 	cpc_walk_requests(sgrp->cpc, set, &(sgrp->sets[sgrp->nelem]),
145*7c478bd9Sstevel@tonic-gate 	    cpc_setgrp_walker);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if (sgrp->sets[sgrp->nelem].sysonly == 1)
148*7c478bd9Sstevel@tonic-gate 		sgrp->has_sysonly_set = 1;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].picnums = emalloc(sgrp->sets[sgrp->nelem].nreqs
151*7c478bd9Sstevel@tonic-gate 	    * sizeof (int));
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].hdr = emalloc((sgrp->sets[sgrp->nelem].nreqs *
154*7c478bd9Sstevel@tonic-gate 	    CHARS_PER_REQ) + 1);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/*
157*7c478bd9Sstevel@tonic-gate 	 * Find out which picnums the requests are using.
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	cpc_walk_requests(sgrp->cpc, set, sgrp->sets[sgrp->nelem].picnums,
160*7c478bd9Sstevel@tonic-gate 	    cpc_setgrp_picwalker);
161*7c478bd9Sstevel@tonic-gate 	/*
162*7c478bd9Sstevel@tonic-gate 	 * Use the picnums we discovered to build a printable header for this
163*7c478bd9Sstevel@tonic-gate 	 * set.
164*7c478bd9Sstevel@tonic-gate 	 */
165*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].hdr[0] = '\0';
166*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sgrp->sets[sgrp->nelem].nreqs; i++) {
167*7c478bd9Sstevel@tonic-gate 		(void) snprintf(hdr, CHARS_PER_REQ, "%8s%-2d ", "pic",
168*7c478bd9Sstevel@tonic-gate 		    sgrp->sets[sgrp->nelem].picnums[i]);
169*7c478bd9Sstevel@tonic-gate 		(void) strncat(sgrp->sets[sgrp->nelem].hdr, hdr,
170*7c478bd9Sstevel@tonic-gate 		    sgrp->sets[sgrp->nelem].nreqs * CHARS_PER_REQ);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	sgrp->sets[sgrp->nelem].hdr[strlen(sgrp->sets[sgrp->nelem].hdr)] = '\0';
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if ((sgrp->sets[sgrp->nelem].name = strdup(spec)) == NULL) {
175*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
176*7c478bd9Sstevel@tonic-gate 		    gettext("cpc_setgrp: no memory available\n"));
177*7c478bd9Sstevel@tonic-gate 		exit(0);
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if ((sgrp->sets[sgrp->nelem].data1 = cpc_buf_create(sgrp->cpc, set))
181*7c478bd9Sstevel@tonic-gate 	    == NULL ||
182*7c478bd9Sstevel@tonic-gate 	    (sgrp->sets[sgrp->nelem].data2 = cpc_buf_create(sgrp->cpc, set))
183*7c478bd9Sstevel@tonic-gate 	    == NULL ||
184*7c478bd9Sstevel@tonic-gate 	    (sgrp->sets[sgrp->nelem].scratch = cpc_buf_create(sgrp->cpc, set))
185*7c478bd9Sstevel@tonic-gate 	    == NULL) {
186*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
187*7c478bd9Sstevel@tonic-gate 		    gettext("cpc_setgrp: no memory available\n"));
188*7c478bd9Sstevel@tonic-gate 		exit(0);
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	if (sgrp->current < 0)
192*7c478bd9Sstevel@tonic-gate 		sgrp->current = 0;
193*7c478bd9Sstevel@tonic-gate 	sgrp->nelem++;
194*7c478bd9Sstevel@tonic-gate 	return (sgrp);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate int
cpc_setgrp_getbufs(cpc_setgrp_t * sgrp,cpc_buf_t *** data1,cpc_buf_t *** data2,cpc_buf_t *** scratch)198*7c478bd9Sstevel@tonic-gate cpc_setgrp_getbufs(cpc_setgrp_t *sgrp, cpc_buf_t ***data1, cpc_buf_t ***data2,
199*7c478bd9Sstevel@tonic-gate     cpc_buf_t ***scratch)
200*7c478bd9Sstevel@tonic-gate {
201*7c478bd9Sstevel@tonic-gate 	if ((uint_t)sgrp->current >= sgrp->nelem)
202*7c478bd9Sstevel@tonic-gate 		return (-1);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	*data1   = &(sgrp->sets[sgrp->current].data1);
205*7c478bd9Sstevel@tonic-gate 	*data2   = &(sgrp->sets[sgrp->current].data2);
206*7c478bd9Sstevel@tonic-gate 	*scratch = &(sgrp->sets[sgrp->current].scratch);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	return (sgrp->sets[sgrp->current].nreqs);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate cpc_setgrp_t *
cpc_setgrp_clone(cpc_setgrp_t * old)212*7c478bd9Sstevel@tonic-gate cpc_setgrp_clone(cpc_setgrp_t *old)
213*7c478bd9Sstevel@tonic-gate {
214*7c478bd9Sstevel@tonic-gate 	int			i;
215*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t		*new;
216*7c478bd9Sstevel@tonic-gate 	struct setgrp_elem	*newa;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	new = emalloc(sizeof (*new));
219*7c478bd9Sstevel@tonic-gate 	newa = emalloc(old->nelem * sizeof (*newa));
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	new->nelem = old->nelem;
222*7c478bd9Sstevel@tonic-gate 	new->current = old->current;
223*7c478bd9Sstevel@tonic-gate 	new->cpc = old->cpc;
224*7c478bd9Sstevel@tonic-gate 	new->sets = newa;
225*7c478bd9Sstevel@tonic-gate 	new->smt = old->smt;
226*7c478bd9Sstevel@tonic-gate 	new->has_sysonly_set = old->has_sysonly_set;
227*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < old->nelem; i++) {
228*7c478bd9Sstevel@tonic-gate 		if ((newa[i].set = cpc_strtoset(old->cpc, old->sets[i].name,
229*7c478bd9Sstevel@tonic-gate 		    old->smt)) == NULL) {
230*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
231*7c478bd9Sstevel@tonic-gate 			    gettext("cpc_setgrp: cpc_strtoset() failed\n"));
232*7c478bd9Sstevel@tonic-gate 			exit(0);
233*7c478bd9Sstevel@tonic-gate 		}
234*7c478bd9Sstevel@tonic-gate 		if ((newa[i].name = strdup(old->sets[i].name)) == NULL) {
235*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
236*7c478bd9Sstevel@tonic-gate 			    gettext("cpc_setgrp: no memory available\n"));
237*7c478bd9Sstevel@tonic-gate 			exit(0);
238*7c478bd9Sstevel@tonic-gate 		}
239*7c478bd9Sstevel@tonic-gate 		newa[i].sysonly = old->sets[i].sysonly;
240*7c478bd9Sstevel@tonic-gate 		newa[i].nreqs = old->sets[i].nreqs;
241*7c478bd9Sstevel@tonic-gate 		newa[i].data1 = cpc_buf_create(old->cpc, newa[i].set);
242*7c478bd9Sstevel@tonic-gate 		newa[i].data2 = cpc_buf_create(old->cpc, newa[i].set);
243*7c478bd9Sstevel@tonic-gate 		newa[i].scratch = cpc_buf_create(old->cpc, newa[i].set);
244*7c478bd9Sstevel@tonic-gate 		if (newa[i].data1 == NULL || newa[i].data2 == NULL ||
245*7c478bd9Sstevel@tonic-gate 		    newa[i].scratch == NULL) {
246*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
247*7c478bd9Sstevel@tonic-gate 			    gettext("cpc_setgrp: no memory available\n"));
248*7c478bd9Sstevel@tonic-gate 			exit(0);
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 		cpc_buf_copy(old->cpc, newa[i].data1, old->sets[i].data1);
251*7c478bd9Sstevel@tonic-gate 		cpc_buf_copy(old->cpc, newa[i].data2, old->sets[i].data2);
252*7c478bd9Sstevel@tonic-gate 		cpc_buf_copy(old->cpc, newa[i].scratch, old->sets[i].scratch);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 	return (new);
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate static void
cpc_setgrp_delset(cpc_setgrp_t * sgrp)258*7c478bd9Sstevel@tonic-gate cpc_setgrp_delset(cpc_setgrp_t *sgrp)
259*7c478bd9Sstevel@tonic-gate {
260*7c478bd9Sstevel@tonic-gate 	int l;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if ((uint_t)sgrp->current >= sgrp->nelem)
263*7c478bd9Sstevel@tonic-gate 		sgrp->current = sgrp->nelem - 1;
264*7c478bd9Sstevel@tonic-gate 	if (sgrp->current < 0)
265*7c478bd9Sstevel@tonic-gate 		return;
266*7c478bd9Sstevel@tonic-gate 	free(sgrp->sets[sgrp->current].name);
267*7c478bd9Sstevel@tonic-gate 	free(sgrp->sets[sgrp->current].hdr);
268*7c478bd9Sstevel@tonic-gate 	free(sgrp->sets[sgrp->current].picnums);
269*7c478bd9Sstevel@tonic-gate 	(void) cpc_buf_destroy(sgrp->cpc, sgrp->sets[sgrp->current].data1);
270*7c478bd9Sstevel@tonic-gate 	(void) cpc_buf_destroy(sgrp->cpc, sgrp->sets[sgrp->current].data2);
271*7c478bd9Sstevel@tonic-gate 	(void) cpc_buf_destroy(sgrp->cpc, sgrp->sets[sgrp->current].scratch);
272*7c478bd9Sstevel@tonic-gate 	for (l = sgrp->current; l < sgrp->nelem - 1; l++)
273*7c478bd9Sstevel@tonic-gate 		sgrp->sets[l] = sgrp->sets[l + 1];
274*7c478bd9Sstevel@tonic-gate 	sgrp->nelem--;
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate void
cpc_setgrp_free(cpc_setgrp_t * sgrp)278*7c478bd9Sstevel@tonic-gate cpc_setgrp_free(cpc_setgrp_t *sgrp)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	if (sgrp->sets) {
281*7c478bd9Sstevel@tonic-gate 		while (sgrp->nelem)
282*7c478bd9Sstevel@tonic-gate 			cpc_setgrp_delset(sgrp);
283*7c478bd9Sstevel@tonic-gate 		free(sgrp->sets);
284*7c478bd9Sstevel@tonic-gate 	}
285*7c478bd9Sstevel@tonic-gate 	free(sgrp);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate cpc_set_t *
cpc_setgrp_getset(cpc_setgrp_t * sgrp)289*7c478bd9Sstevel@tonic-gate cpc_setgrp_getset(cpc_setgrp_t *sgrp)
290*7c478bd9Sstevel@tonic-gate {
291*7c478bd9Sstevel@tonic-gate 	if ((uint_t)sgrp->current >= sgrp->nelem)
292*7c478bd9Sstevel@tonic-gate 		return (NULL);
293*7c478bd9Sstevel@tonic-gate 	return (sgrp->sets[sgrp->current].set);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate const char *
cpc_setgrp_getname(cpc_setgrp_t * sgrp)297*7c478bd9Sstevel@tonic-gate cpc_setgrp_getname(cpc_setgrp_t *sgrp)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate 	if ((uint_t)sgrp->current >= sgrp->nelem)
300*7c478bd9Sstevel@tonic-gate 		return (NULL);
301*7c478bd9Sstevel@tonic-gate 	return (sgrp->sets[sgrp->current].name);
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate const char *
cpc_setgrp_gethdr(cpc_setgrp_t * sgrp)305*7c478bd9Sstevel@tonic-gate cpc_setgrp_gethdr(cpc_setgrp_t *sgrp)
306*7c478bd9Sstevel@tonic-gate {
307*7c478bd9Sstevel@tonic-gate 	if ((uint_t)sgrp->current >= sgrp->nelem)
308*7c478bd9Sstevel@tonic-gate 		return (NULL);
309*7c478bd9Sstevel@tonic-gate 	return (sgrp->sets[sgrp->current].hdr);
310*7c478bd9Sstevel@tonic-gate }
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate int
cpc_setgrp_numsets(cpc_setgrp_t * sgrp)313*7c478bd9Sstevel@tonic-gate cpc_setgrp_numsets(cpc_setgrp_t *sgrp)
314*7c478bd9Sstevel@tonic-gate {
315*7c478bd9Sstevel@tonic-gate 	return (sgrp->nelem);
316*7c478bd9Sstevel@tonic-gate }
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate cpc_set_t *
cpc_setgrp_nextset(cpc_setgrp_t * sgrp)319*7c478bd9Sstevel@tonic-gate cpc_setgrp_nextset(cpc_setgrp_t *sgrp)
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	if (sgrp->current < 0)
322*7c478bd9Sstevel@tonic-gate 		return (NULL);
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	if (++sgrp->current >= sgrp->nelem)
325*7c478bd9Sstevel@tonic-gate 		sgrp->current = 0;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	return (cpc_setgrp_getset(sgrp));
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate /*
331*7c478bd9Sstevel@tonic-gate  * Put the setgrp pointer back to the beginning of the set
332*7c478bd9Sstevel@tonic-gate  */
333*7c478bd9Sstevel@tonic-gate void
cpc_setgrp_reset(cpc_setgrp_t * sgrp)334*7c478bd9Sstevel@tonic-gate cpc_setgrp_reset(cpc_setgrp_t *sgrp)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	if (sgrp->current > 0)
337*7c478bd9Sstevel@tonic-gate 		sgrp->current = 0;
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate /*
341*7c478bd9Sstevel@tonic-gate  * Adds the data from the 'data1' buf into the accum setgrp.
342*7c478bd9Sstevel@tonic-gate  */
343*7c478bd9Sstevel@tonic-gate void
cpc_setgrp_accum(cpc_setgrp_t * accum,cpc_setgrp_t * sgrp)344*7c478bd9Sstevel@tonic-gate cpc_setgrp_accum(cpc_setgrp_t *accum, cpc_setgrp_t *sgrp)
345*7c478bd9Sstevel@tonic-gate {
346*7c478bd9Sstevel@tonic-gate 	int i;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(accum);
349*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(sgrp);
350*7c478bd9Sstevel@tonic-gate 	if (accum->nelem != sgrp->nelem)
351*7c478bd9Sstevel@tonic-gate 		return;
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sgrp->nelem; i++) {
354*7c478bd9Sstevel@tonic-gate 		if (accum->sets[i].nreqs != sgrp->sets[i].nreqs)
355*7c478bd9Sstevel@tonic-gate 			return;
356*7c478bd9Sstevel@tonic-gate 		cpc_buf_add(sgrp->cpc, accum->sets[i].data1,
357*7c478bd9Sstevel@tonic-gate 		    accum->sets[i].data1, sgrp->sets[i].data1);
358*7c478bd9Sstevel@tonic-gate 	}
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate /*
362*7c478bd9Sstevel@tonic-gate  * Returns 1 if all requests in the current set count only system-mode events.
363*7c478bd9Sstevel@tonic-gate  */
364*7c478bd9Sstevel@tonic-gate int
cpc_setgrp_sysonly(cpc_setgrp_t * sgrp)365*7c478bd9Sstevel@tonic-gate cpc_setgrp_sysonly(cpc_setgrp_t *sgrp)
366*7c478bd9Sstevel@tonic-gate {
367*7c478bd9Sstevel@tonic-gate 	return ((int)sgrp->sets[sgrp->current].sysonly);
368*7c478bd9Sstevel@tonic-gate }
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate /*
371*7c478bd9Sstevel@tonic-gate  * Returns 1 if any set in the group is a system-mode-only set.
372*7c478bd9Sstevel@tonic-gate  */
373*7c478bd9Sstevel@tonic-gate int
cpc_setgrp_has_sysonly(cpc_setgrp_t * sgrp)374*7c478bd9Sstevel@tonic-gate cpc_setgrp_has_sysonly(cpc_setgrp_t *sgrp)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	return (sgrp->has_sysonly_set);
377*7c478bd9Sstevel@tonic-gate }
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate /*
380*7c478bd9Sstevel@tonic-gate  * If we ever fail to get memory, we print an error message and exit.
381*7c478bd9Sstevel@tonic-gate  */
382*7c478bd9Sstevel@tonic-gate static void *
emalloc(size_t n)383*7c478bd9Sstevel@tonic-gate emalloc(size_t n)
384*7c478bd9Sstevel@tonic-gate {
385*7c478bd9Sstevel@tonic-gate 	/*
386*7c478bd9Sstevel@tonic-gate 	 * Several callers of this routine need zero-filled buffers.
387*7c478bd9Sstevel@tonic-gate 	 */
388*7c478bd9Sstevel@tonic-gate 	void *p = calloc(1, n);
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
391*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
392*7c478bd9Sstevel@tonic-gate 		    gettext("cpc_setgrp: no memory available\n"));
393*7c478bd9Sstevel@tonic-gate 		exit(0);
394*7c478bd9Sstevel@tonic-gate 	}
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	return (p);
397*7c478bd9Sstevel@tonic-gate }
398