5 # This prints I/O statistics for each file descriptor within a process.
6 # In particular, the time break down during read() and write() events is
9 # $Id: pfilestat 4 2007-08-01 11:01:38Z brendan $
11 # USAGE: pfilestat [-r|-w] pid
14 # STATE microstate: running, sleeping, waitcpu, read, write
15 # FDUM File Descriptor ID
16 # Time Percentage of wallclock time in each STATE
17 # File Name of file, if known
19 # COPYRIGHT: Copyright (c) 2006 Richard McDougall.
23 # The contents of this file are subject to the terms of the
24 # Common Development and Distribution License, Version 1.0 only
25 # (the "License"). You may not use this file except in compliance
28 # You can obtain a copy of the license at Docs/cddl1.txt
29 # or http://www.opensolaris.org/os/licensing.
30 # See the License for the specific language governing permissions
31 # and limitations under the License.
36 # Trace readv() and writev().
38 # 20-Feb-2006 Richard McDougall created this.
39 # 24-Feb-2006 Brendan Gregg tweaked code.
40 # 20-Mar-2006 " " tweaked code.
41 # 20-Mar-2006 " " last update.
44 ##############################
45 # --- Process Arguments ---
49 opt_read=0; opt_write=0
52 while getopts hrw name
58 USAGE: pfilestat [-r|-w] pid
62 pfilestat pid # default, r+w counts
63 pfilestat -r pid # read count only
68 shift `expr $OPTIND - 1`
75 echo "Must supply pid"
80 if [ $opt_read -eq 0 -a $opt_write -eq 0 ]; then
81 opt_read=1; opt_write=1
85 #################################
86 # --- Main Program, DTrace ---
89 #pragma D option quiet
91 inline string CLEAR = "'$clearstr'";
92 inline int OPT_read = '$opt_read';
93 inline int OPT_write = '$opt_write';
94 inline int PID = '$PID';
96 unsigned long long totaltime;
97 unsigned long long totalbytes;
109 OPT_read && OPT_write ? printf("reads and writes") : 1;
110 OPT_read && ! OPT_write ? printf("reads") : 1;
111 ! OPT_read && OPT_write ? printf("writes") : 1;
121 syscall::pread*:entry
122 /pid == PID && OPT_read/
125 @logical["running", (uint64_t)0, ""] = sum(timestamp - last);
126 totaltime += timestamp - last;
136 self->path = args[0]->v_path == 0 ? "<none>" :
137 cleanpath(args[0]->v_path);
140 syscall::read:return,
141 syscall::pread*:return
142 /pid == PID && OPT_read/
145 this->bytes = (int)arg0 > 0 ? (int)arg0 : 0;
146 @logical["read", self->fd - 1, self->path] = sum(timestamp - last);
147 @bytes["read", self->fd - 1, self->path] = sum(this->bytes);
148 totalbytes += this->bytes;
149 totaltime += timestamp - last;
157 syscall::write:entry,
158 syscall::pwrite*:entry
159 /pid == PID && OPT_write/
162 @logical["running", (uint64_t)0, ""] = sum(timestamp - last);
163 totaltime += timestamp - last;
166 self->fd = (int)arg0 + 1;
169 syscall::write:return,
170 syscall::pwrite*:return
171 /pid == PID && OPT_write/
174 this->bytes = (int)arg0 > 0 ? (int)arg0 : 0;
175 @logical["write", self->fd - 1, self->path] = sum(timestamp - last);
176 @bytes["write", self->fd - 1, self->path] = sum(this->bytes);
177 totalbytes += this->bytes;
178 totaltime += timestamp - last;
187 @logical["waitcpu", (uint64_t)0, ""] = sum(timestamp - last);
188 totaltime += timestamp - last;
196 @logical["running", (uint64_t)0, ""] = sum(timestamp - last);
197 totaltime += timestamp - last;
204 @logical["running", (uint64_t)0, ""] = sum(timestamp - last);
205 totaltime += timestamp - last;
210 /args[1]->pr_pid == PID && runstate == OTHER/
212 @logical["sleep", (uint64_t)0, ""] = sum(timestamp - last);
213 totaltime += timestamp - last;
218 /args[1]->pr_pid == PID && runstate == READ/
220 @logical["sleep-r", (uint64_t)0, ""] = sum(timestamp - last);
221 totaltime += timestamp - last;
226 /args[1]->pr_pid == PID && runstate == WRITE/
228 @logical["sleep-w", (uint64_t)0, ""] = sum(timestamp - last);
229 totaltime += timestamp - last;
234 /args[1]->pr_pid == PID/
236 @logical["waitcpu", (uint64_t)0, ""] = sum(timestamp - last);
237 totaltime += timestamp - last;
242 /args[1]->pr_pid == PID/
244 @logical["waitcpu", (uint64_t)0, ""] = sum(timestamp - last);
245 totaltime += timestamp - last;
253 normalize(@logical, totaltime / 100);
255 printf("%10s %7s %9s %-44s\n", "STATE", "FDNUM", "Time", "Filename");
256 printa("%10s %7d %@8d%% %-44.44s\n", @logical);
259 delta = timestamp - stamp;
261 normalize(@bytes, (1024 * delta) / 1000000000);
263 printf("\n%10s %7s %9s %-44s\n", "STATE", "FDNUM", "KB/s",
265 printa("%10s %7d %@9d %-44.44s\n", @bytes);
268 printf("\nTotal event time (ms): %d Total Mbytes/sec: %d\n",
270 (totalbytes * 1000000000) / (delta * 1048576));