1N/A#!/usr/bin/ksh
1N/A#
1N/A# rwtop - display top read/write bytes by process.
1N/A# Written using DTrace (Solaris 10 3/05).
1N/A#
1N/A# This is measuring reads and writes at the application level. This matches
1N/A# read and write system calls.
1N/A#
1N/A# $Id: rwtop 3 2007-08-01 10:50:08Z brendan $
1N/A#
1N/A# USAGE: rwtop [-cC] [-j|-Z] [-n name] [-p pid]
1N/A# [-t top] [interval [count]]
1N/A#
1N/A# rwtop # default output, 5 second samples
1N/A#
1N/A# -C # don't clear the screen
1N/A# -c # print counts
1N/A# -j # print project ID
1N/A# -Z # print zone ID
1N/A# -n name # this process name only
1N/A# -p PID # this PID only
1N/A# -t top # print top number only
1N/A# eg,
1N/A# rwtop 1 # 1 second samples
1N/A# rwtop -t 10 # print top 10 only
1N/A# rwtop -n bash # monitor processes named "bash"
1N/A# rwtop -C 5 12 # print 12 x 5 second samples
1N/A#
1N/A# FIELDS:
1N/A# ZONE Zone ID
1N/A# PROJ Project ID
1N/A# UID User ID
1N/A# PID Process ID
1N/A# PPID Parent Process ID
1N/A# CMD Process name
1N/A# D Direction, Read or Write
1N/A# BYTES Total bytes during sample
1N/A# app_r total reads during sample, Kbytes
1N/A# app_w total writes during sample, Kbytes
1N/A#
1N/A# SEE ALSO: iotop
1N/A#
1N/A# INSPIRATION: top(1) by William LeFebvre
1N/A#
1N/A# COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
1N/A#
1N/A# CDDL HEADER START
1N/A#
1N/A# The contents of this file are subject to the terms of the
1N/A# Common Development and Distribution License, Version 1.0 only
1N/A# (the "License"). You may not use this file except in compliance
1N/A# with the License.
1N/A#
1N/A# You can obtain a copy of the license at Docs/cddl1.txt
1N/A# or http://www.opensolaris.org/os/licensing.
1N/A# See the License for the specific language governing permissions
1N/A# and limitations under the License.
1N/A#
1N/A# CDDL HEADER END
1N/A#
1N/A# Author: Brendan Gregg [Sydney, Australia]
1N/A#
1N/A# 24-Jul-2005 Brendan Gregg Created this.
1N/A# 20-Apr-2006 " " Last update.
1N/A
1N/A
1N/A##############################
1N/A# --- Process Arguments ---
1N/A#
1N/A
1N/A### default variables
1N/Aopt_name=0; opt_pid=0; opt_clear=1; opt_proj=0; opt_zone=0
1N/Aopt_def=1; opt_bytes=1; filter=0; pname=.; pid=0
1N/Aopt_top=0; opt_count=0; interval=5; count=-1; top=0
1N/A
1N/A### process options
1N/Awhile getopts Cchn:p:jt:Z name
1N/Ado
1N/A case $name in
1N/A C) opt_clear=0 ;;
1N/A c) opt_count=1; opt_bytes=0 ;;
1N/A n) opt_name=1; pname=$OPTARG ;;
1N/A p) opt_pid=1; pid=$OPTARG ;;
1N/A j) opt_proj=1; opt_def=0 ;;
1N/A t) opt_top=1; top=$OPTARG ;;
1N/A Z) opt_zone=1; opt_def=0 ;;
1N/A h|?) cat <<-END >&2
1N/A USAGE: rwtop [-cC] [-j|-Z] [-n name] [-p pid]
1N/A [-t top] [interval [count]]
1N/A
1N/A -C # don't clear the screen
1N/A -c # print counts
1N/A -j # print project ID
1N/A -Z # print zone ID
1N/A -n name # this process name only
1N/A -p PID # this PID only
1N/A -t top # print top number only
1N/A eg,
1N/A rwtop # default output, 5 second samples
1N/A rwtop 1 # 1 second samples
1N/A rwtop -t 10 # print top 10 only
1N/A rwtop -n bash # monitor processes named "bash"
1N/A rwtop -C 5 12 # print 12 x 5 second samples
1N/A END
1N/A exit 1
1N/A esac
1N/Adone
1N/A
1N/Ashift $(( $OPTIND - 1 ))
1N/A
1N/A### option logic
1N/Aif [[ "$1" > 0 ]]; then
1N/A interval=$1; shift
1N/Afi
1N/Aif [[ "$1" > 0 ]]; then
1N/A count=$1; shift
1N/Afi
1N/Aif (( opt_proj && opt_zone )); then
1N/A opt_proj=0
1N/Afi
1N/Aif (( opt_name || opt_pid )); then
1N/A filter=1
1N/Afi
1N/Aif (( opt_clear )); then
1N/A clearstr=`clear`
1N/Aelse
1N/A clearstr=.
1N/Afi
1N/A
1N/A
1N/A
1N/A#################################
1N/A# --- Main Program, DTrace ---
1N/A#
1N/A/usr/sbin/dtrace -n '
1N/A /*
1N/A * Command line arguments
1N/A */
1N/A inline int OPT_def = '$opt_def';
1N/A inline int OPT_proj = '$opt_proj';
1N/A inline int OPT_zone = '$opt_zone';
1N/A inline int OPT_clear = '$opt_clear';
1N/A inline int OPT_bytes = '$opt_bytes';
1N/A inline int OPT_count = '$opt_count';
1N/A inline int OPT_name = '$opt_name';
1N/A inline int OPT_pid = '$opt_pid';
1N/A inline int OPT_top = '$opt_top';
1N/A inline int INTERVAL = '$interval';
1N/A inline int COUNTER = '$count';
1N/A inline int FILTER = '$filter';
1N/A inline int TOP = '$top';
1N/A inline int PID = '$pid';
1N/A inline string NAME = "'$pname'";
1N/A inline string CLEAR = "'$clearstr'";
1N/A
1N/A #pragma D option quiet
1N/A
1N/A /*
1N/A * Print header
1N/A */
1N/A dtrace:::BEGIN
1N/A {
1N/A /* starting values */
1N/A counts = COUNTER;
1N/A secs = INTERVAL;
1N/A app_r = 0;
1N/A app_w = 0;
1N/A
1N/A printf("Tracing... Please wait.\n");
1N/A }
1N/A
1N/A /*
1N/A * Check event is being traced
1N/A */
1N/A sysinfo:::readch,
1N/A sysinfo:::writech
1N/A /pid != $pid/
1N/A {
1N/A /* default is to trace unless filtering, */
1N/A this->ok = FILTER ? 0 : 1;
1N/A
1N/A /* check each filter, */
1N/A (OPT_name == 1 && NAME == execname)? this->ok = 1 : 1;
1N/A (OPT_pid == 1 && PID == pid) ? this->ok = 1 : 1;
1N/A }
1N/A
1N/A /*
1N/A * Increment tallys
1N/A */
1N/A sysinfo:::readch
1N/A /this->ok/
1N/A {
1N/A app_r += arg0;
1N/A }
1N/A sysinfo:::writech
1N/A /this->ok/
1N/A {
1N/A app_w += arg0;
1N/A }
1N/A
1N/A /*
1N/A * Process event
1N/A */
1N/A sysinfo:::readch,
1N/A sysinfo:::writech
1N/A /this->ok/
1N/A {
1N/A /* choose statistic to track */
1N/A this->value = OPT_bytes ? arg0 : 1;
1N/A
1N/A /*
1N/A * Save details
1N/A */
1N/A OPT_def ? @out[uid, pid, ppid, execname,
1N/A probename == "readch" ? "R" : "W"] = sum(this->value) : 1;
1N/A OPT_proj ? @out[curpsinfo->pr_projid, pid, ppid, execname,
1N/A probename == "readch" ? "R" : "W"] = sum(this->value) : 1;
1N/A OPT_zone ? @out[curpsinfo->pr_zoneid, pid, ppid, execname,
1N/A probename == "readch" ? "R" : "W"] = sum(this->value) : 1;
1N/A
1N/A this->ok = 0;
1N/A }
1N/A
1N/A /*
1N/A * Timer
1N/A */
1N/A profile:::tick-1sec
1N/A {
1N/A secs--;
1N/A }
1N/A
1N/A /*
1N/A * Print Report
1N/A */
1N/A profile:::tick-1sec
1N/A /secs == 0/
1N/A {
1N/A /* fetch 1 min load average */
1N/A this->load1a = `hp_avenrun[0] / 65536;
1N/A this->load1b = ((`hp_avenrun[0] % 65536) * 100) / 65536;
1N/A
1N/A /* convert counters to Kbytes */
1N/A app_r /= 1024;
1N/A app_w /= 1024;
1N/A
1N/A /* print status */
1N/A OPT_clear ? printf("%s", CLEAR) : 1;
1N/A printf("%Y, load: %d.%02d, app_r: %6d KB, app_w: %6d KB\n\n",
1N/A walltimestamp, this->load1a, this->load1b, app_r, app_w);
1N/A
1N/A /* print headers */
1N/A OPT_def ? printf(" UID ") : 1;
1N/A OPT_proj ? printf(" PROJ ") : 1;
1N/A OPT_zone ? printf(" ZONE ") : 1;
1N/A printf("%6s %6s %-16s %1s",
1N/A "PID", "PPID", "CMD", "D");
1N/A OPT_bytes ? printf(" %16s\n", "BYTES") : 1;
1N/A OPT_count ? printf(" %16s\n", "COUNT") : 1;
1N/A
1N/A /* truncate to top lines if needed */
1N/A OPT_top ? trunc(@out, TOP) : 1;
1N/A
1N/A /* print data */
1N/A printa("%5d %6d %6d %-16s %1s %16@d\n", @out);
1N/A printf("\n");
1N/A
1N/A /* clear data */
1N/A trunc(@out);
1N/A app_r = 0;
1N/A app_w = 0;
1N/A secs = INTERVAL;
1N/A counts--;
1N/A }
1N/A
1N/A /*
1N/A * End of program
1N/A */
1N/A profile:::tick-1sec
1N/A /counts == 0/
1N/A {
1N/A exit(0);
1N/A }
1N/A
1N/A /*
1N/A * Cleanup for Ctrl-C
1N/A */
1N/A dtrace:::END
1N/A {
1N/A trunc(@out);
1N/A }
1N/A'
1N/A