11556Srgrimes#!/usr/sbin/dtrace -Zs
21556Srgrimes/*
31556Srgrimes * py_flowtime.d - snoop Python functions with flow and delta times.
41556Srgrimes *                 Written for the Python DTrace provider.
51556Srgrimes *
61556Srgrimes * $Id: py_flowtime.d 41 2007-09-17 02:20:10Z brendan $
71556Srgrimes *
81556Srgrimes * This traces shell activity from Python programs on the system that are
91556Srgrimes * running with Python provider support.
101556Srgrimes *
111556Srgrimes * USAGE: py_flowtime.d			# hit Ctrl-C to end
121556Srgrimes *
131556Srgrimes * This watches Python function entries and returns, and indents child
141556Srgrimes * function calls.
151556Srgrimes *
161556Srgrimes * FIELDS:
171556Srgrimes *		C		CPU-id
181556Srgrimes *		TIME(us)	Time since boot, us
191556Srgrimes *		FILE		Filename that this function belongs to
201556Srgrimes *		DELTA(us)	Elapsed time from previous line to this line
211556Srgrimes *		FUNC		Python function name
221556Srgrimes *
231556Srgrimes * LEGEND:
241556Srgrimes *		->		function entry
251556Srgrimes *		<-		function return
261556Srgrimes *
271556Srgrimes * Filename and function names are printed if available.
281556Srgrimes *
291556Srgrimes * WARNING: Watch the first column carefully, it prints the CPU-id. If it
301556Srgrimes * changes, then it is very likely that the output has been shuffled.
311556Srgrimes *
321556Srgrimes * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
331556Srgrimes *
3436150Scharnier * CDDL HEADER START
3536150Scharnier *
3636150Scharnier *  The contents of this file are subject to the terms of the
371556Srgrimes *  Common Development and Distribution License, Version 1.0 only
3899110Sobrien *  (the "License").  You may not use this file except in compliance
3999110Sobrien *  with the License.
401556Srgrimes *
411556Srgrimes *  You can obtain a copy of the license at Docs/cddl1.txt
421556Srgrimes *  or http://www.opensolaris.org/os/licensing.
431556Srgrimes *  See the License for the specific language governing permissions
441556Srgrimes *  and limitations under the License.
451556Srgrimes *
46279569Sjilles * CDDL HEADER END
471556Srgrimes *
481556Srgrimes * 09-Sep-2007	Brendan Gregg	Created this.
491556Srgrimes */
501556Srgrimes
5153891Scracauer#pragma D option quiet
5217987Speter#pragma D option switchrate=10
5338521Scracauer
541556Srgrimesself int depth;
5578732Sdd
5617987Speterself int last;
571556Srgrimes
581556Srgrimesdtrace:::BEGIN
591556Srgrimes{
601556Srgrimes	printf("%3s %-16s %-16s %9s  -- %s\n", "C", "TIME(us)", "FILE",
611556Srgrimes	    "DELTA(us)", "FUNC");
621556Srgrimes}
631556Srgrimes
641556Srgrimespython*:::function-entry,
6538521Scracauerpython*:::function-return
6638530Scracauer/self->last == 0/
6738521Scracauer{
681556Srgrimes	self->last = timestamp;
691556Srgrimes}
70213811Sobrien
7120425Sstevepython*:::function-entry
721556Srgrimes{
731556Srgrimes	this->delta = (timestamp - self->last) / 1000;
741556Srgrimes	printf("%3d %-16d %-16s %9d %*s-> %s\n", cpu, timestamp / 1000,
751556Srgrimes	    basename(copyinstr(arg0)), this->delta, self->depth * 2, "",
76199660Sjilles	    copyinstr(arg1));
77199660Sjilles	self->depth++;
78199660Sjilles	self->last = timestamp;
791556Srgrimes}
801556Srgrimes
811556Srgrimespython*:::function-return
8290111Simp{
8317987Speter	this->delta = (timestamp - self->last) / 1000;
84199660Sjilles	self->depth -= self->depth > 0 ? 1 : 0;
851556Srgrimes	printf("%3d %-16d %-16s %9d %*s<- %s\n", cpu, timestamp / 1000,
861556Srgrimes	    basename(copyinstr(arg0)), this->delta, self->depth * 2, "",
871556Srgrimes	    copyinstr(arg1));
881556Srgrimes	self->last = timestamp;
891556Srgrimes}
901556Srgrimes