183610Ssobomax#!/usr/sbin/dtrace -Zs
283610Ssobomax/*
383610Ssobomax * php_malloc.d - PHP libc malloc analysis.
483610Ssobomax *                Written for the PHP DTrace provider.
583610Ssobomax *
683610Ssobomax * $Id: php_malloc.d 53 2007-09-24 04:58:38Z brendan $
783610Ssobomax *
883610Ssobomax * This is an expiremental script to identify who is calling malloc() for
983610Ssobomax * memory allocation, and to print distribution plots of the requested bytes.
1083610Ssobomax * If a malloc() occured while in a PHP function, then that function is
1183610Ssobomax * identified as responsible; else the caller of malloc() is identified as
1283610Ssobomax * responsible - which will be a function from the PHP engine.
1383610Ssobomax *
1483610Ssobomax * USAGE: php_malloc.d { -p PID | -c cmd }	# hit Ctrl-C to end
1583610Ssobomax *
1683610Ssobomax * Filename and function names are printed if available.
1783610Ssobomax *
1883610Ssobomax * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
1983610Ssobomax *
2083610Ssobomax * CDDL HEADER START
2183610Ssobomax *
2283610Ssobomax *  The contents of this file are subject to the terms of the
2383610Ssobomax *  Common Development and Distribution License, Version 1.0 only
2483610Ssobomax *  (the "License").  You may not use this file except in compliance
2583610Ssobomax *  with the License.
2683610Ssobomax *
2783610Ssobomax *  You can obtain a copy of the license at Docs/cddl1.txt
2884221Sdillon *  or http://www.opensolaris.org/os/licensing.
2984221Sdillon *  See the License for the specific language governing permissions
3084221Sdillon *  and limitations under the License.
31174741Ssobomax *
3283610Ssobomax * CDDL HEADER END
33174741Ssobomax *
34174741Ssobomax * 09-Sep-2007	Brendan Gregg	Created this.
35174741Ssobomax */
36174741Ssobomax
37174741Ssobomax#pragma D option quiet
3883610Ssobomax
39174741Ssobomaxdtrace:::BEGIN
40174741Ssobomax{
41174741Ssobomax	printf("Tracing... Hit Ctrl-C to end.\n");
42174741Ssobomax}
43174741Ssobomax
44174741Ssobomaxphp$target:::function-entry
45174741Ssobomax/arg0/
46174741Ssobomax{
47174741Ssobomax	self->file = basename(copyinstr(arg1));
48174741Ssobomax	self->name = copyinstr(arg0);
4983610Ssobomax}
5083610Ssobomax
51146324Sobrienphp$target:::function-return
5283610Ssobomax{
5383610Ssobomax	self->file = 0;
5483610Ssobomax	self->name = 0;
5583610Ssobomax}
5683610Ssobomax
5783610Ssobomaxpid$target:libc:malloc:entry
5883610Ssobomax/self->file != NULL/
5983610Ssobomax{
60174741Ssobomax	@malloc_func_size[self->file, self->name] = sum(arg1);
6183610Ssobomax	@malloc_func_dist[self->file, self->name] = quantize(arg1);
6283610Ssobomax}
6383610Ssobomax
6483610Ssobomaxpid$target:libc:malloc:entry
6583610Ssobomax/self->name == NULL/
6683610Ssobomax{
6783610Ssobomax	@malloc_lib_size[usym(ucaller)] = sum(arg1);
6883610Ssobomax	@malloc_lib_dist[usym(ucaller)] = quantize(arg1);
6983610Ssobomax}
70174741Ssobomax
7183610Ssobomax
7283610Ssobomaxdtrace:::END
7383610Ssobomax{
7483610Ssobomax	printf("\nPHP malloc byte distributions by engine caller,\n\n");
7583610Ssobomax	printa("   %A, total bytes = %@d %@d\n", @malloc_lib_size,
7683610Ssobomax	    @malloc_lib_dist);
7783610Ssobomax
7883610Ssobomax	printf("\nPHP malloc byte distributions by PHP file and ");
7983610Ssobomax	printf("function,\n\n");
8083610Ssobomax	printa("   %s, %s, bytes total = %@d %@d\n", @malloc_func_size,
81174741Ssobomax	    @malloc_func_dist);
8283610Ssobomax}
8383610Ssobomax