Print this page
4471 DTrace count() with histogram
4472 DTrace full width distribution histograms
4473 DTrace frequency trails
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdtrace/common/dt_aggregate.c
+++ new/usr/src/lib/libdtrace/common/dt_aggregate.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 - * Copyright (c) 2011, Joyent, Inc. All rights reserved.
28 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 29 * Copyright (c) 2012 by Delphix. All rights reserved.
30 30 */
31 31
32 32 #include <stdlib.h>
33 33 #include <strings.h>
34 34 #include <errno.h>
35 35 #include <unistd.h>
36 36 #include <dt_impl.h>
37 37 #include <assert.h>
38 38 #include <alloca.h>
39 39 #include <limits.h>
40 40
41 41 #define DTRACE_AHASHSIZE 32779 /* big 'ol prime */
42 42
43 43 /*
44 44 * Because qsort(3C) does not allow an argument to be passed to a comparison
45 45 * function, the variables that affect comparison must regrettably be global;
46 46 * they are protected by a global static lock, dt_qsort_lock.
47 47 */
48 48 static pthread_mutex_t dt_qsort_lock = PTHREAD_MUTEX_INITIALIZER;
49 49
50 50 static int dt_revsort;
51 51 static int dt_keysort;
52 52 static int dt_keypos;
53 53
54 54 #define DT_LESSTHAN (dt_revsort == 0 ? -1 : 1)
55 55 #define DT_GREATERTHAN (dt_revsort == 0 ? 1 : -1)
56 56
57 57 static void
58 58 dt_aggregate_count(int64_t *existing, int64_t *new, size_t size)
59 59 {
60 60 int i;
61 61
62 62 for (i = 0; i < size / sizeof (int64_t); i++)
63 63 existing[i] = existing[i] + new[i];
64 64 }
65 65
66 66 static int
67 67 dt_aggregate_countcmp(int64_t *lhs, int64_t *rhs)
68 68 {
69 69 int64_t lvar = *lhs;
70 70 int64_t rvar = *rhs;
71 71
72 72 if (lvar < rvar)
73 73 return (DT_LESSTHAN);
74 74
75 75 if (lvar > rvar)
76 76 return (DT_GREATERTHAN);
77 77
78 78 return (0);
79 79 }
80 80
81 81 /*ARGSUSED*/
82 82 static void
83 83 dt_aggregate_min(int64_t *existing, int64_t *new, size_t size)
84 84 {
85 85 if (*new < *existing)
86 86 *existing = *new;
87 87 }
88 88
89 89 /*ARGSUSED*/
90 90 static void
91 91 dt_aggregate_max(int64_t *existing, int64_t *new, size_t size)
92 92 {
93 93 if (*new > *existing)
94 94 *existing = *new;
95 95 }
96 96
97 97 static int
98 98 dt_aggregate_averagecmp(int64_t *lhs, int64_t *rhs)
99 99 {
100 100 int64_t lavg = lhs[0] ? (lhs[1] / lhs[0]) : 0;
101 101 int64_t ravg = rhs[0] ? (rhs[1] / rhs[0]) : 0;
102 102
103 103 if (lavg < ravg)
104 104 return (DT_LESSTHAN);
105 105
106 106 if (lavg > ravg)
107 107 return (DT_GREATERTHAN);
108 108
109 109 return (0);
110 110 }
111 111
112 112 static int
113 113 dt_aggregate_stddevcmp(int64_t *lhs, int64_t *rhs)
114 114 {
115 115 uint64_t lsd = dt_stddev((uint64_t *)lhs, 1);
116 116 uint64_t rsd = dt_stddev((uint64_t *)rhs, 1);
117 117
118 118 if (lsd < rsd)
119 119 return (DT_LESSTHAN);
120 120
121 121 if (lsd > rsd)
122 122 return (DT_GREATERTHAN);
123 123
124 124 return (0);
125 125 }
126 126
127 127 /*ARGSUSED*/
128 128 static void
129 129 dt_aggregate_lquantize(int64_t *existing, int64_t *new, size_t size)
130 130 {
131 131 int64_t arg = *existing++;
132 132 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg);
133 133 int i;
134 134
135 135 for (i = 0; i <= levels + 1; i++)
136 136 existing[i] = existing[i] + new[i + 1];
137 137 }
138 138
139 139 static long double
140 140 dt_aggregate_lquantizedsum(int64_t *lquanta)
141 141 {
142 142 int64_t arg = *lquanta++;
143 143 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
144 144 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
145 145 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
146 146 long double total = (long double)lquanta[0] * (long double)(base - 1);
147 147
148 148 for (i = 0; i < levels; base += step, i++)
149 149 total += (long double)lquanta[i + 1] * (long double)base;
150 150
151 151 return (total + (long double)lquanta[levels + 1] *
152 152 (long double)(base + 1));
153 153 }
154 154
155 155 static int64_t
156 156 dt_aggregate_lquantizedzero(int64_t *lquanta)
157 157 {
158 158 int64_t arg = *lquanta++;
159 159 int32_t base = DTRACE_LQUANTIZE_BASE(arg);
160 160 uint16_t step = DTRACE_LQUANTIZE_STEP(arg);
161 161 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg), i;
162 162
163 163 if (base - 1 == 0)
164 164 return (lquanta[0]);
165 165
166 166 for (i = 0; i < levels; base += step, i++) {
167 167 if (base != 0)
168 168 continue;
169 169
170 170 return (lquanta[i + 1]);
171 171 }
172 172
173 173 if (base + 1 == 0)
174 174 return (lquanta[levels + 1]);
175 175
176 176 return (0);
177 177 }
178 178
179 179 static int
180 180 dt_aggregate_lquantizedcmp(int64_t *lhs, int64_t *rhs)
181 181 {
182 182 long double lsum = dt_aggregate_lquantizedsum(lhs);
183 183 long double rsum = dt_aggregate_lquantizedsum(rhs);
184 184 int64_t lzero, rzero;
185 185
186 186 if (lsum < rsum)
187 187 return (DT_LESSTHAN);
188 188
189 189 if (lsum > rsum)
190 190 return (DT_GREATERTHAN);
191 191
192 192 /*
193 193 * If they're both equal, then we will compare based on the weights at
194 194 * zero. If the weights at zero are equal (or if zero is not within
195 195 * the range of the linear quantization), then this will be judged a
196 196 * tie and will be resolved based on the key comparison.
197 197 */
198 198 lzero = dt_aggregate_lquantizedzero(lhs);
199 199 rzero = dt_aggregate_lquantizedzero(rhs);
200 200
201 201 if (lzero < rzero)
202 202 return (DT_LESSTHAN);
203 203
204 204 if (lzero > rzero)
205 205 return (DT_GREATERTHAN);
206 206
207 207 return (0);
208 208 }
209 209
210 210 static void
211 211 dt_aggregate_llquantize(int64_t *existing, int64_t *new, size_t size)
212 212 {
213 213 int i;
214 214
215 215 for (i = 1; i < size / sizeof (int64_t); i++)
216 216 existing[i] = existing[i] + new[i];
217 217 }
218 218
219 219 static long double
220 220 dt_aggregate_llquantizedsum(int64_t *llquanta)
221 221 {
222 222 int64_t arg = *llquanta++;
223 223 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg);
224 224 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg);
225 225 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg);
226 226 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
227 227 int bin = 0, order;
228 228 int64_t value = 1, next, step;
229 229 long double total;
230 230
231 231 assert(nsteps >= factor);
232 232 assert(nsteps % factor == 0);
233 233
234 234 for (order = 0; order < low; order++)
235 235 value *= factor;
236 236
237 237 total = (long double)llquanta[bin++] * (long double)(value - 1);
238 238
239 239 next = value * factor;
240 240 step = next > nsteps ? next / nsteps : 1;
241 241
242 242 while (order <= high) {
243 243 assert(value < next);
244 244 total += (long double)llquanta[bin++] * (long double)(value);
245 245
246 246 if ((value += step) != next)
247 247 continue;
248 248
249 249 next = value * factor;
250 250 step = next > nsteps ? next / nsteps : 1;
251 251 order++;
252 252 }
253 253
254 254 return (total + (long double)llquanta[bin] * (long double)value);
255 255 }
256 256
257 257 static int
258 258 dt_aggregate_llquantizedcmp(int64_t *lhs, int64_t *rhs)
259 259 {
260 260 long double lsum = dt_aggregate_llquantizedsum(lhs);
261 261 long double rsum = dt_aggregate_llquantizedsum(rhs);
262 262 int64_t lzero, rzero;
263 263
264 264 if (lsum < rsum)
265 265 return (DT_LESSTHAN);
266 266
267 267 if (lsum > rsum)
268 268 return (DT_GREATERTHAN);
269 269
270 270 /*
271 271 * If they're both equal, then we will compare based on the weights at
272 272 * zero. If the weights at zero are equal, then this will be judged a
273 273 * tie and will be resolved based on the key comparison.
274 274 */
275 275 lzero = lhs[1];
276 276 rzero = rhs[1];
277 277
278 278 if (lzero < rzero)
279 279 return (DT_LESSTHAN);
280 280
281 281 if (lzero > rzero)
282 282 return (DT_GREATERTHAN);
283 283
284 284 return (0);
285 285 }
286 286
287 287 static int
288 288 dt_aggregate_quantizedcmp(int64_t *lhs, int64_t *rhs)
289 289 {
290 290 int nbuckets = DTRACE_QUANTIZE_NBUCKETS, i;
291 291 long double ltotal = 0, rtotal = 0;
292 292 int64_t lzero, rzero;
293 293
294 294 for (i = 0; i < nbuckets; i++) {
295 295 int64_t bucketval = DTRACE_QUANTIZE_BUCKETVAL(i);
296 296
297 297 if (bucketval == 0) {
298 298 lzero = lhs[i];
299 299 rzero = rhs[i];
300 300 }
301 301
302 302 ltotal += (long double)bucketval * (long double)lhs[i];
303 303 rtotal += (long double)bucketval * (long double)rhs[i];
304 304 }
305 305
306 306 if (ltotal < rtotal)
307 307 return (DT_LESSTHAN);
308 308
309 309 if (ltotal > rtotal)
310 310 return (DT_GREATERTHAN);
311 311
312 312 /*
313 313 * If they're both equal, then we will compare based on the weights at
314 314 * zero. If the weights at zero are equal, then this will be judged a
315 315 * tie and will be resolved based on the key comparison.
316 316 */
317 317 if (lzero < rzero)
318 318 return (DT_LESSTHAN);
319 319
320 320 if (lzero > rzero)
321 321 return (DT_GREATERTHAN);
322 322
323 323 return (0);
324 324 }
325 325
326 326 static void
327 327 dt_aggregate_usym(dtrace_hdl_t *dtp, uint64_t *data)
328 328 {
329 329 uint64_t pid = data[0];
330 330 uint64_t *pc = &data[1];
331 331 struct ps_prochandle *P;
332 332 GElf_Sym sym;
333 333
334 334 if (dtp->dt_vector != NULL)
335 335 return;
336 336
337 337 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
338 338 return;
339 339
340 340 dt_proc_lock(dtp, P);
341 341
342 342 if (Plookup_by_addr(P, *pc, NULL, 0, &sym) == 0)
343 343 *pc = sym.st_value;
344 344
345 345 dt_proc_unlock(dtp, P);
346 346 dt_proc_release(dtp, P);
347 347 }
348 348
349 349 static void
350 350 dt_aggregate_umod(dtrace_hdl_t *dtp, uint64_t *data)
351 351 {
352 352 uint64_t pid = data[0];
353 353 uint64_t *pc = &data[1];
354 354 struct ps_prochandle *P;
355 355 const prmap_t *map;
356 356
357 357 if (dtp->dt_vector != NULL)
358 358 return;
359 359
360 360 if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0)) == NULL)
361 361 return;
362 362
363 363 dt_proc_lock(dtp, P);
364 364
365 365 if ((map = Paddr_to_map(P, *pc)) != NULL)
366 366 *pc = map->pr_vaddr;
367 367
368 368 dt_proc_unlock(dtp, P);
369 369 dt_proc_release(dtp, P);
370 370 }
371 371
372 372 static void
373 373 dt_aggregate_sym(dtrace_hdl_t *dtp, uint64_t *data)
374 374 {
375 375 GElf_Sym sym;
376 376 uint64_t *pc = data;
377 377
378 378 if (dtrace_lookup_by_addr(dtp, *pc, &sym, NULL) == 0)
379 379 *pc = sym.st_value;
380 380 }
381 381
382 382 static void
383 383 dt_aggregate_mod(dtrace_hdl_t *dtp, uint64_t *data)
384 384 {
385 385 uint64_t *pc = data;
386 386 dt_module_t *dmp;
387 387
388 388 if (dtp->dt_vector != NULL) {
389 389 /*
390 390 * We don't have a way of just getting the module for a
391 391 * vectored open, and it doesn't seem to be worth defining
392 392 * one. This means that use of mod() won't get true
393 393 * aggregation in the postmortem case (some modules may
394 394 * appear more than once in aggregation output). It seems
395 395 * unlikely that anyone will ever notice or care...
396 396 */
397 397 return;
398 398 }
399 399
400 400 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
401 401 dmp = dt_list_next(dmp)) {
402 402 if (*pc - dmp->dm_text_va < dmp->dm_text_size) {
403 403 *pc = dmp->dm_text_va;
404 404 return;
405 405 }
406 406 }
407 407 }
408 408
409 409 static dtrace_aggvarid_t
410 410 dt_aggregate_aggvarid(dt_ahashent_t *ent)
411 411 {
412 412 dtrace_aggdesc_t *agg = ent->dtahe_data.dtada_desc;
413 413 caddr_t data = ent->dtahe_data.dtada_data;
414 414 dtrace_recdesc_t *rec = agg->dtagd_rec;
415 415
416 416 /*
417 417 * First, we'll check the variable ID in the aggdesc. If it's valid,
418 418 * we'll return it. If not, we'll use the compiler-generated ID
419 419 * present as the first record.
420 420 */
421 421 if (agg->dtagd_varid != DTRACE_AGGVARIDNONE)
422 422 return (agg->dtagd_varid);
423 423
424 424 agg->dtagd_varid = *((dtrace_aggvarid_t *)(uintptr_t)(data +
425 425 rec->dtrd_offset));
426 426
427 427 return (agg->dtagd_varid);
428 428 }
429 429
430 430
431 431 static int
432 432 dt_aggregate_snap_cpu(dtrace_hdl_t *dtp, processorid_t cpu)
433 433 {
434 434 dtrace_epid_t id;
435 435 uint64_t hashval;
436 436 size_t offs, roffs, size, ndx;
437 437 int i, j, rval;
438 438 caddr_t addr, data;
439 439 dtrace_recdesc_t *rec;
440 440 dt_aggregate_t *agp = &dtp->dt_aggregate;
441 441 dtrace_aggdesc_t *agg;
442 442 dt_ahash_t *hash = &agp->dtat_hash;
443 443 dt_ahashent_t *h;
444 444 dtrace_bufdesc_t b = agp->dtat_buf, *buf = &b;
445 445 dtrace_aggdata_t *aggdata;
446 446 int flags = agp->dtat_flags;
447 447
448 448 buf->dtbd_cpu = cpu;
449 449
450 450 if (dt_ioctl(dtp, DTRACEIOC_AGGSNAP, buf) == -1) {
451 451 if (errno == ENOENT) {
452 452 /*
453 453 * If that failed with ENOENT, it may be because the
454 454 * CPU was unconfigured. This is okay; we'll just
455 455 * do nothing but return success.
456 456 */
457 457 return (0);
458 458 }
459 459
460 460 return (dt_set_errno(dtp, errno));
461 461 }
462 462
463 463 if (buf->dtbd_drops != 0) {
464 464 if (dt_handle_cpudrop(dtp, cpu,
465 465 DTRACEDROP_AGGREGATION, buf->dtbd_drops) == -1)
466 466 return (-1);
467 467 }
468 468
469 469 if (buf->dtbd_size == 0)
470 470 return (0);
471 471
472 472 if (hash->dtah_hash == NULL) {
473 473 size_t size;
474 474
475 475 hash->dtah_size = DTRACE_AHASHSIZE;
476 476 size = hash->dtah_size * sizeof (dt_ahashent_t *);
477 477
478 478 if ((hash->dtah_hash = malloc(size)) == NULL)
479 479 return (dt_set_errno(dtp, EDT_NOMEM));
480 480
481 481 bzero(hash->dtah_hash, size);
482 482 }
483 483
484 484 for (offs = 0; offs < buf->dtbd_size; ) {
485 485 /*
486 486 * We're guaranteed to have an ID.
487 487 */
488 488 id = *((dtrace_epid_t *)((uintptr_t)buf->dtbd_data +
489 489 (uintptr_t)offs));
490 490
491 491 if (id == DTRACE_AGGIDNONE) {
492 492 /*
493 493 * This is filler to assure proper alignment of the
494 494 * next record; we simply ignore it.
495 495 */
496 496 offs += sizeof (id);
497 497 continue;
498 498 }
499 499
500 500 if ((rval = dt_aggid_lookup(dtp, id, &agg)) != 0)
501 501 return (rval);
502 502
503 503 addr = buf->dtbd_data + offs;
504 504 size = agg->dtagd_size;
505 505 hashval = 0;
506 506
507 507 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
508 508 rec = &agg->dtagd_rec[j];
509 509 roffs = rec->dtrd_offset;
510 510
511 511 switch (rec->dtrd_action) {
512 512 case DTRACEACT_USYM:
513 513 dt_aggregate_usym(dtp,
514 514 /* LINTED - alignment */
515 515 (uint64_t *)&addr[roffs]);
516 516 break;
517 517
518 518 case DTRACEACT_UMOD:
519 519 dt_aggregate_umod(dtp,
520 520 /* LINTED - alignment */
521 521 (uint64_t *)&addr[roffs]);
522 522 break;
523 523
524 524 case DTRACEACT_SYM:
525 525 /* LINTED - alignment */
526 526 dt_aggregate_sym(dtp, (uint64_t *)&addr[roffs]);
527 527 break;
528 528
529 529 case DTRACEACT_MOD:
530 530 /* LINTED - alignment */
531 531 dt_aggregate_mod(dtp, (uint64_t *)&addr[roffs]);
532 532 break;
533 533
534 534 default:
535 535 break;
536 536 }
537 537
538 538 for (i = 0; i < rec->dtrd_size; i++)
539 539 hashval += addr[roffs + i];
540 540 }
541 541
542 542 ndx = hashval % hash->dtah_size;
543 543
544 544 for (h = hash->dtah_hash[ndx]; h != NULL; h = h->dtahe_next) {
545 545 if (h->dtahe_hashval != hashval)
546 546 continue;
547 547
548 548 if (h->dtahe_size != size)
549 549 continue;
550 550
551 551 aggdata = &h->dtahe_data;
552 552 data = aggdata->dtada_data;
553 553
554 554 for (j = 0; j < agg->dtagd_nrecs - 1; j++) {
555 555 rec = &agg->dtagd_rec[j];
556 556 roffs = rec->dtrd_offset;
557 557
558 558 for (i = 0; i < rec->dtrd_size; i++)
559 559 if (addr[roffs + i] != data[roffs + i])
560 560 goto hashnext;
561 561 }
562 562
563 563 /*
564 564 * We found it. Now we need to apply the aggregating
565 565 * action on the data here.
566 566 */
567 567 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
568 568 roffs = rec->dtrd_offset;
569 569 /* LINTED - alignment */
570 570 h->dtahe_aggregate((int64_t *)&data[roffs],
571 571 /* LINTED - alignment */
572 572 (int64_t *)&addr[roffs], rec->dtrd_size);
573 573
574 574 /*
575 575 * If we're keeping per CPU data, apply the aggregating
576 576 * action there as well.
577 577 */
578 578 if (aggdata->dtada_percpu != NULL) {
579 579 data = aggdata->dtada_percpu[cpu];
580 580
581 581 /* LINTED - alignment */
582 582 h->dtahe_aggregate((int64_t *)data,
583 583 /* LINTED - alignment */
584 584 (int64_t *)&addr[roffs], rec->dtrd_size);
585 585 }
586 586
587 587 goto bufnext;
588 588 hashnext:
589 589 continue;
590 590 }
591 591
592 592 /*
593 593 * If we're here, we couldn't find an entry for this record.
594 594 */
595 595 if ((h = malloc(sizeof (dt_ahashent_t))) == NULL)
596 596 return (dt_set_errno(dtp, EDT_NOMEM));
597 597 bzero(h, sizeof (dt_ahashent_t));
598 598 aggdata = &h->dtahe_data;
599 599
600 600 if ((aggdata->dtada_data = malloc(size)) == NULL) {
601 601 free(h);
602 602 return (dt_set_errno(dtp, EDT_NOMEM));
603 603 }
604 604
605 605 bcopy(addr, aggdata->dtada_data, size);
606 606 aggdata->dtada_size = size;
607 607 aggdata->dtada_desc = agg;
608 608 aggdata->dtada_handle = dtp;
609 609 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
610 610 &aggdata->dtada_edesc, &aggdata->dtada_pdesc);
611 611 aggdata->dtada_normal = 1;
612 612
613 613 h->dtahe_hashval = hashval;
614 614 h->dtahe_size = size;
615 615 (void) dt_aggregate_aggvarid(h);
616 616
617 617 rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
618 618
619 619 if (flags & DTRACE_A_PERCPU) {
620 620 int max_cpus = agp->dtat_maxcpu;
621 621 caddr_t *percpu = malloc(max_cpus * sizeof (caddr_t));
622 622
623 623 if (percpu == NULL) {
624 624 free(aggdata->dtada_data);
625 625 free(h);
626 626 return (dt_set_errno(dtp, EDT_NOMEM));
627 627 }
628 628
629 629 for (j = 0; j < max_cpus; j++) {
630 630 percpu[j] = malloc(rec->dtrd_size);
631 631
632 632 if (percpu[j] == NULL) {
633 633 while (--j >= 0)
634 634 free(percpu[j]);
635 635
636 636 free(aggdata->dtada_data);
637 637 free(h);
638 638 return (dt_set_errno(dtp, EDT_NOMEM));
639 639 }
640 640
641 641 if (j == cpu) {
642 642 bcopy(&addr[rec->dtrd_offset],
643 643 percpu[j], rec->dtrd_size);
644 644 } else {
645 645 bzero(percpu[j], rec->dtrd_size);
646 646 }
647 647 }
648 648
649 649 aggdata->dtada_percpu = percpu;
650 650 }
651 651
652 652 switch (rec->dtrd_action) {
653 653 case DTRACEAGG_MIN:
654 654 h->dtahe_aggregate = dt_aggregate_min;
655 655 break;
656 656
657 657 case DTRACEAGG_MAX:
658 658 h->dtahe_aggregate = dt_aggregate_max;
659 659 break;
660 660
661 661 case DTRACEAGG_LQUANTIZE:
662 662 h->dtahe_aggregate = dt_aggregate_lquantize;
663 663 break;
664 664
665 665 case DTRACEAGG_LLQUANTIZE:
666 666 h->dtahe_aggregate = dt_aggregate_llquantize;
667 667 break;
668 668
669 669 case DTRACEAGG_COUNT:
670 670 case DTRACEAGG_SUM:
671 671 case DTRACEAGG_AVG:
672 672 case DTRACEAGG_STDDEV:
673 673 case DTRACEAGG_QUANTIZE:
674 674 h->dtahe_aggregate = dt_aggregate_count;
675 675 break;
676 676
677 677 default:
678 678 return (dt_set_errno(dtp, EDT_BADAGG));
679 679 }
680 680
681 681 if (hash->dtah_hash[ndx] != NULL)
682 682 hash->dtah_hash[ndx]->dtahe_prev = h;
683 683
684 684 h->dtahe_next = hash->dtah_hash[ndx];
685 685 hash->dtah_hash[ndx] = h;
686 686
687 687 if (hash->dtah_all != NULL)
688 688 hash->dtah_all->dtahe_prevall = h;
689 689
690 690 h->dtahe_nextall = hash->dtah_all;
691 691 hash->dtah_all = h;
692 692 bufnext:
693 693 offs += agg->dtagd_size;
694 694 }
695 695
696 696 return (0);
697 697 }
698 698
699 699 int
700 700 dtrace_aggregate_snap(dtrace_hdl_t *dtp)
701 701 {
702 702 int i, rval;
703 703 dt_aggregate_t *agp = &dtp->dt_aggregate;
704 704 hrtime_t now = gethrtime();
705 705 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_AGGRATE];
706 706
707 707 if (dtp->dt_lastagg != 0) {
708 708 if (now - dtp->dt_lastagg < interval)
709 709 return (0);
710 710
711 711 dtp->dt_lastagg += interval;
712 712 } else {
713 713 dtp->dt_lastagg = now;
714 714 }
715 715
716 716 if (!dtp->dt_active)
717 717 return (dt_set_errno(dtp, EINVAL));
718 718
719 719 if (agp->dtat_buf.dtbd_size == 0)
720 720 return (0);
721 721
722 722 for (i = 0; i < agp->dtat_ncpus; i++) {
723 723 if (rval = dt_aggregate_snap_cpu(dtp, agp->dtat_cpus[i]))
724 724 return (rval);
725 725 }
726 726
727 727 return (0);
728 728 }
729 729
730 730 static int
731 731 dt_aggregate_hashcmp(const void *lhs, const void *rhs)
732 732 {
733 733 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
734 734 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
735 735 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
736 736 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
737 737
738 738 if (lagg->dtagd_nrecs < ragg->dtagd_nrecs)
739 739 return (DT_LESSTHAN);
740 740
741 741 if (lagg->dtagd_nrecs > ragg->dtagd_nrecs)
742 742 return (DT_GREATERTHAN);
743 743
744 744 return (0);
745 745 }
746 746
747 747 static int
748 748 dt_aggregate_varcmp(const void *lhs, const void *rhs)
749 749 {
750 750 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
751 751 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
752 752 dtrace_aggvarid_t lid, rid;
753 753
754 754 lid = dt_aggregate_aggvarid(lh);
755 755 rid = dt_aggregate_aggvarid(rh);
756 756
757 757 if (lid < rid)
758 758 return (DT_LESSTHAN);
759 759
760 760 if (lid > rid)
761 761 return (DT_GREATERTHAN);
762 762
763 763 return (0);
764 764 }
765 765
766 766 static int
767 767 dt_aggregate_keycmp(const void *lhs, const void *rhs)
768 768 {
769 769 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
770 770 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
771 771 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
772 772 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
773 773 dtrace_recdesc_t *lrec, *rrec;
774 774 char *ldata, *rdata;
775 775 int rval, i, j, keypos, nrecs;
776 776
777 777 if ((rval = dt_aggregate_hashcmp(lhs, rhs)) != 0)
778 778 return (rval);
779 779
780 780 nrecs = lagg->dtagd_nrecs - 1;
781 781 assert(nrecs == ragg->dtagd_nrecs - 1);
782 782
783 783 keypos = dt_keypos + 1 >= nrecs ? 0 : dt_keypos;
784 784
785 785 for (i = 1; i < nrecs; i++) {
786 786 uint64_t lval, rval;
787 787 int ndx = i + keypos;
788 788
789 789 if (ndx >= nrecs)
790 790 ndx = ndx - nrecs + 1;
791 791
792 792 lrec = &lagg->dtagd_rec[ndx];
793 793 rrec = &ragg->dtagd_rec[ndx];
794 794
795 795 ldata = lh->dtahe_data.dtada_data + lrec->dtrd_offset;
796 796 rdata = rh->dtahe_data.dtada_data + rrec->dtrd_offset;
797 797
798 798 if (lrec->dtrd_size < rrec->dtrd_size)
799 799 return (DT_LESSTHAN);
800 800
801 801 if (lrec->dtrd_size > rrec->dtrd_size)
802 802 return (DT_GREATERTHAN);
803 803
804 804 switch (lrec->dtrd_size) {
805 805 case sizeof (uint64_t):
806 806 /* LINTED - alignment */
807 807 lval = *((uint64_t *)ldata);
808 808 /* LINTED - alignment */
809 809 rval = *((uint64_t *)rdata);
810 810 break;
811 811
812 812 case sizeof (uint32_t):
813 813 /* LINTED - alignment */
814 814 lval = *((uint32_t *)ldata);
815 815 /* LINTED - alignment */
816 816 rval = *((uint32_t *)rdata);
817 817 break;
818 818
819 819 case sizeof (uint16_t):
820 820 /* LINTED - alignment */
821 821 lval = *((uint16_t *)ldata);
822 822 /* LINTED - alignment */
823 823 rval = *((uint16_t *)rdata);
824 824 break;
825 825
826 826 case sizeof (uint8_t):
827 827 lval = *((uint8_t *)ldata);
828 828 rval = *((uint8_t *)rdata);
829 829 break;
830 830
831 831 default:
832 832 switch (lrec->dtrd_action) {
833 833 case DTRACEACT_UMOD:
834 834 case DTRACEACT_UADDR:
835 835 case DTRACEACT_USYM:
836 836 for (j = 0; j < 2; j++) {
837 837 /* LINTED - alignment */
838 838 lval = ((uint64_t *)ldata)[j];
839 839 /* LINTED - alignment */
840 840 rval = ((uint64_t *)rdata)[j];
841 841
842 842 if (lval < rval)
843 843 return (DT_LESSTHAN);
844 844
845 845 if (lval > rval)
846 846 return (DT_GREATERTHAN);
847 847 }
848 848
849 849 break;
850 850
851 851 default:
852 852 for (j = 0; j < lrec->dtrd_size; j++) {
853 853 lval = ((uint8_t *)ldata)[j];
854 854 rval = ((uint8_t *)rdata)[j];
855 855
856 856 if (lval < rval)
857 857 return (DT_LESSTHAN);
858 858
859 859 if (lval > rval)
860 860 return (DT_GREATERTHAN);
861 861 }
862 862 }
863 863
864 864 continue;
865 865 }
866 866
867 867 if (lval < rval)
868 868 return (DT_LESSTHAN);
869 869
870 870 if (lval > rval)
871 871 return (DT_GREATERTHAN);
872 872 }
873 873
874 874 return (0);
875 875 }
876 876
877 877 static int
878 878 dt_aggregate_valcmp(const void *lhs, const void *rhs)
879 879 {
880 880 dt_ahashent_t *lh = *((dt_ahashent_t **)lhs);
881 881 dt_ahashent_t *rh = *((dt_ahashent_t **)rhs);
882 882 dtrace_aggdesc_t *lagg = lh->dtahe_data.dtada_desc;
883 883 dtrace_aggdesc_t *ragg = rh->dtahe_data.dtada_desc;
884 884 caddr_t ldata = lh->dtahe_data.dtada_data;
885 885 caddr_t rdata = rh->dtahe_data.dtada_data;
886 886 dtrace_recdesc_t *lrec, *rrec;
887 887 int64_t *laddr, *raddr;
888 888 int rval;
889 889
890 890 assert(lagg->dtagd_nrecs == ragg->dtagd_nrecs);
891 891
892 892 lrec = &lagg->dtagd_rec[lagg->dtagd_nrecs - 1];
893 893 rrec = &ragg->dtagd_rec[ragg->dtagd_nrecs - 1];
894 894
895 895 assert(lrec->dtrd_action == rrec->dtrd_action);
896 896
897 897 laddr = (int64_t *)(uintptr_t)(ldata + lrec->dtrd_offset);
898 898 raddr = (int64_t *)(uintptr_t)(rdata + rrec->dtrd_offset);
899 899
900 900 switch (lrec->dtrd_action) {
901 901 case DTRACEAGG_AVG:
902 902 rval = dt_aggregate_averagecmp(laddr, raddr);
903 903 break;
904 904
905 905 case DTRACEAGG_STDDEV:
906 906 rval = dt_aggregate_stddevcmp(laddr, raddr);
907 907 break;
908 908
909 909 case DTRACEAGG_QUANTIZE:
910 910 rval = dt_aggregate_quantizedcmp(laddr, raddr);
911 911 break;
912 912
913 913 case DTRACEAGG_LQUANTIZE:
914 914 rval = dt_aggregate_lquantizedcmp(laddr, raddr);
915 915 break;
916 916
917 917 case DTRACEAGG_LLQUANTIZE:
918 918 rval = dt_aggregate_llquantizedcmp(laddr, raddr);
919 919 break;
920 920
921 921 case DTRACEAGG_COUNT:
922 922 case DTRACEAGG_SUM:
923 923 case DTRACEAGG_MIN:
924 924 case DTRACEAGG_MAX:
925 925 rval = dt_aggregate_countcmp(laddr, raddr);
926 926 break;
927 927
928 928 default:
929 929 assert(0);
930 930 }
931 931
932 932 return (rval);
933 933 }
934 934
935 935 static int
936 936 dt_aggregate_valkeycmp(const void *lhs, const void *rhs)
937 937 {
938 938 int rval;
939 939
940 940 if ((rval = dt_aggregate_valcmp(lhs, rhs)) != 0)
941 941 return (rval);
942 942
943 943 /*
944 944 * If we're here, the values for the two aggregation elements are
945 945 * equal. We already know that the key layout is the same for the two
946 946 * elements; we must now compare the keys themselves as a tie-breaker.
947 947 */
948 948 return (dt_aggregate_keycmp(lhs, rhs));
949 949 }
950 950
951 951 static int
952 952 dt_aggregate_keyvarcmp(const void *lhs, const void *rhs)
953 953 {
954 954 int rval;
955 955
956 956 if ((rval = dt_aggregate_keycmp(lhs, rhs)) != 0)
957 957 return (rval);
958 958
959 959 return (dt_aggregate_varcmp(lhs, rhs));
960 960 }
961 961
962 962 static int
963 963 dt_aggregate_varkeycmp(const void *lhs, const void *rhs)
964 964 {
965 965 int rval;
966 966
967 967 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
968 968 return (rval);
969 969
970 970 return (dt_aggregate_keycmp(lhs, rhs));
971 971 }
972 972
973 973 static int
974 974 dt_aggregate_valvarcmp(const void *lhs, const void *rhs)
975 975 {
976 976 int rval;
977 977
978 978 if ((rval = dt_aggregate_valkeycmp(lhs, rhs)) != 0)
979 979 return (rval);
980 980
981 981 return (dt_aggregate_varcmp(lhs, rhs));
982 982 }
983 983
984 984 static int
985 985 dt_aggregate_varvalcmp(const void *lhs, const void *rhs)
986 986 {
987 987 int rval;
988 988
989 989 if ((rval = dt_aggregate_varcmp(lhs, rhs)) != 0)
990 990 return (rval);
991 991
992 992 return (dt_aggregate_valkeycmp(lhs, rhs));
993 993 }
994 994
995 995 static int
996 996 dt_aggregate_keyvarrevcmp(const void *lhs, const void *rhs)
997 997 {
998 998 return (dt_aggregate_keyvarcmp(rhs, lhs));
999 999 }
1000 1000
1001 1001 static int
1002 1002 dt_aggregate_varkeyrevcmp(const void *lhs, const void *rhs)
1003 1003 {
1004 1004 return (dt_aggregate_varkeycmp(rhs, lhs));
1005 1005 }
1006 1006
1007 1007 static int
1008 1008 dt_aggregate_valvarrevcmp(const void *lhs, const void *rhs)
1009 1009 {
1010 1010 return (dt_aggregate_valvarcmp(rhs, lhs));
1011 1011 }
1012 1012
1013 1013 static int
1014 1014 dt_aggregate_varvalrevcmp(const void *lhs, const void *rhs)
1015 1015 {
1016 1016 return (dt_aggregate_varvalcmp(rhs, lhs));
1017 1017 }
1018 1018
1019 1019 static int
1020 1020 dt_aggregate_bundlecmp(const void *lhs, const void *rhs)
1021 1021 {
1022 1022 dt_ahashent_t **lh = *((dt_ahashent_t ***)lhs);
1023 1023 dt_ahashent_t **rh = *((dt_ahashent_t ***)rhs);
1024 1024 int i, rval;
1025 1025
1026 1026 if (dt_keysort) {
1027 1027 /*
1028 1028 * If we're sorting on keys, we need to scan until we find the
1029 1029 * last entry -- that's the representative key. (The order of
1030 1030 * the bundle is values followed by key to accommodate the
1031 1031 * default behavior of sorting by value.) If the keys are
1032 1032 * equal, we'll fall into the value comparison loop, below.
1033 1033 */
1034 1034 for (i = 0; lh[i + 1] != NULL; i++)
1035 1035 continue;
1036 1036
1037 1037 assert(i != 0);
1038 1038 assert(rh[i + 1] == NULL);
1039 1039
1040 1040 if ((rval = dt_aggregate_keycmp(&lh[i], &rh[i])) != 0)
1041 1041 return (rval);
1042 1042 }
1043 1043
1044 1044 for (i = 0; ; i++) {
1045 1045 if (lh[i + 1] == NULL) {
1046 1046 /*
1047 1047 * All of the values are equal; if we're sorting on
1048 1048 * keys, then we're only here because the keys were
1049 1049 * found to be equal and these records are therefore
1050 1050 * equal. If we're not sorting on keys, we'll use the
1051 1051 * key comparison from the representative key as the
1052 1052 * tie-breaker.
1053 1053 */
1054 1054 if (dt_keysort)
1055 1055 return (0);
1056 1056
1057 1057 assert(i != 0);
1058 1058 assert(rh[i + 1] == NULL);
1059 1059 return (dt_aggregate_keycmp(&lh[i], &rh[i]));
1060 1060 } else {
1061 1061 if ((rval = dt_aggregate_valcmp(&lh[i], &rh[i])) != 0)
1062 1062 return (rval);
1063 1063 }
1064 1064 }
1065 1065 }
1066 1066
1067 1067 int
1068 1068 dt_aggregate_go(dtrace_hdl_t *dtp)
1069 1069 {
1070 1070 dt_aggregate_t *agp = &dtp->dt_aggregate;
1071 1071 dtrace_optval_t size, cpu;
1072 1072 dtrace_bufdesc_t *buf = &agp->dtat_buf;
1073 1073 int rval, i;
1074 1074
1075 1075 assert(agp->dtat_maxcpu == 0);
1076 1076 assert(agp->dtat_ncpu == 0);
1077 1077 assert(agp->dtat_cpus == NULL);
1078 1078
1079 1079 agp->dtat_maxcpu = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
1080 1080 agp->dtat_ncpu = dt_sysconf(dtp, _SC_NPROCESSORS_MAX);
1081 1081 agp->dtat_cpus = malloc(agp->dtat_ncpu * sizeof (processorid_t));
1082 1082
1083 1083 if (agp->dtat_cpus == NULL)
1084 1084 return (dt_set_errno(dtp, EDT_NOMEM));
1085 1085
1086 1086 /*
1087 1087 * Use the aggregation buffer size as reloaded from the kernel.
1088 1088 */
1089 1089 size = dtp->dt_options[DTRACEOPT_AGGSIZE];
1090 1090
1091 1091 rval = dtrace_getopt(dtp, "aggsize", &size);
1092 1092 assert(rval == 0);
1093 1093
1094 1094 if (size == 0 || size == DTRACEOPT_UNSET)
1095 1095 return (0);
1096 1096
1097 1097 buf = &agp->dtat_buf;
1098 1098 buf->dtbd_size = size;
1099 1099
1100 1100 if ((buf->dtbd_data = malloc(buf->dtbd_size)) == NULL)
1101 1101 return (dt_set_errno(dtp, EDT_NOMEM));
1102 1102
1103 1103 /*
1104 1104 * Now query for the CPUs enabled.
1105 1105 */
1106 1106 rval = dtrace_getopt(dtp, "cpu", &cpu);
1107 1107 assert(rval == 0 && cpu != DTRACEOPT_UNSET);
1108 1108
1109 1109 if (cpu != DTRACE_CPUALL) {
1110 1110 assert(cpu < agp->dtat_ncpu);
1111 1111 agp->dtat_cpus[agp->dtat_ncpus++] = (processorid_t)cpu;
1112 1112
1113 1113 return (0);
1114 1114 }
1115 1115
1116 1116 agp->dtat_ncpus = 0;
1117 1117 for (i = 0; i < agp->dtat_maxcpu; i++) {
1118 1118 if (dt_status(dtp, i) == -1)
1119 1119 continue;
1120 1120
1121 1121 agp->dtat_cpus[agp->dtat_ncpus++] = i;
1122 1122 }
1123 1123
1124 1124 return (0);
1125 1125 }
1126 1126
1127 1127 static int
1128 1128 dt_aggwalk_rval(dtrace_hdl_t *dtp, dt_ahashent_t *h, int rval)
1129 1129 {
1130 1130 dt_aggregate_t *agp = &dtp->dt_aggregate;
1131 1131 dtrace_aggdata_t *data;
1132 1132 dtrace_aggdesc_t *aggdesc;
1133 1133 dtrace_recdesc_t *rec;
1134 1134 int i;
1135 1135
1136 1136 switch (rval) {
1137 1137 case DTRACE_AGGWALK_NEXT:
1138 1138 break;
1139 1139
1140 1140 case DTRACE_AGGWALK_CLEAR: {
1141 1141 uint32_t size, offs = 0;
1142 1142
1143 1143 aggdesc = h->dtahe_data.dtada_desc;
1144 1144 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1145 1145 size = rec->dtrd_size;
1146 1146 data = &h->dtahe_data;
1147 1147
1148 1148 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1149 1149 offs = sizeof (uint64_t);
1150 1150 size -= sizeof (uint64_t);
1151 1151 }
1152 1152
1153 1153 bzero(&data->dtada_data[rec->dtrd_offset] + offs, size);
1154 1154
1155 1155 if (data->dtada_percpu == NULL)
1156 1156 break;
1157 1157
1158 1158 for (i = 0; i < dtp->dt_aggregate.dtat_maxcpu; i++)
1159 1159 bzero(data->dtada_percpu[i] + offs, size);
1160 1160 break;
1161 1161 }
1162 1162
1163 1163 case DTRACE_AGGWALK_ERROR:
1164 1164 /*
1165 1165 * We assume that errno is already set in this case.
1166 1166 */
1167 1167 return (dt_set_errno(dtp, errno));
1168 1168
1169 1169 case DTRACE_AGGWALK_ABORT:
1170 1170 return (dt_set_errno(dtp, EDT_DIRABORT));
1171 1171
1172 1172 case DTRACE_AGGWALK_DENORMALIZE:
1173 1173 h->dtahe_data.dtada_normal = 1;
1174 1174 return (0);
1175 1175
1176 1176 case DTRACE_AGGWALK_NORMALIZE:
1177 1177 if (h->dtahe_data.dtada_normal == 0) {
1178 1178 h->dtahe_data.dtada_normal = 1;
1179 1179 return (dt_set_errno(dtp, EDT_BADRVAL));
1180 1180 }
1181 1181
1182 1182 return (0);
1183 1183
1184 1184 case DTRACE_AGGWALK_REMOVE: {
1185 1185 dtrace_aggdata_t *aggdata = &h->dtahe_data;
1186 1186 int i, max_cpus = agp->dtat_maxcpu;
1187 1187
1188 1188 /*
1189 1189 * First, remove this hash entry from its hash chain.
1190 1190 */
1191 1191 if (h->dtahe_prev != NULL) {
1192 1192 h->dtahe_prev->dtahe_next = h->dtahe_next;
1193 1193 } else {
1194 1194 dt_ahash_t *hash = &agp->dtat_hash;
1195 1195 size_t ndx = h->dtahe_hashval % hash->dtah_size;
1196 1196
1197 1197 assert(hash->dtah_hash[ndx] == h);
1198 1198 hash->dtah_hash[ndx] = h->dtahe_next;
1199 1199 }
1200 1200
1201 1201 if (h->dtahe_next != NULL)
1202 1202 h->dtahe_next->dtahe_prev = h->dtahe_prev;
1203 1203
1204 1204 /*
1205 1205 * Now remove it from the list of all hash entries.
1206 1206 */
1207 1207 if (h->dtahe_prevall != NULL) {
1208 1208 h->dtahe_prevall->dtahe_nextall = h->dtahe_nextall;
1209 1209 } else {
1210 1210 dt_ahash_t *hash = &agp->dtat_hash;
1211 1211
1212 1212 assert(hash->dtah_all == h);
1213 1213 hash->dtah_all = h->dtahe_nextall;
1214 1214 }
1215 1215
1216 1216 if (h->dtahe_nextall != NULL)
1217 1217 h->dtahe_nextall->dtahe_prevall = h->dtahe_prevall;
1218 1218
1219 1219 /*
1220 1220 * We're unlinked. We can safely destroy the data.
1221 1221 */
1222 1222 if (aggdata->dtada_percpu != NULL) {
1223 1223 for (i = 0; i < max_cpus; i++)
1224 1224 free(aggdata->dtada_percpu[i]);
1225 1225 free(aggdata->dtada_percpu);
1226 1226 }
1227 1227
1228 1228 free(aggdata->dtada_data);
1229 1229 free(h);
1230 1230
1231 1231 return (0);
1232 1232 }
1233 1233
1234 1234 default:
1235 1235 return (dt_set_errno(dtp, EDT_BADRVAL));
1236 1236 }
1237 1237
1238 1238 return (0);
1239 1239 }
1240 1240
1241 1241 void
1242 1242 dt_aggregate_qsort(dtrace_hdl_t *dtp, void *base, size_t nel, size_t width,
1243 1243 int (*compar)(const void *, const void *))
1244 1244 {
1245 1245 int rev = dt_revsort, key = dt_keysort, keypos = dt_keypos;
1246 1246 dtrace_optval_t keyposopt = dtp->dt_options[DTRACEOPT_AGGSORTKEYPOS];
1247 1247
1248 1248 dt_revsort = (dtp->dt_options[DTRACEOPT_AGGSORTREV] != DTRACEOPT_UNSET);
1249 1249 dt_keysort = (dtp->dt_options[DTRACEOPT_AGGSORTKEY] != DTRACEOPT_UNSET);
1250 1250
1251 1251 if (keyposopt != DTRACEOPT_UNSET && keyposopt <= INT_MAX) {
1252 1252 dt_keypos = (int)keyposopt;
1253 1253 } else {
1254 1254 dt_keypos = 0;
1255 1255 }
1256 1256
1257 1257 if (compar == NULL) {
1258 1258 if (!dt_keysort) {
1259 1259 compar = dt_aggregate_varvalcmp;
1260 1260 } else {
1261 1261 compar = dt_aggregate_varkeycmp;
1262 1262 }
1263 1263 }
1264 1264
1265 1265 qsort(base, nel, width, compar);
1266 1266
1267 1267 dt_revsort = rev;
1268 1268 dt_keysort = key;
1269 1269 dt_keypos = keypos;
1270 1270 }
1271 1271
1272 1272 int
1273 1273 dtrace_aggregate_walk(dtrace_hdl_t *dtp, dtrace_aggregate_f *func, void *arg)
1274 1274 {
1275 1275 dt_ahashent_t *h, *next;
1276 1276 dt_ahash_t *hash = &dtp->dt_aggregate.dtat_hash;
1277 1277
1278 1278 for (h = hash->dtah_all; h != NULL; h = next) {
1279 1279 /*
1280 1280 * dt_aggwalk_rval() can potentially remove the current hash
1281 1281 * entry; we need to load the next hash entry before calling
1282 1282 * into it.
1283 1283 */
↓ open down ↓ |
1245 lines elided |
↑ open up ↑ |
1284 1284 next = h->dtahe_nextall;
1285 1285
1286 1286 if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1287 1287 return (-1);
1288 1288 }
1289 1289
1290 1290 return (0);
1291 1291 }
1292 1292
1293 1293 static int
1294 +dt_aggregate_total(dtrace_hdl_t *dtp, boolean_t clear)
1295 +{
1296 + dt_ahashent_t *h;
1297 + dtrace_aggdata_t **total;
1298 + dtrace_aggid_t max = DTRACE_AGGVARIDNONE, id;
1299 + dt_aggregate_t *agp = &dtp->dt_aggregate;
1300 + dt_ahash_t *hash = &agp->dtat_hash;
1301 + uint32_t tflags;
1302 +
1303 + tflags = DTRACE_A_TOTAL | DTRACE_A_HASNEGATIVES | DTRACE_A_HASPOSITIVES;
1304 +
1305 + /*
1306 + * If we need to deliver per-aggregation totals, we're going to take
1307 + * three passes over the aggregate: one to clear everything out and
1308 + * determine our maximum aggregation ID, one to actually total
1309 + * everything up, and a final pass to assign the totals to the
1310 + * individual elements.
1311 + */
1312 + for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1313 + dtrace_aggdata_t *aggdata = &h->dtahe_data;
1314 +
1315 + if ((id = dt_aggregate_aggvarid(h)) > max)
1316 + max = id;
1317 +
1318 + aggdata->dtada_total = 0;
1319 + aggdata->dtada_flags &= ~tflags;
1320 + }
1321 +
1322 + if (clear || max == DTRACE_AGGVARIDNONE)
1323 + return (0);
1324 +
1325 + total = dt_zalloc(dtp, (max + 1) * sizeof (dtrace_aggdata_t *));
1326 +
1327 + if (total == NULL)
1328 + return (-1);
1329 +
1330 + for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1331 + dtrace_aggdata_t *aggdata = &h->dtahe_data;
1332 + dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1333 + dtrace_recdesc_t *rec;
1334 + caddr_t data;
1335 + int64_t val, *addr;
1336 +
1337 + rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
1338 + data = aggdata->dtada_data;
1339 + addr = (int64_t *)(uintptr_t)(data + rec->dtrd_offset);
1340 +
1341 + switch (rec->dtrd_action) {
1342 + case DTRACEAGG_STDDEV:
1343 + val = dt_stddev((uint64_t *)addr, 1);
1344 + break;
1345 +
1346 + case DTRACEAGG_SUM:
1347 + case DTRACEAGG_COUNT:
1348 + val = *addr;
1349 + break;
1350 +
1351 + case DTRACEAGG_AVG:
1352 + val = addr[0] ? (addr[1] / addr[0]) : 0;
1353 + break;
1354 +
1355 + default:
1356 + continue;
1357 + }
1358 +
1359 + if (total[agg->dtagd_varid] == NULL) {
1360 + total[agg->dtagd_varid] = aggdata;
1361 + aggdata->dtada_flags |= DTRACE_A_TOTAL;
1362 + } else {
1363 + aggdata = total[agg->dtagd_varid];
1364 + }
1365 +
1366 + if (val > 0)
1367 + aggdata->dtada_flags |= DTRACE_A_HASPOSITIVES;
1368 +
1369 + if (val < 0) {
1370 + aggdata->dtada_flags |= DTRACE_A_HASNEGATIVES;
1371 + val = -val;
1372 + }
1373 +
1374 + if (dtp->dt_options[DTRACEOPT_AGGZOOM] != DTRACEOPT_UNSET) {
1375 + val = (int64_t)((long double)val *
1376 + (1 / DTRACE_AGGZOOM_MAX));
1377 +
1378 + if (val > aggdata->dtada_total)
1379 + aggdata->dtada_total = val;
1380 + } else {
1381 + aggdata->dtada_total += val;
1382 + }
1383 + }
1384 +
1385 + /*
1386 + * And now one final pass to set everyone's total.
1387 + */
1388 + for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1389 + dtrace_aggdata_t *aggdata = &h->dtahe_data, *t;
1390 + dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1391 +
1392 + if ((t = total[agg->dtagd_varid]) == NULL || aggdata == t)
1393 + continue;
1394 +
1395 + aggdata->dtada_total = t->dtada_total;
1396 + aggdata->dtada_flags |= (t->dtada_flags & tflags);
1397 + }
1398 +
1399 + dt_free(dtp, total);
1400 +
1401 + return (0);
1402 +}
1403 +
1404 +static int
1405 +dt_aggregate_minmaxbin(dtrace_hdl_t *dtp, boolean_t clear)
1406 +{
1407 + dt_ahashent_t *h;
1408 + dtrace_aggdata_t **minmax;
1409 + dtrace_aggid_t max = DTRACE_AGGVARIDNONE, id;
1410 + dt_aggregate_t *agp = &dtp->dt_aggregate;
1411 + dt_ahash_t *hash = &agp->dtat_hash;
1412 +
1413 + for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1414 + dtrace_aggdata_t *aggdata = &h->dtahe_data;
1415 +
1416 + if ((id = dt_aggregate_aggvarid(h)) > max)
1417 + max = id;
1418 +
1419 + aggdata->dtada_minbin = 0;
1420 + aggdata->dtada_maxbin = 0;
1421 + aggdata->dtada_flags &= ~DTRACE_A_MINMAXBIN;
1422 + }
1423 +
1424 + if (clear || max == DTRACE_AGGVARIDNONE)
1425 + return (0);
1426 +
1427 + minmax = dt_zalloc(dtp, (max + 1) * sizeof (dtrace_aggdata_t *));
1428 +
1429 + if (minmax == NULL)
1430 + return (-1);
1431 +
1432 + for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1433 + dtrace_aggdata_t *aggdata = &h->dtahe_data;
1434 + dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1435 + dtrace_recdesc_t *rec;
1436 + caddr_t data;
1437 + int64_t *addr;
1438 + int minbin = -1, maxbin = -1, i;
1439 + int start = 0, size;
1440 +
1441 + rec = &agg->dtagd_rec[agg->dtagd_nrecs - 1];
1442 + size = rec->dtrd_size / sizeof (int64_t);
1443 + data = aggdata->dtada_data;
1444 + addr = (int64_t *)(uintptr_t)(data + rec->dtrd_offset);
1445 +
1446 + switch (rec->dtrd_action) {
1447 + case DTRACEAGG_LQUANTIZE:
1448 + /*
1449 + * For lquantize(), we always display the entire range
1450 + * of the aggregation when aggpack is set.
1451 + */
1452 + start = 1;
1453 + minbin = start;
1454 + maxbin = size - 1 - start;
1455 + break;
1456 +
1457 + case DTRACEAGG_QUANTIZE:
1458 + for (i = start; i < size; i++) {
1459 + if (!addr[i])
1460 + continue;
1461 +
1462 + if (minbin == -1)
1463 + minbin = i - start;
1464 +
1465 + maxbin = i - start;
1466 + }
1467 +
1468 + if (minbin == -1) {
1469 + /*
1470 + * If we have no data (e.g., due to a clear()
1471 + * or negative increments), we'll use the
1472 + * zero bucket as both our min and max.
1473 + */
1474 + minbin = maxbin = DTRACE_QUANTIZE_ZEROBUCKET;
1475 + }
1476 +
1477 + break;
1478 +
1479 + default:
1480 + continue;
1481 + }
1482 +
1483 + if (minmax[agg->dtagd_varid] == NULL) {
1484 + minmax[agg->dtagd_varid] = aggdata;
1485 + aggdata->dtada_flags |= DTRACE_A_MINMAXBIN;
1486 + aggdata->dtada_minbin = minbin;
1487 + aggdata->dtada_maxbin = maxbin;
1488 + continue;
1489 + }
1490 +
1491 + if (minbin < minmax[agg->dtagd_varid]->dtada_minbin)
1492 + minmax[agg->dtagd_varid]->dtada_minbin = minbin;
1493 +
1494 + if (maxbin > minmax[agg->dtagd_varid]->dtada_maxbin)
1495 + minmax[agg->dtagd_varid]->dtada_maxbin = maxbin;
1496 + }
1497 +
1498 + /*
1499 + * And now one final pass to set everyone's minbin and maxbin.
1500 + */
1501 + for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1502 + dtrace_aggdata_t *aggdata = &h->dtahe_data, *mm;
1503 + dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1504 +
1505 + if ((mm = minmax[agg->dtagd_varid]) == NULL || aggdata == mm)
1506 + continue;
1507 +
1508 + aggdata->dtada_minbin = mm->dtada_minbin;
1509 + aggdata->dtada_maxbin = mm->dtada_maxbin;
1510 + aggdata->dtada_flags |= DTRACE_A_MINMAXBIN;
1511 + }
1512 +
1513 + dt_free(dtp, minmax);
1514 +
1515 + return (0);
1516 +}
1517 +
1518 +static int
1294 1519 dt_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1295 1520 dtrace_aggregate_f *func, void *arg,
1296 1521 int (*sfunc)(const void *, const void *))
1297 1522 {
1298 1523 dt_aggregate_t *agp = &dtp->dt_aggregate;
1299 1524 dt_ahashent_t *h, **sorted;
1300 1525 dt_ahash_t *hash = &agp->dtat_hash;
1301 1526 size_t i, nentries = 0;
1527 + int rval = -1;
1528 +
1529 + agp->dtat_flags &= ~(DTRACE_A_TOTAL | DTRACE_A_MINMAXBIN);
1530 +
1531 + if (dtp->dt_options[DTRACEOPT_AGGHIST] != DTRACEOPT_UNSET) {
1532 + agp->dtat_flags |= DTRACE_A_TOTAL;
1533 +
1534 + if (dt_aggregate_total(dtp, B_FALSE) != 0)
1535 + return (-1);
1536 + }
1537 +
1538 + if (dtp->dt_options[DTRACEOPT_AGGPACK] != DTRACEOPT_UNSET) {
1539 + agp->dtat_flags |= DTRACE_A_MINMAXBIN;
1540 +
1541 + if (dt_aggregate_minmaxbin(dtp, B_FALSE) != 0)
1542 + return (-1);
1543 + }
1302 1544
1303 1545 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall)
1304 1546 nentries++;
1305 1547
1306 1548 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1307 1549
1308 1550 if (sorted == NULL)
1309 - return (-1);
1551 + goto out;
1310 1552
1311 1553 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall)
1312 1554 sorted[i++] = h;
1313 1555
1314 1556 (void) pthread_mutex_lock(&dt_qsort_lock);
1315 1557
1316 1558 if (sfunc == NULL) {
1317 1559 dt_aggregate_qsort(dtp, sorted, nentries,
1318 1560 sizeof (dt_ahashent_t *), NULL);
1319 1561 } else {
1320 1562 /*
1321 1563 * If we've been explicitly passed a sorting function,
1322 1564 * we'll use that -- ignoring the values of the "aggsortrev",
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
1323 1565 * "aggsortkey" and "aggsortkeypos" options.
1324 1566 */
1325 1567 qsort(sorted, nentries, sizeof (dt_ahashent_t *), sfunc);
1326 1568 }
1327 1569
1328 1570 (void) pthread_mutex_unlock(&dt_qsort_lock);
1329 1571
1330 1572 for (i = 0; i < nentries; i++) {
1331 1573 h = sorted[i];
1332 1574
1333 - if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1) {
1334 - dt_free(dtp, sorted);
1335 - return (-1);
1336 - }
1575 + if (dt_aggwalk_rval(dtp, h, func(&h->dtahe_data, arg)) == -1)
1576 + goto out;
1337 1577 }
1338 1578
1579 + rval = 0;
1580 +out:
1581 + if (agp->dtat_flags & DTRACE_A_TOTAL)
1582 + (void) dt_aggregate_total(dtp, B_TRUE);
1583 +
1584 + if (agp->dtat_flags & DTRACE_A_MINMAXBIN)
1585 + (void) dt_aggregate_minmaxbin(dtp, B_TRUE);
1586 +
1339 1587 dt_free(dtp, sorted);
1340 - return (0);
1588 + return (rval);
1341 1589 }
1342 1590
1343 1591 int
1344 1592 dtrace_aggregate_walk_sorted(dtrace_hdl_t *dtp,
1345 1593 dtrace_aggregate_f *func, void *arg)
1346 1594 {
1347 1595 return (dt_aggregate_walk_sorted(dtp, func, arg, NULL));
1348 1596 }
1349 1597
1350 1598 int
1351 1599 dtrace_aggregate_walk_keysorted(dtrace_hdl_t *dtp,
1352 1600 dtrace_aggregate_f *func, void *arg)
1353 1601 {
1354 1602 return (dt_aggregate_walk_sorted(dtp, func,
1355 1603 arg, dt_aggregate_varkeycmp));
1356 1604 }
1357 1605
1358 1606 int
1359 1607 dtrace_aggregate_walk_valsorted(dtrace_hdl_t *dtp,
1360 1608 dtrace_aggregate_f *func, void *arg)
1361 1609 {
1362 1610 return (dt_aggregate_walk_sorted(dtp, func,
1363 1611 arg, dt_aggregate_varvalcmp));
1364 1612 }
1365 1613
1366 1614 int
1367 1615 dtrace_aggregate_walk_keyvarsorted(dtrace_hdl_t *dtp,
1368 1616 dtrace_aggregate_f *func, void *arg)
1369 1617 {
1370 1618 return (dt_aggregate_walk_sorted(dtp, func,
1371 1619 arg, dt_aggregate_keyvarcmp));
1372 1620 }
1373 1621
1374 1622 int
1375 1623 dtrace_aggregate_walk_valvarsorted(dtrace_hdl_t *dtp,
1376 1624 dtrace_aggregate_f *func, void *arg)
1377 1625 {
1378 1626 return (dt_aggregate_walk_sorted(dtp, func,
1379 1627 arg, dt_aggregate_valvarcmp));
1380 1628 }
1381 1629
1382 1630 int
1383 1631 dtrace_aggregate_walk_keyrevsorted(dtrace_hdl_t *dtp,
1384 1632 dtrace_aggregate_f *func, void *arg)
1385 1633 {
1386 1634 return (dt_aggregate_walk_sorted(dtp, func,
1387 1635 arg, dt_aggregate_varkeyrevcmp));
1388 1636 }
1389 1637
1390 1638 int
1391 1639 dtrace_aggregate_walk_valrevsorted(dtrace_hdl_t *dtp,
1392 1640 dtrace_aggregate_f *func, void *arg)
1393 1641 {
1394 1642 return (dt_aggregate_walk_sorted(dtp, func,
1395 1643 arg, dt_aggregate_varvalrevcmp));
1396 1644 }
1397 1645
1398 1646 int
1399 1647 dtrace_aggregate_walk_keyvarrevsorted(dtrace_hdl_t *dtp,
1400 1648 dtrace_aggregate_f *func, void *arg)
1401 1649 {
1402 1650 return (dt_aggregate_walk_sorted(dtp, func,
1403 1651 arg, dt_aggregate_keyvarrevcmp));
1404 1652 }
1405 1653
1406 1654 int
1407 1655 dtrace_aggregate_walk_valvarrevsorted(dtrace_hdl_t *dtp,
1408 1656 dtrace_aggregate_f *func, void *arg)
1409 1657 {
1410 1658 return (dt_aggregate_walk_sorted(dtp, func,
1411 1659 arg, dt_aggregate_valvarrevcmp));
1412 1660 }
1413 1661
1414 1662 int
1415 1663 dtrace_aggregate_walk_joined(dtrace_hdl_t *dtp, dtrace_aggvarid_t *aggvars,
1416 1664 int naggvars, dtrace_aggregate_walk_joined_f *func, void *arg)
1417 1665 {
1418 1666 dt_aggregate_t *agp = &dtp->dt_aggregate;
1419 1667 dt_ahashent_t *h, **sorted = NULL, ***bundle, **nbundle;
1420 1668 const dtrace_aggdata_t **data;
1421 1669 dt_ahashent_t *zaggdata = NULL;
1422 1670 dt_ahash_t *hash = &agp->dtat_hash;
1423 1671 size_t nentries = 0, nbundles = 0, start, zsize = 0, bundlesize;
1424 1672 dtrace_aggvarid_t max = 0, aggvar;
1425 1673 int rval = -1, *map, *remap = NULL;
1426 1674 int i, j;
1427 1675 dtrace_optval_t sortpos = dtp->dt_options[DTRACEOPT_AGGSORTPOS];
1428 1676
1429 1677 /*
1430 1678 * If the sorting position is greater than the number of aggregation
1431 1679 * variable IDs, we silently set it to 0.
1432 1680 */
1433 1681 if (sortpos == DTRACEOPT_UNSET || sortpos >= naggvars)
1434 1682 sortpos = 0;
1435 1683
1436 1684 /*
1437 1685 * First we need to translate the specified aggregation variable IDs
1438 1686 * into a linear map that will allow us to translate an aggregation
1439 1687 * variable ID into its position in the specified aggvars.
1440 1688 */
1441 1689 for (i = 0; i < naggvars; i++) {
1442 1690 if (aggvars[i] == DTRACE_AGGVARIDNONE || aggvars[i] < 0)
1443 1691 return (dt_set_errno(dtp, EDT_BADAGGVAR));
1444 1692
1445 1693 if (aggvars[i] > max)
1446 1694 max = aggvars[i];
1447 1695 }
1448 1696
1449 1697 if ((map = dt_zalloc(dtp, (max + 1) * sizeof (int))) == NULL)
1450 1698 return (-1);
1451 1699
1452 1700 zaggdata = dt_zalloc(dtp, naggvars * sizeof (dt_ahashent_t));
1453 1701
1454 1702 if (zaggdata == NULL)
1455 1703 goto out;
1456 1704
1457 1705 for (i = 0; i < naggvars; i++) {
1458 1706 int ndx = i + sortpos;
1459 1707
1460 1708 if (ndx >= naggvars)
1461 1709 ndx -= naggvars;
1462 1710
1463 1711 aggvar = aggvars[ndx];
1464 1712 assert(aggvar <= max);
1465 1713
1466 1714 if (map[aggvar]) {
1467 1715 /*
1468 1716 * We have an aggregation variable that is present
1469 1717 * more than once in the array of aggregation
1470 1718 * variables. While it's unclear why one might want
1471 1719 * to do this, it's legal. To support this construct,
1472 1720 * we will allocate a remap that will indicate the
1473 1721 * position from which this aggregation variable
1474 1722 * should be pulled. (That is, where the remap will
1475 1723 * map from one position to another.)
1476 1724 */
1477 1725 if (remap == NULL) {
1478 1726 remap = dt_zalloc(dtp, naggvars * sizeof (int));
1479 1727
1480 1728 if (remap == NULL)
1481 1729 goto out;
1482 1730 }
1483 1731
1484 1732 /*
1485 1733 * Given that the variable is already present, assert
1486 1734 * that following through the mapping and adjusting
1487 1735 * for the sort position yields the same aggregation
1488 1736 * variable ID.
1489 1737 */
1490 1738 assert(aggvars[(map[aggvar] - 1 + sortpos) %
1491 1739 naggvars] == aggvars[ndx]);
1492 1740
1493 1741 remap[i] = map[aggvar];
1494 1742 continue;
1495 1743 }
1496 1744
1497 1745 map[aggvar] = i + 1;
1498 1746 }
1499 1747
1500 1748 /*
1501 1749 * We need to take two passes over the data to size our allocation, so
1502 1750 * we'll use the first pass to also fill in the zero-filled data to be
1503 1751 * used to properly format a zero-valued aggregation.
1504 1752 */
1505 1753 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1506 1754 dtrace_aggvarid_t id;
1507 1755 int ndx;
1508 1756
1509 1757 if ((id = dt_aggregate_aggvarid(h)) > max || !(ndx = map[id]))
1510 1758 continue;
1511 1759
1512 1760 if (zaggdata[ndx - 1].dtahe_size == 0) {
1513 1761 zaggdata[ndx - 1].dtahe_size = h->dtahe_size;
1514 1762 zaggdata[ndx - 1].dtahe_data = h->dtahe_data;
1515 1763 }
1516 1764
1517 1765 nentries++;
1518 1766 }
1519 1767
1520 1768 if (nentries == 0) {
1521 1769 /*
1522 1770 * We couldn't find any entries; there is nothing else to do.
1523 1771 */
1524 1772 rval = 0;
1525 1773 goto out;
1526 1774 }
1527 1775
1528 1776 /*
1529 1777 * Before we sort the data, we're going to look for any holes in our
1530 1778 * zero-filled data. This will occur if an aggregation variable that
1531 1779 * we are being asked to print has not yet been assigned the result of
1532 1780 * any aggregating action for _any_ tuple. The issue becomes that we
1533 1781 * would like a zero value to be printed for all columns for this
1534 1782 * aggregation, but without any record description, we don't know the
1535 1783 * aggregating action that corresponds to the aggregation variable. To
1536 1784 * try to find a match, we're simply going to lookup aggregation IDs
1537 1785 * (which are guaranteed to be contiguous and to start from 1), looking
1538 1786 * for the specified aggregation variable ID. If we find a match,
1539 1787 * we'll use that. If we iterate over all aggregation IDs and don't
1540 1788 * find a match, then we must be an anonymous enabling. (Anonymous
1541 1789 * enablings can't currently derive either aggregation variable IDs or
1542 1790 * aggregation variable names given only an aggregation ID.) In this
1543 1791 * obscure case (anonymous enabling, multiple aggregation printa() with
1544 1792 * some aggregations not represented for any tuple), our defined
1545 1793 * behavior is that the zero will be printed in the format of the first
1546 1794 * aggregation variable that contains any non-zero value.
1547 1795 */
1548 1796 for (i = 0; i < naggvars; i++) {
1549 1797 if (zaggdata[i].dtahe_size == 0) {
1550 1798 dtrace_aggvarid_t aggvar;
1551 1799
1552 1800 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1553 1801 assert(zaggdata[i].dtahe_data.dtada_data == NULL);
1554 1802
1555 1803 for (j = DTRACE_AGGIDNONE + 1; ; j++) {
1556 1804 dtrace_aggdesc_t *agg;
1557 1805 dtrace_aggdata_t *aggdata;
1558 1806
1559 1807 if (dt_aggid_lookup(dtp, j, &agg) != 0)
1560 1808 break;
1561 1809
1562 1810 if (agg->dtagd_varid != aggvar)
1563 1811 continue;
1564 1812
1565 1813 /*
1566 1814 * We have our description -- now we need to
1567 1815 * cons up the zaggdata entry for it.
1568 1816 */
1569 1817 aggdata = &zaggdata[i].dtahe_data;
1570 1818 aggdata->dtada_size = agg->dtagd_size;
1571 1819 aggdata->dtada_desc = agg;
1572 1820 aggdata->dtada_handle = dtp;
1573 1821 (void) dt_epid_lookup(dtp, agg->dtagd_epid,
1574 1822 &aggdata->dtada_edesc,
1575 1823 &aggdata->dtada_pdesc);
1576 1824 aggdata->dtada_normal = 1;
1577 1825 zaggdata[i].dtahe_hashval = 0;
1578 1826 zaggdata[i].dtahe_size = agg->dtagd_size;
1579 1827 break;
1580 1828 }
1581 1829
1582 1830 if (zaggdata[i].dtahe_size == 0) {
1583 1831 caddr_t data;
1584 1832
1585 1833 /*
1586 1834 * We couldn't find this aggregation, meaning
1587 1835 * that we have never seen it before for any
1588 1836 * tuple _and_ this is an anonymous enabling.
1589 1837 * That is, we're in the obscure case outlined
1590 1838 * above. In this case, our defined behavior
1591 1839 * is to format the data in the format of the
1592 1840 * first non-zero aggregation -- of which, of
1593 1841 * course, we know there to be at least one
1594 1842 * (or nentries would have been zero).
1595 1843 */
1596 1844 for (j = 0; j < naggvars; j++) {
1597 1845 if (zaggdata[j].dtahe_size != 0)
1598 1846 break;
1599 1847 }
1600 1848
1601 1849 assert(j < naggvars);
1602 1850 zaggdata[i] = zaggdata[j];
1603 1851
1604 1852 data = zaggdata[i].dtahe_data.dtada_data;
1605 1853 assert(data != NULL);
1606 1854 }
1607 1855 }
1608 1856 }
1609 1857
1610 1858 /*
1611 1859 * Now we need to allocate our zero-filled data for use for
1612 1860 * aggregations that don't have a value corresponding to a given key.
1613 1861 */
1614 1862 for (i = 0; i < naggvars; i++) {
1615 1863 dtrace_aggdata_t *aggdata = &zaggdata[i].dtahe_data;
1616 1864 dtrace_aggdesc_t *aggdesc = aggdata->dtada_desc;
1617 1865 dtrace_recdesc_t *rec;
1618 1866 uint64_t larg;
1619 1867 caddr_t zdata;
1620 1868
1621 1869 zsize = zaggdata[i].dtahe_size;
1622 1870 assert(zsize != 0);
1623 1871
1624 1872 if ((zdata = dt_zalloc(dtp, zsize)) == NULL) {
1625 1873 /*
1626 1874 * If we failed to allocated some zero-filled data, we
1627 1875 * need to zero out the remaining dtada_data pointers
1628 1876 * to prevent the wrong data from being freed below.
1629 1877 */
1630 1878 for (j = i; j < naggvars; j++)
1631 1879 zaggdata[j].dtahe_data.dtada_data = NULL;
1632 1880 goto out;
1633 1881 }
1634 1882
1635 1883 aggvar = aggvars[(i - sortpos + naggvars) % naggvars];
1636 1884
1637 1885 /*
1638 1886 * First, the easy bit. To maintain compatibility with
1639 1887 * consumers that pull the compiler-generated ID out of the
1640 1888 * data, we put that ID at the top of the zero-filled data.
1641 1889 */
1642 1890 rec = &aggdesc->dtagd_rec[0];
1643 1891 /* LINTED - alignment */
1644 1892 *((dtrace_aggvarid_t *)(zdata + rec->dtrd_offset)) = aggvar;
1645 1893
1646 1894 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1647 1895
1648 1896 /*
1649 1897 * Now for the more complicated part. If (and only if) this
1650 1898 * is an lquantize() aggregating action, zero-filled data is
1651 1899 * not equivalent to an empty record: we must also get the
1652 1900 * parameters for the lquantize().
1653 1901 */
1654 1902 if (rec->dtrd_action == DTRACEAGG_LQUANTIZE) {
1655 1903 if (aggdata->dtada_data != NULL) {
1656 1904 /*
1657 1905 * The easier case here is if we actually have
1658 1906 * some prototype data -- in which case we
1659 1907 * manually dig it out of the aggregation
1660 1908 * record.
1661 1909 */
1662 1910 /* LINTED - alignment */
1663 1911 larg = *((uint64_t *)(aggdata->dtada_data +
1664 1912 rec->dtrd_offset));
1665 1913 } else {
1666 1914 /*
1667 1915 * We don't have any prototype data. As a
1668 1916 * result, we know that we _do_ have the
1669 1917 * compiler-generated information. (If this
1670 1918 * were an anonymous enabling, all of our
1671 1919 * zero-filled data would have prototype data
1672 1920 * -- either directly or indirectly.) So as
1673 1921 * gross as it is, we'll grovel around in the
1674 1922 * compiler-generated information to find the
1675 1923 * lquantize() parameters.
1676 1924 */
1677 1925 dtrace_stmtdesc_t *sdp;
1678 1926 dt_ident_t *aid;
1679 1927 dt_idsig_t *isp;
1680 1928
1681 1929 sdp = (dtrace_stmtdesc_t *)(uintptr_t)
1682 1930 aggdesc->dtagd_rec[0].dtrd_uarg;
1683 1931 aid = sdp->dtsd_aggdata;
1684 1932 isp = (dt_idsig_t *)aid->di_data;
1685 1933 assert(isp->dis_auxinfo != 0);
1686 1934 larg = isp->dis_auxinfo;
1687 1935 }
1688 1936
1689 1937 /* LINTED - alignment */
1690 1938 *((uint64_t *)(zdata + rec->dtrd_offset)) = larg;
1691 1939 }
1692 1940
1693 1941 aggdata->dtada_data = zdata;
1694 1942 }
1695 1943
1696 1944 /*
1697 1945 * Now that we've dealt with setting up our zero-filled data, we can
1698 1946 * allocate our sorted array, and take another pass over the data to
1699 1947 * fill it.
1700 1948 */
1701 1949 sorted = dt_alloc(dtp, nentries * sizeof (dt_ahashent_t *));
1702 1950
1703 1951 if (sorted == NULL)
1704 1952 goto out;
1705 1953
1706 1954 for (h = hash->dtah_all, i = 0; h != NULL; h = h->dtahe_nextall) {
1707 1955 dtrace_aggvarid_t id;
1708 1956
1709 1957 if ((id = dt_aggregate_aggvarid(h)) > max || !map[id])
1710 1958 continue;
1711 1959
1712 1960 sorted[i++] = h;
1713 1961 }
1714 1962
1715 1963 assert(i == nentries);
1716 1964
1717 1965 /*
1718 1966 * We've loaded our array; now we need to sort by value to allow us
1719 1967 * to create bundles of like value. We're going to acquire the
1720 1968 * dt_qsort_lock here, and hold it across all of our subsequent
1721 1969 * comparison and sorting.
1722 1970 */
1723 1971 (void) pthread_mutex_lock(&dt_qsort_lock);
1724 1972
1725 1973 qsort(sorted, nentries, sizeof (dt_ahashent_t *),
1726 1974 dt_aggregate_keyvarcmp);
1727 1975
1728 1976 /*
1729 1977 * Now we need to go through and create bundles. Because the number
1730 1978 * of bundles is bounded by the size of the sorted array, we're going
1731 1979 * to reuse the underlying storage. And note that "bundle" is an
1732 1980 * array of pointers to arrays of pointers to dt_ahashent_t -- making
1733 1981 * its type (regrettably) "dt_ahashent_t ***". (Regrettable because
1734 1982 * '*' -- like '_' and 'X' -- should never appear in triplicate in
1735 1983 * an ideal world.)
1736 1984 */
1737 1985 bundle = (dt_ahashent_t ***)sorted;
1738 1986
1739 1987 for (i = 1, start = 0; i <= nentries; i++) {
1740 1988 if (i < nentries &&
1741 1989 dt_aggregate_keycmp(&sorted[i], &sorted[i - 1]) == 0)
1742 1990 continue;
1743 1991
1744 1992 /*
1745 1993 * We have a bundle boundary. Everything from start to
1746 1994 * (i - 1) belongs in one bundle.
1747 1995 */
1748 1996 assert(i - start <= naggvars);
1749 1997 bundlesize = (naggvars + 2) * sizeof (dt_ahashent_t *);
1750 1998
1751 1999 if ((nbundle = dt_zalloc(dtp, bundlesize)) == NULL) {
1752 2000 (void) pthread_mutex_unlock(&dt_qsort_lock);
1753 2001 goto out;
1754 2002 }
1755 2003
1756 2004 for (j = start; j < i; j++) {
1757 2005 dtrace_aggvarid_t id = dt_aggregate_aggvarid(sorted[j]);
1758 2006
1759 2007 assert(id <= max);
1760 2008 assert(map[id] != 0);
1761 2009 assert(map[id] - 1 < naggvars);
1762 2010 assert(nbundle[map[id] - 1] == NULL);
1763 2011 nbundle[map[id] - 1] = sorted[j];
1764 2012
1765 2013 if (nbundle[naggvars] == NULL)
1766 2014 nbundle[naggvars] = sorted[j];
1767 2015 }
1768 2016
1769 2017 for (j = 0; j < naggvars; j++) {
1770 2018 if (nbundle[j] != NULL)
1771 2019 continue;
1772 2020
1773 2021 /*
1774 2022 * Before we assume that this aggregation variable
1775 2023 * isn't present (and fall back to using the
1776 2024 * zero-filled data allocated earlier), check the
1777 2025 * remap. If we have a remapping, we'll drop it in
1778 2026 * here. Note that we might be remapping an
1779 2027 * aggregation variable that isn't present for this
1780 2028 * key; in this case, the aggregation data that we
1781 2029 * copy will point to the zeroed data.
1782 2030 */
1783 2031 if (remap != NULL && remap[j]) {
1784 2032 assert(remap[j] - 1 < j);
1785 2033 assert(nbundle[remap[j] - 1] != NULL);
1786 2034 nbundle[j] = nbundle[remap[j] - 1];
1787 2035 } else {
1788 2036 nbundle[j] = &zaggdata[j];
1789 2037 }
1790 2038 }
1791 2039
1792 2040 bundle[nbundles++] = nbundle;
1793 2041 start = i;
1794 2042 }
1795 2043
1796 2044 /*
1797 2045 * Now we need to re-sort based on the first value.
1798 2046 */
1799 2047 dt_aggregate_qsort(dtp, bundle, nbundles, sizeof (dt_ahashent_t **),
1800 2048 dt_aggregate_bundlecmp);
1801 2049
1802 2050 (void) pthread_mutex_unlock(&dt_qsort_lock);
1803 2051
1804 2052 /*
1805 2053 * We're done! Now we just need to go back over the sorted bundles,
1806 2054 * calling the function.
1807 2055 */
1808 2056 data = alloca((naggvars + 1) * sizeof (dtrace_aggdata_t *));
1809 2057
1810 2058 for (i = 0; i < nbundles; i++) {
1811 2059 for (j = 0; j < naggvars; j++)
1812 2060 data[j + 1] = NULL;
1813 2061
1814 2062 for (j = 0; j < naggvars; j++) {
1815 2063 int ndx = j - sortpos;
1816 2064
1817 2065 if (ndx < 0)
1818 2066 ndx += naggvars;
1819 2067
1820 2068 assert(bundle[i][ndx] != NULL);
1821 2069 data[j + 1] = &bundle[i][ndx]->dtahe_data;
1822 2070 }
1823 2071
1824 2072 for (j = 0; j < naggvars; j++)
1825 2073 assert(data[j + 1] != NULL);
1826 2074
1827 2075 /*
1828 2076 * The representative key is the last element in the bundle.
1829 2077 * Assert that we have one, and then set it to be the first
1830 2078 * element of data.
1831 2079 */
1832 2080 assert(bundle[i][j] != NULL);
1833 2081 data[0] = &bundle[i][j]->dtahe_data;
1834 2082
1835 2083 if ((rval = func(data, naggvars + 1, arg)) == -1)
1836 2084 goto out;
1837 2085 }
1838 2086
1839 2087 rval = 0;
1840 2088 out:
1841 2089 for (i = 0; i < nbundles; i++)
1842 2090 dt_free(dtp, bundle[i]);
1843 2091
1844 2092 if (zaggdata != NULL) {
1845 2093 for (i = 0; i < naggvars; i++)
1846 2094 dt_free(dtp, zaggdata[i].dtahe_data.dtada_data);
1847 2095 }
1848 2096
1849 2097 dt_free(dtp, zaggdata);
1850 2098 dt_free(dtp, sorted);
1851 2099 dt_free(dtp, remap);
1852 2100 dt_free(dtp, map);
↓ open down ↓ |
502 lines elided |
↑ open up ↑ |
1853 2101
1854 2102 return (rval);
1855 2103 }
1856 2104
1857 2105 int
1858 2106 dtrace_aggregate_print(dtrace_hdl_t *dtp, FILE *fp,
1859 2107 dtrace_aggregate_walk_f *func)
1860 2108 {
1861 2109 dt_print_aggdata_t pd;
1862 2110
2111 + bzero(&pd, sizeof (pd));
2112 +
1863 2113 pd.dtpa_dtp = dtp;
1864 2114 pd.dtpa_fp = fp;
1865 2115 pd.dtpa_allunprint = 1;
1866 2116
1867 2117 if (func == NULL)
1868 2118 func = dtrace_aggregate_walk_sorted;
1869 2119
1870 2120 if ((*func)(dtp, dt_print_agg, &pd) == -1)
1871 2121 return (dt_set_errno(dtp, dtp->dt_errno));
1872 2122
1873 2123 return (0);
1874 2124 }
1875 2125
1876 2126 void
1877 2127 dtrace_aggregate_clear(dtrace_hdl_t *dtp)
1878 2128 {
1879 2129 dt_aggregate_t *agp = &dtp->dt_aggregate;
1880 2130 dt_ahash_t *hash = &agp->dtat_hash;
1881 2131 dt_ahashent_t *h;
1882 2132 dtrace_aggdata_t *data;
1883 2133 dtrace_aggdesc_t *aggdesc;
1884 2134 dtrace_recdesc_t *rec;
1885 2135 int i, max_cpus = agp->dtat_maxcpu;
1886 2136
1887 2137 for (h = hash->dtah_all; h != NULL; h = h->dtahe_nextall) {
1888 2138 aggdesc = h->dtahe_data.dtada_desc;
1889 2139 rec = &aggdesc->dtagd_rec[aggdesc->dtagd_nrecs - 1];
1890 2140 data = &h->dtahe_data;
1891 2141
1892 2142 bzero(&data->dtada_data[rec->dtrd_offset], rec->dtrd_size);
1893 2143
1894 2144 if (data->dtada_percpu == NULL)
1895 2145 continue;
1896 2146
1897 2147 for (i = 0; i < max_cpus; i++)
1898 2148 bzero(data->dtada_percpu[i], rec->dtrd_size);
1899 2149 }
1900 2150 }
1901 2151
1902 2152 void
1903 2153 dt_aggregate_destroy(dtrace_hdl_t *dtp)
1904 2154 {
1905 2155 dt_aggregate_t *agp = &dtp->dt_aggregate;
1906 2156 dt_ahash_t *hash = &agp->dtat_hash;
1907 2157 dt_ahashent_t *h, *next;
1908 2158 dtrace_aggdata_t *aggdata;
1909 2159 int i, max_cpus = agp->dtat_maxcpu;
1910 2160
1911 2161 if (hash->dtah_hash == NULL) {
1912 2162 assert(hash->dtah_all == NULL);
1913 2163 } else {
1914 2164 free(hash->dtah_hash);
1915 2165
1916 2166 for (h = hash->dtah_all; h != NULL; h = next) {
1917 2167 next = h->dtahe_nextall;
1918 2168
1919 2169 aggdata = &h->dtahe_data;
1920 2170
1921 2171 if (aggdata->dtada_percpu != NULL) {
1922 2172 for (i = 0; i < max_cpus; i++)
1923 2173 free(aggdata->dtada_percpu[i]);
1924 2174 free(aggdata->dtada_percpu);
1925 2175 }
1926 2176
1927 2177 free(aggdata->dtada_data);
1928 2178 free(h);
1929 2179 }
1930 2180
1931 2181 hash->dtah_hash = NULL;
1932 2182 hash->dtah_all = NULL;
1933 2183 hash->dtah_size = 0;
1934 2184 }
1935 2185
1936 2186 free(agp->dtat_buf.dtbd_data);
1937 2187 free(agp->dtat_cpus);
1938 2188 }
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX