1235368Sgnn#!/usr/bin/sh
2235368Sgnn#
3235368Sgnn# fddist - file descriptor usage distributions.
4235368Sgnn#          Written using DTrace (Solaris 10 3/05).
5235368Sgnn#
6235368Sgnn# This prints distributions for read and write events by file descriptor,
7235368Sgnn# by process. This can be used to determine which file descriptor a 
8235368Sgnn# process is doing the most I/O with.
9235368Sgnn#
10235368Sgnn# $Id: fddist 3 2007-08-01 10:50:08Z brendan $
11235368Sgnn#
12235368Sgnn# USAGE:        fddist [-r|-w]      # hit Ctrl-C to end sample
13235368Sgnn#
14235368Sgnn# FIELDS:
15235368Sgnn#               EXEC       process name
16235368Sgnn#               PID        process ID
17235368Sgnn#               value      file descriptor
18235368Sgnn#               count      number of events
19235368Sgnn#
20235368Sgnn# BASED ON: /usr/demo/dtrace/lquantize.d
21235368Sgnn#
22235368Sgnn# SEE ALSO:
23235368Sgnn#           DTrace Guide "Aggregations" chapter (docs.sun.com)
24235368Sgnn#
25235368Sgnn# PORTIONS: Copyright (c) 2005, 2006 Brendan Gregg.
26235368Sgnn#
27235368Sgnn# CDDL HEADER START
28235368Sgnn#
29235368Sgnn#  The contents of this file are subject to the terms of the
30235368Sgnn#  Common Development and Distribution License, Version 1.0 only
31235368Sgnn#  (the "License").  You may not use this file except in compliance
32235368Sgnn#  with the License.
33235368Sgnn#
34235368Sgnn#  You can obtain a copy of the license at Docs/cddl1.txt
35235368Sgnn#  or http://www.opensolaris.org/os/licensing.
36235368Sgnn#  See the License for the specific language governing permissions
37235368Sgnn#  and limitations under the License.
38235368Sgnn#
39235368Sgnn# CDDL HEADER END
40235368Sgnn#
41235368Sgnn# 09-Jun-2005   Brendan Gregg   Created this.
42235368Sgnn# 20-Apr-2006	   "      "	Last update.
43235368Sgnn
44235368Sgnn
45235368Sgnn##############################
46235368Sgnn# --- Process Arguments ---
47235368Sgnn#
48235368Sgnn
49235368Sgnn### Default variables
50235368Sgnnopt_read=0; opt_write=0
51235368Sgnn
52235368Sgnn### Process options
53235368Sgnnwhile getopts hrw name
54235368Sgnndo
55235368Sgnn	case $name in
56235368Sgnn	r)      opt_read=1 ;;
57235368Sgnn	w)      opt_write=1 ;;
58235368Sgnn	h|?)    cat <<-END >&2
59235368Sgnn		USAGE: fddist [-r|-w] 
60235368Sgnn		              -r        # reads only
61235368Sgnn		              -w        # writes only
62235368Sgnn		  eg,
63235368Sgnn		       fddist           # default, r+w counts
64235368Sgnn		       fddist -r        # read count only
65235368Sgnn		END
66235368Sgnn		exit 1
67235368Sgnn	esac
68235368Sgnndone
69235368Sgnnshift `expr $OPTIND - 1`
70235368Sgnn
71235368Sgnn### Option logic
72235368Sgnnif [ $opt_read -eq 0 -a $opt_write -eq 0 ]; then
73235368Sgnn	opt_read=1; opt_write=1
74235368Sgnnfi
75235368Sgnn
76235368Sgnn
77235368Sgnn#################################
78235368Sgnn# --- Main Program, DTrace ---
79235368Sgnn#
80235368Sgnn/usr/sbin/dtrace -n '
81235368Sgnn #pragma D option quiet
82235368Sgnn
83235368Sgnn inline int OPT_read   = '$opt_read';
84235368Sgnn inline int OPT_write  = '$opt_write';
85235368Sgnn inline int FDMAX      = 255;
86235368Sgnn
87235368Sgnn /* print header */
88235368Sgnn dtrace:::BEGIN
89235368Sgnn {
90235368Sgnn	printf("Tracing ");
91235368Sgnn	OPT_read && OPT_write ? printf("reads and writes") : 1;
92235368Sgnn	OPT_read && ! OPT_write ? printf("reads") : 1;
93235368Sgnn	! OPT_read && OPT_write ? printf("writes") : 1;
94235368Sgnn	printf("... Hit Ctrl-C to end.\n");
95235368Sgnn }
96235368Sgnn
97235368Sgnn /* sample reads */
98235368Sgnn syscall::*read*:entry
99235368Sgnn /OPT_read/
100235368Sgnn {
101235368Sgnn	@Count[execname, pid] = lquantize(arg0, 0, FDMAX, 1);
102235368Sgnn }
103235368Sgnn
104235368Sgnn /* sample writes */
105235368Sgnn syscall::*write*:entry
106235368Sgnn /OPT_write/
107235368Sgnn {
108235368Sgnn	@Count[execname, pid] = lquantize(arg0, 0, FDMAX, 1);
109235368Sgnn }
110235368Sgnn
111235368Sgnn /* print report */
112235368Sgnn dtrace:::END
113235368Sgnn {
114235368Sgnn	printa("EXEC: %-16s PID: %d\n%@d\n",@Count);
115235368Sgnn }
116235368Sgnn'
117