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) 2013, Joyent, Inc. All rights reserved.
29 * Copyright (c) 2012 by Delphix. All rights reserved.
38 #include <dt_oformat.h>
42 #include <sys/sysctl.h>
43 #include <libproc_compat.h>
47 #define DTRACE_AHASHSIZE 32779 /* big 'ol prime */
50 * Because qsort(3C) does not allow an argument to be passed to a comparison
51 * function, the variables that affect comparison must regrettably be global;
52 * they are protected by a global static lock, dt_qsort_lock.
54 static pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
56 static int dt_revsort;
57 static int dt_keysort;
60 #define DT_LESSTHAN (dt_revsort == 0 ? -1 : 1)
61 #define DT_GREATERTHAN (dt_revsort == 0 ? 1 : -1)
64 dt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
68 for (i = 0; i < size / sizeof (int64_t); i++)
69 existing[i] = existing[i] + new[i];
73 dt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
82 return (DT_GREATERTHAN);
89 dt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
97 dt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
104 dt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
106 int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
107 int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
110 return (DT_LESSTHAN);
113 return (DT_GREATERTHAN);
119 dt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
121 uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
122 uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
125 return (DT_LESSTHAN);
128 return (DT_GREATERTHAN);
135 dt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
137 int64_t arg = *existing++;
138 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
141 for (i = 0; i <= levels + 1; i++)
142 existing[i] = existing[i] + new[i + 1];
146 dt_aggregate_lquantizedsum(int64_t *lquanta)
148 int64_t arg = *lquanta++;
149 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
150 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
151 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
152 long double total = (long double)lquanta[0] * (long double)(base - 1);
154 for (i = 0; i < levels; base += step, i++)
155 total += (long double)lquanta[i + 1] * (long double)base;
157 return (total + (long double)lquanta[levels + 1] *
158 (long double)(base + 1));
162 dt_aggregate_lquantizedzero(int64_t *lquanta)
164 int64_t arg = *lquanta++;
165 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
166 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
167 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
172 for (i = 0; i < levels; base += step, i++) {
176 return (lquanta[i + 1]);
180 return (lquanta[levels + 1]);
186 dt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
188 long double lsum = dt_aggregate_lquantizedsum(lhs);
189 long double rsum = dt_aggregate_lquantizedsum(rhs);
190 int64_t lzero, rzero;
193 return (DT_LESSTHAN);
196 return (DT_GREATERTHAN);
199 * If they're both equal, then we will compare based on the weights at
200 * zero. If the weights at zero are equal (or if zero is not within
201 * the range of the linear quantization), then this will be judged a
202 * tie and will be resolved based on the key comparison.
204 lzero = dt_aggregate_lquantizedzero(lhs);
205 rzero = dt_aggregate_lquantizedzero(rhs);
208 return (DT_LESSTHAN);
211 return (DT_GREATERTHAN);
217 dt_aggregate_llquantize(int64_t *existing, int64_t *new, size_t size)
221 for (i = 1; i < size / sizeof (int64_t); i++)
222 existing[i] = existing[i] + new[i];
226 dt_aggregate_llquantizedsum(int64_t *llquanta)
228 int64_t arg = *llquanta++;
229 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
230 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
231 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
232 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
234 int64_t value = 1, next, step;
237 assert(nsteps >= factor);
238 assert(nsteps % factor == 0);
240 for (order = 0; order < low; order++)
243 total = (long double)llquanta[bin++] * (long double)(value - 1);
245 next = value * factor;
246 step = next > nsteps ? next / nsteps : 1;
248 while (order <= high) {
249 assert(value < next);
250 total += (long double)llquanta[bin++] * (long double)(value);
252 if ((value += step) != next)
255 next = value * factor;
256 step = next > nsteps ? next / nsteps : 1;
260 return (total + (long double)llquanta[bin] * (long double)value);
264 dt_aggregate_llquantizedcmp(int64_t *lhs, int64_t *rhs)
266 long double lsum = dt_aggregate_llquantizedsum(lhs);
267 long double rsum = dt_aggregate_llquantizedsum(rhs);
268 int64_t lzero, rzero;
271 return (DT_LESSTHAN);
274 return (DT_GREATERTHAN);
277 * If they're both equal, then we will compare based on the weights at
278 * zero. If the weights at zero are equal, then this will be judged a
279 * tie and will be resolved based on the key comparison.
285 return (DT_LESSTHAN);
288 return (DT_GREATERTHAN);
294 dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
296 int nbuckets = DTRACE_QUANTIZE_NBUCKETS;
297 long double ltotal = 0, rtotal = 0;
298 int64_t lzero, rzero;
301 for (i = 0; i < nbuckets; i++) {
302 int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
304 if (bucketval == 0) {
309 ltotal += (long double)bucketval * (long double)lhs[i];
310 rtotal += (long double)bucketval * (long double)rhs[i];
314 return (DT_LESSTHAN);
317 return (DT_GREATERTHAN);
320 * If they're both equal, then we will compare based on the weights at
321 * zero. If the weights at zero are equal, then this will be judged a
322 * tie and will be resolved based on the key comparison.
325 return (DT_LESSTHAN);
328 return (DT_GREATERTHAN);
334 dt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
336 uint64_t pid = data[0];
337 uint64_t *pc = &data[1];
338 struct ps_prochandle *P;
341 if (dtp->dt_vector != NULL)
344 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
347 dt_proc_lock(dtp, P);
349 if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
352 dt_proc_unlock(dtp, P);
353 dt_proc_release(dtp, P);
357 dt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
359 uint64_t pid = data[0];
360 uint64_t *pc = &data[1];
361 struct ps_prochandle *P;
364 if (dtp->dt_vector != NULL)
367 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
370 dt_proc_lock(dtp, P);
372 if ((map = Paddr_to_map(P, *pc)) != NULL)
375 dt_proc_unlock(dtp, P);
376 dt_proc_release(dtp, P);
380 dt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
385 if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
390 dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
395 if (dtp->dt_vector != NULL) {
397 * We don't have a way of just getting the module for a
398 * vectored open, and it doesn't seem to be worth defining
399 * one. This means that use of mod() won't get true
400 * aggregation in the postmortem case (some modules may
401 * appear more than once in aggregation output). It seems
402 * unlikely that anyone will ever notice or care...
407 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
408 dmp = dt_list_next(dmp)) {
409 if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
410 *pc = dmp->dm_text_va;
416 static dtrace_aggvarid_t
417 dt_aggregate_aggvarid(dt_ahashent_t *ent)
419 dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
420 caddr_t data = ent->dtahe_data.dtada_data;
421 dtrace_recdesc_t *rec = agg->dtagd_rec;
424 * First, we'll check the variable ID in the aggdesc. If it's valid,
425 * we'll return it. If not, we'll use the compiler-generated ID
426 * present as the first record.
428 if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
429 return (agg->dtagd_varid);
431 agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
434 return (agg->dtagd_varid);
439 dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
443 size_t offs, roffs, size, ndx;
446 dtrace_recdesc_t *rec;
447 dt_aggregate_t *agp = &dtp->dt_aggregate;
448 dtrace_aggdesc_t *agg;
449 dt_ahash_t *hash = &agp->dtat_hash;
451 dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
452 dtrace_aggdata_t *aggdata;
453 int flags = agp->dtat_flags;
458 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
460 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, &buf) == -1) {
462 if (errno == ENOENT) {
464 * If that failed with ENOENT, it may be because the
465 * CPU was unconfigured. This is okay; we'll just
466 * do nothing but return success.
471 return (dt_set_errno(dtp, errno));
474 if (buf->dtbd_drops != 0) {
475 xo_open_instance("probes");
476 dt_oformat_drop(dtp, cpu);
477 if (dt_handle_cpudrop(dtp, cpu,
478 DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1) {
479 xo_close_instance("probes");
482 xo_close_instance("probes");
485 if (buf->dtbd_size == 0)
488 if (hash->dtah_hash == NULL) {
491 hash->dtah_size = DTRACE_AHASHSIZE;
492 size = hash->dtah_size * sizeof (dt_ahashent_t *);
494 if ((hash->dtah_hash = malloc(size)) == NULL)
495 return (dt_set_errno(dtp, EDT_NOMEM));
497 bzero(hash->dtah_hash, size);
500 for (offs = 0; offs < buf->dtbd_size; ) {
502 * We're guaranteed to have an ID.
504 id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
507 if (id == DTRACE_AGGIDNONE) {
509 * This is filler to assure proper alignment of the
510 * next record; we simply ignore it.
516 if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
519 addr = buf->dtbd_data + offs;
520 size = agg->dtagd_size;
523 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
524 rec = &agg->dtagd_rec[j];
525 roffs = rec->dtrd_offset;
527 switch (rec->dtrd_action) {
529 dt_aggregate_usym(dtp,
530 /* LINTED - alignment */
531 (uint64_t *)&addr[roffs]);
535 dt_aggregate_umod(dtp,
536 /* LINTED - alignment */
537 (uint64_t *)&addr[roffs]);
541 /* LINTED - alignment */
542 dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
546 /* LINTED - alignment */
547 dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
554 for (i = 0; i < rec->dtrd_size; i++)
555 hashval += addr[roffs + i];
558 ndx = hashval % hash->dtah_size;
560 for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
561 if (h->dtahe_hashval != hashval)
564 if (h->dtahe_size != size)
567 aggdata = &h->dtahe_data;
568 data = aggdata->dtada_data;
570 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
571 rec = &agg->dtagd_rec[j];
572 roffs = rec->dtrd_offset;
574 for (i = 0; i < rec->dtrd_size; i++)
575 if (addr[roffs + i] != data[roffs + i])
580 * We found it. Now we need to apply the aggregating
581 * action on the data here.
583 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
584 roffs = rec->dtrd_offset;
585 /* LINTED - alignment */
586 h->dtahe_aggregate((int64_t *)&data[roffs],
587 /* LINTED - alignment */
588 (int64_t *)&addr[roffs], rec->dtrd_size);
591 * If we're keeping per CPU data, apply the aggregating
592 * action there as well.
594 if (aggdata->dtada_percpu != NULL) {
595 data = aggdata->dtada_percpu[cpu];
597 /* LINTED - alignment */
598 h->dtahe_aggregate((int64_t *)data,
599 /* LINTED - alignment */
600 (int64_t *)&addr[roffs], rec->dtrd_size);
609 * If we're here, we couldn't find an entry for this record.
611 if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
612 return (dt_set_errno(dtp, EDT_NOMEM));
613 bzero(h, sizeof (dt_ahashent_t));
614 aggdata = &h->dtahe_data;
616 if ((aggdata->dtada_data = malloc(size)) == NULL) {
618 return (dt_set_errno(dtp, EDT_NOMEM));
621 bcopy(addr, aggdata->dtada_data, size);
622 aggdata->dtada_size = size;
623 aggdata->dtada_desc = agg;
624 aggdata->dtada_handle = dtp;
625 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
626 &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
627 aggdata->dtada_normal = 1;
629 h->dtahe_hashval = hashval;
630 h->dtahe_size = size;
631 (void) dt_aggregate_aggvarid(h);
633 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
635 if (flags & DTRACE_A_PERCPU) {
636 int max_cpus = agp->dtat_maxcpu;
637 caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
639 if (percpu == NULL) {
640 free(aggdata->dtada_data);
642 return (dt_set_errno(dtp, EDT_NOMEM));
645 for (j = 0; j < max_cpus; j++) {
646 percpu[j] = malloc(rec->dtrd_size);
648 if (percpu[j] == NULL) {
652 free(aggdata->dtada_data);
654 return (dt_set_errno(dtp, EDT_NOMEM));
658 bcopy(&addr[rec->dtrd_offset],
659 percpu[j], rec->dtrd_size);
661 bzero(percpu[j], rec->dtrd_size);
665 aggdata->dtada_percpu = percpu;
668 switch (rec->dtrd_action) {
670 h->dtahe_aggregate = dt_aggregate_min;
674 h->dtahe_aggregate = dt_aggregate_max;
677 case DTRACEAGG_LQUANTIZE:
678 h->dtahe_aggregate = dt_aggregate_lquantize;
681 case DTRACEAGG_LLQUANTIZE:
682 h->dtahe_aggregate = dt_aggregate_llquantize;
685 case DTRACEAGG_COUNT:
688 case DTRACEAGG_STDDEV:
689 case DTRACEAGG_QUANTIZE:
690 h->dtahe_aggregate = dt_aggregate_count;
694 return (dt_set_errno(dtp, EDT_BADAGG));
697 if (hash->dtah_hash[ndx] != NULL)
698 hash->dtah_hash[ndx]->dtahe_prev = h;
700 h->dtahe_next = hash->dtah_hash[ndx];
701 hash->dtah_hash[ndx] = h;
703 if (hash->dtah_all != NULL)
704 hash->dtah_all->dtahe_prevall = h;
706 h->dtahe_nextall = hash->dtah_all;
709 offs += agg->dtagd_size;
716 dtrace_aggregate_snap(dtrace_hdl_t *dtp)
719 dt_aggregate_t *agp = &dtp->dt_aggregate;
720 hrtime_t now = gethrtime();
721 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
723 if (dtp->dt_lastagg != 0) {
724 if (now - dtp->dt_lastagg < interval)
727 dtp->dt_lastagg += interval;
729 dtp->dt_lastagg = now;
733 return (dt_set_errno(dtp, EINVAL));
735 if (agp->dtat_buf.dtbd_size == 0)
738 for (i = 0; i < agp->dtat_ncpus; i++) {
739 if ((rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i])))
747 dt_aggregate_hashcmp(const void *lhs, const void *rhs)
749 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
750 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
751 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
752 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
754 if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
755 return (DT_LESSTHAN);
757 if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
758 return (DT_GREATERTHAN);
764 dt_aggregate_varcmp(const void *lhs, const void *rhs)
766 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
767 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
768 dtrace_aggvarid_t lid, rid;
770 lid = dt_aggregate_aggvarid(lh);
771 rid = dt_aggregate_aggvarid(rh);
774 return (DT_LESSTHAN);
777 return (DT_GREATERTHAN);
783 dt_aggregate_keycmp(const void *lhs, const void *rhs)
785 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
786 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
787 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
788 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
789 dtrace_recdesc_t *lrec, *rrec;
791 int rval, i, j, keypos, nrecs;
793 if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
796 nrecs = lagg->dtagd_nrecs - 1;
797 assert(nrecs == ragg->dtagd_nrecs - 1);
799 keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
801 for (i = 1; i < nrecs; i++) {
803 int ndx = i + keypos;
806 ndx = ndx - nrecs + 1;
808 lrec = &lagg->dtagd_rec[ndx];
809 rrec = &ragg->dtagd_rec[ndx];
811 ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
812 rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
814 if (lrec->dtrd_size < rrec->dtrd_size)
815 return (DT_LESSTHAN);
817 if (lrec->dtrd_size > rrec->dtrd_size)
818 return (DT_GREATERTHAN);
820 switch (lrec->dtrd_size) {
821 case sizeof (uint64_t):
822 /* LINTED - alignment */
823 lval = *((uint64_t *)ldata);
824 /* LINTED - alignment */
825 rval = *((uint64_t *)rdata);
828 case sizeof (uint32_t):
829 /* LINTED - alignment */
830 lval = *((uint32_t *)ldata);
831 /* LINTED - alignment */
832 rval = *((uint32_t *)rdata);
835 case sizeof (uint16_t):
836 /* LINTED - alignment */
837 lval = *((uint16_t *)ldata);
838 /* LINTED - alignment */
839 rval = *((uint16_t *)rdata);
842 case sizeof (uint8_t):
843 lval = *((uint8_t *)ldata);
844 rval = *((uint8_t *)rdata);
848 switch (lrec->dtrd_action) {
850 case DTRACEACT_UADDR:
852 for (j = 0; j < 2; j++) {
853 /* LINTED - alignment */
854 lval = ((uint64_t *)ldata)[j];
855 /* LINTED - alignment */
856 rval = ((uint64_t *)rdata)[j];
859 return (DT_LESSTHAN);
862 return (DT_GREATERTHAN);
868 for (j = 0; j < lrec->dtrd_size; j++) {
869 lval = ((uint8_t *)ldata)[j];
870 rval = ((uint8_t *)rdata)[j];
873 return (DT_LESSTHAN);
876 return (DT_GREATERTHAN);
884 return (DT_LESSTHAN);
887 return (DT_GREATERTHAN);
894 dt_aggregate_valcmp(const void *lhs, const void *rhs)
896 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
897 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
898 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
899 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
900 caddr_t ldata = lh->dtahe_data.dtada_data;
901 caddr_t rdata = rh->dtahe_data.dtada_data;
902 dtrace_recdesc_t *lrec, *rrec;
903 int64_t *laddr, *raddr;
906 assert(lagg->dtagd_nrecs == ragg->dtagd_nrecs);
908 lrec = &lagg->dtagd_rec[lagg->dtagd_nrecs - 1];
909 rrec = &ragg->dtagd_rec[ragg->dtagd_nrecs - 1];
911 assert(lrec->dtrd_action == rrec->dtrd_action);
913 laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
914 raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
916 switch (lrec->dtrd_action) {
918 rval = dt_aggregate_averagecmp(laddr, raddr);
921 case DTRACEAGG_STDDEV:
922 rval = dt_aggregate_stddevcmp(laddr, raddr);
925 case DTRACEAGG_QUANTIZE:
926 rval = dt_aggregate_quantizedcmp(laddr, raddr);
929 case DTRACEAGG_LQUANTIZE:
930 rval = dt_aggregate_lquantizedcmp(laddr, raddr);
933 case DTRACEAGG_LLQUANTIZE:
934 rval = dt_aggregate_llquantizedcmp(laddr, raddr);
937 case DTRACEAGG_COUNT:
941 rval = dt_aggregate_countcmp(laddr, raddr);
952 dt_aggregate_valkeycmp(const void *lhs, const void *rhs)
956 if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
960 * If we're here, the values for the two aggregation elements are
961 * equal. We already know that the key layout is the same for the two
962 * elements; we must now compare the keys themselves as a tie-breaker.
964 return (dt_aggregate_keycmp(lhs, rhs));
968 dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
972 if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
975 return (dt_aggregate_varcmp(lhs, rhs));
979 dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
983 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
986 return (dt_aggregate_keycmp(lhs, rhs));
990 dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
994 if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
997 return (dt_aggregate_varcmp(lhs, rhs));
1001 dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
1005 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
1008 return (dt_aggregate_valkeycmp(lhs, rhs));
1012 dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
1014 return (dt_aggregate_keyvarcmp(rhs, lhs));
1018 dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
1020 return (dt_aggregate_varkeycmp(rhs, lhs));
1024 dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
1026 return (dt_aggregate_valvarcmp(rhs, lhs));
1030 dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
1032 return (dt_aggregate_varvalcmp(rhs, lhs));
1036 dt_aggregate_bundlecmp(const void *lhs, const void *rhs)
1038 dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
1039 dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
1044 * If we're sorting on keys, we need to scan until we find the
1045 * last entry -- that's the representative key. (The order of
1046 * the bundle is values followed by key to accommodate the
1047 * default behavior of sorting by value.) If the keys are
1048 * equal, we'll fall into the value comparison loop, below.
1050 for (i = 0; lh[i + 1] != NULL; i++)
1054 assert(rh[i + 1] == NULL);
1056 if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
1060 for (i = 0; ; i++) {
1061 if (lh[i + 1] == NULL) {
1063 * All of the values are equal; if we're sorting on
1064 * keys, then we're only here because the keys were
1065 * found to be equal and these records are therefore
1066 * equal. If we're not sorting on keys, we'll use the
1067 * key comparison from the representative key as the
1074 assert(rh[i + 1] == NULL);
1075 return (dt_aggregate_keycmp(&lh[i], &rh[i]));
1077 if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
1084 dt_aggregate_go(dtrace_hdl_t *dtp)
1086 dt_aggregate_t *agp = &dtp->dt_aggregate;
1087 dtrace_optval_t size, cpu;
1088 dtrace_bufdesc_t *buf = &agp->dtat_buf;
1091 assert(agp->dtat_maxcpu == 0);
1092 assert(agp->dtat_ncpu == 0);
1093 assert(agp->dtat_cpus == NULL);
1095 agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
1096 agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
1097 agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
1099 if (agp->dtat_cpus == NULL)
1100 return (dt_set_errno(dtp, EDT_NOMEM));
1103 * Use the aggregation buffer size as reloaded from the kernel.
1105 size = dtp->dt_options[DTRACEOPT_AGGSIZE];
1107 rval = dtrace_getopt(dtp, "aggsize", &size);
1110 if (size == 0 || size == DTRACEOPT_UNSET)
1113 buf = &agp->dtat_buf;
1114 buf->dtbd_size = size;
1116 if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
1117 return (dt_set_errno(dtp, EDT_NOMEM));
1120 * Now query for the CPUs enabled.
1122 rval = dtrace_getopt(dtp, "cpu", &cpu);
1123 assert(rval == 0 && cpu != DTRACEOPT_UNSET);
1125 if (cpu != DTRACE_CPUALL) {
1126 assert(cpu < agp->dtat_ncpu);
1127 agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
1132 agp->dtat_ncpus = 0;
1133 for (i = 0; i < agp->dtat_maxcpu; i++) {
1134 if (dt_status(dtp, i) == -1)
1137 agp->dtat_cpus[agp->dtat_ncpus++] = i;
1144 dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
1146 dt_aggregate_t *agp = &dtp->dt_aggregate;
1147 dtrace_aggdata_t *data;
1148 dtrace_aggdesc_t *aggdesc;
1149 dtrace_recdesc_t *rec;
1153 case DTRACE_AGGWALK_NEXT:
1156 case DTRACE_AGGWALK_CLEAR: {
1157 uint32_t size, offs = 0;
1159 aggdesc = h->dtahe_data.dtada_desc;
1160 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1161 size = rec->dtrd_size;
1162 data = &h->dtahe_data;
1164 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1165 offs = sizeof (uint64_t);
1166 size -= sizeof (uint64_t);
1169 bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
1171 if (data->dtada_percpu == NULL)
1174 for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
1175 bzero(data->dtada_percpu[i] + offs, size);
1179 case DTRACE_AGGWALK_ERROR:
1181 * We assume that errno is already set in this case.
1183 return (dt_set_errno(dtp, errno));
1185 case DTRACE_AGGWALK_ABORT:
1186 return (dt_set_errno(dtp, EDT_DIRABORT));
1188 case DTRACE_AGGWALK_DENORMALIZE:
1189 h->dtahe_data.dtada_normal = 1;
1192 case DTRACE_AGGWALK_NORMALIZE:
1193 if (h->dtahe_data.dtada_normal == 0) {
1194 h->dtahe_data.dtada_normal = 1;
1195 return (dt_set_errno(dtp, EDT_BADRVAL));
1200 case DTRACE_AGGWALK_REMOVE: {
1201 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1202 int max_cpus = agp->dtat_maxcpu;
1205 * First, remove this hash entry from its hash chain.
1207 if (h->dtahe_prev != NULL) {
1208 h->dtahe_prev->dtahe_next = h->dtahe_next;
1210 dt_ahash_t *hash = &agp->dtat_hash;
1211 size_t ndx = h->dtahe_hashval % hash->dtah_size;
1213 assert(hash->dtah_hash[ndx] == h);
1214 hash->dtah_hash[ndx] = h->dtahe_next;
1217 if (h->dtahe_next != NULL)
1218 h->dtahe_next->dtahe_prev = h->dtahe_prev;
1221 * Now remove it from the list of all hash entries.
1223 if (h->dtahe_prevall != NULL) {
1224 h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
1226 dt_ahash_t *hash = &agp->dtat_hash;
1228 assert(hash->dtah_all == h);
1229 hash->dtah_all = h->dtahe_nextall;
1232 if (h->dtahe_nextall != NULL)
1233 h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
1236 * We're unlinked. We can safely destroy the data.
1238 if (aggdata->dtada_percpu != NULL) {
1239 for (i = 0; i < max_cpus; i++)
1240 free(aggdata->dtada_percpu[i]);
1241 free(aggdata->dtada_percpu);
1244 free(aggdata->dtada_data);
1251 return (dt_set_errno(dtp, EDT_BADRVAL));
1258 dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
1259 int (*compar)(const void *, const void *))
1261 int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
1262 dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
1264 dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
1265 dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
1267 if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
1268 dt_keypos = (int)keyposopt;
1273 if (compar == NULL) {
1275 compar = dt_aggregate_varvalcmp;
1277 compar = dt_aggregate_varkeycmp;
1281 qsort(base, nel, width, compar);
1289 dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
1291 dt_ahashent_t *h, *next;
1292 dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
1294 for (h = hash->dtah_all; h != NULL; h = next) {
1296 * dt_aggwalk_rval() can potentially remove the current hash
1297 * entry; we need to load the next hash entry before calling
1300 next = h->dtahe_nextall;
1302 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1310 dt_aggregate_total(dtrace_hdl_t *dtp, boolean_t clear)
1313 dtrace_aggdata_t **total;
1314 dtrace_aggid_t max = DTRACE_AGGVARIDNONE, id;
1315 dt_aggregate_t *agp = &dtp->dt_aggregate;
1316 dt_ahash_t *hash = &agp->dtat_hash;
1319 tflags = DTRACE_A_TOTAL | DTRACE_A_HASNEGATIVES | DTRACE_A_HASPOSITIVES;
1322 * If we need to deliver per-aggregation totals, we're going to take
1323 * three passes over the aggregate: one to clear everything out and
1324 * determine our maximum aggregation ID, one to actually total
1325 * everything up, and a final pass to assign the totals to the
1326 * individual elements.
1328 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1329 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1331 if ((id = dt_aggregate_aggvarid(h)) > max)
1334 aggdata->dtada_total = 0;
1335 aggdata->dtada_flags &= ~tflags;
1338 if (clear || max == DTRACE_AGGVARIDNONE)
1341 total = dt_zalloc(dtp, (max + 1) * sizeof (dtrace_aggdata_t *));
1346 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1347 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1348 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1349 dtrace_recdesc_t *rec;
1353 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
1354 data = aggdata->dtada_data;
1355 addr = (int64_t *)(uintptr_t)(data + rec->dtrd_offset);
1357 switch (rec->dtrd_action) {
1358 case DTRACEAGG_STDDEV:
1359 val = dt_stddev((uint64_t *)addr, 1);
1363 case DTRACEAGG_COUNT:
1368 val = addr[0] ? (addr[1] / addr[0]) : 0;
1375 if (total[agg->dtagd_varid] == NULL) {
1376 total[agg->dtagd_varid] = aggdata;
1377 aggdata->dtada_flags |= DTRACE_A_TOTAL;
1379 aggdata = total[agg->dtagd_varid];
1383 aggdata->dtada_flags |= DTRACE_A_HASPOSITIVES;
1386 aggdata->dtada_flags |= DTRACE_A_HASNEGATIVES;
1390 if (dtp->dt_options[DTRACEOPT_AGGZOOM] != DTRACEOPT_UNSET) {
1391 val = (int64_t)((long double)val *
1392 (1 / DTRACE_AGGZOOM_MAX));
1394 if (val > aggdata->dtada_total)
1395 aggdata->dtada_total = val;
1397 aggdata->dtada_total += val;
1402 * And now one final pass to set everyone's total.
1404 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1405 dtrace_aggdata_t *aggdata = &h->dtahe_data, *t;
1406 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1408 if ((t = total[agg->dtagd_varid]) == NULL || aggdata == t)
1411 aggdata->dtada_total = t->dtada_total;
1412 aggdata->dtada_flags |= (t->dtada_flags & tflags);
1415 dt_free(dtp, total);
1421 dt_aggregate_minmaxbin(dtrace_hdl_t *dtp, boolean_t clear)
1424 dtrace_aggdata_t **minmax;
1425 dtrace_aggid_t max = DTRACE_AGGVARIDNONE, id;
1426 dt_aggregate_t *agp = &dtp->dt_aggregate;
1427 dt_ahash_t *hash = &agp->dtat_hash;
1429 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1430 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1432 if ((id = dt_aggregate_aggvarid(h)) > max)
1435 aggdata->dtada_minbin = 0;
1436 aggdata->dtada_maxbin = 0;
1437 aggdata->dtada_flags &= ~DTRACE_A_MINMAXBIN;
1440 if (clear || max == DTRACE_AGGVARIDNONE)
1443 minmax = dt_zalloc(dtp, (max + 1) * sizeof (dtrace_aggdata_t *));
1448 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1449 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1450 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1451 dtrace_recdesc_t *rec;
1454 int minbin = -1, maxbin = -1, i;
1455 int start = 0, size;
1457 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
1458 size = rec->dtrd_size / sizeof (int64_t);
1459 data = aggdata->dtada_data;
1460 addr = (int64_t *)(uintptr_t)(data + rec->dtrd_offset);
1462 switch (rec->dtrd_action) {
1463 case DTRACEAGG_LQUANTIZE:
1465 * For lquantize(), we always display the entire range
1466 * of the aggregation when aggpack is set.
1470 maxbin = size - 1 - start;
1473 case DTRACEAGG_QUANTIZE:
1474 for (i = start; i < size; i++) {
1486 * If we have no data (e.g., due to a clear()
1487 * or negative increments), we'll use the
1488 * zero bucket as both our min and max.
1490 minbin = maxbin = DTRACE_QUANTIZE_ZEROBUCKET;
1499 if (minmax[agg->dtagd_varid] == NULL) {
1500 minmax[agg->dtagd_varid] = aggdata;
1501 aggdata->dtada_flags |= DTRACE_A_MINMAXBIN;
1502 aggdata->dtada_minbin = minbin;
1503 aggdata->dtada_maxbin = maxbin;
1507 if (minbin < minmax[agg->dtagd_varid]->dtada_minbin)
1508 minmax[agg->dtagd_varid]->dtada_minbin = minbin;
1510 if (maxbin > minmax[agg->dtagd_varid]->dtada_maxbin)
1511 minmax[agg->dtagd_varid]->dtada_maxbin = maxbin;
1515 * And now one final pass to set everyone's minbin and maxbin.
1517 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1518 dtrace_aggdata_t *aggdata = &h->dtahe_data, *mm;
1519 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1521 if ((mm = minmax[agg->dtagd_varid]) == NULL || aggdata == mm)
1524 aggdata->dtada_minbin = mm->dtada_minbin;
1525 aggdata->dtada_maxbin = mm->dtada_maxbin;
1526 aggdata->dtada_flags |= DTRACE_A_MINMAXBIN;
1529 dt_free(dtp, minmax);
1535 dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1536 dtrace_aggregate_f *func, void *arg,
1537 int (*sfunc)(const void *, const void *))
1539 dt_aggregate_t *agp = &dtp->dt_aggregate;
1540 dt_ahashent_t *h, **sorted;
1541 dt_ahash_t *hash = &agp->dtat_hash;
1542 size_t i, nentries = 0;
1545 agp->dtat_flags &= ~(DTRACE_A_TOTAL | DTRACE_A_MINMAXBIN);
1547 if (dtp->dt_options[DTRACEOPT_AGGHIST] != DTRACEOPT_UNSET) {
1548 agp->dtat_flags |= DTRACE_A_TOTAL;
1550 if (dt_aggregate_total(dtp, B_FALSE) != 0)
1554 if (dtp->dt_options[DTRACEOPT_AGGPACK] != DTRACEOPT_UNSET) {
1555 agp->dtat_flags |= DTRACE_A_MINMAXBIN;
1557 if (dt_aggregate_minmaxbin(dtp, B_FALSE) != 0)
1561 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
1564 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1569 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
1572 (void) pthread_mutex_lock(&dt_qsort_lock);
1574 if (sfunc == NULL) {
1575 dt_aggregate_qsort(dtp, sorted, nentries,
1576 sizeof (dt_ahashent_t *), NULL);
1579 * If we've been explicitly passed a sorting function,
1580 * we'll use that -- ignoring the values of the "aggsortrev",
1581 * "aggsortkey" and "aggsortkeypos" options.
1583 qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
1586 (void) pthread_mutex_unlock(&dt_qsort_lock);
1588 for (i = 0; i < nentries; i++) {
1591 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1597 if (agp->dtat_flags & DTRACE_A_TOTAL)
1598 (void) dt_aggregate_total(dtp, B_TRUE);
1600 if (agp->dtat_flags & DTRACE_A_MINMAXBIN)
1601 (void) dt_aggregate_minmaxbin(dtp, B_TRUE);
1603 dt_free(dtp, sorted);
1608 dtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1609 dtrace_aggregate_f *func, void *arg)
1611 return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
1615 dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
1616 dtrace_aggregate_f *func, void *arg)
1618 return (dt_aggregate_walk_sorted(dtp, func,
1619 arg, dt_aggregate_varkeycmp));
1623 dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
1624 dtrace_aggregate_f *func, void *arg)
1626 return (dt_aggregate_walk_sorted(dtp, func,
1627 arg, dt_aggregate_varvalcmp));
1631 dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
1632 dtrace_aggregate_f *func, void *arg)
1634 return (dt_aggregate_walk_sorted(dtp, func,
1635 arg, dt_aggregate_keyvarcmp));
1639 dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
1640 dtrace_aggregate_f *func, void *arg)
1642 return (dt_aggregate_walk_sorted(dtp, func,
1643 arg, dt_aggregate_valvarcmp));
1647 dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
1648 dtrace_aggregate_f *func, void *arg)
1650 return (dt_aggregate_walk_sorted(dtp, func,
1651 arg, dt_aggregate_varkeyrevcmp));
1655 dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
1656 dtrace_aggregate_f *func, void *arg)
1658 return (dt_aggregate_walk_sorted(dtp, func,
1659 arg, dt_aggregate_varvalrevcmp));
1663 dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
1664 dtrace_aggregate_f *func, void *arg)
1666 return (dt_aggregate_walk_sorted(dtp, func,
1667 arg, dt_aggregate_keyvarrevcmp));
1671 dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
1672 dtrace_aggregate_f *func, void *arg)
1674 return (dt_aggregate_walk_sorted(dtp, func,
1675 arg, dt_aggregate_valvarrevcmp));
1679 dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
1680 int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
1682 dt_aggregate_t *agp = &dtp->dt_aggregate;
1683 dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
1684 const dtrace_aggdata_t **data;
1685 dt_ahashent_t *zaggdata = NULL;
1686 dt_ahash_t *hash = &agp->dtat_hash;
1687 size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
1688 dtrace_aggvarid_t max = 0, aggvar;
1689 int rval = -1, *map, *remap = NULL;
1691 dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
1694 * If the sorting position is greater than the number of aggregation
1695 * variable IDs, we silently set it to 0.
1697 if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
1701 * First we need to translate the specified aggregation variable IDs
1702 * into a linear map that will allow us to translate an aggregation
1703 * variable ID into its position in the specified aggvars.
1705 for (i = 0; i < naggvars; i++) {
1706 if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
1707 return (dt_set_errno(dtp, EDT_BADAGGVAR));
1709 if (aggvars[i] > max)
1713 if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
1716 zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
1718 if (zaggdata == NULL)
1721 for (i = 0; i < naggvars; i++) {
1722 int ndx = i + sortpos;
1724 if (ndx >= naggvars)
1727 aggvar = aggvars[ndx];
1728 assert(aggvar <= max);
1732 * We have an aggregation variable that is present
1733 * more than once in the array of aggregation
1734 * variables. While it's unclear why one might want
1735 * to do this, it's legal. To support this construct,
1736 * we will allocate a remap that will indicate the
1737 * position from which this aggregation variable
1738 * should be pulled. (That is, where the remap will
1739 * map from one position to another.)
1741 if (remap == NULL) {
1742 remap = dt_zalloc(dtp, naggvars * sizeof (int));
1749 * Given that the variable is already present, assert
1750 * that following through the mapping and adjusting
1751 * for the sort position yields the same aggregation
1754 assert(aggvars[(map[aggvar] - 1 + sortpos) %
1755 naggvars] == aggvars[ndx]);
1757 remap[i] = map[aggvar];
1761 map[aggvar] = i + 1;
1765 * We need to take two passes over the data to size our allocation, so
1766 * we'll use the first pass to also fill in the zero-filled data to be
1767 * used to properly format a zero-valued aggregation.
1769 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1770 dtrace_aggvarid_t id;
1773 if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
1776 if (zaggdata[ndx - 1].dtahe_size == 0) {
1777 zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
1778 zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
1784 if (nentries == 0) {
1786 * We couldn't find any entries; there is nothing else to do.
1793 * Before we sort the data, we're going to look for any holes in our
1794 * zero-filled data. This will occur if an aggregation variable that
1795 * we are being asked to print has not yet been assigned the result of
1796 * any aggregating action for _any_ tuple. The issue becomes that we
1797 * would like a zero value to be printed for all columns for this
1798 * aggregation, but without any record description, we don't know the
1799 * aggregating action that corresponds to the aggregation variable. To
1800 * try to find a match, we're simply going to lookup aggregation IDs
1801 * (which are guaranteed to be contiguous and to start from 1), looking
1802 * for the specified aggregation variable ID. If we find a match,
1803 * we'll use that. If we iterate over all aggregation IDs and don't
1804 * find a match, then we must be an anonymous enabling. (Anonymous
1805 * enablings can't currently derive either aggregation variable IDs or
1806 * aggregation variable names given only an aggregation ID.) In this
1807 * obscure case (anonymous enabling, multiple aggregation printa() with
1808 * some aggregations not represented for any tuple), our defined
1809 * behavior is that the zero will be printed in the format of the first
1810 * aggregation variable that contains any non-zero value.
1812 for (i = 0; i < naggvars; i++) {
1813 if (zaggdata[i].dtahe_size == 0) {
1814 dtrace_aggvarid_t aggvar;
1816 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1817 assert(zaggdata[i].dtahe_data.dtada_data == NULL);
1819 for (j = DTRACE_AGGIDNONE + 1; ; j++) {
1820 dtrace_aggdesc_t *agg;
1821 dtrace_aggdata_t *aggdata;
1823 if (dt_aggid_lookup(dtp, j, &agg) != 0)
1826 if (agg->dtagd_varid != aggvar)
1830 * We have our description -- now we need to
1831 * cons up the zaggdata entry for it.
1833 aggdata = &zaggdata[i].dtahe_data;
1834 aggdata->dtada_size = agg->dtagd_size;
1835 aggdata->dtada_desc = agg;
1836 aggdata->dtada_handle = dtp;
1837 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
1838 &aggdata->dtada_edesc,
1839 &aggdata->dtada_pdesc);
1840 aggdata->dtada_normal = 1;
1841 zaggdata[i].dtahe_hashval = 0;
1842 zaggdata[i].dtahe_size = agg->dtagd_size;
1846 if (zaggdata[i].dtahe_size == 0) {
1850 * We couldn't find this aggregation, meaning
1851 * that we have never seen it before for any
1852 * tuple _and_ this is an anonymous enabling.
1853 * That is, we're in the obscure case outlined
1854 * above. In this case, our defined behavior
1855 * is to format the data in the format of the
1856 * first non-zero aggregation -- of which, of
1857 * course, we know there to be at least one
1858 * (or nentries would have been zero).
1860 for (j = 0; j < naggvars; j++) {
1861 if (zaggdata[j].dtahe_size != 0)
1865 assert(j < naggvars);
1866 zaggdata[i] = zaggdata[j];
1868 data = zaggdata[i].dtahe_data.dtada_data;
1869 assert(data != NULL);
1875 * Now we need to allocate our zero-filled data for use for
1876 * aggregations that don't have a value corresponding to a given key.
1878 for (i = 0; i < naggvars; i++) {
1879 dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
1880 dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
1881 dtrace_recdesc_t *rec;
1885 zsize = zaggdata[i].dtahe_size;
1888 if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
1890 * If we failed to allocated some zero-filled data, we
1891 * need to zero out the remaining dtada_data pointers
1892 * to prevent the wrong data from being freed below.
1894 for (j = i; j < naggvars; j++)
1895 zaggdata[j].dtahe_data.dtada_data = NULL;
1899 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1902 * First, the easy bit. To maintain compatibility with
1903 * consumers that pull the compiler-generated ID out of the
1904 * data, we put that ID at the top of the zero-filled data.
1906 rec = &aggdesc->dtagd_rec[0];
1907 /* LINTED - alignment */
1908 *((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
1910 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1913 * Now for the more complicated part. If (and only if) this
1914 * is an lquantize() aggregating action, zero-filled data is
1915 * not equivalent to an empty record: we must also get the
1916 * parameters for the lquantize().
1918 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1919 if (aggdata->dtada_data != NULL) {
1921 * The easier case here is if we actually have
1922 * some prototype data -- in which case we
1923 * manually dig it out of the aggregation
1926 /* LINTED - alignment */
1927 larg = *((uint64_t *)(aggdata->dtada_data +
1931 * We don't have any prototype data. As a
1932 * result, we know that we _do_ have the
1933 * compiler-generated information. (If this
1934 * were an anonymous enabling, all of our
1935 * zero-filled data would have prototype data
1936 * -- either directly or indirectly.) So as
1937 * gross as it is, we'll grovel around in the
1938 * compiler-generated information to find the
1939 * lquantize() parameters.
1941 dtrace_stmtdesc_t *sdp;
1945 sdp = (dtrace_stmtdesc_t *)(uintptr_t)
1946 aggdesc->dtagd_rec[0].dtrd_uarg;
1947 aid = sdp->dtsd_aggdata;
1948 isp = (dt_idsig_t *)aid->di_data;
1949 assert(isp->dis_auxinfo != 0);
1950 larg = isp->dis_auxinfo;
1953 /* LINTED - alignment */
1954 *((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
1957 aggdata->dtada_data = zdata;
1961 * Now that we've dealt with setting up our zero-filled data, we can
1962 * allocate our sorted array, and take another pass over the data to
1965 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1970 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
1971 dtrace_aggvarid_t id;
1973 if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
1979 assert(i == nentries);
1982 * We've loaded our array; now we need to sort by value to allow us
1983 * to create bundles of like value. We're going to acquire the
1984 * dt_qsort_lock here, and hold it across all of our subsequent
1985 * comparison and sorting.
1987 (void) pthread_mutex_lock(&dt_qsort_lock);
1989 qsort(sorted, nentries, sizeof (dt_ahashent_t *),
1990 dt_aggregate_keyvarcmp);
1993 * Now we need to go through and create bundles. Because the number
1994 * of bundles is bounded by the size of the sorted array, we're going
1995 * to reuse the underlying storage. And note that "bundle" is an
1996 * array of pointers to arrays of pointers to dt_ahashent_t -- making
1997 * its type (regrettably) "dt_ahashent_t ***". (Regrettable because
1998 * '*' -- like '_' and 'X' -- should never appear in triplicate in
2001 bundle = (dt_ahashent_t ***)sorted;
2003 for (i = 1, start = 0; i <= nentries; i++) {
2005 dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
2009 * We have a bundle boundary. Everything from start to
2010 * (i - 1) belongs in one bundle.
2012 assert(i - start <= naggvars);
2013 bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
2015 if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
2016 (void) pthread_mutex_unlock(&dt_qsort_lock);
2020 for (j = start; j < i; j++) {
2021 dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
2024 assert(map[id] != 0);
2025 assert(map[id] - 1 < naggvars);
2026 assert(nbundle[map[id] - 1] == NULL);
2027 nbundle[map[id] - 1] = sorted[j];
2029 if (nbundle[naggvars] == NULL)
2030 nbundle[naggvars] = sorted[j];
2033 for (j = 0; j < naggvars; j++) {
2034 if (nbundle[j] != NULL)
2038 * Before we assume that this aggregation variable
2039 * isn't present (and fall back to using the
2040 * zero-filled data allocated earlier), check the
2041 * remap. If we have a remapping, we'll drop it in
2042 * here. Note that we might be remapping an
2043 * aggregation variable that isn't present for this
2044 * key; in this case, the aggregation data that we
2045 * copy will point to the zeroed data.
2047 if (remap != NULL && remap[j]) {
2048 assert(remap[j] - 1 < j);
2049 assert(nbundle[remap[j] - 1] != NULL);
2050 nbundle[j] = nbundle[remap[j] - 1];
2052 nbundle[j] = &zaggdata[j];
2056 bundle[nbundles++] = nbundle;
2061 * Now we need to re-sort based on the first value.
2063 dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
2064 dt_aggregate_bundlecmp);
2066 (void) pthread_mutex_unlock(&dt_qsort_lock);
2069 * We're done! Now we just need to go back over the sorted bundles,
2070 * calling the function.
2072 data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
2074 for (i = 0; i < nbundles; i++) {
2075 for (j = 0; j < naggvars; j++)
2078 for (j = 0; j < naggvars; j++) {
2079 int ndx = j - sortpos;
2084 assert(bundle[i][ndx] != NULL);
2085 data[j + 1] = &bundle[i][ndx]->dtahe_data;
2088 for (j = 0; j < naggvars; j++)
2089 assert(data[j + 1] != NULL);
2092 * The representative key is the last element in the bundle.
2093 * Assert that we have one, and then set it to be the first
2096 assert(bundle[i][j] != NULL);
2097 data[0] = &bundle[i][j]->dtahe_data;
2099 if ((rval = func(data, naggvars + 1, arg)) == -1)
2105 for (i = 0; i < nbundles; i++)
2106 dt_free(dtp, bundle[i]);
2108 if (zaggdata != NULL) {
2109 for (i = 0; i < naggvars; i++)
2110 dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
2113 dt_free(dtp, zaggdata);
2114 dt_free(dtp, sorted);
2115 dt_free(dtp, remap);
2122 dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
2123 dtrace_aggregate_walk_f *func)
2125 dt_print_aggdata_t pd;
2127 bzero(&pd, sizeof (pd));
2131 pd.dtpa_allunprint = 1;
2134 func = dtrace_aggregate_walk_sorted;
2136 if (dtp->dt_oformat) {
2137 if ((*func)(dtp, dt_format_agg, &pd) == -1)
2138 return (dt_set_errno(dtp, dtp->dt_errno));
2140 if ((*func)(dtp, dt_print_agg, &pd) == -1)
2141 return (dt_set_errno(dtp, dtp->dt_errno));
2148 dtrace_aggregate_clear(dtrace_hdl_t *dtp)
2150 dt_aggregate_t *agp = &dtp->dt_aggregate;
2151 dt_ahash_t *hash = &agp->dtat_hash;
2153 dtrace_aggdata_t *data;
2154 dtrace_aggdesc_t *aggdesc;
2155 dtrace_recdesc_t *rec;
2156 int i, max_cpus = agp->dtat_maxcpu;
2158 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
2159 aggdesc = h->dtahe_data.dtada_desc;
2160 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
2161 data = &h->dtahe_data;
2163 bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
2165 if (data->dtada_percpu == NULL)
2168 for (i = 0; i < max_cpus; i++)
2169 bzero(data->dtada_percpu[i], rec->dtrd_size);
2174 dt_aggregate_destroy(dtrace_hdl_t *dtp)
2176 dt_aggregate_t *agp = &dtp->dt_aggregate;
2177 dt_ahash_t *hash = &agp->dtat_hash;
2178 dt_ahashent_t *h, *next;
2179 dtrace_aggdata_t *aggdata;
2180 int i, max_cpus = agp->dtat_maxcpu;
2182 if (hash->dtah_hash == NULL) {
2183 assert(hash->dtah_all == NULL);
2185 free(hash->dtah_hash);
2187 for (h = hash->dtah_all; h != NULL; h = next) {
2188 next = h->dtahe_nextall;
2190 aggdata = &h->dtahe_data;
2192 if (aggdata->dtada_percpu != NULL) {
2193 for (i = 0; i < max_cpus; i++)
2194 free(aggdata->dtada_percpu[i]);
2195 free(aggdata->dtada_percpu);
2198 free(aggdata->dtada_data);
2202 hash->dtah_hash = NULL;
2203 hash->dtah_all = NULL;
2204 hash->dtah_size = 0;
2207 free(agp->dtat_buf.dtbd_data);
2208 free(agp->dtat_cpus);