hotuser revision 1
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# hotuser - sample on-CPU user-level functions and libraries.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Written using Perl and DTrace (Solaris 10 03/05)
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# This samples the on-CPU function at 1001 Hertz, for a simple yet
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# effective user-level profiling tool for sampling exclusive function time.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# The output will identify which function is on the CPU the most - which
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# is the hottest. See Notes/ALLexclusive_notes.txt for an explanation of
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# exclusive time.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# $Id: hotuser 65 2007-10-04 11:09:40Z brendan $
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# USAGE: hotuser [-hl] { -c command | -p PID }
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# -l # match libraries, not functions
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# -p PID # examine this PID
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# -c command # run and examine this command
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# hotuser -p 81 # sample user functions from PID 81
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# hotuser -lp 81 # sample user libraries from PID 81
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# hotuser -p `pgrep -n Xorg` # sample Xorg
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# FUNCTION Function name
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# LIBRARY Library name
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# COUNT Number of samples
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# PCNT Percentage of total samples
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# CDDL HEADER START
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# The contents of this file are subject to the terms of the
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Common Development and Distribution License, Version 1.0 only
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# (the "License"). You may not use this file except in compliance
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# with the License.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# You can obtain a copy of the license at Docs/cddl1.txt
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# See the License for the specific language governing permissions
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# and limitations under the License.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# CDDL HEADER END
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Author: Brendan Gregg [Sydney, Australia]
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# 29-Jun-2006 Brendan Gregg Created this.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# 29-Jun-2006 " " Last update.
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Command Line Arguments
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncusage() if defined $ARGV[0] and $ARGV[0] eq "--help";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncmy $libs = defined $main::opt_l and $main::opt_l ? 1 : 0;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Cleanup on signals
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Declare DTrace script
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync/usr/sbin/dtrace -n '
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync #pragma D option quiet
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync profile:::profile-1001hz
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync /pid == \$target/
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync \@pc[arg1] = count();
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync dtrace:::END
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync printa("OUT: %A %\@d\\n", \@pc);
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Run DTrace, process output
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncopen DTRACE, "$dtrace |" or die "ERROR1: Can't run dtrace (perms?): $!\n";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncprint "Sampling... Hit Ctrl-C to end.\n";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync next if $line =~ /^\s*$/;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync next if $line !~ /^OUT: /;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync $name =~ s/\`.*// if $libs;
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Print final report
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncprintf "\n%-52s %8s %6s\n", $libs ? "LIBRARY" : "FUNCTION", "COUNT", "PCNT";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsyncforeach my $name (sort { $Count{$a} <=> $Count{$b} } keys %Count) {
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync printf "%-52s %8d %5.1f%%\n", $name, $Count{$name},
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync# Subroutines
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync print STDERR "USAGE: hotuser [-hl] { -c command | -p PID }\n";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync print STDERR " hotuser -p 81 # sample user funcs for PID 81\n";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync print STDERR " hotuser -lp 81 # sample user libs for PID 81\n";
b9a21c3c91c47e090316e28d759194e46628ed49vboxsync print STDERR " hotuser -p `pgrep -n Xorg` # sample Xorg\n";