+samir, Venkat
Tested the values compatibilty on Power11 Dedicated and Shared LPAR's.
All the values are same, except id and uevent which was not there
before. That should help rather than hurt any tool.
This may need more broader checks on different boards/configuration
to ensure it is same. Also DLPAR CPU add/remove is not tested yet.
Please test and report if your board/platform shows up any different
values in those cache entries.
Could you please help to run DLPAR and ensure the cache sysfs entries remain
same. Also if you have a P7/P8 it would be great if you validate there as well.
Please find the below script that i used to do the verification.
(Again, thanks to LLM)
---
#!/usr/bin/env bash
#
# Validate PowerPC cacheinfo before and after migration to generic cacheinfo.
#
# Usage:
# sudo ./validate-cacheinfo.sh capture before
#
# # Reboot into the patched kernel
#
# sudo ./validate-cacheinfo.sh capture after
# sudo ./validate-cacheinfo.sh compare before after
#
# Expected additions from generic cacheinfo:
# index*/id
# index*/uevent
#
# These attributes are excluded from the compatibility comparison.
#
set -euo pipefail
SYS_CPU=/sys/devices/system/cpu
die()
{
echo "ERROR: $*" >&2
exit 1
}
command_exists()
{
command -v "$1" >/dev/null 2>&1
}
capture_file()
{
local source=$1
local destination=$2
if [[ -r "$source" ]]; then
cat "$source" > "$destination"
else
printf '<unavailable>\n' > "$destination"
fi
}
capture_cache_sysfs()
{
local output=$1
local cpu_dir cache_dir index attr
local cpu index_name value
: > "$output"
for cpu_dir in "$SYS_CPU"/cpu[0-9]*; do
[[ -d "$cpu_dir" ]] || continue
cpu=${cpu_dir##*/cpu}
cache_dir="$cpu_dir/cache"
echo "Capturing CPU $cpu" >&2
if [[ ! -d "$cache_dir" ]]; then
printf '%s\t%s\t%s\t%s\n' \
"$cpu" \
"<none>" \
"<cache-directory>" \
"<absent>" \
>> "$output"
continue
fi
for index in "$cache_dir"/index*; do
[[ -d "$index" ]] || continue
index_name=${index##*/}
#
# Do not use find -type f. Sysfs attributes are virtual
# files and may not be classified consistently by all
# find implementations.
#
for attr in "$index"/*; do
[[ -e "$attr" || -L "$attr" ]] || continue
[[ ! -d "$attr" ]] || continue
[[ -r "$attr" ]] || continue
value=$(
tr '\n' ' ' < "$attr" |
sed 's/[[:space:]]*$//'
)
printf '%s\t%s\t%s\t%s\n' \
"$cpu" \
"$index_name" \
"${attr##*/}" \
"$value" \
>> "$output"
done
done
done
sort -t $'\t' \
-k1,1n \
-k2,2V \
-k3,3 \
"$output" -o "$output"
}
generate_compatibility_view()
{
local snapshot=$1
#
# The old PowerPC implementation did not expose id or uevent.
# Exclude both when checking whether existing ABI values changed.
#
awk -F $'\t' '
$3 != "id" && $3 != "uevent" {
print
}
' "$snapshot/cacheinfo.tsv" \
> "$snapshot/cacheinfo-compatible.tsv"
}
generate_id_view()
{
local snapshot=$1
local cpu index attribute value
: > "$snapshot/cache-ids.tsv"
while IFS=$'\t' read -r cpu index attribute value; do
if [[ "$attribute" == "id" ]]; then
printf '%s\t%s\t%s\n' \
"$cpu" "$index" "$value" \
>> "$snapshot/cache-ids.tsv"
fi
done < "$snapshot/cacheinfo.tsv"
}
generate_uevent_view()
{
local snapshot=$1
local cpu index attribute value
: > "$snapshot/cache-uevents.tsv"
while IFS=$'\t' read -r cpu index attribute value; do
if [[ "$attribute" == "uevent" ]]; then
printf '%s\t%s\t%s\n' \
"$cpu" "$index" "$value" \
>> "$snapshot/cache-uevents.tsv"
fi
done < "$snapshot/cacheinfo.tsv"
}
capture_snapshot()
{
local output=$1
local leaves ids uevents
[[ -n "$output" ]] || die "capture directory is required"
[[ ! -e "$output" ]] || die "$output already exists"
mkdir -p "$output"
uname -a > "$output/uname.txt"
capture_file /proc/version "$output/version.txt"
capture_file /proc/cmdline "$output/cmdline.txt"
capture_file "$SYS_CPU/online" "$output/cpus-online.txt"
capture_file "$SYS_CPU/offline" "$output/cpus-offline.txt"
capture_file "$SYS_CPU/possible" "$output/cpus-possible.txt"
capture_file "$SYS_CPU/present" "$output/cpus-present.txt"
capture_cache_sysfs "$output/cacheinfo.tsv"
generate_compatibility_view "$output"
generate_id_view "$output"
generate_uevent_view "$output"
if command_exists lscpu; then
timeout 60 lscpu \
> "$output/lscpu.txt" 2>&1 || true
timeout 60 lscpu -C \
> "$output/lscpu-cache.txt" 2>&1 || true
timeout 60 lscpu -e=CPU,CORE,SOCKET,NODE,CACHE \
> "$output/lscpu-extended.txt" 2>&1 || true
#
# Exclude CACHE from the automatically compared view because
# lscpu may switch from userspace-generated contiguous numbers
# to sparse kernel-provided cache IDs.
#
timeout 60 lscpu -e=CPU,CORE,SOCKET,NODE \
> "$output/lscpu-topology.txt" 2>&1 || true
fi
dmesg > "$output/dmesg.txt" 2>/dev/null || true
leaves=$(
awk -F $'\t' '
$3 == "level" {
count++
}
END {
print count + 0
}
' "$output/cacheinfo.tsv"
)
ids=$(wc -l < "$output/cache-ids.tsv")
uevents=$(wc -l < "$output/cache-uevents.tsv")
echo
echo "Captured snapshot: $output"
echo "Cache leaves: $leaves"
echo "Cache IDs: $ids"
echo "Cache uevents: $uevents"
if [[ "$ids" -eq 0 ]]; then
echo "No cache IDs found."
echo "This is expected for the old PowerPC cacheinfo
implementation."
fi
if [[ "$uevents" -eq 0 ]]; then
echo "No cache uevent files found."
echo "This is expected for the old PowerPC cacheinfo
implementation."
fi
}
expand_cpu_list()
{
local list=$1
local item first last cpu
local -a elements
IFS=',' read -r -a elements <<< "$list"
for item in "${elements[@]}"; do
if [[ "$item" == *-* ]]; then
first=${item%-*}
last=${item#*-}
for ((cpu = first; cpu <= last; cpu++)); do
echo "$cpu"
done
else
echo "$item"
fi
done
}
build_leaf_records()
{
local snapshot=$1
local output=$2
awk -F $'\t' '
{
key = $1 SUBSEP $2
value[key, $3] = $4
seen[key] = 1
}
END {
for (key in seen) {
split(key, part, SUBSEP)
if (part[2] == "<none>")
continue
printf "%s\t%s\t%s\t%s\t%s\t%s\n",
part[1],
part[2],
value[key, "level"],
value[key, "type"],
value[key, "id"],
value[key, "shared_cpu_list"]
}
}' "$snapshot/cacheinfo.tsv" |
sort -t $'\t' -k1,1n -k2,2V > "$output"
}
lookup_leaf_field()
{
local records=$1
local cpu=$2
local level=$3
local type=$4
local field=$5
awk -F $'\t' \
-v cpu="$cpu" \
-v level="$level" \
-v type="$type" \
-v field="$field" '
$1 == cpu && $3 == level && $4 == type {
if (field == "id")
print $5
else if (field == "shared")
print $6
exit
}
' "$records"
}
validate_new_ids()
{
local snapshot=$1
local records
local cpu index level type id shared
local sibling sibling_id sibling_shared
local failed=0
records=$(mktemp)
build_leaf_records "$snapshot" "$records"
echo
echo "Validating generic cache IDs..."
while IFS=$'\t' read -r cpu index level type id shared; do
if [[ -z "$id" ]]; then
echo "ERROR: cpu${cpu}/${index} has no id"
failed=1
continue
fi
if [[ -z "$shared" ]]; then
echo "ERROR: cpu${cpu}/${index} has no shared_cpu_list"
failed=1
continue
fi
#
# Every CPU in shared_cpu_list must report the same ID and
# shared CPU list for a leaf of the same level and type.
#
while IFS= read -r sibling; do
sibling_id=$(
lookup_leaf_field \
"$records" \
"$sibling" \
"$level" \
"$type" \
"id"
)
sibling_shared=$(
lookup_leaf_field \
"$records" \
"$sibling" \
"$level" \
"$type" \
"shared"
)
if [[ -z "$sibling_id" ]]; then
echo "ERROR: cpu${cpu}/${index}: CPU
${sibling}" \
"has no L${level} ${type} cache ID"
failed=1
elif [[ "$sibling_id" != "$id" ]]; then
echo "ERROR: cpu${cpu}/${index}: ID is ${id}," \
"but CPU ${sibling} has ID ${sibling_id}"
failed=1
fi
if [[ -z "$sibling_shared" ]]; then
echo "ERROR: cpu${cpu}/${index}: CPU
${sibling}" \
"has no L${level} ${type} shared_cpu_list"
failed=1
elif [[ "$sibling_shared" != "$shared" ]]; then
echo "ERROR: cpu${cpu}/${index}: shared list
is" \
"${shared}, but CPU ${sibling} reports" \
"${sibling_shared}"
failed=1
fi
done < <(expand_cpu_list "$shared")
done < "$records"
rm -f "$records"
if (( failed )); then
echo "Cache ID validation: FAILED"
return 1
fi
echo "Cache ID validation: PASS"
}
compare_file()
{
local description=$1
local before=$2
local after=$3
echo
echo "Comparing $description..."
if diff -u "$before" "$after"; then
echo "$description: PASS"
return 0
fi
echo "$description: FAILED"
return 1
}
compare_snapshots()
{
local before=$1
local after=$2
local failed=0
local before_ids after_ids
local before_uevents after_uevents
[[ -f "$before/cacheinfo-compatible.tsv" ]] ||
die "$before is not a valid snapshot"
[[ -f "$after/cacheinfo-compatible.tsv" ]] ||
die "$after is not a valid snapshot"
if ! compare_file \
"cache sysfs values excluding id and uevent" \
"$before/cacheinfo-compatible.tsv" \
"$after/cacheinfo-compatible.tsv"; then
failed=1
fi
for mask in online offline possible present; do
if ! compare_file \
"CPU ${mask} mask" \
"$before/cpus-${mask}.txt" \
"$after/cpus-${mask}.txt"; then
failed=1
fi
done
if [[ -f "$before/lscpu-topology.txt" &&
-f "$after/lscpu-topology.txt" ]]; then
if ! compare_file \
"lscpu topology without cache IDs" \
"$before/lscpu-topology.txt" \
"$after/lscpu-topology.txt"; then
failed=1
fi
fi
before_ids=$(wc -l < "$before/cache-ids.tsv")
after_ids=$(wc -l < "$after/cache-ids.tsv")
before_uevents=$(wc -l < "$before/cache-uevents.tsv")
after_uevents=$(wc -l < "$after/cache-uevents.tsv")
echo
echo "Expected generic-cacheinfo additions:"
echo " Before IDs: $before_ids"
echo " After IDs: $after_ids"
echo " Before uevents: $before_uevents"
echo " After uevents: $after_uevents"
if [[ "$after_ids" -eq 0 ]]; then
echo "ERROR: patched kernel exposes no index*/id attributes"
failed=1
fi
if [[ "$after_uevents" -eq 0 ]]; then
echo "ERROR: patched kernel exposes no index*/uevent attributes"
failed=1
fi
echo
echo "Cache IDs exposed by generic cacheinfo:"
if [[ -s "$after/cache-ids.tsv" ]]; then
if command_exists column; then
{
printf 'CPU\tINDEX\tID\n'
cat "$after/cache-ids.tsv"
} | column -t -s $'\t'
else
cat "$after/cache-ids.tsv"
fi
fi
if ! validate_new_ids "$after"; then
failed=1
fi
echo
echo "Informational files for manual inspection:"
echo " $before/lscpu-cache.txt"
echo " $after/lscpu-cache.txt"
echo " $before/lscpu-extended.txt"
echo " $after/lscpu-extended.txt"
echo
echo "The lscpu CACHE column may legitimately change from contiguous"
echo "userspace-generated values to sparse kernel-provided cache IDs."
echo
echo "The new id and uevent files are treated as expected ABI additions"
echo "and are excluded from the compatibility diff."
if (( failed )); then
echo
echo "Overall result: FAILED"
return 1
fi
echo
echo "Overall result: PASS"
}
usage()
{
cat <<EOF
Usage:
$0 capture <directory>
$0 compare <before-directory> <after-directory>
Example:
sudo $0 capture before
# Reboot into the patched kernel
sudo $0 capture after
sudo $0 compare before after
EOF
}
[[ $# -ge 1 ]] || {
usage
exit 1
}
case "$1" in
capture)
[[ $# -eq 2 ]] ||
die "capture requires one output directory"
capture_snapshot "$2"
;;
compare)
[[ $# -eq 3 ]] ||
die "compare requires before and after directories"
compare_snapshots "$2" "$3"
;;
*)
usage
exit 1
;;
esac