xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_aggregate.c (revision 5984:a183e54573d2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52777Stomee  * Common Development and Distribution License (the "License").
62777Stomee  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
212777Stomee 
220Sstevel@tonic-gate /*
23*5984Sjhaslam  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <dt_impl.h>
340Sstevel@tonic-gate #include <assert.h>
351017Sbmc #include <alloca.h>
361017Sbmc #include <limits.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	DTRACE_AHASHSIZE	32779		/* big 'ol prime */
390Sstevel@tonic-gate 
401017Sbmc /*
411017Sbmc  * Because qsort(3C) does not allow an argument to be passed to a comparison
421017Sbmc  * function, the variables that affect comparison must regrettably be global;
431017Sbmc  * they are protected by a global static lock, dt_qsort_lock.
441017Sbmc  */
451017Sbmc static pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
461017Sbmc 
471017Sbmc static int dt_revsort;
481017Sbmc static int dt_keysort;
491017Sbmc static int dt_keypos;
501017Sbmc 
511017Sbmc #define	DT_LESSTHAN	(dt_revsort == 0 ? -1 : 1)
521017Sbmc #define	DT_GREATERTHAN	(dt_revsort == 0 ? 1 : -1)
531017Sbmc 
540Sstevel@tonic-gate static void
dt_aggregate_count(int64_t * existing,int64_t * new,size_t size)55491Sbmc dt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	int i;
580Sstevel@tonic-gate 
59491Sbmc 	for (i = 0; i < size / sizeof (int64_t); i++)
600Sstevel@tonic-gate 		existing[i] = existing[i] + new[i];
610Sstevel@tonic-gate }
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static int
dt_aggregate_countcmp(int64_t * lhs,int64_t * rhs)64491Sbmc dt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
650Sstevel@tonic-gate {
66491Sbmc 	int64_t lvar = *lhs;
67491Sbmc 	int64_t rvar = *rhs;
680Sstevel@tonic-gate 
691017Sbmc 	if (lvar < rvar)
701017Sbmc 		return (DT_LESSTHAN);
710Sstevel@tonic-gate 
721017Sbmc 	if (lvar > rvar)
731017Sbmc 		return (DT_GREATERTHAN);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	return (0);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*ARGSUSED*/
790Sstevel@tonic-gate static void
dt_aggregate_min(int64_t * existing,int64_t * new,size_t size)80491Sbmc dt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	if (*new < *existing)
830Sstevel@tonic-gate 		*existing = *new;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*ARGSUSED*/
870Sstevel@tonic-gate static void
dt_aggregate_max(int64_t * existing,int64_t * new,size_t size)88491Sbmc dt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	if (*new > *existing)
910Sstevel@tonic-gate 		*existing = *new;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static int
dt_aggregate_averagecmp(int64_t * lhs,int64_t * rhs)95491Sbmc dt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
960Sstevel@tonic-gate {
97491Sbmc 	int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
98491Sbmc 	int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
990Sstevel@tonic-gate 
1001017Sbmc 	if (lavg < ravg)
1011017Sbmc 		return (DT_LESSTHAN);
1020Sstevel@tonic-gate 
1031017Sbmc 	if (lavg > ravg)
1041017Sbmc 		return (DT_GREATERTHAN);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	return (0);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
109*5984Sjhaslam static int
dt_aggregate_stddevcmp(int64_t * lhs,int64_t * rhs)110*5984Sjhaslam dt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
111*5984Sjhaslam {
112*5984Sjhaslam 	uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
113*5984Sjhaslam 	uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
114*5984Sjhaslam 
115*5984Sjhaslam 	if (lsd < rsd)
116*5984Sjhaslam 		return (DT_LESSTHAN);
117*5984Sjhaslam 
118*5984Sjhaslam 	if (lsd > rsd)
119*5984Sjhaslam 		return (DT_GREATERTHAN);
120*5984Sjhaslam 
121*5984Sjhaslam 	return (0);
122*5984Sjhaslam }
123*5984Sjhaslam 
1240Sstevel@tonic-gate /*ARGSUSED*/
1250Sstevel@tonic-gate static void
dt_aggregate_lquantize(int64_t * existing,int64_t * new,size_t size)126491Sbmc dt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
1270Sstevel@tonic-gate {
128491Sbmc 	int64_t arg = *existing++;
1290Sstevel@tonic-gate 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
1300Sstevel@tonic-gate 	int i;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	for (i = 0; i <= levels + 1; i++)
1330Sstevel@tonic-gate 		existing[i] = existing[i] + new[i + 1];
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
136457Sbmc static long double
dt_aggregate_lquantizedsum(int64_t * lquanta)137491Sbmc dt_aggregate_lquantizedsum(int64_t *lquanta)
1380Sstevel@tonic-gate {
139491Sbmc 	int64_t arg = *lquanta++;
1400Sstevel@tonic-gate 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
1410Sstevel@tonic-gate 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
1420Sstevel@tonic-gate 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
143457Sbmc 	long double total = (long double)lquanta[0] * (long double)(base - 1);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	for (i = 0; i < levels; base += step, i++)
146457Sbmc 		total += (long double)lquanta[i + 1] * (long double)base;
1470Sstevel@tonic-gate 
148457Sbmc 	return (total + (long double)lquanta[levels + 1] *
149457Sbmc 	    (long double)(base + 1));
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
152491Sbmc static int64_t
dt_aggregate_lquantizedzero(int64_t * lquanta)153491Sbmc dt_aggregate_lquantizedzero(int64_t *lquanta)
154491Sbmc {
155491Sbmc 	int64_t arg = *lquanta++;
156491Sbmc 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
157491Sbmc 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
158491Sbmc 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
159491Sbmc 
160491Sbmc 	if (base - 1 == 0)
161491Sbmc 		return (lquanta[0]);
162491Sbmc 
163491Sbmc 	for (i = 0; i < levels; base += step, i++) {
164491Sbmc 		if (base != 0)
165491Sbmc 			continue;
166491Sbmc 
167491Sbmc 		return (lquanta[i + 1]);
168491Sbmc 	}
169491Sbmc 
170491Sbmc 	if (base + 1 == 0)
171491Sbmc 		return (lquanta[levels + 1]);
172491Sbmc 
173491Sbmc 	return (0);
174491Sbmc }
175491Sbmc 
1760Sstevel@tonic-gate static int
dt_aggregate_lquantizedcmp(int64_t * lhs,int64_t * rhs)177491Sbmc dt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
1780Sstevel@tonic-gate {
179457Sbmc 	long double lsum = dt_aggregate_lquantizedsum(lhs);
180457Sbmc 	long double rsum = dt_aggregate_lquantizedsum(rhs);
181491Sbmc 	int64_t lzero, rzero;
1820Sstevel@tonic-gate 
1831017Sbmc 	if (lsum < rsum)
1841017Sbmc 		return (DT_LESSTHAN);
1850Sstevel@tonic-gate 
1861017Sbmc 	if (lsum > rsum)
1871017Sbmc 		return (DT_GREATERTHAN);
1880Sstevel@tonic-gate 
189491Sbmc 	/*
190491Sbmc 	 * If they're both equal, then we will compare based on the weights at
191491Sbmc 	 * zero.  If the weights at zero are equal (or if zero is not within
192491Sbmc 	 * the range of the linear quantization), then this will be judged a
193491Sbmc 	 * tie and will be resolved based on the key comparison.
194491Sbmc 	 */
195491Sbmc 	lzero = dt_aggregate_lquantizedzero(lhs);
196491Sbmc 	rzero = dt_aggregate_lquantizedzero(rhs);
197491Sbmc 
1981017Sbmc 	if (lzero < rzero)
1991017Sbmc 		return (DT_LESSTHAN);
200491Sbmc 
2011017Sbmc 	if (lzero > rzero)
2021017Sbmc 		return (DT_GREATERTHAN);
203491Sbmc 
2040Sstevel@tonic-gate 	return (0);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static int
dt_aggregate_quantizedcmp(int64_t * lhs,int64_t * rhs)208491Sbmc dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	int nbuckets = DTRACE_QUANTIZE_NBUCKETS, i;
211457Sbmc 	long double ltotal = 0, rtotal = 0;
212491Sbmc 	int64_t lzero, rzero;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	for (i = 0; i < nbuckets; i++) {
2150Sstevel@tonic-gate 		int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
2160Sstevel@tonic-gate 
217491Sbmc 		if (bucketval == 0) {
218491Sbmc 			lzero = lhs[i];
219491Sbmc 			rzero = rhs[i];
220491Sbmc 		}
221491Sbmc 
222457Sbmc 		ltotal += (long double)bucketval * (long double)lhs[i];
223457Sbmc 		rtotal += (long double)bucketval * (long double)rhs[i];
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2261017Sbmc 	if (ltotal < rtotal)
2271017Sbmc 		return (DT_LESSTHAN);
2280Sstevel@tonic-gate 
2291017Sbmc 	if (ltotal > rtotal)
2301017Sbmc 		return (DT_GREATERTHAN);
2310Sstevel@tonic-gate 
232491Sbmc 	/*
233491Sbmc 	 * If they're both equal, then we will compare based on the weights at
234491Sbmc 	 * zero.  If the weights at zero are equal, then this will be judged a
235491Sbmc 	 * tie and will be resolved based on the key comparison.
236491Sbmc 	 */
2371017Sbmc 	if (lzero < rzero)
2381017Sbmc 		return (DT_LESSTHAN);
239491Sbmc 
2401017Sbmc 	if (lzero > rzero)
2411017Sbmc 		return (DT_GREATERTHAN);
242491Sbmc 
2430Sstevel@tonic-gate 	return (0);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
246457Sbmc static void
dt_aggregate_usym(dtrace_hdl_t * dtp,uint64_t * data)247457Sbmc dt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
248457Sbmc {
249457Sbmc 	uint64_t pid = data[0];
250457Sbmc 	uint64_t *pc = &data[1];
251457Sbmc 	struct ps_prochandle *P;
252457Sbmc 	GElf_Sym sym;
253457Sbmc 
254457Sbmc 	if (dtp->dt_vector != NULL)
255457Sbmc 		return;
256457Sbmc 
257457Sbmc 	if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
258457Sbmc 		return;
259457Sbmc 
260457Sbmc 	dt_proc_lock(dtp, P);
261457Sbmc 
262457Sbmc 	if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
263457Sbmc 		*pc = sym.st_value;
264457Sbmc 
265457Sbmc 	dt_proc_unlock(dtp, P);
266457Sbmc 	dt_proc_release(dtp, P);
267457Sbmc }
268457Sbmc 
269457Sbmc static void
dt_aggregate_umod(dtrace_hdl_t * dtp,uint64_t * data)270457Sbmc dt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
271457Sbmc {
272457Sbmc 	uint64_t pid = data[0];
273457Sbmc 	uint64_t *pc = &data[1];
274457Sbmc 	struct ps_prochandle *P;
275457Sbmc 	const prmap_t *map;
276457Sbmc 
277457Sbmc 	if (dtp->dt_vector != NULL)
278457Sbmc 		return;
279457Sbmc 
280457Sbmc 	if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
281457Sbmc 		return;
282457Sbmc 
283457Sbmc 	dt_proc_lock(dtp, P);
284457Sbmc 
285457Sbmc 	if ((map = Paddr_to_map(P, *pc)) != NULL)
286457Sbmc 		*pc = map->pr_vaddr;
287457Sbmc 
288457Sbmc 	dt_proc_unlock(dtp, P);
289457Sbmc 	dt_proc_release(dtp, P);
290457Sbmc }
291457Sbmc 
292457Sbmc static void
dt_aggregate_sym(dtrace_hdl_t * dtp,uint64_t * data)293457Sbmc dt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
294457Sbmc {
295457Sbmc 	GElf_Sym sym;
296457Sbmc 	uint64_t *pc = data;
297457Sbmc 
298457Sbmc 	if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
299457Sbmc 		*pc = sym.st_value;
300457Sbmc }
301457Sbmc 
302457Sbmc static void
dt_aggregate_mod(dtrace_hdl_t * dtp,uint64_t * data)303457Sbmc dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
304457Sbmc {
305457Sbmc 	uint64_t *pc = data;
306457Sbmc 	dt_module_t *dmp;
307457Sbmc 
308457Sbmc 	if (dtp->dt_vector != NULL) {
309457Sbmc 		/*
310457Sbmc 		 * We don't have a way of just getting the module for a
311457Sbmc 		 * vectored open, and it doesn't seem to be worth defining
312457Sbmc 		 * one.  This means that use of mod() won't get true
313457Sbmc 		 * aggregation in the postmortem case (some modules may
314457Sbmc 		 * appear more than once in aggregation output).  It seems
315457Sbmc 		 * unlikely that anyone will ever notice or care...
316457Sbmc 		 */
317457Sbmc 		return;
318457Sbmc 	}
319457Sbmc 
320457Sbmc 	for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
321457Sbmc 	    dmp = dt_list_next(dmp)) {
322457Sbmc 		if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
323457Sbmc 			*pc = dmp->dm_text_va;
324457Sbmc 			return;
325457Sbmc 		}
326457Sbmc 	}
327457Sbmc }
328457Sbmc 
3291017Sbmc static dtrace_aggvarid_t
dt_aggregate_aggvarid(dt_ahashent_t * ent)3301017Sbmc dt_aggregate_aggvarid(dt_ahashent_t *ent)
3311017Sbmc {
3321017Sbmc 	dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
3331017Sbmc 	caddr_t data = ent->dtahe_data.dtada_data;
3341017Sbmc 	dtrace_recdesc_t *rec = agg->dtagd_rec;
3351017Sbmc 
3361017Sbmc 	/*
3371017Sbmc 	 * First, we'll check the variable ID in the aggdesc.  If it's valid,
3381017Sbmc 	 * we'll return it.  If not, we'll use the compiler-generated ID
3391017Sbmc 	 * present as the first record.
3401017Sbmc 	 */
3411017Sbmc 	if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
3421017Sbmc 		return (agg->dtagd_varid);
3431017Sbmc 
3441017Sbmc 	agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
3451017Sbmc 	    rec->dtrd_offset));
3461017Sbmc 
3471017Sbmc 	return (agg->dtagd_varid);
3481017Sbmc }
3491017Sbmc 
3501017Sbmc 
3510Sstevel@tonic-gate static int
dt_aggregate_snap_cpu(dtrace_hdl_t * dtp,processorid_t cpu)3520Sstevel@tonic-gate dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	dtrace_epid_t id;
3550Sstevel@tonic-gate 	uint64_t hashval;
3560Sstevel@tonic-gate 	size_t offs, roffs, size, ndx;
3570Sstevel@tonic-gate 	int i, j, rval;
3580Sstevel@tonic-gate 	caddr_t addr, data;
3590Sstevel@tonic-gate 	dtrace_recdesc_t *rec;
3600Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
3610Sstevel@tonic-gate 	dtrace_aggdesc_t *agg;
3620Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
3630Sstevel@tonic-gate 	dt_ahashent_t *h;
3640Sstevel@tonic-gate 	dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
3650Sstevel@tonic-gate 	dtrace_aggdata_t *aggdata;
3660Sstevel@tonic-gate 	int flags = agp->dtat_flags;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	buf->dtbd_cpu = cpu;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
3710Sstevel@tonic-gate 		if (errno == ENOENT) {
3720Sstevel@tonic-gate 			/*
3730Sstevel@tonic-gate 			 * If that failed with ENOENT, it may be because the
3740Sstevel@tonic-gate 			 * CPU was unconfigured.  This is okay; we'll just
3750Sstevel@tonic-gate 			 * do nothing but return success.
3760Sstevel@tonic-gate 			 */
3770Sstevel@tonic-gate 			return (0);
3780Sstevel@tonic-gate 		}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if (buf->dtbd_drops != 0) {
3840Sstevel@tonic-gate 		if (dt_handle_cpudrop(dtp, cpu,
3850Sstevel@tonic-gate 		    DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1)
3860Sstevel@tonic-gate 			return (-1);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (buf->dtbd_size == 0)
3900Sstevel@tonic-gate 		return (0);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (hash->dtah_hash == NULL) {
3930Sstevel@tonic-gate 		size_t size;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		hash->dtah_size = DTRACE_AHASHSIZE;
3960Sstevel@tonic-gate 		size = hash->dtah_size * sizeof (dt_ahashent_t *);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		if ((hash->dtah_hash = malloc(size)) == NULL)
3990Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		bzero(hash->dtah_hash, size);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	for (offs = 0; offs < buf->dtbd_size; ) {
4050Sstevel@tonic-gate 		/*
4060Sstevel@tonic-gate 		 * We're guaranteed to have an ID.
4070Sstevel@tonic-gate 		 */
4080Sstevel@tonic-gate 		id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
4090Sstevel@tonic-gate 		    (uintptr_t)offs));
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		if (id == DTRACE_AGGIDNONE) {
4120Sstevel@tonic-gate 			/*
4130Sstevel@tonic-gate 			 * This is filler to assure proper alignment of the
4140Sstevel@tonic-gate 			 * next record; we simply ignore it.
4150Sstevel@tonic-gate 			 */
4160Sstevel@tonic-gate 			offs += sizeof (id);
4170Sstevel@tonic-gate 			continue;
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
4210Sstevel@tonic-gate 			return (rval);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		addr = buf->dtbd_data + offs;
4240Sstevel@tonic-gate 		size = agg->dtagd_size;
4250Sstevel@tonic-gate 		hashval = 0;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
4280Sstevel@tonic-gate 			rec = &agg->dtagd_rec[j];
4290Sstevel@tonic-gate 			roffs = rec->dtrd_offset;
4300Sstevel@tonic-gate 
431457Sbmc 			switch (rec->dtrd_action) {
432457Sbmc 			case DTRACEACT_USYM:
433457Sbmc 				dt_aggregate_usym(dtp,
434457Sbmc 				    /* LINTED - alignment */
435457Sbmc 				    (uint64_t *)&addr[roffs]);
436457Sbmc 				break;
437457Sbmc 
438457Sbmc 			case DTRACEACT_UMOD:
439457Sbmc 				dt_aggregate_umod(dtp,
440457Sbmc 				    /* LINTED - alignment */
441457Sbmc 				    (uint64_t *)&addr[roffs]);
442457Sbmc 				break;
443457Sbmc 
444457Sbmc 			case DTRACEACT_SYM:
445457Sbmc 				/* LINTED - alignment */
446457Sbmc 				dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
447457Sbmc 				break;
448457Sbmc 
449457Sbmc 			case DTRACEACT_MOD:
450457Sbmc 				/* LINTED - alignment */
451457Sbmc 				dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
452457Sbmc 				break;
453457Sbmc 
454457Sbmc 			default:
455457Sbmc 				break;
456457Sbmc 			}
457457Sbmc 
4580Sstevel@tonic-gate 			for (i = 0; i < rec->dtrd_size; i++)
4590Sstevel@tonic-gate 				hashval += addr[roffs + i];
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		ndx = hashval % hash->dtah_size;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 		for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
4650Sstevel@tonic-gate 			if (h->dtahe_hashval != hashval)
4660Sstevel@tonic-gate 				continue;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 			if (h->dtahe_size != size)
4690Sstevel@tonic-gate 				continue;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 			aggdata = &h->dtahe_data;
4720Sstevel@tonic-gate 			data = aggdata->dtada_data;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 			for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
4750Sstevel@tonic-gate 				rec = &agg->dtagd_rec[j];
4760Sstevel@tonic-gate 				roffs = rec->dtrd_offset;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 				for (i = 0; i < rec->dtrd_size; i++)
4790Sstevel@tonic-gate 					if (addr[roffs + i] != data[roffs + i])
4800Sstevel@tonic-gate 						goto hashnext;
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 			/*
4840Sstevel@tonic-gate 			 * We found it.  Now we need to apply the aggregating
4850Sstevel@tonic-gate 			 * action on the data here.
4860Sstevel@tonic-gate 			 */
4870Sstevel@tonic-gate 			rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
4880Sstevel@tonic-gate 			roffs = rec->dtrd_offset;
4890Sstevel@tonic-gate 			/* LINTED - alignment */
490491Sbmc 			h->dtahe_aggregate((int64_t *)&data[roffs],
4910Sstevel@tonic-gate 			    /* LINTED - alignment */
492491Sbmc 			    (int64_t *)&addr[roffs], rec->dtrd_size);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 			/*
4950Sstevel@tonic-gate 			 * If we're keeping per CPU data, apply the aggregating
4960Sstevel@tonic-gate 			 * action there as well.
4970Sstevel@tonic-gate 			 */
4980Sstevel@tonic-gate 			if (aggdata->dtada_percpu != NULL) {
4990Sstevel@tonic-gate 				data = aggdata->dtada_percpu[cpu];
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 				/* LINTED - alignment */
502491Sbmc 				h->dtahe_aggregate((int64_t *)data,
5030Sstevel@tonic-gate 				    /* LINTED - alignment */
504491Sbmc 				    (int64_t *)&addr[roffs], rec->dtrd_size);
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 			goto bufnext;
5080Sstevel@tonic-gate hashnext:
5090Sstevel@tonic-gate 			continue;
5100Sstevel@tonic-gate 		}
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 		/*
5130Sstevel@tonic-gate 		 * If we're here, we couldn't find an entry for this record.
5140Sstevel@tonic-gate 		 */
5150Sstevel@tonic-gate 		if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
5160Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
5170Sstevel@tonic-gate 		bzero(h, sizeof (dt_ahashent_t));
5180Sstevel@tonic-gate 		aggdata = &h->dtahe_data;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		if ((aggdata->dtada_data = malloc(size)) == NULL) {
5210Sstevel@tonic-gate 			free(h);
5220Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_NOMEM));
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		bcopy(addr, aggdata->dtada_data, size);
5260Sstevel@tonic-gate 		aggdata->dtada_size = size;
5270Sstevel@tonic-gate 		aggdata->dtada_desc = agg;
5280Sstevel@tonic-gate 		aggdata->dtada_handle = dtp;
5290Sstevel@tonic-gate 		(void) dt_epid_lookup(dtp, agg->dtagd_epid,
5300Sstevel@tonic-gate 		    &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
5310Sstevel@tonic-gate 		aggdata->dtada_normal = 1;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		h->dtahe_hashval = hashval;
5340Sstevel@tonic-gate 		h->dtahe_size = size;
5351017Sbmc 		(void) dt_aggregate_aggvarid(h);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		if (flags & DTRACE_A_PERCPU) {
5400Sstevel@tonic-gate 			int max_cpus = agp->dtat_maxcpu;
5410Sstevel@tonic-gate 			caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 			if (percpu == NULL) {
5440Sstevel@tonic-gate 				free(aggdata->dtada_data);
5450Sstevel@tonic-gate 				free(h);
5460Sstevel@tonic-gate 				return (dt_set_errno(dtp, EDT_NOMEM));
5470Sstevel@tonic-gate 			}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 			for (j = 0; j < max_cpus; j++) {
5500Sstevel@tonic-gate 				percpu[j] = malloc(rec->dtrd_size);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 				if (percpu[j] == NULL) {
5530Sstevel@tonic-gate 					while (--j >= 0)
5540Sstevel@tonic-gate 						free(percpu[j]);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 					free(aggdata->dtada_data);
5570Sstevel@tonic-gate 					free(h);
5580Sstevel@tonic-gate 					return (dt_set_errno(dtp, EDT_NOMEM));
5590Sstevel@tonic-gate 				}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 				if (j == cpu) {
5620Sstevel@tonic-gate 					bcopy(&addr[rec->dtrd_offset],
5630Sstevel@tonic-gate 					    percpu[j], rec->dtrd_size);
5640Sstevel@tonic-gate 				} else {
5650Sstevel@tonic-gate 					bzero(percpu[j], rec->dtrd_size);
5660Sstevel@tonic-gate 				}
5670Sstevel@tonic-gate 			}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 			aggdata->dtada_percpu = percpu;
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		switch (rec->dtrd_action) {
5730Sstevel@tonic-gate 		case DTRACEAGG_MIN:
5740Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_min;
5750Sstevel@tonic-gate 			break;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 		case DTRACEAGG_MAX:
5780Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_max;
5790Sstevel@tonic-gate 			break;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		case DTRACEAGG_LQUANTIZE:
5820Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_lquantize;
5830Sstevel@tonic-gate 			break;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 		case DTRACEAGG_COUNT:
5860Sstevel@tonic-gate 		case DTRACEAGG_SUM:
5870Sstevel@tonic-gate 		case DTRACEAGG_AVG:
588*5984Sjhaslam 		case DTRACEAGG_STDDEV:
5890Sstevel@tonic-gate 		case DTRACEAGG_QUANTIZE:
5900Sstevel@tonic-gate 			h->dtahe_aggregate = dt_aggregate_count;
5910Sstevel@tonic-gate 			break;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		default:
5940Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADAGG));
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		if (hash->dtah_hash[ndx] != NULL)
5980Sstevel@tonic-gate 			hash->dtah_hash[ndx]->dtahe_prev = h;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 		h->dtahe_next = hash->dtah_hash[ndx];
6010Sstevel@tonic-gate 		hash->dtah_hash[ndx] = h;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		if (hash->dtah_all != NULL)
6040Sstevel@tonic-gate 			hash->dtah_all->dtahe_prevall = h;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		h->dtahe_nextall = hash->dtah_all;
6070Sstevel@tonic-gate 		hash->dtah_all = h;
6080Sstevel@tonic-gate bufnext:
6090Sstevel@tonic-gate 		offs += agg->dtagd_size;
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	return (0);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate int
dtrace_aggregate_snap(dtrace_hdl_t * dtp)6160Sstevel@tonic-gate dtrace_aggregate_snap(dtrace_hdl_t *dtp)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	int i, rval;
6190Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
6200Sstevel@tonic-gate 	hrtime_t now = gethrtime();
6210Sstevel@tonic-gate 	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (dtp->dt_lastagg != 0) {
6240Sstevel@tonic-gate 		if (now - dtp->dt_lastagg < interval)
6250Sstevel@tonic-gate 			return (0);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		dtp->dt_lastagg += interval;
6280Sstevel@tonic-gate 	} else {
6290Sstevel@tonic-gate 		dtp->dt_lastagg = now;
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (!dtp->dt_active)
6330Sstevel@tonic-gate 		return (dt_set_errno(dtp, EINVAL));
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (agp->dtat_buf.dtbd_size == 0)
6360Sstevel@tonic-gate 		return (0);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	for (i = 0; i < agp->dtat_ncpus; i++) {
6390Sstevel@tonic-gate 		if (rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i]))
6400Sstevel@tonic-gate 			return (rval);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	return (0);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate static int
dt_aggregate_hashcmp(const void * lhs,const void * rhs)6470Sstevel@tonic-gate dt_aggregate_hashcmp(const void *lhs, const void *rhs)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
6500Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
6510Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
6520Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
6551017Sbmc 		return (DT_LESSTHAN);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
6581017Sbmc 		return (DT_GREATERTHAN);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	return (0);
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate static int
dt_aggregate_varcmp(const void * lhs,const void * rhs)6640Sstevel@tonic-gate dt_aggregate_varcmp(const void *lhs, const void *rhs)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
6670Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
6681017Sbmc 	dtrace_aggvarid_t lid, rid;
6690Sstevel@tonic-gate 
6701017Sbmc 	lid = dt_aggregate_aggvarid(lh);
6711017Sbmc 	rid = dt_aggregate_aggvarid(rh);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (lid < rid)
6741017Sbmc 		return (DT_LESSTHAN);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	if (lid > rid)
6771017Sbmc 		return (DT_GREATERTHAN);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	return (0);
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate static int
dt_aggregate_keycmp(const void * lhs,const void * rhs)6830Sstevel@tonic-gate dt_aggregate_keycmp(const void *lhs, const void *rhs)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
6860Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
6870Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
6880Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
6890Sstevel@tonic-gate 	dtrace_recdesc_t *lrec, *rrec;
6900Sstevel@tonic-gate 	char *ldata, *rdata;
6911017Sbmc 	int rval, i, j, keypos, nrecs;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
6940Sstevel@tonic-gate 		return (rval);
6950Sstevel@tonic-gate 
6961017Sbmc 	nrecs = lagg->dtagd_nrecs - 1;
6971017Sbmc 	assert(nrecs == ragg->dtagd_nrecs - 1);
6981017Sbmc 
6991017Sbmc 	keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
7000Sstevel@tonic-gate 
7011017Sbmc 	for (i = 1; i < nrecs; i++) {
7021017Sbmc 		uint64_t lval, rval;
7031017Sbmc 		int ndx = i + keypos;
7041017Sbmc 
7051017Sbmc 		if (ndx >= nrecs)
7061017Sbmc 			ndx = ndx - nrecs + 1;
7071017Sbmc 
7081017Sbmc 		lrec = &lagg->dtagd_rec[ndx];
7091017Sbmc 		rrec = &ragg->dtagd_rec[ndx];
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
7120Sstevel@tonic-gate 		rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		if (lrec->dtrd_size < rrec->dtrd_size)
7151017Sbmc 			return (DT_LESSTHAN);
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 		if (lrec->dtrd_size > rrec->dtrd_size)
7181017Sbmc 			return (DT_GREATERTHAN);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		switch (lrec->dtrd_size) {
7210Sstevel@tonic-gate 		case sizeof (uint64_t):
7220Sstevel@tonic-gate 			/* LINTED - alignment */
7230Sstevel@tonic-gate 			lval = *((uint64_t *)ldata);
7240Sstevel@tonic-gate 			/* LINTED - alignment */
7250Sstevel@tonic-gate 			rval = *((uint64_t *)rdata);
7260Sstevel@tonic-gate 			break;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		case sizeof (uint32_t):
7290Sstevel@tonic-gate 			/* LINTED - alignment */
7300Sstevel@tonic-gate 			lval = *((uint32_t *)ldata);
7310Sstevel@tonic-gate 			/* LINTED - alignment */
7320Sstevel@tonic-gate 			rval = *((uint32_t *)rdata);
7330Sstevel@tonic-gate 			break;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		case sizeof (uint16_t):
7360Sstevel@tonic-gate 			/* LINTED - alignment */
7370Sstevel@tonic-gate 			lval = *((uint16_t *)ldata);
7380Sstevel@tonic-gate 			/* LINTED - alignment */
7390Sstevel@tonic-gate 			rval = *((uint16_t *)rdata);
7400Sstevel@tonic-gate 			break;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 		case sizeof (uint8_t):
7430Sstevel@tonic-gate 			lval = *((uint8_t *)ldata);
7440Sstevel@tonic-gate 			rval = *((uint8_t *)rdata);
7450Sstevel@tonic-gate 			break;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		default:
7482777Stomee 			switch (lrec->dtrd_action) {
7492777Stomee 			case DTRACEACT_UMOD:
7502777Stomee 			case DTRACEACT_UADDR:
7512777Stomee 			case DTRACEACT_USYM:
7522777Stomee 				for (j = 0; j < 2; j++) {
7532777Stomee 					/* LINTED - alignment */
7542777Stomee 					lval = ((uint64_t *)ldata)[j];
7552777Stomee 					/* LINTED - alignment */
7562777Stomee 					rval = ((uint64_t *)rdata)[j];
7572777Stomee 
7582777Stomee 					if (lval < rval)
7592777Stomee 						return (DT_LESSTHAN);
7600Sstevel@tonic-gate 
7612777Stomee 					if (lval > rval)
7622777Stomee 						return (DT_GREATERTHAN);
7632777Stomee 				}
7642777Stomee 
7652777Stomee 				break;
7660Sstevel@tonic-gate 
7672777Stomee 			default:
7682777Stomee 				for (j = 0; j < lrec->dtrd_size; j++) {
7692777Stomee 					lval = ((uint8_t *)ldata)[j];
7702777Stomee 					rval = ((uint8_t *)rdata)[j];
7711017Sbmc 
7722777Stomee 					if (lval < rval)
7732777Stomee 						return (DT_LESSTHAN);
7742777Stomee 
7752777Stomee 					if (lval > rval)
7762777Stomee 						return (DT_GREATERTHAN);
7772777Stomee 				}
7780Sstevel@tonic-gate 			}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 			continue;
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		if (lval < rval)
7841017Sbmc 			return (DT_LESSTHAN);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 		if (lval > rval)
7871017Sbmc 			return (DT_GREATERTHAN);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	return (0);
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate static int
dt_aggregate_valcmp(const void * lhs,const void * rhs)7940Sstevel@tonic-gate dt_aggregate_valcmp(const void *lhs, const void *rhs)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
7970Sstevel@tonic-gate 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
7980Sstevel@tonic-gate 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
7990Sstevel@tonic-gate 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
8000Sstevel@tonic-gate 	caddr_t ldata = lh->dtahe_data.dtada_data;
8010Sstevel@tonic-gate 	caddr_t rdata = rh->dtahe_data.dtada_data;
8020Sstevel@tonic-gate 	dtrace_recdesc_t *lrec, *rrec;
803491Sbmc 	int64_t *laddr, *raddr;
8040Sstevel@tonic-gate 	int rval, i;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
8070Sstevel@tonic-gate 		return (rval);
8080Sstevel@tonic-gate 
8091017Sbmc 	if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
8101017Sbmc 		return (DT_GREATERTHAN);
8110Sstevel@tonic-gate 
8121017Sbmc 	if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
8131017Sbmc 		return (DT_LESSTHAN);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	for (i = 0; i < lagg->dtagd_nrecs; i++) {
8160Sstevel@tonic-gate 		lrec = &lagg->dtagd_rec[i];
8170Sstevel@tonic-gate 		rrec = &ragg->dtagd_rec[i];
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		if (lrec->dtrd_offset < rrec->dtrd_offset)
8201017Sbmc 			return (DT_LESSTHAN);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 		if (lrec->dtrd_offset > rrec->dtrd_offset)
8231017Sbmc 			return (DT_GREATERTHAN);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 		if (lrec->dtrd_action < rrec->dtrd_action)
8261017Sbmc 			return (DT_LESSTHAN);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		if (lrec->dtrd_action > rrec->dtrd_action)
8291017Sbmc 			return (DT_GREATERTHAN);
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
832491Sbmc 	laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
833491Sbmc 	raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	switch (lrec->dtrd_action) {
8360Sstevel@tonic-gate 	case DTRACEAGG_AVG:
8370Sstevel@tonic-gate 		rval = dt_aggregate_averagecmp(laddr, raddr);
8380Sstevel@tonic-gate 		break;
8390Sstevel@tonic-gate 
840*5984Sjhaslam 	case DTRACEAGG_STDDEV:
841*5984Sjhaslam 		rval = dt_aggregate_stddevcmp(laddr, raddr);
842*5984Sjhaslam 		break;
843*5984Sjhaslam 
8440Sstevel@tonic-gate 	case DTRACEAGG_QUANTIZE:
8450Sstevel@tonic-gate 		rval = dt_aggregate_quantizedcmp(laddr, raddr);
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	case DTRACEAGG_LQUANTIZE:
8490Sstevel@tonic-gate 		rval = dt_aggregate_lquantizedcmp(laddr, raddr);
8500Sstevel@tonic-gate 		break;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	case DTRACEAGG_COUNT:
8530Sstevel@tonic-gate 	case DTRACEAGG_SUM:
8540Sstevel@tonic-gate 	case DTRACEAGG_MIN:
8550Sstevel@tonic-gate 	case DTRACEAGG_MAX:
8560Sstevel@tonic-gate 		rval = dt_aggregate_countcmp(laddr, raddr);
8570Sstevel@tonic-gate 		break;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	default:
8600Sstevel@tonic-gate 		assert(0);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8631017Sbmc 	return (rval);
8641017Sbmc }
8651017Sbmc 
8661017Sbmc static int
dt_aggregate_valkeycmp(const void * lhs,const void * rhs)8671017Sbmc dt_aggregate_valkeycmp(const void *lhs, const void *rhs)
8681017Sbmc {
8691017Sbmc 	int rval;
8701017Sbmc 
8711017Sbmc 	if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
8720Sstevel@tonic-gate 		return (rval);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * If we're here, the values for the two aggregation elements are
8760Sstevel@tonic-gate 	 * equal.  We already know that the key layout is the same for the two
8770Sstevel@tonic-gate 	 * elements; we must now compare the keys themselves as a tie-breaker.
8780Sstevel@tonic-gate 	 */
8790Sstevel@tonic-gate 	return (dt_aggregate_keycmp(lhs, rhs));
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate static int
dt_aggregate_keyvarcmp(const void * lhs,const void * rhs)8830Sstevel@tonic-gate dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	int rval;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
8880Sstevel@tonic-gate 		return (rval);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	return (dt_aggregate_varcmp(lhs, rhs));
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate static int
dt_aggregate_varkeycmp(const void * lhs,const void * rhs)8940Sstevel@tonic-gate dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	int rval;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
8990Sstevel@tonic-gate 		return (rval);
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	return (dt_aggregate_keycmp(lhs, rhs));
9020Sstevel@tonic-gate }
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate static int
dt_aggregate_valvarcmp(const void * lhs,const void * rhs)9050Sstevel@tonic-gate dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
9060Sstevel@tonic-gate {
9070Sstevel@tonic-gate 	int rval;
9080Sstevel@tonic-gate 
9091017Sbmc 	if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
9100Sstevel@tonic-gate 		return (rval);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	return (dt_aggregate_varcmp(lhs, rhs));
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate static int
dt_aggregate_varvalcmp(const void * lhs,const void * rhs)9160Sstevel@tonic-gate dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	int rval;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
9210Sstevel@tonic-gate 		return (rval);
9220Sstevel@tonic-gate 
9231017Sbmc 	return (dt_aggregate_valkeycmp(lhs, rhs));
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate static int
dt_aggregate_keyvarrevcmp(const void * lhs,const void * rhs)9270Sstevel@tonic-gate dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
9280Sstevel@tonic-gate {
9290Sstevel@tonic-gate 	return (dt_aggregate_keyvarcmp(rhs, lhs));
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate static int
dt_aggregate_varkeyrevcmp(const void * lhs,const void * rhs)9330Sstevel@tonic-gate dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate 	return (dt_aggregate_varkeycmp(rhs, lhs));
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate static int
dt_aggregate_valvarrevcmp(const void * lhs,const void * rhs)9390Sstevel@tonic-gate dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
9400Sstevel@tonic-gate {
9410Sstevel@tonic-gate 	return (dt_aggregate_valvarcmp(rhs, lhs));
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate static int
dt_aggregate_varvalrevcmp(const void * lhs,const void * rhs)9450Sstevel@tonic-gate dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
9460Sstevel@tonic-gate {
9470Sstevel@tonic-gate 	return (dt_aggregate_varvalcmp(rhs, lhs));
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9501017Sbmc static int
dt_aggregate_bundlecmp(const void * lhs,const void * rhs)9511017Sbmc dt_aggregate_bundlecmp(const void *lhs, const void *rhs)
9521017Sbmc {
9531017Sbmc 	dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
9541017Sbmc 	dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
9551017Sbmc 	int i, rval;
9561017Sbmc 
9571017Sbmc 	if (dt_keysort) {
9581017Sbmc 		/*
9591017Sbmc 		 * If we're sorting on keys, we need to scan until we find the
9601017Sbmc 		 * last entry -- that's the representative key.  (The order of
9611017Sbmc 		 * the bundle is values followed by key to accommodate the
9621017Sbmc 		 * default behavior of sorting by value.)  If the keys are
9631017Sbmc 		 * equal, we'll fall into the value comparison loop, below.
9641017Sbmc 		 */
9651017Sbmc 		for (i = 0; lh[i + 1] != NULL; i++)
9661017Sbmc 			continue;
9671017Sbmc 
9681017Sbmc 		assert(i != 0);
9691017Sbmc 		assert(rh[i + 1] == NULL);
9701017Sbmc 
9711017Sbmc 		if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
9721017Sbmc 			return (rval);
9731017Sbmc 	}
9741017Sbmc 
9751017Sbmc 	for (i = 0; ; i++) {
9761017Sbmc 		if (lh[i + 1] == NULL) {
9771017Sbmc 			/*
9781017Sbmc 			 * All of the values are equal; if we're sorting on
9791017Sbmc 			 * keys, then we're only here because the keys were
9801017Sbmc 			 * found to be equal and these records are therefore
9811017Sbmc 			 * equal.  If we're not sorting on keys, we'll use the
9821017Sbmc 			 * key comparison from the representative key as the
9831017Sbmc 			 * tie-breaker.
9841017Sbmc 			 */
9851017Sbmc 			if (dt_keysort)
9861017Sbmc 				return (0);
9871017Sbmc 
9881017Sbmc 			assert(i != 0);
9891017Sbmc 			assert(rh[i + 1] == NULL);
9901017Sbmc 			return (dt_aggregate_keycmp(&lh[i], &rh[i]));
9911017Sbmc 		} else {
9921017Sbmc 			if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
9931017Sbmc 				return (rval);
9941017Sbmc 		}
9951017Sbmc 	}
9961017Sbmc }
9971017Sbmc 
9980Sstevel@tonic-gate int
dt_aggregate_go(dtrace_hdl_t * dtp)9990Sstevel@tonic-gate dt_aggregate_go(dtrace_hdl_t *dtp)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
10020Sstevel@tonic-gate 	dtrace_optval_t size, cpu;
10030Sstevel@tonic-gate 	dtrace_bufdesc_t *buf = &agp->dtat_buf;
10040Sstevel@tonic-gate 	int rval, i;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	assert(agp->dtat_maxcpu == 0);
10070Sstevel@tonic-gate 	assert(agp->dtat_ncpu == 0);
10080Sstevel@tonic-gate 	assert(agp->dtat_cpus == NULL);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
10110Sstevel@tonic-gate 	agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
10120Sstevel@tonic-gate 	agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (agp->dtat_cpus == NULL)
10150Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	/*
10180Sstevel@tonic-gate 	 * Use the aggregation buffer size as reloaded from the kernel.
10190Sstevel@tonic-gate 	 */
10200Sstevel@tonic-gate 	size = dtp->dt_options[DTRACEOPT_AGGSIZE];
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "aggsize", &size);
10230Sstevel@tonic-gate 	assert(rval == 0);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (size == 0 || size == DTRACEOPT_UNSET)
10260Sstevel@tonic-gate 		return (0);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	buf = &agp->dtat_buf;
10290Sstevel@tonic-gate 	buf->dtbd_size = size;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
10320Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_NOMEM));
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	/*
10350Sstevel@tonic-gate 	 * Now query for the CPUs enabled.
10360Sstevel@tonic-gate 	 */
10370Sstevel@tonic-gate 	rval = dtrace_getopt(dtp, "cpu", &cpu);
10380Sstevel@tonic-gate 	assert(rval == 0 && cpu != DTRACEOPT_UNSET);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	if (cpu != DTRACE_CPUALL) {
10410Sstevel@tonic-gate 		assert(cpu < agp->dtat_ncpu);
10420Sstevel@tonic-gate 		agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		return (0);
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	agp->dtat_ncpus = 0;
10480Sstevel@tonic-gate 	for (i = 0; i < agp->dtat_maxcpu; i++) {
10490Sstevel@tonic-gate 		if (dt_status(dtp, i) == -1)
10500Sstevel@tonic-gate 			continue;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		agp->dtat_cpus[agp->dtat_ncpus++] = i;
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	return (0);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate static int
dt_aggwalk_rval(dtrace_hdl_t * dtp,dt_ahashent_t * h,int rval)10590Sstevel@tonic-gate dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
10600Sstevel@tonic-gate {
10610Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
10620Sstevel@tonic-gate 	dtrace_aggdata_t *data;
10630Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc;
10640Sstevel@tonic-gate 	dtrace_recdesc_t *rec;
10650Sstevel@tonic-gate 	int i;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	switch (rval) {
10680Sstevel@tonic-gate 	case DTRACE_AGGWALK_NEXT:
10690Sstevel@tonic-gate 		break;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	case DTRACE_AGGWALK_CLEAR: {
10720Sstevel@tonic-gate 		uint32_t size, offs = 0;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 		aggdesc = h->dtahe_data.dtada_desc;
10750Sstevel@tonic-gate 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
10760Sstevel@tonic-gate 		size = rec->dtrd_size;
10770Sstevel@tonic-gate 		data = &h->dtahe_data;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
10800Sstevel@tonic-gate 			offs = sizeof (uint64_t);
10810Sstevel@tonic-gate 			size -= sizeof (uint64_t);
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		if (data->dtada_percpu == NULL)
10870Sstevel@tonic-gate 			break;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 		for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
10900Sstevel@tonic-gate 			bzero(data->dtada_percpu[i] + offs, size);
10910Sstevel@tonic-gate 		break;
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	case DTRACE_AGGWALK_ERROR:
10950Sstevel@tonic-gate 		/*
10960Sstevel@tonic-gate 		 * We assume that errno is already set in this case.
10970Sstevel@tonic-gate 		 */
10980Sstevel@tonic-gate 		return (dt_set_errno(dtp, errno));
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	case DTRACE_AGGWALK_ABORT:
11010Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_DIRABORT));
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	case DTRACE_AGGWALK_DENORMALIZE:
11040Sstevel@tonic-gate 		h->dtahe_data.dtada_normal = 1;
11050Sstevel@tonic-gate 		return (0);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	case DTRACE_AGGWALK_NORMALIZE:
11080Sstevel@tonic-gate 		if (h->dtahe_data.dtada_normal == 0) {
11090Sstevel@tonic-gate 			h->dtahe_data.dtada_normal = 1;
11100Sstevel@tonic-gate 			return (dt_set_errno(dtp, EDT_BADRVAL));
11110Sstevel@tonic-gate 		}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 		return (0);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	case DTRACE_AGGWALK_REMOVE: {
11160Sstevel@tonic-gate 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
11170Sstevel@tonic-gate 		int i, max_cpus = agp->dtat_maxcpu;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 		/*
11200Sstevel@tonic-gate 		 * First, remove this hash entry from its hash chain.
11210Sstevel@tonic-gate 		 */
11220Sstevel@tonic-gate 		if (h->dtahe_prev != NULL) {
11230Sstevel@tonic-gate 			h->dtahe_prev->dtahe_next = h->dtahe_next;
11240Sstevel@tonic-gate 		} else {
11250Sstevel@tonic-gate 			dt_ahash_t *hash = &agp->dtat_hash;
11260Sstevel@tonic-gate 			size_t ndx = h->dtahe_hashval % hash->dtah_size;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 			assert(hash->dtah_hash[ndx] == h);
11290Sstevel@tonic-gate 			hash->dtah_hash[ndx] = h->dtahe_next;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 		if (h->dtahe_next != NULL)
11330Sstevel@tonic-gate 			h->dtahe_next->dtahe_prev = h->dtahe_prev;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		/*
11360Sstevel@tonic-gate 		 * Now remove it from the list of all hash entries.
11370Sstevel@tonic-gate 		 */
11380Sstevel@tonic-gate 		if (h->dtahe_prevall != NULL) {
11390Sstevel@tonic-gate 			h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
11400Sstevel@tonic-gate 		} else {
11410Sstevel@tonic-gate 			dt_ahash_t *hash = &agp->dtat_hash;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 			assert(hash->dtah_all == h);
11440Sstevel@tonic-gate 			hash->dtah_all = h->dtahe_nextall;
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		if (h->dtahe_nextall != NULL)
11480Sstevel@tonic-gate 			h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 		/*
11510Sstevel@tonic-gate 		 * We're unlinked.  We can safely destroy the data.
11520Sstevel@tonic-gate 		 */
11530Sstevel@tonic-gate 		if (aggdata->dtada_percpu != NULL) {
11540Sstevel@tonic-gate 			for (i = 0; i < max_cpus; i++)
11550Sstevel@tonic-gate 				free(aggdata->dtada_percpu[i]);
11560Sstevel@tonic-gate 			free(aggdata->dtada_percpu);
11570Sstevel@tonic-gate 		}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		free(aggdata->dtada_data);
11600Sstevel@tonic-gate 		free(h);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 		return (0);
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	default:
11660Sstevel@tonic-gate 		return (dt_set_errno(dtp, EDT_BADRVAL));
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	return (0);
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11721017Sbmc void
dt_aggregate_qsort(dtrace_hdl_t * dtp,void * base,size_t nel,size_t width,int (* compar)(const void *,const void *))11731017Sbmc dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
11741017Sbmc     int (*compar)(const void *, const void *))
11751017Sbmc {
11761017Sbmc 	int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
11771017Sbmc 	dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
11781017Sbmc 
11791017Sbmc 	dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
11801017Sbmc 	dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
11811017Sbmc 
11821017Sbmc 	if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
11831017Sbmc 		dt_keypos = (int)keyposopt;
11841017Sbmc 	} else {
11851017Sbmc 		dt_keypos = 0;
11861017Sbmc 	}
11871017Sbmc 
11881017Sbmc 	if (compar == NULL) {
11891017Sbmc 		if (!dt_keysort) {
11901017Sbmc 			compar = dt_aggregate_varvalcmp;
11911017Sbmc 		} else {
11921017Sbmc 			compar = dt_aggregate_varkeycmp;
11931017Sbmc 		}
11941017Sbmc 	}
11951017Sbmc 
11961017Sbmc 	qsort(base, nel, width, compar);
11971017Sbmc 
11981017Sbmc 	dt_revsort = rev;
11991017Sbmc 	dt_keysort = key;
12001017Sbmc 	dt_keypos = keypos;
12011017Sbmc }
12021017Sbmc 
12030Sstevel@tonic-gate int
dtrace_aggregate_walk(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)12040Sstevel@tonic-gate dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
12050Sstevel@tonic-gate {
12060Sstevel@tonic-gate 	dt_ahashent_t *h, *next;
12070Sstevel@tonic-gate 	dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	for (h = hash->dtah_all; h != NULL; h = next) {
12100Sstevel@tonic-gate 		/*
12110Sstevel@tonic-gate 		 * dt_aggwalk_rval() can potentially remove the current hash
12120Sstevel@tonic-gate 		 * entry; we need to load the next hash entry before calling
12130Sstevel@tonic-gate 		 * into it.
12140Sstevel@tonic-gate 		 */
12150Sstevel@tonic-gate 		next = h->dtahe_nextall;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
12180Sstevel@tonic-gate 			return (-1);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	return (0);
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate static int
dt_aggregate_walk_sorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg,int (* sfunc)(const void *,const void *))12250Sstevel@tonic-gate dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
12260Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg,
12270Sstevel@tonic-gate     int (*sfunc)(const void *, const void *))
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
12300Sstevel@tonic-gate 	dt_ahashent_t *h, **sorted;
12310Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
12320Sstevel@tonic-gate 	size_t i, nentries = 0;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
12350Sstevel@tonic-gate 		nentries++;
12360Sstevel@tonic-gate 
12371017Sbmc 	sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	if (sorted == NULL)
12401017Sbmc 		return (-1);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
12430Sstevel@tonic-gate 		sorted[i++] = h;
12440Sstevel@tonic-gate 
12451017Sbmc 	(void) pthread_mutex_lock(&dt_qsort_lock);
12461017Sbmc 
12471017Sbmc 	if (sfunc == NULL) {
12481017Sbmc 		dt_aggregate_qsort(dtp, sorted, nentries,
12491017Sbmc 		    sizeof (dt_ahashent_t *), NULL);
12501017Sbmc 	} else {
12511017Sbmc 		/*
12521017Sbmc 		 * If we've been explicitly passed a sorting function,
12531017Sbmc 		 * we'll use that -- ignoring the values of the "aggsortrev",
12541017Sbmc 		 * "aggsortkey" and "aggsortkeypos" options.
12551017Sbmc 		 */
12561017Sbmc 		qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
12571017Sbmc 	}
12581017Sbmc 
12591017Sbmc 	(void) pthread_mutex_unlock(&dt_qsort_lock);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	for (i = 0; i < nentries; i++) {
12620Sstevel@tonic-gate 		h = sorted[i];
12630Sstevel@tonic-gate 
12641017Sbmc 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1) {
12651017Sbmc 			dt_free(dtp, sorted);
12660Sstevel@tonic-gate 			return (-1);
12671017Sbmc 		}
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 
12701017Sbmc 	dt_free(dtp, sorted);
12710Sstevel@tonic-gate 	return (0);
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate int
dtrace_aggregate_walk_sorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)12751017Sbmc dtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
12761017Sbmc     dtrace_aggregate_f *func, void *arg)
12771017Sbmc {
12781017Sbmc 	return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
12791017Sbmc }
12801017Sbmc 
12811017Sbmc int
dtrace_aggregate_walk_keysorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)12820Sstevel@tonic-gate dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
12830Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
12840Sstevel@tonic-gate {
12850Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
12860Sstevel@tonic-gate 	    arg, dt_aggregate_varkeycmp));
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate int
dtrace_aggregate_walk_valsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)12900Sstevel@tonic-gate dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
12910Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
12920Sstevel@tonic-gate {
12930Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
12940Sstevel@tonic-gate 	    arg, dt_aggregate_varvalcmp));
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate int
dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)12980Sstevel@tonic-gate dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
12990Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
13000Sstevel@tonic-gate {
13010Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
13020Sstevel@tonic-gate 	    arg, dt_aggregate_keyvarcmp));
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate int
dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)13060Sstevel@tonic-gate dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
13070Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
13100Sstevel@tonic-gate 	    arg, dt_aggregate_valvarcmp));
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate int
dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)13140Sstevel@tonic-gate dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
13150Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
13160Sstevel@tonic-gate {
13170Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
13180Sstevel@tonic-gate 	    arg, dt_aggregate_varkeyrevcmp));
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate int
dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)13220Sstevel@tonic-gate dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
13230Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
13240Sstevel@tonic-gate {
13250Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
13260Sstevel@tonic-gate 	    arg, dt_aggregate_varvalrevcmp));
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate int
dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)13300Sstevel@tonic-gate dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
13310Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
13340Sstevel@tonic-gate 	    arg, dt_aggregate_keyvarrevcmp));
13350Sstevel@tonic-gate }
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate int
dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t * dtp,dtrace_aggregate_f * func,void * arg)13380Sstevel@tonic-gate dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
13390Sstevel@tonic-gate     dtrace_aggregate_f *func, void *arg)
13400Sstevel@tonic-gate {
13410Sstevel@tonic-gate 	return (dt_aggregate_walk_sorted(dtp, func,
13420Sstevel@tonic-gate 	    arg, dt_aggregate_valvarrevcmp));
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate int
dtrace_aggregate_walk_joined(dtrace_hdl_t * dtp,dtrace_aggvarid_t * aggvars,int naggvars,dtrace_aggregate_walk_joined_f * func,void * arg)13461017Sbmc dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
13471017Sbmc     int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
13481017Sbmc {
13491017Sbmc 	dt_aggregate_t *agp = &dtp->dt_aggregate;
13501017Sbmc 	dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
13511017Sbmc 	const dtrace_aggdata_t **data;
13521017Sbmc 	dt_ahashent_t *zaggdata = NULL;
13531017Sbmc 	dt_ahash_t *hash = &agp->dtat_hash;
13541017Sbmc 	size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
13551017Sbmc 	dtrace_aggvarid_t max = 0, aggvar;
13561017Sbmc 	int rval = -1, *map, *remap = NULL;
13571017Sbmc 	int i, j;
13581017Sbmc 	dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
13591017Sbmc 
13601017Sbmc 	/*
13611017Sbmc 	 * If the sorting position is greater than the number of aggregation
13621017Sbmc 	 * variable IDs, we silently set it to 0.
13631017Sbmc 	 */
13641017Sbmc 	if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
13651017Sbmc 		sortpos = 0;
13661017Sbmc 
13671017Sbmc 	/*
13681017Sbmc 	 * First we need to translate the specified aggregation variable IDs
13691017Sbmc 	 * into a linear map that will allow us to translate an aggregation
13701017Sbmc 	 * variable ID into its position in the specified aggvars.
13711017Sbmc 	 */
13721017Sbmc 	for (i = 0; i < naggvars; i++) {
13731017Sbmc 		if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
13741017Sbmc 			return (dt_set_errno(dtp, EDT_BADAGGVAR));
13751017Sbmc 
13761017Sbmc 		if (aggvars[i] > max)
13771017Sbmc 			max = aggvars[i];
13781017Sbmc 	}
13791017Sbmc 
13801017Sbmc 	if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
13811017Sbmc 		return (-1);
13821017Sbmc 
13831017Sbmc 	zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
13841017Sbmc 
13851017Sbmc 	if (zaggdata == NULL)
13861017Sbmc 		goto out;
13871017Sbmc 
13881017Sbmc 	for (i = 0; i < naggvars; i++) {
13891017Sbmc 		int ndx = i + sortpos;
13901017Sbmc 
13911017Sbmc 		if (ndx >= naggvars)
13921017Sbmc 			ndx -= naggvars;
13931017Sbmc 
13941017Sbmc 		aggvar = aggvars[ndx];
13951017Sbmc 		assert(aggvar <= max);
13961017Sbmc 
13971017Sbmc 		if (map[aggvar]) {
13981017Sbmc 			/*
13991017Sbmc 			 * We have an aggregation variable that is present
14001017Sbmc 			 * more than once in the array of aggregation
14011017Sbmc 			 * variables.  While it's unclear why one might want
14021017Sbmc 			 * to do this, it's legal.  To support this construct,
14031017Sbmc 			 * we will allocate a remap that will indicate the
14041017Sbmc 			 * position from which this aggregation variable
14051017Sbmc 			 * should be pulled.  (That is, where the remap will
14061017Sbmc 			 * map from one position to another.)
14071017Sbmc 			 */
14081017Sbmc 			if (remap == NULL) {
14091017Sbmc 				remap = dt_zalloc(dtp, naggvars * sizeof (int));
14101017Sbmc 
14111017Sbmc 				if (remap == NULL)
14121017Sbmc 					goto out;
14131017Sbmc 			}
14141017Sbmc 
14151017Sbmc 			/*
14161017Sbmc 			 * Given that the variable is already present, assert
14171017Sbmc 			 * that following through the mapping and adjusting
14181017Sbmc 			 * for the sort position yields the same aggregation
14191017Sbmc 			 * variable ID.
14201017Sbmc 			 */
14211017Sbmc 			assert(aggvars[(map[aggvar] - 1 + sortpos) %
14221017Sbmc 			    naggvars] == aggvars[ndx]);
14231017Sbmc 
14241017Sbmc 			remap[i] = map[aggvar];
14251017Sbmc 			continue;
14261017Sbmc 		}
14271017Sbmc 
14281017Sbmc 		map[aggvar] = i + 1;
14291017Sbmc 	}
14301017Sbmc 
14311017Sbmc 	/*
14321017Sbmc 	 * We need to take two passes over the data to size our allocation, so
14331017Sbmc 	 * we'll use the first pass to also fill in the zero-filled data to be
14341017Sbmc 	 * used to properly format a zero-valued aggregation.
14351017Sbmc 	 */
14361017Sbmc 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
14371017Sbmc 		dtrace_aggvarid_t id;
14381017Sbmc 		int ndx;
14391017Sbmc 
14401017Sbmc 		if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
14411017Sbmc 			continue;
14421017Sbmc 
14431017Sbmc 		if (zaggdata[ndx - 1].dtahe_size == 0) {
14441017Sbmc 			zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
14451017Sbmc 			zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
14461017Sbmc 		}
14471017Sbmc 
14481017Sbmc 		nentries++;
14491017Sbmc 	}
14501017Sbmc 
14511017Sbmc 	if (nentries == 0) {
14521017Sbmc 		/*
14531017Sbmc 		 * We couldn't find any entries; there is nothing else to do.
14541017Sbmc 		 */
14551017Sbmc 		rval = 0;
14561017Sbmc 		goto out;
14571017Sbmc 	}
14581017Sbmc 
14591017Sbmc 	/*
14601017Sbmc 	 * Before we sort the data, we're going to look for any holes in our
14611017Sbmc 	 * zero-filled data.  This will occur if an aggregation variable that
14621017Sbmc 	 * we are being asked to print has not yet been assigned the result of
14631017Sbmc 	 * any aggregating action for _any_ tuple.  The issue becomes that we
14641017Sbmc 	 * would like a zero value to be printed for all columns for this
14651017Sbmc 	 * aggregation, but without any record description, we don't know the
14661017Sbmc 	 * aggregating action that corresponds to the aggregation variable.  To
14671017Sbmc 	 * try to find a match, we're simply going to lookup aggregation IDs
14681017Sbmc 	 * (which are guaranteed to be contiguous and to start from 1), looking
14691017Sbmc 	 * for the specified aggregation variable ID.  If we find a match,
14701017Sbmc 	 * we'll use that.  If we iterate over all aggregation IDs and don't
14711017Sbmc 	 * find a match, then we must be an anonymous enabling.  (Anonymous
14721017Sbmc 	 * enablings can't currently derive either aggregation variable IDs or
14731017Sbmc 	 * aggregation variable names given only an aggregation ID.)  In this
14741017Sbmc 	 * obscure case (anonymous enabling, multiple aggregation printa() with
14751017Sbmc 	 * some aggregations not represented for any tuple), our defined
14761017Sbmc 	 * behavior is that the zero will be printed in the format of the first
14771017Sbmc 	 * aggregation variable that contains any non-zero value.
14781017Sbmc 	 */
14791017Sbmc 	for (i = 0; i < naggvars; i++) {
14801017Sbmc 		if (zaggdata[i].dtahe_size == 0) {
14811017Sbmc 			dtrace_aggvarid_t aggvar;
14821017Sbmc 
14831017Sbmc 			aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
14841017Sbmc 			assert(zaggdata[i].dtahe_data.dtada_data == NULL);
14851017Sbmc 
14861017Sbmc 			for (j = DTRACE_AGGIDNONE + 1; ; j++) {
14871017Sbmc 				dtrace_aggdesc_t *agg;
14881017Sbmc 				dtrace_aggdata_t *aggdata;
14891017Sbmc 
14901017Sbmc 				if (dt_aggid_lookup(dtp, j, &agg) != 0)
14911017Sbmc 					break;
14921017Sbmc 
14931017Sbmc 				if (agg->dtagd_varid != aggvar)
14941017Sbmc 					continue;
14951017Sbmc 
14961017Sbmc 				/*
14971017Sbmc 				 * We have our description -- now we need to
14981017Sbmc 				 * cons up the zaggdata entry for it.
14991017Sbmc 				 */
15001017Sbmc 				aggdata = &zaggdata[i].dtahe_data;
15011017Sbmc 				aggdata->dtada_size = agg->dtagd_size;
15021017Sbmc 				aggdata->dtada_desc = agg;
15031017Sbmc 				aggdata->dtada_handle = dtp;
15041017Sbmc 				(void) dt_epid_lookup(dtp, agg->dtagd_epid,
15051017Sbmc 				    &aggdata->dtada_edesc,
15061017Sbmc 				    &aggdata->dtada_pdesc);
15071017Sbmc 				aggdata->dtada_normal = 1;
15081017Sbmc 				zaggdata[i].dtahe_hashval = 0;
15091017Sbmc 				zaggdata[i].dtahe_size = agg->dtagd_size;
15101017Sbmc 				break;
15111017Sbmc 			}
15121017Sbmc 
15131017Sbmc 			if (zaggdata[i].dtahe_size == 0) {
15141017Sbmc 				caddr_t data;
15151017Sbmc 
15161017Sbmc 				/*
15171017Sbmc 				 * We couldn't find this aggregation, meaning
15181017Sbmc 				 * that we have never seen it before for any
15191017Sbmc 				 * tuple _and_ this is an anonymous enabling.
15201017Sbmc 				 * That is, we're in the obscure case outlined
15211017Sbmc 				 * above.  In this case, our defined behavior
15221017Sbmc 				 * is to format the data in the format of the
15231017Sbmc 				 * first non-zero aggregation -- of which, of
15241017Sbmc 				 * course, we know there to be at least one
15251017Sbmc 				 * (or nentries would have been zero).
15261017Sbmc 				 */
15271017Sbmc 				for (j = 0; j < naggvars; j++) {
15281017Sbmc 					if (zaggdata[j].dtahe_size != 0)
15291017Sbmc 						break;
15301017Sbmc 				}
15311017Sbmc 
15321017Sbmc 				assert(j < naggvars);
15331017Sbmc 				zaggdata[i] = zaggdata[j];
15341017Sbmc 
15351017Sbmc 				data = zaggdata[i].dtahe_data.dtada_data;
15361017Sbmc 				assert(data != NULL);
15371017Sbmc 			}
15381017Sbmc 		}
15391017Sbmc 	}
15401017Sbmc 
15411017Sbmc 	/*
15421017Sbmc 	 * Now we need to allocate our zero-filled data for use for
15431017Sbmc 	 * aggregations that don't have a value corresponding to a given key.
15441017Sbmc 	 */
15451017Sbmc 	for (i = 0; i < naggvars; i++) {
15461017Sbmc 		dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
15471017Sbmc 		dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
15481017Sbmc 		dtrace_recdesc_t *rec;
15491017Sbmc 		uint64_t larg;
15501017Sbmc 		caddr_t zdata;
15511017Sbmc 
15521017Sbmc 		zsize = zaggdata[i].dtahe_size;
15531017Sbmc 		assert(zsize != 0);
15541017Sbmc 
15551017Sbmc 		if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
15561017Sbmc 			/*
15571017Sbmc 			 * If we failed to allocated some zero-filled data, we
15581017Sbmc 			 * need to zero out the remaining dtada_data pointers
15591017Sbmc 			 * to prevent the wrong data from being freed below.
15601017Sbmc 			 */
15611017Sbmc 			for (j = i; j < naggvars; j++)
15621017Sbmc 				zaggdata[j].dtahe_data.dtada_data = NULL;
15631017Sbmc 			goto out;
15641017Sbmc 		}
15651017Sbmc 
15661017Sbmc 		aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
15671017Sbmc 
15681017Sbmc 		/*
15691017Sbmc 		 * First, the easy bit.  To maintain compatibility with
15701017Sbmc 		 * consumers that pull the compiler-generated ID out of the
15711017Sbmc 		 * data, we put that ID at the top of the zero-filled data.
15721017Sbmc 		 */
15731017Sbmc 		rec = &aggdesc->dtagd_rec[0];
15741017Sbmc 		/* LINTED - alignment */
15751017Sbmc 		*((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
15761017Sbmc 
15771017Sbmc 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
15781017Sbmc 
15791017Sbmc 		/*
15801017Sbmc 		 * Now for the more complicated part.  If (and only if) this
15811017Sbmc 		 * is an lquantize() aggregating action, zero-filled data is
15821017Sbmc 		 * not equivalent to an empty record:  we must also get the
15831017Sbmc 		 * parameters for the lquantize().
15841017Sbmc 		 */
15851017Sbmc 		if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
15861017Sbmc 			if (aggdata->dtada_data != NULL) {
15871017Sbmc 				/*
15881017Sbmc 				 * The easier case here is if we actually have
15891017Sbmc 				 * some prototype data -- in which case we
15901017Sbmc 				 * manually dig it out of the aggregation
15911017Sbmc 				 * record.
15921017Sbmc 				 */
15931017Sbmc 				/* LINTED - alignment */
15941017Sbmc 				larg = *((uint64_t *)(aggdata->dtada_data +
15951017Sbmc 				    rec->dtrd_offset));
15961017Sbmc 			} else {
15971017Sbmc 				/*
15981017Sbmc 				 * We don't have any prototype data.  As a
15991017Sbmc 				 * result, we know that we _do_ have the
16001017Sbmc 				 * compiler-generated information.  (If this
16011017Sbmc 				 * were an anonymous enabling, all of our
16021017Sbmc 				 * zero-filled data would have prototype data
16031017Sbmc 				 * -- either directly or indirectly.) So as
16041017Sbmc 				 * gross as it is, we'll grovel around in the
16051017Sbmc 				 * compiler-generated information to find the
16061017Sbmc 				 * lquantize() parameters.
16071017Sbmc 				 */
16081017Sbmc 				dtrace_stmtdesc_t *sdp;
16091017Sbmc 				dt_ident_t *aid;
16101017Sbmc 				dt_idsig_t *isp;
16111017Sbmc 
16121017Sbmc 				sdp = (dtrace_stmtdesc_t *)(uintptr_t)
16131017Sbmc 				    aggdesc->dtagd_rec[0].dtrd_uarg;
16141017Sbmc 				aid = sdp->dtsd_aggdata;
16151017Sbmc 				isp = (dt_idsig_t *)aid->di_data;
16161017Sbmc 				assert(isp->dis_auxinfo != 0);
16171017Sbmc 				larg = isp->dis_auxinfo;
16181017Sbmc 			}
16191017Sbmc 
16201017Sbmc 			/* LINTED - alignment */
16211017Sbmc 			*((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
16221017Sbmc 		}
16231017Sbmc 
16241017Sbmc 		aggdata->dtada_data = zdata;
16251017Sbmc 	}
16261017Sbmc 
16271017Sbmc 	/*
16281017Sbmc 	 * Now that we've dealt with setting up our zero-filled data, we can
16291017Sbmc 	 * allocate our sorted array, and take another pass over the data to
16301017Sbmc 	 * fill it.
16311017Sbmc 	 */
16321017Sbmc 	sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
16331017Sbmc 
16341017Sbmc 	if (sorted == NULL)
16351017Sbmc 		goto out;
16361017Sbmc 
16371017Sbmc 	for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
16381017Sbmc 		dtrace_aggvarid_t id;
16391017Sbmc 
16401017Sbmc 		if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
16411017Sbmc 			continue;
16421017Sbmc 
16431017Sbmc 		sorted[i++] = h;
16441017Sbmc 	}
16451017Sbmc 
16461017Sbmc 	assert(i == nentries);
16471017Sbmc 
16481017Sbmc 	/*
16491017Sbmc 	 * We've loaded our array; now we need to sort by value to allow us
16501017Sbmc 	 * to create bundles of like value.  We're going to acquire the
16511017Sbmc 	 * dt_qsort_lock here, and hold it across all of our subsequent
16521017Sbmc 	 * comparison and sorting.
16531017Sbmc 	 */
16541017Sbmc 	(void) pthread_mutex_lock(&dt_qsort_lock);
16551017Sbmc 
16561017Sbmc 	qsort(sorted, nentries, sizeof (dt_ahashent_t *),
16571017Sbmc 	    dt_aggregate_keyvarcmp);
16581017Sbmc 
16591017Sbmc 	/*
16601017Sbmc 	 * Now we need to go through and create bundles.  Because the number
16611017Sbmc 	 * of bundles is bounded by the size of the sorted array, we're going
16621017Sbmc 	 * to reuse the underlying storage.  And note that "bundle" is an
16631017Sbmc 	 * array of pointers to arrays of pointers to dt_ahashent_t -- making
16641017Sbmc 	 * its type (regrettably) "dt_ahashent_t ***".  (Regrettable because
16651017Sbmc 	 * '*' -- like '_' and 'X' -- should never appear in triplicate in
16661017Sbmc 	 * an ideal world.)
16671017Sbmc 	 */
16681017Sbmc 	bundle = (dt_ahashent_t ***)sorted;
16691017Sbmc 
16701017Sbmc 	for (i = 1, start = 0; i <= nentries; i++) {
16711017Sbmc 		if (i < nentries &&
16721017Sbmc 		    dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
16731017Sbmc 			continue;
16741017Sbmc 
16751017Sbmc 		/*
16761017Sbmc 		 * We have a bundle boundary.  Everything from start to
16771017Sbmc 		 * (i - 1) belongs in one bundle.
16781017Sbmc 		 */
16791017Sbmc 		assert(i - start <= naggvars);
16801017Sbmc 		bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
16811017Sbmc 
16821017Sbmc 		if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
16831017Sbmc 			(void) pthread_mutex_unlock(&dt_qsort_lock);
16841017Sbmc 			goto out;
16851017Sbmc 		}
16861017Sbmc 
16871017Sbmc 		for (j = start; j < i; j++) {
16881017Sbmc 			dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
16891017Sbmc 
16901017Sbmc 			assert(id <= max);
16911017Sbmc 			assert(map[id] != 0);
16921017Sbmc 			assert(map[id] - 1 < naggvars);
16931017Sbmc 			assert(nbundle[map[id] - 1] == NULL);
16941017Sbmc 			nbundle[map[id] - 1] = sorted[j];
16951017Sbmc 
16961017Sbmc 			if (nbundle[naggvars] == NULL)
16971017Sbmc 				nbundle[naggvars] = sorted[j];
16981017Sbmc 		}
16991017Sbmc 
17001017Sbmc 		for (j = 0; j < naggvars; j++) {
17011017Sbmc 			if (nbundle[j] != NULL)
17021017Sbmc 				continue;
17031017Sbmc 
17041017Sbmc 			/*
17051017Sbmc 			 * Before we assume that this aggregation variable
17061017Sbmc 			 * isn't present (and fall back to using the
17071017Sbmc 			 * zero-filled data allocated earlier), check the
17081017Sbmc 			 * remap.  If we have a remapping, we'll drop it in
17091017Sbmc 			 * here.  Note that we might be remapping an
17101017Sbmc 			 * aggregation variable that isn't present for this
17111017Sbmc 			 * key; in this case, the aggregation data that we
17121017Sbmc 			 * copy will point to the zeroed data.
17131017Sbmc 			 */
17141017Sbmc 			if (remap != NULL && remap[j]) {
17151017Sbmc 				assert(remap[j] - 1 < j);
17161017Sbmc 				assert(nbundle[remap[j] - 1] != NULL);
17171017Sbmc 				nbundle[j] = nbundle[remap[j] - 1];
17181017Sbmc 			} else {
17191017Sbmc 				nbundle[j] = &zaggdata[j];
17201017Sbmc 			}
17211017Sbmc 		}
17221017Sbmc 
17231017Sbmc 		bundle[nbundles++] = nbundle;
17241017Sbmc 		start = i;
17251017Sbmc 	}
17261017Sbmc 
17271017Sbmc 	/*
17281017Sbmc 	 * Now we need to re-sort based on the first value.
17291017Sbmc 	 */
17301017Sbmc 	dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
17311017Sbmc 	    dt_aggregate_bundlecmp);
17321017Sbmc 
17331017Sbmc 	(void) pthread_mutex_unlock(&dt_qsort_lock);
17341017Sbmc 
17351017Sbmc 	/*
17361017Sbmc 	 * We're done!  Now we just need to go back over the sorted bundles,
17371017Sbmc 	 * calling the function.
17381017Sbmc 	 */
17391017Sbmc 	data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
17401017Sbmc 
17411017Sbmc 	for (i = 0; i < nbundles; i++) {
17421017Sbmc 		for (j = 0; j < naggvars; j++)
17431017Sbmc 			data[j + 1] = NULL;
17441017Sbmc 
17451017Sbmc 		for (j = 0; j < naggvars; j++) {
17461017Sbmc 			int ndx = j - sortpos;
17471017Sbmc 
17481017Sbmc 			if (ndx < 0)
17491017Sbmc 				ndx += naggvars;
17501017Sbmc 
17511017Sbmc 			assert(bundle[i][ndx] != NULL);
17521017Sbmc 			data[j + 1] = &bundle[i][ndx]->dtahe_data;
17531017Sbmc 		}
17541017Sbmc 
17551017Sbmc 		for (j = 0; j < naggvars; j++)
17561017Sbmc 			assert(data[j + 1] != NULL);
17571017Sbmc 
17581017Sbmc 		/*
17591017Sbmc 		 * The representative key is the last element in the bundle.
17601017Sbmc 		 * Assert that we have one, and then set it to be the first
17611017Sbmc 		 * element of data.
17621017Sbmc 		 */
17631017Sbmc 		assert(bundle[i][j] != NULL);
17641017Sbmc 		data[0] = &bundle[i][j]->dtahe_data;
17651017Sbmc 
17661017Sbmc 		if ((rval = func(data, naggvars + 1, arg)) == -1)
17671017Sbmc 			goto out;
17681017Sbmc 	}
17691017Sbmc 
17701017Sbmc 	rval = 0;
17711017Sbmc out:
17721017Sbmc 	for (i = 0; i < nbundles; i++)
17731017Sbmc 		dt_free(dtp, bundle[i]);
17741017Sbmc 
17751017Sbmc 	if (zaggdata != NULL) {
17761017Sbmc 		for (i = 0; i < naggvars; i++)
17771017Sbmc 			dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
17781017Sbmc 	}
17791017Sbmc 
17801017Sbmc 	dt_free(dtp, zaggdata);
17811017Sbmc 	dt_free(dtp, sorted);
17821017Sbmc 	dt_free(dtp, remap);
17831017Sbmc 	dt_free(dtp, map);
17841017Sbmc 
17851017Sbmc 	return (rval);
17861017Sbmc }
17871017Sbmc 
17881017Sbmc int
dtrace_aggregate_print(dtrace_hdl_t * dtp,FILE * fp,dtrace_aggregate_walk_f * func)17890Sstevel@tonic-gate dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
17900Sstevel@tonic-gate     dtrace_aggregate_walk_f *func)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	dt_print_aggdata_t pd;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	pd.dtpa_dtp = dtp;
17950Sstevel@tonic-gate 	pd.dtpa_fp = fp;
17960Sstevel@tonic-gate 	pd.dtpa_allunprint = 1;
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 	if (func == NULL)
17991017Sbmc 		func = dtrace_aggregate_walk_sorted;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	if ((*func)(dtp, dt_print_agg, &pd) == -1)
18020Sstevel@tonic-gate 		return (dt_set_errno(dtp, dtp->dt_errno));
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	return (0);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate void
dtrace_aggregate_clear(dtrace_hdl_t * dtp)18080Sstevel@tonic-gate dtrace_aggregate_clear(dtrace_hdl_t *dtp)
18090Sstevel@tonic-gate {
18100Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
18110Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
18120Sstevel@tonic-gate 	dt_ahashent_t *h;
18130Sstevel@tonic-gate 	dtrace_aggdata_t *data;
18140Sstevel@tonic-gate 	dtrace_aggdesc_t *aggdesc;
18150Sstevel@tonic-gate 	dtrace_recdesc_t *rec;
18160Sstevel@tonic-gate 	int i, max_cpus = agp->dtat_maxcpu;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
18190Sstevel@tonic-gate 		aggdesc = h->dtahe_data.dtada_desc;
18200Sstevel@tonic-gate 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
18210Sstevel@tonic-gate 		data = &h->dtahe_data;
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 		bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 		if (data->dtada_percpu == NULL)
18260Sstevel@tonic-gate 			continue;
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 		for (i = 0; i < max_cpus; i++)
18290Sstevel@tonic-gate 			bzero(data->dtada_percpu[i], rec->dtrd_size);
18300Sstevel@tonic-gate 	}
18310Sstevel@tonic-gate }
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate void
dt_aggregate_destroy(dtrace_hdl_t * dtp)18340Sstevel@tonic-gate dt_aggregate_destroy(dtrace_hdl_t *dtp)
18350Sstevel@tonic-gate {
18360Sstevel@tonic-gate 	dt_aggregate_t *agp = &dtp->dt_aggregate;
18370Sstevel@tonic-gate 	dt_ahash_t *hash = &agp->dtat_hash;
18380Sstevel@tonic-gate 	dt_ahashent_t *h, *next;
18390Sstevel@tonic-gate 	dtrace_aggdata_t *aggdata;
18400Sstevel@tonic-gate 	int i, max_cpus = agp->dtat_maxcpu;
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	if (hash->dtah_hash == NULL) {
18430Sstevel@tonic-gate 		assert(hash->dtah_all == NULL);
18440Sstevel@tonic-gate 	} else {
18450Sstevel@tonic-gate 		free(hash->dtah_hash);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 		for (h = hash->dtah_all; h != NULL; h = next) {
18480Sstevel@tonic-gate 			next = h->dtahe_nextall;
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 			aggdata = &h->dtahe_data;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 			if (aggdata->dtada_percpu != NULL) {
18530Sstevel@tonic-gate 				for (i = 0; i < max_cpus; i++)
18540Sstevel@tonic-gate 					free(aggdata->dtada_percpu[i]);
18550Sstevel@tonic-gate 				free(aggdata->dtada_percpu);
18560Sstevel@tonic-gate 			}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 			free(aggdata->dtada_data);
18590Sstevel@tonic-gate 			free(h);
18600Sstevel@tonic-gate 		}
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 		hash->dtah_hash = NULL;
18630Sstevel@tonic-gate 		hash->dtah_all = NULL;
18640Sstevel@tonic-gate 		hash->dtah_size = 0;
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	free(agp->dtat_buf.dtbd_data);
18680Sstevel@tonic-gate 	free(agp->dtat_cpus);
18690Sstevel@tonic-gate }
1870