1N/A#!/usr/bin/perl -w
1N/A#
1N/A# hotuser - sample on-CPU user-level functions and libraries.
1N/A# Written using Perl and DTrace (Solaris 10 03/05)
1N/A#
1N/A# This samples the on-CPU function at 1001 Hertz, for a simple yet
1N/A# effective user-level profiling tool for sampling exclusive function time.
1N/A# The output will identify which function is on the CPU the most - which
1N/A# is the hottest. See Notes/ALLexclusive_notes.txt for an explanation of
1N/A# exclusive time.
1N/A#
1N/A# $Id: hotuser 65 2007-10-04 11:09:40Z brendan $
1N/A#
1N/A# USAGE: hotuser [-hl] { -c command | -p PID }
1N/A#
1N/A# -h # help
1N/A# -l # match libraries, not functions
1N/A# -p PID # examine this PID
1N/A# -c command # run and examine this command
1N/A# eg,
1N/A# hotuser -p 81 # sample user functions from PID 81
1N/A# hotuser -lp 81 # sample user libraries from PID 81
1N/A# hotuser -p `pgrep -n Xorg` # sample Xorg
1N/A#
1N/A# FIELDS:
1N/A# FUNCTION Function name
1N/A# LIBRARY Library name
1N/A# COUNT Number of samples
1N/A# PCNT Percentage of total samples
1N/A#
1N/A# COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License, Version 1.0 only
1N/A# (the "License"). You may not use this file except in compliance
1N/A# with the License.
1N/A#
1N/A# You can obtain a copy of the license at Docs/cddl1.txt
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A# Author: Brendan Gregg [Sydney, Australia]
1N/A#
1N/A# 29-Jun-2006 Brendan Gregg Created this.
1N/A# 29-Jun-2006 " " Last update.
1N/A#
1N/A
1N/Ause strict;
1N/Ause Getopt::Std;
1N/A
1N/A#
1N/A# Command Line Arguments
1N/A#
1N/Amy $args;
1N/Ausage() if defined $ARGV[0] and $ARGV[0] eq "--help";
1N/Agetopts('c:hlp:') or usage();
1N/Ausage() if defined $main::opt_h and $main::opt_h;
1N/Amy $libs = defined $main::opt_l and $main::opt_l ? 1 : 0;
1N/Aif (defined $main::opt_c) {
1N/A $args = "-c $main::opt_c";
1N/A}
1N/Aelsif (defined $main::opt_p) {
1N/A $args = "-p $main::opt_p";
1N/A}
1N/Aelse {
1N/A usage();
1N/A}
1N/A
1N/A#
1N/A# Cleanup on signals
1N/A#
1N/A$SIG{INT} = \&cleanupsig; # Ctrl-C
1N/A$SIG{QUIT} = \&cleanupsig; # Ctrl-\
1N/A$SIG{TERM} = \&cleanupsig; # TERM
1N/A
1N/A#
1N/A# Declare DTrace script
1N/A#
1N/Amy $dtrace = <<END;
1N/A/usr/sbin/dtrace -n '
1N/A #pragma D option quiet
1N/A profile:::profile-1001hz
1N/A /pid == \$target/
1N/A {
1N/A \@pc[arg1] = count();
1N/A }
1N/A dtrace:::END
1N/A {
1N/A printa("OUT: %A %\@d\\n", \@pc);
1N/A }
1N/A' '$args'
1N/AEND
1N/A
1N/A#
1N/A# Run DTrace, process output
1N/A#
1N/Amy %Count;
1N/Amy $total;
1N/Aopen DTRACE, "$dtrace |" or die "ERROR1: Can't run dtrace (perms?): $!\n";
1N/Aprint "Sampling... Hit Ctrl-C to end.\n";
1N/Awhile (my $line = <DTRACE>) {
1N/A next if $line =~ /^\s*$/;
1N/A next if $line !~ /^OUT: /;
1N/A my ($tag, $addr, $count) = split ' ', $line;
1N/A my ($name, $offset) = split /\+/, $addr;
1N/A next if $name eq "0x0";
1N/A $name =~ s/\`.*// if $libs;
1N/A $Count{$name} += $count;
1N/A $total += $count;
1N/A}
1N/Aclose DTRACE;
1N/A
1N/A#
1N/A# Print final report
1N/A#
1N/Aprintf "\n%-52s %8s %6s\n", $libs ? "LIBRARY" : "FUNCTION", "COUNT", "PCNT";
1N/Aforeach my $name (sort { $Count{$a} <=> $Count{$b} } keys %Count) {
1N/A printf "%-52s %8d %5.1f%%\n", $name, $Count{$name},
1N/A 100 * $Count{$name} / ($total ? $total : 1);
1N/A}
1N/A
1N/A#
1N/A# Subroutines
1N/A#
1N/Asub cleanupsig {
1N/A}
1N/Asub usage {
1N/A print STDERR "USAGE: hotuser [-hl] { -c command | -p PID }\n";
1N/A print STDERR " eg,\n";
1N/A print STDERR " hotuser -p 81 # sample user funcs for PID 81\n";
1N/A print STDERR " hotuser -lp 81 # sample user libs for PID 81\n";
1N/A print STDERR " hotuser -p `pgrep -n Xorg` # sample Xorg\n";
1N/A exit 1;
1N/A}