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.
28 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
29 * Copyright (c) 2012 by Delphix. All rights reserved.
41 #include <sys/sysctl.h>
42 #include <libproc_compat.h>
46 #define DTRACE_AHASHSIZE 32779 /* big 'ol prime */
49 * Because qsort(3C) does not allow an argument to be passed to a comparison
50 * function, the variables that affect comparison must regrettably be global;
51 * they are protected by a global static lock, dt_qsort_lock.
53 static pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
55 static int dt_revsort;
56 static int dt_keysort;
59 #define DT_LESSTHAN (dt_revsort == 0 ? -1 : 1)
60 #define DT_GREATERTHAN (dt_revsort == 0 ? 1 : -1)
63 dt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
67 for (i = 0; i < size / sizeof (int64_t); i++)
68 existing[i] = existing[i] + new[i];
72 dt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
81 return (DT_GREATERTHAN);
88 dt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
96 dt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
103 dt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
105 int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
106 int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
109 return (DT_LESSTHAN);
112 return (DT_GREATERTHAN);
118 dt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
120 uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
121 uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
124 return (DT_LESSTHAN);
127 return (DT_GREATERTHAN);
134 dt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
136 int64_t arg = *existing++;
137 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
140 for (i = 0; i <= levels + 1; i++)
141 existing[i] = existing[i] + new[i + 1];
145 dt_aggregate_lquantizedsum(int64_t *lquanta)
147 int64_t arg = *lquanta++;
148 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
149 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
150 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
151 long double total = (long double)lquanta[0] * (long double)(base - 1);
153 for (i = 0; i < levels; base += step, i++)
154 total += (long double)lquanta[i + 1] * (long double)base;
156 return (total + (long double)lquanta[levels + 1] *
157 (long double)(base + 1));
161 dt_aggregate_lquantizedzero(int64_t *lquanta)
163 int64_t arg = *lquanta++;
164 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
165 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
166 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
171 for (i = 0; i < levels; base += step, i++) {
175 return (lquanta[i + 1]);
179 return (lquanta[levels + 1]);
185 dt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
187 long double lsum = dt_aggregate_lquantizedsum(lhs);
188 long double rsum = dt_aggregate_lquantizedsum(rhs);
189 int64_t lzero, rzero;
192 return (DT_LESSTHAN);
195 return (DT_GREATERTHAN);
198 * If they're both equal, then we will compare based on the weights at
199 * zero. If the weights at zero are equal (or if zero is not within
200 * the range of the linear quantization), then this will be judged a
201 * tie and will be resolved based on the key comparison.
203 lzero = dt_aggregate_lquantizedzero(lhs);
204 rzero = dt_aggregate_lquantizedzero(rhs);
207 return (DT_LESSTHAN);
210 return (DT_GREATERTHAN);
216 dt_aggregate_llquantize(int64_t *existing, int64_t *new, size_t size)
220 for (i = 1; i < size / sizeof (int64_t); i++)
221 existing[i] = existing[i] + new[i];
225 dt_aggregate_llquantizedsum(int64_t *llquanta)
227 int64_t arg = *llquanta++;
228 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
229 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
230 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
231 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
233 int64_t value = 1, next, step;
236 assert(nsteps >= factor);
237 assert(nsteps % factor == 0);
239 for (order = 0; order < low; order++)
242 total = (long double)llquanta[bin++] * (long double)(value - 1);
244 next = value * factor;
245 step = next > nsteps ? next / nsteps : 1;
247 while (order <= high) {
248 assert(value < next);
249 total += (long double)llquanta[bin++] * (long double)(value);
251 if ((value += step) != next)
254 next = value * factor;
255 step = next > nsteps ? next / nsteps : 1;
259 return (total + (long double)llquanta[bin] * (long double)value);
263 dt_aggregate_llquantizedcmp(int64_t *lhs, int64_t *rhs)
265 long double lsum = dt_aggregate_llquantizedsum(lhs);
266 long double rsum = dt_aggregate_llquantizedsum(rhs);
267 int64_t lzero, rzero;
270 return (DT_LESSTHAN);
273 return (DT_GREATERTHAN);
276 * If they're both equal, then we will compare based on the weights at
277 * zero. If the weights at zero are equal, then this will be judged a
278 * tie and will be resolved based on the key comparison.
284 return (DT_LESSTHAN);
287 return (DT_GREATERTHAN);
293 dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
295 int nbuckets = DTRACE_QUANTIZE_NBUCKETS;
296 long double ltotal = 0, rtotal = 0;
297 int64_t lzero, rzero;
300 for (i = 0; i < nbuckets; i++) {
301 int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
303 if (bucketval == 0) {
308 ltotal += (long double)bucketval * (long double)lhs[i];
309 rtotal += (long double)bucketval * (long double)rhs[i];
313 return (DT_LESSTHAN);
316 return (DT_GREATERTHAN);
319 * If they're both equal, then we will compare based on the weights at
320 * zero. If the weights at zero are equal, then this will be judged a
321 * tie and will be resolved based on the key comparison.
324 return (DT_LESSTHAN);
327 return (DT_GREATERTHAN);
333 dt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
335 uint64_t pid = data[0];
336 uint64_t *pc = &data[1];
337 struct ps_prochandle *P;
340 if (dtp->dt_vector != NULL)
343 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
346 dt_proc_lock(dtp, P);
348 if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
351 dt_proc_unlock(dtp, P);
352 dt_proc_release(dtp, P);
356 dt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
358 uint64_t pid = data[0];
359 uint64_t *pc = &data[1];
360 struct ps_prochandle *P;
363 if (dtp->dt_vector != NULL)
366 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
369 dt_proc_lock(dtp, P);
371 if ((map = Paddr_to_map(P, *pc)) != NULL)
374 dt_proc_unlock(dtp, P);
375 dt_proc_release(dtp, P);
379 dt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
384 if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
389 dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
394 if (dtp->dt_vector != NULL) {
396 * We don't have a way of just getting the module for a
397 * vectored open, and it doesn't seem to be worth defining
398 * one. This means that use of mod() won't get true
399 * aggregation in the postmortem case (some modules may
400 * appear more than once in aggregation output). It seems
401 * unlikely that anyone will ever notice or care...
406 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
407 dmp = dt_list_next(dmp)) {
408 if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
409 *pc = dmp->dm_text_va;
415 static dtrace_aggvarid_t
416 dt_aggregate_aggvarid(dt_ahashent_t *ent)
418 dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
419 caddr_t data = ent->dtahe_data.dtada_data;
420 dtrace_recdesc_t *rec = agg->dtagd_rec;
423 * First, we'll check the variable ID in the aggdesc. If it's valid,
424 * we'll return it. If not, we'll use the compiler-generated ID
425 * present as the first record.
427 if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
428 return (agg->dtagd_varid);
430 agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
433 return (agg->dtagd_varid);
438 dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
442 size_t offs, roffs, size, ndx;
445 dtrace_recdesc_t *rec;
446 dt_aggregate_t *agp = &dtp->dt_aggregate;
447 dtrace_aggdesc_t *agg;
448 dt_ahash_t *hash = &agp->dtat_hash;
450 dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
451 dtrace_aggdata_t *aggdata;
452 int flags = agp->dtat_flags;
457 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
459 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, &buf) == -1) {
461 if (errno == ENOENT) {
463 * If that failed with ENOENT, it may be because the
464 * CPU was unconfigured. This is okay; we'll just
465 * do nothing but return success.
470 return (dt_set_errno(dtp, errno));
473 if (buf->dtbd_drops != 0) {
474 if (dt_handle_cpudrop(dtp, cpu,
475 DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1)
479 if (buf->dtbd_size == 0)
482 if (hash->dtah_hash == NULL) {
485 hash->dtah_size = DTRACE_AHASHSIZE;
486 size = hash->dtah_size * sizeof (dt_ahashent_t *);
488 if ((hash->dtah_hash = malloc(size)) == NULL)
489 return (dt_set_errno(dtp, EDT_NOMEM));
491 bzero(hash->dtah_hash, size);
494 for (offs = 0; offs < buf->dtbd_size; ) {
496 * We're guaranteed to have an ID.
498 id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
501 if (id == DTRACE_AGGIDNONE) {
503 * This is filler to assure proper alignment of the
504 * next record; we simply ignore it.
510 if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
513 addr = buf->dtbd_data + offs;
514 size = agg->dtagd_size;
517 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
518 rec = &agg->dtagd_rec[j];
519 roffs = rec->dtrd_offset;
521 switch (rec->dtrd_action) {
523 dt_aggregate_usym(dtp,
524 /* LINTED - alignment */
525 (uint64_t *)&addr[roffs]);
529 dt_aggregate_umod(dtp,
530 /* LINTED - alignment */
531 (uint64_t *)&addr[roffs]);
535 /* LINTED - alignment */
536 dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
540 /* LINTED - alignment */
541 dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
548 for (i = 0; i < rec->dtrd_size; i++)
549 hashval += addr[roffs + i];
552 ndx = hashval % hash->dtah_size;
554 for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
555 if (h->dtahe_hashval != hashval)
558 if (h->dtahe_size != size)
561 aggdata = &h->dtahe_data;
562 data = aggdata->dtada_data;
564 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
565 rec = &agg->dtagd_rec[j];
566 roffs = rec->dtrd_offset;
568 for (i = 0; i < rec->dtrd_size; i++)
569 if (addr[roffs + i] != data[roffs + i])
574 * We found it. Now we need to apply the aggregating
575 * action on the data here.
577 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
578 roffs = rec->dtrd_offset;
579 /* LINTED - alignment */
580 h->dtahe_aggregate((int64_t *)&data[roffs],
581 /* LINTED - alignment */
582 (int64_t *)&addr[roffs], rec->dtrd_size);
585 * If we're keeping per CPU data, apply the aggregating
586 * action there as well.
588 if (aggdata->dtada_percpu != NULL) {
589 data = aggdata->dtada_percpu[cpu];
591 /* LINTED - alignment */
592 h->dtahe_aggregate((int64_t *)data,
593 /* LINTED - alignment */
594 (int64_t *)&addr[roffs], rec->dtrd_size);
603 * If we're here, we couldn't find an entry for this record.
605 if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
606 return (dt_set_errno(dtp, EDT_NOMEM));
607 bzero(h, sizeof (dt_ahashent_t));
608 aggdata = &h->dtahe_data;
610 if ((aggdata->dtada_data = malloc(size)) == NULL) {
612 return (dt_set_errno(dtp, EDT_NOMEM));
615 bcopy(addr, aggdata->dtada_data, size);
616 aggdata->dtada_size = size;
617 aggdata->dtada_desc = agg;
618 aggdata->dtada_handle = dtp;
619 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
620 &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
621 aggdata->dtada_normal = 1;
623 h->dtahe_hashval = hashval;
624 h->dtahe_size = size;
625 (void) dt_aggregate_aggvarid(h);
627 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
629 if (flags & DTRACE_A_PERCPU) {
630 int max_cpus = agp->dtat_maxcpu;
631 caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
633 if (percpu == NULL) {
634 free(aggdata->dtada_data);
636 return (dt_set_errno(dtp, EDT_NOMEM));
639 for (j = 0; j < max_cpus; j++) {
640 percpu[j] = malloc(rec->dtrd_size);
642 if (percpu[j] == NULL) {
646 free(aggdata->dtada_data);
648 return (dt_set_errno(dtp, EDT_NOMEM));
652 bcopy(&addr[rec->dtrd_offset],
653 percpu[j], rec->dtrd_size);
655 bzero(percpu[j], rec->dtrd_size);
659 aggdata->dtada_percpu = percpu;
662 switch (rec->dtrd_action) {
664 h->dtahe_aggregate = dt_aggregate_min;
668 h->dtahe_aggregate = dt_aggregate_max;
671 case DTRACEAGG_LQUANTIZE:
672 h->dtahe_aggregate = dt_aggregate_lquantize;
675 case DTRACEAGG_LLQUANTIZE:
676 h->dtahe_aggregate = dt_aggregate_llquantize;
679 case DTRACEAGG_COUNT:
682 case DTRACEAGG_STDDEV:
683 case DTRACEAGG_QUANTIZE:
684 h->dtahe_aggregate = dt_aggregate_count;
688 return (dt_set_errno(dtp, EDT_BADAGG));
691 if (hash->dtah_hash[ndx] != NULL)
692 hash->dtah_hash[ndx]->dtahe_prev = h;
694 h->dtahe_next = hash->dtah_hash[ndx];
695 hash->dtah_hash[ndx] = h;
697 if (hash->dtah_all != NULL)
698 hash->dtah_all->dtahe_prevall = h;
700 h->dtahe_nextall = hash->dtah_all;
703 offs += agg->dtagd_size;
710 dtrace_aggregate_snap(dtrace_hdl_t *dtp)
713 dt_aggregate_t *agp = &dtp->dt_aggregate;
714 hrtime_t now = gethrtime();
715 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
717 if (dtp->dt_lastagg != 0) {
718 if (now - dtp->dt_lastagg < interval)
721 dtp->dt_lastagg += interval;
723 dtp->dt_lastagg = now;
727 return (dt_set_errno(dtp, EINVAL));
729 if (agp->dtat_buf.dtbd_size == 0)
732 for (i = 0; i < agp->dtat_ncpus; i++) {
733 if ((rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i])))
741 dt_aggregate_hashcmp(const void *lhs, const void *rhs)
743 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
744 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
745 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
746 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
748 if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
749 return (DT_LESSTHAN);
751 if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
752 return (DT_GREATERTHAN);
758 dt_aggregate_varcmp(const void *lhs, const void *rhs)
760 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
761 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
762 dtrace_aggvarid_t lid, rid;
764 lid = dt_aggregate_aggvarid(lh);
765 rid = dt_aggregate_aggvarid(rh);
768 return (DT_LESSTHAN);
771 return (DT_GREATERTHAN);
777 dt_aggregate_keycmp(const void *lhs, const void *rhs)
779 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
780 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
781 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
782 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
783 dtrace_recdesc_t *lrec, *rrec;
785 int rval, i, j, keypos, nrecs;
787 if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
790 nrecs = lagg->dtagd_nrecs - 1;
791 assert(nrecs == ragg->dtagd_nrecs - 1);
793 keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
795 for (i = 1; i < nrecs; i++) {
797 int ndx = i + keypos;
800 ndx = ndx - nrecs + 1;
802 lrec = &lagg->dtagd_rec[ndx];
803 rrec = &ragg->dtagd_rec[ndx];
805 ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
806 rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
808 if (lrec->dtrd_size < rrec->dtrd_size)
809 return (DT_LESSTHAN);
811 if (lrec->dtrd_size > rrec->dtrd_size)
812 return (DT_GREATERTHAN);
814 switch (lrec->dtrd_size) {
815 case sizeof (uint64_t):
816 /* LINTED - alignment */
817 lval = *((uint64_t *)ldata);
818 /* LINTED - alignment */
819 rval = *((uint64_t *)rdata);
822 case sizeof (uint32_t):
823 /* LINTED - alignment */
824 lval = *((uint32_t *)ldata);
825 /* LINTED - alignment */
826 rval = *((uint32_t *)rdata);
829 case sizeof (uint16_t):
830 /* LINTED - alignment */
831 lval = *((uint16_t *)ldata);
832 /* LINTED - alignment */
833 rval = *((uint16_t *)rdata);
836 case sizeof (uint8_t):
837 lval = *((uint8_t *)ldata);
838 rval = *((uint8_t *)rdata);
842 switch (lrec->dtrd_action) {
844 case DTRACEACT_UADDR:
846 for (j = 0; j < 2; j++) {
847 /* LINTED - alignment */
848 lval = ((uint64_t *)ldata)[j];
849 /* LINTED - alignment */
850 rval = ((uint64_t *)rdata)[j];
853 return (DT_LESSTHAN);
856 return (DT_GREATERTHAN);
862 for (j = 0; j < lrec->dtrd_size; j++) {
863 lval = ((uint8_t *)ldata)[j];
864 rval = ((uint8_t *)rdata)[j];
867 return (DT_LESSTHAN);
870 return (DT_GREATERTHAN);
878 return (DT_LESSTHAN);
881 return (DT_GREATERTHAN);
888 dt_aggregate_valcmp(const void *lhs, const void *rhs)
890 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
891 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
892 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
893 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
894 caddr_t ldata = lh->dtahe_data.dtada_data;
895 caddr_t rdata = rh->dtahe_data.dtada_data;
896 dtrace_recdesc_t *lrec, *rrec;
897 int64_t *laddr, *raddr;
900 assert(lagg->dtagd_nrecs == ragg->dtagd_nrecs);
902 lrec = &lagg->dtagd_rec[lagg->dtagd_nrecs - 1];
903 rrec = &ragg->dtagd_rec[ragg->dtagd_nrecs - 1];
905 assert(lrec->dtrd_action == rrec->dtrd_action);
907 laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
908 raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
910 switch (lrec->dtrd_action) {
912 rval = dt_aggregate_averagecmp(laddr, raddr);
915 case DTRACEAGG_STDDEV:
916 rval = dt_aggregate_stddevcmp(laddr, raddr);
919 case DTRACEAGG_QUANTIZE:
920 rval = dt_aggregate_quantizedcmp(laddr, raddr);
923 case DTRACEAGG_LQUANTIZE:
924 rval = dt_aggregate_lquantizedcmp(laddr, raddr);
927 case DTRACEAGG_LLQUANTIZE:
928 rval = dt_aggregate_llquantizedcmp(laddr, raddr);
931 case DTRACEAGG_COUNT:
935 rval = dt_aggregate_countcmp(laddr, raddr);
946 dt_aggregate_valkeycmp(const void *lhs, const void *rhs)
950 if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
954 * If we're here, the values for the two aggregation elements are
955 * equal. We already know that the key layout is the same for the two
956 * elements; we must now compare the keys themselves as a tie-breaker.
958 return (dt_aggregate_keycmp(lhs, rhs));
962 dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
966 if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
969 return (dt_aggregate_varcmp(lhs, rhs));
973 dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
977 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
980 return (dt_aggregate_keycmp(lhs, rhs));
984 dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
988 if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
991 return (dt_aggregate_varcmp(lhs, rhs));
995 dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
999 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
1002 return (dt_aggregate_valkeycmp(lhs, rhs));
1006 dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
1008 return (dt_aggregate_keyvarcmp(rhs, lhs));
1012 dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
1014 return (dt_aggregate_varkeycmp(rhs, lhs));
1018 dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
1020 return (dt_aggregate_valvarcmp(rhs, lhs));
1024 dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
1026 return (dt_aggregate_varvalcmp(rhs, lhs));
1030 dt_aggregate_bundlecmp(const void *lhs, const void *rhs)
1032 dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
1033 dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
1038 * If we're sorting on keys, we need to scan until we find the
1039 * last entry -- that's the representative key. (The order of
1040 * the bundle is values followed by key to accommodate the
1041 * default behavior of sorting by value.) If the keys are
1042 * equal, we'll fall into the value comparison loop, below.
1044 for (i = 0; lh[i + 1] != NULL; i++)
1048 assert(rh[i + 1] == NULL);
1050 if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
1054 for (i = 0; ; i++) {
1055 if (lh[i + 1] == NULL) {
1057 * All of the values are equal; if we're sorting on
1058 * keys, then we're only here because the keys were
1059 * found to be equal and these records are therefore
1060 * equal. If we're not sorting on keys, we'll use the
1061 * key comparison from the representative key as the
1068 assert(rh[i + 1] == NULL);
1069 return (dt_aggregate_keycmp(&lh[i], &rh[i]));
1071 if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
1078 dt_aggregate_go(dtrace_hdl_t *dtp)
1080 dt_aggregate_t *agp = &dtp->dt_aggregate;
1081 dtrace_optval_t size, cpu;
1082 dtrace_bufdesc_t *buf = &agp->dtat_buf;
1085 assert(agp->dtat_maxcpu == 0);
1086 assert(agp->dtat_ncpu == 0);
1087 assert(agp->dtat_cpus == NULL);
1089 agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
1090 agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
1091 agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
1093 if (agp->dtat_cpus == NULL)
1094 return (dt_set_errno(dtp, EDT_NOMEM));
1097 * Use the aggregation buffer size as reloaded from the kernel.
1099 size = dtp->dt_options[DTRACEOPT_AGGSIZE];
1101 rval = dtrace_getopt(dtp, "aggsize", &size);
1104 if (size == 0 || size == DTRACEOPT_UNSET)
1107 buf = &agp->dtat_buf;
1108 buf->dtbd_size = size;
1110 if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
1111 return (dt_set_errno(dtp, EDT_NOMEM));
1114 * Now query for the CPUs enabled.
1116 rval = dtrace_getopt(dtp, "cpu", &cpu);
1117 assert(rval == 0 && cpu != DTRACEOPT_UNSET);
1119 if (cpu != DTRACE_CPUALL) {
1120 assert(cpu < agp->dtat_ncpu);
1121 agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
1126 agp->dtat_ncpus = 0;
1127 for (i = 0; i < agp->dtat_maxcpu; i++) {
1128 if (dt_status(dtp, i) == -1)
1131 agp->dtat_cpus[agp->dtat_ncpus++] = i;
1138 dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
1140 dt_aggregate_t *agp = &dtp->dt_aggregate;
1141 dtrace_aggdata_t *data;
1142 dtrace_aggdesc_t *aggdesc;
1143 dtrace_recdesc_t *rec;
1147 case DTRACE_AGGWALK_NEXT:
1150 case DTRACE_AGGWALK_CLEAR: {
1151 uint32_t size, offs = 0;
1153 aggdesc = h->dtahe_data.dtada_desc;
1154 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1155 size = rec->dtrd_size;
1156 data = &h->dtahe_data;
1158 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1159 offs = sizeof (uint64_t);
1160 size -= sizeof (uint64_t);
1163 bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
1165 if (data->dtada_percpu == NULL)
1168 for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
1169 bzero(data->dtada_percpu[i] + offs, size);
1173 case DTRACE_AGGWALK_ERROR:
1175 * We assume that errno is already set in this case.
1177 return (dt_set_errno(dtp, errno));
1179 case DTRACE_AGGWALK_ABORT:
1180 return (dt_set_errno(dtp, EDT_DIRABORT));
1182 case DTRACE_AGGWALK_DENORMALIZE:
1183 h->dtahe_data.dtada_normal = 1;
1186 case DTRACE_AGGWALK_NORMALIZE:
1187 if (h->dtahe_data.dtada_normal == 0) {
1188 h->dtahe_data.dtada_normal = 1;
1189 return (dt_set_errno(dtp, EDT_BADRVAL));
1194 case DTRACE_AGGWALK_REMOVE: {
1195 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1196 int max_cpus = agp->dtat_maxcpu;
1199 * First, remove this hash entry from its hash chain.
1201 if (h->dtahe_prev != NULL) {
1202 h->dtahe_prev->dtahe_next = h->dtahe_next;
1204 dt_ahash_t *hash = &agp->dtat_hash;
1205 size_t ndx = h->dtahe_hashval % hash->dtah_size;
1207 assert(hash->dtah_hash[ndx] == h);
1208 hash->dtah_hash[ndx] = h->dtahe_next;
1211 if (h->dtahe_next != NULL)
1212 h->dtahe_next->dtahe_prev = h->dtahe_prev;
1215 * Now remove it from the list of all hash entries.
1217 if (h->dtahe_prevall != NULL) {
1218 h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
1220 dt_ahash_t *hash = &agp->dtat_hash;
1222 assert(hash->dtah_all == h);
1223 hash->dtah_all = h->dtahe_nextall;
1226 if (h->dtahe_nextall != NULL)
1227 h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
1230 * We're unlinked. We can safely destroy the data.
1232 if (aggdata->dtada_percpu != NULL) {
1233 for (i = 0; i < max_cpus; i++)
1234 free(aggdata->dtada_percpu[i]);
1235 free(aggdata->dtada_percpu);
1238 free(aggdata->dtada_data);
1245 return (dt_set_errno(dtp, EDT_BADRVAL));
1252 dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
1253 int (*compar)(const void *, const void *))
1255 int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
1256 dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
1258 dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
1259 dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
1261 if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
1262 dt_keypos = (int)keyposopt;
1267 if (compar == NULL) {
1269 compar = dt_aggregate_varvalcmp;
1271 compar = dt_aggregate_varkeycmp;
1275 qsort(base, nel, width, compar);
1283 dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
1285 dt_ahashent_t *h, *next;
1286 dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
1288 for (h = hash->dtah_all; h != NULL; h = next) {
1290 * dt_aggwalk_rval() can potentially remove the current hash
1291 * entry; we need to load the next hash entry before calling
1294 next = h->dtahe_nextall;
1296 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1304 dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1305 dtrace_aggregate_f *func, void *arg,
1306 int (*sfunc)(const void *, const void *))
1308 dt_aggregate_t *agp = &dtp->dt_aggregate;
1309 dt_ahashent_t *h, **sorted;
1310 dt_ahash_t *hash = &agp->dtat_hash;
1311 size_t i, nentries = 0;
1313 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
1316 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1321 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
1324 (void) pthread_mutex_lock(&dt_qsort_lock);
1326 if (sfunc == NULL) {
1327 dt_aggregate_qsort(dtp, sorted, nentries,
1328 sizeof (dt_ahashent_t *), NULL);
1331 * If we've been explicitly passed a sorting function,
1332 * we'll use that -- ignoring the values of the "aggsortrev",
1333 * "aggsortkey" and "aggsortkeypos" options.
1335 qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
1338 (void) pthread_mutex_unlock(&dt_qsort_lock);
1340 for (i = 0; i < nentries; i++) {
1343 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1) {
1344 dt_free(dtp, sorted);
1349 dt_free(dtp, sorted);
1354 dtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1355 dtrace_aggregate_f *func, void *arg)
1357 return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
1361 dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
1362 dtrace_aggregate_f *func, void *arg)
1364 return (dt_aggregate_walk_sorted(dtp, func,
1365 arg, dt_aggregate_varkeycmp));
1369 dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
1370 dtrace_aggregate_f *func, void *arg)
1372 return (dt_aggregate_walk_sorted(dtp, func,
1373 arg, dt_aggregate_varvalcmp));
1377 dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
1378 dtrace_aggregate_f *func, void *arg)
1380 return (dt_aggregate_walk_sorted(dtp, func,
1381 arg, dt_aggregate_keyvarcmp));
1385 dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
1386 dtrace_aggregate_f *func, void *arg)
1388 return (dt_aggregate_walk_sorted(dtp, func,
1389 arg, dt_aggregate_valvarcmp));
1393 dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
1394 dtrace_aggregate_f *func, void *arg)
1396 return (dt_aggregate_walk_sorted(dtp, func,
1397 arg, dt_aggregate_varkeyrevcmp));
1401 dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
1402 dtrace_aggregate_f *func, void *arg)
1404 return (dt_aggregate_walk_sorted(dtp, func,
1405 arg, dt_aggregate_varvalrevcmp));
1409 dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
1410 dtrace_aggregate_f *func, void *arg)
1412 return (dt_aggregate_walk_sorted(dtp, func,
1413 arg, dt_aggregate_keyvarrevcmp));
1417 dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
1418 dtrace_aggregate_f *func, void *arg)
1420 return (dt_aggregate_walk_sorted(dtp, func,
1421 arg, dt_aggregate_valvarrevcmp));
1425 dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
1426 int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
1428 dt_aggregate_t *agp = &dtp->dt_aggregate;
1429 dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
1430 const dtrace_aggdata_t **data;
1431 dt_ahashent_t *zaggdata = NULL;
1432 dt_ahash_t *hash = &agp->dtat_hash;
1433 size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
1434 dtrace_aggvarid_t max = 0, aggvar;
1435 int rval = -1, *map, *remap = NULL;
1437 dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
1440 * If the sorting position is greater than the number of aggregation
1441 * variable IDs, we silently set it to 0.
1443 if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
1447 * First we need to translate the specified aggregation variable IDs
1448 * into a linear map that will allow us to translate an aggregation
1449 * variable ID into its position in the specified aggvars.
1451 for (i = 0; i < naggvars; i++) {
1452 if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
1453 return (dt_set_errno(dtp, EDT_BADAGGVAR));
1455 if (aggvars[i] > max)
1459 if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
1462 zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
1464 if (zaggdata == NULL)
1467 for (i = 0; i < naggvars; i++) {
1468 int ndx = i + sortpos;
1470 if (ndx >= naggvars)
1473 aggvar = aggvars[ndx];
1474 assert(aggvar <= max);
1478 * We have an aggregation variable that is present
1479 * more than once in the array of aggregation
1480 * variables. While it's unclear why one might want
1481 * to do this, it's legal. To support this construct,
1482 * we will allocate a remap that will indicate the
1483 * position from which this aggregation variable
1484 * should be pulled. (That is, where the remap will
1485 * map from one position to another.)
1487 if (remap == NULL) {
1488 remap = dt_zalloc(dtp, naggvars * sizeof (int));
1495 * Given that the variable is already present, assert
1496 * that following through the mapping and adjusting
1497 * for the sort position yields the same aggregation
1500 assert(aggvars[(map[aggvar] - 1 + sortpos) %
1501 naggvars] == aggvars[ndx]);
1503 remap[i] = map[aggvar];
1507 map[aggvar] = i + 1;
1511 * We need to take two passes over the data to size our allocation, so
1512 * we'll use the first pass to also fill in the zero-filled data to be
1513 * used to properly format a zero-valued aggregation.
1515 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1516 dtrace_aggvarid_t id;
1519 if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
1522 if (zaggdata[ndx - 1].dtahe_size == 0) {
1523 zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
1524 zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
1530 if (nentries == 0) {
1532 * We couldn't find any entries; there is nothing else to do.
1539 * Before we sort the data, we're going to look for any holes in our
1540 * zero-filled data. This will occur if an aggregation variable that
1541 * we are being asked to print has not yet been assigned the result of
1542 * any aggregating action for _any_ tuple. The issue becomes that we
1543 * would like a zero value to be printed for all columns for this
1544 * aggregation, but without any record description, we don't know the
1545 * aggregating action that corresponds to the aggregation variable. To
1546 * try to find a match, we're simply going to lookup aggregation IDs
1547 * (which are guaranteed to be contiguous and to start from 1), looking
1548 * for the specified aggregation variable ID. If we find a match,
1549 * we'll use that. If we iterate over all aggregation IDs and don't
1550 * find a match, then we must be an anonymous enabling. (Anonymous
1551 * enablings can't currently derive either aggregation variable IDs or
1552 * aggregation variable names given only an aggregation ID.) In this
1553 * obscure case (anonymous enabling, multiple aggregation printa() with
1554 * some aggregations not represented for any tuple), our defined
1555 * behavior is that the zero will be printed in the format of the first
1556 * aggregation variable that contains any non-zero value.
1558 for (i = 0; i < naggvars; i++) {
1559 if (zaggdata[i].dtahe_size == 0) {
1560 dtrace_aggvarid_t aggvar;
1562 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1563 assert(zaggdata[i].dtahe_data.dtada_data == NULL);
1565 for (j = DTRACE_AGGIDNONE + 1; ; j++) {
1566 dtrace_aggdesc_t *agg;
1567 dtrace_aggdata_t *aggdata;
1569 if (dt_aggid_lookup(dtp, j, &agg) != 0)
1572 if (agg->dtagd_varid != aggvar)
1576 * We have our description -- now we need to
1577 * cons up the zaggdata entry for it.
1579 aggdata = &zaggdata[i].dtahe_data;
1580 aggdata->dtada_size = agg->dtagd_size;
1581 aggdata->dtada_desc = agg;
1582 aggdata->dtada_handle = dtp;
1583 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
1584 &aggdata->dtada_edesc,
1585 &aggdata->dtada_pdesc);
1586 aggdata->dtada_normal = 1;
1587 zaggdata[i].dtahe_hashval = 0;
1588 zaggdata[i].dtahe_size = agg->dtagd_size;
1592 if (zaggdata[i].dtahe_size == 0) {
1596 * We couldn't find this aggregation, meaning
1597 * that we have never seen it before for any
1598 * tuple _and_ this is an anonymous enabling.
1599 * That is, we're in the obscure case outlined
1600 * above. In this case, our defined behavior
1601 * is to format the data in the format of the
1602 * first non-zero aggregation -- of which, of
1603 * course, we know there to be at least one
1604 * (or nentries would have been zero).
1606 for (j = 0; j < naggvars; j++) {
1607 if (zaggdata[j].dtahe_size != 0)
1611 assert(j < naggvars);
1612 zaggdata[i] = zaggdata[j];
1614 data = zaggdata[i].dtahe_data.dtada_data;
1615 assert(data != NULL);
1621 * Now we need to allocate our zero-filled data for use for
1622 * aggregations that don't have a value corresponding to a given key.
1624 for (i = 0; i < naggvars; i++) {
1625 dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
1626 dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
1627 dtrace_recdesc_t *rec;
1631 zsize = zaggdata[i].dtahe_size;
1634 if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
1636 * If we failed to allocated some zero-filled data, we
1637 * need to zero out the remaining dtada_data pointers
1638 * to prevent the wrong data from being freed below.
1640 for (j = i; j < naggvars; j++)
1641 zaggdata[j].dtahe_data.dtada_data = NULL;
1645 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1648 * First, the easy bit. To maintain compatibility with
1649 * consumers that pull the compiler-generated ID out of the
1650 * data, we put that ID at the top of the zero-filled data.
1652 rec = &aggdesc->dtagd_rec[0];
1653 /* LINTED - alignment */
1654 *((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
1656 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1659 * Now for the more complicated part. If (and only if) this
1660 * is an lquantize() aggregating action, zero-filled data is
1661 * not equivalent to an empty record: we must also get the
1662 * parameters for the lquantize().
1664 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1665 if (aggdata->dtada_data != NULL) {
1667 * The easier case here is if we actually have
1668 * some prototype data -- in which case we
1669 * manually dig it out of the aggregation
1672 /* LINTED - alignment */
1673 larg = *((uint64_t *)(aggdata->dtada_data +
1677 * We don't have any prototype data. As a
1678 * result, we know that we _do_ have the
1679 * compiler-generated information. (If this
1680 * were an anonymous enabling, all of our
1681 * zero-filled data would have prototype data
1682 * -- either directly or indirectly.) So as
1683 * gross as it is, we'll grovel around in the
1684 * compiler-generated information to find the
1685 * lquantize() parameters.
1687 dtrace_stmtdesc_t *sdp;
1691 sdp = (dtrace_stmtdesc_t *)(uintptr_t)
1692 aggdesc->dtagd_rec[0].dtrd_uarg;
1693 aid = sdp->dtsd_aggdata;
1694 isp = (dt_idsig_t *)aid->di_data;
1695 assert(isp->dis_auxinfo != 0);
1696 larg = isp->dis_auxinfo;
1699 /* LINTED - alignment */
1700 *((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
1703 aggdata->dtada_data = zdata;
1707 * Now that we've dealt with setting up our zero-filled data, we can
1708 * allocate our sorted array, and take another pass over the data to
1711 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1716 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
1717 dtrace_aggvarid_t id;
1719 if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
1725 assert(i == nentries);
1728 * We've loaded our array; now we need to sort by value to allow us
1729 * to create bundles of like value. We're going to acquire the
1730 * dt_qsort_lock here, and hold it across all of our subsequent
1731 * comparison and sorting.
1733 (void) pthread_mutex_lock(&dt_qsort_lock);
1735 qsort(sorted, nentries, sizeof (dt_ahashent_t *),
1736 dt_aggregate_keyvarcmp);
1739 * Now we need to go through and create bundles. Because the number
1740 * of bundles is bounded by the size of the sorted array, we're going
1741 * to reuse the underlying storage. And note that "bundle" is an
1742 * array of pointers to arrays of pointers to dt_ahashent_t -- making
1743 * its type (regrettably) "dt_ahashent_t ***". (Regrettable because
1744 * '*' -- like '_' and 'X' -- should never appear in triplicate in
1747 bundle = (dt_ahashent_t ***)sorted;
1749 for (i = 1, start = 0; i <= nentries; i++) {
1751 dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
1755 * We have a bundle boundary. Everything from start to
1756 * (i - 1) belongs in one bundle.
1758 assert(i - start <= naggvars);
1759 bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
1761 if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
1762 (void) pthread_mutex_unlock(&dt_qsort_lock);
1766 for (j = start; j < i; j++) {
1767 dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
1770 assert(map[id] != 0);
1771 assert(map[id] - 1 < naggvars);
1772 assert(nbundle[map[id] - 1] == NULL);
1773 nbundle[map[id] - 1] = sorted[j];
1775 if (nbundle[naggvars] == NULL)
1776 nbundle[naggvars] = sorted[j];
1779 for (j = 0; j < naggvars; j++) {
1780 if (nbundle[j] != NULL)
1784 * Before we assume that this aggregation variable
1785 * isn't present (and fall back to using the
1786 * zero-filled data allocated earlier), check the
1787 * remap. If we have a remapping, we'll drop it in
1788 * here. Note that we might be remapping an
1789 * aggregation variable that isn't present for this
1790 * key; in this case, the aggregation data that we
1791 * copy will point to the zeroed data.
1793 if (remap != NULL && remap[j]) {
1794 assert(remap[j] - 1 < j);
1795 assert(nbundle[remap[j] - 1] != NULL);
1796 nbundle[j] = nbundle[remap[j] - 1];
1798 nbundle[j] = &zaggdata[j];
1802 bundle[nbundles++] = nbundle;
1807 * Now we need to re-sort based on the first value.
1809 dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
1810 dt_aggregate_bundlecmp);
1812 (void) pthread_mutex_unlock(&dt_qsort_lock);
1815 * We're done! Now we just need to go back over the sorted bundles,
1816 * calling the function.
1818 data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
1820 for (i = 0; i < nbundles; i++) {
1821 for (j = 0; j < naggvars; j++)
1824 for (j = 0; j < naggvars; j++) {
1825 int ndx = j - sortpos;
1830 assert(bundle[i][ndx] != NULL);
1831 data[j + 1] = &bundle[i][ndx]->dtahe_data;
1834 for (j = 0; j < naggvars; j++)
1835 assert(data[j + 1] != NULL);
1838 * The representative key is the last element in the bundle.
1839 * Assert that we have one, and then set it to be the first
1842 assert(bundle[i][j] != NULL);
1843 data[0] = &bundle[i][j]->dtahe_data;
1845 if ((rval = func(data, naggvars + 1, arg)) == -1)
1851 for (i = 0; i < nbundles; i++)
1852 dt_free(dtp, bundle[i]);
1854 if (zaggdata != NULL) {
1855 for (i = 0; i < naggvars; i++)
1856 dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
1859 dt_free(dtp, zaggdata);
1860 dt_free(dtp, sorted);
1861 dt_free(dtp, remap);
1868 dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
1869 dtrace_aggregate_walk_f *func)
1871 dt_print_aggdata_t pd;
1875 pd.dtpa_allunprint = 1;
1878 func = dtrace_aggregate_walk_sorted;
1880 if ((*func)(dtp, dt_print_agg, &pd) == -1)
1881 return (dt_set_errno(dtp, dtp->dt_errno));
1887 dtrace_aggregate_clear(dtrace_hdl_t *dtp)
1889 dt_aggregate_t *agp = &dtp->dt_aggregate;
1890 dt_ahash_t *hash = &agp->dtat_hash;
1892 dtrace_aggdata_t *data;
1893 dtrace_aggdesc_t *aggdesc;
1894 dtrace_recdesc_t *rec;
1895 int i, max_cpus = agp->dtat_maxcpu;
1897 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1898 aggdesc = h->dtahe_data.dtada_desc;
1899 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1900 data = &h->dtahe_data;
1902 bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
1904 if (data->dtada_percpu == NULL)
1907 for (i = 0; i < max_cpus; i++)
1908 bzero(data->dtada_percpu[i], rec->dtrd_size);
1913 dt_aggregate_destroy(dtrace_hdl_t *dtp)
1915 dt_aggregate_t *agp = &dtp->dt_aggregate;
1916 dt_ahash_t *hash = &agp->dtat_hash;
1917 dt_ahashent_t *h, *next;
1918 dtrace_aggdata_t *aggdata;
1919 int i, max_cpus = agp->dtat_maxcpu;
1921 if (hash->dtah_hash == NULL) {
1922 assert(hash->dtah_all == NULL);
1924 free(hash->dtah_hash);
1926 for (h = hash->dtah_all; h != NULL; h = next) {
1927 next = h->dtahe_nextall;
1929 aggdata = &h->dtahe_data;
1931 if (aggdata->dtada_percpu != NULL) {
1932 for (i = 0; i < max_cpus; i++)
1933 free(aggdata->dtada_percpu[i]);
1934 free(aggdata->dtada_percpu);
1937 free(aggdata->dtada_data);
1941 hash->dtah_hash = NULL;
1942 hash->dtah_all = NULL;
1943 hash->dtah_size = 0;
1946 free(agp->dtat_buf.dtbd_data);
1947 free(agp->dtat_cpus);