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