Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

ps

procps

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)

like image 884
osgx Avatar asked Apr 20 '11 14:04

osgx


People also ask

Can ps show CPU usage?

The ps command reports a snapshot status of current processes. However, its CPU usage value isn't the real-time usage metric of the time point we execute the command. Instead, the CPU usage provided by the ps command is expressed as the percentage of time spent running during the entire lifetime of a process.

How do I find CPU usage on Linux?

You can check how your CPU is being used with the htop command. This prints out real-time information that includes tasks, threads, load average uptime and usage for each CPU. You should see a real-time display with information on how your CPU is being put to use.

How do I check my top 10 CPU usage Linux?

Use ps Command to Find Top Processes by Memory and CPU Usage ps is a Linux command-line utility with many options that helps you to display output in different formats. You can use the ps command with –sort argument to sort the output by memory and CPU usage.

Which command displays the CPU utilization report?

sar Command to Show CPU Utilization The sar tool is a utility for managing system resources. It's not limited strictly to CPU usage, but you can use the -u option to track CPU performance. The –u option tells it to display CPU usage. The 5 indicates that it should display every 5 seconds.


2 Answers

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
like image 147
Mikel Avatar answered Oct 26 '22 18:10

Mikel


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? */

like image 40
jcomeau_ictx Avatar answered Oct 26 '22 20:10

jcomeau_ictx