16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
236ff6d951SJohn Birrell  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
256ff6d951SJohn Birrell  */
266ff6d951SJohn Birrell 
27675cf915SPedro F. Giffuni /*
28a43f0be9SRui Paulo  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29a98ff317SPedro F. Giffuni  * Copyright (c) 2012 by Delphix. All rights reserved.
30675cf915SPedro F. Giffuni  */
316ff6d951SJohn Birrell 
326ff6d951SJohn Birrell #include <stdlib.h>
336ff6d951SJohn Birrell #include <strings.h>
346ff6d951SJohn Birrell #include <errno.h>
356ff6d951SJohn Birrell #include <unistd.h>
366ff6d951SJohn Birrell #include <dt_impl.h>
376ff6d951SJohn Birrell #include <assert.h>
3893f27766SDomagoj Stolfa #include <dt_oformat.h>
39bc96366cSSteven Hartland #ifdef illumos
406ff6d951SJohn Birrell #include <alloca.h>
41d9ce0144SJohn Birrell #else
42d9ce0144SJohn Birrell #include <sys/sysctl.h>
430f2bd1e8SRui Paulo #include <libproc_compat.h>
44d9ce0144SJohn Birrell #endif
456ff6d951SJohn Birrell #include <limits.h>
466ff6d951SJohn Birrell 
476ff6d951SJohn Birrell #define	DTRACE_AHASHSIZE	32779		/* big 'ol prime */
486ff6d951SJohn Birrell 
496ff6d951SJohn Birrell /*
506ff6d951SJohn Birrell  * Because qsort(3C) does not allow an argument to be passed to a comparison
516ff6d951SJohn Birrell  * function, the variables that affect comparison must regrettably be global;
526ff6d951SJohn Birrell  * they are protected by a global static lock, dt_qsort_lock.
536ff6d951SJohn Birrell  */
546ff6d951SJohn Birrell static pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
556ff6d951SJohn Birrell 
566ff6d951SJohn Birrell static int dt_revsort;
576ff6d951SJohn Birrell static int dt_keysort;
586ff6d951SJohn Birrell static int dt_keypos;
596ff6d951SJohn Birrell 
606ff6d951SJohn Birrell #define	DT_LESSTHAN	(dt_revsort == 0 ? -1 : 1)
616ff6d951SJohn Birrell #define	DT_GREATERTHAN	(dt_revsort == 0 ? 1 : -1)
626ff6d951SJohn Birrell 
636ff6d951SJohn Birrell static void
dt_aggregate_count(int64_t * existing,int64_t * new,size_t size)646ff6d951SJohn Birrell dt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
656ff6d951SJohn Birrell {
66d9ce0144SJohn Birrell 	uint_t i;
676ff6d951SJohn Birrell 
686ff6d951SJohn Birrell 	for (i = 0; i < size / sizeof (int64_t); i++)
696ff6d951SJohn Birrell 		existing[i] = existing[i] + new[i];
706ff6d951SJohn Birrell }
716ff6d951SJohn Birrell 
726ff6d951SJohn Birrell static int
dt_aggregate_countcmp(int64_t * lhs,int64_t * rhs)736ff6d951SJohn Birrell dt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
746ff6d951SJohn Birrell {
756ff6d951SJohn Birrell 	int64_t lvar = *lhs;
766ff6d951SJohn Birrell 	int64_t rvar = *rhs;
776ff6d951SJohn Birrell 
786ff6d951SJohn Birrell 	if (lvar < rvar)
796ff6d951SJohn Birrell 		return (DT_LESSTHAN);
806ff6d951SJohn Birrell 
816ff6d951SJohn Birrell 	if (lvar > rvar)
826ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
836ff6d951SJohn Birrell 
846ff6d951SJohn Birrell 	return (0);
856ff6d951SJohn Birrell }
866ff6d951SJohn Birrell 
876ff6d951SJohn Birrell /*ARGSUSED*/
886ff6d951SJohn Birrell static void
dt_aggregate_min(int64_t * existing,int64_t * new,size_t size)896ff6d951SJohn Birrell dt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
906ff6d951SJohn Birrell {
916ff6d951SJohn Birrell 	if (*new < *existing)
926ff6d951SJohn Birrell 		*existing = *new;
936ff6d951SJohn Birrell }
946ff6d951SJohn Birrell 
956ff6d951SJohn Birrell /*ARGSUSED*/
966ff6d951SJohn Birrell static void
dt_aggregate_max(int64_t * existing,int64_t * new,size_t size)976ff6d951SJohn Birrell dt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
986ff6d951SJohn Birrell {
996ff6d951SJohn Birrell 	if (*new > *existing)
1006ff6d951SJohn Birrell 		*existing = *new;
1016ff6d951SJohn Birrell }
1026ff6d951SJohn Birrell 
1036ff6d951SJohn Birrell static int
dt_aggregate_averagecmp(int64_t * lhs,int64_t * rhs)1046ff6d951SJohn Birrell dt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
1056ff6d951SJohn Birrell {
1066ff6d951SJohn Birrell 	int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
1076ff6d951SJohn Birrell 	int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
1086ff6d951SJohn Birrell 
1096ff6d951SJohn Birrell 	if (lavg < ravg)
1106ff6d951SJohn Birrell 		return (DT_LESSTHAN);
1116ff6d951SJohn Birrell 
1126ff6d951SJohn Birrell 	if (lavg > ravg)
1136ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
1146ff6d951SJohn Birrell 
1156ff6d951SJohn Birrell 	return (0);
1166ff6d951SJohn Birrell }
1176ff6d951SJohn Birrell 
1186ff6d951SJohn Birrell static int
dt_aggregate_stddevcmp(int64_t * lhs,int64_t * rhs)1196ff6d951SJohn Birrell dt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
1206ff6d951SJohn Birrell {
1216ff6d951SJohn Birrell 	uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
1226ff6d951SJohn Birrell 	uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
1236ff6d951SJohn Birrell 
1246ff6d951SJohn Birrell 	if (lsd < rsd)
1256ff6d951SJohn Birrell 		return (DT_LESSTHAN);
1266ff6d951SJohn Birrell 
1276ff6d951SJohn Birrell 	if (lsd > rsd)
1286ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
1296ff6d951SJohn Birrell 
1306ff6d951SJohn Birrell 	return (0);
1316ff6d951SJohn Birrell }
1326ff6d951SJohn Birrell 
1336ff6d951SJohn Birrell /*ARGSUSED*/
1346ff6d951SJohn Birrell static void
dt_aggregate_lquantize(int64_t * existing,int64_t * new,size_t size)1356ff6d951SJohn Birrell dt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
1366ff6d951SJohn Birrell {
1376ff6d951SJohn Birrell 	int64_t arg = *existing++;
1386ff6d951SJohn Birrell 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
1396ff6d951SJohn Birrell 	int i;
1406ff6d951SJohn Birrell 
1416ff6d951SJohn Birrell 	for (i = 0; i <= levels + 1; i++)
1426ff6d951SJohn Birrell 		existing[i] = existing[i] + new[i + 1];
1436ff6d951SJohn Birrell }
1446ff6d951SJohn Birrell 
1456ff6d951SJohn Birrell static long double
dt_aggregate_lquantizedsum(int64_t * lquanta)1466ff6d951SJohn Birrell dt_aggregate_lquantizedsum(int64_t *lquanta)
1476ff6d951SJohn Birrell {
1486ff6d951SJohn Birrell 	int64_t arg = *lquanta++;
1496ff6d951SJohn Birrell 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
1506ff6d951SJohn Birrell 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
1516ff6d951SJohn Birrell 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
1526ff6d951SJohn Birrell 	long double total = (long double)lquanta[0] * (long double)(base - 1);
1536ff6d951SJohn Birrell 
1546ff6d951SJohn Birrell 	for (i = 0; i < levels; base += step, i++)
1556ff6d951SJohn Birrell 		total += (long double)lquanta[i + 1] * (long double)base;
1566ff6d951SJohn Birrell 
1576ff6d951SJohn Birrell 	return (total + (long double)lquanta[levels + 1] *
1586ff6d951SJohn Birrell 	    (long double)(base + 1));
1596ff6d951SJohn Birrell }
1606ff6d951SJohn Birrell 
1616ff6d951SJohn Birrell static int64_t
dt_aggregate_lquantizedzero(int64_t * lquanta)1626ff6d951SJohn Birrell dt_aggregate_lquantizedzero(int64_t *lquanta)
1636ff6d951SJohn Birrell {
1646ff6d951SJohn Birrell 	int64_t arg = *lquanta++;
1656ff6d951SJohn Birrell 	int32_t base = DTRACE_LQUANTIZE_BASE(arg);
1666ff6d951SJohn Birrell 	uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
1676ff6d951SJohn Birrell 	uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
1686ff6d951SJohn Birrell 
1696ff6d951SJohn Birrell 	if (base - 1 == 0)
1706ff6d951SJohn Birrell 		return (lquanta[0]);
1716ff6d951SJohn Birrell 
1726ff6d951SJohn Birrell 	for (i = 0; i < levels; base += step, i++) {
1736ff6d951SJohn Birrell 		if (base != 0)
1746ff6d951SJohn Birrell 			continue;
1756ff6d951SJohn Birrell 
1766ff6d951SJohn Birrell 		return (lquanta[i + 1]);
1776ff6d951SJohn Birrell 	}
1786ff6d951SJohn Birrell 
1796ff6d951SJohn Birrell 	if (base + 1 == 0)
1806ff6d951SJohn Birrell 		return (lquanta[levels + 1]);
1816ff6d951SJohn Birrell 
1826ff6d951SJohn Birrell 	return (0);
1836ff6d951SJohn Birrell }
1846ff6d951SJohn Birrell 
1856ff6d951SJohn Birrell static int
dt_aggregate_lquantizedcmp(int64_t * lhs,int64_t * rhs)1866ff6d951SJohn Birrell dt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
1876ff6d951SJohn Birrell {
1886ff6d951SJohn Birrell 	long double lsum = dt_aggregate_lquantizedsum(lhs);
1896ff6d951SJohn Birrell 	long double rsum = dt_aggregate_lquantizedsum(rhs);
1906ff6d951SJohn Birrell 	int64_t lzero, rzero;
1916ff6d951SJohn Birrell 
1926ff6d951SJohn Birrell 	if (lsum < rsum)
1936ff6d951SJohn Birrell 		return (DT_LESSTHAN);
1946ff6d951SJohn Birrell 
1956ff6d951SJohn Birrell 	if (lsum > rsum)
1966ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
1976ff6d951SJohn Birrell 
1986ff6d951SJohn Birrell 	/*
1996ff6d951SJohn Birrell 	 * If they're both equal, then we will compare based on the weights at
2006ff6d951SJohn Birrell 	 * zero.  If the weights at zero are equal (or if zero is not within
2016ff6d951SJohn Birrell 	 * the range of the linear quantization), then this will be judged a
2026ff6d951SJohn Birrell 	 * tie and will be resolved based on the key comparison.
2036ff6d951SJohn Birrell 	 */
2046ff6d951SJohn Birrell 	lzero = dt_aggregate_lquantizedzero(lhs);
2056ff6d951SJohn Birrell 	rzero = dt_aggregate_lquantizedzero(rhs);
2066ff6d951SJohn Birrell 
2076ff6d951SJohn Birrell 	if (lzero < rzero)
2086ff6d951SJohn Birrell 		return (DT_LESSTHAN);
2096ff6d951SJohn Birrell 
2106ff6d951SJohn Birrell 	if (lzero > rzero)
2116ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
2126ff6d951SJohn Birrell 
2136ff6d951SJohn Birrell 	return (0);
2146ff6d951SJohn Birrell }
2156ff6d951SJohn Birrell 
216675cf915SPedro F. Giffuni static void
dt_aggregate_llquantize(int64_t * existing,int64_t * new,size_t size)217675cf915SPedro F. Giffuni dt_aggregate_llquantize(int64_t *existing, int64_t *new, size_t size)
218675cf915SPedro F. Giffuni {
219675cf915SPedro F. Giffuni 	int i;
220675cf915SPedro F. Giffuni 
221675cf915SPedro F. Giffuni 	for (i = 1; i < size / sizeof (int64_t); i++)
222675cf915SPedro F. Giffuni 		existing[i] = existing[i] + new[i];
223675cf915SPedro F. Giffuni }
224675cf915SPedro F. Giffuni 
225675cf915SPedro F. Giffuni static long double
dt_aggregate_llquantizedsum(int64_t * llquanta)226675cf915SPedro F. Giffuni dt_aggregate_llquantizedsum(int64_t *llquanta)
227675cf915SPedro F. Giffuni {
228675cf915SPedro F. Giffuni 	int64_t arg = *llquanta++;
229675cf915SPedro F. Giffuni 	uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
230675cf915SPedro F. Giffuni 	uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
231675cf915SPedro F. Giffuni 	uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
232675cf915SPedro F. Giffuni 	uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
233675cf915SPedro F. Giffuni 	int bin = 0, order;
234675cf915SPedro F. Giffuni 	int64_t value = 1, next, step;
235675cf915SPedro F. Giffuni 	long double total;
236675cf915SPedro F. Giffuni 
237675cf915SPedro F. Giffuni 	assert(nsteps >= factor);
238675cf915SPedro F. Giffuni 	assert(nsteps % factor == 0);
239675cf915SPedro F. Giffuni 
240675cf915SPedro F. Giffuni 	for (order = 0; order < low; order++)
241675cf915SPedro F. Giffuni 		value *= factor;
242675cf915SPedro F. Giffuni 
243675cf915SPedro F. Giffuni 	total = (long double)llquanta[bin++] * (long double)(value - 1);
244675cf915SPedro F. Giffuni 
245675cf915SPedro F. Giffuni 	next = value * factor;
246675cf915SPedro F. Giffuni 	step = next > nsteps ? next / nsteps : 1;
247675cf915SPedro F. Giffuni 
248675cf915SPedro F. Giffuni 	while (order <= high) {
249675cf915SPedro F. Giffuni 		assert(value < next);
250675cf915SPedro F. Giffuni 		total += (long double)llquanta[bin++] * (long double)(value);
251675cf915SPedro F. Giffuni 
252675cf915SPedro F. Giffuni 		if ((value += step) != next)
253675cf915SPedro F. Giffuni 			continue;
254675cf915SPedro F. Giffuni 
255675cf915SPedro F. Giffuni 		next = value * factor;
256675cf915SPedro F. Giffuni 		step = next > nsteps ? next / nsteps : 1;
257675cf915SPedro F. Giffuni 		order++;
258675cf915SPedro F. Giffuni 	}
259675cf915SPedro F. Giffuni 
260675cf915SPedro F. Giffuni 	return (total + (long double)llquanta[bin] * (long double)value);
261675cf915SPedro F. Giffuni }
262675cf915SPedro F. Giffuni 
263675cf915SPedro F. Giffuni static int
dt_aggregate_llquantizedcmp(int64_t * lhs,int64_t * rhs)264675cf915SPedro F. Giffuni dt_aggregate_llquantizedcmp(int64_t *lhs, int64_t *rhs)
265675cf915SPedro F. Giffuni {
266675cf915SPedro F. Giffuni 	long double lsum = dt_aggregate_llquantizedsum(lhs);
267675cf915SPedro F. Giffuni 	long double rsum = dt_aggregate_llquantizedsum(rhs);
268675cf915SPedro F. Giffuni 	int64_t lzero, rzero;
269675cf915SPedro F. Giffuni 
270675cf915SPedro F. Giffuni 	if (lsum < rsum)
271675cf915SPedro F. Giffuni 		return (DT_LESSTHAN);
272675cf915SPedro F. Giffuni 
273675cf915SPedro F. Giffuni 	if (lsum > rsum)
274675cf915SPedro F. Giffuni 		return (DT_GREATERTHAN);
275675cf915SPedro F. Giffuni 
276675cf915SPedro F. Giffuni 	/*
277675cf915SPedro F. Giffuni 	 * If they're both equal, then we will compare based on the weights at
278675cf915SPedro F. Giffuni 	 * zero.  If the weights at zero are equal, then this will be judged a
279675cf915SPedro F. Giffuni 	 * tie and will be resolved based on the key comparison.
280675cf915SPedro F. Giffuni 	 */
281675cf915SPedro F. Giffuni 	lzero = lhs[1];
282675cf915SPedro F. Giffuni 	rzero = rhs[1];
283675cf915SPedro F. Giffuni 
284675cf915SPedro F. Giffuni 	if (lzero < rzero)
285675cf915SPedro F. Giffuni 		return (DT_LESSTHAN);
286675cf915SPedro F. Giffuni 
287675cf915SPedro F. Giffuni 	if (lzero > rzero)
288675cf915SPedro F. Giffuni 		return (DT_GREATERTHAN);
289675cf915SPedro F. Giffuni 
290675cf915SPedro F. Giffuni 	return (0);
291675cf915SPedro F. Giffuni }
292675cf915SPedro F. Giffuni 
2936ff6d951SJohn Birrell static int
dt_aggregate_quantizedcmp(int64_t * lhs,int64_t * rhs)2946ff6d951SJohn Birrell dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
2956ff6d951SJohn Birrell {
296d9ce0144SJohn Birrell 	int nbuckets = DTRACE_QUANTIZE_NBUCKETS;
2976ff6d951SJohn Birrell 	long double ltotal = 0, rtotal = 0;
2986ff6d951SJohn Birrell 	int64_t lzero, rzero;
299d9ce0144SJohn Birrell 	uint_t i;
3006ff6d951SJohn Birrell 
3016ff6d951SJohn Birrell 	for (i = 0; i < nbuckets; i++) {
3026ff6d951SJohn Birrell 		int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
3036ff6d951SJohn Birrell 
3046ff6d951SJohn Birrell 		if (bucketval == 0) {
3056ff6d951SJohn Birrell 			lzero = lhs[i];
3066ff6d951SJohn Birrell 			rzero = rhs[i];
3076ff6d951SJohn Birrell 		}
3086ff6d951SJohn Birrell 
3096ff6d951SJohn Birrell 		ltotal += (long double)bucketval * (long double)lhs[i];
3106ff6d951SJohn Birrell 		rtotal += (long double)bucketval * (long double)rhs[i];
3116ff6d951SJohn Birrell 	}
3126ff6d951SJohn Birrell 
3136ff6d951SJohn Birrell 	if (ltotal < rtotal)
3146ff6d951SJohn Birrell 		return (DT_LESSTHAN);
3156ff6d951SJohn Birrell 
3166ff6d951SJohn Birrell 	if (ltotal > rtotal)
3176ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
3186ff6d951SJohn Birrell 
3196ff6d951SJohn Birrell 	/*
3206ff6d951SJohn Birrell 	 * If they're both equal, then we will compare based on the weights at
3216ff6d951SJohn Birrell 	 * zero.  If the weights at zero are equal, then this will be judged a
3226ff6d951SJohn Birrell 	 * tie and will be resolved based on the key comparison.
3236ff6d951SJohn Birrell 	 */
3246ff6d951SJohn Birrell 	if (lzero < rzero)
3256ff6d951SJohn Birrell 		return (DT_LESSTHAN);
3266ff6d951SJohn Birrell 
3276ff6d951SJohn Birrell 	if (lzero > rzero)
3286ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
3296ff6d951SJohn Birrell 
3306ff6d951SJohn Birrell 	return (0);
3316ff6d951SJohn Birrell }
3326ff6d951SJohn Birrell 
3336ff6d951SJohn Birrell static void
dt_aggregate_usym(dtrace_hdl_t * dtp,uint64_t * data)3346ff6d951SJohn Birrell dt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
3356ff6d951SJohn Birrell {
3366ff6d951SJohn Birrell 	uint64_t pid = data[0];
3376ff6d951SJohn Birrell 	uint64_t *pc = &data[1];
3386ff6d951SJohn Birrell 	struct ps_prochandle *P;
3396ff6d951SJohn Birrell 	GElf_Sym sym;
3406ff6d951SJohn Birrell 
3416ff6d951SJohn Birrell 	if (dtp->dt_vector != NULL)
3426ff6d951SJohn Birrell 		return;
3436ff6d951SJohn Birrell 
3446ff6d951SJohn Birrell 	if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
3456ff6d951SJohn Birrell 		return;
3466ff6d951SJohn Birrell 
3476ff6d951SJohn Birrell 	dt_proc_lock(dtp, P);
3486ff6d951SJohn Birrell 
3496ff6d951SJohn Birrell 	if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
3506ff6d951SJohn Birrell 		*pc = sym.st_value;
3516ff6d951SJohn Birrell 
3526ff6d951SJohn Birrell 	dt_proc_unlock(dtp, P);
3536ff6d951SJohn Birrell 	dt_proc_release(dtp, P);
3546ff6d951SJohn Birrell }
3556ff6d951SJohn Birrell 
3566ff6d951SJohn Birrell static void
dt_aggregate_umod(dtrace_hdl_t * dtp,uint64_t * data)3576ff6d951SJohn Birrell dt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
3586ff6d951SJohn Birrell {
3596ff6d951SJohn Birrell 	uint64_t pid = data[0];
3606ff6d951SJohn Birrell 	uint64_t *pc = &data[1];
3616ff6d951SJohn Birrell 	struct ps_prochandle *P;
3626ff6d951SJohn Birrell 	const prmap_t *map;
3636ff6d951SJohn Birrell 
3646ff6d951SJohn Birrell 	if (dtp->dt_vector != NULL)
3656ff6d951SJohn Birrell 		return;
3666ff6d951SJohn Birrell 
3676ff6d951SJohn Birrell 	if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
3686ff6d951SJohn Birrell 		return;
3696ff6d951SJohn Birrell 
3706ff6d951SJohn Birrell 	dt_proc_lock(dtp, P);
3716ff6d951SJohn Birrell 
3726ff6d951SJohn Birrell 	if ((map = Paddr_to_map(P, *pc)) != NULL)
3736ff6d951SJohn Birrell 		*pc = map->pr_vaddr;
3746ff6d951SJohn Birrell 
3756ff6d951SJohn Birrell 	dt_proc_unlock(dtp, P);
3766ff6d951SJohn Birrell 	dt_proc_release(dtp, P);
3776ff6d951SJohn Birrell }
3786ff6d951SJohn Birrell 
3796ff6d951SJohn Birrell static void
dt_aggregate_sym(dtrace_hdl_t * dtp,uint64_t * data)3806ff6d951SJohn Birrell dt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
3816ff6d951SJohn Birrell {
3826ff6d951SJohn Birrell 	GElf_Sym sym;
3836ff6d951SJohn Birrell 	uint64_t *pc = data;
3846ff6d951SJohn Birrell 
3856ff6d951SJohn Birrell 	if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
3866ff6d951SJohn Birrell 		*pc = sym.st_value;
3876ff6d951SJohn Birrell }
3886ff6d951SJohn Birrell 
3896ff6d951SJohn Birrell static void
dt_aggregate_mod(dtrace_hdl_t * dtp,uint64_t * data)3906ff6d951SJohn Birrell dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
3916ff6d951SJohn Birrell {
3926ff6d951SJohn Birrell 	uint64_t *pc = data;
3936ff6d951SJohn Birrell 	dt_module_t *dmp;
3946ff6d951SJohn Birrell 
3956ff6d951SJohn Birrell 	if (dtp->dt_vector != NULL) {
3966ff6d951SJohn Birrell 		/*
3976ff6d951SJohn Birrell 		 * We don't have a way of just getting the module for a
3986ff6d951SJohn Birrell 		 * vectored open, and it doesn't seem to be worth defining
3996ff6d951SJohn Birrell 		 * one.  This means that use of mod() won't get true
4006ff6d951SJohn Birrell 		 * aggregation in the postmortem case (some modules may
4016ff6d951SJohn Birrell 		 * appear more than once in aggregation output).  It seems
4026ff6d951SJohn Birrell 		 * unlikely that anyone will ever notice or care...
4036ff6d951SJohn Birrell 		 */
4046ff6d951SJohn Birrell 		return;
4056ff6d951SJohn Birrell 	}
4066ff6d951SJohn Birrell 
4076ff6d951SJohn Birrell 	for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
4086ff6d951SJohn Birrell 	    dmp = dt_list_next(dmp)) {
4096ff6d951SJohn Birrell 		if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
4106ff6d951SJohn Birrell 			*pc = dmp->dm_text_va;
4116ff6d951SJohn Birrell 			return;
4126ff6d951SJohn Birrell 		}
4136ff6d951SJohn Birrell 	}
4146ff6d951SJohn Birrell }
4156ff6d951SJohn Birrell 
4166ff6d951SJohn Birrell static dtrace_aggvarid_t
dt_aggregate_aggvarid(dt_ahashent_t * ent)4176ff6d951SJohn Birrell dt_aggregate_aggvarid(dt_ahashent_t *ent)
4186ff6d951SJohn Birrell {
4196ff6d951SJohn Birrell 	dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
4206ff6d951SJohn Birrell 	caddr_t data = ent->dtahe_data.dtada_data;
4216ff6d951SJohn Birrell 	dtrace_recdesc_t *rec = agg->dtagd_rec;
4226ff6d951SJohn Birrell 
4236ff6d951SJohn Birrell 	/*
4246ff6d951SJohn Birrell 	 * First, we'll check the variable ID in the aggdesc.  If it's valid,
4256ff6d951SJohn Birrell 	 * we'll return it.  If not, we'll use the compiler-generated ID
4266ff6d951SJohn Birrell 	 * present as the first record.
4276ff6d951SJohn Birrell 	 */
4286ff6d951SJohn Birrell 	if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
4296ff6d951SJohn Birrell 		return (agg->dtagd_varid);
4306ff6d951SJohn Birrell 
4316ff6d951SJohn Birrell 	agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
4326ff6d951SJohn Birrell 	    rec->dtrd_offset));
4336ff6d951SJohn Birrell 
4346ff6d951SJohn Birrell 	return (agg->dtagd_varid);
4356ff6d951SJohn Birrell }
4366ff6d951SJohn Birrell 
4376ff6d951SJohn Birrell 
4386ff6d951SJohn Birrell static int
dt_aggregate_snap_cpu(dtrace_hdl_t * dtp,processorid_t cpu)4396ff6d951SJohn Birrell dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
4406ff6d951SJohn Birrell {
4416ff6d951SJohn Birrell 	dtrace_epid_t id;
4426ff6d951SJohn Birrell 	uint64_t hashval;
4436ff6d951SJohn Birrell 	size_t offs, roffs, size, ndx;
4446ff6d951SJohn Birrell 	int i, j, rval;
4456ff6d951SJohn Birrell 	caddr_t addr, data;
4466ff6d951SJohn Birrell 	dtrace_recdesc_t *rec;
4476ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
4486ff6d951SJohn Birrell 	dtrace_aggdesc_t *agg;
4496ff6d951SJohn Birrell 	dt_ahash_t *hash = &agp->dtat_hash;
4506ff6d951SJohn Birrell 	dt_ahashent_t *h;
4516ff6d951SJohn Birrell 	dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
4526ff6d951SJohn Birrell 	dtrace_aggdata_t *aggdata;
4536ff6d951SJohn Birrell 	int flags = agp->dtat_flags;
4546ff6d951SJohn Birrell 
4556ff6d951SJohn Birrell 	buf->dtbd_cpu = cpu;
4566ff6d951SJohn Birrell 
457bc96366cSSteven Hartland #ifdef illumos
4586ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
459d9ce0144SJohn Birrell #else
460d9ce0144SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, &buf) == -1) {
461d9ce0144SJohn Birrell #endif
4626ff6d951SJohn Birrell 		if (errno == ENOENT) {
4636ff6d951SJohn Birrell 			/*
4646ff6d951SJohn Birrell 			 * If that failed with ENOENT, it may be because the
4656ff6d951SJohn Birrell 			 * CPU was unconfigured.  This is okay; we'll just
4666ff6d951SJohn Birrell 			 * do nothing but return success.
4676ff6d951SJohn Birrell 			 */
4686ff6d951SJohn Birrell 			return (0);
4696ff6d951SJohn Birrell 		}
4706ff6d951SJohn Birrell 
4716ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
4726ff6d951SJohn Birrell 	}
4736ff6d951SJohn Birrell 
4746ff6d951SJohn Birrell 	if (buf->dtbd_drops != 0) {
47593f27766SDomagoj Stolfa 		xo_open_instance("probes");
47693f27766SDomagoj Stolfa 		dt_oformat_drop(dtp, cpu);
4776ff6d951SJohn Birrell 		if (dt_handle_cpudrop(dtp, cpu,
47893f27766SDomagoj Stolfa 		    DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1) {
47993f27766SDomagoj Stolfa 			xo_close_instance("probes");
4806ff6d951SJohn Birrell 			return (-1);
4816ff6d951SJohn Birrell 		}
48293f27766SDomagoj Stolfa 		xo_close_instance("probes");
48393f27766SDomagoj Stolfa 	}
4846ff6d951SJohn Birrell 
4856ff6d951SJohn Birrell 	if (buf->dtbd_size == 0)
4866ff6d951SJohn Birrell 		return (0);
4876ff6d951SJohn Birrell 
4886ff6d951SJohn Birrell 	if (hash->dtah_hash == NULL) {
4896ff6d951SJohn Birrell 		size_t size;
4906ff6d951SJohn Birrell 
4916ff6d951SJohn Birrell 		hash->dtah_size = DTRACE_AHASHSIZE;
4926ff6d951SJohn Birrell 		size = hash->dtah_size * sizeof (dt_ahashent_t *);
4936ff6d951SJohn Birrell 
4946ff6d951SJohn Birrell 		if ((hash->dtah_hash = malloc(size)) == NULL)
4956ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_NOMEM));
4966ff6d951SJohn Birrell 
4976ff6d951SJohn Birrell 		bzero(hash->dtah_hash, size);
4986ff6d951SJohn Birrell 	}
4996ff6d951SJohn Birrell 
5006ff6d951SJohn Birrell 	for (offs = 0; offs < buf->dtbd_size; ) {
5016ff6d951SJohn Birrell 		/*
5026ff6d951SJohn Birrell 		 * We're guaranteed to have an ID.
5036ff6d951SJohn Birrell 		 */
5046ff6d951SJohn Birrell 		id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
5056ff6d951SJohn Birrell 		    (uintptr_t)offs));
5066ff6d951SJohn Birrell 
5076ff6d951SJohn Birrell 		if (id == DTRACE_AGGIDNONE) {
5086ff6d951SJohn Birrell 			/*
5096ff6d951SJohn Birrell 			 * This is filler to assure proper alignment of the
5106ff6d951SJohn Birrell 			 * next record; we simply ignore it.
5116ff6d951SJohn Birrell 			 */
5126ff6d951SJohn Birrell 			offs += sizeof (id);
5136ff6d951SJohn Birrell 			continue;
5146ff6d951SJohn Birrell 		}
5156ff6d951SJohn Birrell 
5166ff6d951SJohn Birrell 		if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
5176ff6d951SJohn Birrell 			return (rval);
5186ff6d951SJohn Birrell 
5196ff6d951SJohn Birrell 		addr = buf->dtbd_data + offs;
5206ff6d951SJohn Birrell 		size = agg->dtagd_size;
5216ff6d951SJohn Birrell 		hashval = 0;
5226ff6d951SJohn Birrell 
5236ff6d951SJohn Birrell 		for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
5246ff6d951SJohn Birrell 			rec = &agg->dtagd_rec[j];
5256ff6d951SJohn Birrell 			roffs = rec->dtrd_offset;
5266ff6d951SJohn Birrell 
5276ff6d951SJohn Birrell 			switch (rec->dtrd_action) {
5286ff6d951SJohn Birrell 			case DTRACEACT_USYM:
5296ff6d951SJohn Birrell 				dt_aggregate_usym(dtp,
5306ff6d951SJohn Birrell 				    /* LINTED - alignment */
5316ff6d951SJohn Birrell 				    (uint64_t *)&addr[roffs]);
5326ff6d951SJohn Birrell 				break;
5336ff6d951SJohn Birrell 
5346ff6d951SJohn Birrell 			case DTRACEACT_UMOD:
5356ff6d951SJohn Birrell 				dt_aggregate_umod(dtp,
5366ff6d951SJohn Birrell 				    /* LINTED - alignment */
5376ff6d951SJohn Birrell 				    (uint64_t *)&addr[roffs]);
5386ff6d951SJohn Birrell 				break;
5396ff6d951SJohn Birrell 
5406ff6d951SJohn Birrell 			case DTRACEACT_SYM:
5416ff6d951SJohn Birrell 				/* LINTED - alignment */
5426ff6d951SJohn Birrell 				dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
5436ff6d951SJohn Birrell 				break;
5446ff6d951SJohn Birrell 
5456ff6d951SJohn Birrell 			case DTRACEACT_MOD:
5466ff6d951SJohn Birrell 				/* LINTED - alignment */
5476ff6d951SJohn Birrell 				dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
5486ff6d951SJohn Birrell 				break;
5496ff6d951SJohn Birrell 
5506ff6d951SJohn Birrell 			default:
5516ff6d951SJohn Birrell 				break;
5526ff6d951SJohn Birrell 			}
5536ff6d951SJohn Birrell 
5546ff6d951SJohn Birrell 			for (i = 0; i < rec->dtrd_size; i++)
5556ff6d951SJohn Birrell 				hashval += addr[roffs + i];
5566ff6d951SJohn Birrell 		}
5576ff6d951SJohn Birrell 
5586ff6d951SJohn Birrell 		ndx = hashval % hash->dtah_size;
5596ff6d951SJohn Birrell 
5606ff6d951SJohn Birrell 		for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
5616ff6d951SJohn Birrell 			if (h->dtahe_hashval != hashval)
5626ff6d951SJohn Birrell 				continue;
5636ff6d951SJohn Birrell 
5646ff6d951SJohn Birrell 			if (h->dtahe_size != size)
5656ff6d951SJohn Birrell 				continue;
5666ff6d951SJohn Birrell 
5676ff6d951SJohn Birrell 			aggdata = &h->dtahe_data;
5686ff6d951SJohn Birrell 			data = aggdata->dtada_data;
5696ff6d951SJohn Birrell 
5706ff6d951SJohn Birrell 			for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
5716ff6d951SJohn Birrell 				rec = &agg->dtagd_rec[j];
5726ff6d951SJohn Birrell 				roffs = rec->dtrd_offset;
5736ff6d951SJohn Birrell 
5746ff6d951SJohn Birrell 				for (i = 0; i < rec->dtrd_size; i++)
5756ff6d951SJohn Birrell 					if (addr[roffs + i] != data[roffs + i])
5766ff6d951SJohn Birrell 						goto hashnext;
5776ff6d951SJohn Birrell 			}
5786ff6d951SJohn Birrell 
5796ff6d951SJohn Birrell 			/*
5806ff6d951SJohn Birrell 			 * We found it.  Now we need to apply the aggregating
5816ff6d951SJohn Birrell 			 * action on the data here.
5826ff6d951SJohn Birrell 			 */
5836ff6d951SJohn Birrell 			rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
5846ff6d951SJohn Birrell 			roffs = rec->dtrd_offset;
5856ff6d951SJohn Birrell 			/* LINTED - alignment */
5866ff6d951SJohn Birrell 			h->dtahe_aggregate((int64_t *)&data[roffs],
5876ff6d951SJohn Birrell 			    /* LINTED - alignment */
5886ff6d951SJohn Birrell 			    (int64_t *)&addr[roffs], rec->dtrd_size);
5896ff6d951SJohn Birrell 
5906ff6d951SJohn Birrell 			/*
5916ff6d951SJohn Birrell 			 * If we're keeping per CPU data, apply the aggregating
5926ff6d951SJohn Birrell 			 * action there as well.
5936ff6d951SJohn Birrell 			 */
5946ff6d951SJohn Birrell 			if (aggdata->dtada_percpu != NULL) {
5956ff6d951SJohn Birrell 				data = aggdata->dtada_percpu[cpu];
5966ff6d951SJohn Birrell 
5976ff6d951SJohn Birrell 				/* LINTED - alignment */
5986ff6d951SJohn Birrell 				h->dtahe_aggregate((int64_t *)data,
5996ff6d951SJohn Birrell 				    /* LINTED - alignment */
6006ff6d951SJohn Birrell 				    (int64_t *)&addr[roffs], rec->dtrd_size);
6016ff6d951SJohn Birrell 			}
6026ff6d951SJohn Birrell 
6036ff6d951SJohn Birrell 			goto bufnext;
6046ff6d951SJohn Birrell hashnext:
6056ff6d951SJohn Birrell 			continue;
6066ff6d951SJohn Birrell 		}
6076ff6d951SJohn Birrell 
6086ff6d951SJohn Birrell 		/*
6096ff6d951SJohn Birrell 		 * If we're here, we couldn't find an entry for this record.
6106ff6d951SJohn Birrell 		 */
6116ff6d951SJohn Birrell 		if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
6126ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_NOMEM));
6136ff6d951SJohn Birrell 		bzero(h, sizeof (dt_ahashent_t));
6146ff6d951SJohn Birrell 		aggdata = &h->dtahe_data;
6156ff6d951SJohn Birrell 
6166ff6d951SJohn Birrell 		if ((aggdata->dtada_data = malloc(size)) == NULL) {
6176ff6d951SJohn Birrell 			free(h);
6186ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_NOMEM));
6196ff6d951SJohn Birrell 		}
6206ff6d951SJohn Birrell 
6216ff6d951SJohn Birrell 		bcopy(addr, aggdata->dtada_data, size);
6226ff6d951SJohn Birrell 		aggdata->dtada_size = size;
6236ff6d951SJohn Birrell 		aggdata->dtada_desc = agg;
6246ff6d951SJohn Birrell 		aggdata->dtada_handle = dtp;
6256ff6d951SJohn Birrell 		(void) dt_epid_lookup(dtp, agg->dtagd_epid,
6266ff6d951SJohn Birrell 		    &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
6276ff6d951SJohn Birrell 		aggdata->dtada_normal = 1;
6286ff6d951SJohn Birrell 
6296ff6d951SJohn Birrell 		h->dtahe_hashval = hashval;
6306ff6d951SJohn Birrell 		h->dtahe_size = size;
6316ff6d951SJohn Birrell 		(void) dt_aggregate_aggvarid(h);
6326ff6d951SJohn Birrell 
6336ff6d951SJohn Birrell 		rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
6346ff6d951SJohn Birrell 
6356ff6d951SJohn Birrell 		if (flags & DTRACE_A_PERCPU) {
6366ff6d951SJohn Birrell 			int max_cpus = agp->dtat_maxcpu;
6376ff6d951SJohn Birrell 			caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
6386ff6d951SJohn Birrell 
6396ff6d951SJohn Birrell 			if (percpu == NULL) {
6406ff6d951SJohn Birrell 				free(aggdata->dtada_data);
6416ff6d951SJohn Birrell 				free(h);
6426ff6d951SJohn Birrell 				return (dt_set_errno(dtp, EDT_NOMEM));
6436ff6d951SJohn Birrell 			}
6446ff6d951SJohn Birrell 
6456ff6d951SJohn Birrell 			for (j = 0; j < max_cpus; j++) {
6466ff6d951SJohn Birrell 				percpu[j] = malloc(rec->dtrd_size);
6476ff6d951SJohn Birrell 
6486ff6d951SJohn Birrell 				if (percpu[j] == NULL) {
6496ff6d951SJohn Birrell 					while (--j >= 0)
6506ff6d951SJohn Birrell 						free(percpu[j]);
6516ff6d951SJohn Birrell 
6526ff6d951SJohn Birrell 					free(aggdata->dtada_data);
6536ff6d951SJohn Birrell 					free(h);
6546ff6d951SJohn Birrell 					return (dt_set_errno(dtp, EDT_NOMEM));
6556ff6d951SJohn Birrell 				}
6566ff6d951SJohn Birrell 
6576ff6d951SJohn Birrell 				if (j == cpu) {
6586ff6d951SJohn Birrell 					bcopy(&addr[rec->dtrd_offset],
6596ff6d951SJohn Birrell 					    percpu[j], rec->dtrd_size);
6606ff6d951SJohn Birrell 				} else {
6616ff6d951SJohn Birrell 					bzero(percpu[j], rec->dtrd_size);
6626ff6d951SJohn Birrell 				}
6636ff6d951SJohn Birrell 			}
6646ff6d951SJohn Birrell 
6656ff6d951SJohn Birrell 			aggdata->dtada_percpu = percpu;
6666ff6d951SJohn Birrell 		}
6676ff6d951SJohn Birrell 
6686ff6d951SJohn Birrell 		switch (rec->dtrd_action) {
6696ff6d951SJohn Birrell 		case DTRACEAGG_MIN:
6706ff6d951SJohn Birrell 			h->dtahe_aggregate = dt_aggregate_min;
6716ff6d951SJohn Birrell 			break;
6726ff6d951SJohn Birrell 
6736ff6d951SJohn Birrell 		case DTRACEAGG_MAX:
6746ff6d951SJohn Birrell 			h->dtahe_aggregate = dt_aggregate_max;
6756ff6d951SJohn Birrell 			break;
6766ff6d951SJohn Birrell 
6776ff6d951SJohn Birrell 		case DTRACEAGG_LQUANTIZE:
6786ff6d951SJohn Birrell 			h->dtahe_aggregate = dt_aggregate_lquantize;
6796ff6d951SJohn Birrell 			break;
6806ff6d951SJohn Birrell 
681675cf915SPedro F. Giffuni 		case DTRACEAGG_LLQUANTIZE:
682675cf915SPedro F. Giffuni 			h->dtahe_aggregate = dt_aggregate_llquantize;
683675cf915SPedro F. Giffuni 			break;
684675cf915SPedro F. Giffuni 
6856ff6d951SJohn Birrell 		case DTRACEAGG_COUNT:
6866ff6d951SJohn Birrell 		case DTRACEAGG_SUM:
6876ff6d951SJohn Birrell 		case DTRACEAGG_AVG:
6886ff6d951SJohn Birrell 		case DTRACEAGG_STDDEV:
6896ff6d951SJohn Birrell 		case DTRACEAGG_QUANTIZE:
6906ff6d951SJohn Birrell 			h->dtahe_aggregate = dt_aggregate_count;
6916ff6d951SJohn Birrell 			break;
6926ff6d951SJohn Birrell 
6936ff6d951SJohn Birrell 		default:
6946ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_BADAGG));
6956ff6d951SJohn Birrell 		}
6966ff6d951SJohn Birrell 
6976ff6d951SJohn Birrell 		if (hash->dtah_hash[ndx] != NULL)
6986ff6d951SJohn Birrell 			hash->dtah_hash[ndx]->dtahe_prev = h;
6996ff6d951SJohn Birrell 
7006ff6d951SJohn Birrell 		h->dtahe_next = hash->dtah_hash[ndx];
7016ff6d951SJohn Birrell 		hash->dtah_hash[ndx] = h;
7026ff6d951SJohn Birrell 
7036ff6d951SJohn Birrell 		if (hash->dtah_all != NULL)
7046ff6d951SJohn Birrell 			hash->dtah_all->dtahe_prevall = h;
7056ff6d951SJohn Birrell 
7066ff6d951SJohn Birrell 		h->dtahe_nextall = hash->dtah_all;
7076ff6d951SJohn Birrell 		hash->dtah_all = h;
7086ff6d951SJohn Birrell bufnext:
7096ff6d951SJohn Birrell 		offs += agg->dtagd_size;
7106ff6d951SJohn Birrell 	}
7116ff6d951SJohn Birrell 
7126ff6d951SJohn Birrell 	return (0);
7136ff6d951SJohn Birrell }
7146ff6d951SJohn Birrell 
7156ff6d951SJohn Birrell int
7166ff6d951SJohn Birrell dtrace_aggregate_snap(dtrace_hdl_t *dtp)
7176ff6d951SJohn Birrell {
7186ff6d951SJohn Birrell 	int i, rval;
7196ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
7206ff6d951SJohn Birrell 	hrtime_t now = gethrtime();
7216ff6d951SJohn Birrell 	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
7226ff6d951SJohn Birrell 
7236ff6d951SJohn Birrell 	if (dtp->dt_lastagg != 0) {
7246ff6d951SJohn Birrell 		if (now - dtp->dt_lastagg < interval)
7256ff6d951SJohn Birrell 			return (0);
7266ff6d951SJohn Birrell 
7276ff6d951SJohn Birrell 		dtp->dt_lastagg += interval;
7286ff6d951SJohn Birrell 	} else {
7296ff6d951SJohn Birrell 		dtp->dt_lastagg = now;
7306ff6d951SJohn Birrell 	}
7316ff6d951SJohn Birrell 
7326ff6d951SJohn Birrell 	if (!dtp->dt_active)
7336ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EINVAL));
7346ff6d951SJohn Birrell 
7356ff6d951SJohn Birrell 	if (agp->dtat_buf.dtbd_size == 0)
7366ff6d951SJohn Birrell 		return (0);
7376ff6d951SJohn Birrell 
7386ff6d951SJohn Birrell 	for (i = 0; i < agp->dtat_ncpus; i++) {
739d9ce0144SJohn Birrell 		if ((rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i])))
7406ff6d951SJohn Birrell 			return (rval);
7416ff6d951SJohn Birrell 	}
7426ff6d951SJohn Birrell 
7436ff6d951SJohn Birrell 	return (0);
7446ff6d951SJohn Birrell }
7456ff6d951SJohn Birrell 
7466ff6d951SJohn Birrell static int
7476ff6d951SJohn Birrell dt_aggregate_hashcmp(const void *lhs, const void *rhs)
7486ff6d951SJohn Birrell {
7496ff6d951SJohn Birrell 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
7506ff6d951SJohn Birrell 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
7516ff6d951SJohn Birrell 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
7526ff6d951SJohn Birrell 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
7536ff6d951SJohn Birrell 
7546ff6d951SJohn Birrell 	if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
7556ff6d951SJohn Birrell 		return (DT_LESSTHAN);
7566ff6d951SJohn Birrell 
7576ff6d951SJohn Birrell 	if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
7586ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
7596ff6d951SJohn Birrell 
7606ff6d951SJohn Birrell 	return (0);
7616ff6d951SJohn Birrell }
7626ff6d951SJohn Birrell 
7636ff6d951SJohn Birrell static int
7646ff6d951SJohn Birrell dt_aggregate_varcmp(const void *lhs, const void *rhs)
7656ff6d951SJohn Birrell {
7666ff6d951SJohn Birrell 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
7676ff6d951SJohn Birrell 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
7686ff6d951SJohn Birrell 	dtrace_aggvarid_t lid, rid;
7696ff6d951SJohn Birrell 
7706ff6d951SJohn Birrell 	lid = dt_aggregate_aggvarid(lh);
7716ff6d951SJohn Birrell 	rid = dt_aggregate_aggvarid(rh);
7726ff6d951SJohn Birrell 
7736ff6d951SJohn Birrell 	if (lid < rid)
7746ff6d951SJohn Birrell 		return (DT_LESSTHAN);
7756ff6d951SJohn Birrell 
7766ff6d951SJohn Birrell 	if (lid > rid)
7776ff6d951SJohn Birrell 		return (DT_GREATERTHAN);
7786ff6d951SJohn Birrell 
7796ff6d951SJohn Birrell 	return (0);
7806ff6d951SJohn Birrell }
7816ff6d951SJohn Birrell 
7826ff6d951SJohn Birrell static int
7836ff6d951SJohn Birrell dt_aggregate_keycmp(const void *lhs, const void *rhs)
7846ff6d951SJohn Birrell {
7856ff6d951SJohn Birrell 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
7866ff6d951SJohn Birrell 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
7876ff6d951SJohn Birrell 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
7886ff6d951SJohn Birrell 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
7896ff6d951SJohn Birrell 	dtrace_recdesc_t *lrec, *rrec;
7906ff6d951SJohn Birrell 	char *ldata, *rdata;
7916ff6d951SJohn Birrell 	int rval, i, j, keypos, nrecs;
7926ff6d951SJohn Birrell 
7936ff6d951SJohn Birrell 	if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
7946ff6d951SJohn Birrell 		return (rval);
7956ff6d951SJohn Birrell 
7966ff6d951SJohn Birrell 	nrecs = lagg->dtagd_nrecs - 1;
7976ff6d951SJohn Birrell 	assert(nrecs == ragg->dtagd_nrecs - 1);
7986ff6d951SJohn Birrell 
7996ff6d951SJohn Birrell 	keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
8006ff6d951SJohn Birrell 
8016ff6d951SJohn Birrell 	for (i = 1; i < nrecs; i++) {
8026ff6d951SJohn Birrell 		uint64_t lval, rval;
8036ff6d951SJohn Birrell 		int ndx = i + keypos;
8046ff6d951SJohn Birrell 
8056ff6d951SJohn Birrell 		if (ndx >= nrecs)
8066ff6d951SJohn Birrell 			ndx = ndx - nrecs + 1;
8076ff6d951SJohn Birrell 
8086ff6d951SJohn Birrell 		lrec = &lagg->dtagd_rec[ndx];
8096ff6d951SJohn Birrell 		rrec = &ragg->dtagd_rec[ndx];
8106ff6d951SJohn Birrell 
8116ff6d951SJohn Birrell 		ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
8126ff6d951SJohn Birrell 		rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
8136ff6d951SJohn Birrell 
8146ff6d951SJohn Birrell 		if (lrec->dtrd_size < rrec->dtrd_size)
8156ff6d951SJohn Birrell 			return (DT_LESSTHAN);
8166ff6d951SJohn Birrell 
8176ff6d951SJohn Birrell 		if (lrec->dtrd_size > rrec->dtrd_size)
8186ff6d951SJohn Birrell 			return (DT_GREATERTHAN);
8196ff6d951SJohn Birrell 
8206ff6d951SJohn Birrell 		switch (lrec->dtrd_size) {
8216ff6d951SJohn Birrell 		case sizeof (uint64_t):
8226ff6d951SJohn Birrell 			/* LINTED - alignment */
8236ff6d951SJohn Birrell 			lval = *((uint64_t *)ldata);
8246ff6d951SJohn Birrell 			/* LINTED - alignment */
8256ff6d951SJohn Birrell 			rval = *((uint64_t *)rdata);
8266ff6d951SJohn Birrell 			break;
8276ff6d951SJohn Birrell 
8286ff6d951SJohn Birrell 		case sizeof (uint32_t):
8296ff6d951SJohn Birrell 			/* LINTED - alignment */
8306ff6d951SJohn Birrell 			lval = *((uint32_t *)ldata);
8316ff6d951SJohn Birrell 			/* LINTED - alignment */
8326ff6d951SJohn Birrell 			rval = *((uint32_t *)rdata);
8336ff6d951SJohn Birrell 			break;
8346ff6d951SJohn Birrell 
8356ff6d951SJohn Birrell 		case sizeof (uint16_t):
8366ff6d951SJohn Birrell 			/* LINTED - alignment */
8376ff6d951SJohn Birrell 			lval = *((uint16_t *)ldata);
8386ff6d951SJohn Birrell 			/* LINTED - alignment */
8396ff6d951SJohn Birrell 			rval = *((uint16_t *)rdata);
8406ff6d951SJohn Birrell 			break;
8416ff6d951SJohn Birrell 
8426ff6d951SJohn Birrell 		case sizeof (uint8_t):
8436ff6d951SJohn Birrell 			lval = *((uint8_t *)ldata);
8446ff6d951SJohn Birrell 			rval = *((uint8_t *)rdata);
8456ff6d951SJohn Birrell 			break;
8466ff6d951SJohn Birrell 
8476ff6d951SJohn Birrell 		default:
8486ff6d951SJohn Birrell 			switch (lrec->dtrd_action) {
8496ff6d951SJohn Birrell 			case DTRACEACT_UMOD:
8506ff6d951SJohn Birrell 			case DTRACEACT_UADDR:
8516ff6d951SJohn Birrell 			case DTRACEACT_USYM:
8526ff6d951SJohn Birrell 				for (j = 0; j < 2; j++) {
8536ff6d951SJohn Birrell 					/* LINTED - alignment */
8546ff6d951SJohn Birrell 					lval = ((uint64_t *)ldata)[j];
8556ff6d951SJohn Birrell 					/* LINTED - alignment */
8566ff6d951SJohn Birrell 					rval = ((uint64_t *)rdata)[j];
8576ff6d951SJohn Birrell 
8586ff6d951SJohn Birrell 					if (lval < rval)
8596ff6d951SJohn Birrell 						return (DT_LESSTHAN);
8606ff6d951SJohn Birrell 
8616ff6d951SJohn Birrell 					if (lval > rval)
8626ff6d951SJohn Birrell 						return (DT_GREATERTHAN);
8636ff6d951SJohn Birrell 				}
8646ff6d951SJohn Birrell 
8656ff6d951SJohn Birrell 				break;
8666ff6d951SJohn Birrell 
8676ff6d951SJohn Birrell 			default:
8686ff6d951SJohn Birrell 				for (j = 0; j < lrec->dtrd_size; j++) {
8696ff6d951SJohn Birrell 					lval = ((uint8_t *)ldata)[j];
8706ff6d951SJohn Birrell 					rval = ((uint8_t *)rdata)[j];
8716ff6d951SJohn Birrell 
8726ff6d951SJohn Birrell 					if (lval < rval)
8736ff6d951SJohn Birrell 						return (DT_LESSTHAN);
8746ff6d951SJohn Birrell 
8756ff6d951SJohn Birrell 					if (lval > rval)
8766ff6d951SJohn Birrell 						return (DT_GREATERTHAN);
8776ff6d951SJohn Birrell 				}
8786ff6d951SJohn Birrell 			}
8796ff6d951SJohn Birrell 
8806ff6d951SJohn Birrell 			continue;
8816ff6d951SJohn Birrell 		}
8826ff6d951SJohn Birrell 
8836ff6d951SJohn Birrell 		if (lval < rval)
8846ff6d951SJohn Birrell 			return (DT_LESSTHAN);
8856ff6d951SJohn Birrell 
8866ff6d951SJohn Birrell 		if (lval > rval)
8876ff6d951SJohn Birrell 			return (DT_GREATERTHAN);
8886ff6d951SJohn Birrell 	}
8896ff6d951SJohn Birrell 
8906ff6d951SJohn Birrell 	return (0);
8916ff6d951SJohn Birrell }
8926ff6d951SJohn Birrell 
8936ff6d951SJohn Birrell static int
8946ff6d951SJohn Birrell dt_aggregate_valcmp(const void *lhs, const void *rhs)
8956ff6d951SJohn Birrell {
8966ff6d951SJohn Birrell 	dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
8976ff6d951SJohn Birrell 	dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
8986ff6d951SJohn Birrell 	dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
8996ff6d951SJohn Birrell 	dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
9006ff6d951SJohn Birrell 	caddr_t ldata = lh->dtahe_data.dtada_data;
9016ff6d951SJohn Birrell 	caddr_t rdata = rh->dtahe_data.dtada_data;
9026ff6d951SJohn Birrell 	dtrace_recdesc_t *lrec, *rrec;
9036ff6d951SJohn Birrell 	int64_t *laddr, *raddr;
904a98ff317SPedro F. Giffuni 	int rval;
9056ff6d951SJohn Birrell 
906a98ff317SPedro F. Giffuni 	assert(lagg->dtagd_nrecs == ragg->dtagd_nrecs);
9076ff6d951SJohn Birrell 
908a98ff317SPedro F. Giffuni 	lrec = &lagg->dtagd_rec[lagg->dtagd_nrecs - 1];
909a98ff317SPedro F. Giffuni 	rrec = &ragg->dtagd_rec[ragg->dtagd_nrecs - 1];
9106ff6d951SJohn Birrell 
911a98ff317SPedro F. Giffuni 	assert(lrec->dtrd_action == rrec->dtrd_action);
9126ff6d951SJohn Birrell 
9136ff6d951SJohn Birrell 	laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
9146ff6d951SJohn Birrell 	raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
9156ff6d951SJohn Birrell 
9166ff6d951SJohn Birrell 	switch (lrec->dtrd_action) {
9176ff6d951SJohn Birrell 	case DTRACEAGG_AVG:
9186ff6d951SJohn Birrell 		rval = dt_aggregate_averagecmp(laddr, raddr);
9196ff6d951SJohn Birrell 		break;
9206ff6d951SJohn Birrell 
9216ff6d951SJohn Birrell 	case DTRACEAGG_STDDEV:
9226ff6d951SJohn Birrell 		rval = dt_aggregate_stddevcmp(laddr, raddr);
9236ff6d951SJohn Birrell 		break;
9246ff6d951SJohn Birrell 
9256ff6d951SJohn Birrell 	case DTRACEAGG_QUANTIZE:
9266ff6d951SJohn Birrell 		rval = dt_aggregate_quantizedcmp(laddr, raddr);
9276ff6d951SJohn Birrell 		break;
9286ff6d951SJohn Birrell 
9296ff6d951SJohn Birrell 	case DTRACEAGG_LQUANTIZE:
9306ff6d951SJohn Birrell 		rval = dt_aggregate_lquantizedcmp(laddr, raddr);
9316ff6d951SJohn Birrell 		break;
9326ff6d951SJohn Birrell 
933675cf915SPedro F. Giffuni 	case DTRACEAGG_LLQUANTIZE:
934675cf915SPedro F. Giffuni 		rval = dt_aggregate_llquantizedcmp(laddr, raddr);
935675cf915SPedro F. Giffuni 		break;
936675cf915SPedro F. Giffuni 
9376ff6d951SJohn Birrell 	case DTRACEAGG_COUNT:
9386ff6d951SJohn Birrell 	case DTRACEAGG_SUM:
9396ff6d951SJohn Birrell 	case DTRACEAGG_MIN:
9406ff6d951SJohn Birrell 	case DTRACEAGG_MAX:
9416ff6d951SJohn Birrell 		rval = dt_aggregate_countcmp(laddr, raddr);
9426ff6d951SJohn Birrell 		break;
9436ff6d951SJohn Birrell 
9446ff6d951SJohn Birrell 	default:
9456ff6d951SJohn Birrell 		assert(0);
9466ff6d951SJohn Birrell 	}
9476ff6d951SJohn Birrell 
9486ff6d951SJohn Birrell 	return (rval);
9496ff6d951SJohn Birrell }
9506ff6d951SJohn Birrell 
9516ff6d951SJohn Birrell static int
9526ff6d951SJohn Birrell dt_aggregate_valkeycmp(const void *lhs, const void *rhs)
9536ff6d951SJohn Birrell {
9546ff6d951SJohn Birrell 	int rval;
9556ff6d951SJohn Birrell 
9566ff6d951SJohn Birrell 	if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
9576ff6d951SJohn Birrell 		return (rval);
9586ff6d951SJohn Birrell 
9596ff6d951SJohn Birrell 	/*
9606ff6d951SJohn Birrell 	 * If we're here, the values for the two aggregation elements are
9616ff6d951SJohn Birrell 	 * equal.  We already know that the key layout is the same for the two
9626ff6d951SJohn Birrell 	 * elements; we must now compare the keys themselves as a tie-breaker.
9636ff6d951SJohn Birrell 	 */
9646ff6d951SJohn Birrell 	return (dt_aggregate_keycmp(lhs, rhs));
9656ff6d951SJohn Birrell }
9666ff6d951SJohn Birrell 
9676ff6d951SJohn Birrell static int
9686ff6d951SJohn Birrell dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
9696ff6d951SJohn Birrell {
9706ff6d951SJohn Birrell 	int rval;
9716ff6d951SJohn Birrell 
9726ff6d951SJohn Birrell 	if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
9736ff6d951SJohn Birrell 		return (rval);
9746ff6d951SJohn Birrell 
9756ff6d951SJohn Birrell 	return (dt_aggregate_varcmp(lhs, rhs));
9766ff6d951SJohn Birrell }
9776ff6d951SJohn Birrell 
9786ff6d951SJohn Birrell static int
9796ff6d951SJohn Birrell dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
9806ff6d951SJohn Birrell {
9816ff6d951SJohn Birrell 	int rval;
9826ff6d951SJohn Birrell 
9836ff6d951SJohn Birrell 	if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
9846ff6d951SJohn Birrell 		return (rval);
9856ff6d951SJohn Birrell 
9866ff6d951SJohn Birrell 	return (dt_aggregate_keycmp(lhs, rhs));
9876ff6d951SJohn Birrell }
9886ff6d951SJohn Birrell 
9896ff6d951SJohn Birrell static int
9906ff6d951SJohn Birrell dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
9916ff6d951SJohn Birrell {
9926ff6d951SJohn Birrell 	int rval;
9936ff6d951SJohn Birrell 
9946ff6d951SJohn Birrell 	if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
9956ff6d951SJohn Birrell 		return (rval);
9966ff6d951SJohn Birrell 
9976ff6d951SJohn Birrell 	return (dt_aggregate_varcmp(lhs, rhs));
9986ff6d951SJohn Birrell }
9996ff6d951SJohn Birrell 
10006ff6d951SJohn Birrell static int
10016ff6d951SJohn Birrell dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
10026ff6d951SJohn Birrell {
10036ff6d951SJohn Birrell 	int rval;
10046ff6d951SJohn Birrell 
10056ff6d951SJohn Birrell 	if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
10066ff6d951SJohn Birrell 		return (rval);
10076ff6d951SJohn Birrell 
10086ff6d951SJohn Birrell 	return (dt_aggregate_valkeycmp(lhs, rhs));
10096ff6d951SJohn Birrell }
10106ff6d951SJohn Birrell 
10116ff6d951SJohn Birrell static int
10126ff6d951SJohn Birrell dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
10136ff6d951SJohn Birrell {
10146ff6d951SJohn Birrell 	return (dt_aggregate_keyvarcmp(rhs, lhs));
10156ff6d951SJohn Birrell }
10166ff6d951SJohn Birrell 
10176ff6d951SJohn Birrell static int
10186ff6d951SJohn Birrell dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
10196ff6d951SJohn Birrell {
10206ff6d951SJohn Birrell 	return (dt_aggregate_varkeycmp(rhs, lhs));
10216ff6d951SJohn Birrell }
10226ff6d951SJohn Birrell 
10236ff6d951SJohn Birrell static int
10246ff6d951SJohn Birrell dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
10256ff6d951SJohn Birrell {
10266ff6d951SJohn Birrell 	return (dt_aggregate_valvarcmp(rhs, lhs));
10276ff6d951SJohn Birrell }
10286ff6d951SJohn Birrell 
10296ff6d951SJohn Birrell static int
10306ff6d951SJohn Birrell dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
10316ff6d951SJohn Birrell {
10326ff6d951SJohn Birrell 	return (dt_aggregate_varvalcmp(rhs, lhs));
10336ff6d951SJohn Birrell }
10346ff6d951SJohn Birrell 
10356ff6d951SJohn Birrell static int
10366ff6d951SJohn Birrell dt_aggregate_bundlecmp(const void *lhs, const void *rhs)
10376ff6d951SJohn Birrell {
10386ff6d951SJohn Birrell 	dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
10396ff6d951SJohn Birrell 	dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
10406ff6d951SJohn Birrell 	int i, rval;
10416ff6d951SJohn Birrell 
10426ff6d951SJohn Birrell 	if (dt_keysort) {
10436ff6d951SJohn Birrell 		/*
10446ff6d951SJohn Birrell 		 * If we're sorting on keys, we need to scan until we find the
10456ff6d951SJohn Birrell 		 * last entry -- that's the representative key.  (The order of
10466ff6d951SJohn Birrell 		 * the bundle is values followed by key to accommodate the
10476ff6d951SJohn Birrell 		 * default behavior of sorting by value.)  If the keys are
10486ff6d951SJohn Birrell 		 * equal, we'll fall into the value comparison loop, below.
10496ff6d951SJohn Birrell 		 */
10506ff6d951SJohn Birrell 		for (i = 0; lh[i + 1] != NULL; i++)
10516ff6d951SJohn Birrell 			continue;
10526ff6d951SJohn Birrell 
10536ff6d951SJohn Birrell 		assert(i != 0);
10546ff6d951SJohn Birrell 		assert(rh[i + 1] == NULL);
10556ff6d951SJohn Birrell 
10566ff6d951SJohn Birrell 		if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
10576ff6d951SJohn Birrell 			return (rval);
10586ff6d951SJohn Birrell 	}
10596ff6d951SJohn Birrell 
10606ff6d951SJohn Birrell 	for (i = 0; ; i++) {
10616ff6d951SJohn Birrell 		if (lh[i + 1] == NULL) {
10626ff6d951SJohn Birrell 			/*
10636ff6d951SJohn Birrell 			 * All of the values are equal; if we're sorting on
10646ff6d951SJohn Birrell 			 * keys, then we're only here because the keys were
10656ff6d951SJohn Birrell 			 * found to be equal and these records are therefore
10666ff6d951SJohn Birrell 			 * equal.  If we're not sorting on keys, we'll use the
10676ff6d951SJohn Birrell 			 * key comparison from the representative key as the
10686ff6d951SJohn Birrell 			 * tie-breaker.
10696ff6d951SJohn Birrell 			 */
10706ff6d951SJohn Birrell 			if (dt_keysort)
10716ff6d951SJohn Birrell 				return (0);
10726ff6d951SJohn Birrell 
10736ff6d951SJohn Birrell 			assert(i != 0);
10746ff6d951SJohn Birrell 			assert(rh[i + 1] == NULL);
10756ff6d951SJohn Birrell 			return (dt_aggregate_keycmp(&lh[i], &rh[i]));
10766ff6d951SJohn Birrell 		} else {
10776ff6d951SJohn Birrell 			if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
10786ff6d951SJohn Birrell 				return (rval);
10796ff6d951SJohn Birrell 		}
10806ff6d951SJohn Birrell 	}
10816ff6d951SJohn Birrell }
10826ff6d951SJohn Birrell 
10836ff6d951SJohn Birrell int
10846ff6d951SJohn Birrell dt_aggregate_go(dtrace_hdl_t *dtp)
10856ff6d951SJohn Birrell {
10866ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
10876ff6d951SJohn Birrell 	dtrace_optval_t size, cpu;
10886ff6d951SJohn Birrell 	dtrace_bufdesc_t *buf = &agp->dtat_buf;
10896ff6d951SJohn Birrell 	int rval, i;
10906ff6d951SJohn Birrell 
10916ff6d951SJohn Birrell 	assert(agp->dtat_maxcpu == 0);
10926ff6d951SJohn Birrell 	assert(agp->dtat_ncpu == 0);
10936ff6d951SJohn Birrell 	assert(agp->dtat_cpus == NULL);
10946ff6d951SJohn Birrell 
10956ff6d951SJohn Birrell 	agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
10966ff6d951SJohn Birrell 	agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
10976ff6d951SJohn Birrell 	agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
10986ff6d951SJohn Birrell 
10996ff6d951SJohn Birrell 	if (agp->dtat_cpus == NULL)
11006ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
11016ff6d951SJohn Birrell 
11026ff6d951SJohn Birrell 	/*
11036ff6d951SJohn Birrell 	 * Use the aggregation buffer size as reloaded from the kernel.
11046ff6d951SJohn Birrell 	 */
11056ff6d951SJohn Birrell 	size = dtp->dt_options[DTRACEOPT_AGGSIZE];
11066ff6d951SJohn Birrell 
11076ff6d951SJohn Birrell 	rval = dtrace_getopt(dtp, "aggsize", &size);
11086ff6d951SJohn Birrell 	assert(rval == 0);
11096ff6d951SJohn Birrell 
11106ff6d951SJohn Birrell 	if (size == 0 || size == DTRACEOPT_UNSET)
11116ff6d951SJohn Birrell 		return (0);
11126ff6d951SJohn Birrell 
11136ff6d951SJohn Birrell 	buf = &agp->dtat_buf;
11146ff6d951SJohn Birrell 	buf->dtbd_size = size;
11156ff6d951SJohn Birrell 
11166ff6d951SJohn Birrell 	if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
11176ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
11186ff6d951SJohn Birrell 
11196ff6d951SJohn Birrell 	/*
11206ff6d951SJohn Birrell 	 * Now query for the CPUs enabled.
11216ff6d951SJohn Birrell 	 */
11226ff6d951SJohn Birrell 	rval = dtrace_getopt(dtp, "cpu", &cpu);
11236ff6d951SJohn Birrell 	assert(rval == 0 && cpu != DTRACEOPT_UNSET);
11246ff6d951SJohn Birrell 
11256ff6d951SJohn Birrell 	if (cpu != DTRACE_CPUALL) {
11266ff6d951SJohn Birrell 		assert(cpu < agp->dtat_ncpu);
11276ff6d951SJohn Birrell 		agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
11286ff6d951SJohn Birrell 
11296ff6d951SJohn Birrell 		return (0);
11306ff6d951SJohn Birrell 	}
11316ff6d951SJohn Birrell 
11326ff6d951SJohn Birrell 	agp->dtat_ncpus = 0;
11336ff6d951SJohn Birrell 	for (i = 0; i < agp->dtat_maxcpu; i++) {
11346ff6d951SJohn Birrell 		if (dt_status(dtp, i) == -1)
11356ff6d951SJohn Birrell 			continue;
11366ff6d951SJohn Birrell 
11376ff6d951SJohn Birrell 		agp->dtat_cpus[agp->dtat_ncpus++] = i;
11386ff6d951SJohn Birrell 	}
11396ff6d951SJohn Birrell 
11406ff6d951SJohn Birrell 	return (0);
11416ff6d951SJohn Birrell }
11426ff6d951SJohn Birrell 
11436ff6d951SJohn Birrell static int
11446ff6d951SJohn Birrell dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
11456ff6d951SJohn Birrell {
11466ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
11476ff6d951SJohn Birrell 	dtrace_aggdata_t *data;
11486ff6d951SJohn Birrell 	dtrace_aggdesc_t *aggdesc;
11496ff6d951SJohn Birrell 	dtrace_recdesc_t *rec;
11506ff6d951SJohn Birrell 	int i;
11516ff6d951SJohn Birrell 
11526ff6d951SJohn Birrell 	switch (rval) {
11536ff6d951SJohn Birrell 	case DTRACE_AGGWALK_NEXT:
11546ff6d951SJohn Birrell 		break;
11556ff6d951SJohn Birrell 
11566ff6d951SJohn Birrell 	case DTRACE_AGGWALK_CLEAR: {
11576ff6d951SJohn Birrell 		uint32_t size, offs = 0;
11586ff6d951SJohn Birrell 
11596ff6d951SJohn Birrell 		aggdesc = h->dtahe_data.dtada_desc;
11606ff6d951SJohn Birrell 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
11616ff6d951SJohn Birrell 		size = rec->dtrd_size;
11626ff6d951SJohn Birrell 		data = &h->dtahe_data;
11636ff6d951SJohn Birrell 
11646ff6d951SJohn Birrell 		if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
11656ff6d951SJohn Birrell 			offs = sizeof (uint64_t);
11666ff6d951SJohn Birrell 			size -= sizeof (uint64_t);
11676ff6d951SJohn Birrell 		}
11686ff6d951SJohn Birrell 
11696ff6d951SJohn Birrell 		bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
11706ff6d951SJohn Birrell 
11716ff6d951SJohn Birrell 		if (data->dtada_percpu == NULL)
11726ff6d951SJohn Birrell 			break;
11736ff6d951SJohn Birrell 
11746ff6d951SJohn Birrell 		for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
11756ff6d951SJohn Birrell 			bzero(data->dtada_percpu[i] + offs, size);
11766ff6d951SJohn Birrell 		break;
11776ff6d951SJohn Birrell 	}
11786ff6d951SJohn Birrell 
11796ff6d951SJohn Birrell 	case DTRACE_AGGWALK_ERROR:
11806ff6d951SJohn Birrell 		/*
11816ff6d951SJohn Birrell 		 * We assume that errno is already set in this case.
11826ff6d951SJohn Birrell 		 */
11836ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
11846ff6d951SJohn Birrell 
11856ff6d951SJohn Birrell 	case DTRACE_AGGWALK_ABORT:
11866ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_DIRABORT));
11876ff6d951SJohn Birrell 
11886ff6d951SJohn Birrell 	case DTRACE_AGGWALK_DENORMALIZE:
11896ff6d951SJohn Birrell 		h->dtahe_data.dtada_normal = 1;
11906ff6d951SJohn Birrell 		return (0);
11916ff6d951SJohn Birrell 
11926ff6d951SJohn Birrell 	case DTRACE_AGGWALK_NORMALIZE:
11936ff6d951SJohn Birrell 		if (h->dtahe_data.dtada_normal == 0) {
11946ff6d951SJohn Birrell 			h->dtahe_data.dtada_normal = 1;
11956ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_BADRVAL));
11966ff6d951SJohn Birrell 		}
11976ff6d951SJohn Birrell 
11986ff6d951SJohn Birrell 		return (0);
11996ff6d951SJohn Birrell 
12006ff6d951SJohn Birrell 	case DTRACE_AGGWALK_REMOVE: {
12016ff6d951SJohn Birrell 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
1202d9ce0144SJohn Birrell 		int max_cpus = agp->dtat_maxcpu;
12036ff6d951SJohn Birrell 
12046ff6d951SJohn Birrell 		/*
12056ff6d951SJohn Birrell 		 * First, remove this hash entry from its hash chain.
12066ff6d951SJohn Birrell 		 */
12076ff6d951SJohn Birrell 		if (h->dtahe_prev != NULL) {
12086ff6d951SJohn Birrell 			h->dtahe_prev->dtahe_next = h->dtahe_next;
12096ff6d951SJohn Birrell 		} else {
12106ff6d951SJohn Birrell 			dt_ahash_t *hash = &agp->dtat_hash;
12116ff6d951SJohn Birrell 			size_t ndx = h->dtahe_hashval % hash->dtah_size;
12126ff6d951SJohn Birrell 
12136ff6d951SJohn Birrell 			assert(hash->dtah_hash[ndx] == h);
12146ff6d951SJohn Birrell 			hash->dtah_hash[ndx] = h->dtahe_next;
12156ff6d951SJohn Birrell 		}
12166ff6d951SJohn Birrell 
12176ff6d951SJohn Birrell 		if (h->dtahe_next != NULL)
12186ff6d951SJohn Birrell 			h->dtahe_next->dtahe_prev = h->dtahe_prev;
12196ff6d951SJohn Birrell 
12206ff6d951SJohn Birrell 		/*
12216ff6d951SJohn Birrell 		 * Now remove it from the list of all hash entries.
12226ff6d951SJohn Birrell 		 */
12236ff6d951SJohn Birrell 		if (h->dtahe_prevall != NULL) {
12246ff6d951SJohn Birrell 			h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
12256ff6d951SJohn Birrell 		} else {
12266ff6d951SJohn Birrell 			dt_ahash_t *hash = &agp->dtat_hash;
12276ff6d951SJohn Birrell 
12286ff6d951SJohn Birrell 			assert(hash->dtah_all == h);
12296ff6d951SJohn Birrell 			hash->dtah_all = h->dtahe_nextall;
12306ff6d951SJohn Birrell 		}
12316ff6d951SJohn Birrell 
12326ff6d951SJohn Birrell 		if (h->dtahe_nextall != NULL)
12336ff6d951SJohn Birrell 			h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
12346ff6d951SJohn Birrell 
12356ff6d951SJohn Birrell 		/*
12366ff6d951SJohn Birrell 		 * We're unlinked.  We can safely destroy the data.
12376ff6d951SJohn Birrell 		 */
12386ff6d951SJohn Birrell 		if (aggdata->dtada_percpu != NULL) {
12396ff6d951SJohn Birrell 			for (i = 0; i < max_cpus; i++)
12406ff6d951SJohn Birrell 				free(aggdata->dtada_percpu[i]);
12416ff6d951SJohn Birrell 			free(aggdata->dtada_percpu);
12426ff6d951SJohn Birrell 		}
12436ff6d951SJohn Birrell 
12446ff6d951SJohn Birrell 		free(aggdata->dtada_data);
12456ff6d951SJohn Birrell 		free(h);
12466ff6d951SJohn Birrell 
12476ff6d951SJohn Birrell 		return (0);
12486ff6d951SJohn Birrell 	}
12496ff6d951SJohn Birrell 
12506ff6d951SJohn Birrell 	default:
12516ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_BADRVAL));
12526ff6d951SJohn Birrell 	}
12536ff6d951SJohn Birrell 
12546ff6d951SJohn Birrell 	return (0);
12556ff6d951SJohn Birrell }
12566ff6d951SJohn Birrell 
12576ff6d951SJohn Birrell void
12586ff6d951SJohn Birrell dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
12596ff6d951SJohn Birrell     int (*compar)(const void *, const void *))
12606ff6d951SJohn Birrell {
12616ff6d951SJohn Birrell 	int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
12626ff6d951SJohn Birrell 	dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
12636ff6d951SJohn Birrell 
12646ff6d951SJohn Birrell 	dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
12656ff6d951SJohn Birrell 	dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
12666ff6d951SJohn Birrell 
12676ff6d951SJohn Birrell 	if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
12686ff6d951SJohn Birrell 		dt_keypos = (int)keyposopt;
12696ff6d951SJohn Birrell 	} else {
12706ff6d951SJohn Birrell 		dt_keypos = 0;
12716ff6d951SJohn Birrell 	}
12726ff6d951SJohn Birrell 
12736ff6d951SJohn Birrell 	if (compar == NULL) {
12746ff6d951SJohn Birrell 		if (!dt_keysort) {
12756ff6d951SJohn Birrell 			compar = dt_aggregate_varvalcmp;
12766ff6d951SJohn Birrell 		} else {
12776ff6d951SJohn Birrell 			compar = dt_aggregate_varkeycmp;
12786ff6d951SJohn Birrell 		}
12796ff6d951SJohn Birrell 	}
12806ff6d951SJohn Birrell 
12816ff6d951SJohn Birrell 	qsort(base, nel, width, compar);
12826ff6d951SJohn Birrell 
12836ff6d951SJohn Birrell 	dt_revsort = rev;
12846ff6d951SJohn Birrell 	dt_keysort = key;
12856ff6d951SJohn Birrell 	dt_keypos = keypos;
12866ff6d951SJohn Birrell }
12876ff6d951SJohn Birrell 
12886ff6d951SJohn Birrell int
12896ff6d951SJohn Birrell dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
12906ff6d951SJohn Birrell {
12916ff6d951SJohn Birrell 	dt_ahashent_t *h, *next;
12926ff6d951SJohn Birrell 	dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
12936ff6d951SJohn Birrell 
12946ff6d951SJohn Birrell 	for (h = hash->dtah_all; h != NULL; h = next) {
12956ff6d951SJohn Birrell 		/*
12966ff6d951SJohn Birrell 		 * dt_aggwalk_rval() can potentially remove the current hash
12976ff6d951SJohn Birrell 		 * entry; we need to load the next hash entry before calling
12986ff6d951SJohn Birrell 		 * into it.
12996ff6d951SJohn Birrell 		 */
13006ff6d951SJohn Birrell 		next = h->dtahe_nextall;
13016ff6d951SJohn Birrell 
13026ff6d951SJohn Birrell 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
13036ff6d951SJohn Birrell 			return (-1);
13046ff6d951SJohn Birrell 	}
13056ff6d951SJohn Birrell 
13066ff6d951SJohn Birrell 	return (0);
13076ff6d951SJohn Birrell }
13086ff6d951SJohn Birrell 
13096ff6d951SJohn Birrell static int
1310a43f0be9SRui Paulo dt_aggregate_total(dtrace_hdl_t *dtp, boolean_t clear)
1311a43f0be9SRui Paulo {
1312a43f0be9SRui Paulo 	dt_ahashent_t *h;
1313a43f0be9SRui Paulo 	dtrace_aggdata_t **total;
1314a43f0be9SRui Paulo 	dtrace_aggid_t max = DTRACE_AGGVARIDNONE, id;
1315a43f0be9SRui Paulo 	dt_aggregate_t *agp = &dtp->dt_aggregate;
1316a43f0be9SRui Paulo 	dt_ahash_t *hash = &agp->dtat_hash;
1317a43f0be9SRui Paulo 	uint32_t tflags;
1318a43f0be9SRui Paulo 
1319a43f0be9SRui Paulo 	tflags = DTRACE_A_TOTAL | DTRACE_A_HASNEGATIVES | DTRACE_A_HASPOSITIVES;
1320a43f0be9SRui Paulo 
1321a43f0be9SRui Paulo 	/*
1322a43f0be9SRui Paulo 	 * If we need to deliver per-aggregation totals, we're going to take
1323a43f0be9SRui Paulo 	 * three passes over the aggregate:  one to clear everything out and
1324a43f0be9SRui Paulo 	 * determine our maximum aggregation ID, one to actually total
1325a43f0be9SRui Paulo 	 * everything up, and a final pass to assign the totals to the
1326a43f0be9SRui Paulo 	 * individual elements.
1327a43f0be9SRui Paulo 	 */
1328a43f0be9SRui Paulo 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1329a43f0be9SRui Paulo 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
1330a43f0be9SRui Paulo 
1331a43f0be9SRui Paulo 		if ((id = dt_aggregate_aggvarid(h)) > max)
1332a43f0be9SRui Paulo 			max = id;
1333a43f0be9SRui Paulo 
1334a43f0be9SRui Paulo 		aggdata->dtada_total = 0;
1335a43f0be9SRui Paulo 		aggdata->dtada_flags &= ~tflags;
1336a43f0be9SRui Paulo 	}
1337a43f0be9SRui Paulo 
1338a43f0be9SRui Paulo 	if (clear || max == DTRACE_AGGVARIDNONE)
1339a43f0be9SRui Paulo 		return (0);
1340a43f0be9SRui Paulo 
1341a43f0be9SRui Paulo 	total = dt_zalloc(dtp, (max + 1) * sizeof (dtrace_aggdata_t *));
1342a43f0be9SRui Paulo 
1343a43f0be9SRui Paulo 	if (total == NULL)
1344a43f0be9SRui Paulo 		return (-1);
1345a43f0be9SRui Paulo 
1346a43f0be9SRui Paulo 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1347a43f0be9SRui Paulo 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
1348a43f0be9SRui Paulo 		dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1349a43f0be9SRui Paulo 		dtrace_recdesc_t *rec;
1350a43f0be9SRui Paulo 		caddr_t data;
1351a43f0be9SRui Paulo 		int64_t val, *addr;
1352a43f0be9SRui Paulo 
1353a43f0be9SRui Paulo 		rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
1354a43f0be9SRui Paulo 		data = aggdata->dtada_data;
1355a43f0be9SRui Paulo 		addr = (int64_t *)(uintptr_t)(data + rec->dtrd_offset);
1356a43f0be9SRui Paulo 
1357a43f0be9SRui Paulo 		switch (rec->dtrd_action) {
1358a43f0be9SRui Paulo 		case DTRACEAGG_STDDEV:
1359a43f0be9SRui Paulo 			val = dt_stddev((uint64_t *)addr, 1);
1360a43f0be9SRui Paulo 			break;
1361a43f0be9SRui Paulo 
1362a43f0be9SRui Paulo 		case DTRACEAGG_SUM:
1363a43f0be9SRui Paulo 		case DTRACEAGG_COUNT:
1364a43f0be9SRui Paulo 			val = *addr;
1365a43f0be9SRui Paulo 			break;
1366a43f0be9SRui Paulo 
1367a43f0be9SRui Paulo 		case DTRACEAGG_AVG:
1368a43f0be9SRui Paulo 			val = addr[0] ? (addr[1] / addr[0]) : 0;
1369a43f0be9SRui Paulo 			break;
1370a43f0be9SRui Paulo 
1371a43f0be9SRui Paulo 		default:
1372a43f0be9SRui Paulo 			continue;
1373a43f0be9SRui Paulo 		}
1374a43f0be9SRui Paulo 
1375a43f0be9SRui Paulo 		if (total[agg->dtagd_varid] == NULL) {
1376a43f0be9SRui Paulo 			total[agg->dtagd_varid] = aggdata;
1377a43f0be9SRui Paulo 			aggdata->dtada_flags |= DTRACE_A_TOTAL;
1378a43f0be9SRui Paulo 		} else {
1379a43f0be9SRui Paulo 			aggdata = total[agg->dtagd_varid];
1380a43f0be9SRui Paulo 		}
1381a43f0be9SRui Paulo 
1382a43f0be9SRui Paulo 		if (val > 0)
1383a43f0be9SRui Paulo 			aggdata->dtada_flags |= DTRACE_A_HASPOSITIVES;
1384a43f0be9SRui Paulo 
1385a43f0be9SRui Paulo 		if (val < 0) {
1386a43f0be9SRui Paulo 			aggdata->dtada_flags |= DTRACE_A_HASNEGATIVES;
1387a43f0be9SRui Paulo 			val = -val;
1388a43f0be9SRui Paulo 		}
1389a43f0be9SRui Paulo 
1390a43f0be9SRui Paulo 		if (dtp->dt_options[DTRACEOPT_AGGZOOM] != DTRACEOPT_UNSET) {
1391a43f0be9SRui Paulo 			val = (int64_t)((long double)val *
1392a43f0be9SRui Paulo 			    (1 / DTRACE_AGGZOOM_MAX));
1393a43f0be9SRui Paulo 
1394a43f0be9SRui Paulo 			if (val > aggdata->dtada_total)
1395a43f0be9SRui Paulo 				aggdata->dtada_total = val;
1396a43f0be9SRui Paulo 		} else {
1397a43f0be9SRui Paulo 			aggdata->dtada_total += val;
1398a43f0be9SRui Paulo 		}
1399a43f0be9SRui Paulo 	}
1400a43f0be9SRui Paulo 
1401a43f0be9SRui Paulo 	/*
1402a43f0be9SRui Paulo 	 * And now one final pass to set everyone's total.
1403a43f0be9SRui Paulo 	 */
1404a43f0be9SRui Paulo 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1405a43f0be9SRui Paulo 		dtrace_aggdata_t *aggdata = &h->dtahe_data, *t;
1406a43f0be9SRui Paulo 		dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1407a43f0be9SRui Paulo 
1408a43f0be9SRui Paulo 		if ((t = total[agg->dtagd_varid]) == NULL || aggdata == t)
1409a43f0be9SRui Paulo 			continue;
1410a43f0be9SRui Paulo 
1411a43f0be9SRui Paulo 		aggdata->dtada_total = t->dtada_total;
1412a43f0be9SRui Paulo 		aggdata->dtada_flags |= (t->dtada_flags & tflags);
1413a43f0be9SRui Paulo 	}
1414a43f0be9SRui Paulo 
1415a43f0be9SRui Paulo 	dt_free(dtp, total);
1416a43f0be9SRui Paulo 
1417a43f0be9SRui Paulo 	return (0);
1418a43f0be9SRui Paulo }
1419a43f0be9SRui Paulo 
1420a43f0be9SRui Paulo static int
1421a43f0be9SRui Paulo dt_aggregate_minmaxbin(dtrace_hdl_t *dtp, boolean_t clear)
1422a43f0be9SRui Paulo {
1423a43f0be9SRui Paulo 	dt_ahashent_t *h;
1424a43f0be9SRui Paulo 	dtrace_aggdata_t **minmax;
1425a43f0be9SRui Paulo 	dtrace_aggid_t max = DTRACE_AGGVARIDNONE, id;
1426a43f0be9SRui Paulo 	dt_aggregate_t *agp = &dtp->dt_aggregate;
1427a43f0be9SRui Paulo 	dt_ahash_t *hash = &agp->dtat_hash;
1428a43f0be9SRui Paulo 
1429a43f0be9SRui Paulo 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1430a43f0be9SRui Paulo 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
1431a43f0be9SRui Paulo 
1432a43f0be9SRui Paulo 		if ((id = dt_aggregate_aggvarid(h)) > max)
1433a43f0be9SRui Paulo 			max = id;
1434a43f0be9SRui Paulo 
1435a43f0be9SRui Paulo 		aggdata->dtada_minbin = 0;
1436a43f0be9SRui Paulo 		aggdata->dtada_maxbin = 0;
1437a43f0be9SRui Paulo 		aggdata->dtada_flags &= ~DTRACE_A_MINMAXBIN;
1438a43f0be9SRui Paulo 	}
1439a43f0be9SRui Paulo 
1440a43f0be9SRui Paulo 	if (clear || max == DTRACE_AGGVARIDNONE)
1441a43f0be9SRui Paulo 		return (0);
1442a43f0be9SRui Paulo 
1443a43f0be9SRui Paulo 	minmax = dt_zalloc(dtp, (max + 1) * sizeof (dtrace_aggdata_t *));
1444a43f0be9SRui Paulo 
1445a43f0be9SRui Paulo 	if (minmax == NULL)
1446a43f0be9SRui Paulo 		return (-1);
1447a43f0be9SRui Paulo 
1448a43f0be9SRui Paulo 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1449a43f0be9SRui Paulo 		dtrace_aggdata_t *aggdata = &h->dtahe_data;
1450a43f0be9SRui Paulo 		dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1451a43f0be9SRui Paulo 		dtrace_recdesc_t *rec;
1452a43f0be9SRui Paulo 		caddr_t data;
1453a43f0be9SRui Paulo 		int64_t *addr;
1454a43f0be9SRui Paulo 		int minbin = -1, maxbin = -1, i;
1455a43f0be9SRui Paulo 		int start = 0, size;
1456a43f0be9SRui Paulo 
1457a43f0be9SRui Paulo 		rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
1458a43f0be9SRui Paulo 		size = rec->dtrd_size / sizeof (int64_t);
1459a43f0be9SRui Paulo 		data = aggdata->dtada_data;
1460a43f0be9SRui Paulo 		addr = (int64_t *)(uintptr_t)(data + rec->dtrd_offset);
1461a43f0be9SRui Paulo 
1462a43f0be9SRui Paulo 		switch (rec->dtrd_action) {
1463a43f0be9SRui Paulo 		case DTRACEAGG_LQUANTIZE:
1464a43f0be9SRui Paulo 			/*
1465a43f0be9SRui Paulo 			 * For lquantize(), we always display the entire range
1466a43f0be9SRui Paulo 			 * of the aggregation when aggpack is set.
1467a43f0be9SRui Paulo 			 */
1468a43f0be9SRui Paulo 			start = 1;
1469a43f0be9SRui Paulo 			minbin = start;
1470a43f0be9SRui Paulo 			maxbin = size - 1 - start;
1471a43f0be9SRui Paulo 			break;
1472a43f0be9SRui Paulo 
1473a43f0be9SRui Paulo 		case DTRACEAGG_QUANTIZE:
1474a43f0be9SRui Paulo 			for (i = start; i < size; i++) {
1475a43f0be9SRui Paulo 				if (!addr[i])
1476a43f0be9SRui Paulo 					continue;
1477a43f0be9SRui Paulo 
1478a43f0be9SRui Paulo 				if (minbin == -1)
1479a43f0be9SRui Paulo 					minbin = i - start;
1480a43f0be9SRui Paulo 
1481a43f0be9SRui Paulo 				maxbin = i - start;
1482a43f0be9SRui Paulo 			}
1483a43f0be9SRui Paulo 
1484a43f0be9SRui Paulo 			if (minbin == -1) {
1485a43f0be9SRui Paulo 				/*
1486a43f0be9SRui Paulo 				 * If we have no data (e.g., due to a clear()
1487a43f0be9SRui Paulo 				 * or negative increments), we'll use the
1488a43f0be9SRui Paulo 				 * zero bucket as both our min and max.
1489a43f0be9SRui Paulo 				 */
1490a43f0be9SRui Paulo 				minbin = maxbin = DTRACE_QUANTIZE_ZEROBUCKET;
1491a43f0be9SRui Paulo 			}
1492a43f0be9SRui Paulo 
1493a43f0be9SRui Paulo 			break;
1494a43f0be9SRui Paulo 
1495a43f0be9SRui Paulo 		default:
1496a43f0be9SRui Paulo 			continue;
1497a43f0be9SRui Paulo 		}
1498a43f0be9SRui Paulo 
1499a43f0be9SRui Paulo 		if (minmax[agg->dtagd_varid] == NULL) {
1500a43f0be9SRui Paulo 			minmax[agg->dtagd_varid] = aggdata;
1501a43f0be9SRui Paulo 			aggdata->dtada_flags |= DTRACE_A_MINMAXBIN;
1502a43f0be9SRui Paulo 			aggdata->dtada_minbin = minbin;
1503a43f0be9SRui Paulo 			aggdata->dtada_maxbin = maxbin;
1504a43f0be9SRui Paulo 			continue;
1505a43f0be9SRui Paulo 		}
1506a43f0be9SRui Paulo 
1507a43f0be9SRui Paulo 		if (minbin < minmax[agg->dtagd_varid]->dtada_minbin)
1508a43f0be9SRui Paulo 			minmax[agg->dtagd_varid]->dtada_minbin = minbin;
1509a43f0be9SRui Paulo 
1510a43f0be9SRui Paulo 		if (maxbin > minmax[agg->dtagd_varid]->dtada_maxbin)
1511a43f0be9SRui Paulo 			minmax[agg->dtagd_varid]->dtada_maxbin = maxbin;
1512a43f0be9SRui Paulo 	}
1513a43f0be9SRui Paulo 
1514a43f0be9SRui Paulo 	/*
1515a43f0be9SRui Paulo 	 * And now one final pass to set everyone's minbin and maxbin.
1516a43f0be9SRui Paulo 	 */
1517a43f0be9SRui Paulo 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1518a43f0be9SRui Paulo 		dtrace_aggdata_t *aggdata = &h->dtahe_data, *mm;
1519a43f0be9SRui Paulo 		dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1520a43f0be9SRui Paulo 
1521a43f0be9SRui Paulo 		if ((mm = minmax[agg->dtagd_varid]) == NULL || aggdata == mm)
1522a43f0be9SRui Paulo 			continue;
1523a43f0be9SRui Paulo 
1524a43f0be9SRui Paulo 		aggdata->dtada_minbin = mm->dtada_minbin;
1525a43f0be9SRui Paulo 		aggdata->dtada_maxbin = mm->dtada_maxbin;
1526a43f0be9SRui Paulo 		aggdata->dtada_flags |= DTRACE_A_MINMAXBIN;
1527a43f0be9SRui Paulo 	}
1528a43f0be9SRui Paulo 
1529a43f0be9SRui Paulo 	dt_free(dtp, minmax);
1530a43f0be9SRui Paulo 
1531a43f0be9SRui Paulo 	return (0);
1532a43f0be9SRui Paulo }
1533a43f0be9SRui Paulo 
1534a43f0be9SRui Paulo static int
15356ff6d951SJohn Birrell dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
15366ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg,
15376ff6d951SJohn Birrell     int (*sfunc)(const void *, const void *))
15386ff6d951SJohn Birrell {
15396ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
15406ff6d951SJohn Birrell 	dt_ahashent_t *h, **sorted;
15416ff6d951SJohn Birrell 	dt_ahash_t *hash = &agp->dtat_hash;
15426ff6d951SJohn Birrell 	size_t i, nentries = 0;
1543a43f0be9SRui Paulo 	int rval = -1;
1544a43f0be9SRui Paulo 
1545a43f0be9SRui Paulo 	agp->dtat_flags &= ~(DTRACE_A_TOTAL | DTRACE_A_MINMAXBIN);
1546a43f0be9SRui Paulo 
1547a43f0be9SRui Paulo 	if (dtp->dt_options[DTRACEOPT_AGGHIST] != DTRACEOPT_UNSET) {
1548a43f0be9SRui Paulo 		agp->dtat_flags |= DTRACE_A_TOTAL;
1549a43f0be9SRui Paulo 
1550a43f0be9SRui Paulo 		if (dt_aggregate_total(dtp, B_FALSE) != 0)
1551a43f0be9SRui Paulo 			return (-1);
1552a43f0be9SRui Paulo 	}
1553a43f0be9SRui Paulo 
1554a43f0be9SRui Paulo 	if (dtp->dt_options[DTRACEOPT_AGGPACK] != DTRACEOPT_UNSET) {
1555a43f0be9SRui Paulo 		agp->dtat_flags |= DTRACE_A_MINMAXBIN;
1556a43f0be9SRui Paulo 
1557a43f0be9SRui Paulo 		if (dt_aggregate_minmaxbin(dtp, B_FALSE) != 0)
1558a43f0be9SRui Paulo 			return (-1);
1559a43f0be9SRui Paulo 	}
15606ff6d951SJohn Birrell 
15616ff6d951SJohn Birrell 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
15626ff6d951SJohn Birrell 		nentries++;
15636ff6d951SJohn Birrell 
15646ff6d951SJohn Birrell 	sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
15656ff6d951SJohn Birrell 
15666ff6d951SJohn Birrell 	if (sorted == NULL)
1567a43f0be9SRui Paulo 		goto out;
15686ff6d951SJohn Birrell 
15696ff6d951SJohn Birrell 	for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
15706ff6d951SJohn Birrell 		sorted[i++] = h;
15716ff6d951SJohn Birrell 
15726ff6d951SJohn Birrell 	(void) pthread_mutex_lock(&dt_qsort_lock);
15736ff6d951SJohn Birrell 
15746ff6d951SJohn Birrell 	if (sfunc == NULL) {
15756ff6d951SJohn Birrell 		dt_aggregate_qsort(dtp, sorted, nentries,
15766ff6d951SJohn Birrell 		    sizeof (dt_ahashent_t *), NULL);
15776ff6d951SJohn Birrell 	} else {
15786ff6d951SJohn Birrell 		/*
15796ff6d951SJohn Birrell 		 * If we've been explicitly passed a sorting function,
15806ff6d951SJohn Birrell 		 * we'll use that -- ignoring the values of the "aggsortrev",
15816ff6d951SJohn Birrell 		 * "aggsortkey" and "aggsortkeypos" options.
15826ff6d951SJohn Birrell 		 */
15836ff6d951SJohn Birrell 		qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
15846ff6d951SJohn Birrell 	}
15856ff6d951SJohn Birrell 
15866ff6d951SJohn Birrell 	(void) pthread_mutex_unlock(&dt_qsort_lock);
15876ff6d951SJohn Birrell 
15886ff6d951SJohn Birrell 	for (i = 0; i < nentries; i++) {
15896ff6d951SJohn Birrell 		h = sorted[i];
15906ff6d951SJohn Birrell 
1591a43f0be9SRui Paulo 		if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1592a43f0be9SRui Paulo 			goto out;
15936ff6d951SJohn Birrell 	}
15946ff6d951SJohn Birrell 
1595a43f0be9SRui Paulo 	rval = 0;
1596a43f0be9SRui Paulo out:
1597a43f0be9SRui Paulo 	if (agp->dtat_flags & DTRACE_A_TOTAL)
1598a43f0be9SRui Paulo 		(void) dt_aggregate_total(dtp, B_TRUE);
1599a43f0be9SRui Paulo 
1600a43f0be9SRui Paulo 	if (agp->dtat_flags & DTRACE_A_MINMAXBIN)
1601a43f0be9SRui Paulo 		(void) dt_aggregate_minmaxbin(dtp, B_TRUE);
1602a43f0be9SRui Paulo 
16036ff6d951SJohn Birrell 	dt_free(dtp, sorted);
1604a43f0be9SRui Paulo 	return (rval);
16056ff6d951SJohn Birrell }
16066ff6d951SJohn Birrell 
16076ff6d951SJohn Birrell int
16086ff6d951SJohn Birrell dtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
16096ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16106ff6d951SJohn Birrell {
16116ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
16126ff6d951SJohn Birrell }
16136ff6d951SJohn Birrell 
16146ff6d951SJohn Birrell int
16156ff6d951SJohn Birrell dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
16166ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16176ff6d951SJohn Birrell {
16186ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16196ff6d951SJohn Birrell 	    arg, dt_aggregate_varkeycmp));
16206ff6d951SJohn Birrell }
16216ff6d951SJohn Birrell 
16226ff6d951SJohn Birrell int
16236ff6d951SJohn Birrell dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
16246ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16256ff6d951SJohn Birrell {
16266ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16276ff6d951SJohn Birrell 	    arg, dt_aggregate_varvalcmp));
16286ff6d951SJohn Birrell }
16296ff6d951SJohn Birrell 
16306ff6d951SJohn Birrell int
16316ff6d951SJohn Birrell dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
16326ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16336ff6d951SJohn Birrell {
16346ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16356ff6d951SJohn Birrell 	    arg, dt_aggregate_keyvarcmp));
16366ff6d951SJohn Birrell }
16376ff6d951SJohn Birrell 
16386ff6d951SJohn Birrell int
16396ff6d951SJohn Birrell dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
16406ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16416ff6d951SJohn Birrell {
16426ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16436ff6d951SJohn Birrell 	    arg, dt_aggregate_valvarcmp));
16446ff6d951SJohn Birrell }
16456ff6d951SJohn Birrell 
16466ff6d951SJohn Birrell int
16476ff6d951SJohn Birrell dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
16486ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16496ff6d951SJohn Birrell {
16506ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16516ff6d951SJohn Birrell 	    arg, dt_aggregate_varkeyrevcmp));
16526ff6d951SJohn Birrell }
16536ff6d951SJohn Birrell 
16546ff6d951SJohn Birrell int
16556ff6d951SJohn Birrell dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
16566ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16576ff6d951SJohn Birrell {
16586ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16596ff6d951SJohn Birrell 	    arg, dt_aggregate_varvalrevcmp));
16606ff6d951SJohn Birrell }
16616ff6d951SJohn Birrell 
16626ff6d951SJohn Birrell int
16636ff6d951SJohn Birrell dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
16646ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16656ff6d951SJohn Birrell {
16666ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16676ff6d951SJohn Birrell 	    arg, dt_aggregate_keyvarrevcmp));
16686ff6d951SJohn Birrell }
16696ff6d951SJohn Birrell 
16706ff6d951SJohn Birrell int
16716ff6d951SJohn Birrell dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
16726ff6d951SJohn Birrell     dtrace_aggregate_f *func, void *arg)
16736ff6d951SJohn Birrell {
16746ff6d951SJohn Birrell 	return (dt_aggregate_walk_sorted(dtp, func,
16756ff6d951SJohn Birrell 	    arg, dt_aggregate_valvarrevcmp));
16766ff6d951SJohn Birrell }
16776ff6d951SJohn Birrell 
16786ff6d951SJohn Birrell int
16796ff6d951SJohn Birrell dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
16806ff6d951SJohn Birrell     int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
16816ff6d951SJohn Birrell {
16826ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
16836ff6d951SJohn Birrell 	dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
16846ff6d951SJohn Birrell 	const dtrace_aggdata_t **data;
16856ff6d951SJohn Birrell 	dt_ahashent_t *zaggdata = NULL;
16866ff6d951SJohn Birrell 	dt_ahash_t *hash = &agp->dtat_hash;
16876ff6d951SJohn Birrell 	size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
16886ff6d951SJohn Birrell 	dtrace_aggvarid_t max = 0, aggvar;
16896ff6d951SJohn Birrell 	int rval = -1, *map, *remap = NULL;
16906ff6d951SJohn Birrell 	int i, j;
16916ff6d951SJohn Birrell 	dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
16926ff6d951SJohn Birrell 
16936ff6d951SJohn Birrell 	/*
16946ff6d951SJohn Birrell 	 * If the sorting position is greater than the number of aggregation
16956ff6d951SJohn Birrell 	 * variable IDs, we silently set it to 0.
16966ff6d951SJohn Birrell 	 */
16976ff6d951SJohn Birrell 	if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
16986ff6d951SJohn Birrell 		sortpos = 0;
16996ff6d951SJohn Birrell 
17006ff6d951SJohn Birrell 	/*
17016ff6d951SJohn Birrell 	 * First we need to translate the specified aggregation variable IDs
17026ff6d951SJohn Birrell 	 * into a linear map that will allow us to translate an aggregation
17036ff6d951SJohn Birrell 	 * variable ID into its position in the specified aggvars.
17046ff6d951SJohn Birrell 	 */
17056ff6d951SJohn Birrell 	for (i = 0; i < naggvars; i++) {
17066ff6d951SJohn Birrell 		if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
17076ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_BADAGGVAR));
17086ff6d951SJohn Birrell 
17096ff6d951SJohn Birrell 		if (aggvars[i] > max)
17106ff6d951SJohn Birrell 			max = aggvars[i];
17116ff6d951SJohn Birrell 	}
17126ff6d951SJohn Birrell 
17136ff6d951SJohn Birrell 	if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
17146ff6d951SJohn Birrell 		return (-1);
17156ff6d951SJohn Birrell 
17166ff6d951SJohn Birrell 	zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
17176ff6d951SJohn Birrell 
17186ff6d951SJohn Birrell 	if (zaggdata == NULL)
17196ff6d951SJohn Birrell 		goto out;
17206ff6d951SJohn Birrell 
17216ff6d951SJohn Birrell 	for (i = 0; i < naggvars; i++) {
17226ff6d951SJohn Birrell 		int ndx = i + sortpos;
17236ff6d951SJohn Birrell 
17246ff6d951SJohn Birrell 		if (ndx >= naggvars)
17256ff6d951SJohn Birrell 			ndx -= naggvars;
17266ff6d951SJohn Birrell 
17276ff6d951SJohn Birrell 		aggvar = aggvars[ndx];
17286ff6d951SJohn Birrell 		assert(aggvar <= max);
17296ff6d951SJohn Birrell 
17306ff6d951SJohn Birrell 		if (map[aggvar]) {
17316ff6d951SJohn Birrell 			/*
17326ff6d951SJohn Birrell 			 * We have an aggregation variable that is present
17336ff6d951SJohn Birrell 			 * more than once in the array of aggregation
17346ff6d951SJohn Birrell 			 * variables.  While it's unclear why one might want
17356ff6d951SJohn Birrell 			 * to do this, it's legal.  To support this construct,
17366ff6d951SJohn Birrell 			 * we will allocate a remap that will indicate the
17376ff6d951SJohn Birrell 			 * position from which this aggregation variable
17386ff6d951SJohn Birrell 			 * should be pulled.  (That is, where the remap will
17396ff6d951SJohn Birrell 			 * map from one position to another.)
17406ff6d951SJohn Birrell 			 */
17416ff6d951SJohn Birrell 			if (remap == NULL) {
17426ff6d951SJohn Birrell 				remap = dt_zalloc(dtp, naggvars * sizeof (int));
17436ff6d951SJohn Birrell 
17446ff6d951SJohn Birrell 				if (remap == NULL)
17456ff6d951SJohn Birrell 					goto out;
17466ff6d951SJohn Birrell 			}
17476ff6d951SJohn Birrell 
17486ff6d951SJohn Birrell 			/*
17496ff6d951SJohn Birrell 			 * Given that the variable is already present, assert
17506ff6d951SJohn Birrell 			 * that following through the mapping and adjusting
17516ff6d951SJohn Birrell 			 * for the sort position yields the same aggregation
17526ff6d951SJohn Birrell 			 * variable ID.
17536ff6d951SJohn Birrell 			 */
17546ff6d951SJohn Birrell 			assert(aggvars[(map[aggvar] - 1 + sortpos) %
17556ff6d951SJohn Birrell 			    naggvars] == aggvars[ndx]);
17566ff6d951SJohn Birrell 
17576ff6d951SJohn Birrell 			remap[i] = map[aggvar];
17586ff6d951SJohn Birrell 			continue;
17596ff6d951SJohn Birrell 		}
17606ff6d951SJohn Birrell 
17616ff6d951SJohn Birrell 		map[aggvar] = i + 1;
17626ff6d951SJohn Birrell 	}
17636ff6d951SJohn Birrell 
17646ff6d951SJohn Birrell 	/*
17656ff6d951SJohn Birrell 	 * We need to take two passes over the data to size our allocation, so
17666ff6d951SJohn Birrell 	 * we'll use the first pass to also fill in the zero-filled data to be
17676ff6d951SJohn Birrell 	 * used to properly format a zero-valued aggregation.
17686ff6d951SJohn Birrell 	 */
17696ff6d951SJohn Birrell 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
17706ff6d951SJohn Birrell 		dtrace_aggvarid_t id;
17716ff6d951SJohn Birrell 		int ndx;
17726ff6d951SJohn Birrell 
17736ff6d951SJohn Birrell 		if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
17746ff6d951SJohn Birrell 			continue;
17756ff6d951SJohn Birrell 
17766ff6d951SJohn Birrell 		if (zaggdata[ndx - 1].dtahe_size == 0) {
17776ff6d951SJohn Birrell 			zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
17786ff6d951SJohn Birrell 			zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
17796ff6d951SJohn Birrell 		}
17806ff6d951SJohn Birrell 
17816ff6d951SJohn Birrell 		nentries++;
17826ff6d951SJohn Birrell 	}
17836ff6d951SJohn Birrell 
17846ff6d951SJohn Birrell 	if (nentries == 0) {
17856ff6d951SJohn Birrell 		/*
17866ff6d951SJohn Birrell 		 * We couldn't find any entries; there is nothing else to do.
17876ff6d951SJohn Birrell 		 */
17886ff6d951SJohn Birrell 		rval = 0;
17896ff6d951SJohn Birrell 		goto out;
17906ff6d951SJohn Birrell 	}
17916ff6d951SJohn Birrell 
17926ff6d951SJohn Birrell 	/*
17936ff6d951SJohn Birrell 	 * Before we sort the data, we're going to look for any holes in our
17946ff6d951SJohn Birrell 	 * zero-filled data.  This will occur if an aggregation variable that
17956ff6d951SJohn Birrell 	 * we are being asked to print has not yet been assigned the result of
17966ff6d951SJohn Birrell 	 * any aggregating action for _any_ tuple.  The issue becomes that we
17976ff6d951SJohn Birrell 	 * would like a zero value to be printed for all columns for this
17986ff6d951SJohn Birrell 	 * aggregation, but without any record description, we don't know the
17996ff6d951SJohn Birrell 	 * aggregating action that corresponds to the aggregation variable.  To
18006ff6d951SJohn Birrell 	 * try to find a match, we're simply going to lookup aggregation IDs
18016ff6d951SJohn Birrell 	 * (which are guaranteed to be contiguous and to start from 1), looking
18026ff6d951SJohn Birrell 	 * for the specified aggregation variable ID.  If we find a match,
18036ff6d951SJohn Birrell 	 * we'll use that.  If we iterate over all aggregation IDs and don't
18046ff6d951SJohn Birrell 	 * find a match, then we must be an anonymous enabling.  (Anonymous
18056ff6d951SJohn Birrell 	 * enablings can't currently derive either aggregation variable IDs or
18066ff6d951SJohn Birrell 	 * aggregation variable names given only an aggregation ID.)  In this
18076ff6d951SJohn Birrell 	 * obscure case (anonymous enabling, multiple aggregation printa() with
18086ff6d951SJohn Birrell 	 * some aggregations not represented for any tuple), our defined
18096ff6d951SJohn Birrell 	 * behavior is that the zero will be printed in the format of the first
18106ff6d951SJohn Birrell 	 * aggregation variable that contains any non-zero value.
18116ff6d951SJohn Birrell 	 */
18126ff6d951SJohn Birrell 	for (i = 0; i < naggvars; i++) {
18136ff6d951SJohn Birrell 		if (zaggdata[i].dtahe_size == 0) {
18146ff6d951SJohn Birrell 			dtrace_aggvarid_t aggvar;
18156ff6d951SJohn Birrell 
18166ff6d951SJohn Birrell 			aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
18176ff6d951SJohn Birrell 			assert(zaggdata[i].dtahe_data.dtada_data == NULL);
18186ff6d951SJohn Birrell 
18196ff6d951SJohn Birrell 			for (j = DTRACE_AGGIDNONE + 1; ; j++) {
18206ff6d951SJohn Birrell 				dtrace_aggdesc_t *agg;
18216ff6d951SJohn Birrell 				dtrace_aggdata_t *aggdata;
18226ff6d951SJohn Birrell 
18236ff6d951SJohn Birrell 				if (dt_aggid_lookup(dtp, j, &agg) != 0)
18246ff6d951SJohn Birrell 					break;
18256ff6d951SJohn Birrell 
18266ff6d951SJohn Birrell 				if (agg->dtagd_varid != aggvar)
18276ff6d951SJohn Birrell 					continue;
18286ff6d951SJohn Birrell 
18296ff6d951SJohn Birrell 				/*
18306ff6d951SJohn Birrell 				 * We have our description -- now we need to
18316ff6d951SJohn Birrell 				 * cons up the zaggdata entry for it.
18326ff6d951SJohn Birrell 				 */
18336ff6d951SJohn Birrell 				aggdata = &zaggdata[i].dtahe_data;
18346ff6d951SJohn Birrell 				aggdata->dtada_size = agg->dtagd_size;
18356ff6d951SJohn Birrell 				aggdata->dtada_desc = agg;
18366ff6d951SJohn Birrell 				aggdata->dtada_handle = dtp;
18376ff6d951SJohn Birrell 				(void) dt_epid_lookup(dtp, agg->dtagd_epid,
18386ff6d951SJohn Birrell 				    &aggdata->dtada_edesc,
18396ff6d951SJohn Birrell 				    &aggdata->dtada_pdesc);
18406ff6d951SJohn Birrell 				aggdata->dtada_normal = 1;
18416ff6d951SJohn Birrell 				zaggdata[i].dtahe_hashval = 0;
18426ff6d951SJohn Birrell 				zaggdata[i].dtahe_size = agg->dtagd_size;
18436ff6d951SJohn Birrell 				break;
18446ff6d951SJohn Birrell 			}
18456ff6d951SJohn Birrell 
18466ff6d951SJohn Birrell 			if (zaggdata[i].dtahe_size == 0) {
18476ff6d951SJohn Birrell 				caddr_t data;
18486ff6d951SJohn Birrell 
18496ff6d951SJohn Birrell 				/*
18506ff6d951SJohn Birrell 				 * We couldn't find this aggregation, meaning
18516ff6d951SJohn Birrell 				 * that we have never seen it before for any
18526ff6d951SJohn Birrell 				 * tuple _and_ this is an anonymous enabling.
18536ff6d951SJohn Birrell 				 * That is, we're in the obscure case outlined
18546ff6d951SJohn Birrell 				 * above.  In this case, our defined behavior
18556ff6d951SJohn Birrell 				 * is to format the data in the format of the
18566ff6d951SJohn Birrell 				 * first non-zero aggregation -- of which, of
18576ff6d951SJohn Birrell 				 * course, we know there to be at least one
18586ff6d951SJohn Birrell 				 * (or nentries would have been zero).
18596ff6d951SJohn Birrell 				 */
18606ff6d951SJohn Birrell 				for (j = 0; j < naggvars; j++) {
18616ff6d951SJohn Birrell 					if (zaggdata[j].dtahe_size != 0)
18626ff6d951SJohn Birrell 						break;
18636ff6d951SJohn Birrell 				}
18646ff6d951SJohn Birrell 
18656ff6d951SJohn Birrell 				assert(j < naggvars);
18666ff6d951SJohn Birrell 				zaggdata[i] = zaggdata[j];
18676ff6d951SJohn Birrell 
18686ff6d951SJohn Birrell 				data = zaggdata[i].dtahe_data.dtada_data;
18696ff6d951SJohn Birrell 				assert(data != NULL);
18706ff6d951SJohn Birrell 			}
18716ff6d951SJohn Birrell 		}
18726ff6d951SJohn Birrell 	}
18736ff6d951SJohn Birrell 
18746ff6d951SJohn Birrell 	/*
18756ff6d951SJohn Birrell 	 * Now we need to allocate our zero-filled data for use for
18766ff6d951SJohn Birrell 	 * aggregations that don't have a value corresponding to a given key.
18776ff6d951SJohn Birrell 	 */
18786ff6d951SJohn Birrell 	for (i = 0; i < naggvars; i++) {
18796ff6d951SJohn Birrell 		dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
18806ff6d951SJohn Birrell 		dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
18816ff6d951SJohn Birrell 		dtrace_recdesc_t *rec;
18826ff6d951SJohn Birrell 		uint64_t larg;
18836ff6d951SJohn Birrell 		caddr_t zdata;
18846ff6d951SJohn Birrell 
18856ff6d951SJohn Birrell 		zsize = zaggdata[i].dtahe_size;
18866ff6d951SJohn Birrell 		assert(zsize != 0);
18876ff6d951SJohn Birrell 
18886ff6d951SJohn Birrell 		if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
18896ff6d951SJohn Birrell 			/*
18906ff6d951SJohn Birrell 			 * If we failed to allocated some zero-filled data, we
18916ff6d951SJohn Birrell 			 * need to zero out the remaining dtada_data pointers
18926ff6d951SJohn Birrell 			 * to prevent the wrong data from being freed below.
18936ff6d951SJohn Birrell 			 */
18946ff6d951SJohn Birrell 			for (j = i; j < naggvars; j++)
18956ff6d951SJohn Birrell 				zaggdata[j].dtahe_data.dtada_data = NULL;
18966ff6d951SJohn Birrell 			goto out;
18976ff6d951SJohn Birrell 		}
18986ff6d951SJohn Birrell 
18996ff6d951SJohn Birrell 		aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
19006ff6d951SJohn Birrell 
19016ff6d951SJohn Birrell 		/*
19026ff6d951SJohn Birrell 		 * First, the easy bit.  To maintain compatibility with
19036ff6d951SJohn Birrell 		 * consumers that pull the compiler-generated ID out of the
19046ff6d951SJohn Birrell 		 * data, we put that ID at the top of the zero-filled data.
19056ff6d951SJohn Birrell 		 */
19066ff6d951SJohn Birrell 		rec = &aggdesc->dtagd_rec[0];
19076ff6d951SJohn Birrell 		/* LINTED - alignment */
19086ff6d951SJohn Birrell 		*((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
19096ff6d951SJohn Birrell 
19106ff6d951SJohn Birrell 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
19116ff6d951SJohn Birrell 
19126ff6d951SJohn Birrell 		/*
19136ff6d951SJohn Birrell 		 * Now for the more complicated part.  If (and only if) this
19146ff6d951SJohn Birrell 		 * is an lquantize() aggregating action, zero-filled data is
19156ff6d951SJohn Birrell 		 * not equivalent to an empty record:  we must also get the
19166ff6d951SJohn Birrell 		 * parameters for the lquantize().
19176ff6d951SJohn Birrell 		 */
19186ff6d951SJohn Birrell 		if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
19196ff6d951SJohn Birrell 			if (aggdata->dtada_data != NULL) {
19206ff6d951SJohn Birrell 				/*
19216ff6d951SJohn Birrell 				 * The easier case here is if we actually have
19226ff6d951SJohn Birrell 				 * some prototype data -- in which case we
19236ff6d951SJohn Birrell 				 * manually dig it out of the aggregation
19246ff6d951SJohn Birrell 				 * record.
19256ff6d951SJohn Birrell 				 */
19266ff6d951SJohn Birrell 				/* LINTED - alignment */
19276ff6d951SJohn Birrell 				larg = *((uint64_t *)(aggdata->dtada_data +
19286ff6d951SJohn Birrell 				    rec->dtrd_offset));
19296ff6d951SJohn Birrell 			} else {
19306ff6d951SJohn Birrell 				/*
19316ff6d951SJohn Birrell 				 * We don't have any prototype data.  As a
19326ff6d951SJohn Birrell 				 * result, we know that we _do_ have the
19336ff6d951SJohn Birrell 				 * compiler-generated information.  (If this
19346ff6d951SJohn Birrell 				 * were an anonymous enabling, all of our
19356ff6d951SJohn Birrell 				 * zero-filled data would have prototype data
19366ff6d951SJohn Birrell 				 * -- either directly or indirectly.) So as
19376ff6d951SJohn Birrell 				 * gross as it is, we'll grovel around in the
19386ff6d951SJohn Birrell 				 * compiler-generated information to find the
19396ff6d951SJohn Birrell 				 * lquantize() parameters.
19406ff6d951SJohn Birrell 				 */
19416ff6d951SJohn Birrell 				dtrace_stmtdesc_t *sdp;
19426ff6d951SJohn Birrell 				dt_ident_t *aid;
19436ff6d951SJohn Birrell 				dt_idsig_t *isp;
19446ff6d951SJohn Birrell 
19456ff6d951SJohn Birrell 				sdp = (dtrace_stmtdesc_t *)(uintptr_t)
19466ff6d951SJohn Birrell 				    aggdesc->dtagd_rec[0].dtrd_uarg;
19476ff6d951SJohn Birrell 				aid = sdp->dtsd_aggdata;
19486ff6d951SJohn Birrell 				isp = (dt_idsig_t *)aid->di_data;
19496ff6d951SJohn Birrell 				assert(isp->dis_auxinfo != 0);
19506ff6d951SJohn Birrell 				larg = isp->dis_auxinfo;
19516ff6d951SJohn Birrell 			}
19526ff6d951SJohn Birrell 
19536ff6d951SJohn Birrell 			/* LINTED - alignment */
19546ff6d951SJohn Birrell 			*((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
19556ff6d951SJohn Birrell 		}
19566ff6d951SJohn Birrell 
19576ff6d951SJohn Birrell 		aggdata->dtada_data = zdata;
19586ff6d951SJohn Birrell 	}
19596ff6d951SJohn Birrell 
19606ff6d951SJohn Birrell 	/*
19616ff6d951SJohn Birrell 	 * Now that we've dealt with setting up our zero-filled data, we can
19626ff6d951SJohn Birrell 	 * allocate our sorted array, and take another pass over the data to
19636ff6d951SJohn Birrell 	 * fill it.
19646ff6d951SJohn Birrell 	 */
19656ff6d951SJohn Birrell 	sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
19666ff6d951SJohn Birrell 
19676ff6d951SJohn Birrell 	if (sorted == NULL)
19686ff6d951SJohn Birrell 		goto out;
19696ff6d951SJohn Birrell 
19706ff6d951SJohn Birrell 	for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
19716ff6d951SJohn Birrell 		dtrace_aggvarid_t id;
19726ff6d951SJohn Birrell 
19736ff6d951SJohn Birrell 		if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
19746ff6d951SJohn Birrell 			continue;
19756ff6d951SJohn Birrell 
19766ff6d951SJohn Birrell 		sorted[i++] = h;
19776ff6d951SJohn Birrell 	}
19786ff6d951SJohn Birrell 
19796ff6d951SJohn Birrell 	assert(i == nentries);
19806ff6d951SJohn Birrell 
19816ff6d951SJohn Birrell 	/*
19826ff6d951SJohn Birrell 	 * We've loaded our array; now we need to sort by value to allow us
19836ff6d951SJohn Birrell 	 * to create bundles of like value.  We're going to acquire the
19846ff6d951SJohn Birrell 	 * dt_qsort_lock here, and hold it across all of our subsequent
19856ff6d951SJohn Birrell 	 * comparison and sorting.
19866ff6d951SJohn Birrell 	 */
19876ff6d951SJohn Birrell 	(void) pthread_mutex_lock(&dt_qsort_lock);
19886ff6d951SJohn Birrell 
19896ff6d951SJohn Birrell 	qsort(sorted, nentries, sizeof (dt_ahashent_t *),
19906ff6d951SJohn Birrell 	    dt_aggregate_keyvarcmp);
19916ff6d951SJohn Birrell 
19926ff6d951SJohn Birrell 	/*
19936ff6d951SJohn Birrell 	 * Now we need to go through and create bundles.  Because the number
19946ff6d951SJohn Birrell 	 * of bundles is bounded by the size of the sorted array, we're going
19956ff6d951SJohn Birrell 	 * to reuse the underlying storage.  And note that "bundle" is an
19966ff6d951SJohn Birrell 	 * array of pointers to arrays of pointers to dt_ahashent_t -- making
19976ff6d951SJohn Birrell 	 * its type (regrettably) "dt_ahashent_t ***".  (Regrettable because
19986ff6d951SJohn Birrell 	 * '*' -- like '_' and 'X' -- should never appear in triplicate in
19996ff6d951SJohn Birrell 	 * an ideal world.)
20006ff6d951SJohn Birrell 	 */
20016ff6d951SJohn Birrell 	bundle = (dt_ahashent_t ***)sorted;
20026ff6d951SJohn Birrell 
20036ff6d951SJohn Birrell 	for (i = 1, start = 0; i <= nentries; i++) {
20046ff6d951SJohn Birrell 		if (i < nentries &&
20056ff6d951SJohn Birrell 		    dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
20066ff6d951SJohn Birrell 			continue;
20076ff6d951SJohn Birrell 
20086ff6d951SJohn Birrell 		/*
20096ff6d951SJohn Birrell 		 * We have a bundle boundary.  Everything from start to
20106ff6d951SJohn Birrell 		 * (i - 1) belongs in one bundle.
20116ff6d951SJohn Birrell 		 */
20126ff6d951SJohn Birrell 		assert(i - start <= naggvars);
20136ff6d951SJohn Birrell 		bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
20146ff6d951SJohn Birrell 
20156ff6d951SJohn Birrell 		if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
20166ff6d951SJohn Birrell 			(void) pthread_mutex_unlock(&dt_qsort_lock);
20176ff6d951SJohn Birrell 			goto out;
20186ff6d951SJohn Birrell 		}
20196ff6d951SJohn Birrell 
20206ff6d951SJohn Birrell 		for (j = start; j < i; j++) {
20216ff6d951SJohn Birrell 			dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
20226ff6d951SJohn Birrell 
20236ff6d951SJohn Birrell 			assert(id <= max);
20246ff6d951SJohn Birrell 			assert(map[id] != 0);
20256ff6d951SJohn Birrell 			assert(map[id] - 1 < naggvars);
20266ff6d951SJohn Birrell 			assert(nbundle[map[id] - 1] == NULL);
20276ff6d951SJohn Birrell 			nbundle[map[id] - 1] = sorted[j];
20286ff6d951SJohn Birrell 
20296ff6d951SJohn Birrell 			if (nbundle[naggvars] == NULL)
20306ff6d951SJohn Birrell 				nbundle[naggvars] = sorted[j];
20316ff6d951SJohn Birrell 		}
20326ff6d951SJohn Birrell 
20336ff6d951SJohn Birrell 		for (j = 0; j < naggvars; j++) {
20346ff6d951SJohn Birrell 			if (nbundle[j] != NULL)
20356ff6d951SJohn Birrell 				continue;
20366ff6d951SJohn Birrell 
20376ff6d951SJohn Birrell 			/*
20386ff6d951SJohn Birrell 			 * Before we assume that this aggregation variable
20396ff6d951SJohn Birrell 			 * isn't present (and fall back to using the
20406ff6d951SJohn Birrell 			 * zero-filled data allocated earlier), check the
20416ff6d951SJohn Birrell 			 * remap.  If we have a remapping, we'll drop it in
20426ff6d951SJohn Birrell 			 * here.  Note that we might be remapping an
20436ff6d951SJohn Birrell 			 * aggregation variable that isn't present for this
20446ff6d951SJohn Birrell 			 * key; in this case, the aggregation data that we
20456ff6d951SJohn Birrell 			 * copy will point to the zeroed data.
20466ff6d951SJohn Birrell 			 */
20476ff6d951SJohn Birrell 			if (remap != NULL && remap[j]) {
20486ff6d951SJohn Birrell 				assert(remap[j] - 1 < j);
20496ff6d951SJohn Birrell 				assert(nbundle[remap[j] - 1] != NULL);
20506ff6d951SJohn Birrell 				nbundle[j] = nbundle[remap[j] - 1];
20516ff6d951SJohn Birrell 			} else {
20526ff6d951SJohn Birrell 				nbundle[j] = &zaggdata[j];
20536ff6d951SJohn Birrell 			}
20546ff6d951SJohn Birrell 		}
20556ff6d951SJohn Birrell 
20566ff6d951SJohn Birrell 		bundle[nbundles++] = nbundle;
20576ff6d951SJohn Birrell 		start = i;
20586ff6d951SJohn Birrell 	}
20596ff6d951SJohn Birrell 
20606ff6d951SJohn Birrell 	/*
20616ff6d951SJohn Birrell 	 * Now we need to re-sort based on the first value.
20626ff6d951SJohn Birrell 	 */
20636ff6d951SJohn Birrell 	dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
20646ff6d951SJohn Birrell 	    dt_aggregate_bundlecmp);
20656ff6d951SJohn Birrell 
20666ff6d951SJohn Birrell 	(void) pthread_mutex_unlock(&dt_qsort_lock);
20676ff6d951SJohn Birrell 
20686ff6d951SJohn Birrell 	/*
20696ff6d951SJohn Birrell 	 * We're done!  Now we just need to go back over the sorted bundles,
20706ff6d951SJohn Birrell 	 * calling the function.
20716ff6d951SJohn Birrell 	 */
20726ff6d951SJohn Birrell 	data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
20736ff6d951SJohn Birrell 
20746ff6d951SJohn Birrell 	for (i = 0; i < nbundles; i++) {
20756ff6d951SJohn Birrell 		for (j = 0; j < naggvars; j++)
20766ff6d951SJohn Birrell 			data[j + 1] = NULL;
20776ff6d951SJohn Birrell 
20786ff6d951SJohn Birrell 		for (j = 0; j < naggvars; j++) {
20796ff6d951SJohn Birrell 			int ndx = j - sortpos;
20806ff6d951SJohn Birrell 
20816ff6d951SJohn Birrell 			if (ndx < 0)
20826ff6d951SJohn Birrell 				ndx += naggvars;
20836ff6d951SJohn Birrell 
20846ff6d951SJohn Birrell 			assert(bundle[i][ndx] != NULL);
20856ff6d951SJohn Birrell 			data[j + 1] = &bundle[i][ndx]->dtahe_data;
20866ff6d951SJohn Birrell 		}
20876ff6d951SJohn Birrell 
20886ff6d951SJohn Birrell 		for (j = 0; j < naggvars; j++)
20896ff6d951SJohn Birrell 			assert(data[j + 1] != NULL);
20906ff6d951SJohn Birrell 
20916ff6d951SJohn Birrell 		/*
20926ff6d951SJohn Birrell 		 * The representative key is the last element in the bundle.
20936ff6d951SJohn Birrell 		 * Assert that we have one, and then set it to be the first
20946ff6d951SJohn Birrell 		 * element of data.
20956ff6d951SJohn Birrell 		 */
20966ff6d951SJohn Birrell 		assert(bundle[i][j] != NULL);
20976ff6d951SJohn Birrell 		data[0] = &bundle[i][j]->dtahe_data;
20986ff6d951SJohn Birrell 
20996ff6d951SJohn Birrell 		if ((rval = func(data, naggvars + 1, arg)) == -1)
21006ff6d951SJohn Birrell 			goto out;
21016ff6d951SJohn Birrell 	}
21026ff6d951SJohn Birrell 
21036ff6d951SJohn Birrell 	rval = 0;
21046ff6d951SJohn Birrell out:
21056ff6d951SJohn Birrell 	for (i = 0; i < nbundles; i++)
21066ff6d951SJohn Birrell 		dt_free(dtp, bundle[i]);
21076ff6d951SJohn Birrell 
21086ff6d951SJohn Birrell 	if (zaggdata != NULL) {
21096ff6d951SJohn Birrell 		for (i = 0; i < naggvars; i++)
21106ff6d951SJohn Birrell 			dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
21116ff6d951SJohn Birrell 	}
21126ff6d951SJohn Birrell 
21136ff6d951SJohn Birrell 	dt_free(dtp, zaggdata);
21146ff6d951SJohn Birrell 	dt_free(dtp, sorted);
21156ff6d951SJohn Birrell 	dt_free(dtp, remap);
21166ff6d951SJohn Birrell 	dt_free(dtp, map);
21176ff6d951SJohn Birrell 
21186ff6d951SJohn Birrell 	return (rval);
21196ff6d951SJohn Birrell }
21206ff6d951SJohn Birrell 
21216ff6d951SJohn Birrell int
21226ff6d951SJohn Birrell dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
21236ff6d951SJohn Birrell     dtrace_aggregate_walk_f *func)
21246ff6d951SJohn Birrell {
21256ff6d951SJohn Birrell 	dt_print_aggdata_t pd;
21266ff6d951SJohn Birrell 
2127a43f0be9SRui Paulo 	bzero(&pd, sizeof (pd));
2128a43f0be9SRui Paulo 
21296ff6d951SJohn Birrell 	pd.dtpa_dtp = dtp;
21306ff6d951SJohn Birrell 	pd.dtpa_fp = fp;
21316ff6d951SJohn Birrell 	pd.dtpa_allunprint = 1;
21326ff6d951SJohn Birrell 
21336ff6d951SJohn Birrell 	if (func == NULL)
21346ff6d951SJohn Birrell 		func = dtrace_aggregate_walk_sorted;
21356ff6d951SJohn Birrell 
213693f27766SDomagoj Stolfa 	if (dtp->dt_oformat) {
213793f27766SDomagoj Stolfa 		if ((*func)(dtp, dt_format_agg, &pd) == -1)
213893f27766SDomagoj Stolfa 			return (dt_set_errno(dtp, dtp->dt_errno));
213993f27766SDomagoj Stolfa 	} else {
21406ff6d951SJohn Birrell 		if ((*func)(dtp, dt_print_agg, &pd) == -1)
21416ff6d951SJohn Birrell 			return (dt_set_errno(dtp, dtp->dt_errno));
214293f27766SDomagoj Stolfa 	}
21436ff6d951SJohn Birrell 
21446ff6d951SJohn Birrell 	return (0);
21456ff6d951SJohn Birrell }
21466ff6d951SJohn Birrell 
21476ff6d951SJohn Birrell void
21486ff6d951SJohn Birrell dtrace_aggregate_clear(dtrace_hdl_t *dtp)
21496ff6d951SJohn Birrell {
21506ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
21516ff6d951SJohn Birrell 	dt_ahash_t *hash = &agp->dtat_hash;
21526ff6d951SJohn Birrell 	dt_ahashent_t *h;
21536ff6d951SJohn Birrell 	dtrace_aggdata_t *data;
21546ff6d951SJohn Birrell 	dtrace_aggdesc_t *aggdesc;
21556ff6d951SJohn Birrell 	dtrace_recdesc_t *rec;
21566ff6d951SJohn Birrell 	int i, max_cpus = agp->dtat_maxcpu;
21576ff6d951SJohn Birrell 
21586ff6d951SJohn Birrell 	for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
21596ff6d951SJohn Birrell 		aggdesc = h->dtahe_data.dtada_desc;
21606ff6d951SJohn Birrell 		rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
21616ff6d951SJohn Birrell 		data = &h->dtahe_data;
21626ff6d951SJohn Birrell 
21636ff6d951SJohn Birrell 		bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
21646ff6d951SJohn Birrell 
21656ff6d951SJohn Birrell 		if (data->dtada_percpu == NULL)
21666ff6d951SJohn Birrell 			continue;
21676ff6d951SJohn Birrell 
21686ff6d951SJohn Birrell 		for (i = 0; i < max_cpus; i++)
21696ff6d951SJohn Birrell 			bzero(data->dtada_percpu[i], rec->dtrd_size);
21706ff6d951SJohn Birrell 	}
21716ff6d951SJohn Birrell }
21726ff6d951SJohn Birrell 
21736ff6d951SJohn Birrell void
21746ff6d951SJohn Birrell dt_aggregate_destroy(dtrace_hdl_t *dtp)
21756ff6d951SJohn Birrell {
21766ff6d951SJohn Birrell 	dt_aggregate_t *agp = &dtp->dt_aggregate;
21776ff6d951SJohn Birrell 	dt_ahash_t *hash = &agp->dtat_hash;
21786ff6d951SJohn Birrell 	dt_ahashent_t *h, *next;
21796ff6d951SJohn Birrell 	dtrace_aggdata_t *aggdata;
21806ff6d951SJohn Birrell 	int i, max_cpus = agp->dtat_maxcpu;
21816ff6d951SJohn Birrell 
21826ff6d951SJohn Birrell 	if (hash->dtah_hash == NULL) {
21836ff6d951SJohn Birrell 		assert(hash->dtah_all == NULL);
21846ff6d951SJohn Birrell 	} else {
21856ff6d951SJohn Birrell 		free(hash->dtah_hash);
21866ff6d951SJohn Birrell 
21876ff6d951SJohn Birrell 		for (h = hash->dtah_all; h != NULL; h = next) {
21886ff6d951SJohn Birrell 			next = h->dtahe_nextall;
21896ff6d951SJohn Birrell 
21906ff6d951SJohn Birrell 			aggdata = &h->dtahe_data;
21916ff6d951SJohn Birrell 
21926ff6d951SJohn Birrell 			if (aggdata->dtada_percpu != NULL) {
21936ff6d951SJohn Birrell 				for (i = 0; i < max_cpus; i++)
21946ff6d951SJohn Birrell 					free(aggdata->dtada_percpu[i]);
21956ff6d951SJohn Birrell 				free(aggdata->dtada_percpu);
21966ff6d951SJohn Birrell 			}
21976ff6d951SJohn Birrell 
21986ff6d951SJohn Birrell 			free(aggdata->dtada_data);
21996ff6d951SJohn Birrell 			free(h);
22006ff6d951SJohn Birrell 		}
22016ff6d951SJohn Birrell 
22026ff6d951SJohn Birrell 		hash->dtah_hash = NULL;
22036ff6d951SJohn Birrell 		hash->dtah_all = NULL;
22046ff6d951SJohn Birrell 		hash->dtah_size = 0;
22056ff6d951SJohn Birrell 	}
22066ff6d951SJohn Birrell 
22076ff6d951SJohn Birrell 	free(agp->dtat_buf.dtbd_data);
22086ff6d951SJohn Birrell 	free(agp->dtat_cpus);
22096ff6d951SJohn Birrell }
2210