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