Cache Collect Tool

描述

该工具用于收集程序的 L1I/L2I Cache 和 L1D/L2D Cache 信息。

  1. 从函数和指令两个角度生成热点统计数据。
  2. 报告每个进程的 L1I Cache 未命中率、L1D Cache 未命中率、L2I Cache 未命中率、L2D Cache 未命中率以及 IPC(每周期指令数)。

在 L2I Cache 收集模式下,可以将对应数据输出为 BOLT 格式的 txt 文件到工作目录。 格式如下:

1 [funcName] [offset] [number]

其中:funcName为函数名,offset为指令地址偏移量,number为采样到的次数

构建

  1. 构建 libkperf 工具:
bash build.sh
  1. 构建cache collect工具:
cd tools
bash build_tools.sh cache_collect

运行

使用 ./cache_collect --help/-h 查看帮助信息。

  用法: ./cache_collect --pid/-p <pid> [选项]

  必选参数:
    --pid/-p <pid>           : 目标进程 ID,可用 ',' 分隔多个 ID

  可选参数:
    --duration/-d <秒>       : 设置热点收集时间,单位:秒,默认:10
    --level/-l <级别>        : 设置为 'inst' 时输出指令级汇总,默认:以函数级别汇总
    --mode/-m <模式>         : 设置为 'dcache' 采集L1D/L2D Cache数据,默认:采集L1I/L2I Cache数据
    --sort/-o <排序方式>     : 设置热点表格排序方式,可选:'cycles'、'l1'、'l2',默认:cycles
    --interval/-i <毫秒>     : 读取环形缓冲区的间隔,单位:毫秒,默认:1000
    --frequency/-f <频率>    : 采样频率,默认:1000
    --bolt/-b <选项>         : 生成 BOLT 格式输出文件,仅在默认模式下使用。选项包括:'cycles'、'l2i_cache'、'l2i_cache_refill' 或 'all'。
    --summary/-s <秒>        : 设置汇总比例和 IPC 收集的时间,单位:秒,默认:5

  命令示例:
    ./cache_collect -p 125785 -d 10 -l inst -m dcache -o l1 -i 2000
    ./cache_collect -p 125785,143789 -m dcache -f 4000 -b cycles

示例

当前目录: /home/test/libkperf/tools/cache_collect/build

函数级别统计:

命令:

./cache_collect -p 1630278 -b all

输出结果:

==================================================================================================================================================================================================================
                                                                    HOTSPOT FUNC
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function                                          Pid         Start Addr        End Addr          Length            l1 icache refill    l1 icache      l2 icache refill    l2 icache      Cycles         Ratio(%)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
main.addSub                                       1630278     244e20            244f30            110               76                  144             22                 2293           13820058736    52.65
main.addSubFuse                                   1630278     244f30            245030            100               7921                2586            5                  15             12423196469    47.33
gogo                                              1630278     7b0e0             7b120             40                1428                1017            1234               32             2575901        0.01
runtime.sighandler                                1630278     5fa80             60070             5f0               56                  1741            56                 213            2568366        0.01
runtime.retake                                    1630278     572e0             575b0             2d0               0                   0               47                 738            433392         0.00
runtime.unlock2                                   1630278     1c580             1c650             d0                780                 5707            1                  31             296167         0.00
runtime.nanotime1.abi0                            1630278     7f3c0             7f4a0             e0                306                 4522            0                  1              3155           0.00
runtime.usleep.abi0                               1630278     7f150             7f1b0             60                0                   31              2                  0              34             0.00
runtime.sysmon                                    1630278     56e30             572e0             4b0               33                  1               755                0              30             0.00
....
___________________________________________________________________________________________________________________________________________________________________________________________________________________
Bolt file: /home/test/libkperf/tools/cache_collect/build/1630278_20251106_160118_cycles.txt
Bolt file: /home/test/libkperf/tools/cache_collect/build/1630278_20251106_160118_l2i_cache_refill.txt
Bolt file: /home/test/libkperf/tools/cache_collect/build/1630278_20251106_160118_l2i_cache.txt
====================================================================================================================================================================================================================
SUMMARY
-------------------------------------------------------------------------------------------------------------------------
Pid             l1 icache Miss Rate      l1 dcache Miss Rate      l2 icache Miss Rate      l2 dcache Miss Rate       IPC
-------------------------------------------------------------------------------------------------------------------------
1630278                      59.60%                   49.89%                     5.7%                    0.08%       2.71
-------------------------------------------------------------------------------------------------------------------------

Bolt文件'1630278_20251106_160118_cycles.txt'的内容:

no_lbr cycles:
1 main.addSub/1 20 5352
1 main.addSubFuse/1 20 4813
1 gogo/1 20 1 1 runtime.sighandler/1 30 1
1 runtime.retake/1 7c 2
1 runtime.unlock2/1 30 1
1 runtime.nanotime1.abi0/1 98 3
1 runtime.usleep.abi0/1 48 4
1 runtime.sysmon/1 e4 2

指令级别统计:

命令:

./cache_collect -p 1630278 -l inst -b cycles

输出结果:

===================================================================================================================================================================================
                                                                    HOTSPOT INST
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Addr                FuncName                                          Pid            l1 icache refill    l1 icache      l2 icache refill    l2 icache      Cycles         Ratio(%)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
244f50              main.addSubFuse                                   1630278        1004                1699           27                  581            4574485132     17.49
244e98              main.addSub                                       1630278        50                  0              8                   337            2237117248     8.55
244e50              main.addSub                                       1630278        0                   90             10                  443            2157851199     8.25
244e40              main.addSub                                       1630278        0                   0              3                   65             1647745701     6.30
244edc              main.addSub                                       1630278        0                   132            5                   125            1616315405     6.18
244eb0              main.addSub                                       1630278        37                  0              3                   86             1483391344     5.67
244fa4              main.addSubFuse                                   1630278        0                   64             1                   15             1343424392     5.14
244fe8              main.addSubFuse                                   1630278        0                   0              0                   30             1115313116     4.26
244fac              main.addSubFuse                                   1630278        1                   0              2                   16             900182732      3.44
244fc4              main.addSubFuse                                   1630278        42                  553            0                   1              755279597      2.89
244f88              main.addSubFuse                                   1630278        60                  1357           0                   19             714211321      2.73
244f6c              main.addSubFuse                                   1630278        93                  131            3                   15             639790055      2.45
244e74              main.addSub                                       1630278        0                   0              0                   0              565132676      2.16
...
___________________________________________________________________________________________________________________________________________________________________________________
Bolt file: /home/test/libkperf/tools/cache_collect/build/1630278_20251106_161435_cycles.txt
...

Bolt文件的内容:

no_lbr cycles:
1 main.addSubFuse/1 20 1768
1 main.addSub/1 78 869
1 main.addSub/1 30 838
1 main.addSub/1 20 641
1 main.addSub/1 bc 629
1 main.addSub/1 90 576
1 main.addSubFuse/1 74 500 
1 main.addSubFuse/1 b8 433
...