ps utility in linux (procps), how to check which CPU is used

osgx picture osgx · Apr 20, 2011 · Viewed 34k times · Source

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)

Answer

Mikel picture Mikel · Apr 20, 2011

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