Repository navigation
Expand file tree
/
Copy pathiaa_device_monitor.sh
More file actions
297 lines (265 loc) · 11.3 KB
/
Copy pathiaa_device_monitor.sh
File metadata and controls
297 lines (265 loc) · 11.3 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env bash
#SPDX-License-Identifier: BSD-3-Clause
#Copyright (c) 2026, Intel Corporation
# Shared helpers for monitoring iaa_crypto usage in benchmark scripts.
IAA_CRYPTO_DEBUGFS="${IAA_CRYPTO_DEBUGFS:-/sys/kernel/debug/iaa_crypto}"
IAA_DEVICE_STATS_SOURCE="${IAA_DEVICE_STATS_SOURCE:-}"
IAA_DEVICE_STATS_ERROR="${IAA_DEVICE_STATS_ERROR:-}"
iaa_monitor_is_iaa_compressor() {
local profile="$1"
[[ "$profile" == deflate-iaa* ]]
}
iaa_monitor_reset_stats() {
local reset_file="${IAA_CRYPTO_DEBUGFS}/stats_reset"
[[ -e "$reset_file" ]] || return 0
echo 1 > "$reset_file" 2>/dev/null || IAA_DEVICE_STATS_ERROR="cannot reset IAA stats via $reset_file"
}
# Read a debugfs file, skipping the sudo fork when already root (also avoids
# sudo ever blocking on a password prompt from a backgrounded sampler).
iaa_monitor_read_file() {
local f="$1"
[[ -f "$f" ]] || return 0
if [[ "$EUID" -eq 0 ]]; then
cat "$f" 2>/dev/null || true
else
sudo cat "$f" 2>/dev/null || true
fi
}
iaa_monitor_capture_global_stats() {
iaa_monitor_read_file "${IAA_CRYPTO_DEBUGFS}/global_stats"
}
iaa_monitor_get_stat() {
local metric="$1"
local stats="$2"
echo "$stats" | grep "$metric" | grep -oP '\K[0-9]+$' || echo "0"
}
iaa_monitor_collect_global_snapshot() {
local stats
stats="$(iaa_monitor_capture_global_stats)"
[[ -z "$stats" ]] && echo "" && return
# Single awk pass instead of four forked grep|grep pipelines per snapshot,
# since this runs on every sampler tick.
awk '
/total_comp_calls:/ { cc = $NF }
/total_decomp_calls:/ { dc = $NF }
/total_comp_bytes_in:/ { cb_in = $NF; have_cb_in = 1 }
/total_comp_bytes_out:/ { cb_out = $NF }
/total_decomp_bytes_in:/ { db_in = $NF }
/total_decomp_bytes_out:/ { db_out = $NF; have_db_out = 1 }
END {
cb = have_cb_in ? cb_in : cb_out
db = have_db_out ? db_out : db_in
printf "%d|%d|%d|%d\n", cc + 0, dc + 0, cb + 0, db + 0
}
' <<< "$stats"
}
iaa_monitor_calc_throughput_mbs() {
local bytes="$1"
local ns="$2"
if [[ ! "$ns" =~ ^[0-9]+$ ]] || [[ ! "$bytes" =~ ^[0-9]+$ ]] || [[ $ns -le 0 ]] || [[ $bytes -le 0 ]]; then
echo "0.0"
return
fi
awk -v b="$bytes" -v ns="$ns" 'BEGIN{printf "%.2f", (b * 1000000000.0) / (ns * 1048576.0)}'
}
iaa_monitor_calc_throughput() {
local before="$1"
local after="$2"
local comp_elapsed_ns="$3"
local decomp_elapsed_ns="$4"
[[ -z "$before" ]] || [[ -z "$after" ]] && echo "" && return
local before_comp_calls before_decomp_calls before_comp_bytes before_decomp_bytes
local after_comp_calls after_decomp_calls after_comp_bytes after_decomp_bytes
IFS='|' read -r before_comp_calls before_decomp_calls before_comp_bytes before_decomp_bytes <<< "$before"
IFS='|' read -r after_comp_calls after_decomp_calls after_comp_bytes after_decomp_bytes <<< "$after"
local delta_comp_calls=$((after_comp_calls - before_comp_calls))
local delta_decomp_calls=$((after_decomp_calls - before_decomp_calls))
local delta_comp_bytes=$((after_comp_bytes - before_comp_bytes))
local delta_decomp_bytes=$((after_decomp_bytes - before_decomp_bytes))
local comp_mbs decomp_mbs
comp_mbs="$(iaa_monitor_calc_throughput_mbs "$delta_comp_bytes" "$comp_elapsed_ns")"
decomp_mbs="$(iaa_monitor_calc_throughput_mbs "$delta_decomp_bytes" "$decomp_elapsed_ns")"
echo "$delta_comp_calls|$delta_decomp_calls|$delta_comp_bytes|$delta_decomp_bytes|$comp_mbs|$decomp_mbs"
}
# The single before/after snapshot above divides total bytes by the whole
# page-in/page-out wall-clock time, which also includes fault-handling
# overhead unrelated to (de)compression and so underestimates true IAA
# throughput. The sampler below polls global_stats on a fixed interval so
# throughput can instead be computed only over the windows where the byte
# counters actually advanced ("busy" time).
# Poll global_stats every interval_ms and append "ts_ns|snapshot" lines to
# samples_file until iaa_monitor_sampler_stop is called. Prints the sampler
# PID to stdout.
iaa_monitor_sampler_start() {
local interval_ms="$1"
local samples_file="$2"
local sleep_s
sleep_s=$(awk -v ms="$interval_ms" 'BEGIN{printf "%.3f", ms/1000.0}')
: > "$samples_file"
# Detach stdio: otherwise this background subshell inherits the write end
# of the caller's `$(iaa_monitor_sampler_start ...)` pipe, and that
# command substitution blocks forever waiting for EOF since the loop
# never exits on its own.
(
# errexit is inherited from the caller; disable it here so a single
# empty/failed snapshot can't silently kill the sampler loop.
set +e
while true; do
snap="$(iaa_monitor_collect_global_snapshot)"
if [[ -n "$snap" ]]; then
# Bash builtin (no fork), unlike `date +%s%N`.
ts="${EPOCHREALTIME/./}000"
echo "${ts}|${snap}" >> "$samples_file"
fi
sleep "$sleep_s"
done
) < /dev/null > /dev/null 2>&1 &
echo $!
}
iaa_monitor_sampler_stop() {
local pid="$1"
[[ -n "$pid" ]] || return 0
kill "$pid" 2>/dev/null || true
for _ in 1 2 3 4 5; do
kill -0 "$pid" 2>/dev/null || break
sleep 0.1
done
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null || true
fi
wait "$pid" 2>/dev/null || true
return 0
}
# Compute "busy-window" throughput from a samples file: bytes advanced summed
# over only the sampling intervals where that counter increased, divided by
# the cumulative duration of those intervals (idle intervals are excluded).
# Also tracks the peak single-interval throughput (highest instantaneous
# MB/s seen between any two consecutive samples).
# Output: comp_mbs|decomp_mbs|peak_comp_mbs|peak_decomp_mbs|comp_active_intervals|decomp_active_intervals|total_intervals
iaa_monitor_analyze_samples() {
local samples_file="$1"
if [[ ! -s "$samples_file" ]]; then
echo ""
return
fi
awk -F'|' '
NR == 1 { pts = $1; pcb = $4; pdb = $5; next }
{
dt = $1 - pts
if (dt > 0) {
total++
dcomp = $4 - pcb
ddecomp = $5 - pdb
if (dcomp > 0) {
comp_bytes_sum += dcomp; comp_ns_sum += dt; comp_active++
comp_mbs_i = (dcomp * 1000000000.0) / (dt * 1048576.0)
if (comp_mbs_i > peak_comp) peak_comp = comp_mbs_i
}
if (ddecomp > 0) {
decomp_bytes_sum += ddecomp; decomp_ns_sum += dt; decomp_active++
decomp_mbs_i = (ddecomp * 1000000000.0) / (dt * 1048576.0)
if (decomp_mbs_i > peak_decomp) peak_decomp = decomp_mbs_i
}
}
pts = $1; pcb = $4; pdb = $5
}
END {
comp_mbs = (comp_ns_sum > 0) ? (comp_bytes_sum * 1000000000.0) / (comp_ns_sum * 1048576.0) : 0
decomp_mbs = (decomp_ns_sum > 0) ? (decomp_bytes_sum * 1000000000.0) / (decomp_ns_sum * 1048576.0) : 0
printf "%.2f|%.2f|%.2f|%.2f|%d|%d|%d\n", comp_mbs, decomp_mbs, peak_comp + 0, peak_decomp + 0, comp_active + 0, decomp_active + 0, total + 0
}
' "$samples_file"
}
iaa_monitor_capture_device_stats() {
local out_file="$1"
local stats_file=""
if [[ -r "${IAA_CRYPTO_DEBUGFS}/stats" ]]; then
stats_file="${IAA_CRYPTO_DEBUGFS}/stats"
elif [[ -r "${IAA_CRYPTO_DEBUGFS}/wq_stats" ]]; then
stats_file="${IAA_CRYPTO_DEBUGFS}/wq_stats"
else
IAA_DEVICE_STATS_ERROR="IAA device stats not available at ${IAA_CRYPTO_DEBUGFS}/stats or ${IAA_CRYPTO_DEBUGFS}/wq_stats"
return 0
fi
IAA_DEVICE_STATS_SOURCE="$stats_file"
local content
content="$(iaa_monitor_read_file "$stats_file")"
if [[ -z "$content" ]]; then
IAA_DEVICE_STATS_ERROR="cannot read IAA device stats from $stats_file"
rm -f "$out_file"
return 0
fi
printf '%s\n' "$content" > "$out_file"
}
iaa_monitor_print_device_report() {
local log_dir="$1"
local report_file="${log_dir}/iaa_device_usage_report.txt"
local stats_source="${IAA_DEVICE_STATS_SOURCE:-${IAA_CRYPTO_DEBUGFS}/stats or ${IAA_CRYPTO_DEBUGFS}/wq_stats}"
local have_stats=0
local stats_file
mkdir -p "$log_dir"
for stats_file in "$log_dir"/*/iaa_stats_*.txt; do
[[ -s "$stats_file" ]] && have_stats=1 && break
done
if (( ! have_stats )); then
{
echo ""
echo "--- IAA device usage from ${stats_source} ---"
echo "IAA device usage report unavailable: no per-device stats were captured."
[[ -n "$IAA_DEVICE_STATS_ERROR" ]] && echo "$IAA_DEVICE_STATS_ERROR"
echo "Check that ${IAA_CRYPTO_DEBUGFS}/stats or ${IAA_CRYPTO_DEBUGFS}/wq_stats exists and is readable."
} > "$report_file"
echo "IAA device stats report available at: $report_file"
return 0
fi
{
echo ""
echo "--- IAA device usage from ${stats_source} ---"
printf "%-28s %8s %6s %15s %15s %15s %15s %15s\n" \
"Compressor" "Run" "IAA" "CompCalls" "DecompCalls" "CompBytes" "DecompBytes" "TotalBytes"
printf "%-28s %8s %6s %15s %15s %15s %15s %15s\n" \
"----------------------------" "--------" "----" "--------------" "--------------" "--------------" "--------------" "--------------"
for stats_file in "$log_dir"/*/iaa_stats_*.txt; do
[[ -s "$stats_file" ]] || continue
local profile run_name profile_dir file_name
profile_dir="$(dirname "$stats_file")"
profile="$(basename "$profile_dir")"
file_name="$(basename "$stats_file")"
run_name="${file_name#iaa_stats_}"
run_name="${run_name%.txt}"
awk -v profile="$profile" -v run_name="$run_name" '
function reset_device() {
id = "--"
comp_calls = 0
decomp_calls = 0
comp_bytes = 0
decomp_bytes = 0
in_wqs = 0
seen = 0
}
function emit_device() {
if (!seen) {
return
}
printf "%-28s %8s %6s %15d %15d %15d %15d %15d\n", \
profile, run_name, id, comp_calls, decomp_calls, comp_bytes, decomp_bytes, comp_bytes + decomp_bytes
}
BEGIN { reset_device() }
/^iaa device:/ { emit_device(); reset_device(); seen = 1; next }
/^[[:space:]]*wqs:/ { in_wqs = 1; next }
in_wqs { next }
{
key = $1
sub(/:$/, "", key)
if (key == "id") id = $2
else if (key == "comp_calls") comp_calls = $2 + 0
else if (key == "decomp_calls") decomp_calls = $2 + 0
else if (key == "comp_bytes") comp_bytes = $2 + 0
else if (key == "decomp_bytes") decomp_bytes = $2 + 0
}
END { emit_device() }
' "$stats_file"
done
} > "$report_file"
echo "IAA device stats report available at: $report_file"
}