1#!/bin/sh
2##!/usr/bin/ksh
3#
4# diskhits - disk access by file offset.
5#            Written using DTrace (Solaris 10 3/05).
6#
7# 20-Apr-2006, ver 0.72
8#
9# This prints how a file was accessed, the locations on a distribution plot.
10# This is for the cache misses only - the file activity that resulted in
11# disk events.
12#
13# USAGE:	diskhits pathname
14#	eg,
15#		diskhits /var/adm/messages
16#
17# FIELDS:
18#		Location (KB)	The file offset of the disk activity, Kbytes.
19#		Size (KB)	Size of the disk activity, Kbytes.
20#		Total RW	Total disk activity, reads + writes.
21#
22# BASED ON: /usr/demo/dtrace/applicat.d
23#
24# SEE ALSO: DTrace Guide "io Provider" chapter (docs.sun.com)
25#           iosnoop (DTraceToolkit)
26#
27# PORTIONS: Copyright (c) 2005, 2006 Brendan Gregg.
28#
29# CDDL HEADER START
30#
31#  The contents of this file are subject to the terms of the
32#  Common Development and Distribution License, Version 1.0 only
33#  (the "License").  You may not use this file except in compliance
34#  with the License.
35#
36#  You can obtain a copy of the license at Docs/cddl1.txt
37#  or http://www.opensolaris.org/os/licensing.
38#  See the License for the specific language governing permissions
39#  and limitations under the License.
40#
41# CDDL HEADER END
42#
43# 08-Jun-2005   Brendan Gregg   Created this.
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	echo "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