perf

cd tools/perf
sudo apt install libtraceevent-dev

## 如果有 conda 环境退出, 因为编译过程依赖了 libpython
## 要么手动加上 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<lib path>
# conda deactivate

make -j`nproc`

编译的时候会提示缺少什么依赖

20240626153915

编译之后需要手动 cp 到一个全局路径中

sudo cp perf /usr/local/bin

使用

假设有如下程序 a.c, 矩阵乘

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define N 1024

double A[N][N];
double B[N][N];
double C[N][N];


int main(){
    /* init srand */
    srand((unsigned)time(NULL));

    /* init matrix */
    for(int i = 0 ; i < N ; ++i){
        for(int j = 0 ; j < N ; ++j){
            A[i][j] = rand()/32767.0;
            B[i][j] = rand()/32767.0;
            C[i][j] = 0;
        }
    }

    /* mult matrix */
    for(int i = 0 ; i < N ; ++i){
        for(int j = 0 ; j < N ; ++j){
            for(int k = 0 ; k < N ; ++k){
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }

    return 0;
}
gcc -g a.c -o a
perf stat ./a

20240627144243

TMA 指 Topdown Microarchitecture Analysis

其他信息

从输出中可以看出:

总体而言,程序的性能受到了错误分支预测的较大影响,可能需要优化分支预测或调整代码结构以减少错误分支预测的发生.

参考

zood