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