2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdlib.h>
2N/A#include <strings.h>
2N/A#include <errno.h>
2N/A#include <unistd.h>
2N/A#include <dt_impl.h>
2N/A#include <assert.h>
2N/A#include <alloca.h>
2N/A#include <limits.h>
2N/A
2N/Astatic size_t dtrace_ahashsize[] = {
2N/A 32779,
2N/A 500009,
2N/A 2104987,
2N/A 4000037,
2N/A 7954979
2N/A };
2N/A
2N/A#define DTRACE_NUM_AHASHSIZE \
2N/A (sizeof (dtrace_ahashsize) / sizeof (dtrace_ahashsize[0]))
2N/A
2N/A#define LSHIFT 4
2N/A#define RSHIFT (sizeof (caddr_t) * 8 - LSHIFT)
2N/A/*
2N/A * Rotate the hash by LSHIFT bits, then XOR the next word.
2N/A * hashval must be an unsigned type.
2N/A */
2N/A#define DTRACE_AGG_HASH(hashval, addr, roffs) \
2N/A { \
2N/A hashval = (hashval << LSHIFT) | (hashval >> RSHIFT); \
2N/A hashval = hashval ^ ((unsigned long) addr[roffs]); \
2N/A }
2N/A
2N/A/*
2N/A * Because qsort(3C) does not allow an argument to be passed to a comparison
2N/A * function, the variables that affect comparison must regrettably be global;
2N/A * they are protected by a global static lock, dt_qsort_lock.
2N/A */
2N/Astatic pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
2N/A
2N/Astatic int dt_revsort;
2N/Astatic int dt_keysort;
2N/Astatic int dt_keypos;
2N/A
2N/A#define DT_LESSTHAN (dt_revsort == 0 ? -1 : 1)
2N/A#define DT_GREATERTHAN (dt_revsort == 0 ? 1 : -1)
2N/A
2N/Astatic void
2N/Adt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
2N/A{
2N/A int i;
2N/A
2N/A for (i = 0; i < size / sizeof (int64_t); i++)
2N/A existing[i] = existing[i] + new[i];
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
2N/A{
2N/A int64_t lvar = *lhs;
2N/A int64_t rvar = *rhs;
2N/A
2N/A if (lvar < rvar)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lvar > rvar)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Adt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
2N/A{
2N/A if (*new < *existing)
2N/A *existing = *new;
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Adt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
2N/A{
2N/A if (*new > *existing)
2N/A *existing = *new;
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
2N/A{
2N/A int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
2N/A int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
2N/A
2N/A if (lavg < ravg)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lavg > ravg)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
2N/A{
2N/A uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
2N/A uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
2N/A
2N/A if (lsd < rsd)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lsd > rsd)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Adt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
2N/A{
2N/A int64_t arg = *existing++;
2N/A uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
2N/A int i;
2N/A
2N/A for (i = 0; i <= levels + 1; i++)
2N/A existing[i] = existing[i] + new[i + 1];
2N/A}
2N/A
2N/Astatic long double
2N/Adt_aggregate_lquantizedsum(int64_t *lquanta)
2N/A{
2N/A int64_t arg = *lquanta++;
2N/A int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2N/A uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2N/A uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
2N/A long double total = (long double)lquanta[0] * (long double)(base - 1);
2N/A
2N/A for (i = 0; i < levels; base += step, i++)
2N/A total += (long double)lquanta[i + 1] * (long double)base;
2N/A
2N/A return (total + (long double)lquanta[levels + 1] *
2N/A (long double)(base + 1));
2N/A}
2N/A
2N/Astatic int64_t
2N/Adt_aggregate_lquantizedzero(int64_t *lquanta)
2N/A{
2N/A int64_t arg = *lquanta++;
2N/A int32_t base = DTRACE_LQUANTIZE_BASE(arg);
2N/A uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
2N/A uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
2N/A
2N/A if (base - 1 == 0)
2N/A return (lquanta[0]);
2N/A
2N/A for (i = 0; i < levels; base += step, i++) {
2N/A if (base != 0)
2N/A continue;
2N/A
2N/A return (lquanta[i + 1]);
2N/A }
2N/A
2N/A if (base + 1 == 0)
2N/A return (lquanta[levels + 1]);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
2N/A{
2N/A long double lsum = dt_aggregate_lquantizedsum(lhs);
2N/A long double rsum = dt_aggregate_lquantizedsum(rhs);
2N/A int64_t lzero, rzero;
2N/A
2N/A if (lsum < rsum)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lsum > rsum)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A /*
2N/A * If they're both equal, then we will compare based on the weights at
2N/A * zero. If the weights at zero are equal (or if zero is not within
2N/A * the range of the linear quantization), then this will be judged a
2N/A * tie and will be resolved based on the key comparison.
2N/A */
2N/A lzero = dt_aggregate_lquantizedzero(lhs);
2N/A rzero = dt_aggregate_lquantizedzero(rhs);
2N/A
2N/A if (lzero < rzero)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lzero > rzero)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
2N/A{
2N/A int nbuckets = DTRACE_QUANTIZE_NBUCKETS, i;
2N/A long double ltotal = 0, rtotal = 0;
2N/A int64_t lzero, rzero;
2N/A
2N/A for (i = 0; i < nbuckets; i++) {
2N/A int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
2N/A
2N/A if (bucketval == 0) {
2N/A lzero = lhs[i];
2N/A rzero = rhs[i];
2N/A }
2N/A
2N/A ltotal += (long double)bucketval * (long double)lhs[i];
2N/A rtotal += (long double)bucketval * (long double)rhs[i];
2N/A }
2N/A
2N/A if (ltotal < rtotal)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (ltotal > rtotal)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A /*
2N/A * If they're both equal, then we will compare based on the weights at
2N/A * zero. If the weights at zero are equal, then this will be judged a
2N/A * tie and will be resolved based on the key comparison.
2N/A */
2N/A if (lzero < rzero)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lzero > rzero)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic void
2N/Adt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
2N/A{
2N/A uint64_t pid = data[0];
2N/A uint64_t *pc = &data[1];
2N/A struct ps_prochandle *P;
2N/A GElf_Sym sym;
2N/A
2N/A if (dtp->dt_vector != NULL)
2N/A return;
2N/A
2N/A if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
2N/A return;
2N/A
2N/A dt_proc_lock(dtp, P);
2N/A
2N/A if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
2N/A *pc = sym.st_value;
2N/A
2N/A dt_proc_unlock(dtp, P);
2N/A dt_proc_release(dtp, P);
2N/A}
2N/A
2N/Astatic void
2N/Adt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
2N/A{
2N/A uint64_t pid = data[0];
2N/A uint64_t *pc = &data[1];
2N/A struct ps_prochandle *P;
2N/A const prmap_t *map;
2N/A
2N/A if (dtp->dt_vector != NULL)
2N/A return;
2N/A
2N/A if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
2N/A return;
2N/A
2N/A dt_proc_lock(dtp, P);
2N/A
2N/A if ((map = Paddr_to_map(P, *pc)) != NULL)
2N/A *pc = map->pr_vaddr;
2N/A
2N/A dt_proc_unlock(dtp, P);
2N/A dt_proc_release(dtp, P);
2N/A}
2N/A
2N/Astatic void
2N/Adt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
2N/A{
2N/A GElf_Sym sym;
2N/A uint64_t *pc = data;
2N/A
2N/A if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
2N/A *pc = sym.st_value;
2N/A}
2N/A
2N/Astatic void
2N/Adt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
2N/A{
2N/A uint64_t *pc = data;
2N/A dt_module_t *dmp;
2N/A
2N/A if (dtp->dt_vector != NULL) {
2N/A /*
2N/A * We don't have a way of just getting the module for a
2N/A * vectored open, and it doesn't seem to be worth defining
2N/A * one. This means that use of mod() won't get true
2N/A * aggregation in the postmortem case (some modules may
2N/A * appear more than once in aggregation output). It seems
2N/A * unlikely that anyone will ever notice or care...
2N/A */
2N/A return;
2N/A }
2N/A
2N/A for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
2N/A dmp = dt_list_next(dmp)) {
2N/A if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
2N/A *pc = dmp->dm_text_va;
2N/A return;
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic dtrace_aggvarid_t
2N/Adt_aggregate_aggvarid(dt_ahashent_t *ent)
2N/A{
2N/A dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
2N/A caddr_t data = ent->dtahe_data.dtada_data;
2N/A dtrace_recdesc_t *rec = agg->dtagd_rec;
2N/A
2N/A /*
2N/A * First, we'll check the variable ID in the aggdesc. If it's valid,
2N/A * we'll return it. If not, we'll use the compiler-generated ID
2N/A * present as the first record.
2N/A */
2N/A if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
2N/A return (agg->dtagd_varid);
2N/A
2N/A agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
2N/A rec->dtrd_offset));
2N/A
2N/A return (agg->dtagd_varid);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Adt_aggregate_expand_hash(dtrace_hdl_t *dtp, int *allocation_failed)
2N/A
2N/A{
2N/A int i;
2N/A size_t size, old_size, new_size, ndx;
2N/A dt_ahash_t new_hash;
2N/A dt_ahashent_t *h, *moving;
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A
2N/A /* Check to see if we're at the maximum size */
2N/A if ((hash->dtah_sizendx + 1) >= DTRACE_NUM_AHASHSIZE) {
2N/A *allocation_failed = 1;
2N/A return;
2N/A }
2N/A
2N/A /* First, allocate the new array; if we fail, we will not resize. */
2N/A
2N/A bzero(&new_hash, sizeof (dt_ahash_t));
2N/A old_size = dtrace_ahashsize[hash->dtah_sizendx];
2N/A hash->dtah_sizendx++;
2N/A new_size = new_hash.dtah_size = dtrace_ahashsize[hash->dtah_sizendx];
2N/A size = new_size * sizeof (dt_ahashent_t *);
2N/A
2N/A if ((new_hash.dtah_hash = malloc(size)) == NULL) {
2N/A hash->dtah_sizendx--;
2N/A *allocation_failed = 1;
2N/A return;
2N/A }
2N/A
2N/A bzero(new_hash.dtah_hash, size);
2N/A for (i = 0; i < old_size; i++) {
2N/A for (h = hash->dtah_hash[i]; h != NULL; ) {
2N/A moving = h;
2N/A h = h->dtahe_next;
2N/A moving->dtahe_prev = NULL;
2N/A ndx = moving->dtahe_hashval % new_size;
2N/A if (new_hash.dtah_hash[ndx] != NULL)
2N/A new_hash.dtah_hash[ndx]->dtahe_prev = moving;
2N/A moving->dtahe_next = new_hash.dtah_hash[ndx];
2N/A new_hash.dtah_hash[ndx] = moving;
2N/A
2N/A if (new_hash.dtah_all != NULL)
2N/A new_hash.dtah_all->dtahe_prevall = moving;
2N/A
2N/A moving->dtahe_nextall = new_hash.dtah_all;
2N/A new_hash.dtah_all = moving;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * All moved, now update hash info.
2N/A */
2N/A hash->dtah_size = new_hash.dtah_size;
2N/A free(hash->dtah_hash);
2N/A hash->dtah_hash = new_hash.dtah_hash;
2N/A hash->dtah_all = new_hash.dtah_all;
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
2N/A{
2N/A dtrace_epid_t id;
2N/A uint64_t hashval;
2N/A size_t offs, roffs, size, ndx;
2N/A int i, j, rval;
2N/A caddr_t addr, data;
2N/A dtrace_recdesc_t *rec;
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dtrace_aggdesc_t *agg;
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
2N/A dtrace_aggdata_t *aggdata;
2N/A dt_ahashent_t *h;
2N/A int flags = agp->dtat_flags;
2N/A int allocation_failed = 0;
2N/A
2N/A buf->dtbd_cpu = cpu;
2N/A
2N/A if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
2N/A if (errno == ENOENT) {
2N/A /*
2N/A * If that failed with ENOENT, it may be because the
2N/A * CPU was unconfigured. This is okay; we'll just
2N/A * do nothing but return success.
2N/A */
2N/A return (0);
2N/A }
2N/A
2N/A return (dt_set_errno(dtp, errno));
2N/A }
2N/A
2N/A if (buf->dtbd_drops != 0) {
2N/A if (dt_handle_cpudrop(dtp, cpu,
2N/A DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1)
2N/A return (-1);
2N/A }
2N/A
2N/A if (buf->dtbd_size == 0)
2N/A return (0);
2N/A
2N/A if (hash->dtah_hash == NULL) {
2N/A size_t size;
2N/A
2N/A hash->dtah_size = dtrace_ahashsize[hash->dtah_sizendx];
2N/A size = hash->dtah_size * sizeof (dt_ahashent_t *);
2N/A
2N/A if ((hash->dtah_hash = malloc(size)) == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A bzero(hash->dtah_hash, size);
2N/A }
2N/A
2N/A for (offs = 0; offs < buf->dtbd_size; ) {
2N/A /*
2N/A * We're guaranteed to have an ID.
2N/A */
2N/A id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
2N/A (uintptr_t)offs));
2N/A
2N/A if (id == DTRACE_AGGIDNONE) {
2N/A /*
2N/A * This is filler to assure proper alignment of the
2N/A * next record; we simply ignore it.
2N/A */
2N/A offs += sizeof (id);
2N/A continue;
2N/A }
2N/A
2N/A if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
2N/A return (rval);
2N/A
2N/A addr = buf->dtbd_data + offs;
2N/A size = agg->dtagd_size;
2N/A hashval = 0;
2N/A
2N/A for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
2N/A rec = &agg->dtagd_rec[j];
2N/A roffs = rec->dtrd_offset;
2N/A
2N/A switch (rec->dtrd_action) {
2N/A case DTRACEACT_USYM:
2N/A dt_aggregate_usym(dtp,
2N/A /* LINTED - alignment */
2N/A (uint64_t *)&addr[roffs]);
2N/A break;
2N/A
2N/A case DTRACEACT_UMOD:
2N/A dt_aggregate_umod(dtp,
2N/A /* LINTED - alignment */
2N/A (uint64_t *)&addr[roffs]);
2N/A break;
2N/A
2N/A case DTRACEACT_SYM:
2N/A /* LINTED - alignment */
2N/A dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
2N/A break;
2N/A
2N/A case DTRACEACT_MOD:
2N/A /* LINTED - alignment */
2N/A dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
2N/A break;
2N/A
2N/A default:
2N/A break;
2N/A }
2N/A
2N/A for (i = 0; i < rec->dtrd_size; i++) {
2N/A DTRACE_AGG_HASH(hashval, addr, (roffs+i));
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * For locks (lockstat) things are int aligned, so we
2N/A * are not using the entire hash.
2N/A */
2N/A
2N/A ndx = hashval % dtrace_ahashsize[hash->dtah_sizendx];
2N/A
2N/A for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
2N/A if (h->dtahe_hashval != hashval)
2N/A continue;
2N/A
2N/A if (h->dtahe_size != size)
2N/A continue;
2N/A
2N/A aggdata = &h->dtahe_data;
2N/A data = aggdata->dtada_data;
2N/A
2N/A for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
2N/A rec = &agg->dtagd_rec[j];
2N/A roffs = rec->dtrd_offset;
2N/A
2N/A for (i = 0; i < rec->dtrd_size; i++) {
2N/A if (addr[roffs + i] != data[roffs + i])
2N/A goto hashnext;
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * We found it. Now we need to apply the aggregating
2N/A * action on the data here.
2N/A */
2N/A rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
2N/A roffs = rec->dtrd_offset;
2N/A /* LINTED - alignment */
2N/A h->dtahe_aggregate((int64_t *)&data[roffs],
2N/A /* LINTED - alignment */
2N/A (int64_t *)&addr[roffs], rec->dtrd_size);
2N/A
2N/A /*
2N/A * If we're keeping per CPU data, apply the aggregating
2N/A * action there as well.
2N/A */
2N/A if (aggdata->dtada_percpu != NULL) {
2N/A data = aggdata->dtada_percpu[cpu];
2N/A
2N/A /* LINTED - alignment */
2N/A h->dtahe_aggregate((int64_t *)data,
2N/A /* LINTED - alignment */
2N/A (int64_t *)&addr[roffs], rec->dtrd_size);
2N/A }
2N/A
2N/A goto bufnext;
2N/Ahashnext:
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * No entry for this record -- allocate a new one. Before doing
2N/A * so, see if we should grow the hash. (If it cannot be grown,
2N/A * just add the entry to the existing hash.)
2N/A */
2N/A if ((hash->dtah_nent
2N/A > (dtrace_ahashsize[hash->dtah_sizendx]*2)) &&
2N/A !allocation_failed)
2N/A dt_aggregate_expand_hash(dtp, &allocation_failed);
2N/A
2N/A if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A bzero(h, sizeof (dt_ahashent_t));
2N/A aggdata = &h->dtahe_data;
2N/A
2N/A if ((aggdata->dtada_data = malloc(size)) == NULL) {
2N/A free(h);
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A }
2N/A
2N/A bcopy(addr, aggdata->dtada_data, size);
2N/A aggdata->dtada_size = size;
2N/A aggdata->dtada_desc = agg;
2N/A aggdata->dtada_handle = dtp;
2N/A (void) dt_epid_lookup(dtp, agg->dtagd_epid,
2N/A &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
2N/A aggdata->dtada_normal = 1;
2N/A
2N/A h->dtahe_hashval = hashval;
2N/A h->dtahe_size = size;
2N/A (void) dt_aggregate_aggvarid(h);
2N/A
2N/A rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
2N/A
2N/A if (flags & DTRACE_A_PERCPU) {
2N/A int max_cpus = agp->dtat_maxcpu;
2N/A caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
2N/A
2N/A if (percpu == NULL) {
2N/A free(aggdata->dtada_data);
2N/A free(h);
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A }
2N/A
2N/A for (j = 0; j < max_cpus; j++) {
2N/A percpu[j] = malloc(rec->dtrd_size);
2N/A
2N/A if (percpu[j] == NULL) {
2N/A while (--j >= 0)
2N/A free(percpu[j]);
2N/A
2N/A free(aggdata->dtada_data);
2N/A free(h);
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A }
2N/A
2N/A if (j == cpu) {
2N/A bcopy(&addr[rec->dtrd_offset],
2N/A percpu[j], rec->dtrd_size);
2N/A } else {
2N/A bzero(percpu[j], rec->dtrd_size);
2N/A }
2N/A }
2N/A
2N/A aggdata->dtada_percpu = percpu;
2N/A }
2N/A
2N/A switch (rec->dtrd_action) {
2N/A case DTRACEAGG_MIN:
2N/A h->dtahe_aggregate = dt_aggregate_min;
2N/A break;
2N/A
2N/A case DTRACEAGG_MAX:
2N/A h->dtahe_aggregate = dt_aggregate_max;
2N/A break;
2N/A
2N/A case DTRACEAGG_LQUANTIZE:
2N/A h->dtahe_aggregate = dt_aggregate_lquantize;
2N/A break;
2N/A
2N/A case DTRACEAGG_COUNT:
2N/A case DTRACEAGG_SUM:
2N/A case DTRACEAGG_AVG:
2N/A case DTRACEAGG_STDDEV:
2N/A case DTRACEAGG_QUANTIZE:
2N/A h->dtahe_aggregate = dt_aggregate_count;
2N/A break;
2N/A
2N/A default:
2N/A return (dt_set_errno(dtp, EDT_BADAGG));
2N/A }
2N/A
2N/A ndx = h->dtahe_hashval % hash->dtah_size;
2N/A if (hash->dtah_hash[ndx] != NULL)
2N/A hash->dtah_hash[ndx]->dtahe_prev = h;
2N/A
2N/A h->dtahe_next = hash->dtah_hash[ndx];
2N/A hash->dtah_hash[ndx] = h;
2N/A
2N/A if (hash->dtah_all != NULL)
2N/A hash->dtah_all->dtahe_prevall = h;
2N/A
2N/A h->dtahe_nextall = hash->dtah_all;
2N/A hash->dtah_all = h;
2N/A
2N/A hash->dtah_nent++;
2N/Abufnext:
2N/A offs += agg->dtagd_size;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_snap(dtrace_hdl_t *dtp)
2N/A{
2N/A int i, rval;
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A hrtime_t now = gethrtime();
2N/A dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
2N/A
2N/A if (dtp->dt_lastagg != 0) {
2N/A if (now - dtp->dt_lastagg < interval)
2N/A return (0);
2N/A
2N/A dtp->dt_lastagg += interval;
2N/A } else {
2N/A dtp->dt_lastagg = now;
2N/A }
2N/A
2N/A if (!dtp->dt_active)
2N/A return (dt_set_errno(dtp, EINVAL));
2N/A
2N/A if (agp->dtat_buf.dtbd_size == 0)
2N/A return (0);
2N/A
2N/A for (i = 0; i < agp->dtat_ncpus; i++) {
2N/A if (rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i]))
2N/A return (rval);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_hashcmp(const void *lhs, const void *rhs)
2N/A{
2N/A dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
2N/A dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
2N/A dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
2N/A dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
2N/A
2N/A if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_varcmp(const void *lhs, const void *rhs)
2N/A{
2N/A dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
2N/A dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
2N/A dtrace_aggvarid_t lid, rid;
2N/A
2N/A lid = dt_aggregate_aggvarid(lh);
2N/A rid = dt_aggregate_aggvarid(rh);
2N/A
2N/A if (lid < rid)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lid > rid)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_keycmp(const void *lhs, const void *rhs)
2N/A{
2N/A dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
2N/A dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
2N/A dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
2N/A dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
2N/A dtrace_recdesc_t *lrec, *rrec;
2N/A char *ldata, *rdata;
2N/A int rval, i, j, keypos, nrecs;
2N/A
2N/A if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A nrecs = lagg->dtagd_nrecs - 1;
2N/A assert(nrecs == ragg->dtagd_nrecs - 1);
2N/A
2N/A keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
2N/A
2N/A for (i = 1; i < nrecs; i++) {
2N/A uint64_t lval, rval;
2N/A int ndx = i + keypos;
2N/A
2N/A if (ndx >= nrecs)
2N/A ndx = ndx - nrecs + 1;
2N/A
2N/A lrec = &lagg->dtagd_rec[ndx];
2N/A rrec = &ragg->dtagd_rec[ndx];
2N/A
2N/A ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
2N/A rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
2N/A
2N/A if (lrec->dtrd_size < rrec->dtrd_size)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lrec->dtrd_size > rrec->dtrd_size)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A switch (lrec->dtrd_size) {
2N/A case sizeof (uint64_t):
2N/A /* LINTED - alignment */
2N/A lval = *((uint64_t *)ldata);
2N/A /* LINTED - alignment */
2N/A rval = *((uint64_t *)rdata);
2N/A break;
2N/A
2N/A case sizeof (uint32_t):
2N/A /* LINTED - alignment */
2N/A lval = *((uint32_t *)ldata);
2N/A /* LINTED - alignment */
2N/A rval = *((uint32_t *)rdata);
2N/A break;
2N/A
2N/A case sizeof (uint16_t):
2N/A /* LINTED - alignment */
2N/A lval = *((uint16_t *)ldata);
2N/A /* LINTED - alignment */
2N/A rval = *((uint16_t *)rdata);
2N/A break;
2N/A
2N/A case sizeof (uint8_t):
2N/A lval = *((uint8_t *)ldata);
2N/A rval = *((uint8_t *)rdata);
2N/A break;
2N/A
2N/A default:
2N/A switch (lrec->dtrd_action) {
2N/A case DTRACEACT_UMOD:
2N/A case DTRACEACT_UADDR:
2N/A case DTRACEACT_USYM:
2N/A for (j = 0; j < 2; j++) {
2N/A /* LINTED - alignment */
2N/A lval = ((uint64_t *)ldata)[j];
2N/A /* LINTED - alignment */
2N/A rval = ((uint64_t *)rdata)[j];
2N/A
2N/A if (lval < rval)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lval > rval)
2N/A return (DT_GREATERTHAN);
2N/A }
2N/A
2N/A break;
2N/A
2N/A default:
2N/A for (j = 0; j < lrec->dtrd_size; j++) {
2N/A lval = ((uint8_t *)ldata)[j];
2N/A rval = ((uint8_t *)rdata)[j];
2N/A
2N/A if (lval < rval)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lval > rval)
2N/A return (DT_GREATERTHAN);
2N/A }
2N/A }
2N/A
2N/A continue;
2N/A }
2N/A
2N/A if (lval < rval)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lval > rval)
2N/A return (DT_GREATERTHAN);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_valcmp(const void *lhs, const void *rhs)
2N/A{
2N/A dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
2N/A dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
2N/A dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
2N/A dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
2N/A caddr_t ldata = lh->dtahe_data.dtada_data;
2N/A caddr_t rdata = rh->dtahe_data.dtada_data;
2N/A dtrace_recdesc_t *lrec, *rrec;
2N/A int64_t *laddr, *raddr;
2N/A int rval, i;
2N/A
2N/A if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
2N/A return (DT_LESSTHAN);
2N/A
2N/A for (i = 0; i < lagg->dtagd_nrecs; i++) {
2N/A lrec = &lagg->dtagd_rec[i];
2N/A rrec = &ragg->dtagd_rec[i];
2N/A
2N/A if (lrec->dtrd_offset < rrec->dtrd_offset)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lrec->dtrd_offset > rrec->dtrd_offset)
2N/A return (DT_GREATERTHAN);
2N/A
2N/A if (lrec->dtrd_action < rrec->dtrd_action)
2N/A return (DT_LESSTHAN);
2N/A
2N/A if (lrec->dtrd_action > rrec->dtrd_action)
2N/A return (DT_GREATERTHAN);
2N/A }
2N/A
2N/A laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
2N/A raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
2N/A
2N/A switch (lrec->dtrd_action) {
2N/A case DTRACEAGG_AVG:
2N/A rval = dt_aggregate_averagecmp(laddr, raddr);
2N/A break;
2N/A
2N/A case DTRACEAGG_STDDEV:
2N/A rval = dt_aggregate_stddevcmp(laddr, raddr);
2N/A break;
2N/A
2N/A case DTRACEAGG_QUANTIZE:
2N/A rval = dt_aggregate_quantizedcmp(laddr, raddr);
2N/A break;
2N/A
2N/A case DTRACEAGG_LQUANTIZE:
2N/A rval = dt_aggregate_lquantizedcmp(laddr, raddr);
2N/A break;
2N/A
2N/A case DTRACEAGG_COUNT:
2N/A case DTRACEAGG_SUM:
2N/A case DTRACEAGG_MIN:
2N/A case DTRACEAGG_MAX:
2N/A rval = dt_aggregate_countcmp(laddr, raddr);
2N/A break;
2N/A
2N/A default:
2N/A assert(0);
2N/A }
2N/A
2N/A return (rval);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_valkeycmp(const void *lhs, const void *rhs)
2N/A{
2N/A int rval;
2N/A
2N/A if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A /*
2N/A * If we're here, the values for the two aggregation elements are
2N/A * equal. We already know that the key layout is the same for the two
2N/A * elements; we must now compare the keys themselves as a tie-breaker.
2N/A */
2N/A return (dt_aggregate_keycmp(lhs, rhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
2N/A{
2N/A int rval;
2N/A
2N/A if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A return (dt_aggregate_varcmp(lhs, rhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_varkeycmp(const void *lhs, const void *rhs)
2N/A{
2N/A int rval;
2N/A
2N/A if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A return (dt_aggregate_keycmp(lhs, rhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_valvarcmp(const void *lhs, const void *rhs)
2N/A{
2N/A int rval;
2N/A
2N/A if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A return (dt_aggregate_varcmp(lhs, rhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_varvalcmp(const void *lhs, const void *rhs)
2N/A{
2N/A int rval;
2N/A
2N/A if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
2N/A return (rval);
2N/A
2N/A return (dt_aggregate_valkeycmp(lhs, rhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
2N/A{
2N/A return (dt_aggregate_keyvarcmp(rhs, lhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
2N/A{
2N/A return (dt_aggregate_varkeycmp(rhs, lhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
2N/A{
2N/A return (dt_aggregate_valvarcmp(rhs, lhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
2N/A{
2N/A return (dt_aggregate_varvalcmp(rhs, lhs));
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_bundlecmp(const void *lhs, const void *rhs)
2N/A{
2N/A dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
2N/A dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
2N/A int i, rval;
2N/A
2N/A if (dt_keysort) {
2N/A /*
2N/A * If we're sorting on keys, we need to scan until we find the
2N/A * last entry -- that's the representative key. (The order of
2N/A * the bundle is values followed by key to accommodate the
2N/A * default behavior of sorting by value.) If the keys are
2N/A * equal, we'll fall into the value comparison loop, below.
2N/A */
2N/A for (i = 0; lh[i + 1] != NULL; i++)
2N/A continue;
2N/A
2N/A assert(i != 0);
2N/A assert(rh[i + 1] == NULL);
2N/A
2N/A if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
2N/A return (rval);
2N/A }
2N/A
2N/A for (i = 0; ; i++) {
2N/A if (lh[i + 1] == NULL) {
2N/A /*
2N/A * All of the values are equal; if we're sorting on
2N/A * keys, then we're only here because the keys were
2N/A * found to be equal and these records are therefore
2N/A * equal. If we're not sorting on keys, we'll use the
2N/A * key comparison from the representative key as the
2N/A * tie-breaker.
2N/A */
2N/A if (dt_keysort)
2N/A return (0);
2N/A
2N/A assert(i != 0);
2N/A assert(rh[i + 1] == NULL);
2N/A return (dt_aggregate_keycmp(&lh[i], &rh[i]));
2N/A } else {
2N/A if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
2N/A return (rval);
2N/A }
2N/A }
2N/A}
2N/A
2N/Aint
2N/Adt_aggregate_go(dtrace_hdl_t *dtp)
2N/A{
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dtrace_optval_t size, cpu;
2N/A dtrace_bufdesc_t *buf = &agp->dtat_buf;
2N/A int rval, i;
2N/A
2N/A assert(agp->dtat_maxcpu == 0);
2N/A assert(agp->dtat_ncpu == 0);
2N/A assert(agp->dtat_cpus == NULL);
2N/A
2N/A agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2N/A agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
2N/A agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
2N/A
2N/A if (agp->dtat_cpus == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A
2N/A /*
2N/A * Use the aggregation buffer size as reloaded from the kernel.
2N/A */
2N/A size = dtp->dt_options[DTRACEOPT_AGGSIZE];
2N/A
2N/A rval = dtrace_getopt(dtp, "aggsize", &size);
2N/A assert(rval == 0);
2N/A
2N/A if (size == 0 || size == DTRACEOPT_UNSET)
2N/A return (0);
2N/A
2N/A buf = &agp->dtat_buf;
2N/A buf->dtbd_size = size;
2N/A
2N/A if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
2N/A return (dt_set_errno(dtp, EDT_NOMEM));
2N/A
2N/A /*
2N/A * Now query for the CPUs enabled.
2N/A */
2N/A rval = dtrace_getopt(dtp, "cpu", &cpu);
2N/A assert(rval == 0 && cpu != DTRACEOPT_UNSET);
2N/A
2N/A if (cpu != DTRACE_CPUALL) {
2N/A assert(cpu < agp->dtat_ncpu);
2N/A agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
2N/A
2N/A return (0);
2N/A }
2N/A
2N/A agp->dtat_ncpus = 0;
2N/A for (i = 0; i < agp->dtat_maxcpu; i++) {
2N/A if (dt_status(dtp, i) == -1)
2N/A continue;
2N/A
2N/A agp->dtat_cpus[agp->dtat_ncpus++] = i;
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
2N/A{
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dtrace_aggdata_t *data;
2N/A dtrace_aggdesc_t *aggdesc;
2N/A dtrace_recdesc_t *rec;
2N/A int i;
2N/A
2N/A switch (rval) {
2N/A case DTRACE_AGGWALK_NEXT:
2N/A break;
2N/A
2N/A case DTRACE_AGGWALK_CLEAR: {
2N/A uint32_t size, offs = 0;
2N/A
2N/A aggdesc = h->dtahe_data.dtada_desc;
2N/A rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
2N/A size = rec->dtrd_size;
2N/A data = &h->dtahe_data;
2N/A
2N/A if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
2N/A offs = sizeof (uint64_t);
2N/A size -= sizeof (uint64_t);
2N/A }
2N/A
2N/A bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
2N/A
2N/A if (data->dtada_percpu == NULL)
2N/A break;
2N/A
2N/A for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
2N/A bzero(data->dtada_percpu[i] + offs, size);
2N/A break;
2N/A }
2N/A
2N/A case DTRACE_AGGWALK_ERROR:
2N/A /*
2N/A * We assume that errno is already set in this case.
2N/A */
2N/A return (dt_set_errno(dtp, errno));
2N/A
2N/A case DTRACE_AGGWALK_ABORT:
2N/A return (dt_set_errno(dtp, EDT_DIRABORT));
2N/A
2N/A case DTRACE_AGGWALK_DENORMALIZE:
2N/A h->dtahe_data.dtada_normal = 1;
2N/A return (0);
2N/A
2N/A case DTRACE_AGGWALK_NORMALIZE:
2N/A if (h->dtahe_data.dtada_normal == 0) {
2N/A h->dtahe_data.dtada_normal = 1;
2N/A return (dt_set_errno(dtp, EDT_BADRVAL));
2N/A }
2N/A
2N/A return (0);
2N/A
2N/A case DTRACE_AGGWALK_REMOVE: {
2N/A dtrace_aggdata_t *aggdata = &h->dtahe_data;
2N/A int i, max_cpus = agp->dtat_maxcpu;
2N/A
2N/A /*
2N/A * First, remove this hash entry from its hash chain.
2N/A */
2N/A if (h->dtahe_prev != NULL) {
2N/A h->dtahe_prev->dtahe_next = h->dtahe_next;
2N/A } else {
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A size_t ndx = h->dtahe_hashval % hash->dtah_size;
2N/A
2N/A assert(hash->dtah_hash[ndx] == h);
2N/A hash->dtah_hash[ndx] = h->dtahe_next;
2N/A }
2N/A
2N/A if (h->dtahe_next != NULL)
2N/A h->dtahe_next->dtahe_prev = h->dtahe_prev;
2N/A
2N/A /*
2N/A * Now remove it from the list of all hash entries.
2N/A */
2N/A if (h->dtahe_prevall != NULL) {
2N/A h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
2N/A } else {
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A
2N/A assert(hash->dtah_all == h);
2N/A hash->dtah_all = h->dtahe_nextall;
2N/A }
2N/A
2N/A if (h->dtahe_nextall != NULL)
2N/A h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
2N/A
2N/A (agp->dtat_hash).dtah_nent--;
2N/A
2N/A /*
2N/A * We're unlinked. We can safely destroy the data.
2N/A */
2N/A if (aggdata->dtada_percpu != NULL) {
2N/A for (i = 0; i < max_cpus; i++)
2N/A free(aggdata->dtada_percpu[i]);
2N/A free(aggdata->dtada_percpu);
2N/A }
2N/A
2N/A free(aggdata->dtada_data);
2N/A free(h);
2N/A
2N/A return (0);
2N/A }
2N/A
2N/A default:
2N/A return (dt_set_errno(dtp, EDT_BADRVAL));
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Adt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
2N/A int (*compar)(const void *, const void *))
2N/A{
2N/A int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
2N/A dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
2N/A
2N/A dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
2N/A dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
2N/A
2N/A if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
2N/A dt_keypos = (int)keyposopt;
2N/A } else {
2N/A dt_keypos = 0;
2N/A }
2N/A
2N/A if (compar == NULL) {
2N/A if (!dt_keysort) {
2N/A compar = dt_aggregate_varvalcmp;
2N/A } else {
2N/A compar = dt_aggregate_varkeycmp;
2N/A }
2N/A }
2N/A
2N/A qsort(base, nel, width, compar);
2N/A
2N/A dt_revsort = rev;
2N/A dt_keysort = key;
2N/A dt_keypos = keypos;
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A dt_ahashent_t *h, *next;
2N/A dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
2N/A
2N/A for (h = hash->dtah_all; h != NULL; h = next) {
2N/A /*
2N/A * dt_aggwalk_rval() can potentially remove the current hash
2N/A * entry; we need to load the next hash entry before calling
2N/A * into it.
2N/A */
2N/A next = h->dtahe_nextall;
2N/A
2N/A if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Adt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg,
2N/A int (*sfunc)(const void *, const void *))
2N/A{
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dt_ahashent_t *h, **sorted;
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A size_t i, nentries = 0;
2N/A
2N/A for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
2N/A nentries++;
2N/A
2N/A sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
2N/A
2N/A if (sorted == NULL)
2N/A return (-1);
2N/A
2N/A for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
2N/A sorted[i++] = h;
2N/A
2N/A (void) pthread_mutex_lock(&dt_qsort_lock);
2N/A
2N/A if (sfunc == NULL) {
2N/A dt_aggregate_qsort(dtp, sorted, nentries,
2N/A sizeof (dt_ahashent_t *), NULL);
2N/A } else {
2N/A /*
2N/A * If we've been explicitly passed a sorting function,
2N/A * we'll use that -- ignoring the values of the "aggsortrev",
2N/A * "aggsortkey" and "aggsortkeypos" options.
2N/A */
2N/A qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dt_qsort_lock);
2N/A
2N/A for (i = 0; i < nentries; i++) {
2N/A h = sorted[i];
2N/A
2N/A if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1) {
2N/A dt_free(dtp, sorted);
2N/A return (-1);
2N/A }
2N/A }
2N/A
2N/A dt_free(dtp, sorted);
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_varkeycmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_varvalcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_keyvarcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_valvarcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_varkeyrevcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_varvalrevcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_keyvarrevcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
2N/A dtrace_aggregate_f *func, void *arg)
2N/A{
2N/A return (dt_aggregate_walk_sorted(dtp, func,
2N/A arg, dt_aggregate_valvarrevcmp));
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
2N/A int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
2N/A{
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
2N/A const dtrace_aggdata_t **data;
2N/A dt_ahashent_t *zaggdata = NULL;
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
2N/A dtrace_aggvarid_t max = 0, aggvar;
2N/A int rval = -1, *map, *remap = NULL;
2N/A int i, j;
2N/A dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
2N/A
2N/A /*
2N/A * If the sorting position is greater than the number of aggregation
2N/A * variable IDs, we silently set it to 0.
2N/A */
2N/A if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
2N/A sortpos = 0;
2N/A
2N/A /*
2N/A * First we need to translate the specified aggregation variable IDs
2N/A * into a linear map that will allow us to translate an aggregation
2N/A * variable ID into its position in the specified aggvars.
2N/A */
2N/A for (i = 0; i < naggvars; i++) {
2N/A if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
2N/A return (dt_set_errno(dtp, EDT_BADAGGVAR));
2N/A
2N/A if (aggvars[i] > max)
2N/A max = aggvars[i];
2N/A }
2N/A
2N/A if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
2N/A return (-1);
2N/A
2N/A zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
2N/A
2N/A if (zaggdata == NULL)
2N/A goto out;
2N/A
2N/A for (i = 0; i < naggvars; i++) {
2N/A int ndx = i + sortpos;
2N/A
2N/A if (ndx >= naggvars)
2N/A ndx -= naggvars;
2N/A
2N/A aggvar = aggvars[ndx];
2N/A assert(aggvar <= max);
2N/A
2N/A if (map[aggvar]) {
2N/A /*
2N/A * We have an aggregation variable that is present
2N/A * more than once in the array of aggregation
2N/A * variables. While it's unclear why one might want
2N/A * to do this, it's legal. To support this construct,
2N/A * we will allocate a remap that will indicate the
2N/A * position from which this aggregation variable
2N/A * should be pulled. (That is, where the remap will
2N/A * map from one position to another.)
2N/A */
2N/A if (remap == NULL) {
2N/A remap = dt_zalloc(dtp, naggvars * sizeof (int));
2N/A
2N/A if (remap == NULL)
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Given that the variable is already present, assert
2N/A * that following through the mapping and adjusting
2N/A * for the sort position yields the same aggregation
2N/A * variable ID.
2N/A */
2N/A assert(aggvars[(map[aggvar] - 1 + sortpos) %
2N/A naggvars] == aggvars[ndx]);
2N/A
2N/A remap[i] = map[aggvar];
2N/A continue;
2N/A }
2N/A
2N/A map[aggvar] = i + 1;
2N/A }
2N/A
2N/A /*
2N/A * We need to take two passes over the data to size our allocation, so
2N/A * we'll use the first pass to also fill in the zero-filled data to be
2N/A * used to properly format a zero-valued aggregation.
2N/A */
2N/A for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
2N/A dtrace_aggvarid_t id;
2N/A int ndx;
2N/A
2N/A if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
2N/A continue;
2N/A
2N/A if (zaggdata[ndx - 1].dtahe_size == 0) {
2N/A zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
2N/A zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
2N/A }
2N/A
2N/A nentries++;
2N/A }
2N/A
2N/A if (nentries == 0) {
2N/A /*
2N/A * We couldn't find any entries; there is nothing else to do.
2N/A */
2N/A rval = 0;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Before we sort the data, we're going to look for any holes in our
2N/A * zero-filled data. This will occur if an aggregation variable that
2N/A * we are being asked to print has not yet been assigned the result of
2N/A * any aggregating action for _any_ tuple. The issue becomes that we
2N/A * would like a zero value to be printed for all columns for this
2N/A * aggregation, but without any record description, we don't know the
2N/A * aggregating action that corresponds to the aggregation variable. To
2N/A * try to find a match, we're simply going to lookup aggregation IDs
2N/A * (which are guaranteed to be contiguous and to start from 1), looking
2N/A * for the specified aggregation variable ID. If we find a match,
2N/A * we'll use that. If we iterate over all aggregation IDs and don't
2N/A * find a match, then we must be an anonymous enabling. (Anonymous
2N/A * enablings can't currently derive either aggregation variable IDs or
2N/A * aggregation variable names given only an aggregation ID.) In this
2N/A * obscure case (anonymous enabling, multiple aggregation printa() with
2N/A * some aggregations not represented for any tuple), our defined
2N/A * behavior is that the zero will be printed in the format of the first
2N/A * aggregation variable that contains any non-zero value.
2N/A */
2N/A for (i = 0; i < naggvars; i++) {
2N/A if (zaggdata[i].dtahe_size == 0) {
2N/A dtrace_aggvarid_t aggvar;
2N/A
2N/A aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
2N/A assert(zaggdata[i].dtahe_data.dtada_data == NULL);
2N/A
2N/A for (j = DTRACE_AGGIDNONE + 1; ; j++) {
2N/A dtrace_aggdesc_t *agg;
2N/A dtrace_aggdata_t *aggdata;
2N/A
2N/A if (dt_aggid_lookup(dtp, j, &agg) != 0)
2N/A break;
2N/A
2N/A if (agg->dtagd_varid != aggvar)
2N/A continue;
2N/A
2N/A /*
2N/A * We have our description -- now we need to
2N/A * cons up the zaggdata entry for it.
2N/A */
2N/A aggdata = &zaggdata[i].dtahe_data;
2N/A aggdata->dtada_size = agg->dtagd_size;
2N/A aggdata->dtada_desc = agg;
2N/A aggdata->dtada_handle = dtp;
2N/A (void) dt_epid_lookup(dtp, agg->dtagd_epid,
2N/A &aggdata->dtada_edesc,
2N/A &aggdata->dtada_pdesc);
2N/A aggdata->dtada_normal = 1;
2N/A zaggdata[i].dtahe_hashval = 0;
2N/A zaggdata[i].dtahe_size = agg->dtagd_size;
2N/A break;
2N/A }
2N/A
2N/A if (zaggdata[i].dtahe_size == 0) {
2N/A caddr_t data;
2N/A
2N/A /*
2N/A * We couldn't find this aggregation, meaning
2N/A * that we have never seen it before for any
2N/A * tuple _and_ this is an anonymous enabling.
2N/A * That is, we're in the obscure case outlined
2N/A * above. In this case, our defined behavior
2N/A * is to format the data in the format of the
2N/A * first non-zero aggregation -- of which, of
2N/A * course, we know there to be at least one
2N/A * (or nentries would have been zero).
2N/A */
2N/A for (j = 0; j < naggvars; j++) {
2N/A if (zaggdata[j].dtahe_size != 0)
2N/A break;
2N/A }
2N/A
2N/A assert(j < naggvars);
2N/A zaggdata[i] = zaggdata[j];
2N/A
2N/A data = zaggdata[i].dtahe_data.dtada_data;
2N/A assert(data != NULL);
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Now we need to allocate our zero-filled data for use for
2N/A * aggregations that don't have a value corresponding to a given key.
2N/A */
2N/A for (i = 0; i < naggvars; i++) {
2N/A dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
2N/A dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
2N/A dtrace_recdesc_t *rec;
2N/A uint64_t larg;
2N/A caddr_t zdata;
2N/A
2N/A zsize = zaggdata[i].dtahe_size;
2N/A assert(zsize != 0);
2N/A
2N/A if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
2N/A /*
2N/A * If we failed to allocated some zero-filled data, we
2N/A * need to zero out the remaining dtada_data pointers
2N/A * to prevent the wrong data from being freed below.
2N/A */
2N/A for (j = i; j < naggvars; j++)
2N/A zaggdata[j].dtahe_data.dtada_data = NULL;
2N/A goto out;
2N/A }
2N/A
2N/A aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
2N/A
2N/A /*
2N/A * First, the easy bit. To maintain compatibility with
2N/A * consumers that pull the compiler-generated ID out of the
2N/A * data, we put that ID at the top of the zero-filled data.
2N/A */
2N/A rec = &aggdesc->dtagd_rec[0];
2N/A /* LINTED - alignment */
2N/A *((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
2N/A
2N/A rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
2N/A
2N/A /*
2N/A * Now for the more complicated part. If (and only if) this
2N/A * is an lquantize() aggregating action, zero-filled data is
2N/A * not equivalent to an empty record: we must also get the
2N/A * parameters for the lquantize().
2N/A */
2N/A if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
2N/A if (aggdata->dtada_data != NULL) {
2N/A /*
2N/A * The easier case here is if we actually have
2N/A * some prototype data -- in which case we
2N/A * manually dig it out of the aggregation
2N/A * record.
2N/A */
2N/A /* LINTED - alignment */
2N/A larg = *((uint64_t *)(aggdata->dtada_data +
2N/A rec->dtrd_offset));
2N/A } else {
2N/A /*
2N/A * We don't have any prototype data. As a
2N/A * result, we know that we _do_ have the
2N/A * compiler-generated information. (If this
2N/A * were an anonymous enabling, all of our
2N/A * zero-filled data would have prototype data
2N/A * -- either directly or indirectly.) So as
2N/A * gross as it is, we'll grovel around in the
2N/A * compiler-generated information to find the
2N/A * lquantize() parameters.
2N/A */
2N/A dtrace_stmtdesc_t *sdp;
2N/A dt_ident_t *aid;
2N/A dt_idsig_t *isp;
2N/A
2N/A sdp = (dtrace_stmtdesc_t *)(uintptr_t)
2N/A aggdesc->dtagd_rec[0].dtrd_uarg;
2N/A aid = sdp->dtsd_aggdata;
2N/A isp = (dt_idsig_t *)aid->di_data;
2N/A assert(isp->dis_auxinfo != 0);
2N/A larg = isp->dis_auxinfo;
2N/A }
2N/A
2N/A /* LINTED - alignment */
2N/A *((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
2N/A }
2N/A
2N/A aggdata->dtada_data = zdata;
2N/A }
2N/A
2N/A /*
2N/A * Now that we've dealt with setting up our zero-filled data, we can
2N/A * allocate our sorted array, and take another pass over the data to
2N/A * fill it.
2N/A */
2N/A sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
2N/A
2N/A if (sorted == NULL)
2N/A goto out;
2N/A
2N/A for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
2N/A dtrace_aggvarid_t id;
2N/A
2N/A if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
2N/A continue;
2N/A
2N/A sorted[i++] = h;
2N/A }
2N/A
2N/A assert(i == nentries);
2N/A
2N/A /*
2N/A * We've loaded our array; now we need to sort by value to allow us
2N/A * to create bundles of like value. We're going to acquire the
2N/A * dt_qsort_lock here, and hold it across all of our subsequent
2N/A * comparison and sorting.
2N/A */
2N/A (void) pthread_mutex_lock(&dt_qsort_lock);
2N/A
2N/A qsort(sorted, nentries, sizeof (dt_ahashent_t *),
2N/A dt_aggregate_keyvarcmp);
2N/A
2N/A /*
2N/A * Now we need to go through and create bundles. Because the number
2N/A * of bundles is bounded by the size of the sorted array, we're going
2N/A * to reuse the underlying storage. And note that "bundle" is an
2N/A * array of pointers to arrays of pointers to dt_ahashent_t -- making
2N/A * its type (regrettably) "dt_ahashent_t ***". (Regrettable because
2N/A * '*' -- like '_' and 'X' -- should never appear in triplicate in
2N/A * an ideal world.)
2N/A */
2N/A bundle = (dt_ahashent_t ***)sorted;
2N/A
2N/A for (i = 1, start = 0; i <= nentries; i++) {
2N/A if (i < nentries &&
2N/A dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
2N/A continue;
2N/A
2N/A /*
2N/A * We have a bundle boundary. Everything from start to
2N/A * (i - 1) belongs in one bundle.
2N/A */
2N/A assert(i - start <= naggvars);
2N/A bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
2N/A
2N/A if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
2N/A (void) pthread_mutex_unlock(&dt_qsort_lock);
2N/A goto out;
2N/A }
2N/A
2N/A for (j = start; j < i; j++) {
2N/A dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
2N/A
2N/A assert(id <= max);
2N/A assert(map[id] != 0);
2N/A assert(map[id] - 1 < naggvars);
2N/A assert(nbundle[map[id] - 1] == NULL);
2N/A nbundle[map[id] - 1] = sorted[j];
2N/A
2N/A if (nbundle[naggvars] == NULL)
2N/A nbundle[naggvars] = sorted[j];
2N/A }
2N/A
2N/A for (j = 0; j < naggvars; j++) {
2N/A if (nbundle[j] != NULL)
2N/A continue;
2N/A
2N/A /*
2N/A * Before we assume that this aggregation variable
2N/A * isn't present (and fall back to using the
2N/A * zero-filled data allocated earlier), check the
2N/A * remap. If we have a remapping, we'll drop it in
2N/A * here. Note that we might be remapping an
2N/A * aggregation variable that isn't present for this
2N/A * key; in this case, the aggregation data that we
2N/A * copy will point to the zeroed data.
2N/A */
2N/A if (remap != NULL && remap[j]) {
2N/A assert(remap[j] - 1 < j);
2N/A assert(nbundle[remap[j] - 1] != NULL);
2N/A nbundle[j] = nbundle[remap[j] - 1];
2N/A } else {
2N/A nbundle[j] = &zaggdata[j];
2N/A }
2N/A }
2N/A
2N/A bundle[nbundles++] = nbundle;
2N/A start = i;
2N/A }
2N/A
2N/A /*
2N/A * Now we need to re-sort based on the first value.
2N/A */
2N/A dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
2N/A dt_aggregate_bundlecmp);
2N/A
2N/A (void) pthread_mutex_unlock(&dt_qsort_lock);
2N/A
2N/A /*
2N/A * We're done! Now we just need to go back over the sorted bundles,
2N/A * calling the function.
2N/A */
2N/A data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
2N/A
2N/A for (i = 0; i < nbundles; i++) {
2N/A for (j = 0; j < naggvars; j++)
2N/A data[j + 1] = NULL;
2N/A
2N/A for (j = 0; j < naggvars; j++) {
2N/A int ndx = j - sortpos;
2N/A
2N/A if (ndx < 0)
2N/A ndx += naggvars;
2N/A
2N/A assert(bundle[i][ndx] != NULL);
2N/A data[j + 1] = &bundle[i][ndx]->dtahe_data;
2N/A }
2N/A
2N/A for (j = 0; j < naggvars; j++)
2N/A assert(data[j + 1] != NULL);
2N/A
2N/A /*
2N/A * The representative key is the last element in the bundle.
2N/A * Assert that we have one, and then set it to be the first
2N/A * element of data.
2N/A */
2N/A assert(bundle[i][j] != NULL);
2N/A data[0] = &bundle[i][j]->dtahe_data;
2N/A
2N/A if ((rval = func(data, naggvars + 1, arg)) == -1)
2N/A goto out;
2N/A }
2N/A
2N/A rval = 0;
2N/Aout:
2N/A for (i = 0; i < nbundles; i++)
2N/A dt_free(dtp, bundle[i]);
2N/A
2N/A if (zaggdata != NULL) {
2N/A for (i = 0; i < naggvars; i++)
2N/A dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
2N/A }
2N/A
2N/A dt_free(dtp, zaggdata);
2N/A dt_free(dtp, sorted);
2N/A dt_free(dtp, remap);
2N/A dt_free(dtp, map);
2N/A
2N/A return (rval);
2N/A}
2N/A
2N/Aint
2N/Adtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
2N/A dtrace_aggregate_walk_f *func)
2N/A{
2N/A dt_print_aggdata_t pd;
2N/A
2N/A pd.dtpa_dtp = dtp;
2N/A pd.dtpa_fp = fp;
2N/A pd.dtpa_allunprint = 1;
2N/A
2N/A if (func == NULL)
2N/A func = dtrace_aggregate_walk_sorted;
2N/A
2N/A if ((*func)(dtp, dt_print_agg, &pd) == -1)
2N/A return (dt_set_errno(dtp, dtp->dt_errno));
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Adtrace_aggregate_clear(dtrace_hdl_t *dtp)
2N/A{
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A dt_ahashent_t *h;
2N/A dtrace_aggdata_t *data;
2N/A dtrace_aggdesc_t *aggdesc;
2N/A dtrace_recdesc_t *rec;
2N/A int i, max_cpus = agp->dtat_maxcpu;
2N/A
2N/A for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
2N/A aggdesc = h->dtahe_data.dtada_desc;
2N/A rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
2N/A data = &h->dtahe_data;
2N/A
2N/A bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
2N/A
2N/A if (data->dtada_percpu == NULL)
2N/A continue;
2N/A
2N/A for (i = 0; i < max_cpus; i++)
2N/A bzero(data->dtada_percpu[i], rec->dtrd_size);
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Adt_aggregate_destroy(dtrace_hdl_t *dtp)
2N/A{
2N/A dt_aggregate_t *agp = &dtp->dt_aggregate;
2N/A dt_ahash_t *hash = &agp->dtat_hash;
2N/A dt_ahashent_t *h, *next;
2N/A dtrace_aggdata_t *aggdata;
2N/A int i, max_cpus = agp->dtat_maxcpu;
2N/A
2N/A if (hash->dtah_hash == NULL) {
2N/A assert(hash->dtah_all == NULL);
2N/A } else {
2N/A free(hash->dtah_hash);
2N/A
2N/A for (h = hash->dtah_all; h != NULL; h = next) {
2N/A next = h->dtahe_nextall;
2N/A
2N/A aggdata = &h->dtahe_data;
2N/A
2N/A if (aggdata->dtada_percpu != NULL) {
2N/A for (i = 0; i < max_cpus; i++)
2N/A free(aggdata->dtada_percpu[i]);
2N/A free(aggdata->dtada_percpu);
2N/A }
2N/A
2N/A free(aggdata->dtada_data);
2N/A free(h);
2N/A }
2N/A
2N/A hash->dtah_hash = NULL;
2N/A hash->dtah_all = NULL;
2N/A hash->dtah_size = 0;
2N/A hash->dtah_nent = 0;
2N/A }
2N/A
2N/A free(agp->dtat_buf.dtbd_data);
2N/A free(agp->dtat_cpus);
2N/A}