-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add FreeBSD support for common widgets #51
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ __cpu_freq_detail() { | |
|
||
__cpu_freq() { | ||
local hz freq count | ||
|
||
if [ -r "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" ]; then | ||
read hz < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | ||
fpdiv $hz "1000000" 1 # 1Ghz | ||
|
@@ -33,15 +34,18 @@ __cpu_freq() { | |
if egrep -q -s -i -m 1 "^cpu MHz|^clock" /proc/cpuinfo; then | ||
freq=$(egrep -i -m 1 "^cpu MHz|^clock" /proc/cpuinfo | awk -F"[:.]" '{ printf "%01.1f", $2 / 1000 }') | ||
else | ||
# Must scale frequency by number of processors, if counting bogomips | ||
count=$(getconf _NPROCESSORS_ONLN 2>/dev/null || grep -ci "^processor" /proc/cpuinfo) | ||
freq=$(egrep -i -m 1 "^bogomips" /proc/cpuinfo | awk -F"[:.]" '{ print $2 }') | ||
freq=$(printf "%s %s" "$freq" "$count" | awk '{printf "%01.1f\n", $1/$2/1000}') | ||
fi | ||
elif hz=$(sysctl -n hw.cpufrequency 2>/dev/null); then | ||
fpdiv $hz "1000000000" 1 # 1Ghz | ||
freq="$_RET" | ||
elif hz=$(sysctl -n machdep.tsc_freq 2>/dev/null); then | ||
fpdiv $hz "1000000000" 1 | ||
freq="$_RET" | ||
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary duplication of code. The same can be achieved via: elif hz=$(sysctl -n hw.cpufrequency 2>/dev/null || sysctl -n machdep.tsc_freq 2>/dev/null); then Ideally insert a comment saying which OID is available on which system. |
||
fi | ||
|
||
[ -n "$freq" ] || return | ||
color b c W; printf "%s" "$freq"; color -; color c W; printf "%s" "$ICON_GHz"; color -- | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,11 @@ | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
__disk_detail() { | ||
df -h -P | ||
if [ $(uname) = "FreeBSD" ]; then | ||
df -h | ||
else | ||
df -h -P | ||
fi | ||
Comment on lines
-23
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Granted df -Ph # order matters: with FreeBSD's version of df the -P option must be overridden by -h |
||
} | ||
|
||
__disk() { | ||
|
@@ -32,7 +36,11 @@ __disk() { | |
esac | ||
# this could be done faster with 'stat --file-system --format' | ||
# but then we'd have to do blocks -> human units ourselves | ||
out=$({ df -h -P "$MP" 2>/dev/null || df -h "$MP"; } | awk 'END { printf("%s %s", $2, $5); }') | ||
if [ $(uname) = "FreeBSD" ]; then | ||
out=$({ df -h "$MP" 2>/dev/null || df -h "$MP"; } | awk 'END { printf("%s %s", $2, $5); }') | ||
else | ||
out=$({ df -h -P "$MP" 2>/dev/null || df -h "$MP"; } | awk 'END { printf("%s %s", $2, $5); }') | ||
fi | ||
set -- ${out} | ||
size=${1}; pct=${2}; | ||
unit=${size#${size%?}} # get the unit (last char) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ __load_average_detail() { | |
__load_average() { | ||
if [ -r "/proc/loadavg" ]; then | ||
read one five fifteen other < /proc/loadavg | ||
elif [ $(uname) = "FreeBSD" ]; then | ||
one=$(uptime | sed -En 's:.*averages\: ([[:digit:]]+\.[[:digit:]]+),.*:\1:p') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To some people regular expressions may be witchcraft. Maybe we can instead write this as: sysctl -n vm.loadavg | read brace_left one five fifteen brace_right |
||
else | ||
one=$(uptime | sed -e "s/.*://" | awk '{print $1}') | ||
fi | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,16 +26,35 @@ __memory_detail() { | |
__memory() { | ||
local free="" total="" buffers="" cached="" | ||
local kb_main_used=0 buffers_plus_cached=0 fo_buffers=0 fo_cached=0 | ||
if [ -r /proc/meminfo ]; then | ||
while read tok val unit; do | ||
case "$tok" in | ||
MemTotal:) total=${val};; | ||
MemFree:) free=${val};; | ||
Buffers:) buffers=${val};; | ||
Cached:) cached=${val};; | ||
esac | ||
[ -n "${free}" -a -n "${total}" -a -n "${buffers}" -a -n "${cached}" ] && break; | ||
done < /proc/meminfo | ||
|
||
if [ $(uname) = "Linux" ]; then | ||
if [ -r /proc/meminfo ]; then | ||
while read tok val unit; do | ||
case "$tok" in | ||
MemTotal:) total=${val};; | ||
MemFree:) free=${val};; | ||
Buffers:) buffers=${val};; | ||
Cached:) cached=${val};; | ||
esac | ||
[ -n "${free}" -a -n "${total}" -a -n "${buffers}" -a -n "${cached}" ] && break; | ||
done < /proc/meminfo | ||
fi | ||
elif [ $(uname) = "FreeBSD" ]; then | ||
# FreeBSD support | ||
mem_phys=$(sysctl -n hw.physmem) | ||
page_size=$(sysctl -n hw.pagesize) | ||
mem_inactive=$(($(sysctl -n vm.stats.vm.v_inactive_count)*$page_size)) | ||
mem_cache=$(($(sysctl -n vm.stats.vm.v_cache_count)*$page_size)) | ||
mem_free=$(($(sysctl -n vm.stats.vm.v_free_count)*$page_size)) | ||
Comment on lines
+44
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you do not perform error checking of any kind, these can be grouped into one sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count | \
read mem_phys page_size mem_inactive mem_cache mem_free The factor is applied subsequently: mem_inactive=$(($mem_inactive*$page_size))
mem_cache=$(($mem_cache*$page_size))
mem_free=$(($mem_free*$page_size)) |
||
|
||
mem_avail=$(($mem_inactive+$mem_cache+$mem_free)) | ||
mem_used=$(($mem_phys-$mem_avail)) | ||
|
||
total=$(($mem_phys/1024)) | ||
free=$(($mem_avail/1024)) | ||
|
||
buffers=0 | ||
cached=0 | ||
elif eval $BYOBU_TEST vm_stat >/dev/null 2>&1; then | ||
# MacOS support | ||
# calculation borrowed from http://apple.stackexchange.com/a/48195/18857 | ||
|
@@ -44,16 +63,18 @@ __memory() { | |
speculative_blocks=$(vm_stat | grep speculative | awk '{ print $3 }' | sed -e 's/\.//') | ||
free=$((($free_blocks+speculative_blocks)*4)) | ||
inactive=$(($inactive_blocks*4)) | ||
total=$((($free+$inactive))) | ||
total=$(($free+$inactive)) | ||
buffers=0 | ||
cached=0 | ||
fi | ||
kb_main_used=$(($total-$free)) | ||
buffers_plus_cached=$(($buffers+$cached)) | ||
# "free output" buffers and cache (output from 'free') | ||
fo_buffers=$(($kb_main_used - $buffers_plus_cached)) | ||
fpdiv $((100*${fo_buffers})) "${total}" 0; | ||
usage=${_RET} | ||
|
||
kb_main_used=$(($total-$free)) | ||
buffers_plus_cached=$(($buffers+$cached)) | ||
# "free output" buffers and cache (output from 'free') | ||
fo_buffers=$(($kb_main_used - $buffers_plus_cached)) | ||
fpdiv $((100*${fo_buffers})) "${total}" 0; | ||
usage=${_RET} | ||
|
||
if [ $total -ge 1048576 ]; then | ||
fpdiv "$total" 1048567 1 | ||
total=${_RET} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,3 +172,5 @@ fi | |
color k w | ||
printf "%s" "$display_time" | ||
color -- | ||
|
||
# vi: syntax=sh ts=4 noexpandtab |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently
getconf _NPROCESSORS_ONLN
succeeds on a FreeBSD system, hence thesysctl
is never invoked (via||
list-operator).