displaying total CPU usage

David Wolfskill david at catwhisker.org
Fri Oct 23 11:20:39 UTC 2020


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.

Peace,
david
-- 
David H. Wolfskill				david at catwhisker.org
My ballot status: "Your ballot has been accepted! Your ballot was
received by your county elections office and will be counted."

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 618 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20201023/b46f4949/attachment.sig>


More information about the freebsd-hackers mailing list