* usr/lib/byobu/network:

- make network calculations drastically cheaper, by using status_freq
    rather than a file stat
This commit is contained in:
Dustin Kirkland 2011-08-15 20:45:37 -07:00
commit be7e9d7d12
2 changed files with 48 additions and 49 deletions

3
debian/changelog vendored
View file

@ -47,6 +47,9 @@ byobu (4.30) unreleased; urgency=low
- combine mem_* to memory
- decrease raid status frequency
- add trailing space to logo
* usr/lib/byobu/network:
- make network calculations drastically cheaper, by using status_freq
rather than a file stat
-- Dustin Kirkland <kirkland@ubuntu.com> Thu, 11 Aug 2011 10:31:31 -0500

View file

@ -26,11 +26,12 @@ __network_detail() {
__network() {
get_network_interface; local interface="$_RET"
local t1=0 t2=0 x1=0 x2=0
local x1=0 x2=0
status_freq network
t="$_RET"
# By default, we won't bug the user with the display of network traffic
# below NETWORK_THRESHOLD in kbps; override in $BYOBU_CONFIG_DIR/status
[ -n "$NETWORK_THRESHOLD" ] || NETWORK_THRESHOLD=20
t2=`date +%s`
OIFS=$IFS
for i in up down; do
unit="kbps"
@ -39,54 +40,49 @@ __network() {
down) symbol="$ICON_DN" ;;
esac
cache="$BYOBU_RUN_DIR/network.$i"
t1=`stat -c %Y "$cache"` 2>/dev/null || t1=0
if [ $t2 -le $t1 ]; then
rate=0
else
[ -r "$cache" ] && read x1 < "$cache" || tx1=0
IFS="$OIFS:"
while read iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed; do
if [ "$iface" = "${interface}" ]; then
[ "$i" = "up" ] && x2=${tbytes} || x2=${rbytes}
break;
fi
done < /proc/net/dev
IFS=$OIFS
echo "$x2" > "$cache"
rate=$((8*($x2 - $x1) / ($t2 - $t1) / 1024)) # in kbps
[ "$rate" -lt 0 ] && rate=0
if [ $rate -gt $NETWORK_THRESHOLD ]; then
case "$NETWORK_UNITS" in
bytes)
rate=$(($rate/8))
if [ "$rate" -gt 1048576 ]; then
fpdiv "$rate" 1048576 1
rate=${_RET}
unit="GB/s"
elif [ "$rate" -gt 1024 ]; then
fpdiv "$rate" 1024 1
rate=${_RET}
unit="MB/s"
else
unit="kB/s"
fi
;;
*)
# Default to bps
# Why 1000 and not 1024? http://en.wikipedia.org/wiki/Data_rate_units
if [ "$rate" -gt 1000000 ]; then
fpdiv "$rate" 1000000 1
rate=${_RET}
unit="Gbps"
elif [ "$rate" -gt 1000 ]; then
fpdiv "$rate" 1000 1
rate=${_RET}
unit="Mbps"
fi
;;
esac
color b m w; printf "%s%s" "$symbol" "$rate"; color -; color m w; printf "%s" "$unit"; color --
[ -r "$cache" ] && read x1 < "$cache" || tx1=0
IFS="$OIFS:"
while read iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed; do
if [ "$iface" = "${interface}" ]; then
[ "$i" = "up" ] && x2=${tbytes} || x2=${rbytes}
break;
fi
done < /proc/net/dev
IFS=$OIFS
echo "$x2" > "$cache"
rate=$((8*($x2 - $x1) / $t / 1024)) # in kbps
[ "$rate" -lt 0 ] && rate=0
if [ $rate -gt $NETWORK_THRESHOLD ]; then
case "$NETWORK_UNITS" in
bytes)
rate=$(($rate/8))
if [ "$rate" -gt 1048576 ]; then
fpdiv "$rate" 1048576 1
rate=${_RET}
unit="GB/s"
elif [ "$rate" -gt 1024 ]; then
fpdiv "$rate" 1024 1
rate=${_RET}
unit="MB/s"
else
unit="kB/s"
fi
;;
*)
# Default to bps
# Why 1000 and not 1024? http://en.wikipedia.org/wiki/Data_rate_units
if [ "$rate" -gt 1000000 ]; then
fpdiv "$rate" 1000000 1
rate=${_RET}
unit="Gbps"
elif [ "$rate" -gt 1000 ]; then
fpdiv "$rate" 1000 1
rate=${_RET}
unit="Mbps"
fi
;;
esac
color b m w; printf "%s%s" "$symbol" "$rate"; color -; color m w; printf "%s" "$unit"; color --
fi
done
}