1*c29d5175Schristos #!/usr/sbin/dtrace -Zs 2*c29d5175Schristos /* 3*c29d5175Schristos * py_malloc.d - Python libc malloc analysis. 4*c29d5175Schristos * Written for the Python DTrace provider. 5*c29d5175Schristos * 6*c29d5175Schristos * $Id: py_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 Python 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 Python engine. 13*c29d5175Schristos * 14*c29d5175Schristos * USAGE: py_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 python$target:::function-entry 45*c29d5175Schristos { 46*c29d5175Schristos self->file = basename(copyinstr(arg0)); 47*c29d5175Schristos self->name = copyinstr(arg1); 48*c29d5175Schristos } 49*c29d5175Schristos 50*c29d5175Schristos python$target:::function-return 51*c29d5175Schristos { 52*c29d5175Schristos self->file = 0; 53*c29d5175Schristos self->name = 0; 54*c29d5175Schristos } 55*c29d5175Schristos 56*c29d5175Schristos pid$target:libc:malloc:entry 57*c29d5175Schristos /self->file != NULL/ 58*c29d5175Schristos { 59*c29d5175Schristos @malloc_func_size[self->file, self->name] = sum(arg0); 60*c29d5175Schristos @malloc_func_dist[self->file, self->name] = quantize(arg0); 61*c29d5175Schristos } 62*c29d5175Schristos 63*c29d5175Schristos pid$target:libc:malloc:entry 64*c29d5175Schristos /self->name == NULL/ 65*c29d5175Schristos { 66*c29d5175Schristos @malloc_lib_size[usym(ucaller)] = sum(arg0); 67*c29d5175Schristos @malloc_lib_dist[usym(ucaller)] = quantize(arg0); 68*c29d5175Schristos } 69*c29d5175Schristos 70*c29d5175Schristos 71*c29d5175Schristos dtrace:::END 72*c29d5175Schristos { 73*c29d5175Schristos printf("\nPython malloc byte distributions by engine caller,\n\n"); 74*c29d5175Schristos printa(" %A, total bytes = %@d %@d\n", @malloc_lib_size, 75*c29d5175Schristos @malloc_lib_dist); 76*c29d5175Schristos 77*c29d5175Schristos printf("\nPython malloc byte distributions by Python file and "); 78*c29d5175Schristos printf("function,\n\n"); 79*c29d5175Schristos printa(" %s, %s, bytes total = %@d %@d\n", @malloc_func_size, 80*c29d5175Schristos @malloc_func_dist); 81*c29d5175Schristos } 82