1N/A#!/usr/bin/sh
1N/A#
1N/A# procsystime - print process system call time details.
1N/A# Written using DTrace (Solaris 10 3/05).
1N/A#
1N/A# $Id: procsystime 4 2007-08-01 11:01:38Z brendan $
1N/A#
1N/A# USAGE: procsystime [-acehoT] [ -p PID | -n name | command ]
1N/A#
1N/A# -p PID # examine this PID
1N/A# -n name # examine this process name
1N/A# -a # print all details
1N/A# -c # print syscall counts
1N/A# -e # print elapsed times
1N/A# -o # print CPU times
1N/A# -T # print totals
1N/A# eg,
1N/A# procsystime -p 1871 # examine PID 1871
1N/A# procsystime -n tar # examine processes called "tar"
1N/A# procsystime -aTn bash # print all details for bash shells
1N/A# procsystime df -h # run and examine "df -h"
1N/A#
1N/A# The elapsed times are interesting, to help identify syscalls that take
1N/A# some time to complete (during which the process may have slept). CPU time
1N/A# helps us identify syscalls that are consuming CPU cycles to run.
1N/A#
1N/A# FIELDS:
1N/A# SYSCALL System call name
1N/A# TIME (ns) Total time, nanoseconds
1N/A# COUNT Number of occurrences
1N/A#
1N/A# COPYRIGHT: Copyright (c) 2005 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# 27-Apr-2005 Brendan Gregg Created this.
1N/A# 08-Jun-2005 " " Added command option.
1N/A# 22-Sep-2005 " " Allowed systemwide tracing.
1N/A# 22-Sep-2005 " " Last update.
1N/A#
1N/A
1N/A
1N/A##############################
1N/A# --- Process Arguments ---
1N/A#
1N/A
1N/A### Default variables
1N/Aopt_filter=0; opt_pid=0; opt_name=0; pid=0; pname=".";
1N/Aopt_elapsed=0; opt_cpu=0; opt_counts=0; opt_totals=0
1N/Aopt_command=0; command="";
1N/A
1N/A### Process options
1N/Awhile getopts acehn:op:T name
1N/Ado
1N/A case $name in
1N/A p) opt_filter=1; opt_pid=1; pid=$OPTARG ;;
1N/A n) opt_filter=1; opt_name=1; pname=$OPTARG ;;
1N/A a) opt_totals=1; opt_elapsed=1; opt_cpu=1; opt_counts=1 ;;
1N/A e) opt_elapsed=1 ;;
1N/A c) opt_counts=1 ;;
1N/A o) opt_cpu=1 ;;
1N/A T) opt_totals=1 ;;
1N/A h|?) cat <<-END >&2
1N/A USAGE: procsystime [-aceho] [ -p PID | -n name | command ]
1N/A -p PID # examine this PID
1N/A -n name # examine this process name
1N/A -a # print all details
1N/A -e # print elapsed times
1N/A -c # print syscall counts
1N/A -o # print CPU times
1N/A -T # print totals
1N/A eg,
1N/A procsystime -p 1871 # examine PID 1871
1N/A procsystime -n tar # examine processes called "tar"
1N/A procsystime -aTn bash # print all details for bash
1N/A procsystime df -h # run and examine "df -h"
1N/A END
1N/A exit 1
1N/A esac
1N/Adone
1N/Ashift `expr $OPTIND - 1`
1N/A
1N/A### Option logic
1N/Aif [ $opt_pid -eq 0 -a $opt_name -eq 0 -a "$*" != "" ]; then
1N/A opt_filter=1
1N/A opt_command=1
1N/A command="$*"
1N/Afi
1N/Aif [ $opt_elapsed -eq 0 -a $opt_cpu -eq 0 -a $opt_counts -eq 0 ]; then
1N/A opt_elapsed=1;
1N/Afi
1N/A
1N/A
1N/A#################################
1N/A# --- Main Program, DTrace ---
1N/A#
1N/Adtrace='
1N/A #pragma D option quiet
1N/A
1N/A /*
1N/A * Command line arguments
1N/A */
1N/A inline int OPT_elapsed = '$opt_elapsed';
1N/A inline int OPT_cpu = '$opt_cpu';
1N/A inline int OPT_counts = '$opt_counts';
1N/A inline int OPT_filter = '$opt_filter';
1N/A inline int OPT_pid = '$opt_pid';
1N/A inline int OPT_name = '$opt_name';
1N/A inline int OPT_totals = '$opt_totals';
1N/A inline int OPT_command = '$opt_command';
1N/A inline int PID = '$pid';
1N/A inline string NAME = "'$pname'";
1N/A inline string COMMAND = "'$command'";
1N/A
1N/A dtrace:::BEGIN
1N/A {
1N/A self->start = 0;
1N/A self->vstart = 0;
1N/A }
1N/A dtrace:::BEGIN
1N/A /! OPT_command/
1N/A {
1N/A printf("Tracing... Hit Ctrl-C to end...\n");
1N/A }
1N/A
1N/A /*
1N/A * Set start timestamp and counts
1N/A */
1N/A syscall:::entry
1N/A /(! OPT_filter) ||
1N/A (OPT_pid && pid == PID) ||
1N/A (OPT_name && execname == NAME) ||
1N/A (OPT_command && pid == $target)/
1N/A {
1N/A self->ok = 1;
1N/A }
1N/A syscall:::entry
1N/A /self->ok/
1N/A {
1N/A OPT_counts ? @Counts[probefunc] = count() : 1;
1N/A (OPT_counts && OPT_totals) ? @Counts["TOTAL:"] = count() : 1;
1N/A OPT_elapsed ? self->start = timestamp : 1;
1N/A OPT_cpu ? self->vstart = vtimestamp : 1;
1N/A self->ok = 0;
1N/A }
1N/A
1N/A /*
1N/A * Calculate time deltas
1N/A */
1N/A syscall:::return
1N/A /self->start/
1N/A {
1N/A this->elapsed = timestamp - self->start;
1N/A @Elapsed[probefunc] = sum(this->elapsed);
1N/A OPT_totals ? @Elapsed["TOTAL:"] = sum(this->elapsed) : 1;
1N/A self->start = 0;
1N/A }
1N/A syscall:::return
1N/A /self->vstart/
1N/A {
1N/A this->cpu = vtimestamp - self->vstart;
1N/A @CPU[probefunc] = sum(this->cpu);
1N/A OPT_totals ? @CPU["TOTAL:"] = sum(this->cpu) : 1;
1N/A self->vstart = 0;
1N/A }
1N/A
1N/A /*
1N/A * Elapsed time report
1N/A */
1N/A dtrace:::END
1N/A /OPT_elapsed/
1N/A {
1N/A printf("\nElapsed Times for ");
1N/A OPT_pid ? printf("PID %d,\n\n",PID) : 1;
1N/A OPT_name ? printf("processes %s,\n\n",NAME) : 1;
1N/A OPT_command ? printf("command %s,\n\n",COMMAND) : 1;
1N/A (! OPT_filter) ? printf("all processes,\n\n") : 1;
1N/A printf("%16s %18s\n","SYSCALL","TIME (ns)");
1N/A printa("%16s %@18d\n",@Elapsed);
1N/A }
1N/A
1N/A /*
1N/A * CPU time report
1N/A */
1N/A dtrace:::END
1N/A /OPT_cpu/
1N/A {
1N/A printf("\nCPU Times for ");
1N/A OPT_pid ? printf("PID %d,\n\n",PID) : 1;
1N/A OPT_name ? printf("processes %s,\n\n",NAME) : 1;
1N/A OPT_command ? printf("command %s,\n\n",COMMAND) : 1;
1N/A (! OPT_filter) ? printf("all processes,\n\n") : 1;
1N/A printf("%16s %18s\n","SYSCALL","TIME (ns)");
1N/A printa("%16s %@18d\n",@CPU);
1N/A }
1N/A
1N/A /*
1N/A * Syscall count report
1N/A */
1N/A dtrace:::END
1N/A /OPT_counts/
1N/A {
1N/A printf("\nSyscall Counts for ");
1N/A OPT_pid ? printf("PID %d,\n\n",PID) : 1;
1N/A OPT_name ? printf("processes %s,\n\n",NAME) : 1;
1N/A OPT_command ? printf("command %s,\n\n",COMMAND) : 1;
1N/A (! OPT_filter) ? printf("all processes,\n\n") : 1;
1N/A printf("%16s %18s\n","SYSCALL","COUNT");
1N/A OPT_counts ? printa("%16s %@18d\n",@Counts) : 1;
1N/A }
1N/A'
1N/A
1N/A### Run DTrace
1N/Aif [ $opt_command -eq 1 ]; then
1N/A /usr/sbin/dtrace -n "$dtrace" -x evaltime=exec -c "$command" >&2
1N/Aelse
1N/A /usr/sbin/dtrace -n "$dtrace" >&2
1N/Afi
1N/A