1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * pl_cpudist.d - measure Perl on-CPU times for subroutines.
4235368Sgnn *                Written for the Perl DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: pl_cpudist.d 28 2007-09-13 10:49:37Z brendan $
7235368Sgnn *
8235368Sgnn * This traces Perl activity from all programs running on the system with
9235368Sgnn * Perl provider support.
10235368Sgnn *
11235368Sgnn * USAGE: pl_cpudist.d 		# hit Ctrl-C to end
12235368Sgnn *
13235368Sgnn * This script prints distribution plots of elapsed time for Perl subrotines.
14235368Sgnn * Use pl_cputime.d for summary reports.
15235368Sgnn *
16235368Sgnn * FIELDS:
17235368Sgnn *		1		Filename of the Perl program
18235368Sgnn *		2		Type of call (sub)
19235368Sgnn *		3		Name of call
20235368Sgnn *
21235368Sgnn * Filename and subroutine names are printed if available.
22235368Sgnn *
23235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
24235368Sgnn *
25235368Sgnn * CDDL HEADER START
26235368Sgnn *
27235368Sgnn *  The contents of this file are subject to the terms of the
28235368Sgnn *  Common Development and Distribution License, Version 1.0 only
29235368Sgnn *  (the "License").  You may not use this file except in compliance
30235368Sgnn *  with the License.
31235368Sgnn *
32235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
33235368Sgnn *  or http://www.opensolaris.org/os/licensing.
34235368Sgnn *  See the License for the specific language governing permissions
35235368Sgnn *  and limitations under the License.
36235368Sgnn *
37235368Sgnn * CDDL HEADER END
38235368Sgnn *
39235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
40235368Sgnn */
41235368Sgnn
42235368Sgnn#pragma D option quiet
43235368Sgnn
44235368Sgnndtrace:::BEGIN
45235368Sgnn{
46235368Sgnn	printf("Tracing... Hit Ctrl-C to end.\n");
47235368Sgnn}
48235368Sgnn
49235368Sgnnperl*:::sub-entry
50235368Sgnn{
51235368Sgnn	self->depth++;
52235368Sgnn	self->exclude[self->depth] = 0;
53235368Sgnn	self->sub[self->depth] = vtimestamp;
54235368Sgnn}
55235368Sgnn
56235368Sgnnperl*:::sub-return
57235368Sgnn/self->sub[self->depth]/
58235368Sgnn{
59235368Sgnn	this->oncpu_incl = vtimestamp - self->sub[self->depth];
60235368Sgnn	this->oncpu_excl = this->oncpu_incl - self->exclude[self->depth];
61235368Sgnn	self->sub[self->depth] = 0;
62235368Sgnn	self->exclude[self->depth] = 0;
63235368Sgnn	this->file = basename(copyinstr(arg1));
64235368Sgnn	this->name = copyinstr(arg0);
65235368Sgnn
66235368Sgnn	@types_incl[this->file, "sub", this->name] =
67235368Sgnn	    quantize(this->oncpu_incl / 1000);
68235368Sgnn	@types_excl[this->file, "sub", this->name] =
69235368Sgnn	    quantize(this->oncpu_excl / 1000);
70235368Sgnn
71235368Sgnn	self->depth--;
72235368Sgnn	self->exclude[self->depth] += this->oncpu_incl;
73235368Sgnn}
74235368Sgnn
75235368Sgnndtrace:::END
76235368Sgnn{
77235368Sgnn	printf("\nExclusive subroutine on-CPU times (us),\n");
78235368Sgnn	printa("   %s, %s, %s %@d\n", @types_excl);
79235368Sgnn
80235368Sgnn	printf("\nInclusive subroutine on-CPU times (us),\n");
81235368Sgnn	printa("   %s, %s, %s %@d\n", @types_incl);
82235368Sgnn}
83