1N/A#!/usr/sbin/dtrace -Zs
1N/A/*
1N/A * py_malloc.d - Python libc malloc analysis.
1N/A * Written for the Python DTrace provider.
1N/A *
1N/A * $Id: py_malloc.d 19 2007-09-12 07:47:59Z brendan $
1N/A *
1N/A * This is an expiremental script to identify who is calling malloc() for
1N/A * memory allocation, and to print distribution plots of the requested bytes.
1N/A * If a malloc() occured while in a Python function, then that function is
1N/A * identified as responsible; else the caller of malloc() is identified as
1N/A * responsible - which will be a function from the Python engine.
1N/A *
1N/A * USAGE: py_malloc.d { -p PID | -c cmd } # hit Ctrl-C to end
1N/A *
1N/A * Filename and function names are printed if available.
1N/A *
1N/A * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
1N/A *
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License, Version 1.0 only
1N/A * (the "License"). You may not use this file except in compliance
1N/A * with the License.
1N/A *
1N/A * You can obtain a copy of the license at Docs/cddl1.txt
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * CDDL HEADER END
1N/A *
1N/A * 09-Sep-2007 Brendan Gregg Created this.
1N/A */
1N/A
1N/A#pragma D option quiet
1N/A
1N/Adtrace:::BEGIN
1N/A{
1N/A printf("Tracing... Hit Ctrl-C to end.\n");
1N/A}
1N/A
1N/Apython$target:::function-entry
1N/A{
1N/A self->file = basename(copyinstr(arg0));
1N/A self->name = copyinstr(arg1);
1N/A}
1N/A
1N/Apython$target:::function-return
1N/A{
1N/A self->file = 0;
1N/A self->name = 0;
1N/A}
1N/A
1N/Apid$target:libc:malloc:entry
1N/A/self->file != NULL/
1N/A{
1N/A @malloc_func_size[self->file, self->name] = sum(arg0);
1N/A @malloc_func_dist[self->file, self->name] = quantize(arg0);
1N/A}
1N/A
1N/Apid$target:libc:malloc:entry
1N/A/self->name == NULL/
1N/A{
1N/A @malloc_lib_size[usym(ucaller)] = sum(arg0);
1N/A @malloc_lib_dist[usym(ucaller)] = quantize(arg0);
1N/A}
1N/A
1N/A
1N/Adtrace:::END
1N/A{
1N/A printf("\nPython malloc byte distributions by engine caller,\n\n");
1N/A printa(" %A, total bytes = %@d %@d\n", @malloc_lib_size,
1N/A @malloc_lib_dist);
1N/A
1N/A printf("\nPython malloc byte distributions by Python file and ");
1N/A printf("function,\n\n");
1N/A printa(" %s, %s, bytes total = %@d %@d\n", @malloc_func_size,
1N/A @malloc_func_dist);
1N/A}