Skip to content

Commit

Permalink
load: do not attempt to infer CPU utilisation from load avgerage
Browse files Browse the repository at this point in the history
Liquidprompt's load feature currently assumes, incorrectly, that the
load average is a percentage (it isn't) that can be converted directly
to CPU utilisation (it can't).

Because the load average is a gauge reading with an essentially
unlimited ceiling, ensure the colouring function is passed an
appropriate ceiling instead of the default value of 100. This ceiling is
now set (rather arbitrarily) at 20*$LP_LOAD_THRESHOLD, meaning the
"highest" colour starts being used at 18*$LP_LOAD_THRESHOLD.

To handle the case where the actual load exceeds the
20*$LP_LOAD_THRESHOLD ceiling, the _lp_color_map() function is changed
to apply the "highest" colour (instead of no colour) whenever the
supplied value exceeds the ceiling of the scale. This change is an
alternative implementation of what has already been proposed in liquidprompt#455.

Closes liquidprompt#499, liquidprompt#530.

Signed-off-by: Tore Anderson <[email protected]>
  • Loading branch information
toreanderson committed Jun 7, 2020
1 parent 5f4aeec commit 79ef07f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ variety of Unix systems: `tput`, `grep`, `awk`, `sed`, `ps`, `who`, and `expr`.
You can configure some variables in the `~/.config/liquidpromptrc` file:

* `LP_BATTERY_THRESHOLD`, the maximal value under which the battery level is displayed
* `LP_LOAD_THRESHOLD`, the minimal value after which the load average is displayed
* `LP_LOAD_THRESHOLD`, the minimal value (centiload per cpu) after which the load average is displayed
* `LP_TEMP_THRESHOLD`, the minimal value after which the average temperature is displayed
* `LP_RUNTIME_THRESHOLD`, the minimal value after which the runtime is displayed
* `LP_PATH_LENGTH`, the maximum percentage of the screen width used to display the path
Expand Down
37 changes: 19 additions & 18 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,23 @@ _LP_SED_EXTENDED=r
# get current load
case "$LP_OS" in
Linux)
_lp_cpu_load () {
_lp_loadavg () {
local eol
read lp_cpu_load eol < /proc/loadavg
read lp_loadavg eol < /proc/loadavg
}
;;
FreeBSD|Darwin|OpenBSD)
_lp_cpu_load () {
_lp_loadavg () {
local bol eol
# If you have problems with syntax coloring due to the following
# line, do this: ln -s liquidprompt liquidprompt.bash
# and edit liquidprompt.bash
read bol lp_cpu_load eol <<<"$( LC_ALL=C sysctl -n vm.loadavg )"
read bol lp_loadavg eol <<<"$( LC_ALL=C sysctl -n vm.loadavg )"
}
;;
SunOS)
_lp_cpu_load () {
read lp_cpu_load <<<"$( LC_ALL=C uptime | sed 's/.*load average: *\([0-9.]*\).*/\1/' )"
_lp_loadavg () {
read lp_loadavg <<<"$( LC_ALL=C uptime | sed 's/.*load average: *\([0-9.]*\).*/\1/' )"
}
esac

Expand Down Expand Up @@ -1399,6 +1399,8 @@ _lp_color_map() {
scale=${2:-100}
# Transform the value to a 0..${#COLOR_MAP} scale
idx=_LP_FIRST_INDEX+100*$1/scale/${#LP_COLORMAP[*]}
# When off the scale, use the "highest" available color
(( idx < ${#LP_COLORMAP[*]} )) || idx=-1
echo -nE "${LP_COLORMAP[idx]}"
}

Expand Down Expand Up @@ -1479,21 +1481,20 @@ _lp_load_color()

(( LP_ENABLE_LOAD )) || return

local lp_cpu_load
# Get value (OS-specific) into lp_cpu_load
_lp_cpu_load
local lp_loadavg centiload
# Get value (OS-specific) into lp_loadavg
_lp_loadavg

lp_cpu_load=${lp_cpu_load/./} # Remove '.'
lp_cpu_load=${lp_cpu_load#0} # Remove leading '0'
lp_cpu_load=${lp_cpu_load#0} # Remove leading '0', again (ex: 0.09)
local -i load=${lp_cpu_load:-0}/$_lp_CPUNUM
centiload=${lp_loadavg/./} # Remove '.'
centiload=${centiload#0} # Remove leading '0'
centiload=${centiload#0} # Remove leading '0', again (ex: 0.09)
local -i centiload_per_cpu=${centiload:-0}/$_lp_CPUNUM

if (( load > LP_LOAD_THRESHOLD )); then
local ret="$(_lp_color_map $load)${LP_MARK_LOAD}"
if (( centiload_per_cpu > LP_LOAD_THRESHOLD )); then
local -i ceiling=$LP_LOAD_THRESHOLD*20
local ret="$(_lp_color_map $centiload_per_cpu $ceiling)${LP_MARK_LOAD}"

if (( LP_PERCENTS_ALWAYS )); then
ret+="${load}${_LP_PERCENT}"
fi
ret+="${lp_loadavg}"
echo -nE "${ret}${NO_COL}"
fi
}
Expand Down
13 changes: 9 additions & 4 deletions liquidpromptrc-dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
# Recommended value is 75
LP_BATTERY_THRESHOLD=75

# Display the load average when the load is above this threshold.
# Display the load average over the past 5 minutes when above this threshold.
# The value is expressed in centiload per CPU. For example, when using the
# default value of 60, the load average will be displayed if above:
# * 0.60 on a single-core machine, or
# * 1.20 on a dual-core machine, or
# * 2.40 on a quad-core machine, and so on.
# Recommended value is 60
LP_LOAD_THRESHOLD=60

Expand Down Expand Up @@ -58,9 +63,9 @@ LP_ENABLE_FQDN=0
# users)
LP_USER_ALWAYS=1

# Display the percentages of load/batteries along with their
# corresponding marks. Set to 0 to only print the colored marks.
# Defaults to 1 (display percentages)
# Display the battery percentage along with its
# corresponding mark. Set to 0 to only print the colored mark.
# Defaults to 1 (display percentage)
LP_PERCENTS_ALWAYS=1

# Use the permissions feature and display a red ':' before the prompt to show
Expand Down

0 comments on commit 79ef07f

Please sign in to comment.