ps utility in linux (procps), how to check which CPU is used [closed]
Asked Answered
H

4

13

It is about procps package, utility ps for linux.

Can it print the number of last used CPU for each process (thread)?

Update: Not a CPU Time (10 seconds), but a CPU NUMBER (CPU0,CPU5,CPU123)

Hoot answered 20/4, 2011 at 14:42 Comment(0)
P
4

which of multiple processors? it does not offer an option for that according to the manpage. but on my Debian stable system it accepts the undocumented -o cpu


after looking at the source, and the output of ps L, I believe your answer is either the cpuid or sgi_p output options, column IDs CPUID and P, respectively.
And 'cpu' should work according to this note in output.c, but it's currently tied to the 'nop' output pr_nop():

{"cpu", "CPU", pr_nop, sr_nop, 3, 0, BSD, AN|RIGHT}, /* FIXME ... HP-UX wants this as the CPU number for SMP? */

Preposition answered 20/4, 2011 at 14:49 Comment(6)
no, it doesn't. Not a CPU time, but Number of last used CPU (CPU0,CPU1,CPU2) as it is in top "f" "j" J: P = Last used cpu (SMP)Hoot
But top from same procps can.Hoot
I see that and agree. But there is no such option in ps as documented by the manpage.Preposition
aha! try -o cpu; it accepts it, but on my single-processor system only shows "-"Preposition
on my 4cpu system it is also - for all processes (Hoot
please see my latest edit, hopefully one of those options will serve your needs.Preposition
W
30

The ps(1) man page says you can use the psr field:

   psr        PSR     processor that process is currently assigned to.
$ ps -o pid,psr,comm
  PID PSR COMMAND
 7871   1 bash
 9953   3 ps

Or you can use the cpuid field, which does the same thing.

$ ps -o pid,cpuid,comm
  PID CPUID COMMAND
 7871     1 bash
10746     3 ps

The reason for two names is for compatibility with Solaris (psr) and NetBSD/OpenBSD (cpuid).

To get threads too, add the -L option (and the lwp field if you are using -o).

Without threads:

$ ps -U $USER -o pid,psr,comm | egrep 'chromi|PID' | head -4
  PID PSR COMMAND
 6457   3 chromium-browse
 6459   0 chromium-browse
 6461   2 chromium-browse

With threads:

$ ps -U $USER -L -o pid,lwp,psr,comm | egrep 'chromi|PID' | head -4
  PID   LWP PSR COMMAND
 6457  6457   3 chromium-browse
 6457  6464   1 chromium-browse
 6457  6465   2 chromium-browse

There's also an undocumented -P option, which adds psr to the normal fields:

$ ps -U $USER -LP | egrep 'chromi|PID' | head -4
  PID   LWP PSR TTY          TIME CMD
 6457  6457   3 ?        00:01:19 chromium-browse
 6457  6464   1 ?        00:00:00 chromium-browse
 6457  6465   2 ?        00:00:00 chromium-browse
Wynd answered 20/4, 2011 at 21:31 Comment(1)
There's also a -P option, which isn't documented, but saves some typing.Wynd
P
4

which of multiple processors? it does not offer an option for that according to the manpage. but on my Debian stable system it accepts the undocumented -o cpu


after looking at the source, and the output of ps L, I believe your answer is either the cpuid or sgi_p output options, column IDs CPUID and P, respectively.
And 'cpu' should work according to this note in output.c, but it's currently tied to the 'nop' output pr_nop():

{"cpu", "CPU", pr_nop, sr_nop, 3, 0, BSD, AN|RIGHT}, /* FIXME ... HP-UX wants this as the CPU number for SMP? */

Preposition answered 20/4, 2011 at 14:49 Comment(6)
no, it doesn't. Not a CPU time, but Number of last used CPU (CPU0,CPU1,CPU2) as it is in top "f" "j" J: P = Last used cpu (SMP)Hoot
But top from same procps can.Hoot
I see that and agree. But there is no such option in ps as documented by the manpage.Preposition
aha! try -o cpu; it accepts it, but on my single-processor system only shows "-"Preposition
on my 4cpu system it is also - for all processes (Hoot
please see my latest edit, hopefully one of those options will serve your needs.Preposition
O
4

Also much underrated:

mpstat -I ALL 1 | less -SR

You can add an iteration count (like mpstat 1 1 for just one iteration).

To still have color terminal output, tell mpstat:

S_COLORS=always mpstat -I ALL 1 | less -SR

enter image description here

To have it statically updating on a terminal without scrolling:

watch -wcn .5 S_COLORS=always mpstat -I ALL 1 1 

enter image description here

Olnton answered 20/4, 2011 at 21:40 Comment(6)
Is it available in old ubuntu?Hoot
It was already available in Dapper (2006) and lives in package sysstat. Haven't looked farther back thoughOlnton
For that matter, you can run top and press 1 (number 1) to see workload per CPU.Wynd
i have a 4-socket 96 core server, with mpstat and even top 1 is there a way to just print a single snapshot output that I can scroll to see everything in the window? My monitor is not big enough to resize a single window to see all 96.Industrialist
@Industrialist Just add the iteration count: mpstat -I ALL -1 1 | less -SR does the trick for me (look at me supporting random answers after 13 years :))Olnton
@Industrialist Ooh, just realized that mpstat supports terminal colors now, so S_COLORS=always mpstat -I ALL 1 1 | less -SR is even better. Updated the answerOlnton
J
3

I did it this way on Arch, it might help someone out there:

ps -C "process" -L -o pid,lwp,pcpu,cpuid,time
  • -C: select the process named "process"
  • -L: list the process threads
  • -o: specify output info
    • pid: process id
    • lwp: light weight process (thread)
    • pcpu: CPU usage (percent)
    • cpuid: CPU id
    • time: thread time (from start)
Jugurtha answered 5/9, 2018 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.