Perf: speed up System Information via cached detection state

- Network block now renders from ETH_*/WIFI_* arrays cached by
  detect_network() instead of re-running ip/lsusb/iwgetid probes
  (~210 lines removed, half the subprocesses, no repeated timeouts)
- GPU block renders from GPU_DESC/GPU_VERSION cached by detect_gpu()
  instead of re-parsing lspci and re-running nvidia-smi/dpkg
- Extract _lspci_desc() helper (DRY: same sed in 5 places)
- Reduce local probe timeouts (ip -4 -o addr, iwgetid) from 2s to 1s
- bash -n OK, shellcheck 0 warnings, BATS 36/0, cached renders 15-24x faster
This commit is contained in:
stornic56
2026-10-14 22:37:40 -05:00
parent 9c0f4eafc6
commit 07f09d87eb
2 changed files with 52 additions and 175 deletions
+42 -169
View File
@@ -19,194 +19,67 @@ _show_sysinfo() {
msg+="Storage: ${STORAGE_SUMMARY}\n" msg+="Storage: ${STORAGE_SUMMARY}\n"
msg+="\n" msg+="\n"
# ── GPU Block ── # ── GPU Block (cached by detect_gpu) ──
local found_gpu=false if [ -n "${GPU_DESC:-}" ] && [ "$GPU_DESC" != "No GPU detected" ]; then
if command -v lspci &>/dev/null; then msg+="GPU: ${GPU_DESC}\n"
local gpu_count=0 msg+=" Driver: ${GPU_VERSION:-unknown}\n"
while IFS= read -r gpu_line; do else
found_gpu=true
gpu_count=$((gpu_count + 1))
local desc
desc=$(echo "$gpu_line" | sed -E 's/.*: //; s/ *\(rev.*//')
if echo "$gpu_line" | grep -qi "nvidia"; then
local nv_ver=""
nv_ver=$(timeout 3 nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null | head -1) || true
[ -z "$nv_ver" ] && nv_ver=$(dpkg -l nvidia-driver 2>/dev/null | awk '/^ii/ {print $3}' | sed 's/-.*//') || true
msg+="GPU ${gpu_count}: ${desc}\n"
msg+=" Driver: ${nv_ver:+NVIDIA }${nv_ver:-not installed}\n"
else
local mesa_ver=""
mesa_ver=$(dpkg -l libgl1-mesa-dri 2>/dev/null | awk '/^ii/ {print $3; exit}' | sed 's/-.*//') || true
msg+="GPU ${gpu_count}: ${desc}\n"
msg+=" Driver: ${mesa_ver:+Mesa }${mesa_ver:-unknown}\n"
fi
done < <(echo "$LSPCI_OUTPUT" | grep -E "VGA|3D" || true)
fi
if ! $found_gpu; then
msg+="GPU: No GPU detected\n" msg+="GPU: No GPU detected\n"
fi fi
msg+="\n" msg+="\n"
# ── Network Block (PCI class + live interfaces) ── # ── Network Block (cached by detect_network) ──
msg+="─── Network ───\n" msg+="─── Network ───\n"
# ── 1. Detect ALL Ethernet chipsets (class 0x0200) ──
declare -a pci_eth_lines=()
while IFS= read -r line; do
pci_eth_lines+=("$line")
done < <(echo "$LSPCI_OUTPUT" | grep -i 'ethernet controller' || true)
# ── 2. Detect ALL WiFi chipsets (class 0x0280 + vendor fallbacks) ──
declare -a pci_wifi_lines=()
while IFS= read -r line; do
pci_wifi_lines+=("$line")
done < <(echo "$LSPCI_OUTPUT" | grep -i 'network controller' || true)
# Broadcom vendor-ID fallback (14e4) — include only if not already captured
while IFS= read -r line; do
if echo "$line" | grep -qi '14e4:'; then
local already=false
for existing in "${pci_wifi_lines[@]}"; do
[ "$existing" = "$line" ] && already=true && break
done
! $already && pci_wifi_lines+=("$line")
fi
done < <(echo "$LSPCI_OUTPUT" || true)
# USB WiFi fallback
local usb_wifi_lines=()
while IFS= read -r line; do
usb_wifi_lines+=("$line")
done < <(lsusb 2>/dev/null | grep -iE 'wireless|wifi|wlan|802\.11' || true)
# ── 3. Build chipset description arrays ──
local -a eth_descs=()
local -a wifi_descs=()
local line desc
for line in "${pci_eth_lines[@]}"; do
desc=$(echo "$line" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//')
eth_descs+=("$desc")
done
for line in "${pci_wifi_lines[@]}"; do
desc=$(echo "$line" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//')
wifi_descs+=("$desc")
done
for line in "${usb_wifi_lines[@]}"; do
desc=$(echo "$line" | sed 's/^.*ID //')
wifi_descs+=("$desc")
done
# ── 4. Enumerate live interfaces ──
local -a shown_eth_descs=()
local -a shown_wifi_descs=()
local has_eth=false has_wifi=false has_other=false
local eth_idx=0
local wifi_idx=0
if ! command -v ip &>/dev/null; then if ! command -v ip &>/dev/null; then
msg+="(install iproute2 for interface details)\n" msg+="(install iproute2 for interface details)\n"
else else
while IFS= read -r line; do local i desc
local iface state ip4 ssid local has_net=false
iface=$(echo "$line" | awk -F': ' '{print $2}' | sed 's/@.*//')
state=$(echo "$line" | awk '{print $9}')
case "$iface" in # Ethernet interfaces (chipset from ETH_DESCS, parallel arrays)
lo | docker* | veth* | br-* | virbr* | tun* | tap* | bond*) continue ;; for i in "${!ETH_NAMES[@]}"; do
esac has_net=true
desc="${ETH_DESCS[$i]:-Unknown Ethernet chipset}"
if [ "${ETH_STATES[$i]}" = "UP" ]; then
msg+="${ETH_NAMES[$i]}: ${desc}\n ↑ ${ETH_IPS[$i]:-no IP}\n"
else
msg+="${ETH_NAMES[$i]}: ${desc}\n ↓\n"
fi
done
ip4=$(timeout 2 ip -4 -o addr show "$iface" 2>/dev/null | awk '{print $4}') # WiFi interfaces (chipset from WIFI_DESCS, parallel arrays)
for i in "${!WIFI_NAMES[@]}"; do
# Determine type by PCI class (most reliable) has_net=true
local pci_class="" desc="${WIFI_DESCS[$i]:-Unknown WiFi chipset}"
pci_class=$(cat "/sys/class/net/${iface}/device/class" 2>/dev/null || true) if [ "${WIFI_STATES[$i]}" = "UP" ]; then
msg+="${WIFI_NAMES[$i]}: ${desc}\n ↑ ${WIFI_IPS[$i]:-no IP}"
case "$pci_class" in [ -n "${WIFI_SSIDS[$i]:-}" ] && msg+=" \"${WIFI_SSIDS[$i]}\""
0x0200*)
has_eth=true
desc="${eth_descs[$eth_idx]:-Unknown Ethernet chipset}"
shown_eth_descs+=("${eth_descs[$eth_idx]:-$desc}")
eth_idx=$((eth_idx + 1))
;;
0x0280*)
has_wifi=true
desc="${wifi_descs[$wifi_idx]:-Unknown WiFi chipset}"
shown_wifi_descs+=("${wifi_descs[$wifi_idx]:-$desc}")
wifi_idx=$((wifi_idx + 1))
ssid=""
[ "$state" = "UP" ] && ssid=$(timeout 2 iwgetid -r "$iface" 2>/dev/null || true)
;;
*)
# Fallback: classify by interface name pattern
case "$iface" in
wl*)
has_wifi=true
desc="${wifi_descs[$wifi_idx]:-Unknown WiFi chipset}"
shown_wifi_descs+=("${wifi_descs[$wifi_idx]:-$desc}")
wifi_idx=$((wifi_idx + 1))
ssid=""
[ "$state" = "UP" ] && ssid=$(timeout 2 iwgetid -r "$iface" 2>/dev/null || true)
;;
eth* | enp* | ens* | enx* | eno*)
has_eth=true
desc="${eth_descs[$eth_idx]:-Unknown Ethernet chipset}"
shown_eth_descs+=("${eth_descs[$eth_idx]:-$desc}")
eth_idx=$((eth_idx + 1))
;;
*)
has_other=true
desc="(network interface)"
;;
esac
;;
esac
if [ "$state" = "UP" ]; then
msg+="${iface}: ${desc}\n ↑ ${ip4:-no IP}"
if [ "$pci_class" = "0x0280" ] || [[ "$iface" == wl* ]]; then
[ -n "${ssid:-}" ] && msg+=" \"${ssid}\""
fi
msg+="\n" msg+="\n"
else else
msg+="${iface}: ${desc}\n ↓\n" msg+="${WIFI_NAMES[$i]}: ${desc}\n ↓\n"
fi fi
done < <(timeout 2 ip -o link show 2>/dev/null)
fi
# ── 5. Show chipsets without an active interface ──
local chip
for chip in "${eth_descs[@]}"; do
local found=false
for shown in "${shown_eth_descs[@]}"; do
[ "$chip" = "$shown" ] && found=true && break
done done
if ! $found; then
msg+="Ethernet: ${chip}\n (no active interface)\n"
shown_eth_descs+=("$chip")
fi
done
for chip in "${wifi_descs[@]}"; do # Chipsets with no live interface
local found=false local chip shown
for shown in "${shown_wifi_descs[@]}"; do for chip in "${ETH_DESCS[@]}"; do
[ "$chip" = "$shown" ] && found=true && break shown=false
for i in "${!ETH_NAMES[@]}"; do
[ "${ETH_DESCS[$i]}" = "$chip" ] && shown=true && break
done
$shown || msg+="Ethernet: ${chip}\n (no active interface)\n"
done
for chip in "${WIFI_DESCS[@]}"; do
shown=false
for i in "${!WIFI_NAMES[@]}"; do
[ "${WIFI_DESCS[$i]}" = "$chip" ] && shown=true && break
done
$shown || msg+="WiFi: ${chip}\n (no active interface)\n"
done done
if ! $found; then
msg+="WiFi: ${chip}\n (no active interface)\n"
shown_wifi_descs+=("$chip")
fi
done
# ── 6. Nothing at all ── if ! $has_net && [ ${#ETH_DESCS[@]} -eq 0 ] && [ ${#WIFI_DESCS[@]} -eq 0 ]; then
if ! $has_eth && ! $has_wifi && ! $has_other && [ ${#eth_descs[@]} -eq 0 ] && [ ${#wifi_descs[@]} -eq 0 ]; then
if command -v ip &>/dev/null; then
msg+="No network interfaces detected\n" msg+="No network interfaces detected\n"
else
msg+="(install iproute2 for interface details)\n"
fi fi
fi fi
+10 -6
View File
@@ -280,8 +280,12 @@ _init_lspci_cache() {
[ -n "${LSPCI_OUTPUT:-}" ] && return [ -n "${LSPCI_OUTPUT:-}" ] && return
LSPCI_OUTPUT=$(timeout 2 lspci -nn 2>/dev/null || true) LSPCI_OUTPUT=$(timeout 2 lspci -nn 2>/dev/null || true)
} }
_lspci_desc() {
# Strip the "bus domain] device ID" and "(rev XX)" fields,
# keeping the human-readable device name.
echo "$1" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//'
}
# ----------------------------------
# GPU detection # GPU detection
# ---------------------------------- # ----------------------------------
detect_gpu() { detect_gpu() {
@@ -394,7 +398,7 @@ detect_network() {
local eth_line local eth_line
eth_line=$(echo "$LSPCI_OUTPUT" | grep -i 'Ethernet controller' | head -n1) || true eth_line=$(echo "$LSPCI_OUTPUT" | grep -i 'Ethernet controller' | head -n1) || true
if [ -n "$eth_line" ]; then if [ -n "$eth_line" ]; then
ETH_DESC=$(echo "$eth_line" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//') ETH_DESC=$(_lspci_desc "$eth_line")
fi fi
local wifi_line local wifi_line
@@ -410,7 +414,7 @@ detect_network() {
fi fi
if [ -n "$wifi_line" ]; then if [ -n "$wifi_line" ]; then
WIFI_CHIPSET="$wifi_line" WIFI_CHIPSET="$wifi_line"
WIFI_DESC=$(echo "$wifi_line" | sed -E 's/^.*\]: //; s/ \[[0-9a-fA-F]{4}:[0-9a-fA-F]{4}\]//; s/ \(rev [0-9a-fA-F]+\)//') WIFI_DESC=$(_lspci_desc "$wifi_line")
fi fi
# Layer 4: USB WiFi adapter (no PCI device) # Layer 4: USB WiFi adapter (no PCI device)
if [ -z "$wifi_line" ] && command -v lsusb &>/dev/null; then if [ -z "$wifi_line" ] && command -v lsusb &>/dev/null; then
@@ -434,16 +438,16 @@ detect_network() {
state=$(echo "$line" | awk '{print $9}') state=$(echo "$line" | awk '{print $9}')
case "$iface" in case "$iface" in
eth* | enp* | ens* | enx* | eno*) eth* | enp* | ens* | enx* | eno*)
ip4=$(timeout 2 ip -4 -o addr show "$iface" 2>/dev/null | awk '{print $4}') ip4=$(timeout 1 ip -4 -o addr show "$iface" 2>/dev/null | awk '{print $4}')
ETH_NAMES+=("$iface") ETH_NAMES+=("$iface")
ETH_STATES+=("$state") ETH_STATES+=("$state")
ETH_IPS+=("${ip4:-}") ETH_IPS+=("${ip4:-}")
ETH_DESCS+=("${ETH_DESC:-}") ETH_DESCS+=("${ETH_DESC:-}")
;; ;;
wl*) wl*)
ip4=$(timeout 2 ip -4 -o addr show "$iface" 2>/dev/null | awk '{print $4}') ip4=$(timeout 1 ip -4 -o addr show "$iface" 2>/dev/null | awk '{print $4}')
ssid="" ssid=""
[ "$state" = "UP" ] && ssid=$(timeout 2 iwgetid -r "$iface" 2>/dev/null || true) [ "$state" = "UP" ] && ssid=$(timeout 1 iwgetid -r "$iface" 2>/dev/null || true)
WIFI_NAMES+=("$iface") WIFI_NAMES+=("$iface")
WIFI_STATES+=("$state") WIFI_STATES+=("$state")
WIFI_IPS+=("${ip4:-}") WIFI_IPS+=("${ip4:-}")