1#!/usr/sbin/dtrace -Zs
2/*
3 * pl_malloc.d - Perl libc malloc analysis.
4 *               Written for the Perl DTrace provider.
5 *
6 * $Id: pl_malloc.d 19 2007-09-12 07:47:59Z brendan $
7 *
8 * This is an expiremental script to identify who is calling malloc() for
9 * memory allocation, and to print distribution plots of the requested bytes.
10 * If a malloc() occured while in a Perl subroutine, then that subroutine is
11 * identified as responsible; else the caller of malloc() is identified as
12 * responsible - which will be a function from the Perl engine.
13 *
14 * USAGE: pl_malloc.d { -p PID | -c cmd }	# hit Ctrl-C to end
15 *
16 * Filename and subroutine names are printed if available.
17 *
18 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
19 *
20 * CDDL HEADER START
21 *
22 *  The contents of this file are subject to the terms of the
23 *  Common Development and Distribution License, Version 1.0 only
24 *  (the "License").  You may not use this file except in compliance
25 *  with the License.
26 *
27 *  You can obtain a copy of the license at Docs/cddl1.txt
28 *  or http://www.opensolaris.org/os/licensing.
29 *  See the License for the specific language governing permissions
30 *  and limitations under the License.
31 *
32 * CDDL HEADER END
33 *
34 * 09-Sep-2007	Brendan Gregg	Created this.
35 */
36
37#pragma D option quiet
38
39dtrace:::BEGIN
40{
41	printf("Tracing... Hit Ctrl-C to end.\n");
42}
43
44perl$target:::sub-entry
45{
46	self->file = basename(copyinstr(arg1));
47	self->name = copyinstr(arg0);
48}
49
50perl$target:::sub-return
51{
52	self->file = 0;
53	self->name = 0;
54}
55
56pid$target:libc:malloc:entry
57/self->file != NULL/
58{
59	@malloc_sub_size[self->file, self->name] = sum(arg0);
60	@malloc_sub_dist[self->file, self->name] = quantize(arg0);
61}
62
63pid$target:libc:malloc:entry
64/self->name == NULL/
65{
66	@malloc_lib_size[usym(ucaller)] = sum(arg0);
67	@malloc_lib_dist[usym(ucaller)] = quantize(arg0);
68}
69
70
71dtrace:::END
72{
73	printf("\nPerl malloc byte distributions by engine caller,\n\n");
74	printa("   %A, total bytes = %@d %@d\n", @malloc_lib_size,
75	    @malloc_lib_dist);
76
77	printf("\nPerl malloc byte distributions by Perl file and ");
78	printf("subroutine,\n\n");
79	printa("   %s, %s, bytes total = %@d %@d\n", @malloc_sub_size,
80	    @malloc_sub_dist);
81}
82