Re: Option for machine-readable output (e.g. JSON) from top?

"Krzysztof Piecuch" <[email protected]> ("piecuch") Thu, 06 Jun 2024 08:54:10 +0000
Newsgroups gmane.linux.procps.devel
Message-ID <AcQnp6OO7s7LWakkLkkJ2EL9ikpgQZpmWldIlaD9R3gYtykCvAox_DnzF-NSc2NtOlYyvwKRiks288jWnv1NNdUDfg_DB4Wfag0DmckMirA=@kpiecuch.pl>
Hi,

As Craig mentions, you can use other programs to get all the information to=
p has in easily parsable format. To make it more palpable, here's the break=
down:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

first line:
 top - 10:43:28 up 14:23,  1 user,  load average: 1.70, 1.14, 0.87
is exactly the same as reported by uptime
 10:44:15 up 14:23,  1 user,  load average: 1.03, 1.03, 0.84

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Second line:
Tasks: 375 total,   1 running, 374 sleeping,   0 stopped,   0 zombie
is `ps`

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Third line:
%Cpu(s):  1.3 us,  1.5 sy,  0.0 ni, 95.7 id,  0.0 wa,  1.3 hi,  0.1 si,  0.=
0 st=20

is `mpstat`:

~  10:44:15 RC=3D0=C2=BB mpstat
Linux 6.6.32-1-lts (ZGROZA)     06.06.2024      _x86_64_        (8 CPU)

10:45:09     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %=
guest  %gnice   %idle
10:45:09     all    5.76    0.00    1.08    0.13    0.41    0.10    0.00   =
 0.00    0.00   92.52

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Next:
MiB Mem :  39964.4 total,  22867.2 free,  16526.1 used,   1459.2 buff/cache=
    =20
MiB Swap:      0.0 total,      0.0 free,      0.0 used.  23438.3 avail Mem=
=20

is free

~  10:45:09 RC=3D0=C2=BB free  =20
               total        used        free      shared  buff/cache   avai=
lable
Mem:        40923528    17401236    22937476      407832     1505928    235=
22292
Swap:              0           0           0

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

real-time cpu consumption is pidstat:

~  10:45:54 RC=3D0=C2=BB pidstat 1
Linux 6.6.32-1-lts (ZGROZA)     06.06.2024      _x86_64_        (8 CPU)

10:45:59      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  =
Command
10:46:00        0      1113    0.00    0.98    0.00    0.00    0.98     2  =
tailscaled
10:46:00     1001      1314    0.98    0.00    0.00    0.00    0.98     0  =
offlineimap
10:46:00     1001      1809    0.98    0.00    0.00    0.00    0.98     2  =
xmobar
10:46:00     1001      2013    0.98    0.00    0.00    0.00    0.98     1  =
wireplumber
10:46:00     1001      2014    0.98    0.00    0.00    0.00    0.98     3  =
pipewire-pulse
10:46:00     1001      2342    0.98    0.00    0.00    0.00    0.98     1  =
WebExtensions
10:46:00     1001      5780    0.98    0.00    0.00    0.00    0.98     0  =
chromium
10:46:00     1001     27280    8.82    0.00    0.00    0.00    8.82     6  =
Isolated Web Co
10:46:00     1001     28562    1.96    0.00    0.00    0.00    1.96     7  =
Isolated Web Co
10:46:00     1001     33165    1.96    0.00    0.00    0.00    1.96     0  =
strawberry
10:46:00        0     35384    0.00    0.98    0.00    0.00    0.98     1  =
kworker/u17:3-hci0

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

per-process memory info, niceness and other stuff can be obtained with `ps`=
.

Hope that helps in whatever project you're developing. These tools can usua=
lly be invoked as long-running daemons to avoid fork/exec spam every second=
. Moreover, many of these tools support machine-readable formats for easier=
 parsing, ex `mpstat -o JSON 1` will report JSON data.

Best,
Kris