1N/A#!/usr/bin/perl -w
1N/A#
1N/A# hotkernel - sample on-CPU kernel-level functions and modules.
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 kernel-level profiling tool for sampling exclusive function time.
1N/A# The output will identify which function is on the CPU the most - which is
1N/A# the hottest. See Notes/ALLexclusive_notes.txt for an explanation of
1N/A# exclusive time.
1N/A#
1N/A# $Id: hotkernel 65 2007-10-04 11:09:40Z brendan $
1N/A#
1N/A# USAGE: hotkernel [-hm]
1N/A#
1N/A# -h # help
1N/A# -m # match modules, not functions
1N/A# eg,
1N/A# hotkernel # sample kernel functions
1N/A# hotkernel -m # sample kernel modules
1N/A#
1N/A# FIELDS:
1N/A# FUNCTION Function name
1N/A# MODULE Module 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('hm') or usage();
1N/Ausage() if defined $main::opt_h and $main::opt_h;
1N/Amy $mods = defined $main::opt_m and $main::opt_m ? 1 : 0;
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 /arg0/
1N/A {
1N/A \@pc[arg0] = count();
1N/A }
1N/A dtrace:::END
1N/A {
1N/A printa("%a %\@d\\n", \@pc);
1N/A }
1N/A'
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 my ($addr, $count) = split ' ', $line;
1N/A my ($name, $offset) = split /\+/, $addr;
1N/A next if $name eq "0x0";
1N/A $name =~ s/\`.*// if $mods;
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", $mods ? "MODULE" : "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: hotkernel [-hm]\n";
1N/A print STDERR " eg,\n";
1N/A print STDERR " hotkernel # sample kernel functions\n";
1N/A print STDERR " hotkernel -m # sample kernel modules\n";
1N/A exit 1;
1N/A}