Last active
November 24, 2024 12:06
-
-
Save fesdonomist/2adf700b4d997596fc30f623b60d70b5 to your computer and use it in GitHub Desktop.
Get readable memory information without needing sudo for dmidecode on Linux - taken from plasma/kinfocenter MR !204
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL | |
| # SPDX-FileCopyrightText: 2024 Thomas Duckworth <tduck973564@gmail.com> | |
| o="$(udevadm info -q property -p /sys/devices/virtual/dmi/id --no-pager)" | |
| # Calculate minimum width required for all output | |
| width="$(echo "$o"| grep -e "MEMORY_ARRAY" -e "MEMORY_DEVICE" | awk -F '=' ' | |
| { | |
| gsub(/MEMORY_DEVICE_[0-9]*_|MEMORY_ARRAY_/, "", $1) | |
| if (length($1) > width) { | |
| width = length($1) | |
| } | |
| } | |
| END { | |
| print width | |
| }')" | |
| section() { | |
| printf "%s:\n" "$2" | |
| echo "$1" | awk -v width="$width" -F '=' ' | |
| function pretty_bytes(x) { | |
| sizes=" B KiB MiB GiB TiB EiB PiB YiB ZiB" | |
| while (x >= 1024 && length(sizes) > 1) { | |
| x /= 1024 | |
| sizes = substr(sizes, 5) | |
| } | |
| sizes = substr(sizes, 1, 4) | |
| xf = (sizes == " B ") ? "%-1d" : "%-1.2f" | |
| return sprintf(xf"%s", x, sizes) | |
| } | |
| { | |
| # Strip whitespace from either side | |
| key = $1 | |
| gsub(/^[ \t]+|[ \t]+$/, "", key) | |
| value = $2 | |
| gsub(/^[ \t]+|[ \t]+$/, "", value) | |
| # Remove leading MEMORY_DEVICE_*_ or MEMORY_ARRAY_ from keys | |
| gsub(/MEMORY_DEVICE_[0-9]*_|MEMORY_ARRAY_/, "", key) | |
| # Replace underscores with spaces | |
| gsub(/_/, " ", key) | |
| # Capitalize first letter, lowercase rest, trim whitespace beforehand | |
| key = tolower(key) | |
| gsub(/^[ \t]+/, "", key) | |
| key = toupper(substr(key, 1, 1)) substr(key, 2); | |
| # Make keys and values more readable | |
| gsub(/ mts/, " in MT/s", key) | |
| gsub(/ id/, " ID", key) | |
| gsub(/Num /, "Number of ", key) | |
| if (key == "Present" && (value == "0" || value == "1")) { | |
| value = (value == "1") ? "Yes" : "No" | |
| } | |
| if (value ~ /^[0-9]+$/ && key == "Size" || key == "Volatile size" || key == "Data width" || key == "Max capacity" || key == "Total width") { | |
| value = pretty_bytes(value) | |
| } | |
| # Only output things that actually provide information - except for "No", which just means the slot is empty | |
| if (value != "Unknown" && value != "<OUT OF SPEC>" && value != "Not Specified" && value != "None" && value ~ /[^[:space:]]/) { | |
| dict[key] = value | |
| } | |
| } | |
| END { | |
| margin = " " | |
| for (key in dict) { | |
| printf margin key"="dict[key]"\n" | |
| } | |
| # Crude way of preserving same alignment of all sections | |
| normalizer = margin | |
| for (i = 1; i <= width; i++) { | |
| normalizer = normalizer " " | |
| } | |
| normalizer = normalizer "=" | |
| printf normalizer | |
| }' | column -t -s = | |
| } | |
| section "$(echo "$o" | grep "MEMORY_ARRAY")" "Memory array information" | |
| num_devices="$(echo "$o" | grep "MEMORY_ARRAY_NUM_DEVICES" | cut -d '=' -f2)" | |
| for i in $(seq 1 "$num_devices"); do | |
| section "$(echo "$o" | grep "MEMORY_DEVICE_$((i - 1))")" "Memory slot $i" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment