displaying total CPU usage

Konstantin Belousov kostikbel at gmail.com
Fri Oct 23 11:46:12 UTC 2020


On Fri, Oct 23, 2020 at 04:20:30AM -0700, David Wolfskill wrote:
> On Fri, Oct 23, 2020 at 01:03:30PM +0200, Wojciech Puchar wrote:
> > how to do it in batch - something like this line
> > 
> > CPU:  0.8% user,  0.0% nice,  1.3% system,  0.0% interrupt, 97.9% idle
> > 
> > from top but produced in non terminal/batch mode
> > 
> > all my attempts to do it with top failed - this line is missing
> > ....
> 
> Measuring CPU usage requires sampling the per-state counters at the
> beginning and at the end of an interval: it's thus done over a time
> period, not an "instant."
> 
> It's a bit of arthmetic, but you can get the counters via `sysctl
> kern.cp_time` (if you want per-core, use `sysctl kern.cp_times`).
> 
> The result is a (per-core array, in the latter case) array of counters:
> system, interrupt, user, nice, and idle.
> 
> Each difference will be the numerator; the sum of the differences will
> be the denominator for each state.  Multiply each fraction by 100 if you
> want percentages.

`vmstat 1' readily provides the measurements.


More information about the freebsd-hackers mailing list