xref: /netbsd-src/external/cddl/dtracetoolkit/dist/Python/py_flowtime.d (revision c29d51755812ace2e87aeefdb06cb2b4dac7087a)
1*c29d5175Schristos #!/usr/sbin/dtrace -Zs
2*c29d5175Schristos /*
3*c29d5175Schristos  * py_flowtime.d - snoop Python functions with flow and delta times.
4*c29d5175Schristos  *                 Written for the Python DTrace provider.
5*c29d5175Schristos  *
6*c29d5175Schristos  * $Id: py_flowtime.d,v 1.1.1.1 2015/09/30 22:01:09 christos Exp $
7*c29d5175Schristos  *
8*c29d5175Schristos  * This traces shell activity from Python programs on the system that are
9*c29d5175Schristos  * running with Python provider support.
10*c29d5175Schristos  *
11*c29d5175Schristos  * USAGE: py_flowtime.d			# hit Ctrl-C to end
12*c29d5175Schristos  *
13*c29d5175Schristos  * This watches Python function entries and returns, and indents child
14*c29d5175Schristos  * function calls.
15*c29d5175Schristos  *
16*c29d5175Schristos  * FIELDS:
17*c29d5175Schristos  *		C		CPU-id
18*c29d5175Schristos  *		TIME(us)	Time since boot, us
19*c29d5175Schristos  *		FILE		Filename that this function belongs to
20*c29d5175Schristos  *		DELTA(us)	Elapsed time from previous line to this line
21*c29d5175Schristos  *		FUNC		Python function name
22*c29d5175Schristos  *
23*c29d5175Schristos  * LEGEND:
24*c29d5175Schristos  *		->		function entry
25*c29d5175Schristos  *		<-		function return
26*c29d5175Schristos  *
27*c29d5175Schristos  * Filename and function names are printed if available.
28*c29d5175Schristos  *
29*c29d5175Schristos  * WARNING: Watch the first column carefully, it prints the CPU-id. If it
30*c29d5175Schristos  * changes, then it is very likely that the output has been shuffled.
31*c29d5175Schristos  *
32*c29d5175Schristos  * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
33*c29d5175Schristos  *
34*c29d5175Schristos  * CDDL HEADER START
35*c29d5175Schristos  *
36*c29d5175Schristos  *  The contents of this file are subject to the terms of the
37*c29d5175Schristos  *  Common Development and Distribution License, Version 1.0 only
38*c29d5175Schristos  *  (the "License").  You may not use this file except in compliance
39*c29d5175Schristos  *  with the License.
40*c29d5175Schristos  *
41*c29d5175Schristos  *  You can obtain a copy of the license at Docs/cddl1.txt
42*c29d5175Schristos  *  or http://www.opensolaris.org/os/licensing.
43*c29d5175Schristos  *  See the License for the specific language governing permissions
44*c29d5175Schristos  *  and limitations under the License.
45*c29d5175Schristos  *
46*c29d5175Schristos  * CDDL HEADER END
47*c29d5175Schristos  *
48*c29d5175Schristos  * 09-Sep-2007	Brendan Gregg	Created this.
49*c29d5175Schristos  */
50*c29d5175Schristos 
51*c29d5175Schristos #pragma D option quiet
52*c29d5175Schristos #pragma D option switchrate=10
53*c29d5175Schristos 
54*c29d5175Schristos self int depth;
55*c29d5175Schristos 
56*c29d5175Schristos self int last;
57*c29d5175Schristos 
58*c29d5175Schristos dtrace:::BEGIN
59*c29d5175Schristos {
60*c29d5175Schristos 	printf("%3s %-16s %-16s %9s  -- %s\n", "C", "TIME(us)", "FILE",
61*c29d5175Schristos 	    "DELTA(us)", "FUNC");
62*c29d5175Schristos }
63*c29d5175Schristos 
64*c29d5175Schristos python*:::function-entry,
65*c29d5175Schristos python*:::function-return
66*c29d5175Schristos /self->last == 0/
67*c29d5175Schristos {
68*c29d5175Schristos 	self->last = timestamp;
69*c29d5175Schristos }
70*c29d5175Schristos 
71*c29d5175Schristos python*:::function-entry
72*c29d5175Schristos {
73*c29d5175Schristos 	this->delta = (timestamp - self->last) / 1000;
74*c29d5175Schristos 	printf("%3d %-16d %-16s %9d %*s-> %s\n", cpu, timestamp / 1000,
75*c29d5175Schristos 	    basename(copyinstr(arg0)), this->delta, self->depth * 2, "",
76*c29d5175Schristos 	    copyinstr(arg1));
77*c29d5175Schristos 	self->depth++;
78*c29d5175Schristos 	self->last = timestamp;
79*c29d5175Schristos }
80*c29d5175Schristos 
81*c29d5175Schristos python*:::function-return
82*c29d5175Schristos {
83*c29d5175Schristos 	this->delta = (timestamp - self->last) / 1000;
84*c29d5175Schristos 	self->depth -= self->depth > 0 ? 1 : 0;
85*c29d5175Schristos 	printf("%3d %-16d %-16s %9d %*s<- %s\n", cpu, timestamp / 1000,
86*c29d5175Schristos 	    basename(copyinstr(arg0)), this->delta, self->depth * 2, "",
87*c29d5175Schristos 	    copyinstr(arg1));
88*c29d5175Schristos 	self->last = timestamp;
89*c29d5175Schristos }
90