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