---
title: LTO+PGO优化方法
description: "1. 使用毕昇编译器构建插桩版MySQL。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengdevps/compilation/ug-bisheng/kunpengbisheng_30_0027.html
sourcePath: /source/zh/kunpengdevps/compilation/ug-bisheng/kunpengbisheng_30_0027.html
indexId: 87ac05f2e7deef93a66787abb5c26569cb713fed28f740185b3963d1ea5a2c1d74
---
# LTO+PGO优化方法

1. 使用毕昇编译器构建插桩版MySQL。
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 compilerPath=/home/xxxx/BiSheng #设置毕昇编译器路径 readonly SHELL_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #获取当前路径 options="-O3 -mcpu=tsv110 -flto=thin -fuse-ld=lld -Wl,-q " #设置编译选项，其中“-flto=thin -fuse-ld=lld”是LTO相关的。 pgoGenOpt="-fprofile-generate=$SHELL_PATH/pgo_gen" #设置PGO generate 路径，此处设为pgo_gen pgoUseOpt="-fprofile-use=$SHELL_PATH/pgo_use" #设置PGO use路径，此处设为pgo_use cmake .. -DWITH_BOOST=$SHELL_PATH/boost \ -DCMAKE_C_COMPILER=$compilerPath/bin/clang \ -DCMAKE_CXX_COMPILER=$compilerPath/bin/clang++ -DENABLE_DOWNLOADS=1 \ -DCMAKE_BUILD_TYPE=RELEASE \ -DBUILD_CONFIG=mysql_release \ -DCMAKE_INSTALL_PREFIX=$SHELL_PATH/install \ -DCMAKE_C_FLAGS=' $options $pgoGenOpt ' \ -DCMAKE_CXX_FLAGS=' $options $pgoGenOpt ' \ -DCMAKE_EXE_LINKER_FLAGS=' $options $pgoGenOpt ' \ -DCMAKE_AR='$compilerPath/bin/llvm-ar' \ # 开启LTO时， AR and RANLIB 需要换成毕昇的 -DCMAKE_RANLIB=`$compilerPath/bin/llvm-ranlib`

2. 运行典型的benchmark，如sysbench、TPCC。运行结束、退出mysql后，会在pgo_gen目录生成profile文件。
3. 合并生成的profile，并转换成pgo use时需要的格式。
  1 $compilerPath/bin/llvm-profdata merge pgo_gen --output= pgo_use

4. 结合采集的profile文件，生成高性能的mysql二进制。
  1 2 3 4 5 6 7 8 9 10 cmake .. -DWITH_BOOST=$SHELL_PATH/boost \ -DCMAKE_C_COMPILER=$compilerPath/bin/clang -DCMAKE_CXX_COMPILER=$compilerPath/bin/clang++ -DENABLE_DOWNLOADS=1 \ -DCMAKE_BUILD_TYPE=RELEASE \ -DBUILD_CONFIG=mysql_release \ -DCMAKE_INSTALL_PREFIX=$SHELL_PATH/install \ -DCMAKE_C_FLAGS=' $options $pgoUseOpt ' \ -DCMAKE_CXX_FLAGS=' $options $pgoUseOpt ' \ -DCMAKE_EXE_LINKER_FLAGS=' $options $pgoUseOpt '\ -DCMAKE_AR='$compilerPath/bin/llvm-ar' \ # 开启LTO时， AR and RANLIB 需要换成毕昇的 -DCMAKE_RANLIB=`$compilerPath/bin/llvm-ranlib`
