Merge pull request #527 from dylanaraps/cpu_fix

CPU: Function cleanup
This commit is contained in:
Dylan Araps 2016-12-14 19:23:09 +11:00 committed by GitHub
commit 2433b79d1e
2 changed files with 52 additions and 58 deletions

View file

@ -134,11 +134,12 @@ shell_version="on"
# CPU speed type # CPU speed type
# #
# Default: 'bios' # Default: 'bios_limit'
# Values: 'current', 'min', 'max', 'bios', # Values: 'scaling_cur_freq', 'scaling_min_freq', 'scaling_max_freq', 'bios_limit'.
# Flag: --speed_type # Flag: --speed_type
# Supports: Linux with 'cpufreq' # Supports: Linux with 'cpufreq'
speed_type="bios" # NOTE: Any file in '/sys/devices/system/cpu/cpu0/cpufreq' can be used as a value.
speed_type="bios_limit"
# Shorten the output of the CPU function # Shorten the output of the CPU function
# #

103
neofetch
View file

@ -723,34 +723,25 @@ get_cpu() {
# NetBSD emulates the linux /proc filesystem instead of using sysctl for hw # NetBSD emulates the linux /proc filesystem instead of using sysctl for hw
# information so we have to use this block below which temporarily sets the # information so we have to use this block below which temporarily sets the
# OS to 'Linux' for the duration of this function. # OS to 'Linux' for the duration of this function.
case "$distro" in [[ "$distro" == "NetBSD"* ]] && local os="Linux"
"NetBSD"*) local os="Linux" ;;
esac
case "$os" in case "$os" in
"Linux" | "Windows") "Linux" | "Windows")
# Get cpu name # Get CPU name
case "$distro" in case "$distro" in
"Android"*) cpu="$(getprop ro.product.board)" ;; "Android"*) cpu="$(getprop ro.product.board)" ;;
*) cpu="$(awk -F ': | @' '/model name|Processor/ {printf $2; exit}' /proc/cpuinfo)" ;; *) cpu="$(awk -F ': | @' '/model name|Processor/ {printf $2; exit}' /proc/cpuinfo)" ;;
esac esac
speed_dir="/sys/devices/system/cpu/cpu0/cpufreq" speed_dir="/sys/devices/system/cpu/cpu0/cpufreq"
temp_dir="/sys/class/hwmon/hwmon0/temp1_input" temp_dir="/sys/class/hwmon/hwmon0/temp1_input"
# Get cpu speed # Get CPU speed
if [[ -d "$speed_dir" ]]; then if [[ -d "$speed_dir" ]]; then
case "$speed_type" in # Fallback to bios_limit if $speed_type fails.
"current") speed_type="scaling_cur_freq" ;;
"min") speed_type="scaling_min_freq" ;;
"max") speed_type="scaling_max_freq" ;;
"bios") speed_type="bios_limit" ;;
esac
# Fallback to cpuinfo_max_freq if $speed_type fails
speed="$(< "${speed_dir}/${speed_type}")" || \ speed="$(< "${speed_dir}/${speed_type}")" || \
speed="$(< "${speed_dir}/bios_limit")" || \ speed="$(< "${speed_dir}/bios_limit")" || \
speed="$(< "${speed_dir}/scaling_max_freq")" speed="$(< "${speed_dir}/scaling_max_freq")"
speed="$((speed / 100000))" speed="$((speed / 100000))"
else else
@ -758,14 +749,14 @@ get_cpu() {
speed="$((speed / 100))" speed="$((speed / 100))"
fi fi
# Get cpu temp # Get CPU temp
if [[ "$cpu_temp" == "on" && -f "$temp_dir" ]]; then if [[ "$cpu_temp" == "on" && -f "$temp_dir" ]]; then
temp="$(< "$temp_dir")" temp="$(< "$temp_dir")"
temp="$((temp * 100 / 10000))" temp="$((temp * 100 / 10000))"
temp="[${temp/${temp: -1}}.${temp: -1}°C]" temp="[${temp/${temp: -1}}.${temp: -1}°C]"
fi fi
# Show/hide hyperthreaded cores # Get CPU cores
case "$cpu_cores" in case "$cpu_cores" in
"logical" | "on") cores="$(grep -c "^processor" /proc/cpuinfo)" ;; "logical" | "on") cores="$(grep -c "^processor" /proc/cpuinfo)" ;;
"physical") cores="$(grep "^core id" /proc/cpuinfo | sort -u | wc -l)" ;; "physical") cores="$(grep "^core id" /proc/cpuinfo | sort -u | wc -l)" ;;
@ -775,7 +766,7 @@ get_cpu() {
"Mac OS X") "Mac OS X")
cpu="$(sysctl -n machdep.cpu.brand_string)" cpu="$(sysctl -n machdep.cpu.brand_string)"
# Show/hide hyperthreaded cores # Get CPU cores
case "$cpu_cores" in case "$cpu_cores" in
"logical" | "on") cores="$(sysctl -n hw.logicalcpu_max)" ;; "logical" | "on") cores="$(sysctl -n hw.logicalcpu_max)" ;;
"physical") cores="$(sysctl -n hw.physicalcpu_max)" ;; "physical") cores="$(sysctl -n hw.physicalcpu_max)" ;;
@ -810,20 +801,20 @@ get_cpu() {
;; ;;
"BSD") "BSD")
# Get cpu name # Get CPU name
cpu="$(sysctl -n hw.model)" cpu="$(sysctl -n hw.model)"
cpu="${cpu/[0-9]\.*}" cpu="${cpu/[0-9]\.*}"
cpu="${cpu/ @*}" cpu="${cpu/ @*}"
# Get cpu speed # Get CPU speed
speed="$(sysctl -n hw.cpuspeed)" speed="$(sysctl -n hw.cpuspeed)"
[[ -z "$speed" ]] && speed="$(sysctl -n hw.clockrate)" [[ -z "$speed" ]] && speed="$(sysctl -n hw.clockrate)"
speed="$((speed / 100))" speed="$((speed / 100))"
# Get cpu cores # Get CPU cores
cores="$(sysctl -n hw.ncpu)" cores="$(sysctl -n hw.ncpu)"
# Get cpu temp # Get CPU temp
if [[ "$cpu_temp" == "on" ]]; then if [[ "$cpu_temp" == "on" ]]; then
case "$kernel_name" in case "$kernel_name" in
"FreeBSD"* | "DragonFly"*) "FreeBSD"* | "DragonFly"*)
@ -839,17 +830,17 @@ get_cpu() {
;; ;;
"Solaris") "Solaris")
# Get cpuname # Get CPU name
cpu="$(psrinfo -pv)" cpu="$(psrinfo -pv)"
cpu="${cpu//*$'\n'}" cpu="${cpu//*$'\n'}"
cpu="${cpu/[0-9]\.*}" cpu="${cpu/[0-9]\.*}"
cpu="${cpu/ @*}" cpu="${cpu/ @*}"
# Get cpu speed # Get CPU speed
speed="$(psrinfo -v | awk '/operates at/ {print $6; exit}')" speed="$(psrinfo -v | awk '/operates at/ {print $6; exit}')"
speed="$((speed / 100))" speed="$((speed / 100))"
# Show/hide hyperthreaded cores # Get CPU cores
case "$cpu_cores" in case "$cpu_cores" in
"logical" | "on") cores="$(kstat -m cpu_info | grep -c -F "chip_id")" ;; "logical" | "on") cores="$(kstat -m cpu_info | grep -c -F "chip_id")" ;;
"physical") cores="$(psrinfo -p)" ;; "physical") cores="$(psrinfo -p)" ;;
@ -857,35 +848,33 @@ get_cpu() {
;; ;;
"Haiku") "Haiku")
# Get CPU name
cpu="$(sysinfo -cpu | awk -F '\\"' '/CPU #0/ {print $2}')" cpu="$(sysinfo -cpu | awk -F '\\"' '/CPU #0/ {print $2}')"
cpu="${cpu/@*}" cpu="${cpu/@*}"
# Get CPU speed
speed="$(sysinfo -cpu | awk '/running at/ {print $NF; exit}')" speed="$(sysinfo -cpu | awk '/running at/ {print $NF; exit}')"
speed="${speed/MHz}" speed="${speed/MHz}"
speed="$((speed / 100))" speed="$((speed / 100))"
# Get CPU cores
cores="$(sysinfo -cpu | grep -c -F 'CPU #')" cores="$(sysinfo -cpu | grep -c -F 'CPU #')"
;; ;;
esac esac
# Format the output # Fix for speeds under 1ghz
case "$os" in if [[ "$speed" ]]; then
"Mac OS X" | "iPhone OS") ;; if [[ -z "${speed:1}" ]]; then
*) speed="0.${speed}"
# Fix for speeds under 1ghz else
if [[ -z "${speed:1}" ]]; then speed="${speed:0:1}.${speed:1}"
speed="0.${speed}" fi
else
speed="${speed:0:1}.${speed:1}"
fi
cpu="$cpu @ ${speed}GHz $temp" cpu="$cpu @ ${speed}GHz $temp"
;; fi
esac
# Remove uneeded patterns from cpu output # Remove uneeded patterns from cpu output
# This is faster than sed/gsub
cpu="${cpu//(tm)}"
cpu="${cpu//(TM)}" cpu="${cpu//(TM)}"
cpu="${cpu//(r)}"
cpu="${cpu//(R)}" cpu="${cpu//(R)}"
cpu="${cpu//CPU}" cpu="${cpu//CPU}"
cpu="${cpu//Processor}" cpu="${cpu//Processor}"
@ -910,14 +899,11 @@ get_cpu() {
"speed") cpu="${cpu#*@ }" ;; "speed") cpu="${cpu#*@ }" ;;
"on" | "tiny") "on" | "tiny")
cpu="${cpu/Intel }"
cpu="${cpu/Core }"
cpu="${cpu/Core? Duo }"
cpu="${cpu/AMD }" cpu="${cpu/AMD }"
cpu="${cpu/Intel }"
cpu="${cpu/Core? Duo }"
case "$cpu_shorthand" in [[ "$cpu_shorthand" == "tiny" ]] && cpu="${cpu/@*}"
"tiny") cpu="${cpu/@*}" ;;
esac
;; ;;
esac esac
} }
@ -2996,18 +2982,25 @@ old_functions() {
fi fi
} }
old_flags() { old_options() {
[[ -n "$osx_buildversion" ]] && err "Config: \$osx_buildversion is deprecated, use \$distro_shorthand instead." [[ -n "$osx_buildversion" ]] && err "Config: \$osx_buildversion is deprecated, use \$distro_shorthand instead."
[[ -n "$osx_codename" ]] && err "Config: \$osx_codename is deprecated, use \$distro_shorthand instead." [[ -n "$osx_codename" ]] && err "Config: \$osx_codename is deprecated, use \$distro_shorthand instead."
[[ "$cpu_cores" == "on" ]] && err "Config: \$cpu_cores='on' is deprecated, use \$cpu_cores='logical|physical|off' instead." [[ "$cpu_cores" == "on" ]] && err "Config: cpu_cores='on' is deprecated, use cpu_cores='logical|physical|off' instead."
[[ -n "$image" ]] && { err "Config: \$image is deprecated, use \$image_source instead."; image_source="$image"; } [[ -n "$image" ]] && { err "Config: \$image is deprecated, use \$image_source instead."; image_source="$image"; }
# All progress_ variables were changed to bar_ # All progress_ variables were changed to bar_
[[ -n "$progress_char" ]] && err "Config: \$progress_char is deprecated, use \$bar_char_elapsed and \$bar_char_total instead." [[ -n "$progress_char" ]] && err "Config: \$progress_char is deprecated, use \$bar_char_elapsed and \$bar_char_total instead."
[[ -n "$progress_border" ]] && err "Config: \$progress_border is deprecated, use \$bar_border instead." [[ -n "$progress_border" ]] && err "Config: \$progress_border is deprecated, use \$bar_border instead."
[[ -n "$progress_length" ]] && err "Config: \$progress_length is deprecated, use \$bar_length instead." [[ -n "$progress_length" ]] && err "Config: \$progress_length is deprecated, use \$bar_length instead."
[[ -n "$progress_color_elapsed" ]] && err "Config: \$progress_color_elapsed is deprecated, use \$bar_color_elapsed instead." [[ -n "$progress_color_elapsed" ]] && err "Config: \$progress_color_elapsed is deprecated, use \$bar_color_elapsed instead."
[[ -n "$progress_color_total" ]] && err "Config: \$progress_color_total is deprecated, use \$bar_color_total instead." [[ -n "$progress_color_total" ]] && err "Config: \$progress_color_total is deprecated, use \$bar_color_total instead."
# All cpufreq values were changed in 2.1.0.
# 'current', 'min', 'max', 'bios'
[[ "$speed_type" == "current" ]] && err "Config: speed_type='current' is deprecated, use speed_type='scaling_cur_freq' instead."
[[ "$speed_type" == "min" ]] && err "Config: speed_type='min' is deprecated, use speed_type='scaling_min_freq' instead."
[[ "$speed_type" == "max" ]] && err "Config: speed_type='max' is deprecated, use speed_type='scaling_max_freq' instead."
[[ "$speed_type" == "bios" ]] && err "Config: speed_type='bios' is deprecated, use speed_type='bios_limit' instead."
} }
cache_uname() { cache_uname() {
@ -3332,7 +3325,7 @@ main() {
get_os get_os
get_default_config 2>/dev/null get_default_config 2>/dev/null
get_args "$@" get_args "$@"
old_flags old_options
get_distro get_distro
get_bold get_bold
get_distro_colors get_distro_colors