1235368Sgnn#!/usr/bin/sh
2#
3# topsysproc - display top syscalls by process name.
4#              Written using DTrace (Solaris 10 3/05).
5#
6# This program continually prints a report of the number of system calls
7# by process name, and refreshes the display every 1 second or as specified
8# at the command line. Similar data can be fetched with "prstat -m".
9#
10# $Id: topsysproc 19 2007-09-12 07:47:59Z brendan $
11#
12# USAGE:        topsysproc [interval]
13#
14# FIELDS:
15#		load avg   load averages, see uptime(1)
16#		syscalls   total number of syscalls in this interval
17#		PROCESS    process name
18#		COUNT      number of occurances in this interval
19#
20# NOTE: There may be several PIDs with the same process name.
21#
22# SEE ALSO:     prstat(1M)
23#
24# INSPIRATION:  top(1) by William LeFebvre
25#
26# COPYRIGHT: Copyright (c) 2005 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# 13-Jun-2005	Brendan Gregg	Created this.
43# 20-Apr-2006	   "      "	Last update.
44#
45
46#
47#  Check options
48#
49if [ "$1" = "-h" -o "$1" = "--help" ]; then
50	cat <<-END
51	USAGE: topsysproc [interval]
52	   eg,
53	       topsysproc            # default, 1 second updates
54	       topsysproc 5          # 5 second updates
55	END
56	exit 1
57fi
58interval=1
59if [ "$1" -gt 0 ]; then
60	interval=$1
61fi
62
63#
64#  Run DTrace
65#
66/usr/sbin/dtrace -n '
67 #pragma D option quiet
68 #pragma D option destructive
69
70 /* constants */
71 inline int INTERVAL = '$interval';
72 inline int SCREEN   = 20;
73
74 /* variables */
75 dtrace:::BEGIN
76 {
77	secs = 0;
78	printf("Tracing... Please wait.\n");
79 }
80
81 /* record syscall event */
82 syscall:::entry
83 {
84	@Name[execname] = count();
85	@Total = count();
86 }
87
88 /* update screen */
89 profile:::tick-1sec
90 /++secs >= INTERVAL/
91 {
92        /* fetch load averages */
93        this->load1a  = `hp_avenrun[0] / 65536;
94        this->load5a  = `hp_avenrun[1] / 65536;
95        this->load15a = `hp_avenrun[2] / 65536;
96        this->load1b  = ((`hp_avenrun[0] % 65536) * 100) / 65536;
97        this->load5b  = ((`hp_avenrun[1] % 65536) * 100) / 65536;
98        this->load15b = ((`hp_avenrun[2] % 65536) * 100) / 65536;
99
100	/* clear screen */
101	system("clear");
102
103        /* print load average */
104        printf("%Y, load average: %d.%02d, %d.%02d, %d.%02d",
105            walltimestamp, this->load1a, this->load1b, this->load5a,
106            this->load5b, this->load15a, this->load15b);
107
108	/* print syscall count */
109	printa("   syscalls: %@d\n",@Total);
110
111	/* print report */
112	trunc(@Name, SCREEN);
113	printf("\n   %-25s %12s\n", "PROCESS", "COUNT");
114	printa("   %-25s %@12d\n", @Name);
115
116	/* reset variables */
117	trunc(@Name);
118	clear(@Total);
119	secs = 0;
120 }
121'
122