Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion usr/lib/byobu/cpu_count
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ __cpu_count_detail() {

__cpu_count() {
local c
c=$(getconf _NPROCESSORS_ONLN 2>/dev/null || grep -ci "^processor" /proc/cpuinfo)
c=$(getconf _NPROCESSORS_ONLN 2>/dev/null || \
grep -ci "^processor" /proc/cpuinfo || \
sysctl -n hw.ncpu)

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 the sysctl is never invoked (via || list-operator).

[ "$c" = "1" ] || printf "%sx" "$c"
}

Expand Down
6 changes: 5 additions & 1 deletion usr/lib/byobu/cpu_freq
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Choose a reason for hiding this comment

The 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 --
}
Expand Down
12 changes: 10 additions & 2 deletions usr/lib/byobu/disk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Granted ‑P has a different meaning in the FreeBSD version of df, it does not matter since ‑h renders the effects of ‑P irrelevant. There is no column indicating the size in blocks.

df -Ph  # order matters: with FreeBSD's version of df the -P option must be overridden by -h

}

__disk() {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions usr/lib/byobu/load_average
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Choose a reason for hiding this comment

The 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
Expand Down
55 changes: 38 additions & 17 deletions usr/lib/byobu/memory
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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 command:

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
Expand All @@ -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}
Expand Down
2 changes: 2 additions & 0 deletions usr/lib/byobu/time_binary
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,5 @@ fi
color k w
printf "%s" "$display_time"
color --

# vi: syntax=sh ts=4 noexpandtab
2 changes: 1 addition & 1 deletion usr/lib/byobu/trash
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ __trash() {
printf "%s[%s]" "$ICON_TRASH" "$count"
}

# vi: syntax=sh ts=4 noexpandtab
# vi: syntax=sh ts=4 noexpandtab
3 changes: 3 additions & 0 deletions usr/lib/byobu/uptime
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ __uptime() {
if [ -r /proc/uptime ]; then
read u idle < /proc/uptime
u=${u%.*}
elif [ $(uname) = "FreeBSD" ]; then
u=$(sysctl -n kern.boottime | sed -En 's:.*sec = ([[:digit:]]+),.*:\1:p')
u=$(($(date +%s) - $u))
elif [ -x /usr/sbin/sysctl ]; then
# MacOS support
u=$(/usr/sbin/sysctl -n kern.boottime | cut -f4 -d' ' | cut -d',' -f1)
Expand Down