aboutsummaryrefslogtreecommitdiff
path: root/hotkernel
blob: 84d3362ff0775cd3c261326415221dfb1c95cf0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl -w
#
# hotkernel - sample on-CPU kernel-level functions and modules.
#             Written using Perl and DTrace (Solaris 10 03/05)
#
# This samples the on-CPU function at 1001 Hertz, for a simple yet
# effective kernel-level profiling tool for sampling exclusive function time.
# The output will identify which function is on the CPU the most - which is
# the hottest. See Notes/ALLexclusive_notes.txt for an explanation of
# exclusive time.
#
# $Id: hotkernel 65 2007-10-04 11:09:40Z brendan $
#
# USAGE:        hotkernel [-hm]
#
#		-h              # help
#		-m              # match modules, not functions
#       eg,
#		hotkernel       # sample kernel functions
#		hotkernel -m    # sample kernel modules
#
# FIELDS:
#		FUNCTION        Function name
#		MODULE          Module name
#		COUNT           Number of samples
#		PCNT            Percentage of total samples
#
# COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
#
# CDDL HEADER START
#
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
#
# CDDL HEADER END
#
# Author: Brendan Gregg  [Sydney, Australia]
#
# 29-Jun-2006	Brendan Gregg	Created this.
# 29-Jun-2006	   "      "	Last update.
#

use strict;
use Getopt::Std;

#
# Command Line Arguments
#
my $args;
usage() if defined $ARGV[0] and $ARGV[0] eq "--help";
getopts('hm') or usage();
usage() if defined $main::opt_h and $main::opt_h;
my $mods = defined $main::opt_m and $main::opt_m ? 1 : 0;

#
# Cleanup on signals
#
$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;
}