1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * pl_malloc.d - Perl libc malloc analysis.
4235368Sgnn *               Written for the Perl DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: pl_malloc.d 19 2007-09-12 07:47:59Z brendan $
7235368Sgnn *
8235368Sgnn * This is an expiremental script to identify who is calling malloc() for
9235368Sgnn * memory allocation, and to print distribution plots of the requested bytes.
10235368Sgnn * If a malloc() occured while in a Perl subroutine, then that subroutine is
11235368Sgnn * identified as responsible; else the caller of malloc() is identified as
12235368Sgnn * responsible - which will be a function from the Perl engine.
13235368Sgnn *
14235368Sgnn * USAGE: pl_malloc.d { -p PID | -c cmd }	# hit Ctrl-C to end
15235368Sgnn *
16235368Sgnn * Filename and subroutine names are printed if available.
17235368Sgnn *
18235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
19235368Sgnn *
20235368Sgnn * CDDL HEADER START
21235368Sgnn *
22235368Sgnn *  The contents of this file are subject to the terms of the
23235368Sgnn *  Common Development and Distribution License, Version 1.0 only
24235368Sgnn *  (the "License").  You may not use this file except in compliance
25235368Sgnn *  with the License.
26235368Sgnn *
27235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
28235368Sgnn *  or http://www.opensolaris.org/os/licensing.
29235368Sgnn *  See the License for the specific language governing permissions
30235368Sgnn *  and limitations under the License.
31235368Sgnn *
32235368Sgnn * CDDL HEADER END
33235368Sgnn *
34235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
35235368Sgnn */
36235368Sgnn
37235368Sgnn#pragma D option quiet
38235368Sgnn
39235368Sgnndtrace:::BEGIN
40235368Sgnn{
41235368Sgnn	printf("Tracing... Hit Ctrl-C to end.\n");
42235368Sgnn}
43235368Sgnn
44235368Sgnnperl$target:::sub-entry
45235368Sgnn{
46235368Sgnn	self->file = basename(copyinstr(arg1));
47235368Sgnn	self->name = copyinstr(arg0);
48235368Sgnn}
49235368Sgnn
50235368Sgnnperl$target:::sub-return
51235368Sgnn{
52235368Sgnn	self->file = 0;
53235368Sgnn	self->name = 0;
54235368Sgnn}
55235368Sgnn
56235368Sgnnpid$target:libc:malloc:entry
57235368Sgnn/self->file != NULL/
58235368Sgnn{
59235368Sgnn	@malloc_sub_size[self->file, self->name] = sum(arg0);
60235368Sgnn	@malloc_sub_dist[self->file, self->name] = quantize(arg0);
61235368Sgnn}
62235368Sgnn
63235368Sgnnpid$target:libc:malloc:entry
64235368Sgnn/self->name == NULL/
65235368Sgnn{
66235368Sgnn	@malloc_lib_size[usym(ucaller)] = sum(arg0);
67235368Sgnn	@malloc_lib_dist[usym(ucaller)] = quantize(arg0);
68235368Sgnn}
69235368Sgnn
70235368Sgnn
71235368Sgnndtrace:::END
72235368Sgnn{
73235368Sgnn	printf("\nPerl malloc byte distributions by engine caller,\n\n");
74235368Sgnn	printa("   %A, total bytes = %@d %@d\n", @malloc_lib_size,
75235368Sgnn	    @malloc_lib_dist);
76235368Sgnn
77235368Sgnn	printf("\nPerl malloc byte distributions by Perl file and ");
78235368Sgnn	printf("subroutine,\n\n");
79235368Sgnn	printa("   %s, %s, bytes total = %@d %@d\n", @malloc_sub_size,
80235368Sgnn	    @malloc_sub_dist);
81235368Sgnn}
82