Re: [wmii] Re: Adding cpu usage to status bar

2009-05-06 Thread Manuel Rotter
2009/5/6, Jesús Gabriel y Galán :
> On Tue, May 5, 2009 at 10:46 PM, Kris Maglione  wrote:

> although as  Kris pointed
> out, top might be a bit slow for a status script.

I am not having any speedproblems with top. My computer isn't
high-end, in fact he is old and it takes me less than 5 sec til he
shows me CPU Usage with top -n 2.



Re: [wmii] Re: Adding cpu usage to status bar

2009-05-06 Thread Jesús Gabriel y Galán
On Tue, May 5, 2009 at 10:46 PM, Kris Maglione  wrote:
> I've lately used iostat,

Thanks, I'll look into it.

>> To be honest, getting CPU 'usage' is pretty pointless as it's either busy,
>> idle or busy waiting on IO.  You should be using loadavg really as
>> instantaneous CPU usage tells you nothing.

I already show load averages in the status bar, but I find CPU usage
interesting information anyway.

Also thanks, Manuel, for the suggestion of the -b option for top,
although as  Kris pointed
out, top might be a bit slow for a status script.

Jesus.



Re: [wmii] Re: Adding cpu usage to status bar

2009-05-05 Thread Kris Maglione

On Tue, May 05, 2009 at 06:50:07PM +0100, Alexander Clouter wrote:

Eugh, that truly is horrible.

Have a look at /proc/stat and section 1.8 of 
Documentation/filesystems/proc.txt in the Linux kernel tree.


You're right, but I don't think /proc/stat is especially useful. 
It requires too much processing for a script. I've lately used 
iostat, because it gives me disk useage information along with 
CPU usage. The attached script is fairly arcane, but most of it 
goes towards determining the background/foreground color for a 
given usage.


To be honest, getting CPU 'usage' is pretty pointless as it's either 
busy, idle or busy waiting on IO.  You should be using loadavg really 
as instantaneous CPU usage tells you nothing.


I don't agree. I have my load averages in my bar, but I still 
generally find immediate CPU usage statistics more useful. Load 
averages are probably useful for servers, but for my 
workstations, I like to know fairly immediately when something 
starts using a lot of CPU time, and I find my brain more than 
apt at guessing just how loaded my system is based on those 
data. As for load averages, I have to wait at least 30 seconds 
before I begin to see the effect of any runaway process, and the 
value never seems to be of nearly as much use to me as the CPU 
utilization percentage.


--
Kris Maglione

You do not really understand something unless you can explain it to
your grandmother.
--Albert Einstein

#!/bin/rc
# ©2006 Kris Maglione 
#
# You may do whatever you like with this script, as long as the
# above copyright notice remains. The BSD-Lite disclaimers apply.
. 9.rc
. wmii.rc rc.ios

if(~ $1 -x)
fn wmiir {
shift; bar=$1 {
shift 4
echo $bar $*
}
}

# Set this to the update interval
interval=1

# Bar Names
bar_cpu=s3cpu
bar_io=s4io
bars=($bar_cpu $bar_io)

# End Configuration

fn sigterm sigint {
for(i in $bars $bar_time)
wmiir remove /bar/$i >[2]/dev/null
exit
}

fn m {hoc -e $"*}

for(i in ($bars $bar_time))
wmiir remove /rbar/$i >[2]/dev/null
sleep `{m $interval + 1}
for (i in $bars)
echo -n $wmiinormcol | wmiir create /rbar/$i

fn tuple {
a=$(wmii$1^col); idx=$2
h = `{echo $a($idx) | tr 'a-f' 'A-F' | sed 's/#//; s/../& /g'}
echo 16i $h^p | 9 dc
}
normbg=`{tuple norm 2}
selbg=`{tuple focus 2}
normbr=`{tuple norm 3}
selbr=`{tuple focus 3}

fn col {
n=$1; rat=$2; a=$(norm$3); b=$(sel$3); shift 3
m 'int(('$b($n) - $a($n)')' '*' $rat + $a($n)')'
}

fn cols {
val=$1; max=$2; ary=$3; shift 3
if(~ `{m $max - $val} -*)
val = $max
rat = `{m $val / $max}
printf '#%02x%02x%02x' `{col 1 $rat $ary} `{col 2 $rat $ary} `{col 3 
$rat $ary}
}

iostat -c $interval | sed -l 1d | awk 'NR%3 == 0{print;fflush()}' |
while(*=`{read}) {

us=$1; ni=$2; sy=$3; await=$4; steal=$5; idle=$6
to=`{m 'int('$us + $sy')'}

fg = '#00'

bg = `{cols $"to 100 bg}
br = `{cols $"to 100 br}

*=`{m print 'int('^($to $us $sy $ni)^'),' 0}
wmiir xwrite /rbar/$bar_cpu >[2]/dev/null \
$fg $bg $br $1:$2+$3 $4 \
|| exit

bg = `{cols $await 15 bg}
br = `{cols $await 15 br}

wmiir xwrite /rbar/$bar_io >[2]/dev/null \
$fg $bg $br $await \
|| exit
}