1

The time -v command outputs % CPU utilization for a given command on Linux. How do I do this on OS X? The Linux/OS X difference is illustrated here. I would like to measure multi-core utilization across the total execution period of a short-running program, so top probably wouldn't work, as it measures/averages at particular points in time.

  • I think you can just use Activity Monitor for this. – gardenhead Jan 5 '16 at 20:04
  • No, Activity Monitor or top show resource usage in real time. If a program does not run for an extended period it will not show. Likewise these programs average over a pre-defined interval no from start to finish, such as time. – user12719 Jan 6 '16 at 21:37
  • El Capitan (mine) has a highly modified version of sar. I can't even grab the version. – Rich_F Mar 23 at 19:53
2

You man install the sysstat package and use the sar command.(https://tipstricks.itmatrix.eu/installing-sar-monitoring-tools/)

CPU Usage of ALL CPUs (sar -u)

This gives the cumulative real-time CPU usage of all CPUs. “1 3″ reports for every 1 seconds a total of 3 times. Most likely you’ll focus on the last field “%idle” to see the cpu load.

$ sar -u 1 3
 Linux 2.6.18-194.el5PAE (dev-db)        03/26/2011      _i686_  (8 CPU)

  01:27:32 PM       CPU     %user     %nice   %system   %iowait       %steal     %idle
  01:27:33 PM       all      0.00      0.00      0.00      0.00      0.00    100.00
  01:27:34 PM       all      0.25      0.00      0.25      0.00      0.00     99.50
  01:27:35 PM       all      0.75      0.00      0.25      0.00      0.00     99.00
  Average:          all      0.33      0.00      0.17      0.00      0.00       99.50

Following are few variations:

sar -u Displays CPU usage for the current day that was collected until that point.
sar -u 1 3 Displays real time CPU usage every 1 second for 3 times.
sar -u ALL Same as “sar -u” but displays additional fields.
sar -u ALL 1 3 Same as “sar -u 1 3″ but displays additional fields.
sar -u -f /var/log/sa/sa10 Displays CPU usage for the 10day of the month from the sa10 file.

CPU Usage of Individual CPU or Core (sar -P)

If you have 4 Cores on the machine and would like to see what the individual cores are doing, do the following.

“-P ALL” indicates that it should displays statistics for ALL the individual Cores.

In the following example under “CPU” column 0, 1, 2, and 3 indicates the corresponding CPU core numbers.

  $ sar -P ALL 1 1
  Linux 2.6.18-194.el5PAE (dev-db)        03/26/2011      _i686_  (8 CPU)

  01:34:12 PM       CPU     %user     %nice   %system   %iowait    %steal     %idle
  01:34:13 PM       all     11.69      0.00      4.71      0.69      0.00     82.90
  01:34:13 PM         0     35.00      0.00      6.00      0.00      0.00     59.00
  01:34:13 PM         1     22.00      0.00      5.00      0.00      0.00     73.00
  01:34:13 PM         2      3.00      0.00      1.00      0.00      0.00     96.00
  01:34:13 PM         3      0.00      0.00      0.00      0.00      0.00    100.00

“-P 1″ indicates that it should displays statistics only for the 2nd Core. (Note that Core number starts from 0).

  $ sar -P 1 1 1
  Linux 2.6.18-194.el5PAE (dev-db)        03/26/2011      _i686_  (8 CPU)

  01:36:25 PM       CPU     %user     %nice   %system   %iowait    %steal     %idle
 01:36:26 PM         1      8.08      0.00      2.02      1.01      0.00     88.89

Following are few variations:

sar -P ALL Displays CPU usage broken down by all cores for the current day.
sar -P ALL 1 3 Displays real time CPU usage for ALL cores every 1 second for 3 times (broken down by all cores).
sar -P 1 Displays CPU usage for core number 1 for the current day.
sar -P 1 1 3 Displays real time CPU usage for core number 1, every 1 second for 3 times.
sar -P ALL -f /var/log/sa/sa10 Displays CPU usage broken down by all cores for the 10day day of the month from sa10 file.
  • Thanks for the suggestion. However, sar seems to integrate resource usage over a particular interval similar to top. Is it possible to use it similar to time, e.g. time <command>? Such that it integrates resource usage over the entire life time of a program. – user12719 Jan 6 '16 at 14:44
0

Seems like there is no real alternative to the gnu time command. So, in the end I installed just that. On OS X gnu-time can be installed with homebrew: brew install gnu-time. Thereafter CPU utilization for a specific command can be measured using gtime <command>. A test shows that my program is indeed running concurrently: 1.73user 0.13system 0:01.61elapsed 115%CPU.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.