MacOS – Get per process user and system CPU time on OSX via command line

command linemacosterminal

I am familiar with pidstat command that gives separate user and system CPU per process on Linux:

$ sudo pidstat -p 3162
Linux 4.18.0-13-generic (ubi)   26.12.2018  _x86_64_    (1 CPU)

11:26:13      UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
11:26:13     1000      3162    0.24    0.04    0.00    0.12    0.28     0  emacs

How can I accomplish the same on Mac OSX?

top on OSX gives these values but only on the machine level and not per process. For processes, it seems to give only total CPU used

Load Avg: 3.26, 3.02, 3.01  CPU usage: 3.13% user, 5.69% sys, 91.16% idle  SharedLibs: 169M resident, 44M data, 11M

PID    COMMAND      %CPU TIME     #TH   #WQ  #PORT MEM    PURG   CMPRS  PGRP  PPID  STATE    BOOSTS
97229  powerlogd    0.0  00:00.45 2     1    51    1160K  16K    1744K  97229 1     sleeping *1[1]

Per process, it shows only the total cpu percentage and not the user and system split.
How do I get both user and system time?

Best Answer

Not sure the information you are looking for is accessible so easily. You could try ps

$ ps -p 407 -o pid,time,utime,etime,comm
  PID      TIME     UTIME     ELAPSED COMM
  407   2:08.23   1:46.36 11-17:10:27 /Applications/Aquamacs.app/Contents/MacOS/Aquamacs

and calculate the system time (TIME - UTIME) and the percentages. This is just the total since the process started though, not an ongoing sampling.