42.3. 使用 SystemTap 确定内核和用户空间花费的时间

您可以使用 thread-times.stp SystemTap 脚本来确定给定线程在内核或用户空间中的花费的时间长度。

先决条件

流程

  • 运行 thread-times.stp 脚本:

    # stap --example thread-times.stp

    这个脚本会在 5 秒期间显示 CPU 时间的前 20 个进程,以及样本中进行的 CPU 选项总数。这个脚本的输出还记录每个进程使用的 CPU 时间的百分比,以及时间是在内核空间或用户空间中消耗的。

    tid   %user %kernel (of 20002 ticks)
      0   0.00%  87.88%
    32169   5.24%   0.03%
    9815   3.33%   0.36%
    9859   0.95%   0.00%
    3611   0.56%   0.12%
    9861   0.62%   0.01%
    11106   0.37%   0.02%
    32167   0.08%   0.08%
    3897   0.01%   0.08%
    3800   0.03%   0.00%
    2886   0.02%   0.00%
    3243   0.00%   0.01%
    3862   0.01%   0.00%
    3782   0.00%   0.00%
    21767   0.00%   0.00%
    2522   0.00%   0.00%
    3883   0.00%   0.00%
    3775   0.00%   0.00%
    3943   0.00%   0.00%
    3873   0.00%   0.00%