4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <sys/sysctl.h>
39 #include <libproc_compat.h>
43 #define DTRACE_AHASHSIZE 32779 /* big 'ol prime */
46 * Because qsort(3C) does not allow an argument to be passed to a comparison
47 * function, the variables that affect comparison must regrettably be global;
48 * they are protected by a global static lock, dt_qsort_lock.
50 static pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
52 static int dt_revsort;
53 static int dt_keysort;
56 #define DT_LESSTHAN (dt_revsort == 0 ? -1 : 1)
57 #define DT_GREATERTHAN (dt_revsort == 0 ? 1 : -1)
60 dt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
64 for (i = 0; i < size / sizeof (int64_t); i++)
65 existing[i] = existing[i] + new[i];
69 dt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
78 return (DT_GREATERTHAN);
85 dt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
93 dt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
100 dt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
102 int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
103 int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
106 return (DT_LESSTHAN);
109 return (DT_GREATERTHAN);
115 dt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
117 uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
118 uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
121 return (DT_LESSTHAN);
124 return (DT_GREATERTHAN);
131 dt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
133 int64_t arg = *existing++;
134 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
137 for (i = 0; i <= levels + 1; i++)
138 existing[i] = existing[i] + new[i + 1];
142 dt_aggregate_lquantizedsum(int64_t *lquanta)
144 int64_t arg = *lquanta++;
145 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
146 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
147 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
148 long double total = (long double)lquanta[0] * (long double)(base - 1);
150 for (i = 0; i < levels; base += step, i++)
151 total += (long double)lquanta[i + 1] * (long double)base;
153 return (total + (long double)lquanta[levels + 1] *
154 (long double)(base + 1));
158 dt_aggregate_lquantizedzero(int64_t *lquanta)
160 int64_t arg = *lquanta++;
161 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
162 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
163 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
168 for (i = 0; i < levels; base += step, i++) {
172 return (lquanta[i + 1]);
176 return (lquanta[levels + 1]);
182 dt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
184 long double lsum = dt_aggregate_lquantizedsum(lhs);
185 long double rsum = dt_aggregate_lquantizedsum(rhs);
186 int64_t lzero, rzero;
189 return (DT_LESSTHAN);
192 return (DT_GREATERTHAN);
195 * If they're both equal, then we will compare based on the weights at
196 * zero. If the weights at zero are equal (or if zero is not within
197 * the range of the linear quantization), then this will be judged a
198 * tie and will be resolved based on the key comparison.
200 lzero = dt_aggregate_lquantizedzero(lhs);
201 rzero = dt_aggregate_lquantizedzero(rhs);
204 return (DT_LESSTHAN);
207 return (DT_GREATERTHAN);
213 dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
215 int nbuckets = DTRACE_QUANTIZE_NBUCKETS;
216 long double ltotal = 0, rtotal = 0;
217 int64_t lzero, rzero;
220 for (i = 0; i < nbuckets; i++) {
221 int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
223 if (bucketval == 0) {
228 ltotal += (long double)bucketval * (long double)lhs[i];
229 rtotal += (long double)bucketval * (long double)rhs[i];
233 return (DT_LESSTHAN);
236 return (DT_GREATERTHAN);
239 * If they're both equal, then we will compare based on the weights at
240 * zero. If the weights at zero are equal, then this will be judged a
241 * tie and will be resolved based on the key comparison.
244 return (DT_LESSTHAN);
247 return (DT_GREATERTHAN);
253 dt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
255 uint64_t pid = data[0];
256 uint64_t *pc = &data[1];
257 struct ps_prochandle *P;
260 if (dtp->dt_vector != NULL)
263 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
266 dt_proc_lock(dtp, P);
268 if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
271 dt_proc_unlock(dtp, P);
272 dt_proc_release(dtp, P);
276 dt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
278 uint64_t pid = data[0];
279 uint64_t *pc = &data[1];
280 struct ps_prochandle *P;
283 if (dtp->dt_vector != NULL)
286 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
289 dt_proc_lock(dtp, P);
291 if ((map = Paddr_to_map(P, *pc)) != NULL)
294 dt_proc_unlock(dtp, P);
295 dt_proc_release(dtp, P);
299 dt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
304 if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
309 dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
314 if (dtp->dt_vector != NULL) {
316 * We don't have a way of just getting the module for a
317 * vectored open, and it doesn't seem to be worth defining
318 * one. This means that use of mod() won't get true
319 * aggregation in the postmortem case (some modules may
320 * appear more than once in aggregation output). It seems
321 * unlikely that anyone will ever notice or care...
326 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
327 dmp = dt_list_next(dmp)) {
328 if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
329 *pc = dmp->dm_text_va;
335 static dtrace_aggvarid_t
336 dt_aggregate_aggvarid(dt_ahashent_t *ent)
338 dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
339 caddr_t data = ent->dtahe_data.dtada_data;
340 dtrace_recdesc_t *rec = agg->dtagd_rec;
343 * First, we'll check the variable ID in the aggdesc. If it's valid,
344 * we'll return it. If not, we'll use the compiler-generated ID
345 * present as the first record.
347 if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
348 return (agg->dtagd_varid);
350 agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
353 return (agg->dtagd_varid);
358 dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
362 size_t offs, roffs, size, ndx;
365 dtrace_recdesc_t *rec;
366 dt_aggregate_t *agp = &dtp->dt_aggregate;
367 dtrace_aggdesc_t *agg;
368 dt_ahash_t *hash = &agp->dtat_hash;
370 dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
371 dtrace_aggdata_t *aggdata;
372 int flags = agp->dtat_flags;
377 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
379 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, &buf) == -1) {
381 if (errno == ENOENT) {
383 * If that failed with ENOENT, it may be because the
384 * CPU was unconfigured. This is okay; we'll just
385 * do nothing but return success.
390 return (dt_set_errno(dtp, errno));
393 if (buf->dtbd_drops != 0) {
394 if (dt_handle_cpudrop(dtp, cpu,
395 DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1)
399 if (buf->dtbd_size == 0)
402 if (hash->dtah_hash == NULL) {
405 hash->dtah_size = DTRACE_AHASHSIZE;
406 size = hash->dtah_size * sizeof (dt_ahashent_t *);
408 if ((hash->dtah_hash = malloc(size)) == NULL)
409 return (dt_set_errno(dtp, EDT_NOMEM));
411 bzero(hash->dtah_hash, size);
414 for (offs = 0; offs < buf->dtbd_size; ) {
416 * We're guaranteed to have an ID.
418 id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
421 if (id == DTRACE_AGGIDNONE) {
423 * This is filler to assure proper alignment of the
424 * next record; we simply ignore it.
430 if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
433 addr = buf->dtbd_data + offs;
434 size = agg->dtagd_size;
437 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
438 rec = &agg->dtagd_rec[j];
439 roffs = rec->dtrd_offset;
441 switch (rec->dtrd_action) {
443 dt_aggregate_usym(dtp,
444 /* LINTED - alignment */
445 (uint64_t *)&addr[roffs]);
449 dt_aggregate_umod(dtp,
450 /* LINTED - alignment */
451 (uint64_t *)&addr[roffs]);
455 /* LINTED - alignment */
456 dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
460 /* LINTED - alignment */
461 dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
468 for (i = 0; i < rec->dtrd_size; i++)
469 hashval += addr[roffs + i];
472 ndx = hashval % hash->dtah_size;
474 for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
475 if (h->dtahe_hashval != hashval)
478 if (h->dtahe_size != size)
481 aggdata = &h->dtahe_data;
482 data = aggdata->dtada_data;
484 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
485 rec = &agg->dtagd_rec[j];
486 roffs = rec->dtrd_offset;
488 for (i = 0; i < rec->dtrd_size; i++)
489 if (addr[roffs + i] != data[roffs + i])
494 * We found it. Now we need to apply the aggregating
495 * action on the data here.
497 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
498 roffs = rec->dtrd_offset;
499 /* LINTED - alignment */
500 h->dtahe_aggregate((int64_t *)&data[roffs],
501 /* LINTED - alignment */
502 (int64_t *)&addr[roffs], rec->dtrd_size);
505 * If we're keeping per CPU data, apply the aggregating
506 * action there as well.
508 if (aggdata->dtada_percpu != NULL) {
509 data = aggdata->dtada_percpu[cpu];
511 /* LINTED - alignment */
512 h->dtahe_aggregate((int64_t *)data,
513 /* LINTED - alignment */
514 (int64_t *)&addr[roffs], rec->dtrd_size);
523 * If we're here, we couldn't find an entry for this record.
525 if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
526 return (dt_set_errno(dtp, EDT_NOMEM));
527 bzero(h, sizeof (dt_ahashent_t));
528 aggdata = &h->dtahe_data;
530 if ((aggdata->dtada_data = malloc(size)) == NULL) {
532 return (dt_set_errno(dtp, EDT_NOMEM));
535 bcopy(addr, aggdata->dtada_data, size);
536 aggdata->dtada_size = size;
537 aggdata->dtada_desc = agg;
538 aggdata->dtada_handle = dtp;
539 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
540 &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
541 aggdata->dtada_normal = 1;
543 h->dtahe_hashval = hashval;
544 h->dtahe_size = size;
545 (void) dt_aggregate_aggvarid(h);
547 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
549 if (flags & DTRACE_A_PERCPU) {
550 int max_cpus = agp->dtat_maxcpu;
551 caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
553 if (percpu == NULL) {
554 free(aggdata->dtada_data);
556 return (dt_set_errno(dtp, EDT_NOMEM));
559 for (j = 0; j < max_cpus; j++) {
560 percpu[j] = malloc(rec->dtrd_size);
562 if (percpu[j] == NULL) {
566 free(aggdata->dtada_data);
568 return (dt_set_errno(dtp, EDT_NOMEM));
572 bcopy(&addr[rec->dtrd_offset],
573 percpu[j], rec->dtrd_size);
575 bzero(percpu[j], rec->dtrd_size);
579 aggdata->dtada_percpu = percpu;
582 switch (rec->dtrd_action) {
584 h->dtahe_aggregate = dt_aggregate_min;
588 h->dtahe_aggregate = dt_aggregate_max;
591 case DTRACEAGG_LQUANTIZE:
592 h->dtahe_aggregate = dt_aggregate_lquantize;
595 case DTRACEAGG_COUNT:
598 case DTRACEAGG_STDDEV:
599 case DTRACEAGG_QUANTIZE:
600 h->dtahe_aggregate = dt_aggregate_count;
604 return (dt_set_errno(dtp, EDT_BADAGG));
607 if (hash->dtah_hash[ndx] != NULL)
608 hash->dtah_hash[ndx]->dtahe_prev = h;
610 h->dtahe_next = hash->dtah_hash[ndx];
611 hash->dtah_hash[ndx] = h;
613 if (hash->dtah_all != NULL)
614 hash->dtah_all->dtahe_prevall = h;
616 h->dtahe_nextall = hash->dtah_all;
619 offs += agg->dtagd_size;
626 dtrace_aggregate_snap(dtrace_hdl_t *dtp)
629 dt_aggregate_t *agp = &dtp->dt_aggregate;
630 hrtime_t now = gethrtime();
631 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
633 if (dtp->dt_lastagg != 0) {
634 if (now - dtp->dt_lastagg < interval)
637 dtp->dt_lastagg += interval;
639 dtp->dt_lastagg = now;
643 return (dt_set_errno(dtp, EINVAL));
645 if (agp->dtat_buf.dtbd_size == 0)
648 for (i = 0; i < agp->dtat_ncpus; i++) {
649 if ((rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i])))
657 dt_aggregate_hashcmp(const void *lhs, const void *rhs)
659 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
660 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
661 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
662 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
664 if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
665 return (DT_LESSTHAN);
667 if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
668 return (DT_GREATERTHAN);
674 dt_aggregate_varcmp(const void *lhs, const void *rhs)
676 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
677 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
678 dtrace_aggvarid_t lid, rid;
680 lid = dt_aggregate_aggvarid(lh);
681 rid = dt_aggregate_aggvarid(rh);
684 return (DT_LESSTHAN);
687 return (DT_GREATERTHAN);
693 dt_aggregate_keycmp(const void *lhs, const void *rhs)
695 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
696 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
697 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
698 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
699 dtrace_recdesc_t *lrec, *rrec;
701 int rval, i, j, keypos, nrecs;
703 if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
706 nrecs = lagg->dtagd_nrecs - 1;
707 assert(nrecs == ragg->dtagd_nrecs - 1);
709 keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
711 for (i = 1; i < nrecs; i++) {
713 int ndx = i + keypos;
716 ndx = ndx - nrecs + 1;
718 lrec = &lagg->dtagd_rec[ndx];
719 rrec = &ragg->dtagd_rec[ndx];
721 ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
722 rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
724 if (lrec->dtrd_size < rrec->dtrd_size)
725 return (DT_LESSTHAN);
727 if (lrec->dtrd_size > rrec->dtrd_size)
728 return (DT_GREATERTHAN);
730 switch (lrec->dtrd_size) {
731 case sizeof (uint64_t):
732 /* LINTED - alignment */
733 lval = *((uint64_t *)ldata);
734 /* LINTED - alignment */
735 rval = *((uint64_t *)rdata);
738 case sizeof (uint32_t):
739 /* LINTED - alignment */
740 lval = *((uint32_t *)ldata);
741 /* LINTED - alignment */
742 rval = *((uint32_t *)rdata);
745 case sizeof (uint16_t):
746 /* LINTED - alignment */
747 lval = *((uint16_t *)ldata);
748 /* LINTED - alignment */
749 rval = *((uint16_t *)rdata);
752 case sizeof (uint8_t):
753 lval = *((uint8_t *)ldata);
754 rval = *((uint8_t *)rdata);
758 switch (lrec->dtrd_action) {
760 case DTRACEACT_UADDR:
762 for (j = 0; j < 2; j++) {
763 /* LINTED - alignment */
764 lval = ((uint64_t *)ldata)[j];
765 /* LINTED - alignment */
766 rval = ((uint64_t *)rdata)[j];
769 return (DT_LESSTHAN);
772 return (DT_GREATERTHAN);
778 for (j = 0; j < lrec->dtrd_size; j++) {
779 lval = ((uint8_t *)ldata)[j];
780 rval = ((uint8_t *)rdata)[j];
783 return (DT_LESSTHAN);
786 return (DT_GREATERTHAN);
794 return (DT_LESSTHAN);
797 return (DT_GREATERTHAN);
804 dt_aggregate_valcmp(const void *lhs, const void *rhs)
806 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
807 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
808 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
809 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
810 caddr_t ldata = lh->dtahe_data.dtada_data;
811 caddr_t rdata = rh->dtahe_data.dtada_data;
812 dtrace_recdesc_t *lrec, *rrec;
813 int64_t *laddr, *raddr;
816 if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
819 if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
820 return (DT_GREATERTHAN);
822 if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
823 return (DT_LESSTHAN);
825 for (i = 0; i < lagg->dtagd_nrecs; i++) {
826 lrec = &lagg->dtagd_rec[i];
827 rrec = &ragg->dtagd_rec[i];
829 if (lrec->dtrd_offset < rrec->dtrd_offset)
830 return (DT_LESSTHAN);
832 if (lrec->dtrd_offset > rrec->dtrd_offset)
833 return (DT_GREATERTHAN);
835 if (lrec->dtrd_action < rrec->dtrd_action)
836 return (DT_LESSTHAN);
838 if (lrec->dtrd_action > rrec->dtrd_action)
839 return (DT_GREATERTHAN);
842 laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
843 raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
845 switch (lrec->dtrd_action) {
847 rval = dt_aggregate_averagecmp(laddr, raddr);
850 case DTRACEAGG_STDDEV:
851 rval = dt_aggregate_stddevcmp(laddr, raddr);
854 case DTRACEAGG_QUANTIZE:
855 rval = dt_aggregate_quantizedcmp(laddr, raddr);
858 case DTRACEAGG_LQUANTIZE:
859 rval = dt_aggregate_lquantizedcmp(laddr, raddr);
862 case DTRACEAGG_COUNT:
866 rval = dt_aggregate_countcmp(laddr, raddr);
877 dt_aggregate_valkeycmp(const void *lhs, const void *rhs)
881 if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
885 * If we're here, the values for the two aggregation elements are
886 * equal. We already know that the key layout is the same for the two
887 * elements; we must now compare the keys themselves as a tie-breaker.
889 return (dt_aggregate_keycmp(lhs, rhs));
893 dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
897 if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
900 return (dt_aggregate_varcmp(lhs, rhs));
904 dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
908 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
911 return (dt_aggregate_keycmp(lhs, rhs));
915 dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
919 if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
922 return (dt_aggregate_varcmp(lhs, rhs));
926 dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
930 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
933 return (dt_aggregate_valkeycmp(lhs, rhs));
937 dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
939 return (dt_aggregate_keyvarcmp(rhs, lhs));
943 dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
945 return (dt_aggregate_varkeycmp(rhs, lhs));
949 dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
951 return (dt_aggregate_valvarcmp(rhs, lhs));
955 dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
957 return (dt_aggregate_varvalcmp(rhs, lhs));
961 dt_aggregate_bundlecmp(const void *lhs, const void *rhs)
963 dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
964 dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
969 * If we're sorting on keys, we need to scan until we find the
970 * last entry -- that's the representative key. (The order of
971 * the bundle is values followed by key to accommodate the
972 * default behavior of sorting by value.) If the keys are
973 * equal, we'll fall into the value comparison loop, below.
975 for (i = 0; lh[i + 1] != NULL; i++)
979 assert(rh[i + 1] == NULL);
981 if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
986 if (lh[i + 1] == NULL) {
988 * All of the values are equal; if we're sorting on
989 * keys, then we're only here because the keys were
990 * found to be equal and these records are therefore
991 * equal. If we're not sorting on keys, we'll use the
992 * key comparison from the representative key as the
999 assert(rh[i + 1] == NULL);
1000 return (dt_aggregate_keycmp(&lh[i], &rh[i]));
1002 if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
1009 dt_aggregate_go(dtrace_hdl_t *dtp)
1011 dt_aggregate_t *agp = &dtp->dt_aggregate;
1012 dtrace_optval_t size, cpu;
1013 dtrace_bufdesc_t *buf = &agp->dtat_buf;
1016 assert(agp->dtat_maxcpu == 0);
1017 assert(agp->dtat_ncpu == 0);
1018 assert(agp->dtat_cpus == NULL);
1020 agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
1021 agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
1022 agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
1024 if (agp->dtat_cpus == NULL)
1025 return (dt_set_errno(dtp, EDT_NOMEM));
1028 * Use the aggregation buffer size as reloaded from the kernel.
1030 size = dtp->dt_options[DTRACEOPT_AGGSIZE];
1032 rval = dtrace_getopt(dtp, "aggsize", &size);
1035 if (size == 0 || size == DTRACEOPT_UNSET)
1038 buf = &agp->dtat_buf;
1039 buf->dtbd_size = size;
1041 if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
1042 return (dt_set_errno(dtp, EDT_NOMEM));
1045 * Now query for the CPUs enabled.
1047 rval = dtrace_getopt(dtp, "cpu", &cpu);
1048 assert(rval == 0 && cpu != DTRACEOPT_UNSET);
1050 if (cpu != DTRACE_CPUALL) {
1051 assert(cpu < agp->dtat_ncpu);
1052 agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
1057 agp->dtat_ncpus = 0;
1058 for (i = 0; i < agp->dtat_maxcpu; i++) {
1059 if (dt_status(dtp, i) == -1)
1062 agp->dtat_cpus[agp->dtat_ncpus++] = i;
1069 dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
1071 dt_aggregate_t *agp = &dtp->dt_aggregate;
1072 dtrace_aggdata_t *data;
1073 dtrace_aggdesc_t *aggdesc;
1074 dtrace_recdesc_t *rec;
1078 case DTRACE_AGGWALK_NEXT:
1081 case DTRACE_AGGWALK_CLEAR: {
1082 uint32_t size, offs = 0;
1084 aggdesc = h->dtahe_data.dtada_desc;
1085 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1086 size = rec->dtrd_size;
1087 data = &h->dtahe_data;
1089 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1090 offs = sizeof (uint64_t);
1091 size -= sizeof (uint64_t);
1094 bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
1096 if (data->dtada_percpu == NULL)
1099 for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
1100 bzero(data->dtada_percpu[i] + offs, size);
1104 case DTRACE_AGGWALK_ERROR:
1106 * We assume that errno is already set in this case.
1108 return (dt_set_errno(dtp, errno));
1110 case DTRACE_AGGWALK_ABORT:
1111 return (dt_set_errno(dtp, EDT_DIRABORT));
1113 case DTRACE_AGGWALK_DENORMALIZE:
1114 h->dtahe_data.dtada_normal = 1;
1117 case DTRACE_AGGWALK_NORMALIZE:
1118 if (h->dtahe_data.dtada_normal == 0) {
1119 h->dtahe_data.dtada_normal = 1;
1120 return (dt_set_errno(dtp, EDT_BADRVAL));
1125 case DTRACE_AGGWALK_REMOVE: {
1126 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1127 int max_cpus = agp->dtat_maxcpu;
1130 * First, remove this hash entry from its hash chain.
1132 if (h->dtahe_prev != NULL) {
1133 h->dtahe_prev->dtahe_next = h->dtahe_next;
1135 dt_ahash_t *hash = &agp->dtat_hash;
1136 size_t ndx = h->dtahe_hashval % hash->dtah_size;
1138 assert(hash->dtah_hash[ndx] == h);
1139 hash->dtah_hash[ndx] = h->dtahe_next;
1142 if (h->dtahe_next != NULL)
1143 h->dtahe_next->dtahe_prev = h->dtahe_prev;
1146 * Now remove it from the list of all hash entries.
1148 if (h->dtahe_prevall != NULL) {
1149 h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
1151 dt_ahash_t *hash = &agp->dtat_hash;
1153 assert(hash->dtah_all == h);
1154 hash->dtah_all = h->dtahe_nextall;
1157 if (h->dtahe_nextall != NULL)
1158 h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
1161 * We're unlinked. We can safely destroy the data.
1163 if (aggdata->dtada_percpu != NULL) {
1164 for (i = 0; i < max_cpus; i++)
1165 free(aggdata->dtada_percpu[i]);
1166 free(aggdata->dtada_percpu);
1169 free(aggdata->dtada_data);
1176 return (dt_set_errno(dtp, EDT_BADRVAL));
1183 dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
1184 int (*compar)(const void *, const void *))
1186 int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
1187 dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
1189 dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
1190 dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
1192 if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
1193 dt_keypos = (int)keyposopt;
1198 if (compar == NULL) {
1200 compar = dt_aggregate_varvalcmp;
1202 compar = dt_aggregate_varkeycmp;
1206 qsort(base, nel, width, compar);
1214 dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
1216 dt_ahashent_t *h, *next;
1217 dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
1219 for (h = hash->dtah_all; h != NULL; h = next) {
1221 * dt_aggwalk_rval() can potentially remove the current hash
1222 * entry; we need to load the next hash entry before calling
1225 next = h->dtahe_nextall;
1227 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1235 dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1236 dtrace_aggregate_f *func, void *arg,
1237 int (*sfunc)(const void *, const void *))
1239 dt_aggregate_t *agp = &dtp->dt_aggregate;
1240 dt_ahashent_t *h, **sorted;
1241 dt_ahash_t *hash = &agp->dtat_hash;
1242 size_t i, nentries = 0;
1244 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
1247 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1252 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
1255 (void) pthread_mutex_lock(&dt_qsort_lock);
1257 if (sfunc == NULL) {
1258 dt_aggregate_qsort(dtp, sorted, nentries,
1259 sizeof (dt_ahashent_t *), NULL);
1262 * If we've been explicitly passed a sorting function,
1263 * we'll use that -- ignoring the values of the "aggsortrev",
1264 * "aggsortkey" and "aggsortkeypos" options.
1266 qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
1269 (void) pthread_mutex_unlock(&dt_qsort_lock);
1271 for (i = 0; i < nentries; i++) {
1274 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1) {
1275 dt_free(dtp, sorted);
1280 dt_free(dtp, sorted);
1285 dtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1286 dtrace_aggregate_f *func, void *arg)
1288 return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
1292 dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
1293 dtrace_aggregate_f *func, void *arg)
1295 return (dt_aggregate_walk_sorted(dtp, func,
1296 arg, dt_aggregate_varkeycmp));
1300 dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
1301 dtrace_aggregate_f *func, void *arg)
1303 return (dt_aggregate_walk_sorted(dtp, func,
1304 arg, dt_aggregate_varvalcmp));
1308 dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
1309 dtrace_aggregate_f *func, void *arg)
1311 return (dt_aggregate_walk_sorted(dtp, func,
1312 arg, dt_aggregate_keyvarcmp));
1316 dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
1317 dtrace_aggregate_f *func, void *arg)
1319 return (dt_aggregate_walk_sorted(dtp, func,
1320 arg, dt_aggregate_valvarcmp));
1324 dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
1325 dtrace_aggregate_f *func, void *arg)
1327 return (dt_aggregate_walk_sorted(dtp, func,
1328 arg, dt_aggregate_varkeyrevcmp));
1332 dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
1333 dtrace_aggregate_f *func, void *arg)
1335 return (dt_aggregate_walk_sorted(dtp, func,
1336 arg, dt_aggregate_varvalrevcmp));
1340 dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
1341 dtrace_aggregate_f *func, void *arg)
1343 return (dt_aggregate_walk_sorted(dtp, func,
1344 arg, dt_aggregate_keyvarrevcmp));
1348 dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
1349 dtrace_aggregate_f *func, void *arg)
1351 return (dt_aggregate_walk_sorted(dtp, func,
1352 arg, dt_aggregate_valvarrevcmp));
1356 dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
1357 int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
1359 dt_aggregate_t *agp = &dtp->dt_aggregate;
1360 dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
1361 const dtrace_aggdata_t **data;
1362 dt_ahashent_t *zaggdata = NULL;
1363 dt_ahash_t *hash = &agp->dtat_hash;
1364 size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
1365 dtrace_aggvarid_t max = 0, aggvar;
1366 int rval = -1, *map, *remap = NULL;
1368 dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
1371 * If the sorting position is greater than the number of aggregation
1372 * variable IDs, we silently set it to 0.
1374 if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
1378 * First we need to translate the specified aggregation variable IDs
1379 * into a linear map that will allow us to translate an aggregation
1380 * variable ID into its position in the specified aggvars.
1382 for (i = 0; i < naggvars; i++) {
1383 if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
1384 return (dt_set_errno(dtp, EDT_BADAGGVAR));
1386 if (aggvars[i] > max)
1390 if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
1393 zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
1395 if (zaggdata == NULL)
1398 for (i = 0; i < naggvars; i++) {
1399 int ndx = i + sortpos;
1401 if (ndx >= naggvars)
1404 aggvar = aggvars[ndx];
1405 assert(aggvar <= max);
1409 * We have an aggregation variable that is present
1410 * more than once in the array of aggregation
1411 * variables. While it's unclear why one might want
1412 * to do this, it's legal. To support this construct,
1413 * we will allocate a remap that will indicate the
1414 * position from which this aggregation variable
1415 * should be pulled. (That is, where the remap will
1416 * map from one position to another.)
1418 if (remap == NULL) {
1419 remap = dt_zalloc(dtp, naggvars * sizeof (int));
1426 * Given that the variable is already present, assert
1427 * that following through the mapping and adjusting
1428 * for the sort position yields the same aggregation
1431 assert(aggvars[(map[aggvar] - 1 + sortpos) %
1432 naggvars] == aggvars[ndx]);
1434 remap[i] = map[aggvar];
1438 map[aggvar] = i + 1;
1442 * We need to take two passes over the data to size our allocation, so
1443 * we'll use the first pass to also fill in the zero-filled data to be
1444 * used to properly format a zero-valued aggregation.
1446 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1447 dtrace_aggvarid_t id;
1450 if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
1453 if (zaggdata[ndx - 1].dtahe_size == 0) {
1454 zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
1455 zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
1461 if (nentries == 0) {
1463 * We couldn't find any entries; there is nothing else to do.
1470 * Before we sort the data, we're going to look for any holes in our
1471 * zero-filled data. This will occur if an aggregation variable that
1472 * we are being asked to print has not yet been assigned the result of
1473 * any aggregating action for _any_ tuple. The issue becomes that we
1474 * would like a zero value to be printed for all columns for this
1475 * aggregation, but without any record description, we don't know the
1476 * aggregating action that corresponds to the aggregation variable. To
1477 * try to find a match, we're simply going to lookup aggregation IDs
1478 * (which are guaranteed to be contiguous and to start from 1), looking
1479 * for the specified aggregation variable ID. If we find a match,
1480 * we'll use that. If we iterate over all aggregation IDs and don't
1481 * find a match, then we must be an anonymous enabling. (Anonymous
1482 * enablings can't currently derive either aggregation variable IDs or
1483 * aggregation variable names given only an aggregation ID.) In this
1484 * obscure case (anonymous enabling, multiple aggregation printa() with
1485 * some aggregations not represented for any tuple), our defined
1486 * behavior is that the zero will be printed in the format of the first
1487 * aggregation variable that contains any non-zero value.
1489 for (i = 0; i < naggvars; i++) {
1490 if (zaggdata[i].dtahe_size == 0) {
1491 dtrace_aggvarid_t aggvar;
1493 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1494 assert(zaggdata[i].dtahe_data.dtada_data == NULL);
1496 for (j = DTRACE_AGGIDNONE + 1; ; j++) {
1497 dtrace_aggdesc_t *agg;
1498 dtrace_aggdata_t *aggdata;
1500 if (dt_aggid_lookup(dtp, j, &agg) != 0)
1503 if (agg->dtagd_varid != aggvar)
1507 * We have our description -- now we need to
1508 * cons up the zaggdata entry for it.
1510 aggdata = &zaggdata[i].dtahe_data;
1511 aggdata->dtada_size = agg->dtagd_size;
1512 aggdata->dtada_desc = agg;
1513 aggdata->dtada_handle = dtp;
1514 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
1515 &aggdata->dtada_edesc,
1516 &aggdata->dtada_pdesc);
1517 aggdata->dtada_normal = 1;
1518 zaggdata[i].dtahe_hashval = 0;
1519 zaggdata[i].dtahe_size = agg->dtagd_size;
1523 if (zaggdata[i].dtahe_size == 0) {
1527 * We couldn't find this aggregation, meaning
1528 * that we have never seen it before for any
1529 * tuple _and_ this is an anonymous enabling.
1530 * That is, we're in the obscure case outlined
1531 * above. In this case, our defined behavior
1532 * is to format the data in the format of the
1533 * first non-zero aggregation -- of which, of
1534 * course, we know there to be at least one
1535 * (or nentries would have been zero).
1537 for (j = 0; j < naggvars; j++) {
1538 if (zaggdata[j].dtahe_size != 0)
1542 assert(j < naggvars);
1543 zaggdata[i] = zaggdata[j];
1545 data = zaggdata[i].dtahe_data.dtada_data;
1546 assert(data != NULL);
1552 * Now we need to allocate our zero-filled data for use for
1553 * aggregations that don't have a value corresponding to a given key.
1555 for (i = 0; i < naggvars; i++) {
1556 dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
1557 dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
1558 dtrace_recdesc_t *rec;
1562 zsize = zaggdata[i].dtahe_size;
1565 if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
1567 * If we failed to allocated some zero-filled data, we
1568 * need to zero out the remaining dtada_data pointers
1569 * to prevent the wrong data from being freed below.
1571 for (j = i; j < naggvars; j++)
1572 zaggdata[j].dtahe_data.dtada_data = NULL;
1576 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1579 * First, the easy bit. To maintain compatibility with
1580 * consumers that pull the compiler-generated ID out of the
1581 * data, we put that ID at the top of the zero-filled data.
1583 rec = &aggdesc->dtagd_rec[0];
1584 /* LINTED - alignment */
1585 *((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
1587 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1590 * Now for the more complicated part. If (and only if) this
1591 * is an lquantize() aggregating action, zero-filled data is
1592 * not equivalent to an empty record: we must also get the
1593 * parameters for the lquantize().
1595 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1596 if (aggdata->dtada_data != NULL) {
1598 * The easier case here is if we actually have
1599 * some prototype data -- in which case we
1600 * manually dig it out of the aggregation
1603 /* LINTED - alignment */
1604 larg = *((uint64_t *)(aggdata->dtada_data +
1608 * We don't have any prototype data. As a
1609 * result, we know that we _do_ have the
1610 * compiler-generated information. (If this
1611 * were an anonymous enabling, all of our
1612 * zero-filled data would have prototype data
1613 * -- either directly or indirectly.) So as
1614 * gross as it is, we'll grovel around in the
1615 * compiler-generated information to find the
1616 * lquantize() parameters.
1618 dtrace_stmtdesc_t *sdp;
1622 sdp = (dtrace_stmtdesc_t *)(uintptr_t)
1623 aggdesc->dtagd_rec[0].dtrd_uarg;
1624 aid = sdp->dtsd_aggdata;
1625 isp = (dt_idsig_t *)aid->di_data;
1626 assert(isp->dis_auxinfo != 0);
1627 larg = isp->dis_auxinfo;
1630 /* LINTED - alignment */
1631 *((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
1634 aggdata->dtada_data = zdata;
1638 * Now that we've dealt with setting up our zero-filled data, we can
1639 * allocate our sorted array, and take another pass over the data to
1642 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1647 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
1648 dtrace_aggvarid_t id;
1650 if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
1656 assert(i == nentries);
1659 * We've loaded our array; now we need to sort by value to allow us
1660 * to create bundles of like value. We're going to acquire the
1661 * dt_qsort_lock here, and hold it across all of our subsequent
1662 * comparison and sorting.
1664 (void) pthread_mutex_lock(&dt_qsort_lock);
1666 qsort(sorted, nentries, sizeof (dt_ahashent_t *),
1667 dt_aggregate_keyvarcmp);
1670 * Now we need to go through and create bundles. Because the number
1671 * of bundles is bounded by the size of the sorted array, we're going
1672 * to reuse the underlying storage. And note that "bundle" is an
1673 * array of pointers to arrays of pointers to dt_ahashent_t -- making
1674 * its type (regrettably) "dt_ahashent_t ***". (Regrettable because
1675 * '*' -- like '_' and 'X' -- should never appear in triplicate in
1678 bundle = (dt_ahashent_t ***)sorted;
1680 for (i = 1, start = 0; i <= nentries; i++) {
1682 dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
1686 * We have a bundle boundary. Everything from start to
1687 * (i - 1) belongs in one bundle.
1689 assert(i - start <= naggvars);
1690 bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
1692 if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
1693 (void) pthread_mutex_unlock(&dt_qsort_lock);
1697 for (j = start; j < i; j++) {
1698 dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
1701 assert(map[id] != 0);
1702 assert(map[id] - 1 < naggvars);
1703 assert(nbundle[map[id] - 1] == NULL);
1704 nbundle[map[id] - 1] = sorted[j];
1706 if (nbundle[naggvars] == NULL)
1707 nbundle[naggvars] = sorted[j];
1710 for (j = 0; j < naggvars; j++) {
1711 if (nbundle[j] != NULL)
1715 * Before we assume that this aggregation variable
1716 * isn't present (and fall back to using the
1717 * zero-filled data allocated earlier), check the
1718 * remap. If we have a remapping, we'll drop it in
1719 * here. Note that we might be remapping an
1720 * aggregation variable that isn't present for this
1721 * key; in this case, the aggregation data that we
1722 * copy will point to the zeroed data.
1724 if (remap != NULL && remap[j]) {
1725 assert(remap[j] - 1 < j);
1726 assert(nbundle[remap[j] - 1] != NULL);
1727 nbundle[j] = nbundle[remap[j] - 1];
1729 nbundle[j] = &zaggdata[j];
1733 bundle[nbundles++] = nbundle;
1738 * Now we need to re-sort based on the first value.
1740 dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
1741 dt_aggregate_bundlecmp);
1743 (void) pthread_mutex_unlock(&dt_qsort_lock);
1746 * We're done! Now we just need to go back over the sorted bundles,
1747 * calling the function.
1749 data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
1751 for (i = 0; i < nbundles; i++) {
1752 for (j = 0; j < naggvars; j++)
1755 for (j = 0; j < naggvars; j++) {
1756 int ndx = j - sortpos;
1761 assert(bundle[i][ndx] != NULL);
1762 data[j + 1] = &bundle[i][ndx]->dtahe_data;
1765 for (j = 0; j < naggvars; j++)
1766 assert(data[j + 1] != NULL);
1769 * The representative key is the last element in the bundle.
1770 * Assert that we have one, and then set it to be the first
1773 assert(bundle[i][j] != NULL);
1774 data[0] = &bundle[i][j]->dtahe_data;
1776 if ((rval = func(data, naggvars + 1, arg)) == -1)
1782 for (i = 0; i < nbundles; i++)
1783 dt_free(dtp, bundle[i]);
1785 if (zaggdata != NULL) {
1786 for (i = 0; i < naggvars; i++)
1787 dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
1790 dt_free(dtp, zaggdata);
1791 dt_free(dtp, sorted);
1792 dt_free(dtp, remap);
1799 dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
1800 dtrace_aggregate_walk_f *func)
1802 dt_print_aggdata_t pd;
1806 pd.dtpa_allunprint = 1;
1809 func = dtrace_aggregate_walk_sorted;
1811 if ((*func)(dtp, dt_print_agg, &pd) == -1)
1812 return (dt_set_errno(dtp, dtp->dt_errno));
1818 dtrace_aggregate_clear(dtrace_hdl_t *dtp)
1820 dt_aggregate_t *agp = &dtp->dt_aggregate;
1821 dt_ahash_t *hash = &agp->dtat_hash;
1823 dtrace_aggdata_t *data;
1824 dtrace_aggdesc_t *aggdesc;
1825 dtrace_recdesc_t *rec;
1826 int i, max_cpus = agp->dtat_maxcpu;
1828 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1829 aggdesc = h->dtahe_data.dtada_desc;
1830 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1831 data = &h->dtahe_data;
1833 bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
1835 if (data->dtada_percpu == NULL)
1838 for (i = 0; i < max_cpus; i++)
1839 bzero(data->dtada_percpu[i], rec->dtrd_size);
1844 dt_aggregate_destroy(dtrace_hdl_t *dtp)
1846 dt_aggregate_t *agp = &dtp->dt_aggregate;
1847 dt_ahash_t *hash = &agp->dtat_hash;
1848 dt_ahashent_t *h, *next;
1849 dtrace_aggdata_t *aggdata;
1850 int i, max_cpus = agp->dtat_maxcpu;
1852 if (hash->dtah_hash == NULL) {
1853 assert(hash->dtah_all == NULL);
1855 free(hash->dtah_hash);
1857 for (h = hash->dtah_all; h != NULL; h = next) {
1858 next = h->dtahe_nextall;
1860 aggdata = &h->dtahe_data;
1862 if (aggdata->dtada_percpu != NULL) {
1863 for (i = 0; i < max_cpus; i++)
1864 free(aggdata->dtada_percpu[i]);
1865 free(aggdata->dtada_percpu);
1868 free(aggdata->dtada_data);
1872 hash->dtah_hash = NULL;
1873 hash->dtah_all = NULL;
1874 hash->dtah_size = 0;
1877 free(agp->dtat_buf.dtbd_data);
1878 free(agp->dtat_cpus);