perf: optimize chezmoi chassis and CPU detection using direct sysfs/nproc access

This commit is contained in:
2025-12-29 11:35:25 -06:00
parent 4dae357980
commit 444adedd4b
+11 -4
View File
@@ -1,5 +1,5 @@
{{- /* Detect chassis type (likely: laptop | desktop | container) */ -}} {{- /* Detect chassis type (likely: laptop | desktop | container) */ -}}
{{- /* https://www.freedesktop.org/software/systemd/man/latest/hostnamectl.html#chassis%20%5BTYPE%5D */ -}} {{- /* Chassis type mapping: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.6.0.pdf */ -}}
{{- $chassisType := "desktop" }} {{- $chassisType := "desktop" }}
{{- if eq .chezmoi.os "darwin" }} {{- if eq .chezmoi.os "darwin" }}
{{- if contains "MacBook" (output "sysctl" "-n" "hw.model") }} {{- if contains "MacBook" (output "sysctl" "-n" "hw.model") }}
@@ -8,7 +8,14 @@
{{- $chassisType = "desktop" }} {{- $chassisType = "desktop" }}
{{- end }} {{- end }}
{{- else if eq .chezmoi.os "linux" }} {{- else if eq .chezmoi.os "linux" }}
{{- $chassisType = (output "hostnamectl" "--json=short" | mustFromJson).Chassis }} {{- $chassisRaw := output "cat" "/sys/class/dmi/id/chassis_type" | trim }}
{{- if or (eq $chassisRaw "8") (eq $chassisRaw "9") (eq $chassisRaw "10") (eq $chassisRaw "14") }}
{{- $chassisType = "laptop" }}
{{- else if or (eq $chassisRaw "3") (eq $chassisRaw "4") (eq $chassisRaw "5") (eq $chassisRaw "6") (eq $chassisRaw "7") }}
{{- $chassisType = "desktop" }}
{{- else }}
{{- $chassisType = "desktop" }}
{{- end }}
{{- else if eq .chezmoi.os "windows" }} {{- else if eq .chezmoi.os "windows" }}
{{- $chassisType = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "if ((Get-CimInstance -Class Win32_Battery | Measure-Object).Count -gt 0) { Write-Output 'laptop' } else { Write-Output 'desktop' }") | trim }} {{- $chassisType = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "if ((Get-CimInstance -Class Win32_Battery | Measure-Object).Count -gt 0) { Write-Output 'laptop' } else { Write-Output 'desktop' }") | trim }}
{{- end }} {{- end }}
@@ -20,8 +27,8 @@
{{- $cpuCores = (output "sysctl" "-n" "hw.physicalcpu_max") | trim | atoi }} {{- $cpuCores = (output "sysctl" "-n" "hw.physicalcpu_max") | trim | atoi }}
{{- $cpuThreads = (output "sysctl" "-n" "hw.logicalcpu_max") | trim | atoi }} {{- $cpuThreads = (output "sysctl" "-n" "hw.logicalcpu_max") | trim | atoi }}
{{- else if eq .chezmoi.os "linux" }} {{- else if eq .chezmoi.os "linux" }}
{{- $cpuCores = (output "sh" "-c" "lscpu --online --parse | grep --invert-match '^#' | sort --field-separator=',' --key='2,4' --unique | wc --lines") | trim | atoi }} {{- $cpuThreads = (output "nproc" "--all") | trim | atoi }}
{{- $cpuThreads = (output "sh" "-c" "lscpu --online --parse | grep --invert-match '^#' | wc --lines") | trim | atoi }} {{- $cpuCores = (output "sh" "-c" "lscpu | grep 'Core(s) per socket' | awk '{print $4}'") | trim | atoi }}
{{- else if eq .chezmoi.os "windows" }} {{- else if eq .chezmoi.os "windows" }}
{{- $cpuCores = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfCores") | trim | atoi }} {{- $cpuCores = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfCores") | trim | atoi }}
{{- $cpuThreads = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfLogicalProcessors") | trim | atoi }} {{- $cpuThreads = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfLogicalProcessors") | trim | atoi }}