summaryrefslogtreecommitdiffstats
path: root/contrib/diskdevstat.stp
blob: db098cb551ba4bca800a77260fd5fc5f4e0caf17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/stap
#
# diskdevstat: A simple systemtap script to record harddisk activity of processes and
#              display statistics for read/write operations
# Copyright (C) 2008, 2009 Red Hat, Inc.
# Authors: Phil Knirsch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

global ifavg, iflast, ifmin, ifmax;

probe vfs.write
{
	if(pid() == 0 || devname == "N/A") {
		next;
	}
	ns = gettimeofday_ms();
	if(iflast[0, pid(), devname, execname(), uid()] == 0) {
		iflast[0, pid(), devname, execname(), uid()] = ns;
	} else {
		diff = ns - iflast[0, pid(), devname, execname(), uid()];
		iflast[0, pid(), devname, execname(), uid()] = ns;
		if(ifmin[0, pid(), devname, execname(), uid()] == 0 || diff < ifmin[0, pid(), devname, execname(), uid()])
			ifmin[0, pid(), devname, execname(), uid()] = diff;
		if(diff > ifmax[0, pid(), devname, execname(), uid()])
			ifmax[0, pid(), devname, execname(), uid()] = diff;
		ifavg[0, pid(), devname, execname(), uid()] <<< diff;
	}
}

probe vfs.read
{
	if(pid() == 0 || devname == "N/A") {
		next;
	}
	ns = gettimeofday_ms();
	if(iflast[1, pid(), devname, execname(), uid()] == 0) {
		iflast[1, pid(), devname, execname(), uid()] = ns;
	} else {
		diff = ns - iflast[1, pid(), devname, execname(), uid()];
		iflast[1, pid(), devname, execname(), uid()] = ns;
		if(ifmin[1, pid(), devname, execname(), uid()] == 0 || diff < ifmin[1, pid(), devname, execname(), uid()])
			ifmin[1, pid(), devname, execname(), uid()] = diff;
		if(diff > ifmax[1, pid(), devname, execname(), uid()])
			ifmax[1, pid(), devname, execname(), uid()] = diff;
		ifavg[1, pid(), devname, execname(), uid()] <<< diff;
	}
}


function print_activity()
{
        printf("%5s %5s %-7s %9s %9s %9s %9s   %9s %9s %9s %9s %-15s\n",
                "PID", "UID", "DEV", "WRITE_CNT", "WRITE_MIN", "WRITE_MAX", "WRITE_AVG", "READ_CNT", "READ_MIN", "READ_MAX", "READ_AVG", "COMMAND")

        foreach ([type, pid, dev, exec, uid] in ifavg-) {
                nxmit = @count(ifavg[0, pid, dev, exec, uid])
                nrecv = @count(ifavg[1, pid, dev, exec, uid])
                if(type == 0 || nxmit == 0) {
                        printf("%5d %5d %-7s %9d %5d.%03d %5d.%03d %5d.%03d   %9d %5d.%03d %5d.%03d %5d.%03d %-15s\n",
                                pid, uid, dev,
                                nxmit, ifmin[0, pid, dev, exec, uid]/1000, ifmin[0, pid, dev, exec, uid]%1000,
                                       ifmax[0, pid, dev, exec, uid]/1000, ifmax[0, pid, dev, exec, uid]%1000,
                                       nxmit ? @avg(ifavg[0, pid, dev, exec, uid])/1000 : 0, nxmit ? @avg(ifavg[0, pid, dev, exec, uid])%1000 : 0,
                                nrecv, ifmin[1, pid, dev, exec, uid]/1000, ifmin[1, pid, dev, exec, uid]%1000,
                                       ifmax[1, pid, dev, exec, uid]/1000, ifmax[1, pid, dev, exec, uid]%1000,
                                       nrecv ? @avg(ifavg[1, pid, dev, exec, uid])/1000 : 0, nrecv ? @avg(ifavg[1, pid, dev, exec, uid])%1000 : 0,
                                exec)
                }
        }

        print("\n")
}

probe timer.ms(5000), end, error
{
        print_activity()
}