xref: /titanic_41/usr/src/cmd/stat/arcstat/arcstat.pl (revision 653d2d91a355b2a5df7db09e17c965ddc5d3ea47)
1#!/usr/perl5/bin/perl -w
2# The above invocation line was changed in 0.5 to allow for
3# interoperability with linux.
4#
5# Print out ZFS ARC Statistics exported via kstat(1)
6# For a definition of fields, or usage, use arctstat.pl -v
7#
8# This script is a fork of the original arcstat.pl (0.1) by
9# Neelakanth Nadgir, originally published on his Sun blog on
10# 09/18/2007
11#     http://blogs.sun.com/realneel/entry/zfs_arc_statistics
12#
13# This version aims to improve upon the original by adding features
14# and fixing bugs as needed.  This version is maintained by
15# Mike Harsch and is hosted in a public open source repository:
16#    http://github.com/mharsch/arcstat
17#
18# Comments, Questions, or Suggestions are always welcome.
19# Contact the maintainer at ( mike at harschsystems dot com )
20#
21# CDDL HEADER START
22#
23# The contents of this file are subject to the terms of the
24# Common Development and Distribution License, Version 1.0 only
25# (the "License").  You may not use this file except in compliance
26# with the License.
27#
28# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
29# or http://www.opensolaris.org/os/licensing.
30# See the License for the specific language governing permissions
31# and limitations under the License.
32#
33# When distributing Covered Code, include this CDDL HEADER in each
34# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
35# If applicable, add the following below this CDDL HEADER, with the
36# fields enclosed by brackets "[]" replaced with your own identifying
37# information: Portions Copyright [yyyy] [name of copyright owner]
38#
39# CDDL HEADER END
40#
41#
42# Fields have a fixed width. Every interval, we fill the "v"
43# hash with its corresponding value (v[field]=value) using calculate().
44# @hdr is the array of fields that needs to be printed, so we
45# just iterate over this array and print the values using our pretty printer.
46
47use strict;
48use warnings;
49use POSIX qw(strftime);
50use Sun::Solaris::Kstat;
51use Getopt::Long;
52use IO::Handle;
53
54my %cols = (# HDR => [Size, Scale, Description]
55	"time"		=>[8, -1, "Time"],
56	"hits"		=>[4, 1000, "ARC reads per second"],
57	"miss"		=>[4, 1000, "ARC misses per second"],
58	"read"		=>[4, 1000, "Total ARC accesses per second"],
59	"hit%"		=>[4, 100, "ARC Hit percentage"],
60	"miss%"		=>[5, 100, "ARC miss percentage"],
61	"dhit"		=>[4, 1000, "Demand Data hits per second"],
62	"dmis"		=>[4, 1000, "Demand Data misses per second"],
63	"dh%"		=>[3, 100, "Demand Data hit percentage"],
64	"dm%"		=>[3, 100, "Demand Data miss percentage"],
65	"phit"		=>[4, 1000, "Prefetch hits per second"],
66	"pmis"		=>[4, 1000, "Prefetch misses per second"],
67	"ph%"		=>[3, 100, "Prefetch hits percentage"],
68	"pm%"		=>[3, 100, "Prefetch miss percentage"],
69	"mhit"		=>[4, 1000, "Metadata hits per second"],
70	"mmis"		=>[4, 1000, "Metadata misses per second"],
71	"mread"		=>[4, 1000, "Metadata accesses per second"],
72	"mh%"		=>[3, 100, "Metadata hit percentage"],
73	"mm%"		=>[3, 100, "Metadata miss percentage"],
74	"arcsz"		=>[5, 1024, "ARC Size"],
75	"c" 		=>[4, 1024, "ARC Target Size"],
76	"mfu" 		=>[4, 1000, "MFU List hits per second"],
77	"mru" 		=>[4, 1000, "MRU List hits per second"],
78	"mfug" 		=>[4, 1000, "MFU Ghost List hits per second"],
79	"mrug" 		=>[4, 1000, "MRU Ghost List hits per second"],
80	"eskip"		=>[5, 1000, "evict_skip per second"],
81	"mtxmis"	=>[6, 1000, "mutex_miss per second"],
82	"rmis"		=>[4, 1000, "recycle_miss per second"],
83	"dread"		=>[5, 1000, "Demand data accesses per second"],
84	"pread"		=>[5, 1000, "Prefetch accesses per second"],
85	"l2hits"	=>[6, 1000, "L2ARC hits per second"],
86	"l2miss"	=>[6, 1000, "L2ARC misses per second"],
87	"l2read"	=>[6, 1000, "Total L2ARC accesses per second"],
88	"l2hit%"	=>[6, 100, "L2ARC access hit percentage"],
89	"l2miss%"	=>[7, 100, "L2ARC access miss percentage"],
90	"l2asize"       =>[7, 1024, "Actual (compressed) size of the L2ARC"],
91	"l2size"	=>[6, 1024, "Size of the L2ARC"],
92	"l2bytes"	=>[7, 1024, "bytes read per second from the L2ARC"],
93);
94my %v=();
95my @hdr = qw(time read miss miss% dmis dm% pmis pm% mmis mm% arcsz c);
96my @xhdr = qw(time mfu mru mfug mrug eskip mtxmis rmis dread pread read);
97my $int = 1;		# Default interval is 1 second
98my $count = 1;		# Default count is 1
99my $hdr_intr = 20;	# Print header every 20 lines of output
100my $opfile = "";
101my $sep = "  ";		# Default separator is 2 spaces
102my $raw_output;
103my $version = "0.5";
104my $l2exist = 0;
105my $cmd = "Usage: arcstat [-hvxr] [-f fields] [-o file] [-s string] " .
106    "[interval [count]]\n";
107my %cur;
108my %d;
109my $out;
110my $kstat = Sun::Solaris::Kstat->new();
111STDOUT->autoflush;
112
113sub detailed_usage {
114	print STDERR "$cmd\n";
115	print STDERR "Field definitions are as follows:\n";
116	foreach my $hdr (keys %cols) {
117		print STDERR sprintf("%11s : %s\n", $hdr, $cols{$hdr}[2]);
118	}
119	exit(1);
120}
121
122sub usage {
123	print STDERR "$cmd\n";
124	print STDERR "\t -h : Print this help message\n";
125	print STDERR "\t -v : List all possible field headers " .
126	    "and definitions\n";
127	print STDERR "\t -x : Print extended stats\n";
128	print STDERR "\t -r : Raw output mode (values not scaled)\n";
129	print STDERR "\t -f : Specify specific fields to print (see -v)\n";
130	print STDERR "\t -o : Redirect output to the specified file\n";
131	print STDERR "\t -s : Override default field separator with custom " .
132	    "character or string\n";
133	print STDERR "\nExamples:\n";
134	print STDERR "\tarcstat -o /tmp/a.log 2 10\n";
135	print STDERR "\tarcstat -s \",\" -o /tmp/a.log 2 10\n";
136	print STDERR "\tarcstat -v\n";
137	print STDERR "\tarcstat -f time,hit%,dh%,ph%,mh% 1\n";
138	exit(1);
139}
140
141sub init {
142	my $desired_cols;
143	my $xflag = '';
144	my $hflag = '';
145	my $vflag;
146	my $res = GetOptions('x' => \$xflag,
147	    'o=s' => \$opfile,
148	    'help|h|?' => \$hflag,
149	    'v' => \$vflag,
150	    's=s' => \$sep,
151	    'f=s' => \$desired_cols,
152	    'r' => \$raw_output);
153
154	if (defined $ARGV[0] && defined $ARGV[1]) {
155		$int = $ARGV[0];
156		$count = $ARGV[1];
157	} elsif (defined $ARGV[0]) {
158		$int = $ARGV[0];
159		$count = 0;
160	}
161
162	usage() if !$res or $hflag or ($xflag and $desired_cols);
163	detailed_usage() if $vflag;
164	@hdr = @xhdr if $xflag;		#reset headers to xhdr
165
166	# check if L2ARC exists
167	snap_stats();
168	if (defined $cur{"l2_size"}) {
169		$l2exist = 1;
170	}
171
172	if ($desired_cols) {
173		@hdr = split(/[ ,]+/, $desired_cols);
174		# Now check if they are valid fields
175		my @invalid = ();
176		my @incompat = ();
177		foreach my $ele (@hdr) {
178			if (not exists($cols{$ele})) {
179				push(@invalid, $ele);
180			} elsif (($l2exist == 0) && ($ele =~ /^l2/)) {
181				printf("No L2ARC here\n", $ele);
182				push(@incompat, $ele);
183			}
184		}
185		if (scalar @invalid > 0) {
186			print STDERR "Invalid column definition! -- "
187			    . "@invalid\n\n";
188			usage();
189		}
190
191		if (scalar @incompat > 0) {
192			print STDERR "Incompatible field specified -- "
193			    . "@incompat\n\n";
194			usage();
195		}
196	}
197
198	if ($opfile) {
199		open($out, ">$opfile") ||die "Cannot open $opfile for writing";
200		$out->autoflush;
201		select $out;
202	}
203}
204
205# Capture kstat statistics. We maintain 3 hashes, prev, cur, and
206# d (delta). As their names imply they maintain the previous, current,
207# and delta (cur - prev) statistics.
208sub snap_stats {
209	my %prev = %cur;
210	if ($kstat->update()) {
211		printf("<State Changed>\n");
212	}
213	my $hashref_cur = $kstat->{"zfs"}{0}{"arcstats"};
214	%cur = %$hashref_cur;
215	foreach my $key (keys %cur) {
216		next if $key =~ /class/;
217		if (defined $prev{$key}) {
218			$d{$key} = $cur{$key} - $prev{$key};
219		} else {
220			$d{$key} = $cur{$key};
221		}
222	}
223}
224
225# Pretty print num. Arguments are width, scale, and num
226sub prettynum {
227	my @suffix = (' ', 'K', 'M', 'G', 'T');
228	my $num = $_[2] || 0;
229	my $scale = $_[1];
230	my $sz = $_[0];
231	my $index = 0;
232	my $save = 0;
233
234	if ($scale == -1) {			#special case for date field
235		return sprintf("%s", $num);
236	} elsif (($num > 0) && ($num < 1)) {	#rounding error.  return 0
237		$num = 0;
238	}
239
240	while ($num > $scale and $index < 5) {
241		$save = $num;
242		$num = $num/$scale;
243		$index++;
244	}
245
246	return sprintf("%*d", $sz, $num) if ($index == 0);
247	if (($save / $scale) < 10) {
248		return sprintf("%*.1f%s", $sz - 1, $num,$suffix[$index]);
249	} else {
250		return sprintf("%*d%s", $sz - 1, $num,$suffix[$index]);
251	}
252}
253
254sub print_values {
255	foreach my $col (@hdr) {
256		if (not $raw_output) {
257			printf("%s%s", prettynum($cols{$col}[0], $cols{$col}[1],
258			    $v{$col}), $sep);
259		} else {
260			printf("%d%s", $v{$col} || 0, $sep);
261		}
262	}
263	printf("\n");
264}
265
266sub print_header {
267	if (not $raw_output) {
268		foreach my $col (@hdr) {
269			printf("%*s%s", $cols{$col}[0], $col, $sep);
270		}
271	} else {
272		# Don't try to align headers in raw mode
273		foreach my $col (@hdr) {
274			printf("%s%s", $col, $sep);
275		}
276	}
277	printf("\n");
278}
279
280sub calculate {
281	%v = ();
282
283	if ($raw_output) {
284		$v{"time"} = strftime("%s", localtime);
285	} else {
286		$v{"time"} = strftime("%H:%M:%S", localtime);
287	}
288
289	$v{"hits"} = $d{"hits"}/$int;
290	$v{"miss"} = $d{"misses"}/$int;
291	$v{"read"} = $v{"hits"} + $v{"miss"};
292	$v{"hit%"} = 100 * ($v{"hits"} / $v{"read"}) if $v{"read"} > 0;
293	$v{"miss%"} = 100 - $v{"hit%"} if $v{"read"} > 0;
294
295	$v{"dhit"} = ($d{"demand_data_hits"} +
296	    $d{"demand_metadata_hits"})/$int;
297	$v{"dmis"} = ($d{"demand_data_misses"} +
298	    $d{"demand_metadata_misses"})/$int;
299
300	$v{"dread"} = $v{"dhit"} + $v{"dmis"};
301	$v{"dh%"} = 100 * ($v{"dhit"} / $v{"dread"}) if $v{"dread"} > 0;
302	$v{"dm%"} = 100 - $v{"dh%"} if $v{"dread"} > 0;
303
304	$v{"phit"} = ($d{"prefetch_data_hits"} +
305	    $d{"prefetch_metadata_hits"})/$int;
306	$v{"pmis"} = ($d{"prefetch_data_misses"} +
307	    $d{"prefetch_metadata_misses"})/$int;
308
309	$v{"pread"} = $v{"phit"} + $v{"pmis"};
310	$v{"ph%"} = 100 * ($v{"phit"} / $v{"pread"}) if $v{"pread"} > 0;
311	$v{"pm%"} = 100 - $v{"ph%"} if $v{"pread"} > 0;
312
313	$v{"mhit"} = ($d{"prefetch_metadata_hits"} +
314		$d{"demand_metadata_hits"})/$int;
315	$v{"mmis"} = ($d{"prefetch_metadata_misses"} +
316	    $d{"demand_metadata_misses"})/$int;
317
318	$v{"mread"} = $v{"mhit"} + $v{"mmis"};
319	$v{"mh%"} = 100 * ($v{"mhit"} / $v{"mread"}) if $v{"mread"} > 0;
320	$v{"mm%"} = 100 - $v{"mh%"} if $v{"mread"} > 0;
321
322	$v{"arcsz"} = $cur{"size"};
323	$v{"c"} = $cur{"c"};
324	$v{"mfu"} = $d{"mfu_hits"}/$int;
325	$v{"mru"} = $d{"mru_hits"}/$int;
326	$v{"mrug"} = $d{"mru_ghost_hits"}/$int;
327	$v{"mfug"} = $d{"mfu_ghost_hits"}/$int;
328	$v{"eskip"} = $d{"evict_skip"}/$int;
329	$v{"rmiss"} = $d{"recycle_miss"}/$int;
330	$v{"mtxmis"} = $d{"mutex_miss"}/$int;
331
332	if ($l2exist) {
333		$v{"l2hits"} = $d{"l2_hits"}/$int;
334		$v{"l2miss"} = $d{"l2_misses"}/$int;
335		$v{"l2read"} = $v{"l2hits"} + $v{"l2miss"};
336		$v{"l2hit%"} = 100 * ($v{"l2hits"} / $v{"l2read"})
337		    if $v{"l2read"} > 0;
338
339		$v{"l2miss%"} = 100 - $v{"l2hit%"} if $v{"l2read"} > 0;
340		$v{"l2size"} = $cur{"l2_size"};
341		$v{"l2asize"} = $cur{"l2_asize"};
342		$v{"l2bytes"} = $d{"l2_read_bytes"}/$int;
343	}
344}
345
346sub main {
347	my $i = 0;
348	my $count_flag = 0;
349
350	init();
351	if ($count > 0) { $count_flag = 1; }
352	while (1) {
353		print_header() if ($i == 0);
354		snap_stats();
355		calculate();
356		print_values();
357		last if ($count_flag == 1 && $count-- <= 1);
358		$i = (($i == $hdr_intr) && (not $raw_output)) ? 0 : $i+1;
359		sleep($int);
360	}
361	close($out) if defined $out;
362}
363
364&main;
365