Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Feb 2005 14:16:51 -0600
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Nimnod <nimnod@gnu.univ.gda.pl>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: how to calculate CPU utilization?
Message-ID:  <20050212201651.GE49626@dan.emsphone.com>
In-Reply-To: <opsl23o6hf5x937e@localhost.kolonianet.pl>
References:  <opsl23o6hf5x937e@localhost.kolonianet.pl>

next in thread | previous in thread | raw e-mail | index | archive | help
In the last episode (Feb 12), Nimnod said:
> i am looking for how to accurately calculate the CPU utilization per
> process. i have looked through the top sources, and found out it's
> using the kvm lib, which isn't suitable for my purpose.

Why not?  You don't need to be setuid root to call kvm functions; when
possible they fetch their info via sysctl.  Neither top nor ps are
setuid.

> is there a way to accurately calculate %CPU based on information from
> the struct proc ? it does carry info on proc time (p_runtime),
> system, user and interrupt ticks, and previous system, user and int
> times in usec, but i just can't make the right equation to get the
> appropriate values. first, i attempted to divide the process runtime
> by system uptime, which seemed ok, but obviously this is not the way.
> could anyone please help?

You need to take two samples separated by some delay (1 or 5 seconds
maybe), then take their difference and divide by the delay.  That will
give you the cpu usage over that period.

-- 
	Dan Nelson
	dnelson@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050212201651.GE49626>