1235368Sgnn#!/usr/sbin/dtrace -CZs
2235368Sgnn/*
3235368Sgnn * py_profile.d - sample stack traces with Python translations using DTrace.
4235368Sgnn *                Written for the Python DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: py_profile.d 19 2007-09-12 07:47:59Z brendan $
7235368Sgnn *
8235368Sgnn * USAGE: py_profile.d { -p PID | -c cmd }	# hit Ctrl-C to end
9235368Sgnn *
10235368Sgnn * This samples stack traces for the process specified. This stack trace
11235368Sgnn * will cross the Python engine and system libraries, and insert
12235368Sgnn * translations for Python stack frames where appropriate. This is best
13235368Sgnn * explained with an example stack frame output,
14235368Sgnn *
15235368Sgnn *            libpython2.4.so.1.0`PyEval_EvalFrame+0x2fbf
16235368Sgnn *              [ ./func_loop.py:5 (func_c) ]
17235368Sgnn *            libpython2.4.so.1.0`fast_function+0xa8
18235368Sgnn *            libpython2.4.so.1.0`call_function+0xda
19235368Sgnn *            libpython2.4.so.1.0`PyEval_EvalFrame+0xbdf
20235368Sgnn *              [ ./func_loop.py:11 (func_b) ]
21235368Sgnn *            libpython2.4.so.1.0`fast_function+0xa8
22235368Sgnn *            libpython2.4.so.1.0`call_function+0xda
23235368Sgnn *            libpython2.4.so.1.0`PyEval_EvalFrame+0xbdf
24235368Sgnn *              [ ./func_loop.py:14 (func_a) ]
25235368Sgnn *            libpython2.4.so.1.0`fast_function+0xa8
26235368Sgnn *            libpython2.4.so.1.0`call_function+0xda
27235368Sgnn *            libpython2.4.so.1.0`PyEval_EvalFrame+0xbdf
28235368Sgnn *              [ ./func_loop.py:16 (?) ]
29235368Sgnn *
30235368Sgnn * The lines in square brackets are the native Python frames, the rest
31235368Sgnn * are the Python engine.
32235368Sgnn *
33235368Sgnn * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
34235368Sgnn *
35235368Sgnn * CDDL HEADER START
36235368Sgnn *
37235368Sgnn *  The contents of this file are subject to the terms of the
38235368Sgnn *  Common Development and Distribution License, Version 1.0 only
39235368Sgnn *  (the "License").  You may not use this file except in compliance
40235368Sgnn *  with the License.
41235368Sgnn *
42235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
43235368Sgnn *  or http://www.opensolaris.org/os/licensing.
44235368Sgnn *  See the License for the specific language governing permissions
45235368Sgnn *  and limitations under the License.
46235368Sgnn *
47235368Sgnn * CDDL HEADER END
48235368Sgnn *
49235368Sgnn * 09-Sep-2007	Brendan Gregg	Created this.
50235368Sgnn */
51235368Sgnn
52235368Sgnn#pragma D option quiet
53235368Sgnn#pragma D option jstackstrsize=1024
54235368Sgnn
55235368Sgnn/*
56235368Sgnn * Tunables
57235368Sgnn */
58235368Sgnn#define DEPTH	10		/* stack depth, frames */
59235368Sgnn#define RATE	1001		/* sampling rate, Hertz */
60235368Sgnn#define TOP	25		/* number of stacks to output */
61235368Sgnn
62235368Sgnndtrace:::BEGIN
63235368Sgnn{
64235368Sgnn	printf("Sampling %d-level stacks at %d Hertz... Hit Ctrl-C to end.\n",
65235368Sgnn	    DEPTH, RATE);
66235368Sgnn}
67235368Sgnn
68235368Sgnnprofile-RATE
69235368Sgnn/pid == $target/
70235368Sgnn{
71235368Sgnn	@stacks[jstack(DEPTH)] = count();
72235368Sgnn}
73235368Sgnn
74235368Sgnndtrace:::END
75235368Sgnn{
76235368Sgnn	trunc(@stacks, TOP);
77235368Sgnn	printf("Top %d most frequently sampled stacks,\n", TOP);
78235368Sgnn	printa(@stacks);
79235368Sgnn}
80