| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
# General event counter / monitor. Gives a report of event counts/rates on a |
| 4 |
# per-process or systemwide basis at script shutdown. |
| 5 |
# |
| 6 |
# Invoke with a list of probe point patterns as script command-line arguments: |
| 7 |
# stap eventcount.stp 'syscall.*' ... |
| 8 |
# |
| 9 |
# Optionally, specify process/thread-id to limit analysis: |
| 10 |
# stap eventcount.stp -c "CMD ARGS" [...probe points...] |
| 11 |
# or stap eventcount.stp -x PID [...probe points...] |
| 12 |
# or stap eventcount.stp -G comm=EXECNAME [...probe points...] |
| 13 |
# |
| 14 |
# Optionally, override periodic screen update, every NNN seconds (default 10) |
| 15 |
# stap eventcount.stp -G period=NNN [...probe points...] |
| 16 |
# ... or cumulative totals |
| 17 |
# stap eventcount.stp -G period=0 [...probe points...] |
| 18 |
# |
| 19 |
# Optionally, specify cumulative mode for periodic updates |
| 20 |
# stap eventcount.stp -G period=NNN -G cumulative=1 [...probe points...] |
| 21 |
# stap eventcount.stp -G period=NNN -G cumulative=1 [...probe points...] |
| 22 |
# |
| 23 |
# Optionally, specify sorting other than by =count |
| 24 |
# stap eventcount.stp -G sort=tid [...probe points...] |
| 25 |
# stap eventcount.stp -G sort=count [...probe points...] |
| 26 |
# stap eventcount.stp -G sort=event [...probe points...] |
| 27 |
# stap eventcount.stp -G sort=unsorted [...probe points...] |
| 28 |
# |
| 29 |
# Optionally, specify a limit for sorted event count reports |
| 30 |
# stap eventcount.stp -G lines=100 [...probe points...] |
| 31 |
# |
| 32 |
# Runtime controls: |
| 33 |
# 'j': cycles forwards through the sort options |
| 34 |
# 'k': cycles backwards through the sort options |
| 35 |
# 'l': toggles cumulative mode |
| 36 |
# |
| 37 |
# Pressing enter pauses the reporting and allows a new value for 'comm' |
| 38 |
# to be inputted. Doing so resets all counts. To return to analyzing |
| 39 |
# all processes leave 'comm' blank. |
| 40 |
|
| 41 |
global period=10 |
| 42 |
global cumulative=0 |
| 43 |
global comm="" |
| 44 |
global sort="count" |
| 45 |
global sort_options |
| 46 |
global lines=20 |
| 47 |
|
| 48 |
probe %($# == 0 || $# > 32 %? begin %: never %) |
| 49 |
{ |
| 50 |
printf("Please specify between 1 and 32 events to count.\n") |
| 51 |
exit() |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
global c% |
| 56 |
global totalc, filteredc |
| 57 |
|
| 58 |
//paramaterize up to 32 arguments |
| 59 |
probe %($# >= 1 %? $1 %: never %), |
| 60 |
%($# >= 2 %? $2 %: never %), |
| 61 |
%($# >= 3 %? $3 %: never %), |
| 62 |
%($# >= 4 %? $4 %: never %), |
| 63 |
%($# >= 5 %? $5 %: never %), |
| 64 |
%($# >= 6 %? $6 %: never %), |
| 65 |
%($# >= 7 %? $7 %: never %), |
| 66 |
%($# >= 8 %? $8 %: never %), |
| 67 |
%($# >= 9 %? $9 %: never %), |
| 68 |
%($# >= 10 %? $10 %: never %), |
| 69 |
%($# >= 11 %? $11 %: never %), |
| 70 |
%($# >= 12 %? $12 %: never %), |
| 71 |
%($# >= 13 %? $13 %: never %), |
| 72 |
%($# >= 14 %? $14 %: never %), |
| 73 |
%($# >= 15 %? $15 %: never %), |
| 74 |
%($# >= 16 %? $16 %: never %), |
| 75 |
%($# >= 17 %? $17 %: never %), |
| 76 |
%($# >= 18 %? $18 %: never %), |
| 77 |
%($# >= 19 %? $19 %: never %), |
| 78 |
%($# >= 20 %? $20 %: never %), |
| 79 |
%($# >= 21 %? $21 %: never %), |
| 80 |
%($# >= 22 %? $22 %: never %), |
| 81 |
%($# >= 23 %? $23 %: never %), |
| 82 |
%($# >= 24 %? $24 %: never %), |
| 83 |
%($# >= 25 %? $25 %: never %), |
| 84 |
%($# >= 26 %? $26 %: never %), |
| 85 |
%($# >= 27 %? $27 %: never %), |
| 86 |
%($# >= 28 %? $28 %: never %), |
| 87 |
%($# >= 29 %? $29 %: never %), |
| 88 |
%($# >= 30 %? $30 %: never %), |
| 89 |
%($# >= 31 %? $32 %: never %), |
| 90 |
%($# >= 32 %? $32 %: never %) |
| 91 |
{ |
| 92 |
totalc <<< 1 |
| 93 |
if (target() && ! target_set_pid(pid())) |
| 94 |
next |
| 95 |
if (comm != "" && execname() != comm) |
| 96 |
next |
| 97 |
filteredc <<< 1 |
| 98 |
c[sprintf("%s(%d)",execname(),tid()), pn()]<<<1 |
| 99 |
} |
| 100 |
|
| 101 |
probe begin { |
| 102 |
start_ms = gettimeofday_ms() |
| 103 |
if (target()) msg = sprintf ("pid %d + children", target()) |
| 104 |
else if (comm != "") msg = sprintf ("execname %s", comm) |
| 105 |
else msg = "unfiltered"; |
| 106 |
printf("Starting event counting at %s, %s\n", |
| 107 |
tz_ctime(gettimeofday_s()), msg) |
| 108 |
format = sprintf("max %d lines, sorted by %s", lines, sort); |
| 109 |
if (period) |
| 110 |
printf("%s reporting (%s) every %d s\n", |
| 111 |
cumulative ? "Cumulative" : "Incremental", |
| 112 |
format, period) |
| 113 |
else |
| 114 |
printf("One-time cumulative reporting (%s) at script termination (^C)\n", |
| 115 |
format) |
| 116 |
|
| 117 |
sort_options[0] = "count" |
| 118 |
sort_options[1] = "tid" |
| 119 |
sort_options[2] = "event" |
| 120 |
sort_options[3] = "unsorted" |
| 121 |
} |
| 122 |
global start_ms |
| 123 |
|
| 124 |
|
| 125 |
function reportline(tid, name, countstr) { |
| 126 |
// fine-grained control over the column layout here |
| 127 |
printf("%-22s %-15s %s\n", tid, countstr, name) |
| 128 |
} |
| 129 |
|
| 130 |
function reportline2(tid, name, count, elapsed_ms) { |
| 131 |
reportline(tid, name, |
| 132 |
sprintf("%d (%d.%02d)", count, |
| 133 |
(count * 100000 / elapsed_ms)/100, |
| 134 |
(count * 100000 / elapsed_ms)%100)) |
| 135 |
} |
| 136 |
|
| 137 |
global input_mode = "char" |
| 138 |
|
| 139 |
function report () { |
| 140 |
elapsed_ms = gettimeofday_ms() - start_ms |
| 141 |
if (elapsed_ms < 0) elapsed_ms=1 |
| 142 |
printf("%s time elapsed: %d ms, %d events, %d after filtering.\n", |
| 143 |
(cumulative ? "Cumulative" : "Period"), |
| 144 |
elapsed_ms, @count(totalc), @count(filteredc)) |
| 145 |
|
| 146 |
reportline("TID", "EVENT", "COUNT (Hz)") |
| 147 |
reportline("---", "-----", "----------") |
| 148 |
if (sort == "count") |
| 149 |
foreach([tid, name] in c- limit lines) |
| 150 |
reportline2(tid, name, @count(c[tid,name]), elapsed_ms) |
| 151 |
else if (sort == "tid") |
| 152 |
foreach([tid+, name] in c limit lines) |
| 153 |
reportline2(tid, name, @count(c[tid,name]), elapsed_ms) |
| 154 |
else if (sort == "event") |
| 155 |
foreach([tid, name+] in c limit lines) |
| 156 |
reportline2(tid, name, @count(c[tid,name]), elapsed_ms) |
| 157 |
else # unsorted |
| 158 |
foreach([tid, name] in c limit lines) |
| 159 |
reportline2(tid, name, @count(c[tid,name]), elapsed_ms) |
| 160 |
} |
| 161 |
|
| 162 |
probe end { |
| 163 |
report() |
| 164 |
printf("Finished event counting at %s.\n", tz_ctime(gettimeofday_s())) |
| 165 |
} |
| 166 |
|
| 167 |
probe timer.s(1) { |
| 168 |
if (period <= 0) next |
| 169 |
if ((gettimeofday_s() % period) == 0) { |
| 170 |
if (input_mode == "char") |
| 171 |
report() |
| 172 |
if (! cumulative) { |
| 173 |
delete c |
| 174 |
delete filteredc |
| 175 |
delete totalc |
| 176 |
start_ms = gettimeofday_ms() |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
probe input.line { |
| 182 |
if (input_mode != "line") |
| 183 |
next |
| 184 |
// remove newline character |
| 185 |
comm = substr(line, 0, strlen(line) - 1) |
| 186 |
if (comm == "") { |
| 187 |
print("\nNow analyzing all processes") |
| 188 |
} else { |
| 189 |
print("\nNow analyzing process \"" . comm . "\"") |
| 190 |
} |
| 191 |
delete c |
| 192 |
delete filteredc |
| 193 |
delete totalc |
| 194 |
} |
| 195 |
|
| 196 |
global idx = 0 // index into sort_options |
| 197 |
|
| 198 |
probe input.char { |
| 199 |
if (input_mode == "line") { |
| 200 |
print(char) |
| 201 |
if (char == "\n") |
| 202 |
input_mode = "char" |
| 203 |
} |
| 204 |
else if (char == "j") { |
| 205 |
idx = (idx + 1) % 4 |
| 206 |
sort = sort_options[idx] |
| 207 |
println("Sorting by " . sort) |
| 208 |
} |
| 209 |
else if (char == "k") { |
| 210 |
idx = idx ? (idx - 1) % 4 : 3 |
| 211 |
sort = sort_options[idx] |
| 212 |
println("Sorting by " . sort) |
| 213 |
} |
| 214 |
else if (char == "l") { |
| 215 |
cumulative = 1 - cumulative |
| 216 |
if (cumulative) |
| 217 |
println("Cumulative mode enabled") |
| 218 |
else |
| 219 |
println("Cumulative mode disabled") |
| 220 |
} |
| 221 |
else if (char == "\n") { |
| 222 |
input_mode = "line" |
| 223 |
println("Please enter a process name:") |
| 224 |
} |
| 225 |
} |