From 59ecb3998bf7defad873a77b4322c857bf750ec7 Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Thu, 5 Feb 2026 11:42:12 -0500 Subject: [PATCH] Cache lspci output in get_storage_controllers The lspci command is now called only once on first invocation of get_storage_controllers, with results cached in LSPCI_CACHE. Subsequent calls from different layout generators (10bay, large1, micro) reuse the cached output, reducing subprocess overhead. Also added function documentation. Fixes: https://code.lotusguild.org/LotusGuild/driveAtlas/issues/17 Co-Authored-By: Claude Opus 4.5 --- driveAtlas.sh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/driveAtlas.sh b/driveAtlas.sh index a950193..37fd8b4 100644 --- a/driveAtlas.sh +++ b/driveAtlas.sh @@ -520,12 +520,29 @@ declare -A CHASSIS_TYPES=( # Core Functions #------------------------------------------------------------------------------ +# Cache for lspci output (populated on first call) +LSPCI_CACHE="" + +#------------------------------------------------------------------------------ +# get_storage_controllers +# +# Returns a formatted list of storage controllers found via lspci. +# Uses cached output if available to avoid redundant lspci calls. +# +# Output Format: " PCI_ADDR: DESCRIPTION" (one per line) +#------------------------------------------------------------------------------ get_storage_controllers() { - # Returns a formatted list of storage controllers (HBAs, SATA, NVMe) - lspci 2>/dev/null | grep -iE "SAS|SATA|RAID|Mass storage|NVMe" | while read -r line; do - pci_addr=$(echo "$line" | awk '{print $1}') + # Cache lspci output on first call + if [[ -z "$LSPCI_CACHE" ]]; then + LSPCI_CACHE="$(lspci 2>/dev/null | grep -iE "SAS|SATA|RAID|Mass storage|NVMe")" + fi + + # Format and return cached output + echo "$LSPCI_CACHE" | while read -r line; do + [[ -z "$line" ]] && continue + pci_addr="$(echo "$line" | awk '{print $1}')" # Get short description (strip PCI address) - desc=$(echo "$line" | sed 's/^[0-9a-f:.]\+ //') + desc="$(echo "$line" | sed 's/^[0-9a-f:.]\+ //')" echo " $pci_addr: $desc" done }