Date: Sat, 4 Aug 2007 08:10:13 +0100 From: "mal content" <artifact.one@googlemail.com> To: freebsd-hackers@freebsd.org Subject: CPU activity as percentage. Message-ID: <8e96a0b90708040010r3980d5ffj709e16058740955a@mail.gmail.com>
next in thread | raw e-mail | index | archive | help
Hello.
I'm trying to write a function sys_cpu_percent() that
returns the current cpu usage as a percentage. I currently
have this:
double sys_cpu_percent()
{
long cp_time[CPUSTATES];
double used;
double total;
size_t len = sizeof(cp_time);
if (sysctlbyname("kern.cp_time", cp_time, &len, 0, 0) < 0) return 0;
used = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] +
cp_time[CP_INTR];
total = cp_time[CP_USER] + cp_time[CP_NICE] + cp_time[CP_SYS] +
cp_time[CP_INTR] + cp_time[CP_IDLE];
return (used / total) * 100;
}
However the function always returns ~9%, even when
running a cpu intensive task in the background. Am I
missing something obvious here? Is there a better way
of doing this?
My system is uniprocessor, if that makes any difference.
thanks,
MC
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?8e96a0b90708040010r3980d5ffj709e16058740955a>
