C/C++语言场景

计算精度分析工具用于定位x86和鲲鹏指令引起的C/C++语言精度差异问题。

使用要求

使用示例

此处以扫描“/home/test/demo2”路径下源码文件为例,编译命令为cmake ..make,请根据实际情况进行修改。

demo2文件目录结构:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
demo2
├── benchmark.c
├── build
├── CMakeLists.txt
├── include
│   ├── complex.h
│   └── fft.h
├── lib
│   ├── complex.c
│   └── fft.c
├── README.md
└── test.c
  1. 分别在鲲鹏环境和x86环境的项目目录下运行编译插桩操作。

    1
    2
    3
    cd /home/test/demo2/build
    cmake ..
    devkit advisor precision -instrument -c make -p /home/test/demo2 -e test.c:main -d 1
    

    回显信息如下:

    Perform the instrumentation and compilation mode.
    Finish run precision check tool.
    For detail logs on the tool, see /Path_to_DevKit_CLI/advisor/logs/preccheck.log and /home/test/demo2/build/precision_20250221082942_78fd.txt
    Please check whether error information exists in above two files to ensure that the project can run properly.
    Before running the project to get the log file, please add:
      1) the libcorrect.mod file path: export LD_LIBRARY_PATH=/ /Path_to_DevKit_CLI//advisor/devkitplugins/affinity/tools/compileopt/libcruiser/gnu_lib:$LD_LIBRARY_PATH
      2) the log file path: export CRUISER_LOG=/LOGPATH/
    • 若使用的是icc编译器,请前往libcruiser仓库查看README文件,重新编译libcruiser。
    • 在执行编译插桩模式前,必须保证项目是未编译过的,否则在增量编译的情况下,编译命令获取不完整,会导致程序执行失败。
    • “/Path_to_DevKit_CLI/”:DevKit命令行工具路径。
    • -p指定项目源码路径(“/home/test1/demo2/”),-e参数指定项目源码路径下的插桩入口文件和对应的函数(test.c:main)。
    • 执行编译插桩后,会在插桩入口文件所在路径下生成同名的.bak文件(test.c.bak),会在“build”目录下生成编译数据库文件(dev_compile_commands.json)、可执行文件以及其他的文件。

  2. 在鲲鹏和x86环境下重新运行编译后的可执行文件。

    1. 根据编译器类型添加对应的libcruiser库路径。
      GCC:
      export LD_LIBRARY_PATH=/Path_to_DevKit_CLI/advisor/devkitplugins/affinity/tools/compileopt/libcruiser/gnu_lib:$LD_LIBRARY_PATH

      Clang:

      export LD_LIBRARY_PATH=/Path_to_DevKit_CLI/advisor/devkitplugins/affinity/tools/compileopt/libcruiser/bisheng_lib:$LD_LIBRARY_PATH

      icc:

      export LD_LIBRARY_PATH=/Path_to_DevKit_CLI/advisor/devkitplugins/affinity/tools/compileopt/libcruiser/user_lib:$LD_LIBRARY_PATH
    2. 添加生成日志文件路径。
      export CRUISER_LOG=/LOGPATH/

      “/LOGPATH/”:生成的日志文件存放路径,存放路径下必须为空。

    3. 运行可执行文件。
      ./test

      运行可执行文件后若有以下输出信息,表示日志文件所在路径磁盘空间已不满足5G,请清理磁盘空间。

      1
      [WARNING] The remaining space is less than 5 GB in /home/test1, please clear enough space and run the program again.
      

      test:生成的可执行文件。

      生成日志时请注意磁盘空间大小。

    4. 查看生成的日志文件,日志内容如下:
      1
      2
      3
      4
      5
      6
      7
      test.c|main|=|37|array_p[0]|2167530|886b21e841552361bf877c4e7801931e
      test.c|main|=|42|*(p.p)|2167530|2f8f10c2df86a30c87871a57a85bed5c
      test.c|main|=|44|p.p[0]|2167530|886b21e841552361bf877c4e7801931e
      test.c|main|=|48|*(p.pa[0])|2167530|2f8f10c2df86a30c87871a57a85bed5c
      test.c|main|=|50|p.pa[0][1]|2167530|2f8f10c2df86a30c87871a57a85bed5c
      test.c|main|=|59|array1|2167530|40fa79220b4a04a64e4d49b542c45ee2
      test.c|main|=|74|res|2167530|57f18dfd63d01a1223a0d88683fcca95
      

      日志文件中的每条日志分为七部分内容,以图中日志信息为例,对每部分内容进行解释。

      1:文件名。

      2:第三部分内容表达式语句所在的当前最小作用域名称,可能是函数、子例程或主程序。

      3:当插桩变量位于函数调用语句(作为函数参数)时,此处为被调用函数名;当插桩变量位于赋值语句时,此处为“=”号。

      4:行号。

      5:插桩变量名。

      6:线程ID。

      7:插桩变量值对应的HASH值。

  3. 在鲲鹏或x86平台上进行日志差异分析操作。

    将鲲鹏平台上生成的日志文件目录改名为kunpeng,x86平台上生成的日志文件目录改名为x86,将其放到其中一个平台上。
    1
    devkit advisor precision -analysis -i "/home/kunpeng;/home/x86"
    

    回显信息如下:

    Perform the logs analysis mode.
    Finish run precision check tool.
    For detail logs on the tool, see /Path_to_DevKit_CLI/advisor/logs/preccheck.log and /home/test1/demo2/build/precision_20250221083857_c23f.txt
    The analysis result is in /home/test1/demo2/build/analysis_diff_20250221083926_423d.txt

  4. 请查看3生成的txt文件,定位源码,查找精度问题。