1*c29d5175Schristos #!/usr/sbin/dtrace -CZs 2*c29d5175Schristos /* 3*c29d5175Schristos * py_profile.d - sample stack traces with Python translations using DTrace. 4*c29d5175Schristos * Written for the Python DTrace provider. 5*c29d5175Schristos * 6*c29d5175Schristos * $Id: py_profile.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $ 7*c29d5175Schristos * 8*c29d5175Schristos * USAGE: py_profile.d { -p PID | -c cmd } # hit Ctrl-C to end 9*c29d5175Schristos * 10*c29d5175Schristos * This samples stack traces for the process specified. This stack trace 11*c29d5175Schristos * will cross the Python engine and system libraries, and insert 12*c29d5175Schristos * translations for Python stack frames where appropriate. This is best 13*c29d5175Schristos * explained with an example stack frame output, 14*c29d5175Schristos * 15*c29d5175Schristos * libpython2.4.so.1.0`PyEval_EvalFrame+0x2fbf 16*c29d5175Schristos * [ ./func_loop.py:5 (func_c) ] 17*c29d5175Schristos * libpython2.4.so.1.0`fast_function+0xa8 18*c29d5175Schristos * libpython2.4.so.1.0`call_function+0xda 19*c29d5175Schristos * libpython2.4.so.1.0`PyEval_EvalFrame+0xbdf 20*c29d5175Schristos * [ ./func_loop.py:11 (func_b) ] 21*c29d5175Schristos * libpython2.4.so.1.0`fast_function+0xa8 22*c29d5175Schristos * libpython2.4.so.1.0`call_function+0xda 23*c29d5175Schristos * libpython2.4.so.1.0`PyEval_EvalFrame+0xbdf 24*c29d5175Schristos * [ ./func_loop.py:14 (func_a) ] 25*c29d5175Schristos * libpython2.4.so.1.0`fast_function+0xa8 26*c29d5175Schristos * libpython2.4.so.1.0`call_function+0xda 27*c29d5175Schristos * libpython2.4.so.1.0`PyEval_EvalFrame+0xbdf 28*c29d5175Schristos * [ ./func_loop.py:16 (?) ] 29*c29d5175Schristos * 30*c29d5175Schristos * The lines in square brackets are the native Python frames, the rest 31*c29d5175Schristos * are the Python engine. 32*c29d5175Schristos * 33*c29d5175Schristos * COPYRIGHT: Copyright (c) 2007 Brendan Gregg. 34*c29d5175Schristos * 35*c29d5175Schristos * CDDL HEADER START 36*c29d5175Schristos * 37*c29d5175Schristos * The contents of this file are subject to the terms of the 38*c29d5175Schristos * Common Development and Distribution License, Version 1.0 only 39*c29d5175Schristos * (the "License"). You may not use this file except in compliance 40*c29d5175Schristos * with the License. 41*c29d5175Schristos * 42*c29d5175Schristos * You can obtain a copy of the license at Docs/cddl1.txt 43*c29d5175Schristos * or http://www.opensolaris.org/os/licensing. 44*c29d5175Schristos * See the License for the specific language governing permissions 45*c29d5175Schristos * and limitations under the License. 46*c29d5175Schristos * 47*c29d5175Schristos * CDDL HEADER END 48*c29d5175Schristos * 49*c29d5175Schristos * 09-Sep-2007 Brendan Gregg Created this. 50*c29d5175Schristos */ 51*c29d5175Schristos 52*c29d5175Schristos #pragma D option quiet 53*c29d5175Schristos #pragma D option jstackstrsize=1024 54*c29d5175Schristos 55*c29d5175Schristos /* 56*c29d5175Schristos * Tunables 57*c29d5175Schristos */ 58*c29d5175Schristos #define DEPTH 10 /* stack depth, frames */ 59*c29d5175Schristos #define RATE 1001 /* sampling rate, Hertz */ 60*c29d5175Schristos #define TOP 25 /* number of stacks to output */ 61*c29d5175Schristos 62*c29d5175Schristos dtrace:::BEGIN 63*c29d5175Schristos { 64*c29d5175Schristos printf("Sampling %d-level stacks at %d Hertz... Hit Ctrl-C to end.\n", 65*c29d5175Schristos DEPTH, RATE); 66*c29d5175Schristos } 67*c29d5175Schristos 68*c29d5175Schristos profile-RATE 69*c29d5175Schristos /pid == $target/ 70*c29d5175Schristos { 71*c29d5175Schristos @stacks[jstack(DEPTH)] = count(); 72*c29d5175Schristos } 73*c29d5175Schristos 74*c29d5175Schristos dtrace:::END 75*c29d5175Schristos { 76*c29d5175Schristos trunc(@stacks, TOP); 77*c29d5175Schristos printf("Top %d most frequently sampled stacks,\n", TOP); 78*c29d5175Schristos printa(@stacks); 79*c29d5175Schristos } 80