1235368Sgnn#!/usr/sbin/dtrace -Zs
2/*
3 * php_malloc.d - PHP libc malloc analysis.
4 *                Written for the PHP DTrace provider.
5 *
6 * $Id: php_malloc.d 53 2007-09-24 04:58:38Z 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 PHP function, then that function is
11 * identified as responsible; else the caller of malloc() is identified as
12 * responsible - which will be a function from the PHP engine.
13 *
14 * USAGE: php_malloc.d { -p PID | -c cmd }	# hit Ctrl-C to end
15 *
16 * Filename and function 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
44php$target:::function-entry
45/arg0/
46{
47	self->file = basename(copyinstr(arg1));
48	self->name = copyinstr(arg0);
49}
50
51php$target:::function-return
52{
53	self->file = 0;
54	self->name = 0;
55}
56
57pid$target:libc:malloc:entry
58/self->file != NULL/
59{
60	@malloc_func_size[self->file, self->name] = sum(arg1);
61	@malloc_func_dist[self->file, self->name] = quantize(arg1);
62}
63
64pid$target:libc:malloc:entry
65/self->name == NULL/
66{
67	@malloc_lib_size[usym(ucaller)] = sum(arg1);
68	@malloc_lib_dist[usym(ucaller)] = quantize(arg1);
69}
70
71
72dtrace:::END
73{
74	printf("\nPHP malloc byte distributions by engine caller,\n\n");
75	printa("   %A, total bytes = %@d %@d\n", @malloc_lib_size,
76	    @malloc_lib_dist);
77
78	printf("\nPHP malloc byte distributions by PHP file and ");
79	printf("function,\n\n");
80	printa("   %s, %s, bytes total = %@d %@d\n", @malloc_func_size,
81	    @malloc_func_dist);
82}
83