3 # hotkernel - sample on-CPU kernel-level functions and modules.
4 # Written using Perl and DTrace (Solaris 10 03/05)
6 # This samples the on-CPU function at 1001 Hertz, for a simple yet
7 # effective kernel-level profiling tool for sampling exclusive function time.
8 # The output will identify which function is on the CPU the most - which is
9 # the hottest. See Notes/ALLexclusive_notes.txt for an explanation of
12 # $Id: hotkernel 65 2007-10-04 11:09:40Z brendan $
14 # USAGE: hotkernel [-hm]
17 # -m # match modules, not functions
19 # hotkernel # sample kernel functions
20 # hotkernel -m # sample kernel modules
23 # FUNCTION Function name
25 # COUNT Number of samples
26 # PCNT Percentage of total samples
28 # COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
32 # The contents of this file are subject to the terms of the
33 # Common Development and Distribution License, Version 1.0 only
34 # (the "License"). You may not use this file except in compliance
37 # You can obtain a copy of the license at Docs/cddl1.txt
38 # or http://www.opensolaris.org/os/licensing.
39 # See the License for the specific language governing permissions
40 # and limitations under the License.
44 # Author: Brendan Gregg [Sydney, Australia]
46 # 29-Jun-2006 Brendan Gregg Created this.
47 # 29-Jun-2006 " " Last update.
54 # Command Line Arguments
57 usage() if defined $ARGV[0] and $ARGV[0] eq "--help";
58 getopts('hm') or usage();
59 usage() if defined $main::opt_h and $main::opt_h;
60 my $mods = defined $main::opt_m and $main::opt_m ? 1 : 0;
65 $SIG{INT} = \&cleanupsig; # Ctrl-C
66 $SIG{QUIT} = \&cleanupsig; # Ctrl-\
67 $SIG{TERM} = \&cleanupsig; # TERM
70 # Declare DTrace script
74 #pragma D option quiet
75 profile:::profile-1001hz
82 printa("%a %\@d\\n", \@pc);
88 # Run DTrace, process output
92 open DTRACE, "$dtrace |" or die "ERROR1: Can't run dtrace (perms?): $!\n";
93 print "Sampling... Hit Ctrl-C to end.\n";
94 while (my $line = <DTRACE>) {
95 next if $line =~ /^\s*$/;
96 my ($addr, $count) = split ' ', $line;
97 my ($name, $offset) = split /\+/, $addr;
98 next if $name eq "0x0";
99 $name =~ s/\`.*// if $mods;
100 $Count{$name} += $count;
108 printf "\n%-52s %8s %6s\n", $mods ? "MODULE" : "FUNCTION", "COUNT", "PCNT";
109 foreach my $name (sort { $Count{$a} <=> $Count{$b} } keys %Count) {
110 printf "%-52s %8d %5.1f%%\n", $name, $Count{$name},
111 100 * $Count{$name} / ($total ? $total : 1);
120 print STDERR "USAGE: hotkernel [-hm]\n";
121 print STDERR " eg,\n";
122 print STDERR " hotkernel # sample kernel functions\n";
123 print STDERR " hotkernel -m # sample kernel modules\n";