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_consume.c
+++ new/usr/src/lib/libdtrace/common/dt_consume.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
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 - * Copyright (c) 2011, Joyent, Inc. All rights reserved.
27 + * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 28 * Copyright (c) 2012 by Delphix. All rights reserved.
29 29 */
30 30
31 31 #include <stdlib.h>
32 32 #include <strings.h>
33 33 #include <errno.h>
34 34 #include <unistd.h>
35 35 #include <limits.h>
36 36 #include <assert.h>
37 37 #include <ctype.h>
38 38 #include <alloca.h>
39 39 #include <dt_impl.h>
40 40 #include <dt_pq.h>
41 41
42 42 #define DT_MASK_LO 0x00000000FFFFFFFFULL
43 43
44 44 /*
45 45 * We declare this here because (1) we need it and (2) we want to avoid a
46 46 * dependency on libm in libdtrace.
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
47 47 */
48 48 static long double
49 49 dt_fabsl(long double x)
50 50 {
51 51 if (x < 0)
52 52 return (-x);
53 53
54 54 return (x);
55 55 }
56 56
57 +static int
58 +dt_ndigits(long long val)
59 +{
60 + int rval = 1;
61 + long long cmp = 10;
62 +
63 + if (val < 0) {
64 + val = val == INT64_MIN ? INT64_MAX : -val;
65 + rval++;
66 + }
67 +
68 + while (val > cmp && cmp > 0) {
69 + rval++;
70 + cmp *= 10;
71 + }
72 +
73 + return (rval < 4 ? 4 : rval);
74 +}
75 +
57 76 /*
58 77 * 128-bit arithmetic functions needed to support the stddev() aggregating
59 78 * action.
60 79 */
61 80 static int
62 81 dt_gt_128(uint64_t *a, uint64_t *b)
63 82 {
64 83 return (a[1] > b[1] || (a[1] == b[1] && a[0] > b[0]));
65 84 }
66 85
67 86 static int
68 87 dt_ge_128(uint64_t *a, uint64_t *b)
69 88 {
70 89 return (a[1] > b[1] || (a[1] == b[1] && a[0] >= b[0]));
71 90 }
72 91
73 92 static int
74 93 dt_le_128(uint64_t *a, uint64_t *b)
75 94 {
76 95 return (a[1] < b[1] || (a[1] == b[1] && a[0] <= b[0]));
77 96 }
78 97
79 98 /*
80 99 * Shift the 128-bit value in a by b. If b is positive, shift left.
81 100 * If b is negative, shift right.
82 101 */
83 102 static void
84 103 dt_shift_128(uint64_t *a, int b)
85 104 {
86 105 uint64_t mask;
87 106
88 107 if (b == 0)
89 108 return;
90 109
91 110 if (b < 0) {
92 111 b = -b;
93 112 if (b >= 64) {
94 113 a[0] = a[1] >> (b - 64);
95 114 a[1] = 0;
96 115 } else {
97 116 a[0] >>= b;
98 117 mask = 1LL << (64 - b);
99 118 mask -= 1;
100 119 a[0] |= ((a[1] & mask) << (64 - b));
101 120 a[1] >>= b;
102 121 }
103 122 } else {
104 123 if (b >= 64) {
105 124 a[1] = a[0] << (b - 64);
106 125 a[0] = 0;
107 126 } else {
108 127 a[1] <<= b;
109 128 mask = a[0] >> (64 - b);
110 129 a[1] |= mask;
111 130 a[0] <<= b;
112 131 }
113 132 }
114 133 }
115 134
116 135 static int
117 136 dt_nbits_128(uint64_t *a)
118 137 {
119 138 int nbits = 0;
120 139 uint64_t tmp[2];
121 140 uint64_t zero[2] = { 0, 0 };
122 141
123 142 tmp[0] = a[0];
124 143 tmp[1] = a[1];
125 144
126 145 dt_shift_128(tmp, -1);
127 146 while (dt_gt_128(tmp, zero)) {
128 147 dt_shift_128(tmp, -1);
129 148 nbits++;
130 149 }
131 150
132 151 return (nbits);
133 152 }
134 153
135 154 static void
136 155 dt_subtract_128(uint64_t *minuend, uint64_t *subtrahend, uint64_t *difference)
137 156 {
138 157 uint64_t result[2];
139 158
140 159 result[0] = minuend[0] - subtrahend[0];
141 160 result[1] = minuend[1] - subtrahend[1] -
142 161 (minuend[0] < subtrahend[0] ? 1 : 0);
143 162
144 163 difference[0] = result[0];
145 164 difference[1] = result[1];
146 165 }
147 166
148 167 static void
149 168 dt_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum)
150 169 {
151 170 uint64_t result[2];
152 171
153 172 result[0] = addend1[0] + addend2[0];
154 173 result[1] = addend1[1] + addend2[1] +
155 174 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0);
156 175
157 176 sum[0] = result[0];
158 177 sum[1] = result[1];
159 178 }
160 179
161 180 /*
162 181 * The basic idea is to break the 2 64-bit values into 4 32-bit values,
163 182 * use native multiplication on those, and then re-combine into the
164 183 * resulting 128-bit value.
165 184 *
166 185 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) =
167 186 * hi1 * hi2 << 64 +
168 187 * hi1 * lo2 << 32 +
169 188 * hi2 * lo1 << 32 +
170 189 * lo1 * lo2
171 190 */
172 191 static void
173 192 dt_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product)
174 193 {
175 194 uint64_t hi1, hi2, lo1, lo2;
176 195 uint64_t tmp[2];
177 196
178 197 hi1 = factor1 >> 32;
179 198 hi2 = factor2 >> 32;
180 199
181 200 lo1 = factor1 & DT_MASK_LO;
182 201 lo2 = factor2 & DT_MASK_LO;
183 202
184 203 product[0] = lo1 * lo2;
185 204 product[1] = hi1 * hi2;
186 205
187 206 tmp[0] = hi1 * lo2;
188 207 tmp[1] = 0;
189 208 dt_shift_128(tmp, 32);
190 209 dt_add_128(product, tmp, product);
191 210
192 211 tmp[0] = hi2 * lo1;
193 212 tmp[1] = 0;
194 213 dt_shift_128(tmp, 32);
195 214 dt_add_128(product, tmp, product);
196 215 }
197 216
198 217 /*
199 218 * This is long-hand division.
200 219 *
201 220 * We initialize subtrahend by shifting divisor left as far as possible. We
202 221 * loop, comparing subtrahend to dividend: if subtrahend is smaller, we
203 222 * subtract and set the appropriate bit in the result. We then shift
204 223 * subtrahend right by one bit for the next comparison.
205 224 */
206 225 static void
207 226 dt_divide_128(uint64_t *dividend, uint64_t divisor, uint64_t *quotient)
208 227 {
209 228 uint64_t result[2] = { 0, 0 };
210 229 uint64_t remainder[2];
211 230 uint64_t subtrahend[2];
212 231 uint64_t divisor_128[2];
213 232 uint64_t mask[2] = { 1, 0 };
214 233 int log = 0;
215 234
216 235 assert(divisor != 0);
217 236
218 237 divisor_128[0] = divisor;
219 238 divisor_128[1] = 0;
220 239
221 240 remainder[0] = dividend[0];
222 241 remainder[1] = dividend[1];
223 242
224 243 subtrahend[0] = divisor;
225 244 subtrahend[1] = 0;
226 245
227 246 while (divisor > 0) {
228 247 log++;
229 248 divisor >>= 1;
230 249 }
231 250
232 251 dt_shift_128(subtrahend, 128 - log);
233 252 dt_shift_128(mask, 128 - log);
234 253
235 254 while (dt_ge_128(remainder, divisor_128)) {
236 255 if (dt_ge_128(remainder, subtrahend)) {
237 256 dt_subtract_128(remainder, subtrahend, remainder);
238 257 result[0] |= mask[0];
239 258 result[1] |= mask[1];
240 259 }
241 260
242 261 dt_shift_128(subtrahend, -1);
243 262 dt_shift_128(mask, -1);
244 263 }
245 264
246 265 quotient[0] = result[0];
247 266 quotient[1] = result[1];
248 267 }
249 268
250 269 /*
251 270 * This is the long-hand method of calculating a square root.
252 271 * The algorithm is as follows:
253 272 *
254 273 * 1. Group the digits by 2 from the right.
255 274 * 2. Over the leftmost group, find the largest single-digit number
256 275 * whose square is less than that group.
257 276 * 3. Subtract the result of the previous step (2 or 4, depending) and
258 277 * bring down the next two-digit group.
259 278 * 4. For the result R we have so far, find the largest single-digit number
260 279 * x such that 2 * R * 10 * x + x^2 is less than the result from step 3.
261 280 * (Note that this is doubling R and performing a decimal left-shift by 1
262 281 * and searching for the appropriate decimal to fill the one's place.)
263 282 * The value x is the next digit in the square root.
264 283 * Repeat steps 3 and 4 until the desired precision is reached. (We're
265 284 * dealing with integers, so the above is sufficient.)
266 285 *
267 286 * In decimal, the square root of 582,734 would be calculated as so:
268 287 *
269 288 * __7__6__3
270 289 * | 58 27 34
271 290 * -49 (7^2 == 49 => 7 is the first digit in the square root)
272 291 * --
273 292 * 9 27 (Subtract and bring down the next group.)
274 293 * 146 8 76 (2 * 7 * 10 * 6 + 6^2 == 876 => 6 is the next digit in
275 294 * ----- the square root)
276 295 * 51 34 (Subtract and bring down the next group.)
277 296 * 1523 45 69 (2 * 76 * 10 * 3 + 3^2 == 4569 => 3 is the next digit in
278 297 * ----- the square root)
279 298 * 5 65 (remainder)
280 299 *
281 300 * The above algorithm applies similarly in binary, but note that the
282 301 * only possible non-zero value for x in step 4 is 1, so step 4 becomes a
283 302 * simple decision: is 2 * R * 2 * 1 + 1^2 (aka R << 2 + 1) less than the
284 303 * preceding difference?
285 304 *
286 305 * In binary, the square root of 11011011 would be calculated as so:
287 306 *
288 307 * __1__1__1__0
289 308 * | 11 01 10 11
290 309 * 01 (0 << 2 + 1 == 1 < 11 => this bit is 1)
291 310 * --
292 311 * 10 01 10 11
293 312 * 101 1 01 (1 << 2 + 1 == 101 < 1001 => next bit is 1)
294 313 * -----
295 314 * 1 00 10 11
296 315 * 1101 11 01 (11 << 2 + 1 == 1101 < 10010 => next bit is 1)
297 316 * -------
298 317 * 1 01 11
299 318 * 11101 1 11 01 (111 << 2 + 1 == 11101 > 10111 => last bit is 0)
300 319 *
301 320 */
302 321 static uint64_t
303 322 dt_sqrt_128(uint64_t *square)
304 323 {
305 324 uint64_t result[2] = { 0, 0 };
306 325 uint64_t diff[2] = { 0, 0 };
307 326 uint64_t one[2] = { 1, 0 };
308 327 uint64_t next_pair[2];
309 328 uint64_t next_try[2];
310 329 uint64_t bit_pairs, pair_shift;
311 330 int i;
312 331
313 332 bit_pairs = dt_nbits_128(square) / 2;
314 333 pair_shift = bit_pairs * 2;
315 334
316 335 for (i = 0; i <= bit_pairs; i++) {
317 336 /*
318 337 * Bring down the next pair of bits.
319 338 */
320 339 next_pair[0] = square[0];
321 340 next_pair[1] = square[1];
322 341 dt_shift_128(next_pair, -pair_shift);
323 342 next_pair[0] &= 0x3;
324 343 next_pair[1] = 0;
325 344
326 345 dt_shift_128(diff, 2);
327 346 dt_add_128(diff, next_pair, diff);
328 347
329 348 /*
330 349 * next_try = R << 2 + 1
331 350 */
332 351 next_try[0] = result[0];
333 352 next_try[1] = result[1];
334 353 dt_shift_128(next_try, 2);
335 354 dt_add_128(next_try, one, next_try);
336 355
337 356 if (dt_le_128(next_try, diff)) {
338 357 dt_subtract_128(diff, next_try, diff);
339 358 dt_shift_128(result, 1);
340 359 dt_add_128(result, one, result);
341 360 } else {
342 361 dt_shift_128(result, 1);
343 362 }
344 363
345 364 pair_shift -= 2;
346 365 }
347 366
348 367 assert(result[1] == 0);
349 368
350 369 return (result[0]);
351 370 }
352 371
353 372 uint64_t
354 373 dt_stddev(uint64_t *data, uint64_t normal)
355 374 {
356 375 uint64_t avg_of_squares[2];
357 376 uint64_t square_of_avg[2];
358 377 int64_t norm_avg;
359 378 uint64_t diff[2];
360 379
361 380 /*
362 381 * The standard approximation for standard deviation is
363 382 * sqrt(average(x**2) - average(x)**2), i.e. the square root
364 383 * of the average of the squares minus the square of the average.
365 384 */
366 385 dt_divide_128(data + 2, normal, avg_of_squares);
367 386 dt_divide_128(avg_of_squares, data[0], avg_of_squares);
368 387
369 388 norm_avg = (int64_t)data[1] / (int64_t)normal / (int64_t)data[0];
370 389
371 390 if (norm_avg < 0)
372 391 norm_avg = -norm_avg;
373 392
374 393 dt_multiply_128((uint64_t)norm_avg, (uint64_t)norm_avg, square_of_avg);
375 394
376 395 dt_subtract_128(avg_of_squares, square_of_avg, diff);
377 396
378 397 return (dt_sqrt_128(diff));
379 398 }
380 399
381 400 static int
382 401 dt_flowindent(dtrace_hdl_t *dtp, dtrace_probedata_t *data, dtrace_epid_t last,
383 402 dtrace_bufdesc_t *buf, size_t offs)
384 403 {
385 404 dtrace_probedesc_t *pd = data->dtpda_pdesc, *npd;
386 405 dtrace_eprobedesc_t *epd = data->dtpda_edesc, *nepd;
387 406 char *p = pd->dtpd_provider, *n = pd->dtpd_name, *sub;
388 407 dtrace_flowkind_t flow = DTRACEFLOW_NONE;
389 408 const char *str = NULL;
390 409 static const char *e_str[2] = { " -> ", " => " };
391 410 static const char *r_str[2] = { " <- ", " <= " };
392 411 static const char *ent = "entry", *ret = "return";
393 412 static int entlen = 0, retlen = 0;
394 413 dtrace_epid_t next, id = epd->dtepd_epid;
395 414 int rval;
396 415
397 416 if (entlen == 0) {
398 417 assert(retlen == 0);
399 418 entlen = strlen(ent);
400 419 retlen = strlen(ret);
401 420 }
402 421
403 422 /*
404 423 * If the name of the probe is "entry" or ends with "-entry", we
405 424 * treat it as an entry; if it is "return" or ends with "-return",
406 425 * we treat it as a return. (This allows application-provided probes
407 426 * like "method-entry" or "function-entry" to participate in flow
408 427 * indentation -- without accidentally misinterpreting popular probe
409 428 * names like "carpentry", "gentry" or "Coventry".)
410 429 */
411 430 if ((sub = strstr(n, ent)) != NULL && sub[entlen] == '\0' &&
412 431 (sub == n || sub[-1] == '-')) {
413 432 flow = DTRACEFLOW_ENTRY;
414 433 str = e_str[strcmp(p, "syscall") == 0];
415 434 } else if ((sub = strstr(n, ret)) != NULL && sub[retlen] == '\0' &&
416 435 (sub == n || sub[-1] == '-')) {
417 436 flow = DTRACEFLOW_RETURN;
418 437 str = r_str[strcmp(p, "syscall") == 0];
419 438 }
420 439
421 440 /*
422 441 * If we're going to indent this, we need to check the ID of our last
423 442 * call. If we're looking at the same probe ID but a different EPID,
424 443 * we _don't_ want to indent. (Yes, there are some minor holes in
425 444 * this scheme -- it's a heuristic.)
426 445 */
427 446 if (flow == DTRACEFLOW_ENTRY) {
428 447 if ((last != DTRACE_EPIDNONE && id != last &&
429 448 pd->dtpd_id == dtp->dt_pdesc[last]->dtpd_id))
430 449 flow = DTRACEFLOW_NONE;
431 450 }
432 451
433 452 /*
434 453 * If we're going to unindent this, it's more difficult to see if
435 454 * we don't actually want to unindent it -- we need to look at the
436 455 * _next_ EPID.
437 456 */
438 457 if (flow == DTRACEFLOW_RETURN) {
439 458 offs += epd->dtepd_size;
440 459
441 460 do {
442 461 if (offs >= buf->dtbd_size)
443 462 goto out;
444 463
445 464 next = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
446 465
447 466 if (next == DTRACE_EPIDNONE)
448 467 offs += sizeof (id);
449 468 } while (next == DTRACE_EPIDNONE);
450 469
451 470 if ((rval = dt_epid_lookup(dtp, next, &nepd, &npd)) != 0)
452 471 return (rval);
453 472
454 473 if (next != id && npd->dtpd_id == pd->dtpd_id)
455 474 flow = DTRACEFLOW_NONE;
456 475 }
457 476
458 477 out:
459 478 if (flow == DTRACEFLOW_ENTRY || flow == DTRACEFLOW_RETURN) {
460 479 data->dtpda_prefix = str;
461 480 } else {
462 481 data->dtpda_prefix = "| ";
463 482 }
464 483
465 484 if (flow == DTRACEFLOW_RETURN && data->dtpda_indent > 0)
466 485 data->dtpda_indent -= 2;
467 486
468 487 data->dtpda_flow = flow;
469 488
470 489 return (0);
471 490 }
472 491
473 492 static int
474 493 dt_nullprobe()
↓ open down ↓ |
408 lines elided |
↑ open up ↑ |
475 494 {
476 495 return (DTRACE_CONSUME_THIS);
477 496 }
478 497
479 498 static int
480 499 dt_nullrec()
481 500 {
482 501 return (DTRACE_CONSUME_NEXT);
483 502 }
484 503
485 -int
504 +static void
505 +dt_quantize_total(dtrace_hdl_t *dtp, int64_t datum, long double *total)
506 +{
507 + long double val = dt_fabsl((long double)datum);
508 +
509 + if (dtp->dt_options[DTRACEOPT_AGGZOOM] == DTRACEOPT_UNSET) {
510 + *total += val;
511 + return;
512 + }
513 +
514 + /*
515 + * If we're zooming in on an aggregation, we want the height of the
516 + * highest value to be approximately 95% of total bar height -- so we
517 + * adjust up by the reciprocal of DTRACE_AGGZOOM_MAX when comparing to
518 + * our highest value.
519 + */
520 + val *= 1 / DTRACE_AGGZOOM_MAX;
521 +
522 + if (*total < val)
523 + *total = val;
524 +}
525 +
526 +static int
527 +dt_print_quanthdr(dtrace_hdl_t *dtp, FILE *fp, int width)
528 +{
529 + return (dt_printf(dtp, fp, "\n%*s %41s %-9s\n",
530 + width ? width : 16, width ? "key" : "value",
531 + "------------- Distribution -------------", "count"));
532 +}
533 +
534 +static int
535 +dt_print_quanthdr_packed(dtrace_hdl_t *dtp, FILE *fp, int width,
536 + const dtrace_aggdata_t *aggdata, dtrace_actkind_t action)
537 +{
538 + int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin;
539 + int minwidth, maxwidth, i;
540 +
541 + assert(action == DTRACEAGG_QUANTIZE || action == DTRACEAGG_LQUANTIZE);
542 +
543 + if (action == DTRACEAGG_QUANTIZE) {
544 + if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
545 + min--;
546 +
547 + if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
548 + max++;
549 +
550 + minwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(min));
551 + maxwidth = dt_ndigits(DTRACE_QUANTIZE_BUCKETVAL(max));
552 + } else {
553 + maxwidth = 8;
554 + minwidth = maxwidth - 1;
555 + max++;
556 + }
557 +
558 + if (dt_printf(dtp, fp, "\n%*s %*s .",
559 + width, width > 0 ? "key" : "", minwidth, "min") < 0)
560 + return (-1);
561 +
562 + for (i = min; i <= max; i++) {
563 + if (dt_printf(dtp, fp, "-") < 0)
564 + return (-1);
565 + }
566 +
567 + return (dt_printf(dtp, fp, ". %*s | count\n", -maxwidth, "max"));
568 +}
569 +
570 +/*
571 + * We use a subset of the Unicode Block Elements (U+2588 through U+258F,
572 + * inclusive) to represent aggregations via UTF-8 -- which are expressed via
573 + * 3-byte UTF-8 sequences.
574 + */
575 +#define DTRACE_AGGUTF8_FULL 0x2588
576 +#define DTRACE_AGGUTF8_BASE 0x258f
577 +#define DTRACE_AGGUTF8_LEVELS 8
578 +
579 +#define DTRACE_AGGUTF8_BYTE0(val) (0xe0 | ((val) >> 12))
580 +#define DTRACE_AGGUTF8_BYTE1(val) (0x80 | (((val) >> 6) & 0x3f))
581 +#define DTRACE_AGGUTF8_BYTE2(val) (0x80 | ((val) & 0x3f))
582 +
583 +static int
584 +dt_print_quantline_utf8(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
585 + uint64_t normal, long double total)
586 +{
587 + uint_t len = 40, i, whole, partial;
588 + long double f = (dt_fabsl((long double)val) * len) / total;
589 + const char *spaces = " ";
590 +
591 + whole = (uint_t)f;
592 + partial = (uint_t)((f - (long double)(uint_t)f) *
593 + (long double)DTRACE_AGGUTF8_LEVELS);
594 +
595 + if (dt_printf(dtp, fp, "|") < 0)
596 + return (-1);
597 +
598 + for (i = 0; i < whole; i++) {
599 + if (dt_printf(dtp, fp, "%c%c%c",
600 + DTRACE_AGGUTF8_BYTE0(DTRACE_AGGUTF8_FULL),
601 + DTRACE_AGGUTF8_BYTE1(DTRACE_AGGUTF8_FULL),
602 + DTRACE_AGGUTF8_BYTE2(DTRACE_AGGUTF8_FULL)) < 0)
603 + return (-1);
604 + }
605 +
606 + if (partial != 0) {
607 + partial = DTRACE_AGGUTF8_BASE - (partial - 1);
608 +
609 + if (dt_printf(dtp, fp, "%c%c%c",
610 + DTRACE_AGGUTF8_BYTE0(partial),
611 + DTRACE_AGGUTF8_BYTE1(partial),
612 + DTRACE_AGGUTF8_BYTE2(partial)) < 0)
613 + return (-1);
614 +
615 + i++;
616 + }
617 +
618 + return (dt_printf(dtp, fp, "%s %-9lld\n", spaces + i,
619 + (long long)val / normal));
620 +}
621 +
622 +static int
486 623 dt_print_quantline(dtrace_hdl_t *dtp, FILE *fp, int64_t val,
487 624 uint64_t normal, long double total, char positives, char negatives)
488 625 {
489 626 long double f;
490 627 uint_t depth, len = 40;
491 628
492 629 const char *ats = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";
493 630 const char *spaces = " ";
494 631
495 632 assert(strlen(ats) == len && strlen(spaces) == len);
496 633 assert(!(total == 0 && (positives || negatives)));
497 634 assert(!(val < 0 && !negatives));
498 635 assert(!(val > 0 && !positives));
499 636 assert(!(val != 0 && total == 0));
500 637
501 638 if (!negatives) {
502 639 if (positives) {
640 + if (dtp->dt_encoding == DT_ENCODING_UTF8) {
641 + return (dt_print_quantline_utf8(dtp, fp, val,
642 + normal, total));
643 + }
644 +
503 645 f = (dt_fabsl((long double)val) * len) / total;
504 646 depth = (uint_t)(f + 0.5);
505 647 } else {
506 648 depth = 0;
507 649 }
508 650
509 651 return (dt_printf(dtp, fp, "|%s%s %-9lld\n", ats + len - depth,
510 652 spaces + depth, (long long)val / normal));
511 653 }
512 654
513 655 if (!positives) {
514 656 f = (dt_fabsl((long double)val) * len) / total;
515 657 depth = (uint_t)(f + 0.5);
516 658
517 659 return (dt_printf(dtp, fp, "%s%s| %-9lld\n", spaces + depth,
518 660 ats + len - depth, (long long)val / normal));
519 661 }
520 662
521 663 /*
522 664 * If we're here, we have both positive and negative bucket values.
523 665 * To express this graphically, we're going to generate both positive
524 666 * and negative bars separated by a centerline. These bars are half
525 667 * the size of normal quantize()/lquantize() bars, so we divide the
526 668 * length in half before calculating the bar length.
527 669 */
528 670 len /= 2;
529 671 ats = &ats[len];
530 672 spaces = &spaces[len];
531 673
532 674 f = (dt_fabsl((long double)val) * len) / total;
533 675 depth = (uint_t)(f + 0.5);
534 676
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
535 677 if (val <= 0) {
536 678 return (dt_printf(dtp, fp, "%s%s|%*s %-9lld\n", spaces + depth,
537 679 ats + len - depth, len, "", (long long)val / normal));
538 680 } else {
539 681 return (dt_printf(dtp, fp, "%20s|%s%s %-9lld\n", "",
540 682 ats + len - depth, spaces + depth,
541 683 (long long)val / normal));
542 684 }
543 685 }
544 686
687 +/*
688 + * As with UTF-8 printing of aggregations, we use a subset of the Unicode
689 + * Block Elements (U+2581 through U+2588, inclusive) to represent our packed
690 + * aggregation.
691 + */
692 +#define DTRACE_AGGPACK_BASE 0x2581
693 +#define DTRACE_AGGPACK_LEVELS 8
694 +
695 +static int
696 +dt_print_packed(dtrace_hdl_t *dtp, FILE *fp,
697 + long double datum, long double total)
698 +{
699 + static boolean_t utf8_checked = B_FALSE;
700 + static boolean_t utf8;
701 + char *ascii = "__xxxxXX";
702 + char *neg = "vvvvVV";
703 + unsigned int len;
704 + long double val;
705 +
706 + if (!utf8_checked) {
707 + char *term;
708 +
709 + /*
710 + * We want to determine if we can reasonably emit UTF-8 for our
711 + * packed aggregation. To do this, we will check for terminals
712 + * that are known to be primitive to emit UTF-8 on these.
713 + */
714 + utf8_checked = B_TRUE;
715 +
716 + if (dtp->dt_encoding == DT_ENCODING_ASCII)
717 + utf8 = B_FALSE;
718 + else if (dtp->dt_encoding == DT_ENCODING_UTF8)
719 + utf8 = B_TRUE;
720 + else if ((term = getenv("TERM")) != NULL &&
721 + (strcmp(term, "sun") == 0 ||
722 + strcmp(term, "sun-color") == 0) ||
723 + strcmp(term, "dumb") == 0)
724 + utf8 = B_FALSE;
725 + else
726 + utf8 = B_TRUE;
727 + }
728 +
729 + if (datum == 0)
730 + return (dt_printf(dtp, fp, " "));
731 +
732 + if (datum < 0) {
733 + len = strlen(neg);
734 + val = dt_fabsl(datum * (len - 1)) / total;
735 + return (dt_printf(dtp, fp, "%c", neg[(uint_t)(val + 0.5)]));
736 + }
737 +
738 + if (utf8) {
739 + int block = DTRACE_AGGPACK_BASE + (unsigned int)(((datum *
740 + (DTRACE_AGGPACK_LEVELS - 1)) / total) + 0.5);
741 +
742 + return (dt_printf(dtp, fp, "%c%c%c",
743 + DTRACE_AGGUTF8_BYTE0(block),
744 + DTRACE_AGGUTF8_BYTE1(block),
745 + DTRACE_AGGUTF8_BYTE2(block)));
746 + }
747 +
748 + len = strlen(ascii);
749 + val = (datum * (len - 1)) / total;
750 + return (dt_printf(dtp, fp, "%c", ascii[(uint_t)(val + 0.5)]));
751 +}
752 +
545 753 int
546 754 dt_print_quantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
547 755 size_t size, uint64_t normal)
548 756 {
549 757 const int64_t *data = addr;
550 758 int i, first_bin = 0, last_bin = DTRACE_QUANTIZE_NBUCKETS - 1;
551 759 long double total = 0;
552 760 char positives = 0, negatives = 0;
553 761
554 762 if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
555 763 return (dt_set_errno(dtp, EDT_DMISMATCH));
556 764
557 765 while (first_bin < DTRACE_QUANTIZE_NBUCKETS - 1 && data[first_bin] == 0)
558 766 first_bin++;
559 767
560 768 if (first_bin == DTRACE_QUANTIZE_NBUCKETS - 1) {
561 769 /*
562 - * There isn't any data. This is possible if (and only if)
563 - * negative increment values have been used. In this case,
564 - * we'll print the buckets around 0.
770 + * There isn't any data. This is possible if the aggregation
771 + * has been clear()'d or if negative increment values have been
772 + * used. Regardless, we'll print the buckets around 0.
565 773 */
566 774 first_bin = DTRACE_QUANTIZE_ZEROBUCKET - 1;
567 775 last_bin = DTRACE_QUANTIZE_ZEROBUCKET + 1;
568 776 } else {
569 777 if (first_bin > 0)
570 778 first_bin--;
571 779
572 780 while (last_bin > 0 && data[last_bin] == 0)
573 781 last_bin--;
574 782
575 783 if (last_bin < DTRACE_QUANTIZE_NBUCKETS - 1)
576 784 last_bin++;
577 785 }
578 786
579 787 for (i = first_bin; i <= last_bin; i++) {
580 788 positives |= (data[i] > 0);
581 789 negatives |= (data[i] < 0);
582 - total += dt_fabsl((long double)data[i]);
790 + dt_quantize_total(dtp, data[i], &total);
583 791 }
584 792
585 - if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
586 - "------------- Distribution -------------", "count") < 0)
793 + if (dt_print_quanthdr(dtp, fp, 0) < 0)
587 794 return (-1);
588 795
589 796 for (i = first_bin; i <= last_bin; i++) {
590 797 if (dt_printf(dtp, fp, "%16lld ",
591 798 (long long)DTRACE_QUANTIZE_BUCKETVAL(i)) < 0)
592 799 return (-1);
593 800
594 801 if (dt_print_quantline(dtp, fp, data[i], normal, total,
595 802 positives, negatives) < 0)
596 803 return (-1);
597 804 }
598 805
599 806 return (0);
600 807 }
601 808
602 809 int
810 +dt_print_quantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
811 + size_t size, const dtrace_aggdata_t *aggdata)
812 +{
813 + const int64_t *data = addr;
814 + long double total = 0, count = 0;
815 + int min = aggdata->dtada_minbin, max = aggdata->dtada_maxbin, i;
816 + int64_t minval, maxval;
817 +
818 + if (size != DTRACE_QUANTIZE_NBUCKETS * sizeof (uint64_t))
819 + return (dt_set_errno(dtp, EDT_DMISMATCH));
820 +
821 + if (min != 0 && min != DTRACE_QUANTIZE_ZEROBUCKET)
822 + min--;
823 +
824 + if (max < DTRACE_QUANTIZE_NBUCKETS - 1)
825 + max++;
826 +
827 + minval = DTRACE_QUANTIZE_BUCKETVAL(min);
828 + maxval = DTRACE_QUANTIZE_BUCKETVAL(max);
829 +
830 + if (dt_printf(dtp, fp, " %*lld :", dt_ndigits(minval),
831 + (long long)minval) < 0)
832 + return (-1);
833 +
834 + for (i = min; i <= max; i++) {
835 + dt_quantize_total(dtp, data[i], &total);
836 + count += data[i];
837 + }
838 +
839 + for (i = min; i <= max; i++) {
840 + if (dt_print_packed(dtp, fp, data[i], total) < 0)
841 + return (-1);
842 + }
843 +
844 + if (dt_printf(dtp, fp, ": %*lld | %lld\n",
845 + -dt_ndigits(maxval), (long long)maxval, (long long)count) < 0)
846 + return (-1);
847 +
848 + return (0);
849 +}
850 +
851 +int
603 852 dt_print_lquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
604 853 size_t size, uint64_t normal)
605 854 {
606 855 const int64_t *data = addr;
607 856 int i, first_bin, last_bin, base;
608 857 uint64_t arg;
609 858 long double total = 0;
610 859 uint16_t step, levels;
611 860 char positives = 0, negatives = 0;
612 861
613 862 if (size < sizeof (uint64_t))
614 863 return (dt_set_errno(dtp, EDT_DMISMATCH));
615 864
616 865 arg = *data++;
617 866 size -= sizeof (uint64_t);
618 867
619 868 base = DTRACE_LQUANTIZE_BASE(arg);
620 869 step = DTRACE_LQUANTIZE_STEP(arg);
621 870 levels = DTRACE_LQUANTIZE_LEVELS(arg);
622 871
623 872 first_bin = 0;
624 873 last_bin = levels + 1;
625 874
626 875 if (size != sizeof (uint64_t) * (levels + 2))
627 876 return (dt_set_errno(dtp, EDT_DMISMATCH));
628 877
629 878 while (first_bin <= levels + 1 && data[first_bin] == 0)
630 879 first_bin++;
631 880
632 881 if (first_bin > levels + 1) {
633 882 first_bin = 0;
634 883 last_bin = 2;
635 884 } else {
636 885 if (first_bin > 0)
637 886 first_bin--;
638 887
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
639 888 while (last_bin > 0 && data[last_bin] == 0)
640 889 last_bin--;
641 890
642 891 if (last_bin < levels + 1)
643 892 last_bin++;
644 893 }
645 894
646 895 for (i = first_bin; i <= last_bin; i++) {
647 896 positives |= (data[i] > 0);
648 897 negatives |= (data[i] < 0);
649 - total += dt_fabsl((long double)data[i]);
898 + dt_quantize_total(dtp, data[i], &total);
650 899 }
651 900
652 901 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
653 902 "------------- Distribution -------------", "count") < 0)
654 903 return (-1);
655 904
656 905 for (i = first_bin; i <= last_bin; i++) {
657 906 char c[32];
658 907 int err;
659 908
660 909 if (i == 0) {
661 - (void) snprintf(c, sizeof (c), "< %d",
662 - base / (uint32_t)normal);
910 + (void) snprintf(c, sizeof (c), "< %d", base);
663 911 err = dt_printf(dtp, fp, "%16s ", c);
664 912 } else if (i == levels + 1) {
665 913 (void) snprintf(c, sizeof (c), ">= %d",
666 914 base + (levels * step));
667 915 err = dt_printf(dtp, fp, "%16s ", c);
668 916 } else {
669 917 err = dt_printf(dtp, fp, "%16d ",
670 918 base + (i - 1) * step);
671 919 }
672 920
673 921 if (err < 0 || dt_print_quantline(dtp, fp, data[i], normal,
674 922 total, positives, negatives) < 0)
675 923 return (-1);
676 924 }
677 925
678 926 return (0);
679 927 }
680 928
929 +/*ARGSUSED*/
930 +int
931 +dt_print_lquantize_packed(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
932 + size_t size, const dtrace_aggdata_t *aggdata)
933 +{
934 + const int64_t *data = addr;
935 + long double total = 0, count = 0;
936 + int min, max, base, err;
937 + uint64_t arg;
938 + uint16_t step, levels;
939 + char c[32];
940 + unsigned int i;
941 +
942 + if (size < sizeof (uint64_t))
943 + return (dt_set_errno(dtp, EDT_DMISMATCH));
944 +
945 + arg = *data++;
946 + size -= sizeof (uint64_t);
947 +
948 + base = DTRACE_LQUANTIZE_BASE(arg);
949 + step = DTRACE_LQUANTIZE_STEP(arg);
950 + levels = DTRACE_LQUANTIZE_LEVELS(arg);
951 +
952 + if (size != sizeof (uint64_t) * (levels + 2))
953 + return (dt_set_errno(dtp, EDT_DMISMATCH));
954 +
955 + min = 0;
956 + max = levels + 1;
957 +
958 + if (min == 0) {
959 + (void) snprintf(c, sizeof (c), "< %d", base);
960 + err = dt_printf(dtp, fp, "%8s :", c);
961 + } else {
962 + err = dt_printf(dtp, fp, "%8d :", base + (min - 1) * step);
963 + }
964 +
965 + if (err < 0)
966 + return (-1);
967 +
968 + for (i = min; i <= max; i++) {
969 + dt_quantize_total(dtp, data[i], &total);
970 + count += data[i];
971 + }
972 +
973 + for (i = min; i <= max; i++) {
974 + if (dt_print_packed(dtp, fp, data[i], total) < 0)
975 + return (-1);
976 + }
977 +
978 + (void) snprintf(c, sizeof (c), ">= %d", base + (levels * step));
979 + return (dt_printf(dtp, fp, ": %-8s | %lld\n", c, (long long)count));
980 +}
981 +
681 982 int
682 983 dt_print_llquantize(dtrace_hdl_t *dtp, FILE *fp, const void *addr,
683 984 size_t size, uint64_t normal)
684 985 {
685 986 int i, first_bin, last_bin, bin = 1, order, levels;
686 987 uint16_t factor, low, high, nsteps;
687 988 const int64_t *data = addr;
688 989 int64_t value = 1, next, step;
689 990 char positives = 0, negatives = 0;
690 991 long double total = 0;
691 992 uint64_t arg;
692 993 char c[32];
693 994
694 995 if (size < sizeof (uint64_t))
695 996 return (dt_set_errno(dtp, EDT_DMISMATCH));
696 997
697 998 arg = *data++;
698 999 size -= sizeof (uint64_t);
699 1000
700 1001 factor = DTRACE_LLQUANTIZE_FACTOR(arg);
701 1002 low = DTRACE_LLQUANTIZE_LOW(arg);
702 1003 high = DTRACE_LLQUANTIZE_HIGH(arg);
703 1004 nsteps = DTRACE_LLQUANTIZE_NSTEP(arg);
704 1005
705 1006 /*
706 1007 * We don't expect to be handed invalid llquantize() parameters here,
707 1008 * but sanity check them (to a degree) nonetheless.
708 1009 */
709 1010 if (size > INT32_MAX || factor < 2 || low >= high ||
710 1011 nsteps == 0 || factor > nsteps)
711 1012 return (dt_set_errno(dtp, EDT_DMISMATCH));
712 1013
713 1014 levels = (int)size / sizeof (uint64_t);
714 1015
715 1016 first_bin = 0;
716 1017 last_bin = levels - 1;
717 1018
718 1019 while (first_bin < levels && data[first_bin] == 0)
719 1020 first_bin++;
720 1021
721 1022 if (first_bin == levels) {
722 1023 first_bin = 0;
723 1024 last_bin = 1;
724 1025 } else {
725 1026 if (first_bin > 0)
726 1027 first_bin--;
727 1028
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
728 1029 while (last_bin > 0 && data[last_bin] == 0)
729 1030 last_bin--;
730 1031
731 1032 if (last_bin < levels - 1)
732 1033 last_bin++;
733 1034 }
734 1035
735 1036 for (i = first_bin; i <= last_bin; i++) {
736 1037 positives |= (data[i] > 0);
737 1038 negatives |= (data[i] < 0);
738 - total += dt_fabsl((long double)data[i]);
1039 + dt_quantize_total(dtp, data[i], &total);
739 1040 }
740 1041
741 1042 if (dt_printf(dtp, fp, "\n%16s %41s %-9s\n", "value",
742 1043 "------------- Distribution -------------", "count") < 0)
743 1044 return (-1);
744 1045
745 1046 for (order = 0; order < low; order++)
746 1047 value *= factor;
747 1048
748 1049 next = value * factor;
749 1050 step = next > nsteps ? next / nsteps : 1;
750 1051
751 1052 if (first_bin == 0) {
752 1053 (void) snprintf(c, sizeof (c), "< %lld", value);
753 1054
754 1055 if (dt_printf(dtp, fp, "%16s ", c) < 0)
755 1056 return (-1);
756 1057
757 1058 if (dt_print_quantline(dtp, fp, data[0], normal,
758 1059 total, positives, negatives) < 0)
759 1060 return (-1);
760 1061 }
761 1062
762 1063 while (order <= high) {
763 1064 if (bin >= first_bin && bin <= last_bin) {
764 1065 if (dt_printf(dtp, fp, "%16lld ", (long long)value) < 0)
765 1066 return (-1);
766 1067
767 1068 if (dt_print_quantline(dtp, fp, data[bin],
768 1069 normal, total, positives, negatives) < 0)
769 1070 return (-1);
770 1071 }
771 1072
772 1073 assert(value < next);
773 1074 bin++;
774 1075
775 1076 if ((value += step) != next)
776 1077 continue;
777 1078
778 1079 next = value * factor;
779 1080 step = next > nsteps ? next / nsteps : 1;
780 1081 order++;
781 1082 }
782 1083
783 1084 if (last_bin < bin)
784 1085 return (0);
785 1086
786 1087 assert(last_bin == bin);
787 1088 (void) snprintf(c, sizeof (c), ">= %lld", value);
788 1089
789 1090 if (dt_printf(dtp, fp, "%16s ", c) < 0)
790 1091 return (-1);
791 1092
792 1093 return (dt_print_quantline(dtp, fp, data[bin], normal,
793 1094 total, positives, negatives));
794 1095 }
795 1096
796 1097 /*ARGSUSED*/
797 1098 static int
798 1099 dt_print_average(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
799 1100 size_t size, uint64_t normal)
800 1101 {
801 1102 /* LINTED - alignment */
802 1103 int64_t *data = (int64_t *)addr;
803 1104
804 1105 return (dt_printf(dtp, fp, " %16lld", data[0] ?
805 1106 (long long)(data[1] / (int64_t)normal / data[0]) : 0));
806 1107 }
807 1108
808 1109 /*ARGSUSED*/
809 1110 static int
810 1111 dt_print_stddev(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
↓ open down ↓ |
62 lines elided |
↑ open up ↑ |
811 1112 size_t size, uint64_t normal)
812 1113 {
813 1114 /* LINTED - alignment */
814 1115 uint64_t *data = (uint64_t *)addr;
815 1116
816 1117 return (dt_printf(dtp, fp, " %16llu", data[0] ?
817 1118 (unsigned long long) dt_stddev(data, normal) : 0));
818 1119 }
819 1120
820 1121 /*ARGSUSED*/
821 -int
1122 +static int
822 1123 dt_print_bytes(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr,
823 1124 size_t nbytes, int width, int quiet, int forceraw)
824 1125 {
825 1126 /*
826 1127 * If the byte stream is a series of printable characters, followed by
827 1128 * a terminating byte, we print it out as a string. Otherwise, we
828 1129 * assume that it's something else and just print the bytes.
829 1130 */
830 1131 int i, j, margin = 5;
831 1132 char *c = (char *)addr;
832 1133
833 1134 if (nbytes == 0)
834 1135 return (0);
835 1136
836 1137 if (forceraw)
837 1138 goto raw;
838 1139
839 1140 if (dtp->dt_options[DTRACEOPT_RAWBYTES] != DTRACEOPT_UNSET)
840 1141 goto raw;
841 1142
842 1143 for (i = 0; i < nbytes; i++) {
843 1144 /*
844 1145 * We define a "printable character" to be one for which
845 1146 * isprint(3C) returns non-zero, isspace(3C) returns non-zero,
846 1147 * or a character which is either backspace or the bell.
847 1148 * Backspace and the bell are regrettably special because
848 1149 * they fail the first two tests -- and yet they are entirely
849 1150 * printable. These are the only two control characters that
850 1151 * have meaning for the terminal and for which isprint(3C) and
851 1152 * isspace(3C) return 0.
852 1153 */
853 1154 if (isprint(c[i]) || isspace(c[i]) ||
854 1155 c[i] == '\b' || c[i] == '\a')
855 1156 continue;
856 1157
857 1158 if (c[i] == '\0' && i > 0) {
858 1159 /*
859 1160 * This looks like it might be a string. Before we
860 1161 * assume that it is indeed a string, check the
861 1162 * remainder of the byte range; if it contains
862 1163 * additional non-nul characters, we'll assume that
863 1164 * it's a binary stream that just happens to look like
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
864 1165 * a string, and we'll print out the individual bytes.
865 1166 */
866 1167 for (j = i + 1; j < nbytes; j++) {
867 1168 if (c[j] != '\0')
868 1169 break;
869 1170 }
870 1171
871 1172 if (j != nbytes)
872 1173 break;
873 1174
874 - if (quiet)
1175 + if (quiet) {
875 1176 return (dt_printf(dtp, fp, "%s", c));
876 - else
877 - return (dt_printf(dtp, fp, " %-*s", width, c));
1177 + } else {
1178 + return (dt_printf(dtp, fp, " %s%*s",
1179 + width < 0 ? " " : "", width, c));
1180 + }
878 1181 }
879 1182
880 1183 break;
881 1184 }
882 1185
883 1186 if (i == nbytes) {
884 1187 /*
885 1188 * The byte range is all printable characters, but there is
886 1189 * no trailing nul byte. We'll assume that it's a string and
887 1190 * print it as such.
888 1191 */
889 1192 char *s = alloca(nbytes + 1);
890 1193 bcopy(c, s, nbytes);
891 1194 s[nbytes] = '\0';
892 1195 return (dt_printf(dtp, fp, " %-*s", width, s));
893 1196 }
894 1197
895 1198 raw:
896 1199 if (dt_printf(dtp, fp, "\n%*s ", margin, "") < 0)
897 1200 return (-1);
898 1201
899 1202 for (i = 0; i < 16; i++)
900 1203 if (dt_printf(dtp, fp, " %c", "0123456789abcdef"[i]) < 0)
901 1204 return (-1);
902 1205
903 1206 if (dt_printf(dtp, fp, " 0123456789abcdef\n") < 0)
904 1207 return (-1);
905 1208
906 1209
907 1210 for (i = 0; i < nbytes; i += 16) {
908 1211 if (dt_printf(dtp, fp, "%*s%5x:", margin, "", i) < 0)
909 1212 return (-1);
910 1213
911 1214 for (j = i; j < i + 16 && j < nbytes; j++) {
912 1215 if (dt_printf(dtp, fp, " %02x", (uchar_t)c[j]) < 0)
913 1216 return (-1);
914 1217 }
915 1218
916 1219 while (j++ % 16) {
917 1220 if (dt_printf(dtp, fp, " ") < 0)
918 1221 return (-1);
919 1222 }
920 1223
921 1224 if (dt_printf(dtp, fp, " ") < 0)
922 1225 return (-1);
923 1226
924 1227 for (j = i; j < i + 16 && j < nbytes; j++) {
925 1228 if (dt_printf(dtp, fp, "%c",
926 1229 c[j] < ' ' || c[j] > '~' ? '.' : c[j]) < 0)
927 1230 return (-1);
928 1231 }
929 1232
930 1233 if (dt_printf(dtp, fp, "\n") < 0)
931 1234 return (-1);
932 1235 }
933 1236
934 1237 return (0);
935 1238 }
936 1239
937 1240 int
938 1241 dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
939 1242 caddr_t addr, int depth, int size)
940 1243 {
941 1244 dtrace_syminfo_t dts;
942 1245 GElf_Sym sym;
943 1246 int i, indent;
944 1247 char c[PATH_MAX * 2];
945 1248 uint64_t pc;
946 1249
947 1250 if (dt_printf(dtp, fp, "\n") < 0)
948 1251 return (-1);
949 1252
950 1253 if (format == NULL)
951 1254 format = "%s";
952 1255
953 1256 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
954 1257 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
955 1258 else
956 1259 indent = _dtrace_stkindent;
957 1260
958 1261 for (i = 0; i < depth; i++) {
959 1262 switch (size) {
960 1263 case sizeof (uint32_t):
961 1264 /* LINTED - alignment */
962 1265 pc = *((uint32_t *)addr);
963 1266 break;
964 1267
965 1268 case sizeof (uint64_t):
966 1269 /* LINTED - alignment */
967 1270 pc = *((uint64_t *)addr);
968 1271 break;
969 1272
970 1273 default:
971 1274 return (dt_set_errno(dtp, EDT_BADSTACKPC));
972 1275 }
973 1276
974 1277 if (pc == NULL)
975 1278 break;
976 1279
977 1280 addr += size;
978 1281
979 1282 if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
980 1283 return (-1);
981 1284
982 1285 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
983 1286 if (pc > sym.st_value) {
984 1287 (void) snprintf(c, sizeof (c), "%s`%s+0x%llx",
985 1288 dts.dts_object, dts.dts_name,
986 1289 pc - sym.st_value);
987 1290 } else {
988 1291 (void) snprintf(c, sizeof (c), "%s`%s",
989 1292 dts.dts_object, dts.dts_name);
990 1293 }
991 1294 } else {
992 1295 /*
993 1296 * We'll repeat the lookup, but this time we'll specify
994 1297 * a NULL GElf_Sym -- indicating that we're only
995 1298 * interested in the containing module.
996 1299 */
997 1300 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
998 1301 (void) snprintf(c, sizeof (c), "%s`0x%llx",
999 1302 dts.dts_object, pc);
1000 1303 } else {
1001 1304 (void) snprintf(c, sizeof (c), "0x%llx", pc);
1002 1305 }
1003 1306 }
1004 1307
1005 1308 if (dt_printf(dtp, fp, format, c) < 0)
1006 1309 return (-1);
1007 1310
1008 1311 if (dt_printf(dtp, fp, "\n") < 0)
1009 1312 return (-1);
1010 1313 }
1011 1314
1012 1315 return (0);
1013 1316 }
1014 1317
1015 1318 int
1016 1319 dt_print_ustack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
1017 1320 caddr_t addr, uint64_t arg)
1018 1321 {
1019 1322 /* LINTED - alignment */
1020 1323 uint64_t *pc = (uint64_t *)addr;
1021 1324 uint32_t depth = DTRACE_USTACK_NFRAMES(arg);
1022 1325 uint32_t strsize = DTRACE_USTACK_STRSIZE(arg);
1023 1326 const char *strbase = addr + (depth + 1) * sizeof (uint64_t);
1024 1327 const char *str = strsize ? strbase : NULL;
1025 1328 int err = 0;
1026 1329
1027 1330 char name[PATH_MAX], objname[PATH_MAX], c[PATH_MAX * 2];
1028 1331 struct ps_prochandle *P;
1029 1332 GElf_Sym sym;
1030 1333 int i, indent;
1031 1334 pid_t pid;
1032 1335
1033 1336 if (depth == 0)
1034 1337 return (0);
1035 1338
1036 1339 pid = (pid_t)*pc++;
1037 1340
1038 1341 if (dt_printf(dtp, fp, "\n") < 0)
1039 1342 return (-1);
1040 1343
1041 1344 if (format == NULL)
1042 1345 format = "%s";
1043 1346
1044 1347 if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
1045 1348 indent = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
1046 1349 else
1047 1350 indent = _dtrace_stkindent;
1048 1351
1049 1352 /*
1050 1353 * Ultimately, we need to add an entry point in the library vector for
1051 1354 * determining <symbol, offset> from <pid, address>. For now, if
1052 1355 * this is a vector open, we just print the raw address or string.
1053 1356 */
1054 1357 if (dtp->dt_vector == NULL)
1055 1358 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1056 1359 else
1057 1360 P = NULL;
1058 1361
1059 1362 if (P != NULL)
1060 1363 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1061 1364
1062 1365 for (i = 0; i < depth && pc[i] != NULL; i++) {
1063 1366 const prmap_t *map;
1064 1367
1065 1368 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1066 1369 break;
1067 1370
1068 1371 if (P != NULL && Plookup_by_addr(P, pc[i],
1069 1372 name, sizeof (name), &sym) == 0) {
1070 1373 (void) Pobjname(P, pc[i], objname, sizeof (objname));
1071 1374
1072 1375 if (pc[i] > sym.st_value) {
1073 1376 (void) snprintf(c, sizeof (c),
1074 1377 "%s`%s+0x%llx", dt_basename(objname), name,
1075 1378 (u_longlong_t)(pc[i] - sym.st_value));
1076 1379 } else {
1077 1380 (void) snprintf(c, sizeof (c),
1078 1381 "%s`%s", dt_basename(objname), name);
1079 1382 }
1080 1383 } else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
1081 1384 (P != NULL && ((map = Paddr_to_map(P, pc[i])) == NULL ||
1082 1385 (map->pr_mflags & MA_WRITE)))) {
1083 1386 /*
1084 1387 * If the current string pointer in the string table
1085 1388 * does not point to an empty string _and_ the program
1086 1389 * counter falls in a writable region, we'll use the
1087 1390 * string from the string table instead of the raw
1088 1391 * address. This last condition is necessary because
1089 1392 * some (broken) ustack helpers will return a string
1090 1393 * even for a program counter that they can't
1091 1394 * identify. If we have a string for a program
1092 1395 * counter that falls in a segment that isn't
1093 1396 * writable, we assume that we have fallen into this
1094 1397 * case and we refuse to use the string.
1095 1398 */
1096 1399 (void) snprintf(c, sizeof (c), "%s", str);
1097 1400 } else {
1098 1401 if (P != NULL && Pobjname(P, pc[i], objname,
1099 1402 sizeof (objname)) != NULL) {
1100 1403 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1101 1404 dt_basename(objname), (u_longlong_t)pc[i]);
1102 1405 } else {
1103 1406 (void) snprintf(c, sizeof (c), "0x%llx",
1104 1407 (u_longlong_t)pc[i]);
1105 1408 }
1106 1409 }
1107 1410
1108 1411 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1109 1412 break;
1110 1413
1111 1414 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1112 1415 break;
1113 1416
1114 1417 if (str != NULL && str[0] == '@') {
1115 1418 /*
1116 1419 * If the first character of the string is an "at" sign,
1117 1420 * then the string is inferred to be an annotation --
1118 1421 * and it is printed out beneath the frame and offset
1119 1422 * with brackets.
1120 1423 */
1121 1424 if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
1122 1425 break;
1123 1426
1124 1427 (void) snprintf(c, sizeof (c), " [ %s ]", &str[1]);
1125 1428
1126 1429 if ((err = dt_printf(dtp, fp, format, c)) < 0)
1127 1430 break;
1128 1431
1129 1432 if ((err = dt_printf(dtp, fp, "\n")) < 0)
1130 1433 break;
1131 1434 }
1132 1435
1133 1436 if (str != NULL) {
1134 1437 str += strlen(str) + 1;
1135 1438 if (str - strbase >= strsize)
1136 1439 str = NULL;
1137 1440 }
1138 1441 }
1139 1442
1140 1443 if (P != NULL) {
1141 1444 dt_proc_unlock(dtp, P);
1142 1445 dt_proc_release(dtp, P);
1143 1446 }
1144 1447
1145 1448 return (err);
1146 1449 }
1147 1450
1148 1451 static int
1149 1452 dt_print_usym(dtrace_hdl_t *dtp, FILE *fp, caddr_t addr, dtrace_actkind_t act)
1150 1453 {
1151 1454 /* LINTED - alignment */
1152 1455 uint64_t pid = ((uint64_t *)addr)[0];
1153 1456 /* LINTED - alignment */
1154 1457 uint64_t pc = ((uint64_t *)addr)[1];
1155 1458 const char *format = " %-50s";
1156 1459 char *s;
1157 1460 int n, len = 256;
1158 1461
1159 1462 if (act == DTRACEACT_USYM && dtp->dt_vector == NULL) {
1160 1463 struct ps_prochandle *P;
1161 1464
1162 1465 if ((P = dt_proc_grab(dtp, pid,
1163 1466 PGRAB_RDONLY | PGRAB_FORCE, 0)) != NULL) {
1164 1467 GElf_Sym sym;
1165 1468
1166 1469 dt_proc_lock(dtp, P);
1167 1470
1168 1471 if (Plookup_by_addr(P, pc, NULL, 0, &sym) == 0)
1169 1472 pc = sym.st_value;
1170 1473
1171 1474 dt_proc_unlock(dtp, P);
1172 1475 dt_proc_release(dtp, P);
1173 1476 }
1174 1477 }
1175 1478
1176 1479 do {
1177 1480 n = len;
1178 1481 s = alloca(n);
1179 1482 } while ((len = dtrace_uaddr2str(dtp, pid, pc, s, n)) > n);
1180 1483
1181 1484 return (dt_printf(dtp, fp, format, s));
1182 1485 }
1183 1486
1184 1487 int
1185 1488 dt_print_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1186 1489 {
1187 1490 /* LINTED - alignment */
1188 1491 uint64_t pid = ((uint64_t *)addr)[0];
1189 1492 /* LINTED - alignment */
1190 1493 uint64_t pc = ((uint64_t *)addr)[1];
1191 1494 int err = 0;
1192 1495
1193 1496 char objname[PATH_MAX], c[PATH_MAX * 2];
1194 1497 struct ps_prochandle *P;
1195 1498
1196 1499 if (format == NULL)
1197 1500 format = " %-50s";
1198 1501
1199 1502 /*
1200 1503 * See the comment in dt_print_ustack() for the rationale for
1201 1504 * printing raw addresses in the vectored case.
1202 1505 */
1203 1506 if (dtp->dt_vector == NULL)
1204 1507 P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
1205 1508 else
1206 1509 P = NULL;
1207 1510
1208 1511 if (P != NULL)
1209 1512 dt_proc_lock(dtp, P); /* lock handle while we perform lookups */
1210 1513
1211 1514 if (P != NULL && Pobjname(P, pc, objname, sizeof (objname)) != NULL) {
1212 1515 (void) snprintf(c, sizeof (c), "%s", dt_basename(objname));
1213 1516 } else {
1214 1517 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1215 1518 }
1216 1519
1217 1520 err = dt_printf(dtp, fp, format, c);
1218 1521
1219 1522 if (P != NULL) {
1220 1523 dt_proc_unlock(dtp, P);
1221 1524 dt_proc_release(dtp, P);
1222 1525 }
1223 1526
1224 1527 return (err);
1225 1528 }
1226 1529
1227 1530 static int
1228 1531 dt_print_sym(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1229 1532 {
1230 1533 /* LINTED - alignment */
1231 1534 uint64_t pc = *((uint64_t *)addr);
1232 1535 dtrace_syminfo_t dts;
1233 1536 GElf_Sym sym;
1234 1537 char c[PATH_MAX * 2];
1235 1538
1236 1539 if (format == NULL)
1237 1540 format = " %-50s";
1238 1541
1239 1542 if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
1240 1543 (void) snprintf(c, sizeof (c), "%s`%s",
1241 1544 dts.dts_object, dts.dts_name);
1242 1545 } else {
1243 1546 /*
1244 1547 * We'll repeat the lookup, but this time we'll specify a
1245 1548 * NULL GElf_Sym -- indicating that we're only interested in
1246 1549 * the containing module.
1247 1550 */
1248 1551 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1249 1552 (void) snprintf(c, sizeof (c), "%s`0x%llx",
1250 1553 dts.dts_object, (u_longlong_t)pc);
1251 1554 } else {
1252 1555 (void) snprintf(c, sizeof (c), "0x%llx",
1253 1556 (u_longlong_t)pc);
1254 1557 }
1255 1558 }
1256 1559
1257 1560 if (dt_printf(dtp, fp, format, c) < 0)
1258 1561 return (-1);
1259 1562
1260 1563 return (0);
1261 1564 }
1262 1565
1263 1566 int
1264 1567 dt_print_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format, caddr_t addr)
1265 1568 {
1266 1569 /* LINTED - alignment */
1267 1570 uint64_t pc = *((uint64_t *)addr);
1268 1571 dtrace_syminfo_t dts;
1269 1572 char c[PATH_MAX * 2];
1270 1573
1271 1574 if (format == NULL)
1272 1575 format = " %-50s";
1273 1576
1274 1577 if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0) {
1275 1578 (void) snprintf(c, sizeof (c), "%s", dts.dts_object);
1276 1579 } else {
1277 1580 (void) snprintf(c, sizeof (c), "0x%llx", (u_longlong_t)pc);
1278 1581 }
1279 1582
1280 1583 if (dt_printf(dtp, fp, format, c) < 0)
1281 1584 return (-1);
1282 1585
1283 1586 return (0);
1284 1587 }
1285 1588
1286 1589 typedef struct dt_normal {
1287 1590 dtrace_aggvarid_t dtnd_id;
1288 1591 uint64_t dtnd_normal;
1289 1592 } dt_normal_t;
1290 1593
1291 1594 static int
1292 1595 dt_normalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1293 1596 {
1294 1597 dt_normal_t *normal = arg;
1295 1598 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1296 1599 dtrace_aggvarid_t id = normal->dtnd_id;
1297 1600
1298 1601 if (agg->dtagd_nrecs == 0)
1299 1602 return (DTRACE_AGGWALK_NEXT);
1300 1603
1301 1604 if (agg->dtagd_varid != id)
1302 1605 return (DTRACE_AGGWALK_NEXT);
1303 1606
1304 1607 ((dtrace_aggdata_t *)aggdata)->dtada_normal = normal->dtnd_normal;
1305 1608 return (DTRACE_AGGWALK_NORMALIZE);
1306 1609 }
1307 1610
1308 1611 static int
1309 1612 dt_normalize(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1310 1613 {
1311 1614 dt_normal_t normal;
1312 1615 caddr_t addr;
1313 1616
1314 1617 /*
1315 1618 * We (should) have two records: the aggregation ID followed by the
1316 1619 * normalization value.
1317 1620 */
1318 1621 addr = base + rec->dtrd_offset;
1319 1622
1320 1623 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1321 1624 return (dt_set_errno(dtp, EDT_BADNORMAL));
1322 1625
1323 1626 /* LINTED - alignment */
1324 1627 normal.dtnd_id = *((dtrace_aggvarid_t *)addr);
1325 1628 rec++;
1326 1629
1327 1630 if (rec->dtrd_action != DTRACEACT_LIBACT)
1328 1631 return (dt_set_errno(dtp, EDT_BADNORMAL));
1329 1632
1330 1633 if (rec->dtrd_arg != DT_ACT_NORMALIZE)
1331 1634 return (dt_set_errno(dtp, EDT_BADNORMAL));
1332 1635
1333 1636 addr = base + rec->dtrd_offset;
1334 1637
1335 1638 switch (rec->dtrd_size) {
1336 1639 case sizeof (uint64_t):
1337 1640 /* LINTED - alignment */
1338 1641 normal.dtnd_normal = *((uint64_t *)addr);
1339 1642 break;
1340 1643 case sizeof (uint32_t):
1341 1644 /* LINTED - alignment */
1342 1645 normal.dtnd_normal = *((uint32_t *)addr);
1343 1646 break;
1344 1647 case sizeof (uint16_t):
1345 1648 /* LINTED - alignment */
1346 1649 normal.dtnd_normal = *((uint16_t *)addr);
1347 1650 break;
1348 1651 case sizeof (uint8_t):
1349 1652 normal.dtnd_normal = *((uint8_t *)addr);
1350 1653 break;
1351 1654 default:
1352 1655 return (dt_set_errno(dtp, EDT_BADNORMAL));
1353 1656 }
1354 1657
1355 1658 (void) dtrace_aggregate_walk(dtp, dt_normalize_agg, &normal);
1356 1659
1357 1660 return (0);
1358 1661 }
1359 1662
1360 1663 static int
1361 1664 dt_denormalize_agg(const dtrace_aggdata_t *aggdata, void *arg)
1362 1665 {
1363 1666 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1364 1667 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1365 1668
1366 1669 if (agg->dtagd_nrecs == 0)
1367 1670 return (DTRACE_AGGWALK_NEXT);
1368 1671
1369 1672 if (agg->dtagd_varid != id)
1370 1673 return (DTRACE_AGGWALK_NEXT);
1371 1674
1372 1675 return (DTRACE_AGGWALK_DENORMALIZE);
1373 1676 }
1374 1677
1375 1678 static int
1376 1679 dt_clear_agg(const dtrace_aggdata_t *aggdata, void *arg)
1377 1680 {
1378 1681 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1379 1682 dtrace_aggvarid_t id = *((dtrace_aggvarid_t *)arg);
1380 1683
1381 1684 if (agg->dtagd_nrecs == 0)
1382 1685 return (DTRACE_AGGWALK_NEXT);
1383 1686
1384 1687 if (agg->dtagd_varid != id)
1385 1688 return (DTRACE_AGGWALK_NEXT);
1386 1689
1387 1690 return (DTRACE_AGGWALK_CLEAR);
1388 1691 }
1389 1692
1390 1693 typedef struct dt_trunc {
1391 1694 dtrace_aggvarid_t dttd_id;
1392 1695 uint64_t dttd_remaining;
1393 1696 } dt_trunc_t;
1394 1697
1395 1698 static int
1396 1699 dt_trunc_agg(const dtrace_aggdata_t *aggdata, void *arg)
1397 1700 {
1398 1701 dt_trunc_t *trunc = arg;
1399 1702 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1400 1703 dtrace_aggvarid_t id = trunc->dttd_id;
1401 1704
1402 1705 if (agg->dtagd_nrecs == 0)
1403 1706 return (DTRACE_AGGWALK_NEXT);
1404 1707
1405 1708 if (agg->dtagd_varid != id)
1406 1709 return (DTRACE_AGGWALK_NEXT);
1407 1710
1408 1711 if (trunc->dttd_remaining == 0)
1409 1712 return (DTRACE_AGGWALK_REMOVE);
1410 1713
1411 1714 trunc->dttd_remaining--;
1412 1715 return (DTRACE_AGGWALK_NEXT);
1413 1716 }
1414 1717
1415 1718 static int
1416 1719 dt_trunc(dtrace_hdl_t *dtp, caddr_t base, dtrace_recdesc_t *rec)
1417 1720 {
1418 1721 dt_trunc_t trunc;
1419 1722 caddr_t addr;
1420 1723 int64_t remaining;
1421 1724 int (*func)(dtrace_hdl_t *, dtrace_aggregate_f *, void *);
1422 1725
1423 1726 /*
1424 1727 * We (should) have two records: the aggregation ID followed by the
1425 1728 * number of aggregation entries after which the aggregation is to be
1426 1729 * truncated.
1427 1730 */
1428 1731 addr = base + rec->dtrd_offset;
1429 1732
1430 1733 if (rec->dtrd_size != sizeof (dtrace_aggvarid_t))
1431 1734 return (dt_set_errno(dtp, EDT_BADTRUNC));
1432 1735
1433 1736 /* LINTED - alignment */
1434 1737 trunc.dttd_id = *((dtrace_aggvarid_t *)addr);
1435 1738 rec++;
1436 1739
1437 1740 if (rec->dtrd_action != DTRACEACT_LIBACT)
1438 1741 return (dt_set_errno(dtp, EDT_BADTRUNC));
1439 1742
1440 1743 if (rec->dtrd_arg != DT_ACT_TRUNC)
1441 1744 return (dt_set_errno(dtp, EDT_BADTRUNC));
1442 1745
1443 1746 addr = base + rec->dtrd_offset;
1444 1747
1445 1748 switch (rec->dtrd_size) {
1446 1749 case sizeof (uint64_t):
1447 1750 /* LINTED - alignment */
1448 1751 remaining = *((int64_t *)addr);
1449 1752 break;
1450 1753 case sizeof (uint32_t):
1451 1754 /* LINTED - alignment */
1452 1755 remaining = *((int32_t *)addr);
1453 1756 break;
1454 1757 case sizeof (uint16_t):
1455 1758 /* LINTED - alignment */
1456 1759 remaining = *((int16_t *)addr);
1457 1760 break;
1458 1761 case sizeof (uint8_t):
1459 1762 remaining = *((int8_t *)addr);
1460 1763 break;
1461 1764 default:
1462 1765 return (dt_set_errno(dtp, EDT_BADNORMAL));
1463 1766 }
1464 1767
1465 1768 if (remaining < 0) {
1466 1769 func = dtrace_aggregate_walk_valsorted;
1467 1770 remaining = -remaining;
1468 1771 } else {
1469 1772 func = dtrace_aggregate_walk_valrevsorted;
1470 1773 }
1471 1774
↓ open down ↓ |
584 lines elided |
↑ open up ↑ |
1472 1775 assert(remaining >= 0);
1473 1776 trunc.dttd_remaining = remaining;
1474 1777
1475 1778 (void) func(dtp, dt_trunc_agg, &trunc);
1476 1779
1477 1780 return (0);
1478 1781 }
1479 1782
1480 1783 static int
1481 1784 dt_print_datum(dtrace_hdl_t *dtp, FILE *fp, dtrace_recdesc_t *rec,
1482 - caddr_t addr, size_t size, uint64_t normal)
1785 + caddr_t addr, size_t size, const dtrace_aggdata_t *aggdata,
1786 + uint64_t normal, dt_print_aggdata_t *pd)
1483 1787 {
1484 - int err;
1788 + int err, width;
1485 1789 dtrace_actkind_t act = rec->dtrd_action;
1790 + boolean_t packed = pd->dtpa_agghist || pd->dtpa_aggpack;
1791 + dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1792 +
1793 + static struct {
1794 + size_t size;
1795 + int width;
1796 + int packedwidth;
1797 + } *fmt, fmttab[] = {
1798 + { sizeof (uint8_t), 3, 3 },
1799 + { sizeof (uint16_t), 5, 5 },
1800 + { sizeof (uint32_t), 8, 8 },
1801 + { sizeof (uint64_t), 16, 16 },
1802 + { 0, -50, 16 }
1803 + };
1804 +
1805 + if (packed && pd->dtpa_agghisthdr != agg->dtagd_varid) {
1806 + dtrace_recdesc_t *r;
1807 +
1808 + width = 0;
1809 +
1810 + /*
1811 + * To print our quantization header for either an agghist or
1812 + * aggpack aggregation, we need to iterate through all of our
1813 + * of our records to determine their width.
1814 + */
1815 + for (r = rec; !DTRACEACT_ISAGG(r->dtrd_action); r++) {
1816 + for (fmt = fmttab; fmt->size &&
1817 + fmt->size != r->dtrd_size; fmt++)
1818 + continue;
1819 +
1820 + width += fmt->packedwidth + 1;
1821 + }
1822 +
1823 + if (pd->dtpa_agghist) {
1824 + if (dt_print_quanthdr(dtp, fp, width) < 0)
1825 + return (-1);
1826 + } else {
1827 + if (dt_print_quanthdr_packed(dtp, fp,
1828 + width, aggdata, r->dtrd_action) < 0)
1829 + return (-1);
1830 + }
1831 +
1832 + pd->dtpa_agghisthdr = agg->dtagd_varid;
1833 + }
1834 +
1835 + if (pd->dtpa_agghist && DTRACEACT_ISAGG(act)) {
1836 + char positives = aggdata->dtada_flags & DTRACE_A_HASPOSITIVES;
1837 + char negatives = aggdata->dtada_flags & DTRACE_A_HASNEGATIVES;
1838 + int64_t val;
1839 +
1840 + assert(act == DTRACEAGG_SUM || act == DTRACEAGG_COUNT);
1841 + val = (long long)*((uint64_t *)addr);
1842 +
1843 + if (dt_printf(dtp, fp, " ") < 0)
1844 + return (-1);
1845 +
1846 + return (dt_print_quantline(dtp, fp, val, normal,
1847 + aggdata->dtada_total, positives, negatives));
1848 + }
1849 +
1850 + if (pd->dtpa_aggpack && DTRACEACT_ISAGG(act)) {
1851 + switch (act) {
1852 + case DTRACEAGG_QUANTIZE:
1853 + return (dt_print_quantize_packed(dtp,
1854 + fp, addr, size, aggdata));
1855 + case DTRACEAGG_LQUANTIZE:
1856 + return (dt_print_lquantize_packed(dtp,
1857 + fp, addr, size, aggdata));
1858 + default:
1859 + break;
1860 + }
1861 + }
1486 1862
1487 1863 switch (act) {
1488 1864 case DTRACEACT_STACK:
1489 1865 return (dt_print_stack(dtp, fp, NULL, addr,
1490 1866 rec->dtrd_arg, rec->dtrd_size / rec->dtrd_arg));
1491 1867
1492 1868 case DTRACEACT_USTACK:
1493 1869 case DTRACEACT_JSTACK:
1494 1870 return (dt_print_ustack(dtp, fp, NULL, addr, rec->dtrd_arg));
1495 1871
1496 1872 case DTRACEACT_USYM:
1497 1873 case DTRACEACT_UADDR:
1498 1874 return (dt_print_usym(dtp, fp, addr, act));
1499 1875
1500 1876 case DTRACEACT_UMOD:
1501 1877 return (dt_print_umod(dtp, fp, NULL, addr));
1502 1878
1503 1879 case DTRACEACT_SYM:
1504 1880 return (dt_print_sym(dtp, fp, NULL, addr));
1505 1881
1506 1882 case DTRACEACT_MOD:
1507 1883 return (dt_print_mod(dtp, fp, NULL, addr));
1508 1884
1509 1885 case DTRACEAGG_QUANTIZE:
1510 1886 return (dt_print_quantize(dtp, fp, addr, size, normal));
1511 1887
1512 1888 case DTRACEAGG_LQUANTIZE:
1513 1889 return (dt_print_lquantize(dtp, fp, addr, size, normal));
1514 1890
1515 1891 case DTRACEAGG_LLQUANTIZE:
1516 1892 return (dt_print_llquantize(dtp, fp, addr, size, normal));
1517 1893
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
1518 1894 case DTRACEAGG_AVG:
1519 1895 return (dt_print_average(dtp, fp, addr, size, normal));
1520 1896
1521 1897 case DTRACEAGG_STDDEV:
1522 1898 return (dt_print_stddev(dtp, fp, addr, size, normal));
1523 1899
1524 1900 default:
1525 1901 break;
1526 1902 }
1527 1903
1904 + for (fmt = fmttab; fmt->size && fmt->size != size; fmt++)
1905 + continue;
1906 +
1907 + width = packed ? fmt->packedwidth : fmt->width;
1908 +
1528 1909 switch (size) {
1529 1910 case sizeof (uint64_t):
1530 - err = dt_printf(dtp, fp, " %16lld",
1911 + err = dt_printf(dtp, fp, " %*lld", width,
1531 1912 /* LINTED - alignment */
1532 1913 (long long)*((uint64_t *)addr) / normal);
1533 1914 break;
1534 1915 case sizeof (uint32_t):
1535 1916 /* LINTED - alignment */
1536 - err = dt_printf(dtp, fp, " %8d", *((uint32_t *)addr) /
1917 + err = dt_printf(dtp, fp, " %*d", width, *((uint32_t *)addr) /
1537 1918 (uint32_t)normal);
1538 1919 break;
1539 1920 case sizeof (uint16_t):
1540 1921 /* LINTED - alignment */
1541 - err = dt_printf(dtp, fp, " %5d", *((uint16_t *)addr) /
1922 + err = dt_printf(dtp, fp, " %*d", width, *((uint16_t *)addr) /
1542 1923 (uint32_t)normal);
1543 1924 break;
1544 1925 case sizeof (uint8_t):
1545 - err = dt_printf(dtp, fp, " %3d", *((uint8_t *)addr) /
1926 + err = dt_printf(dtp, fp, " %*d", width, *((uint8_t *)addr) /
1546 1927 (uint32_t)normal);
1547 1928 break;
1548 1929 default:
1549 - err = dt_print_bytes(dtp, fp, addr, size, 50, 0, 0);
1930 + err = dt_print_bytes(dtp, fp, addr, size, width, 0, 0);
1550 1931 break;
1551 1932 }
1552 1933
1553 1934 return (err);
1554 1935 }
1555 1936
1556 1937 int
1557 1938 dt_print_aggs(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
1558 1939 {
1559 1940 int i, aggact = 0;
1560 1941 dt_print_aggdata_t *pd = arg;
1561 1942 const dtrace_aggdata_t *aggdata = aggsdata[0];
1562 1943 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1563 1944 FILE *fp = pd->dtpa_fp;
1564 1945 dtrace_hdl_t *dtp = pd->dtpa_dtp;
1565 1946 dtrace_recdesc_t *rec;
1566 1947 dtrace_actkind_t act;
1567 1948 caddr_t addr;
1568 1949 size_t size;
1569 1950
1951 + pd->dtpa_agghist = (aggdata->dtada_flags & DTRACE_A_TOTAL);
1952 + pd->dtpa_aggpack = (aggdata->dtada_flags & DTRACE_A_MINMAXBIN);
1953 +
1570 1954 /*
1571 1955 * Iterate over each record description in the key, printing the traced
1572 1956 * data, skipping the first datum (the tuple member created by the
1573 1957 * compiler).
1574 1958 */
1575 1959 for (i = 1; i < agg->dtagd_nrecs; i++) {
1576 1960 rec = &agg->dtagd_rec[i];
1577 1961 act = rec->dtrd_action;
1578 1962 addr = aggdata->dtada_data + rec->dtrd_offset;
1579 1963 size = rec->dtrd_size;
1580 1964
1581 1965 if (DTRACEACT_ISAGG(act)) {
1582 1966 aggact = i;
1583 1967 break;
1584 1968 }
1585 1969
1586 - if (dt_print_datum(dtp, fp, rec, addr, size, 1) < 0)
1970 + if (dt_print_datum(dtp, fp, rec, addr,
1971 + size, aggdata, 1, pd) < 0)
1587 1972 return (-1);
1588 1973
1589 1974 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1590 1975 DTRACE_BUFDATA_AGGKEY) < 0)
1591 1976 return (-1);
1592 1977 }
1593 1978
1594 1979 assert(aggact != 0);
1595 1980
1596 1981 for (i = (naggvars == 1 ? 0 : 1); i < naggvars; i++) {
1597 1982 uint64_t normal;
1598 1983
↓ open down ↓ |
2 lines elided |
↑ open up ↑ |
1599 1984 aggdata = aggsdata[i];
1600 1985 agg = aggdata->dtada_desc;
1601 1986 rec = &agg->dtagd_rec[aggact];
1602 1987 act = rec->dtrd_action;
1603 1988 addr = aggdata->dtada_data + rec->dtrd_offset;
1604 1989 size = rec->dtrd_size;
1605 1990
1606 1991 assert(DTRACEACT_ISAGG(act));
1607 1992 normal = aggdata->dtada_normal;
1608 1993
1609 - if (dt_print_datum(dtp, fp, rec, addr, size, normal) < 0)
1994 + if (dt_print_datum(dtp, fp, rec, addr,
1995 + size, aggdata, normal, pd) < 0)
1610 1996 return (-1);
1611 1997
1612 1998 if (dt_buffered_flush(dtp, NULL, rec, aggdata,
1613 1999 DTRACE_BUFDATA_AGGVAL) < 0)
1614 2000 return (-1);
1615 2001
1616 2002 if (!pd->dtpa_allunprint)
1617 2003 agg->dtagd_flags |= DTRACE_AGD_PRINTED;
1618 2004 }
1619 2005
1620 - if (dt_printf(dtp, fp, "\n") < 0)
1621 - return (-1);
2006 + if (!pd->dtpa_agghist && !pd->dtpa_aggpack) {
2007 + if (dt_printf(dtp, fp, "\n") < 0)
2008 + return (-1);
2009 + }
1622 2010
1623 2011 if (dt_buffered_flush(dtp, NULL, NULL, aggdata,
1624 2012 DTRACE_BUFDATA_AGGFORMAT | DTRACE_BUFDATA_AGGLAST) < 0)
1625 2013 return (-1);
1626 2014
1627 2015 return (0);
1628 2016 }
1629 2017
1630 2018 int
1631 2019 dt_print_agg(const dtrace_aggdata_t *aggdata, void *arg)
1632 2020 {
1633 2021 dt_print_aggdata_t *pd = arg;
1634 2022 dtrace_aggdesc_t *agg = aggdata->dtada_desc;
1635 2023 dtrace_aggvarid_t aggvarid = pd->dtpa_id;
1636 2024
1637 2025 if (pd->dtpa_allunprint) {
1638 2026 if (agg->dtagd_flags & DTRACE_AGD_PRINTED)
1639 2027 return (0);
1640 2028 } else {
1641 2029 /*
1642 2030 * If we're not printing all unprinted aggregations, then the
1643 2031 * aggregation variable ID denotes a specific aggregation
1644 2032 * variable that we should print -- skip any other aggregations
1645 2033 * that we encounter.
1646 2034 */
1647 2035 if (agg->dtagd_nrecs == 0)
1648 2036 return (0);
1649 2037
1650 2038 if (aggvarid != agg->dtagd_varid)
1651 2039 return (0);
1652 2040 }
1653 2041
1654 2042 return (dt_print_aggs(&aggdata, 1, arg));
1655 2043 }
1656 2044
1657 2045 int
1658 2046 dt_setopt(dtrace_hdl_t *dtp, const dtrace_probedata_t *data,
1659 2047 const char *option, const char *value)
1660 2048 {
1661 2049 int len, rval;
1662 2050 char *msg;
1663 2051 const char *errstr;
1664 2052 dtrace_setoptdata_t optdata;
1665 2053
1666 2054 bzero(&optdata, sizeof (optdata));
1667 2055 (void) dtrace_getopt(dtp, option, &optdata.dtsda_oldval);
1668 2056
1669 2057 if (dtrace_setopt(dtp, option, value) == 0) {
1670 2058 (void) dtrace_getopt(dtp, option, &optdata.dtsda_newval);
1671 2059 optdata.dtsda_probe = data;
1672 2060 optdata.dtsda_option = option;
1673 2061 optdata.dtsda_handle = dtp;
1674 2062
1675 2063 if ((rval = dt_handle_setopt(dtp, &optdata)) != 0)
1676 2064 return (rval);
1677 2065
1678 2066 return (0);
1679 2067 }
1680 2068
1681 2069 errstr = dtrace_errmsg(dtp, dtrace_errno(dtp));
1682 2070 len = strlen(option) + strlen(value) + strlen(errstr) + 80;
1683 2071 msg = alloca(len);
1684 2072
1685 2073 (void) snprintf(msg, len, "couldn't set option \"%s\" to \"%s\": %s\n",
1686 2074 option, value, errstr);
1687 2075
1688 2076 if ((rval = dt_handle_liberr(dtp, data, msg)) == 0)
1689 2077 return (0);
1690 2078
1691 2079 return (rval);
1692 2080 }
1693 2081
1694 2082 static int
1695 2083 dt_consume_cpu(dtrace_hdl_t *dtp, FILE *fp, int cpu,
1696 2084 dtrace_bufdesc_t *buf, boolean_t just_one,
1697 2085 dtrace_consume_probe_f *efunc, dtrace_consume_rec_f *rfunc, void *arg)
1698 2086 {
1699 2087 dtrace_epid_t id;
1700 2088 size_t offs;
1701 2089 int flow = (dtp->dt_options[DTRACEOPT_FLOWINDENT] != DTRACEOPT_UNSET);
1702 2090 int quiet = (dtp->dt_options[DTRACEOPT_QUIET] != DTRACEOPT_UNSET);
1703 2091 int rval, i, n;
1704 2092 uint64_t tracememsize = 0;
1705 2093 dtrace_probedata_t data;
1706 2094 uint64_t drops;
1707 2095
1708 2096 bzero(&data, sizeof (data));
1709 2097 data.dtpda_handle = dtp;
1710 2098 data.dtpda_cpu = cpu;
1711 2099 data.dtpda_flow = dtp->dt_flow;
1712 2100 data.dtpda_indent = dtp->dt_indent;
1713 2101 data.dtpda_prefix = dtp->dt_prefix;
1714 2102
1715 2103 for (offs = buf->dtbd_oldest; offs < buf->dtbd_size; ) {
1716 2104 dtrace_eprobedesc_t *epd;
1717 2105
1718 2106 /*
1719 2107 * We're guaranteed to have an ID.
1720 2108 */
1721 2109 id = *(uint32_t *)((uintptr_t)buf->dtbd_data + offs);
1722 2110
1723 2111 if (id == DTRACE_EPIDNONE) {
1724 2112 /*
1725 2113 * This is filler to assure proper alignment of the
1726 2114 * next record; we simply ignore it.
1727 2115 */
1728 2116 offs += sizeof (id);
1729 2117 continue;
1730 2118 }
1731 2119
1732 2120 if ((rval = dt_epid_lookup(dtp, id, &data.dtpda_edesc,
1733 2121 &data.dtpda_pdesc)) != 0)
1734 2122 return (rval);
1735 2123
1736 2124 epd = data.dtpda_edesc;
1737 2125 data.dtpda_data = buf->dtbd_data + offs;
1738 2126
1739 2127 if (data.dtpda_edesc->dtepd_uarg != DT_ECB_DEFAULT) {
1740 2128 rval = dt_handle(dtp, &data);
1741 2129
1742 2130 if (rval == DTRACE_CONSUME_NEXT)
1743 2131 goto nextepid;
1744 2132
1745 2133 if (rval == DTRACE_CONSUME_ERROR)
1746 2134 return (-1);
1747 2135 }
1748 2136
1749 2137 if (flow)
1750 2138 (void) dt_flowindent(dtp, &data, dtp->dt_last_epid,
1751 2139 buf, offs);
1752 2140
1753 2141 rval = (*efunc)(&data, arg);
1754 2142
1755 2143 if (flow) {
1756 2144 if (data.dtpda_flow == DTRACEFLOW_ENTRY)
1757 2145 data.dtpda_indent += 2;
1758 2146 }
1759 2147
1760 2148 if (rval == DTRACE_CONSUME_NEXT)
1761 2149 goto nextepid;
1762 2150
1763 2151 if (rval == DTRACE_CONSUME_ABORT)
1764 2152 return (dt_set_errno(dtp, EDT_DIRABORT));
1765 2153
1766 2154 if (rval != DTRACE_CONSUME_THIS)
1767 2155 return (dt_set_errno(dtp, EDT_BADRVAL));
1768 2156
1769 2157 for (i = 0; i < epd->dtepd_nrecs; i++) {
1770 2158 caddr_t addr;
1771 2159 dtrace_recdesc_t *rec = &epd->dtepd_rec[i];
1772 2160 dtrace_actkind_t act = rec->dtrd_action;
1773 2161
1774 2162 data.dtpda_data = buf->dtbd_data + offs +
1775 2163 rec->dtrd_offset;
1776 2164 addr = data.dtpda_data;
1777 2165
1778 2166 if (act == DTRACEACT_LIBACT) {
1779 2167 uint64_t arg = rec->dtrd_arg;
1780 2168 dtrace_aggvarid_t id;
1781 2169
1782 2170 switch (arg) {
1783 2171 case DT_ACT_CLEAR:
1784 2172 /* LINTED - alignment */
1785 2173 id = *((dtrace_aggvarid_t *)addr);
1786 2174 (void) dtrace_aggregate_walk(dtp,
1787 2175 dt_clear_agg, &id);
1788 2176 continue;
1789 2177
1790 2178 case DT_ACT_DENORMALIZE:
1791 2179 /* LINTED - alignment */
1792 2180 id = *((dtrace_aggvarid_t *)addr);
1793 2181 (void) dtrace_aggregate_walk(dtp,
1794 2182 dt_denormalize_agg, &id);
1795 2183 continue;
1796 2184
1797 2185 case DT_ACT_FTRUNCATE:
1798 2186 if (fp == NULL)
1799 2187 continue;
1800 2188
1801 2189 (void) fflush(fp);
1802 2190 (void) ftruncate(fileno(fp), 0);
1803 2191 (void) fseeko(fp, 0, SEEK_SET);
1804 2192 continue;
1805 2193
1806 2194 case DT_ACT_NORMALIZE:
1807 2195 if (i == epd->dtepd_nrecs - 1)
1808 2196 return (dt_set_errno(dtp,
1809 2197 EDT_BADNORMAL));
1810 2198
1811 2199 if (dt_normalize(dtp,
1812 2200 buf->dtbd_data + offs, rec) != 0)
1813 2201 return (-1);
1814 2202
1815 2203 i++;
1816 2204 continue;
1817 2205
1818 2206 case DT_ACT_SETOPT: {
1819 2207 uint64_t *opts = dtp->dt_options;
1820 2208 dtrace_recdesc_t *valrec;
1821 2209 uint32_t valsize;
1822 2210 caddr_t val;
1823 2211 int rv;
1824 2212
1825 2213 if (i == epd->dtepd_nrecs - 1) {
1826 2214 return (dt_set_errno(dtp,
1827 2215 EDT_BADSETOPT));
1828 2216 }
1829 2217
1830 2218 valrec = &epd->dtepd_rec[++i];
1831 2219 valsize = valrec->dtrd_size;
1832 2220
1833 2221 if (valrec->dtrd_action != act ||
1834 2222 valrec->dtrd_arg != arg) {
1835 2223 return (dt_set_errno(dtp,
1836 2224 EDT_BADSETOPT));
1837 2225 }
1838 2226
1839 2227 if (valsize > sizeof (uint64_t)) {
1840 2228 val = buf->dtbd_data + offs +
1841 2229 valrec->dtrd_offset;
1842 2230 } else {
1843 2231 val = "1";
1844 2232 }
1845 2233
1846 2234 rv = dt_setopt(dtp, &data, addr, val);
1847 2235
1848 2236 if (rv != 0)
1849 2237 return (-1);
1850 2238
1851 2239 flow = (opts[DTRACEOPT_FLOWINDENT] !=
1852 2240 DTRACEOPT_UNSET);
1853 2241 quiet = (opts[DTRACEOPT_QUIET] !=
1854 2242 DTRACEOPT_UNSET);
1855 2243
1856 2244 continue;
1857 2245 }
1858 2246
1859 2247 case DT_ACT_TRUNC:
1860 2248 if (i == epd->dtepd_nrecs - 1)
1861 2249 return (dt_set_errno(dtp,
1862 2250 EDT_BADTRUNC));
1863 2251
1864 2252 if (dt_trunc(dtp,
1865 2253 buf->dtbd_data + offs, rec) != 0)
1866 2254 return (-1);
1867 2255
1868 2256 i++;
1869 2257 continue;
1870 2258
1871 2259 default:
1872 2260 continue;
1873 2261 }
1874 2262 }
1875 2263
1876 2264 if (act == DTRACEACT_TRACEMEM_DYNSIZE &&
1877 2265 rec->dtrd_size == sizeof (uint64_t)) {
1878 2266 /* LINTED - alignment */
1879 2267 tracememsize = *((unsigned long long *)addr);
1880 2268 continue;
1881 2269 }
1882 2270
1883 2271 rval = (*rfunc)(&data, rec, arg);
1884 2272
1885 2273 if (rval == DTRACE_CONSUME_NEXT)
1886 2274 continue;
1887 2275
1888 2276 if (rval == DTRACE_CONSUME_ABORT)
1889 2277 return (dt_set_errno(dtp, EDT_DIRABORT));
1890 2278
1891 2279 if (rval != DTRACE_CONSUME_THIS)
1892 2280 return (dt_set_errno(dtp, EDT_BADRVAL));
1893 2281
1894 2282 if (act == DTRACEACT_STACK) {
1895 2283 int depth = rec->dtrd_arg;
1896 2284
1897 2285 if (dt_print_stack(dtp, fp, NULL, addr, depth,
1898 2286 rec->dtrd_size / depth) < 0)
1899 2287 return (-1);
1900 2288 goto nextrec;
1901 2289 }
1902 2290
1903 2291 if (act == DTRACEACT_USTACK ||
1904 2292 act == DTRACEACT_JSTACK) {
1905 2293 if (dt_print_ustack(dtp, fp, NULL,
1906 2294 addr, rec->dtrd_arg) < 0)
1907 2295 return (-1);
1908 2296 goto nextrec;
1909 2297 }
1910 2298
1911 2299 if (act == DTRACEACT_SYM) {
1912 2300 if (dt_print_sym(dtp, fp, NULL, addr) < 0)
1913 2301 return (-1);
1914 2302 goto nextrec;
1915 2303 }
1916 2304
1917 2305 if (act == DTRACEACT_MOD) {
1918 2306 if (dt_print_mod(dtp, fp, NULL, addr) < 0)
1919 2307 return (-1);
1920 2308 goto nextrec;
1921 2309 }
1922 2310
1923 2311 if (act == DTRACEACT_USYM || act == DTRACEACT_UADDR) {
1924 2312 if (dt_print_usym(dtp, fp, addr, act) < 0)
1925 2313 return (-1);
1926 2314 goto nextrec;
1927 2315 }
1928 2316
1929 2317 if (act == DTRACEACT_UMOD) {
1930 2318 if (dt_print_umod(dtp, fp, NULL, addr) < 0)
1931 2319 return (-1);
1932 2320 goto nextrec;
1933 2321 }
1934 2322
1935 2323 if (DTRACEACT_ISPRINTFLIKE(act)) {
1936 2324 void *fmtdata;
1937 2325 int (*func)(dtrace_hdl_t *, FILE *, void *,
1938 2326 const dtrace_probedata_t *,
1939 2327 const dtrace_recdesc_t *, uint_t,
1940 2328 const void *buf, size_t);
1941 2329
1942 2330 if ((fmtdata = dt_format_lookup(dtp,
1943 2331 rec->dtrd_format)) == NULL)
1944 2332 goto nofmt;
1945 2333
1946 2334 switch (act) {
1947 2335 case DTRACEACT_PRINTF:
1948 2336 func = dtrace_fprintf;
1949 2337 break;
1950 2338 case DTRACEACT_PRINTA:
1951 2339 func = dtrace_fprinta;
1952 2340 break;
1953 2341 case DTRACEACT_SYSTEM:
1954 2342 func = dtrace_system;
1955 2343 break;
1956 2344 case DTRACEACT_FREOPEN:
1957 2345 func = dtrace_freopen;
1958 2346 break;
1959 2347 }
1960 2348
1961 2349 n = (*func)(dtp, fp, fmtdata, &data,
1962 2350 rec, epd->dtepd_nrecs - i,
1963 2351 (uchar_t *)buf->dtbd_data + offs,
1964 2352 buf->dtbd_size - offs);
1965 2353
1966 2354 if (n < 0)
1967 2355 return (-1); /* errno is set for us */
1968 2356
1969 2357 if (n > 0)
1970 2358 i += n - 1;
1971 2359 goto nextrec;
1972 2360 }
1973 2361
1974 2362 /*
1975 2363 * If this is a DIF expression, and the record has a
1976 2364 * format set, this indicates we have a CTF type name
1977 2365 * associated with the data and we should try to print
1978 2366 * it out by type.
1979 2367 */
1980 2368 if (act == DTRACEACT_DIFEXPR) {
1981 2369 const char *strdata = dt_strdata_lookup(dtp,
1982 2370 rec->dtrd_format);
1983 2371 if (strdata != NULL) {
1984 2372 n = dtrace_print(dtp, fp, strdata,
1985 2373 addr, rec->dtrd_size);
1986 2374
1987 2375 /*
1988 2376 * dtrace_print() will return -1 on
1989 2377 * error, or return the number of bytes
1990 2378 * consumed. It will return 0 if the
1991 2379 * type couldn't be determined, and we
1992 2380 * should fall through to the normal
1993 2381 * trace method.
1994 2382 */
1995 2383 if (n < 0)
1996 2384 return (-1);
1997 2385
1998 2386 if (n > 0)
1999 2387 goto nextrec;
2000 2388 }
2001 2389 }
2002 2390
2003 2391 nofmt:
2004 2392 if (act == DTRACEACT_PRINTA) {
2005 2393 dt_print_aggdata_t pd;
2006 2394 dtrace_aggvarid_t *aggvars;
2007 2395 int j, naggvars = 0;
2008 2396 size_t size = ((epd->dtepd_nrecs - i) *
2009 2397 sizeof (dtrace_aggvarid_t));
2010 2398
2011 2399 if ((aggvars = dt_alloc(dtp, size)) == NULL)
2012 2400 return (-1);
2013 2401
2014 2402 /*
2015 2403 * This might be a printa() with multiple
2016 2404 * aggregation variables. We need to scan
2017 2405 * forward through the records until we find
2018 2406 * a record from a different statement.
2019 2407 */
2020 2408 for (j = i; j < epd->dtepd_nrecs; j++) {
2021 2409 dtrace_recdesc_t *nrec;
2022 2410 caddr_t naddr;
2023 2411
2024 2412 nrec = &epd->dtepd_rec[j];
2025 2413
2026 2414 if (nrec->dtrd_uarg != rec->dtrd_uarg)
2027 2415 break;
2028 2416
2029 2417 if (nrec->dtrd_action != act) {
2030 2418 return (dt_set_errno(dtp,
2031 2419 EDT_BADAGG));
2032 2420 }
2033 2421
2034 2422 naddr = buf->dtbd_data + offs +
2035 2423 nrec->dtrd_offset;
2036 2424
2037 2425 aggvars[naggvars++] =
2038 2426 /* LINTED - alignment */
2039 2427 *((dtrace_aggvarid_t *)naddr);
2040 2428 }
2041 2429
2042 2430 i = j - 1;
2043 2431 bzero(&pd, sizeof (pd));
2044 2432 pd.dtpa_dtp = dtp;
2045 2433 pd.dtpa_fp = fp;
2046 2434
2047 2435 assert(naggvars >= 1);
2048 2436
2049 2437 if (naggvars == 1) {
2050 2438 pd.dtpa_id = aggvars[0];
2051 2439 dt_free(dtp, aggvars);
2052 2440
2053 2441 if (dt_printf(dtp, fp, "\n") < 0 ||
2054 2442 dtrace_aggregate_walk_sorted(dtp,
2055 2443 dt_print_agg, &pd) < 0)
2056 2444 return (-1);
2057 2445 goto nextrec;
2058 2446 }
2059 2447
2060 2448 if (dt_printf(dtp, fp, "\n") < 0 ||
2061 2449 dtrace_aggregate_walk_joined(dtp, aggvars,
2062 2450 naggvars, dt_print_aggs, &pd) < 0) {
2063 2451 dt_free(dtp, aggvars);
2064 2452 return (-1);
2065 2453 }
2066 2454
2067 2455 dt_free(dtp, aggvars);
↓ open down ↓ |
436 lines elided |
↑ open up ↑ |
2068 2456 goto nextrec;
2069 2457 }
2070 2458
2071 2459 if (act == DTRACEACT_TRACEMEM) {
2072 2460 if (tracememsize == 0 ||
2073 2461 tracememsize > rec->dtrd_size) {
2074 2462 tracememsize = rec->dtrd_size;
2075 2463 }
2076 2464
2077 2465 n = dt_print_bytes(dtp, fp, addr,
2078 - tracememsize, 33, quiet, 1);
2466 + tracememsize, -33, quiet, 1);
2079 2467
2080 2468 tracememsize = 0;
2081 2469
2082 2470 if (n < 0)
2083 2471 return (-1);
2084 2472
2085 2473 goto nextrec;
2086 2474 }
2087 2475
2088 2476 switch (rec->dtrd_size) {
2089 2477 case sizeof (uint64_t):
2090 2478 n = dt_printf(dtp, fp,
2091 2479 quiet ? "%lld" : " %16lld",
2092 2480 /* LINTED - alignment */
2093 2481 *((unsigned long long *)addr));
2094 2482 break;
2095 2483 case sizeof (uint32_t):
2096 2484 n = dt_printf(dtp, fp, quiet ? "%d" : " %8d",
2097 2485 /* LINTED - alignment */
2098 2486 *((uint32_t *)addr));
2099 2487 break;
2100 2488 case sizeof (uint16_t):
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
2101 2489 n = dt_printf(dtp, fp, quiet ? "%d" : " %5d",
2102 2490 /* LINTED - alignment */
2103 2491 *((uint16_t *)addr));
2104 2492 break;
2105 2493 case sizeof (uint8_t):
2106 2494 n = dt_printf(dtp, fp, quiet ? "%d" : " %3d",
2107 2495 *((uint8_t *)addr));
2108 2496 break;
2109 2497 default:
2110 2498 n = dt_print_bytes(dtp, fp, addr,
2111 - rec->dtrd_size, 33, quiet, 0);
2499 + rec->dtrd_size, -33, quiet, 0);
2112 2500 break;
2113 2501 }
2114 2502
2115 2503 if (n < 0)
2116 2504 return (-1); /* errno is set for us */
2117 2505
2118 2506 nextrec:
2119 2507 if (dt_buffered_flush(dtp, &data, rec, NULL, 0) < 0)
2120 2508 return (-1); /* errno is set for us */
2121 2509 }
2122 2510
2123 2511 /*
2124 2512 * Call the record callback with a NULL record to indicate
2125 2513 * that we're done processing this EPID.
2126 2514 */
2127 2515 rval = (*rfunc)(&data, NULL, arg);
2128 2516 nextepid:
2129 2517 offs += epd->dtepd_size;
2130 2518 dtp->dt_last_epid = id;
2131 2519 if (just_one) {
2132 2520 buf->dtbd_oldest = offs;
2133 2521 break;
2134 2522 }
2135 2523 }
2136 2524
2137 2525 dtp->dt_flow = data.dtpda_flow;
2138 2526 dtp->dt_indent = data.dtpda_indent;
2139 2527 dtp->dt_prefix = data.dtpda_prefix;
2140 2528
2141 2529 if ((drops = buf->dtbd_drops) == 0)
2142 2530 return (0);
2143 2531
2144 2532 /*
2145 2533 * Explicitly zero the drops to prevent us from processing them again.
2146 2534 */
2147 2535 buf->dtbd_drops = 0;
2148 2536
2149 2537 return (dt_handle_cpudrop(dtp, cpu, DTRACEDROP_PRINCIPAL, drops));
2150 2538 }
2151 2539
2152 2540 /*
2153 2541 * Reduce memory usage by shrinking the buffer if it's no more than half full.
2154 2542 * Note, we need to preserve the alignment of the data at dtbd_oldest, which is
2155 2543 * only 4-byte aligned.
2156 2544 */
2157 2545 static void
2158 2546 dt_realloc_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf, int cursize)
2159 2547 {
2160 2548 uint64_t used = buf->dtbd_size - buf->dtbd_oldest;
2161 2549 if (used < cursize / 2) {
2162 2550 int misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2163 2551 char *newdata = dt_alloc(dtp, used + misalign);
2164 2552 if (newdata == NULL)
2165 2553 return;
2166 2554 bzero(newdata, misalign);
2167 2555 bcopy(buf->dtbd_data + buf->dtbd_oldest,
2168 2556 newdata + misalign, used);
2169 2557 dt_free(dtp, buf->dtbd_data);
2170 2558 buf->dtbd_oldest = misalign;
2171 2559 buf->dtbd_size = used + misalign;
2172 2560 buf->dtbd_data = newdata;
2173 2561 }
2174 2562 }
2175 2563
2176 2564 /*
2177 2565 * If the ring buffer has wrapped, the data is not in order. Rearrange it
2178 2566 * so that it is. Note, we need to preserve the alignment of the data at
2179 2567 * dtbd_oldest, which is only 4-byte aligned.
2180 2568 */
2181 2569 static int
2182 2570 dt_unring_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2183 2571 {
2184 2572 int misalign;
2185 2573 char *newdata, *ndp;
2186 2574
2187 2575 if (buf->dtbd_oldest == 0)
2188 2576 return (0);
2189 2577
2190 2578 misalign = buf->dtbd_oldest & (sizeof (uint64_t) - 1);
2191 2579 newdata = ndp = dt_alloc(dtp, buf->dtbd_size + misalign);
2192 2580
2193 2581 if (newdata == NULL)
2194 2582 return (-1);
2195 2583
2196 2584 assert(0 == (buf->dtbd_size & (sizeof (uint64_t) - 1)));
2197 2585
2198 2586 bzero(ndp, misalign);
2199 2587 ndp += misalign;
2200 2588
2201 2589 bcopy(buf->dtbd_data + buf->dtbd_oldest, ndp,
2202 2590 buf->dtbd_size - buf->dtbd_oldest);
2203 2591 ndp += buf->dtbd_size - buf->dtbd_oldest;
2204 2592
2205 2593 bcopy(buf->dtbd_data, ndp, buf->dtbd_oldest);
2206 2594
2207 2595 dt_free(dtp, buf->dtbd_data);
2208 2596 buf->dtbd_oldest = 0;
2209 2597 buf->dtbd_data = newdata;
2210 2598 buf->dtbd_size += misalign;
2211 2599
2212 2600 return (0);
2213 2601 }
2214 2602
2215 2603 static void
2216 2604 dt_put_buf(dtrace_hdl_t *dtp, dtrace_bufdesc_t *buf)
2217 2605 {
2218 2606 dt_free(dtp, buf->dtbd_data);
2219 2607 dt_free(dtp, buf);
2220 2608 }
2221 2609
2222 2610 /*
2223 2611 * Returns 0 on success, in which case *cbp will be filled in if we retrieved
2224 2612 * data, or NULL if there is no data for this CPU.
2225 2613 * Returns -1 on failure and sets dt_errno.
2226 2614 */
2227 2615 static int
2228 2616 dt_get_buf(dtrace_hdl_t *dtp, int cpu, dtrace_bufdesc_t **bufp)
2229 2617 {
2230 2618 dtrace_optval_t size;
2231 2619 dtrace_bufdesc_t *buf = dt_zalloc(dtp, sizeof (*buf));
2232 2620 int error;
2233 2621
2234 2622 if (buf == NULL)
2235 2623 return (-1);
2236 2624
2237 2625 (void) dtrace_getopt(dtp, "bufsize", &size);
2238 2626 buf->dtbd_data = dt_alloc(dtp, size);
2239 2627 if (buf->dtbd_data == NULL) {
2240 2628 dt_free(dtp, buf);
2241 2629 return (-1);
2242 2630 }
2243 2631 buf->dtbd_size = size;
2244 2632 buf->dtbd_cpu = cpu;
2245 2633
2246 2634 if (dt_ioctl(dtp, DTRACEIOC_BUFSNAP, buf) == -1) {
2247 2635 dt_put_buf(dtp, buf);
2248 2636 /*
2249 2637 * If we failed with ENOENT, it may be because the
2250 2638 * CPU was unconfigured -- this is okay. Any other
2251 2639 * error, however, is unexpected.
2252 2640 */
2253 2641 if (errno == ENOENT) {
2254 2642 *bufp = NULL;
2255 2643 return (0);
2256 2644 }
2257 2645
2258 2646 return (dt_set_errno(dtp, errno));
2259 2647 }
2260 2648
2261 2649 error = dt_unring_buf(dtp, buf);
2262 2650 if (error != 0) {
2263 2651 dt_put_buf(dtp, buf);
2264 2652 return (error);
2265 2653 }
2266 2654 dt_realloc_buf(dtp, buf, size);
2267 2655
2268 2656 *bufp = buf;
2269 2657 return (0);
2270 2658 }
2271 2659
2272 2660 typedef struct dt_begin {
2273 2661 dtrace_consume_probe_f *dtbgn_probefunc;
2274 2662 dtrace_consume_rec_f *dtbgn_recfunc;
2275 2663 void *dtbgn_arg;
2276 2664 dtrace_handle_err_f *dtbgn_errhdlr;
2277 2665 void *dtbgn_errarg;
2278 2666 int dtbgn_beginonly;
2279 2667 } dt_begin_t;
2280 2668
2281 2669 static int
2282 2670 dt_consume_begin_probe(const dtrace_probedata_t *data, void *arg)
2283 2671 {
2284 2672 dt_begin_t *begin = arg;
2285 2673 dtrace_probedesc_t *pd = data->dtpda_pdesc;
2286 2674
2287 2675 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2288 2676 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2289 2677
2290 2678 if (begin->dtbgn_beginonly) {
2291 2679 if (!(r1 && r2))
2292 2680 return (DTRACE_CONSUME_NEXT);
2293 2681 } else {
2294 2682 if (r1 && r2)
2295 2683 return (DTRACE_CONSUME_NEXT);
2296 2684 }
2297 2685
2298 2686 /*
2299 2687 * We have a record that we're interested in. Now call the underlying
2300 2688 * probe function...
2301 2689 */
2302 2690 return (begin->dtbgn_probefunc(data, begin->dtbgn_arg));
2303 2691 }
2304 2692
2305 2693 static int
2306 2694 dt_consume_begin_record(const dtrace_probedata_t *data,
2307 2695 const dtrace_recdesc_t *rec, void *arg)
2308 2696 {
2309 2697 dt_begin_t *begin = arg;
2310 2698
2311 2699 return (begin->dtbgn_recfunc(data, rec, begin->dtbgn_arg));
2312 2700 }
2313 2701
2314 2702 static int
2315 2703 dt_consume_begin_error(const dtrace_errdata_t *data, void *arg)
2316 2704 {
2317 2705 dt_begin_t *begin = (dt_begin_t *)arg;
2318 2706 dtrace_probedesc_t *pd = data->dteda_pdesc;
2319 2707
2320 2708 int r1 = (strcmp(pd->dtpd_provider, "dtrace") == 0);
2321 2709 int r2 = (strcmp(pd->dtpd_name, "BEGIN") == 0);
2322 2710
2323 2711 if (begin->dtbgn_beginonly) {
2324 2712 if (!(r1 && r2))
2325 2713 return (DTRACE_HANDLE_OK);
2326 2714 } else {
2327 2715 if (r1 && r2)
2328 2716 return (DTRACE_HANDLE_OK);
2329 2717 }
2330 2718
2331 2719 return (begin->dtbgn_errhdlr(data, begin->dtbgn_errarg));
2332 2720 }
2333 2721
2334 2722 static int
2335 2723 dt_consume_begin(dtrace_hdl_t *dtp, FILE *fp,
2336 2724 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2337 2725 {
2338 2726 /*
2339 2727 * There's this idea that the BEGIN probe should be processed before
2340 2728 * everything else, and that the END probe should be processed after
2341 2729 * anything else. In the common case, this is pretty easy to deal
2342 2730 * with. However, a situation may arise where the BEGIN enabling and
2343 2731 * END enabling are on the same CPU, and some enabling in the middle
2344 2732 * occurred on a different CPU. To deal with this (blech!) we need to
2345 2733 * consume the BEGIN buffer up until the end of the BEGIN probe, and
2346 2734 * then set it aside. We will then process every other CPU, and then
2347 2735 * we'll return to the BEGIN CPU and process the rest of the data
2348 2736 * (which will inevitably include the END probe, if any). Making this
2349 2737 * even more complicated (!) is the library's ERROR enabling. Because
2350 2738 * this enabling is processed before we even get into the consume call
2351 2739 * back, any ERROR firing would result in the library's ERROR enabling
2352 2740 * being processed twice -- once in our first pass (for BEGIN probes),
2353 2741 * and again in our second pass (for everything but BEGIN probes). To
2354 2742 * deal with this, we interpose on the ERROR handler to assure that we
2355 2743 * only process ERROR enablings induced by BEGIN enablings in the
2356 2744 * first pass, and that we only process ERROR enablings _not_ induced
2357 2745 * by BEGIN enablings in the second pass.
2358 2746 */
2359 2747
2360 2748 dt_begin_t begin;
2361 2749 processorid_t cpu = dtp->dt_beganon;
2362 2750 int rval, i;
2363 2751 static int max_ncpus;
2364 2752 dtrace_bufdesc_t *buf;
2365 2753
2366 2754 dtp->dt_beganon = -1;
2367 2755
2368 2756 if (dt_get_buf(dtp, cpu, &buf) != 0)
2369 2757 return (-1);
2370 2758 if (buf == NULL)
2371 2759 return (0);
2372 2760
2373 2761 if (!dtp->dt_stopped || buf->dtbd_cpu != dtp->dt_endedon) {
2374 2762 /*
2375 2763 * This is the simple case. We're either not stopped, or if
2376 2764 * we are, we actually processed any END probes on another
2377 2765 * CPU. We can simply consume this buffer and return.
2378 2766 */
2379 2767 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
2380 2768 pf, rf, arg);
2381 2769 dt_put_buf(dtp, buf);
2382 2770 return (rval);
2383 2771 }
2384 2772
2385 2773 begin.dtbgn_probefunc = pf;
2386 2774 begin.dtbgn_recfunc = rf;
2387 2775 begin.dtbgn_arg = arg;
2388 2776 begin.dtbgn_beginonly = 1;
2389 2777
2390 2778 /*
2391 2779 * We need to interpose on the ERROR handler to be sure that we
2392 2780 * only process ERRORs induced by BEGIN.
2393 2781 */
2394 2782 begin.dtbgn_errhdlr = dtp->dt_errhdlr;
2395 2783 begin.dtbgn_errarg = dtp->dt_errarg;
2396 2784 dtp->dt_errhdlr = dt_consume_begin_error;
2397 2785 dtp->dt_errarg = &begin;
2398 2786
2399 2787 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
2400 2788 dt_consume_begin_probe, dt_consume_begin_record, &begin);
2401 2789
2402 2790 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2403 2791 dtp->dt_errarg = begin.dtbgn_errarg;
2404 2792
2405 2793 if (rval != 0) {
2406 2794 dt_put_buf(dtp, buf);
2407 2795 return (rval);
2408 2796 }
2409 2797
2410 2798 if (max_ncpus == 0)
2411 2799 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2412 2800
2413 2801 for (i = 0; i < max_ncpus; i++) {
2414 2802 dtrace_bufdesc_t *nbuf;
2415 2803 if (i == cpu)
2416 2804 continue;
2417 2805
2418 2806 if (dt_get_buf(dtp, i, &nbuf) != 0) {
2419 2807 dt_put_buf(dtp, buf);
2420 2808 return (-1);
2421 2809 }
2422 2810 if (nbuf == NULL)
2423 2811 continue;
2424 2812
2425 2813 rval = dt_consume_cpu(dtp, fp, i, nbuf, B_FALSE,
2426 2814 pf, rf, arg);
2427 2815 dt_put_buf(dtp, nbuf);
2428 2816 if (rval != 0) {
2429 2817 dt_put_buf(dtp, buf);
2430 2818 return (rval);
2431 2819 }
2432 2820 }
2433 2821
2434 2822 /*
2435 2823 * Okay -- we're done with the other buffers. Now we want to
2436 2824 * reconsume the first buffer -- but this time we're looking for
2437 2825 * everything _but_ BEGIN. And of course, in order to only consume
2438 2826 * those ERRORs _not_ associated with BEGIN, we need to reinstall our
2439 2827 * ERROR interposition function...
2440 2828 */
2441 2829 begin.dtbgn_beginonly = 0;
2442 2830
2443 2831 assert(begin.dtbgn_errhdlr == dtp->dt_errhdlr);
2444 2832 assert(begin.dtbgn_errarg == dtp->dt_errarg);
2445 2833 dtp->dt_errhdlr = dt_consume_begin_error;
2446 2834 dtp->dt_errarg = &begin;
2447 2835
2448 2836 rval = dt_consume_cpu(dtp, fp, cpu, buf, B_FALSE,
2449 2837 dt_consume_begin_probe, dt_consume_begin_record, &begin);
2450 2838
2451 2839 dtp->dt_errhdlr = begin.dtbgn_errhdlr;
2452 2840 dtp->dt_errarg = begin.dtbgn_errarg;
2453 2841
2454 2842 return (rval);
2455 2843 }
2456 2844
2457 2845 /* ARGSUSED */
2458 2846 static uint64_t
2459 2847 dt_buf_oldest(void *elem, void *arg)
2460 2848 {
2461 2849 dtrace_bufdesc_t *buf = elem;
2462 2850 size_t offs = buf->dtbd_oldest;
2463 2851
2464 2852 while (offs < buf->dtbd_size) {
2465 2853 dtrace_rechdr_t *dtrh =
2466 2854 /* LINTED - alignment */
2467 2855 (dtrace_rechdr_t *)(buf->dtbd_data + offs);
2468 2856 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) {
2469 2857 offs += sizeof (dtrace_epid_t);
2470 2858 } else {
2471 2859 return (DTRACE_RECORD_LOAD_TIMESTAMP(dtrh));
2472 2860 }
2473 2861 }
2474 2862
2475 2863 /* There are no records left; use the time the buffer was retrieved. */
2476 2864 return (buf->dtbd_timestamp);
2477 2865 }
2478 2866
2479 2867 int
2480 2868 dtrace_consume(dtrace_hdl_t *dtp, FILE *fp,
2481 2869 dtrace_consume_probe_f *pf, dtrace_consume_rec_f *rf, void *arg)
2482 2870 {
2483 2871 dtrace_optval_t size;
2484 2872 static int max_ncpus;
2485 2873 int i, rval;
2486 2874 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_SWITCHRATE];
2487 2875 hrtime_t now = gethrtime();
2488 2876
2489 2877 if (dtp->dt_lastswitch != 0) {
2490 2878 if (now - dtp->dt_lastswitch < interval)
2491 2879 return (0);
2492 2880
2493 2881 dtp->dt_lastswitch += interval;
2494 2882 } else {
2495 2883 dtp->dt_lastswitch = now;
2496 2884 }
2497 2885
2498 2886 if (!dtp->dt_active)
2499 2887 return (dt_set_errno(dtp, EINVAL));
2500 2888
2501 2889 if (max_ncpus == 0)
2502 2890 max_ncpus = dt_sysconf(dtp, _SC_CPUID_MAX) + 1;
2503 2891
2504 2892 if (pf == NULL)
2505 2893 pf = (dtrace_consume_probe_f *)dt_nullprobe;
2506 2894
2507 2895 if (rf == NULL)
2508 2896 rf = (dtrace_consume_rec_f *)dt_nullrec;
2509 2897
2510 2898 if (dtp->dt_options[DTRACEOPT_TEMPORAL] == DTRACEOPT_UNSET) {
2511 2899 /*
2512 2900 * The output will not be in the order it was traced. Rather,
2513 2901 * we will consume all of the data from each CPU's buffer in
2514 2902 * turn. We apply special handling for the records from BEGIN
2515 2903 * and END probes so that they are consumed first and last,
2516 2904 * respectively.
2517 2905 *
2518 2906 * If we have just begun, we want to first process the CPU that
2519 2907 * executed the BEGIN probe (if any).
2520 2908 */
2521 2909 if (dtp->dt_active && dtp->dt_beganon != -1 &&
2522 2910 (rval = dt_consume_begin(dtp, fp, pf, rf, arg)) != 0)
2523 2911 return (rval);
2524 2912
2525 2913 for (i = 0; i < max_ncpus; i++) {
2526 2914 dtrace_bufdesc_t *buf;
2527 2915
2528 2916 /*
2529 2917 * If we have stopped, we want to process the CPU on
2530 2918 * which the END probe was processed only _after_ we
2531 2919 * have processed everything else.
2532 2920 */
2533 2921 if (dtp->dt_stopped && (i == dtp->dt_endedon))
2534 2922 continue;
2535 2923
2536 2924 if (dt_get_buf(dtp, i, &buf) != 0)
2537 2925 return (-1);
2538 2926 if (buf == NULL)
2539 2927 continue;
2540 2928
2541 2929 dtp->dt_flow = 0;
2542 2930 dtp->dt_indent = 0;
2543 2931 dtp->dt_prefix = NULL;
2544 2932 rval = dt_consume_cpu(dtp, fp, i,
2545 2933 buf, B_FALSE, pf, rf, arg);
2546 2934 dt_put_buf(dtp, buf);
2547 2935 if (rval != 0)
2548 2936 return (rval);
2549 2937 }
2550 2938 if (dtp->dt_stopped) {
2551 2939 dtrace_bufdesc_t *buf;
2552 2940
2553 2941 if (dt_get_buf(dtp, dtp->dt_endedon, &buf) != 0)
2554 2942 return (-1);
2555 2943 if (buf == NULL)
2556 2944 return (0);
2557 2945
2558 2946 rval = dt_consume_cpu(dtp, fp, dtp->dt_endedon,
2559 2947 buf, B_FALSE, pf, rf, arg);
2560 2948 dt_put_buf(dtp, buf);
2561 2949 return (rval);
2562 2950 }
2563 2951 } else {
2564 2952 /*
2565 2953 * The output will be in the order it was traced (or for
2566 2954 * speculations, when it was committed). We retrieve a buffer
2567 2955 * from each CPU and put it into a priority queue, which sorts
2568 2956 * based on the first entry in the buffer. This is sufficient
2569 2957 * because entries within a buffer are already sorted.
2570 2958 *
2571 2959 * We then consume records one at a time, always consuming the
2572 2960 * oldest record, as determined by the priority queue. When
2573 2961 * we reach the end of the time covered by these buffers,
2574 2962 * we need to stop and retrieve more records on the next pass.
2575 2963 * The kernel tells us the time covered by each buffer, in
2576 2964 * dtbd_timestamp. The first buffer's timestamp tells us the
2577 2965 * time covered by all buffers, as subsequently retrieved
2578 2966 * buffers will cover to a more recent time.
2579 2967 */
2580 2968
2581 2969 uint64_t *drops = alloca(max_ncpus * sizeof (uint64_t));
2582 2970 uint64_t first_timestamp = 0;
2583 2971 uint_t cookie = 0;
2584 2972 dtrace_bufdesc_t *buf;
2585 2973
2586 2974 bzero(drops, max_ncpus * sizeof (uint64_t));
2587 2975
2588 2976 if (dtp->dt_bufq == NULL) {
2589 2977 dtp->dt_bufq = dt_pq_init(dtp, max_ncpus * 2,
2590 2978 dt_buf_oldest, NULL);
2591 2979 if (dtp->dt_bufq == NULL) /* ENOMEM */
2592 2980 return (-1);
2593 2981 }
2594 2982
2595 2983 /* Retrieve data from each CPU. */
2596 2984 (void) dtrace_getopt(dtp, "bufsize", &size);
2597 2985 for (i = 0; i < max_ncpus; i++) {
2598 2986 dtrace_bufdesc_t *buf;
2599 2987
2600 2988 if (dt_get_buf(dtp, i, &buf) != 0)
2601 2989 return (-1);
2602 2990 if (buf != NULL) {
2603 2991 if (first_timestamp == 0)
2604 2992 first_timestamp = buf->dtbd_timestamp;
2605 2993 assert(buf->dtbd_timestamp >= first_timestamp);
2606 2994
2607 2995 dt_pq_insert(dtp->dt_bufq, buf);
2608 2996 drops[i] = buf->dtbd_drops;
2609 2997 buf->dtbd_drops = 0;
2610 2998 }
2611 2999 }
2612 3000
2613 3001 /* Consume records. */
2614 3002 for (;;) {
2615 3003 dtrace_bufdesc_t *buf = dt_pq_pop(dtp->dt_bufq);
2616 3004 uint64_t timestamp;
2617 3005
2618 3006 if (buf == NULL)
2619 3007 break;
2620 3008
2621 3009 timestamp = dt_buf_oldest(buf, dtp);
2622 3010 assert(timestamp >= dtp->dt_last_timestamp);
2623 3011 dtp->dt_last_timestamp = timestamp;
2624 3012
2625 3013 if (timestamp == buf->dtbd_timestamp) {
2626 3014 /*
2627 3015 * We've reached the end of the time covered
2628 3016 * by this buffer. If this is the oldest
2629 3017 * buffer, we must do another pass
2630 3018 * to retrieve more data.
2631 3019 */
2632 3020 dt_put_buf(dtp, buf);
2633 3021 if (timestamp == first_timestamp &&
2634 3022 !dtp->dt_stopped)
2635 3023 break;
2636 3024 continue;
2637 3025 }
2638 3026
2639 3027 if ((rval = dt_consume_cpu(dtp, fp,
2640 3028 buf->dtbd_cpu, buf, B_TRUE, pf, rf, arg)) != 0)
2641 3029 return (rval);
2642 3030 dt_pq_insert(dtp->dt_bufq, buf);
2643 3031 }
2644 3032
2645 3033 /* Consume drops. */
2646 3034 for (i = 0; i < max_ncpus; i++) {
2647 3035 if (drops[i] != 0) {
2648 3036 int error = dt_handle_cpudrop(dtp, i,
2649 3037 DTRACEDROP_PRINCIPAL, drops[i]);
2650 3038 if (error != 0)
2651 3039 return (error);
2652 3040 }
2653 3041 }
2654 3042
2655 3043 /*
2656 3044 * Reduce memory usage by re-allocating smaller buffers
2657 3045 * for the "remnants".
2658 3046 */
2659 3047 while (buf = dt_pq_walk(dtp->dt_bufq, &cookie))
2660 3048 dt_realloc_buf(dtp, buf, buf->dtbd_size);
2661 3049 }
2662 3050
2663 3051 return (0);
2664 3052 }
↓ open down ↓ |
543 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX