1#!/bin/sh
2# #!/usr/bin/sh
3#
4# fddist - file descriptor usage distributions.
5#          Written using DTrace (Solaris 10 3/05).
6#
7# This prints distributions for read and write events by file descriptor,
8# by process. This can be used to determine which file descriptor a 
9# process is doing the most I/O with.
10#
11# 20-Apr-2006, ver 0.76
12#
13# USAGE:        fddist [-r|-w]      # hit Ctrl-C to end sample
14#
15# FIELDS:
16#               EXEC       process name
17#               PID        process ID
18#               value      file descriptor
19#               count      number of events
20#
21# BASED ON: /usr/demo/dtrace/lquantize.d
22#
23# SEE ALSO:
24#           DTrace Guide "Aggregations" chapter (docs.sun.com)
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# 09-Jun-2005   Brendan Gregg   Created this.
43
44
45##############################
46# --- Process Arguments ---
47#
48
49### Default variables
50opt_read=0; opt_write=0
51
52### Process options
53while getopts hrw name
54do
55	case $name in
56	r)      opt_read=1 ;;
57	w)      opt_write=1 ;;
58	h|?)    cat <<-END >&2
59		USAGE: fddist [-r|-w] 
60		              -r        # reads only
61		              -w        # writes only
62		  eg,
63		       fddist           # default, r+w counts
64		       fddist -r        # read count only
65		END
66		exit 1
67	esac
68done
69shift `expr $OPTIND - 1`
70
71### Option logic
72if [ $opt_read -eq 0 -a $opt_write -eq 0 ]; then
73	opt_read=1; opt_write=1
74fi
75
76
77#################################
78# --- Main Program, DTrace ---
79#
80/usr/sbin/dtrace -n '
81 #pragma D option quiet
82
83 inline int OPT_read   = '$opt_read';
84 inline int OPT_write  = '$opt_write';
85 inline int FDMAX      = 255;
86
87 /* print header */
88 dtrace:::BEGIN
89 {
90	printf("Tracing ");
91	OPT_read && OPT_write ? printf("reads and writes") : 1;
92	OPT_read && ! OPT_write ? printf("reads") : 1;
93	! OPT_read && OPT_write ? printf("writes") : 1;
94	printf("... Hit Ctrl-C to end.\n");
95 }
96
97 /* sample reads */
98 syscall::*read*:entry
99 /OPT_read/
100 {
101	@Count[execname, pid] = lquantize(arg0, 0, FDMAX, 1);
102 }
103
104 /* sample writes */
105 syscall::*write*:entry
106 /OPT_write/
107 {
108	@Count[execname, pid] = lquantize(arg0, 0, FDMAX, 1);
109 }
110
111 /* print report */
112 dtrace:::END
113 {
114	printa("EXEC: %-16s PID: %d\n%@d\n",@Count);
115 }
116'
117