Philip Guenther wrote:
> On Wed, Jan 13, 2010 at 2:07 AM, Sebastian Spies
> <[email protected]> wrote:
>
>> I need a method to get the load of a process in percent of cpu time on a
>> per second basis, just like top on Linux does.
>>
>
> So grab the code in 'top' or 'ps' that calculates the process
> cumulative cpu time and run that twice, one second apart, then do the
> subtraction and express as a percentage. Depending on your philosophy
> of MP, divide by the # of CPUs. WTP?
>
>
Ok, I've done that. Let me share my current with you:
#!/bin/sh
PID=$1
SLEEPTIME=$2
if [ "$#" != 2 ]; then
echo "Usage: $0 <pid> <sleeptime>"
exit 1
fi
ps -p $PID > /dev/null || exit 2
calc()
{
CPUTIME=`ps -p $PID -o cputime | tail -n 1`
MINS=`echo $CPUTIME | sed -e 's,:.*,,'`
SECS=`echo $CPUTIME | sed -e 's,.*:,,' -e 's,\..*,,'`
HSECS=`echo $CPUTIME | sed -e 's,.*\.,,'`
return `expr $MINS \* 6000 + $SECS \* 100 + $HSECS`
}
calc
TICK1=$?
sleep $SLEEPTIME
calc
TICK2=$?
expr "(" $TICK2 - $TICK1 ")" / $SLEEPTIME
Sebastian
--
Sebastian Spies e-mail: [email protected]
DE-CIX Management GmbH
Lindleystr. 12, 60314 Frankfurt Mobile: +49 1577 7830883
Geschaeftsfuehrer Harald A. Summa Fax: +49 69 4056 2716
Registergericht AG Koeln, HRB 51135 http://www.de-cix.net
Zentrale: Lichtstr. 43i, 50825 Koeln
[demime 1.01d removed an attachment of type application/pgp-signature which had
a name of signature.asc]