Home | History | Annotate | Line # | Download | only in Bin
      1  1.1  christos #!/usr/bin/sh
      2  1.1  christos #
      3  1.1  christos # fddist - file descriptor usage distributions.
      4  1.1  christos #          Written using DTrace (Solaris 10 3/05).
      5  1.1  christos #
      6  1.1  christos # This prints distributions for read and write events by file descriptor,
      7  1.1  christos # by process. This can be used to determine which file descriptor a 
      8  1.1  christos # process is doing the most I/O with.
      9  1.1  christos #
     10  1.1  christos # $Id: fddist,v 1.1.1.1 2015/09/30 22:01:07 christos Exp $
     11  1.1  christos #
     12  1.1  christos # USAGE:        fddist [-r|-w]      # hit Ctrl-C to end sample
     13  1.1  christos #
     14  1.1  christos # FIELDS:
     15  1.1  christos #               EXEC       process name
     16  1.1  christos #               PID        process ID
     17  1.1  christos #               value      file descriptor
     18  1.1  christos #               count      number of events
     19  1.1  christos #
     20  1.1  christos # BASED ON: /usr/demo/dtrace/lquantize.d
     21  1.1  christos #
     22  1.1  christos # SEE ALSO:
     23  1.1  christos #           DTrace Guide "Aggregations" chapter (docs.sun.com)
     24  1.1  christos #
     25  1.1  christos # PORTIONS: Copyright (c) 2005, 2006 Brendan Gregg.
     26  1.1  christos #
     27  1.1  christos # CDDL HEADER START
     28  1.1  christos #
     29  1.1  christos #  The contents of this file are subject to the terms of the
     30  1.1  christos #  Common Development and Distribution License, Version 1.0 only
     31  1.1  christos #  (the "License").  You may not use this file except in compliance
     32  1.1  christos #  with the License.
     33  1.1  christos #
     34  1.1  christos #  You can obtain a copy of the license at Docs/cddl1.txt
     35  1.1  christos #  or http://www.opensolaris.org/os/licensing.
     36  1.1  christos #  See the License for the specific language governing permissions
     37  1.1  christos #  and limitations under the License.
     38  1.1  christos #
     39  1.1  christos # CDDL HEADER END
     40  1.1  christos #
     41  1.1  christos # 09-Jun-2005   Brendan Gregg   Created this.
     42  1.1  christos # 20-Apr-2006	   "      "	Last update.
     43  1.1  christos 
     44  1.1  christos 
     45  1.1  christos ##############################
     46  1.1  christos # --- Process Arguments ---
     47  1.1  christos #
     48  1.1  christos 
     49  1.1  christos ### Default variables
     50  1.1  christos opt_read=0; opt_write=0
     51  1.1  christos 
     52  1.1  christos ### Process options
     53  1.1  christos while getopts hrw name
     54  1.1  christos do
     55  1.1  christos 	case $name in
     56  1.1  christos 	r)      opt_read=1 ;;
     57  1.1  christos 	w)      opt_write=1 ;;
     58  1.1  christos 	h|?)    cat <<-END >&2
     59  1.1  christos 		USAGE: fddist [-r|-w] 
     60  1.1  christos 		              -r        # reads only
     61  1.1  christos 		              -w        # writes only
     62  1.1  christos 		  eg,
     63  1.1  christos 		       fddist           # default, r+w counts
     64  1.1  christos 		       fddist -r        # read count only
     65  1.1  christos 		END
     66  1.1  christos 		exit 1
     67  1.1  christos 	esac
     68  1.1  christos done
     69  1.1  christos shift `expr $OPTIND - 1`
     70  1.1  christos 
     71  1.1  christos ### Option logic
     72  1.1  christos if [ $opt_read -eq 0 -a $opt_write -eq 0 ]; then
     73  1.1  christos 	opt_read=1; opt_write=1
     74  1.1  christos fi
     75  1.1  christos 
     76  1.1  christos 
     77  1.1  christos #################################
     78  1.1  christos # --- Main Program, DTrace ---
     79  1.1  christos #
     80  1.1  christos /usr/sbin/dtrace -n '
     81  1.1  christos  #pragma D option quiet
     82  1.1  christos 
     83  1.1  christos  inline int OPT_read   = '$opt_read';
     84  1.1  christos  inline int OPT_write  = '$opt_write';
     85  1.1  christos  inline int FDMAX      = 255;
     86  1.1  christos 
     87  1.1  christos  /* print header */
     88  1.1  christos  dtrace:::BEGIN
     89  1.1  christos  {
     90  1.1  christos 	printf("Tracing ");
     91  1.1  christos 	OPT_read && OPT_write ? printf("reads and writes") : 1;
     92  1.1  christos 	OPT_read && ! OPT_write ? printf("reads") : 1;
     93  1.1  christos 	! OPT_read && OPT_write ? printf("writes") : 1;
     94  1.1  christos 	printf("... Hit Ctrl-C to end.\n");
     95  1.1  christos  }
     96  1.1  christos 
     97  1.1  christos  /* sample reads */
     98  1.1  christos  syscall::*read*:entry
     99  1.1  christos  /OPT_read/
    100  1.1  christos  {
    101  1.1  christos 	@Count[execname, pid] = lquantize(arg0, 0, FDMAX, 1);
    102  1.1  christos  }
    103  1.1  christos 
    104  1.1  christos  /* sample writes */
    105  1.1  christos  syscall::*write*:entry
    106  1.1  christos  /OPT_write/
    107  1.1  christos  {
    108  1.1  christos 	@Count[execname, pid] = lquantize(arg0, 0, FDMAX, 1);
    109  1.1  christos  }
    110  1.1  christos 
    111  1.1  christos  /* print report */
    112  1.1  christos  dtrace:::END
    113  1.1  christos  {
    114  1.1  christos 	printa("EXEC: %-16s PID: %d\n%@d\n",@Count);
    115  1.1  christos  }
    116  1.1  christos '
    117