Skip to content

Commit 8e2e18e

Browse files
committed
show progress; show only top 10 wasters
1 parent 5b5196c commit 8e2e18e

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

ch8/waste_kmalloc_slabs.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,50 @@ name=$(basename $0)
2121
TMPF=/tmp/t.$$
2222
IFS=$'\n\t'
2323
cmd="grep -r -H -w waste /sys/kernel/debug/slab/kmalloc-[1-9]*/alloc_traces"
24+
n=0
25+
26+
echo "${name}: gathering data, please be patient ..."
2427
for rec in $(eval ${cmd})
2528
do
2629
#echo "rec = ${rec}"
2730
# Eg.
2831
# /sys/kernel/debug/slab/kmalloc-8/alloc_traces: 58 strndup_user+0x4a/0x70 waste=406/7 age=831078/831403/831933 pid=1-801 cpus=0-5 406
2932
# -----kmalloc-<foo> slab----------------------: num-times-requested func+start/len waste=num_wasted_total/num_wasted_eachtime pid=<...> cpus=<...>
3033

34+
# Get the absolute wastage:
3135
# Kernel modules contain an extra field, the module name [foo]
3236
# So get the field# of the column having the string 'waste=' ...
3337
waste_fieldnum=$(echo "${rec}" | awk '{for(i=1;i<=NF;i++) {if ($i ~ /waste=/) print i}}')
3438
# ... and then extract the absolute number of wasted bytes based on it
3539
# (of the form: waste=406/7, i.e., waste=num_wasted_total/num_wasted_eachtime)
3640
wastage_abs=$(echo "${rec}" | awk -v fld=${waste_fieldnum} '{print $fld}'|cut -d= -f2|cut -d/ -f1)
3741
echo "${rec} ${wastage_abs}" >> ${TMPF}
42+
let n=n+1
43+
[[ $((n % 100)) -eq 0 ]] && echo -n "."
3844
done
45+
echo
3946

4047
# separate out kernel internal kmalloc-* slabs from kernel modules slabs
41-
grep -v "\[.*\]" ${TMPF} > /tmp/kint.waste
42-
grep "\[.*\]" ${TMPF} > /tmp/kmods.waste
48+
grep -v "\[.*\]" ${TMPF} > kint.waste || true
49+
grep "\[.*\]" ${TMPF} > kmods.waste || true
4350

4451
echo "======== Wastage (highest-to-lowest with duplicate lines eliminated) ========"
45-
echo "--------------- kernel internal ----------------"
52+
echo "--------------- kernel internal ----------------
53+
Top 10 wasters (in desc order). (To see all, lookup the full report here: kint.waste)"
54+
[[ -s kint.waste ]] && {
4655
# kernel internal - the 8th fields is the number of 'waste' bytes
4756
# (As the abs number of wasted bytes is already there in the output, we use awk
4857
# to eliminate the last col, the number of wasted bytes)
4958
# Also, use uniq(1) to eliminate duplicate lines (?)
50-
sort -k8nr /tmp/kint.waste | awk 'NF{NF--};1' | uniq
51-
echo "--------------- kernel modules ----------------"
59+
sort -k8nr kint.waste | awk 'NF{NF--};1' | uniq | head
60+
} || echo "-none-"
61+
62+
echo "
63+
--------------- kernel modules ----------------
64+
Top 10 wasters (in desc order). (To see all, lookup the full report here: kmods.waste)"
5265
# kernel modules - the 9th fields is the number of 'waste' bytes
53-
sort -k9nr /tmp/kmods.waste | awk 'NF{NF--};1' | uniq
66+
[[ -s kmods.waste ]] && {
67+
sort -k9nr kmods.waste | awk 'NF{NF--};1' | uniq | head
68+
} || echo "-none-"
5469

5570
exit 0

0 commit comments

Comments
 (0)