1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * php_malloc.d - PHP libc malloc analysis.
4235368Sgnn *                Written for the PHP DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: php_malloc.d 53 2007-09-24 04:58:38Z 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 PHP function, then that function is
11235368Sgnn * identified as responsible; else the caller of malloc() is identified as
12235368Sgnn * responsible - which will be a function from the PHP engine.
13235368Sgnn *
14235368Sgnn * USAGE: php_malloc.d { -p PID | -c cmd }	# hit Ctrl-C to end
15235368Sgnn *
16235368Sgnn * Filename and function 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
44235368Sgnnphp$target:::function-entry
45235368Sgnn/arg0/
46235368Sgnn{
47235368Sgnn	self->file = basename(copyinstr(arg1));
48235368Sgnn	self->name = copyinstr(arg0);
49235368Sgnn}
50235368Sgnn
51235368Sgnnphp$target:::function-return
52235368Sgnn{
53235368Sgnn	self->file = 0;
54235368Sgnn	self->name = 0;
55235368Sgnn}
56235368Sgnn
57235368Sgnnpid$target:libc:malloc:entry
58235368Sgnn/self->file != NULL/
59235368Sgnn{
60235368Sgnn	@malloc_func_size[self->file, self->name] = sum(arg1);
61235368Sgnn	@malloc_func_dist[self->file, self->name] = quantize(arg1);
62235368Sgnn}
63235368Sgnn
64235368Sgnnpid$target:libc:malloc:entry
65235368Sgnn/self->name == NULL/
66235368Sgnn{
67235368Sgnn	@malloc_lib_size[usym(ucaller)] = sum(arg1);
68235368Sgnn	@malloc_lib_dist[usym(ucaller)] = quantize(arg1);
69235368Sgnn}
70235368Sgnn
71235368Sgnn
72235368Sgnndtrace:::END
73235368Sgnn{
74235368Sgnn	printf("\nPHP malloc byte distributions by engine caller,\n\n");
75235368Sgnn	printa("   %A, total bytes = %@d %@d\n", @malloc_lib_size,
76235368Sgnn	    @malloc_lib_dist);
77235368Sgnn
78235368Sgnn	printf("\nPHP malloc byte distributions by PHP file and ");
79235368Sgnn	printf("function,\n\n");
80235368Sgnn	printa("   %s, %s, bytes total = %@d %@d\n", @malloc_func_size,
81235368Sgnn	    @malloc_func_dist);
82235368Sgnn}
83