1#!/usr/bin/sh
2#
3# dnlcstat - DNLC statistics.
4#            Written in DTrace (Solaris 10 3/05).
5#
6# The DNLC is the Directory Name Lookup Cache. Filename lookups often
7# return a hit from here, before needing to traverse the regular file
8# system cache or go to disk.
9#
10# 14-Jun-2005, ver 0.70
11#
12# USAGE:	dnlcstat [interval [count]]
13#
14# FIELDS:
15#
16#		%hit	hit percentage for this sample
17#		hit	number of DNLC hits in this sample
18#		miss	number of DNLC misses in this sample
19#
20# SEE ALSO: 	CacheKit, http://www.brendangregg.com/cachekit.html
21#		(contains a dnlcstat written in Perl, which uses less CPU)
22#
23# COPYRIGHT: Copyright (c) 2005 Brendan Gregg.
24#
25# CDDL HEADER START
26#
27#  The contents of this file are subject to the terms of the
28#  Common Development and Distribution License, Version 1.0 only
29#  (the "License").  You may not use this file except in compliance
30#  with the License.
31#
32#  You can obtain a copy of the license at Docs/cddl1.txt
33#  or http://www.opensolaris.org/os/licensing.
34#  See the License for the specific language governing permissions
35#  and limitations under the License.
36#
37# CDDL HEADER END
38#
39# 27-Mar-2004	Brendan Gregg	Created this.
40# 14-Jun-2005	   "      "  	Updated style.
41#
42
43##############################
44# --- Process Arguments ---
45#
46
47### default values
48interval=1; count=-1
49
50### check arguments
51if [ "$1" = "-h" -o "$1" = "--help" ]; then
52	cat <<-END >&2
53	USAGE: dnlcstat [interval [count]]
54	       dnlcstat          # 1 second samples, infinite
55	  eg,
56	       dnlcstat 1        # print every 1 second
57	       dnlcstat 5 6      # print every 5 seconds, 6 times
58	END
59        exit 1
60fi
61
62### argument logic
63if [ "$1" -gt 0 ]; then
64        interval=$1; count=-1; shift
65fi
66if [ "$1" -gt 0 ]; then
67        count=$1; shift
68fi
69if [ $interval -eq 0 ]; then
70        interval=1
71fi
72
73
74#################################
75# --- Main Program, DTrace ---
76#
77/usr/sbin/dtrace -n '
78 #pragma D option quiet
79
80 /*
81  * Command line arguments
82  */
83 inline int INTERVAL   = '$interval';
84 inline int COUNTER    = '$count';
85 inline int SCREEN = 21;
86
87 int hits;			/* hits */
88 int misses;			/* misses */
89
90 /*
91  * Initialise variables
92  */
93 dtrace:::BEGIN
94 {
95	lines = SCREEN + 1;
96	counts = COUNTER;
97	secs = INTERVAL;
98	first = 1;
99 }
100
101 /*
102  * Print header
103  */
104 dtrace:::BEGIN,
105 tick-1sec
106 /first || (secs == 0 && lines > SCREEN)/
107 { 
108	printf("%10s %8s %8s\n","dnlc  %hit","hit","miss");
109	lines = 0;
110	first = 0;
111 }
112
113 /*
114  * Probe DNLC lookups
115  */
116 fbt:genunix:dnlc_lookup:return
117 {
118	hits   += arg1 == 0 ? 0 : 1;
119	misses += arg1 == 0 ? 1 : 0;
120 }
121
122 profile:::tick-1sec
123 {
124        secs--;
125 }
126
127
128 /*
129  * Print output line
130  */
131 profile:::tick-1sec
132 /secs == 0/
133 {
134	/* calculate hit percent */
135	this->divide = misses + hits == 0 ? 1 : misses + hits;
136	ratio = hits * 100 / this->divide;
137
138	/* print output */
139	printf("%10d %8d %8d\n",ratio,hits,misses);
140
141	/* clear counters */
142	hits = 0;
143	misses = 0;
144
145        /* process counts */
146        secs = INTERVAL;
147        counts--;
148        lines++;
149
150 }
151
152 /*
153  * End
154  */
155 profile:::tick-1sec
156 /counts == 0/
157 {
158        exit(0);
159 }
160'
161
162