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