【问题标题】:Measure CPU usage of multithreaded program测量多线程程序的 CPU 使用率
【发布时间】:2015-08-12 16:41:27
【问题描述】:

在一份 GCC 错误报告 (Bug 51617) 中,发布者计算了他的异步 (C++11) 程序的执行时间,输出如下所示:

/tmp/tst  81.54s user 0.23s system 628% cpu 13.001 total

张贴者可能会使用什么来提供该(或类似的)输出?

注意:检查我的 time 的 man 条目并没有发现任何有用的信息。

【问题讨论】:

  • 通常,time 是一个内置的 shell
  • @BasileStarynkevitch 确实如此——我的系统上没有与之关联的二进制文件,所以我认为它是我的 BASH 实现的一部分。我担心我在问题中包含time 会分散注意力。我真的只是想知道什么可能会产生错误报告的输出。

标签: linux multithreading performance c++11 gcc


【解决方案1】:

我的 (Ubuntu) Linux 系统上的 time(1) 手册页显示:

   -f FORMAT, --format FORMAT
          Use  FORMAT  as  the  format  string that controls the output of
          time.  See the below more information.

FORMATTING THE OUTPUT
   The format string FORMAT controls the contents of the time output.  The
   format  string can be set using the `-f' or `--format', `-v' or `--ver‐
   bose', or `-p' or `--portability' options.  If they are not given,  but
   the  TIME  environment variable is set, its value is used as the format
   string.  Otherwise, a built-in default format  is  used.   The  default
   format is:
     %Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
     %Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps

   The resource specifiers, which are a superset of  those  recognized  by
   the tcsh(1) builtin `time' command, are:
          %      A literal `%'.
          C      Name  and  command  line  arguments  of the command being
                 timed.
          D      Average size of the  process's  unshared  data  area,  in
                 Kilobytes.
          E      Elapsed  real  (wall  clock) time used by the process, in
                 [hours:]minutes:seconds.
          F      Number of major, or I/O-requiring, page faults  that  oc‐
                 curred  while  the process was running.  These are faults
                 where the page has actually migrated out of primary memo‐
                 ry.
          I      Number of file system inputs by the process.
          K      Average   total   (data+stack+text)  memory  use  of  the
                 process, in Kilobytes.
          M      Maximum resident set size of the process during its life‐
                 time, in Kilobytes.
          O      Number of file system outputs by the process.
          P      Percentage  of  the  CPU that this job got.  This is just
                 user + system times divided by the total running time. It
                 also prints a percentage sign.
          R      Number  of minor, or recoverable, page faults.  These are
                 pages that are not valid (so they fault) but  which  have
                 not  yet  been  claimed by other virtual pages.  Thus the
                 data in the page is still valid  but  the  system  tables
                 must be updated.
          S      Total  number of CPU-seconds used by the system on behalf
                 of the process (in kernel mode), in seconds.
          U      Total number of CPU-seconds that the process used direct‐
                 ly (in user mode), in seconds.
          W      Number of times the process was swapped out of main memo‐
                 ry.
          X      Average amount of shared text in the  process,  in  Kilo‐
                 bytes.
          Z      System's  page size, in bytes.  This is a per-system con‐
                 stant, but varies between systems.
          c      Number of times the process was context-switched involun‐
                 tarily (because the time slice expired).
          e      Elapsed  real  (wall  clock) time used by the process, in
                 seconds.
          k      Number of signals delivered to the process.
          p      Average unshared stack size of the process, in Kilobytes.
          r      Number of socket messages received by the process.
          s      Number of socket messages sent by the process.
          t      Average resident set size of the process, in Kilobytes.
          w      Number of times that  the  program  was  context-switched
                 voluntarily, for instance while waiting for an I/O opera‐
                 tion to complete.
          x      Exit status of the command.

所以你可以得到%P格式的CPU百分比。

请注意,这是针对 /usr/bin/time 二进制文件的 -- 内置的 shell time 通常是不同的(并且功能较弱)

【讨论】:

  • 我必须安装time 的二进制版本。你是对的,它们是完全不同的——内置于 BASH 中的甚至没有实现 -v 选项。我放在一起模仿上面的命令行是/usr/bin/time -f "%C %Us user %Ss system %P CPU %Es total exit %x"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 2018-08-02
相关资源
最近更新 更多