1#!/usr/bin/ksh
2#
3# diskhits - disk access by file offset.
4#            Written using DTrace (Solaris 10 3/05).
5#
6# $Id: diskhits 3 2007-08-01 10:50:08Z brendan $
7#
8# This prints how a file was accessed, the locations on a distribution plot.
9# This is for the cache misses only - the file activity that resulted in
10# disk events.
11#
12# USAGE:	diskhits pathname
13#	eg,
14#		diskhits /var/adm/messages
15#
16# FIELDS:
17#		Location (KB)	The file offset of the disk activity, Kbytes.
18#		Size (KB)	Size of the disk activity, Kbytes.
19#		Total RW	Total disk activity, reads + writes.
20#
21# BASED ON: /usr/demo/dtrace/applicat.d
22#
23# SEE ALSO: DTrace Guide "io Provider" chapter (docs.sun.com)
24#           iosnoop (DTraceToolkit)
25#
26# PORTIONS: Copyright (c) 2005, 2006 Brendan Gregg.
27#
28# CDDL HEADER START
29#
30#  The contents of this file are subject to the terms of the
31#  Common Development and Distribution License, Version 1.0 only
32#  (the "License").  You may not use this file except in compliance
33#  with the License.
34#
35#  You can obtain a copy of the license at Docs/cddl1.txt
36#  or http://www.opensolaris.org/os/licensing.
37#  See the License for the specific language governing permissions
38#  and limitations under the License.
39#
40# CDDL HEADER END
41#
42# 08-Jun-2005   Brendan Gregg   Created this.
43# 20-Apr-2006	   "      "	Last update.
44#
45
46### Usage
47function usage
48{
49	cat <<-END >&2
50	USAGE: diskhits pathname
51	   eg,
52	       diskhits /var/adm/wtmpx
53	END
54	exit 1
55}
56
57### Process arguments
58if (( $# != 1 )); then
59	usage
60fi
61if [[ $1 == "-h" ]]; then
62	usage
63fi
64pathname=$1
65if [[ ! -e $pathname ]]; then
66	print "ERROR2: file $pathname not found" >&2
67	exit 2
68fi
69
70### Calculate output scale
71report_lines=20
72set -- `ls -l $pathname`
73filesize=$5
74(( file_kb_max = filesize / 1024 ))
75(( scale_kb = filesize / (1024 * report_lines) ))
76if (( file_kb_max < 20 )); then file_kb_max=20; fi
77if (( scale_kb < 1 )); then scale_kb=1; fi
78
79#
80#  Run DTrace
81#
82/usr/sbin/dtrace -n '
83 #pragma D option quiet
84
85 inline string PATHNAME = "'$pathname'";
86 inline int FILE_KB_MAX = '$file_kb_max';
87 inline int SCALE_KB = '$scale_kb';
88
89 dtrace:::BEGIN
90 {
91	printf("Tracing... Hit Ctrl-C to end.\n");
92 }
93
94 io:::start
95 /args[2]->fi_pathname == PATHNAME/
96 {
97	this->kb = args[2]->fi_offset == -1 ? -1 : args[2]->fi_offset / 1024;
98	@Location = lquantize(this->kb, 0, FILE_KB_MAX, SCALE_KB);
99	@Size = quantize(args[0]->b_bcount/1024);
100	@Total = sum(args[0]->b_bcount/1024);
101 }
102
103 dtrace:::END
104 {
105	printf("Location (KB),");
106	printa(@Location);
107
108	printf("Size (KB),");
109	printa(@Size);
110
111	printa("Total RW: %@d KB\n", @Total);
112 }
113'
114