1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <strings.h>
31*0Sstevel@tonic-gate #include <errno.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <dt_impl.h>
34*0Sstevel@tonic-gate #include <assert.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #define	DTRACE_AHASHSIZE	32779		/* big 'ol prime */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static void
39*0Sstevel@tonic-gate dt_aggregate_count(uint64_t *existing, uint64_t *new, size_t size)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	int i;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	for (i = 0; i < size / sizeof (uint64_t); i++)
44*0Sstevel@tonic-gate 		existing[i] = existing[i] + new[i];
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static int
48*0Sstevel@tonic-gate dt_aggregate_countcmp(uint64_t *lhs, uint64_t *rhs)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	uint64_t lvar = *lhs;
51*0Sstevel@tonic-gate 	uint64_t rvar = *rhs;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	if (lvar > rvar)
54*0Sstevel@tonic-gate 		return (1);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	if (lvar < rvar)
57*0Sstevel@tonic-gate 		return (-1);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	return (0);
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*ARGSUSED*/
63*0Sstevel@tonic-gate static void
64*0Sstevel@tonic-gate dt_aggregate_min(uint64_t *existing, uint64_t *new, size_t size)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate 	if (*new < *existing)
67*0Sstevel@tonic-gate 		*existing = *new;
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate /*ARGSUSED*/
71*0Sstevel@tonic-gate static void
72*0Sstevel@tonic-gate dt_aggregate_max(uint64_t *existing, uint64_t *new, size_t size)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	if (*new > *existing)
75*0Sstevel@tonic-gate 		*existing = *new;
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static int
79*0Sstevel@tonic-gate dt_aggregate_averagecmp(uint64_t *lhs, uint64_t *rhs)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	uint64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
82*0Sstevel@tonic-gate 	uint64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	if (lavg > ravg)
85*0Sstevel@tonic-gate 		return (1);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	if (lavg < ravg)
88*0Sstevel@tonic-gate 		return (-1);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	return (0);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /*ARGSUSED*/
94*0Sstevel@tonic-gate static void
95*0Sstevel@tonic-gate dt_aggregate_lquantize(uint64_t *existing, uint64_t *new, size_t size)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	uint64_t arg = *existing++;
98*0Sstevel@tonic-gate 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
99*0Sstevel@tonic-gate 	int i;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	for (i = 0; i <= levels + 1; i++)
102*0Sstevel@tonic-gate 		existing[i] = existing[i] + new[i + 1];
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate static int64_t
106*0Sstevel@tonic-gate dt_aggregate_lquantizedsum(uint64_t *lquanta)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	uint64_t arg = *lquanta++;
109*0Sstevel@tonic-gate 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
110*0Sstevel@tonic-gate 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
111*0Sstevel@tonic-gate 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
112*0Sstevel@tonic-gate 	int64_t total = lquanta[0] * (base - 1);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	for (i = 0; i < levels; base += step, i++)
115*0Sstevel@tonic-gate 		total += lquanta[i + 1] * base;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (total + lquanta[levels + 1] * (base + 1));
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static int
121*0Sstevel@tonic-gate dt_aggregate_lquantizedcmp(uint64_t *lhs, uint64_t *rhs)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	int64_t lsum = dt_aggregate_lquantizedsum(lhs);
124*0Sstevel@tonic-gate 	int64_t rsum = dt_aggregate_lquantizedsum(rhs);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (lsum > rsum)
127*0Sstevel@tonic-gate 		return (1);
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (lsum < rsum)
130*0Sstevel@tonic-gate 		return (-1);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	return (0);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate static int
136*0Sstevel@tonic-gate dt_aggregate_quantizedcmp(uint64_t *lhs, uint64_t *rhs)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	int nbuckets = DTRACE_QUANTIZE_NBUCKETS, i;
139*0Sstevel@tonic-gate 	int64_t ltotal = 0, rtotal = 0;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	for (i = 0; i < nbuckets; i++) {
142*0Sstevel@tonic-gate 		int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		ltotal += bucketval * lhs[i];
145*0Sstevel@tonic-gate 		rtotal += bucketval * rhs[i];
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (ltotal > rtotal)
149*0Sstevel@tonic-gate 		return (1);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (ltotal < rtotal)
152*0Sstevel@tonic-gate 		return (-1);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	return (0);
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate static int
158*0Sstevel@tonic-gate dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	dtrace_epid_t id;
161*0Sstevel@tonic-gate 	uint64_t hashval;
162*0Sstevel@tonic-gate 	size_t offs, roffs, size, ndx;
163*0Sstevel@tonic-gate 	int i, j, rval;
164*0Sstevel@tonic-gate 	caddr_t addr, data;
165*0Sstevel@tonic-gate 	dtrace_recdesc_t *rec;
166*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
167*0Sstevel@tonic-gate 	dtrace_aggdesc_t *agg;
168*0Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
169*0Sstevel@tonic-gate 	dt_ahashent_t *h;
170*0Sstevel@tonic-gate 	dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
171*0Sstevel@tonic-gate 	dtrace_aggdata_t *aggdata;
172*0Sstevel@tonic-gate 	int flags = agp->dtat_flags;
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	buf->dtbd_cpu = cpu;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
177*0Sstevel@tonic-gate 		if (errno == ENOENT) {
178*0Sstevel@tonic-gate 			/*
179*0Sstevel@tonic-gate 			 * If that failed with ENOENT, it may be because the
180*0Sstevel@tonic-gate 			 * CPU was unconfigured.  This is okay; we'll just
181*0Sstevel@tonic-gate 			 * do nothing but return success.
182*0Sstevel@tonic-gate 			 */
183*0Sstevel@tonic-gate 			return (0);
184*0Sstevel@tonic-gate 		}
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
187*0Sstevel@tonic-gate 	}
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (buf->dtbd_drops != 0) {
190*0Sstevel@tonic-gate 		if (dt_handle_cpudrop(dtp, cpu,
191*0Sstevel@tonic-gate 		    DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1)
192*0Sstevel@tonic-gate 			return (-1);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if (buf->dtbd_size == 0)
196*0Sstevel@tonic-gate 		return (0);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	if (hash->dtah_hash == NULL) {
199*0Sstevel@tonic-gate 		size_t size;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		hash->dtah_size = DTRACE_AHASHSIZE;
202*0Sstevel@tonic-gate 		size = hash->dtah_size * sizeof (dt_ahashent_t *);
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		if ((hash->dtah_hash = malloc(size)) == NULL)
205*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 		bzero(hash->dtah_hash, size);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	for (offs = 0; offs < buf->dtbd_size; ) {
211*0Sstevel@tonic-gate 		/*
212*0Sstevel@tonic-gate 		 * We're guaranteed to have an ID.
213*0Sstevel@tonic-gate 		 */
214*0Sstevel@tonic-gate 		id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
215*0Sstevel@tonic-gate 		    (uintptr_t)offs));
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 		if (id == DTRACE_AGGIDNONE) {
218*0Sstevel@tonic-gate 			/*
219*0Sstevel@tonic-gate 			 * This is filler to assure proper alignment of the
220*0Sstevel@tonic-gate 			 * next record; we simply ignore it.
221*0Sstevel@tonic-gate 			 */
222*0Sstevel@tonic-gate 			offs += sizeof (id);
223*0Sstevel@tonic-gate 			continue;
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 		if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
227*0Sstevel@tonic-gate 			return (rval);
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 		addr = buf->dtbd_data + offs;
230*0Sstevel@tonic-gate 		size = agg->dtagd_size;
231*0Sstevel@tonic-gate 		hashval = 0;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 		for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
234*0Sstevel@tonic-gate 			rec = &agg->dtagd_rec[j];
235*0Sstevel@tonic-gate 			roffs = rec->dtrd_offset;
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 			for (i = 0; i < rec->dtrd_size; i++)
238*0Sstevel@tonic-gate 				hashval += addr[roffs + i];
239*0Sstevel@tonic-gate 		}
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 		ndx = hashval % hash->dtah_size;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 		for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
244*0Sstevel@tonic-gate 			if (h->dtahe_hashval != hashval)
245*0Sstevel@tonic-gate 				continue;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 			if (h->dtahe_size != size)
248*0Sstevel@tonic-gate 				continue;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 			aggdata = &h->dtahe_data;
251*0Sstevel@tonic-gate 			data = aggdata->dtada_data;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 			for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
254*0Sstevel@tonic-gate 				rec = &agg->dtagd_rec[j];
255*0Sstevel@tonic-gate 				roffs = rec->dtrd_offset;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 				for (i = 0; i < rec->dtrd_size; i++)
258*0Sstevel@tonic-gate 					if (addr[roffs + i] != data[roffs + i])
259*0Sstevel@tonic-gate 						goto hashnext;
260*0Sstevel@tonic-gate 			}
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 			/*
263*0Sstevel@tonic-gate 			 * We found it.  Now we need to apply the aggregating
264*0Sstevel@tonic-gate 			 * action on the data here.
265*0Sstevel@tonic-gate 			 */
266*0Sstevel@tonic-gate 			rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
267*0Sstevel@tonic-gate 			roffs = rec->dtrd_offset;
268*0Sstevel@tonic-gate 			/* LINTED - alignment */
269*0Sstevel@tonic-gate 			h->dtahe_aggregate((uint64_t *)&data[roffs],
270*0Sstevel@tonic-gate 			    /* LINTED - alignment */
271*0Sstevel@tonic-gate 			    (uint64_t *)&addr[roffs], rec->dtrd_size);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 			/*
274*0Sstevel@tonic-gate 			 * If we're keeping per CPU data, apply the aggregating
275*0Sstevel@tonic-gate 			 * action there as well.
276*0Sstevel@tonic-gate 			 */
277*0Sstevel@tonic-gate 			if (aggdata->dtada_percpu != NULL) {
278*0Sstevel@tonic-gate 				data = aggdata->dtada_percpu[cpu];
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 				/* LINTED - alignment */
281*0Sstevel@tonic-gate 				h->dtahe_aggregate((uint64_t *)data,
282*0Sstevel@tonic-gate 				    /* LINTED - alignment */
283*0Sstevel@tonic-gate 				    (uint64_t *)&addr[roffs], rec->dtrd_size);
284*0Sstevel@tonic-gate 			}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 			goto bufnext;
287*0Sstevel@tonic-gate hashnext:
288*0Sstevel@tonic-gate 			continue;
289*0Sstevel@tonic-gate 		}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 		/*
292*0Sstevel@tonic-gate 		 * If we're here, we couldn't find an entry for this record.
293*0Sstevel@tonic-gate 		 */
294*0Sstevel@tonic-gate 		if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
295*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
296*0Sstevel@tonic-gate 		bzero(h, sizeof (dt_ahashent_t));
297*0Sstevel@tonic-gate 		aggdata = &h->dtahe_data;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 		if ((aggdata->dtada_data = malloc(size)) == NULL) {
300*0Sstevel@tonic-gate 			free(h);
301*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		bcopy(addr, aggdata->dtada_data, size);
305*0Sstevel@tonic-gate 		aggdata->dtada_size = size;
306*0Sstevel@tonic-gate 		aggdata->dtada_desc = agg;
307*0Sstevel@tonic-gate 		aggdata->dtada_handle = dtp;
308*0Sstevel@tonic-gate 		(void) dt_epid_lookup(dtp, agg->dtagd_epid,
309*0Sstevel@tonic-gate 		    &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
310*0Sstevel@tonic-gate 		aggdata->dtada_normal = 1;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		h->dtahe_hashval = hashval;
313*0Sstevel@tonic-gate 		h->dtahe_size = size;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 		rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		if (flags & DTRACE_A_PERCPU) {
318*0Sstevel@tonic-gate 			int max_cpus = agp->dtat_maxcpu;
319*0Sstevel@tonic-gate 			caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 			if (percpu == NULL) {
322*0Sstevel@tonic-gate 				free(aggdata->dtada_data);
323*0Sstevel@tonic-gate 				free(h);
324*0Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_NOMEM));
325*0Sstevel@tonic-gate 			}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 			for (j = 0; j < max_cpus; j++) {
328*0Sstevel@tonic-gate 				percpu[j] = malloc(rec->dtrd_size);
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 				if (percpu[j] == NULL) {
331*0Sstevel@tonic-gate 					while (--j >= 0)
332*0Sstevel@tonic-gate 						free(percpu[j]);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 					free(aggdata->dtada_data);
335*0Sstevel@tonic-gate 					free(h);
336*0Sstevel@tonic-gate 					return (dt_set_errno(dtp, EDT_NOMEM));
337*0Sstevel@tonic-gate 				}
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 				if (j == cpu) {
340*0Sstevel@tonic-gate 					bcopy(&addr[rec->dtrd_offset],
341*0Sstevel@tonic-gate 					    percpu[j], rec->dtrd_size);
342*0Sstevel@tonic-gate 				} else {
343*0Sstevel@tonic-gate 					bzero(percpu[j], rec->dtrd_size);
344*0Sstevel@tonic-gate 				}
345*0Sstevel@tonic-gate 			}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 			aggdata->dtada_percpu = percpu;
348*0Sstevel@tonic-gate 		}
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 		switch (rec->dtrd_action) {
351*0Sstevel@tonic-gate 		case DTRACEAGG_MIN:
352*0Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_min;
353*0Sstevel@tonic-gate 			break;
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 		case DTRACEAGG_MAX:
356*0Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_max;
357*0Sstevel@tonic-gate 			break;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 		case DTRACEAGG_LQUANTIZE:
360*0Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_lquantize;
361*0Sstevel@tonic-gate 			break;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 		case DTRACEAGG_COUNT:
364*0Sstevel@tonic-gate 		case DTRACEAGG_SUM:
365*0Sstevel@tonic-gate 		case DTRACEAGG_AVG:
366*0Sstevel@tonic-gate 		case DTRACEAGG_QUANTIZE:
367*0Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_count;
368*0Sstevel@tonic-gate 			break;
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 		default:
371*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADAGG));
372*0Sstevel@tonic-gate 		}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 		if (hash->dtah_hash[ndx] != NULL)
375*0Sstevel@tonic-gate 			hash->dtah_hash[ndx]->dtahe_prev = h;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 		h->dtahe_next = hash->dtah_hash[ndx];
378*0Sstevel@tonic-gate 		hash->dtah_hash[ndx] = h;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 		if (hash->dtah_all != NULL)
381*0Sstevel@tonic-gate 			hash->dtah_all->dtahe_prevall = h;
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 		h->dtahe_nextall = hash->dtah_all;
384*0Sstevel@tonic-gate 		hash->dtah_all = h;
385*0Sstevel@tonic-gate bufnext:
386*0Sstevel@tonic-gate 		offs += agg->dtagd_size;
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	return (0);
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate int
393*0Sstevel@tonic-gate dtrace_aggregate_snap(dtrace_hdl_t *dtp)
394*0Sstevel@tonic-gate {
395*0Sstevel@tonic-gate 	int i, rval;
396*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
397*0Sstevel@tonic-gate 	hrtime_t now = gethrtime();
398*0Sstevel@tonic-gate 	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	if (dtp->dt_lastagg != 0) {
401*0Sstevel@tonic-gate 		if (now - dtp->dt_lastagg < interval)
402*0Sstevel@tonic-gate 			return (0);
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 		dtp->dt_lastagg += interval;
405*0Sstevel@tonic-gate 	} else {
406*0Sstevel@tonic-gate 		dtp->dt_lastagg = now;
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	if (!dtp->dt_active)
410*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	if (agp->dtat_buf.dtbd_size == 0)
413*0Sstevel@tonic-gate 		return (0);
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	for (i = 0; i < agp->dtat_ncpus; i++) {
416*0Sstevel@tonic-gate 		if (rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i]))
417*0Sstevel@tonic-gate 			return (rval);
418*0Sstevel@tonic-gate 	}
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	return (0);
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate static int
424*0Sstevel@tonic-gate dt_aggregate_hashcmp(const void *lhs, const void *rhs)
425*0Sstevel@tonic-gate {
426*0Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
427*0Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
428*0Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
429*0Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
432*0Sstevel@tonic-gate 		return (-1);
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
435*0Sstevel@tonic-gate 		return (1);
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	return (0);
438*0Sstevel@tonic-gate }
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate static int
441*0Sstevel@tonic-gate dt_aggregate_varcmp(const void *lhs, const void *rhs)
442*0Sstevel@tonic-gate {
443*0Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
444*0Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
445*0Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
446*0Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
447*0Sstevel@tonic-gate 	caddr_t ldata = lh->dtahe_data.dtada_data;
448*0Sstevel@tonic-gate 	caddr_t rdata = rh->dtahe_data.dtada_data;
449*0Sstevel@tonic-gate 	dtrace_recdesc_t *lrec, *rrec;
450*0Sstevel@tonic-gate 	uint64_t lid, rid;
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	/*
453*0Sstevel@tonic-gate 	 * We know that we have a compiler-generated ID as the first record.
454*0Sstevel@tonic-gate 	 */
455*0Sstevel@tonic-gate 	lrec = lagg->dtagd_rec;
456*0Sstevel@tonic-gate 	rrec = ragg->dtagd_rec;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 	lid = *((uint64_t *)(uintptr_t)(ldata + lrec->dtrd_offset));
459*0Sstevel@tonic-gate 	rid = *((uint64_t *)(uintptr_t)(rdata + rrec->dtrd_offset));
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	if (lid < rid)
462*0Sstevel@tonic-gate 		return (-1);
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 	if (lid > rid)
465*0Sstevel@tonic-gate 		return (1);
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	return (0);
468*0Sstevel@tonic-gate }
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate static int
471*0Sstevel@tonic-gate dt_aggregate_keycmp(const void *lhs, const void *rhs)
472*0Sstevel@tonic-gate {
473*0Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
474*0Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
475*0Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
476*0Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
477*0Sstevel@tonic-gate 	dtrace_recdesc_t *lrec, *rrec;
478*0Sstevel@tonic-gate 	char *ldata, *rdata;
479*0Sstevel@tonic-gate 	int rval, i;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
482*0Sstevel@tonic-gate 		return (rval);
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	for (i = 1; i < lagg->dtagd_nrecs - 1; i++) {
485*0Sstevel@tonic-gate 		uint64_t lval, rval;
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 		lrec = &lagg->dtagd_rec[i];
488*0Sstevel@tonic-gate 		rrec = &ragg->dtagd_rec[i];
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 		ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
491*0Sstevel@tonic-gate 		rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		if (lrec->dtrd_size < rrec->dtrd_size)
494*0Sstevel@tonic-gate 			return (-1);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		if (lrec->dtrd_size > rrec->dtrd_size)
497*0Sstevel@tonic-gate 			return (1);
498*0Sstevel@tonic-gate 
499*0Sstevel@tonic-gate 		switch (lrec->dtrd_size) {
500*0Sstevel@tonic-gate 		case sizeof (uint64_t):
501*0Sstevel@tonic-gate 			/* LINTED - alignment */
502*0Sstevel@tonic-gate 			lval = *((uint64_t *)ldata);
503*0Sstevel@tonic-gate 			/* LINTED - alignment */
504*0Sstevel@tonic-gate 			rval = *((uint64_t *)rdata);
505*0Sstevel@tonic-gate 			break;
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 		case sizeof (uint32_t):
508*0Sstevel@tonic-gate 			/* LINTED - alignment */
509*0Sstevel@tonic-gate 			lval = *((uint32_t *)ldata);
510*0Sstevel@tonic-gate 			/* LINTED - alignment */
511*0Sstevel@tonic-gate 			rval = *((uint32_t *)rdata);
512*0Sstevel@tonic-gate 			break;
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 		case sizeof (uint16_t):
515*0Sstevel@tonic-gate 			/* LINTED - alignment */
516*0Sstevel@tonic-gate 			lval = *((uint16_t *)ldata);
517*0Sstevel@tonic-gate 			/* LINTED - alignment */
518*0Sstevel@tonic-gate 			rval = *((uint16_t *)rdata);
519*0Sstevel@tonic-gate 			break;
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 		case sizeof (uint8_t):
522*0Sstevel@tonic-gate 			lval = *((uint8_t *)ldata);
523*0Sstevel@tonic-gate 			rval = *((uint8_t *)rdata);
524*0Sstevel@tonic-gate 			break;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 		default:
527*0Sstevel@tonic-gate 			for (i = 0; i < lrec->dtrd_size; i++) {
528*0Sstevel@tonic-gate 				lval = ((uint8_t *)ldata)[i];
529*0Sstevel@tonic-gate 				rval = ((uint8_t *)rdata)[i];
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 				if (lval < rval)
532*0Sstevel@tonic-gate 					return (-1);
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 				if (lval > rval)
535*0Sstevel@tonic-gate 					return (1);
536*0Sstevel@tonic-gate 			}
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 			continue;
539*0Sstevel@tonic-gate 		}
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 		if (lval < rval)
542*0Sstevel@tonic-gate 			return (-1);
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 		if (lval > rval)
545*0Sstevel@tonic-gate 			return (1);
546*0Sstevel@tonic-gate 	}
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	return (0);
549*0Sstevel@tonic-gate }
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate static int
552*0Sstevel@tonic-gate dt_aggregate_valcmp(const void *lhs, const void *rhs)
553*0Sstevel@tonic-gate {
554*0Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
555*0Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
556*0Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
557*0Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
558*0Sstevel@tonic-gate 	caddr_t ldata = lh->dtahe_data.dtada_data;
559*0Sstevel@tonic-gate 	caddr_t rdata = rh->dtahe_data.dtada_data;
560*0Sstevel@tonic-gate 	dtrace_recdesc_t *lrec, *rrec;
561*0Sstevel@tonic-gate 	uint64_t *laddr, *raddr;
562*0Sstevel@tonic-gate 	int rval, i;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
565*0Sstevel@tonic-gate 		return (rval);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
568*0Sstevel@tonic-gate 		return (-1);
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
571*0Sstevel@tonic-gate 		return (1);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	for (i = 0; i < lagg->dtagd_nrecs; i++) {
574*0Sstevel@tonic-gate 		lrec = &lagg->dtagd_rec[i];
575*0Sstevel@tonic-gate 		rrec = &ragg->dtagd_rec[i];
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 		if (lrec->dtrd_offset < rrec->dtrd_offset)
578*0Sstevel@tonic-gate 			return (-1);
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 		if (lrec->dtrd_offset > rrec->dtrd_offset)
581*0Sstevel@tonic-gate 			return (1);
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 		if (lrec->dtrd_action < rrec->dtrd_action)
584*0Sstevel@tonic-gate 			return (-1);
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 		if (lrec->dtrd_action > rrec->dtrd_action)
587*0Sstevel@tonic-gate 			return (1);
588*0Sstevel@tonic-gate 	}
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	laddr = (uint64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
591*0Sstevel@tonic-gate 	raddr = (uint64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
592*0Sstevel@tonic-gate 
593*0Sstevel@tonic-gate 	switch (lrec->dtrd_action) {
594*0Sstevel@tonic-gate 	case DTRACEAGG_AVG:
595*0Sstevel@tonic-gate 		rval = dt_aggregate_averagecmp(laddr, raddr);
596*0Sstevel@tonic-gate 		break;
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	case DTRACEAGG_QUANTIZE:
599*0Sstevel@tonic-gate 		rval = dt_aggregate_quantizedcmp(laddr, raddr);
600*0Sstevel@tonic-gate 		break;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	case DTRACEAGG_LQUANTIZE:
603*0Sstevel@tonic-gate 		rval = dt_aggregate_lquantizedcmp(laddr, raddr);
604*0Sstevel@tonic-gate 		break;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	case DTRACEAGG_COUNT:
607*0Sstevel@tonic-gate 	case DTRACEAGG_SUM:
608*0Sstevel@tonic-gate 	case DTRACEAGG_MIN:
609*0Sstevel@tonic-gate 	case DTRACEAGG_MAX:
610*0Sstevel@tonic-gate 		rval = dt_aggregate_countcmp(laddr, raddr);
611*0Sstevel@tonic-gate 		break;
612*0Sstevel@tonic-gate 
613*0Sstevel@tonic-gate 	default:
614*0Sstevel@tonic-gate 		assert(0);
615*0Sstevel@tonic-gate 	}
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	if (rval != 0)
618*0Sstevel@tonic-gate 		return (rval);
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	/*
621*0Sstevel@tonic-gate 	 * If we're here, the values for the two aggregation elements are
622*0Sstevel@tonic-gate 	 * equal.  We already know that the key layout is the same for the two
623*0Sstevel@tonic-gate 	 * elements; we must now compare the keys themselves as a tie-breaker.
624*0Sstevel@tonic-gate 	 */
625*0Sstevel@tonic-gate 	return (dt_aggregate_keycmp(lhs, rhs));
626*0Sstevel@tonic-gate }
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate static int
629*0Sstevel@tonic-gate dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
630*0Sstevel@tonic-gate {
631*0Sstevel@tonic-gate 	int rval;
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate 	if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
634*0Sstevel@tonic-gate 		return (rval);
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	return (dt_aggregate_varcmp(lhs, rhs));
637*0Sstevel@tonic-gate }
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate static int
640*0Sstevel@tonic-gate dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
641*0Sstevel@tonic-gate {
642*0Sstevel@tonic-gate 	int rval;
643*0Sstevel@tonic-gate 
644*0Sstevel@tonic-gate 	if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
645*0Sstevel@tonic-gate 		return (rval);
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 	return (dt_aggregate_keycmp(lhs, rhs));
648*0Sstevel@tonic-gate }
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate static int
651*0Sstevel@tonic-gate dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
652*0Sstevel@tonic-gate {
653*0Sstevel@tonic-gate 	int rval;
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
656*0Sstevel@tonic-gate 		return (rval);
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 	return (dt_aggregate_varcmp(lhs, rhs));
659*0Sstevel@tonic-gate }
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate static int
662*0Sstevel@tonic-gate dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
663*0Sstevel@tonic-gate {
664*0Sstevel@tonic-gate 	int rval;
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
667*0Sstevel@tonic-gate 		return (rval);
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	return (dt_aggregate_valcmp(lhs, rhs));
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate static int
673*0Sstevel@tonic-gate dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
674*0Sstevel@tonic-gate {
675*0Sstevel@tonic-gate 	return (dt_aggregate_keyvarcmp(rhs, lhs));
676*0Sstevel@tonic-gate }
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate static int
679*0Sstevel@tonic-gate dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
680*0Sstevel@tonic-gate {
681*0Sstevel@tonic-gate 	return (dt_aggregate_varkeycmp(rhs, lhs));
682*0Sstevel@tonic-gate }
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate static int
685*0Sstevel@tonic-gate dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
686*0Sstevel@tonic-gate {
687*0Sstevel@tonic-gate 	return (dt_aggregate_valvarcmp(rhs, lhs));
688*0Sstevel@tonic-gate }
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate static int
691*0Sstevel@tonic-gate dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
692*0Sstevel@tonic-gate {
693*0Sstevel@tonic-gate 	return (dt_aggregate_varvalcmp(rhs, lhs));
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate int
697*0Sstevel@tonic-gate dt_aggregate_go(dtrace_hdl_t *dtp)
698*0Sstevel@tonic-gate {
699*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
700*0Sstevel@tonic-gate 	dtrace_optval_t size, cpu;
701*0Sstevel@tonic-gate 	dtrace_bufdesc_t *buf = &agp->dtat_buf;
702*0Sstevel@tonic-gate 	int rval, i;
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate 	assert(agp->dtat_maxcpu == 0);
705*0Sstevel@tonic-gate 	assert(agp->dtat_ncpu == 0);
706*0Sstevel@tonic-gate 	assert(agp->dtat_cpus == NULL);
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
709*0Sstevel@tonic-gate 	agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
710*0Sstevel@tonic-gate 	agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	if (agp->dtat_cpus == NULL)
713*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 	/*
716*0Sstevel@tonic-gate 	 * Use the aggregation buffer size as reloaded from the kernel.
717*0Sstevel@tonic-gate 	 */
718*0Sstevel@tonic-gate 	size = dtp->dt_options[DTRACEOPT_AGGSIZE];
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "aggsize", &size);
721*0Sstevel@tonic-gate 	assert(rval == 0);
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	if (size == 0 || size == DTRACEOPT_UNSET)
724*0Sstevel@tonic-gate 		return (0);
725*0Sstevel@tonic-gate 
726*0Sstevel@tonic-gate 	buf = &agp->dtat_buf;
727*0Sstevel@tonic-gate 	buf->dtbd_size = size;
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 	if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
730*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 	/*
733*0Sstevel@tonic-gate 	 * Now query for the CPUs enabled.
734*0Sstevel@tonic-gate 	 */
735*0Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "cpu", &cpu);
736*0Sstevel@tonic-gate 	assert(rval == 0 && cpu != DTRACEOPT_UNSET);
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	if (cpu != DTRACE_CPUALL) {
739*0Sstevel@tonic-gate 		assert(cpu < agp->dtat_ncpu);
740*0Sstevel@tonic-gate 		agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 		return (0);
743*0Sstevel@tonic-gate 	}
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	agp->dtat_ncpus = 0;
746*0Sstevel@tonic-gate 	for (i = 0; i < agp->dtat_maxcpu; i++) {
747*0Sstevel@tonic-gate 		if (dt_status(dtp, i) == -1)
748*0Sstevel@tonic-gate 			continue;
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 		agp->dtat_cpus[agp->dtat_ncpus++] = i;
751*0Sstevel@tonic-gate 	}
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	return (0);
754*0Sstevel@tonic-gate }
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate static int
757*0Sstevel@tonic-gate dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
758*0Sstevel@tonic-gate {
759*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
760*0Sstevel@tonic-gate 	dtrace_aggdata_t *data;
761*0Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc;
762*0Sstevel@tonic-gate 	dtrace_recdesc_t *rec;
763*0Sstevel@tonic-gate 	int i;
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	switch (rval) {
766*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_NEXT:
767*0Sstevel@tonic-gate 		break;
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_CLEAR: {
770*0Sstevel@tonic-gate 		uint32_t size, offs = 0;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 		aggdesc = h->dtahe_data.dtada_desc;
773*0Sstevel@tonic-gate 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
774*0Sstevel@tonic-gate 		size = rec->dtrd_size;
775*0Sstevel@tonic-gate 		data = &h->dtahe_data;
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 		if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
778*0Sstevel@tonic-gate 			offs = sizeof (uint64_t);
779*0Sstevel@tonic-gate 			size -= sizeof (uint64_t);
780*0Sstevel@tonic-gate 		}
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 		bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 		if (data->dtada_percpu == NULL)
785*0Sstevel@tonic-gate 			break;
786*0Sstevel@tonic-gate 
787*0Sstevel@tonic-gate 		for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
788*0Sstevel@tonic-gate 			bzero(data->dtada_percpu[i] + offs, size);
789*0Sstevel@tonic-gate 		break;
790*0Sstevel@tonic-gate 	}
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_ERROR:
793*0Sstevel@tonic-gate 		/*
794*0Sstevel@tonic-gate 		 * We assume that errno is already set in this case.
795*0Sstevel@tonic-gate 		 */
796*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_ABORT:
799*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DIRABORT));
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_DENORMALIZE:
802*0Sstevel@tonic-gate 		h->dtahe_data.dtada_normal = 1;
803*0Sstevel@tonic-gate 		return (0);
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_NORMALIZE:
806*0Sstevel@tonic-gate 		if (h->dtahe_data.dtada_normal == 0) {
807*0Sstevel@tonic-gate 			h->dtahe_data.dtada_normal = 1;
808*0Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADRVAL));
809*0Sstevel@tonic-gate 		}
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 		return (0);
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	case DTRACE_AGGWALK_REMOVE: {
814*0Sstevel@tonic-gate 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
815*0Sstevel@tonic-gate 		int i, max_cpus = agp->dtat_maxcpu;
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 		/*
818*0Sstevel@tonic-gate 		 * First, remove this hash entry from its hash chain.
819*0Sstevel@tonic-gate 		 */
820*0Sstevel@tonic-gate 		if (h->dtahe_prev != NULL) {
821*0Sstevel@tonic-gate 			h->dtahe_prev->dtahe_next = h->dtahe_next;
822*0Sstevel@tonic-gate 		} else {
823*0Sstevel@tonic-gate 			dt_ahash_t *hash = &agp->dtat_hash;
824*0Sstevel@tonic-gate 			size_t ndx = h->dtahe_hashval % hash->dtah_size;
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 			assert(hash->dtah_hash[ndx] == h);
827*0Sstevel@tonic-gate 			hash->dtah_hash[ndx] = h->dtahe_next;
828*0Sstevel@tonic-gate 		}
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		if (h->dtahe_next != NULL)
831*0Sstevel@tonic-gate 			h->dtahe_next->dtahe_prev = h->dtahe_prev;
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 		/*
834*0Sstevel@tonic-gate 		 * Now remove it from the list of all hash entries.
835*0Sstevel@tonic-gate 		 */
836*0Sstevel@tonic-gate 		if (h->dtahe_prevall != NULL) {
837*0Sstevel@tonic-gate 			h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
838*0Sstevel@tonic-gate 		} else {
839*0Sstevel@tonic-gate 			dt_ahash_t *hash = &agp->dtat_hash;
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 			assert(hash->dtah_all == h);
842*0Sstevel@tonic-gate 			hash->dtah_all = h->dtahe_nextall;
843*0Sstevel@tonic-gate 		}
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 		if (h->dtahe_nextall != NULL)
846*0Sstevel@tonic-gate 			h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate 		/*
849*0Sstevel@tonic-gate 		 * We're unlinked.  We can safely destroy the data.
850*0Sstevel@tonic-gate 		 */
851*0Sstevel@tonic-gate 		if (aggdata->dtada_percpu != NULL) {
852*0Sstevel@tonic-gate 			for (i = 0; i < max_cpus; i++)
853*0Sstevel@tonic-gate 				free(aggdata->dtada_percpu[i]);
854*0Sstevel@tonic-gate 			free(aggdata->dtada_percpu);
855*0Sstevel@tonic-gate 		}
856*0Sstevel@tonic-gate 
857*0Sstevel@tonic-gate 		free(aggdata->dtada_data);
858*0Sstevel@tonic-gate 		free(h);
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 		return (0);
861*0Sstevel@tonic-gate 	}
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	default:
864*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADRVAL));
865*0Sstevel@tonic-gate 	}
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate 	return (0);
868*0Sstevel@tonic-gate }
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate int
871*0Sstevel@tonic-gate dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
872*0Sstevel@tonic-gate {
873*0Sstevel@tonic-gate 	dt_ahashent_t *h, *next;
874*0Sstevel@tonic-gate 	dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	for (h = hash->dtah_all; h != NULL; h = next) {
877*0Sstevel@tonic-gate 		/*
878*0Sstevel@tonic-gate 		 * dt_aggwalk_rval() can potentially remove the current hash
879*0Sstevel@tonic-gate 		 * entry; we need to load the next hash entry before calling
880*0Sstevel@tonic-gate 		 * into it.
881*0Sstevel@tonic-gate 		 */
882*0Sstevel@tonic-gate 		next = h->dtahe_nextall;
883*0Sstevel@tonic-gate 
884*0Sstevel@tonic-gate 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
885*0Sstevel@tonic-gate 			return (-1);
886*0Sstevel@tonic-gate 	}
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 	return (0);
889*0Sstevel@tonic-gate }
890*0Sstevel@tonic-gate 
891*0Sstevel@tonic-gate static int
892*0Sstevel@tonic-gate dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
893*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg,
894*0Sstevel@tonic-gate     int (*sfunc)(const void *, const void *))
895*0Sstevel@tonic-gate {
896*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
897*0Sstevel@tonic-gate 	dt_ahashent_t *h, **sorted;
898*0Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
899*0Sstevel@tonic-gate 	size_t i, nentries = 0;
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
902*0Sstevel@tonic-gate 		nentries++;
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	sorted = malloc(nentries * sizeof (dt_ahashent_t *));
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 	if (sorted == NULL)
907*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
910*0Sstevel@tonic-gate 		sorted[i++] = h;
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 	qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate 	for (i = 0; i < nentries; i++) {
915*0Sstevel@tonic-gate 		h = sorted[i];
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
918*0Sstevel@tonic-gate 			return (-1);
919*0Sstevel@tonic-gate 	}
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 	free(sorted);
922*0Sstevel@tonic-gate 	return (0);
923*0Sstevel@tonic-gate }
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate int
926*0Sstevel@tonic-gate dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
927*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
928*0Sstevel@tonic-gate {
929*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
930*0Sstevel@tonic-gate 	    arg, dt_aggregate_varkeycmp));
931*0Sstevel@tonic-gate }
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate int
934*0Sstevel@tonic-gate dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
935*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
936*0Sstevel@tonic-gate {
937*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
938*0Sstevel@tonic-gate 	    arg, dt_aggregate_varvalcmp));
939*0Sstevel@tonic-gate }
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate int
942*0Sstevel@tonic-gate dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
943*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
944*0Sstevel@tonic-gate {
945*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
946*0Sstevel@tonic-gate 	    arg, dt_aggregate_keyvarcmp));
947*0Sstevel@tonic-gate }
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate int
950*0Sstevel@tonic-gate dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
951*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
952*0Sstevel@tonic-gate {
953*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
954*0Sstevel@tonic-gate 	    arg, dt_aggregate_valvarcmp));
955*0Sstevel@tonic-gate }
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate int
958*0Sstevel@tonic-gate dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
959*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
960*0Sstevel@tonic-gate {
961*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
962*0Sstevel@tonic-gate 	    arg, dt_aggregate_varkeyrevcmp));
963*0Sstevel@tonic-gate }
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate int
966*0Sstevel@tonic-gate dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
967*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
968*0Sstevel@tonic-gate {
969*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
970*0Sstevel@tonic-gate 	    arg, dt_aggregate_varvalrevcmp));
971*0Sstevel@tonic-gate }
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate int
974*0Sstevel@tonic-gate dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
975*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
976*0Sstevel@tonic-gate {
977*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
978*0Sstevel@tonic-gate 	    arg, dt_aggregate_keyvarrevcmp));
979*0Sstevel@tonic-gate }
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate int
982*0Sstevel@tonic-gate dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
983*0Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
984*0Sstevel@tonic-gate {
985*0Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
986*0Sstevel@tonic-gate 	    arg, dt_aggregate_valvarrevcmp));
987*0Sstevel@tonic-gate }
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate int
990*0Sstevel@tonic-gate dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
991*0Sstevel@tonic-gate     dtrace_aggregate_walk_f *func)
992*0Sstevel@tonic-gate {
993*0Sstevel@tonic-gate 	dt_print_aggdata_t pd;
994*0Sstevel@tonic-gate 
995*0Sstevel@tonic-gate 	pd.dtpa_dtp = dtp;
996*0Sstevel@tonic-gate 	pd.dtpa_fp = fp;
997*0Sstevel@tonic-gate 	pd.dtpa_allunprint = 1;
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	if (func == NULL)
1000*0Sstevel@tonic-gate 		func = dtrace_aggregate_walk_valsorted;
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	if ((*func)(dtp, dt_print_agg, &pd) == -1)
1003*0Sstevel@tonic-gate 		return (dt_set_errno(dtp, dtp->dt_errno));
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate 	return (0);
1006*0Sstevel@tonic-gate }
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate void
1009*0Sstevel@tonic-gate dtrace_aggregate_clear(dtrace_hdl_t *dtp)
1010*0Sstevel@tonic-gate {
1011*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
1012*0Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
1013*0Sstevel@tonic-gate 	dt_ahashent_t *h;
1014*0Sstevel@tonic-gate 	dtrace_aggdata_t *data;
1015*0Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc;
1016*0Sstevel@tonic-gate 	dtrace_recdesc_t *rec;
1017*0Sstevel@tonic-gate 	int i, max_cpus = agp->dtat_maxcpu;
1018*0Sstevel@tonic-gate 
1019*0Sstevel@tonic-gate 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1020*0Sstevel@tonic-gate 		aggdesc = h->dtahe_data.dtada_desc;
1021*0Sstevel@tonic-gate 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1022*0Sstevel@tonic-gate 		data = &h->dtahe_data;
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 		bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 		if (data->dtada_percpu == NULL)
1027*0Sstevel@tonic-gate 			continue;
1028*0Sstevel@tonic-gate 
1029*0Sstevel@tonic-gate 		for (i = 0; i < max_cpus; i++)
1030*0Sstevel@tonic-gate 			bzero(data->dtada_percpu[i], rec->dtrd_size);
1031*0Sstevel@tonic-gate 	}
1032*0Sstevel@tonic-gate }
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate void
1035*0Sstevel@tonic-gate dt_aggregate_destroy(dtrace_hdl_t *dtp)
1036*0Sstevel@tonic-gate {
1037*0Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
1038*0Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
1039*0Sstevel@tonic-gate 	dt_ahashent_t *h, *next;
1040*0Sstevel@tonic-gate 	dtrace_aggdata_t *aggdata;
1041*0Sstevel@tonic-gate 	int i, max_cpus = agp->dtat_maxcpu;
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 	if (hash->dtah_hash == NULL) {
1044*0Sstevel@tonic-gate 		assert(hash->dtah_all == NULL);
1045*0Sstevel@tonic-gate 	} else {
1046*0Sstevel@tonic-gate 		free(hash->dtah_hash);
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 		for (h = hash->dtah_all; h != NULL; h = next) {
1049*0Sstevel@tonic-gate 			next = h->dtahe_nextall;
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 			aggdata = &h->dtahe_data;
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 			if (aggdata->dtada_percpu != NULL) {
1054*0Sstevel@tonic-gate 				for (i = 0; i < max_cpus; i++)
1055*0Sstevel@tonic-gate 					free(aggdata->dtada_percpu[i]);
1056*0Sstevel@tonic-gate 				free(aggdata->dtada_percpu);
1057*0Sstevel@tonic-gate 			}
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 			free(aggdata->dtada_data);
1060*0Sstevel@tonic-gate 			free(h);
1061*0Sstevel@tonic-gate 		}
1062*0Sstevel@tonic-gate 
1063*0Sstevel@tonic-gate 		hash->dtah_hash = NULL;
1064*0Sstevel@tonic-gate 		hash->dtah_all = NULL;
1065*0Sstevel@tonic-gate 		hash->dtah_size = 0;
1066*0Sstevel@tonic-gate 	}
1067*0Sstevel@tonic-gate 
1068*0Sstevel@tonic-gate 	free(agp->dtat_buf.dtbd_data);
1069*0Sstevel@tonic-gate 	free(agp->dtat_cpus);
1070*0Sstevel@tonic-gate }
1071