Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 23 Oct 2020 04:20:30 -0700
From:      David Wolfskill <david@catwhisker.org>
To:        Wojciech Puchar <wojtek@puchar.net>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: displaying total CPU usage
Message-ID:  <20201023112030.GF1427@albert.catwhisker.org>
In-Reply-To: <alpine.BSF.2.20.2010231301550.3469@puchar.net>
References:  <alpine.BSF.2.20.2010231301550.3469@puchar.net>

next in thread | previous in thread | raw e-mail | index | archive | help

--Lfdj09rG01n7l08B
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Oct 23, 2020 at 01:03:30PM +0200, Wojciech Puchar wrote:
> how to do it in batch - something like this line
>=20
> CPU:  0.8% user,  0.0% nice,  1.3% system,  0.0% interrupt, 97.9% idle
>=20
> from top but produced in non terminal/batch mode
>=20
> 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
--=20
David H. Wolfskill				david@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.

--Lfdj09rG01n7l08B
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQGTBAEBCgB9FiEE4owz2QxMJyaxAefyQLJg+bY2PckFAl+SvH1fFIAAAAAALgAo
aXNzdWVyLWZwckBub3RhdGlvbnMub3BlbnBncC5maWZ0aGhvcnNlbWFuLm5ldEUy
OEMzM0Q5MEM0QzI3MjZCMTAxRTdGMjQwQjI2MEY5QjYzNjNEQzkACgkQQLJg+bY2
PclYYAf/WWMkW8QNyye8u98qgrsazO6bcc8TGYZoaceGU3sD9ap2eDWejBunNdP/
+zY+ppo1V1iEoyLvy7IaENgk7KeoVTyAvOzRnQ2EoTCDHNN3yYJg8YPnD/0SVI+N
JgVmUEEbVQwmf9jreNy2gYlqK6p8NzOiHqds1A+NoLapdLWr1tJizUif7S45Co6p
QrCJOAGMVySXnyx0Mqw7jvv+Q0XLa2XBSISQwDP2g5b3+lxwUiZ64EaxeiseUKin
SI40PKiPoJPopyA5/rcdnL7GyTgLLTDA7/e+rdZVzRKcLuUCPKW4/XIuJds5pjvy
B6GRKOyjq8fpjBqoGtrOuTL0L7bFSg==
=eSJb
-----END PGP SIGNATURE-----

--Lfdj09rG01n7l08B--



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