开发者
资源
我要评分
获取效率
正确性
完整性
易理解
在线提单
论坛求助

火焰图扫描模式使用场景

用户可以通过在命令./tiancheng之后,添加“--flamegraph”参数的方式,实现对火焰图SVG文件中热点函数的扫描和分析。推荐用户优先使用火焰图扫描模式进行分析。

前提条件

以天秤源码优化工具安装路径“/home/tiancheng-x.x.x-Linux-Kunpeng”为例。

火焰图扫描使用示例

  1. 解析火焰图SVG文件,对Top3的热点函数进行扫描并输出分析报告。此处以扫描“/home/demo”路径下的hotspot.svg文件,分析报告存放至“/home/demo”路径为例,请根据实际情况进行修改。
    ./tiancheng --flamegraph /home/demo/hotspot.svg --top=3 -I /path/to/code_project/include -o /home/demo -r /path/to/code_project

    解析“/home/demo”下的hotspot.svg文件,提取出前3个热点函数,对前3个热点函数进行扫描,同时指定项目的头文件,在“/home/demo”下生成向量化检查报告和JSON报告文件。同时指定待分析源码文件所属项目的根目录“/path/to/code_project”,避免头文件缺失。

    返回信息如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    Compile database file does not exist or is unreachable.  
    Target functions to analyze:
      - FindCompoundDictionaryMatch (Score: 14.42%)  // 提示待分析的函数为FindCompoundDictionaryMatch函数,Score表示该函数的采样占比。
      - FindLongestMatchH5.constprop.0 (Score: 12.39%)
      - FindMatchLengthWithLimit (Score: 6.13%)
    [Locator] Analyzing: FindCompoundDictionaryMatch    // 开始匹配FindCompoundDictionaryMatch函数。
      -> [Match] /path/to/code_project/enc/hash.h: 510  // 成功匹配FindCompoundDictionaryMatch函数所在的hash.h源文件,510表示该函数所在的行数。
    [Locator] Analyzing: FindLongestMatchH5
      -> [Match] /path/to/code_project/enc/hash_longest_match_inc.h: 163
    [Locator] Analyzing: FindMatchLengthWithLimit
      -> [Match] /path/to/code_project/enc/find_match_length.h: 37
    
    
    ================================================================================
    Analyzing file: /path/to/code_project/enc/find_match_length.h
    --------------------------------------------------------------------------------
    Start to recognize file: /path/to/code_project/enc/find_match_length.h
    compile_commands.json not found under: /path/to/code_project
    Recursively collecting include paths as fallback. 
    Start to scan code.
    
    
    [Match Found] Function: FindMatchLengthWithLimit at /path/to/code_project/enc/find_match_length.h:37
      [FilterAnchors] Limiting from 21 to 10 anchors
      [CallDepth] Function 'FindMatchLengthWithLimit': original anchors=21, filtered=10  disabling interprocedural analysis 
    
    
    Vectorization check task is being executed. Please wait...
    Scanning start time: 2026/05/27 16:39:20
    
    
    ================================================================================
    Analyzing file: /path/to/code_project/enc/hash.h   // 分析hash.h源文件。
    --------------------------------------------------------------------------------
    Start to recognize file: /path/to/code_project/enc/hash.h    // 开始识别hash.h源文件。
    compile_commands.json not found under: /path/to/code_project
    Recursively collecting include paths as fallback. 
    Start to scan code.   // 开始扫描代码。
    
    
    [Match Found] Function: FindCompoundDictionaryMatch at /path/to/code_project/enc/hash.h:510   // 提示FindCompoundDictionaryMatch函数匹配成功。
      [FilterAnchors] Limiting from 49 to 10 anchors    // 提示将过滤锚点数量从49个限制为10个。
      [CallDepth] Function 'FindCompoundDictionaryMatch': original anchors=49, filtered=10  disabling interprocedural analysis   // 限制调用深度,提示禁用跨函数分析。
    
    Vectorization check task is being executed. Please wait...
    Scanning start time: 2026/05/27 16:39:32
    
    
    ================================================================================
    Analyzing file: /path/to/code_project/enc/hash_longest_match_inc.h
    --------------------------------------------------------------------------------
    Start to recognize file: /path/to/code_project/enc/hash_longest_match_inc.h
    compile_commands.json not found under: /path/to/code_project
    Recursively collecting include paths as fallback. 
    Start to scan code.
    For the details information, please check:
    Successfully generated vectorization report: /home/demo/Vectorization_Source_20260527_163925_111_3056.html    // 提示向量化检查报告文件的存放路径。
    Successfully generated vectorization optimization result: /home/demo/Vectorization_Optimization_Result_20260527_163925_111_3056.json   // 提示JSON报告文件的存放路径。
    
  2. 打开生成的JSON报告文件,分析并确认修改点。
    vim /home/demo/Vectorization_Optimization_Result_20260527_163925_111_3056.json

    JSON文件详情如下:

    {
      "advisor_items": [
        {
          "opt_file": {
            "begin_line": 315,
            "description": "",
            "end_line": 317,
            "fileName": "/path/to/code_project/enc/hash.h",
            "headerFile": "#include <arm_neon.h>",
            "headerLocBegin": 11,
            "headerLocEnd": 11,
            "sample_code": "          k = 0;\n          for (; k + 16 < step; k += 16) {\n              vst1q_u8(good_for_rle + i - k - 1, vdupq_n_u8(1));\n          }\n          for (; k < step; ++k) {\n            good_for_rle[i - k - 1] = 1;\n          }"
          }
        },
        {
          "opt_file": {
            "begin_line": 348,
            "description": "",
            "end_line": 352,
            "fileName": "/path/to/code_project/enc/hash.h",
            "headerFile": "#include <arm_neon.h>",
            "headerLocBegin": 11,
            "headerLocEnd": 11,
            "sample_code": "        k = 0;\n        for (; k + 4 < stride; k += 4) {\n            vst1q_u32(counts + i - k - 1, vdupq_n_u32(count));\n        }\n        for (; k < stride; ++k) {\n          /* We don't want to change value at counts[i],\n             that is already belonging to the next stride. Thus - 1. */\n          counts[i - k - 1] = (uint32_t)count;\n        }"
          }
        }
      ],
      "code": 0,
      "recognizedPattern": 2
    }      
    表1 字段说明

    字段

    字段说明

    advisor_items

    代码优化点建议数组,里面的每一项都是一个代码优化点建议。

    opt_file

    单个优化建议的详细信息。

    begin_line

    需要优化代码的起始行号。

    示例中“315”为优化代码的起始行号。

    description

    优化建议的详细描述。

    end_line

    需要优化代码的结束行号。

    示例中“317”为优化代码的结束行号。

    fileName

    当前分析的源文件所在路径。

    示例中“/path/to/code_project/enc/hash.h”为分析的源文件所在路径。

    headerFile

    需要添加的头文件。

    示例中“#include <arm_neon.h>”表明此段代码需要添加ARM NEON的头文件,用于使用NEON指令集。

    headerLocBegin

    头文件插入的起始行号。

    • 若为-1,表示头文件在原始代码中已经包含,无需手动添加。
    • 若为正整数,表示头文件插入的起始行号。

    示例中“11”为头文件插入的起始行号。

    headerLocEnd

    头文件插入的结束行号。

    • 若为-1,表示头文件在原始代码中已经包含,无需手动添加。
    • 若为正整数,表示头文件插入的结束行号。

    示例中“11”为头文件插入的结束行号。

    sample_code

    推荐的向量化实现代码样例,可直接用于替换源文件的原始代码。

    示例中以下代码为优化后的代码样例。

     k = 0;\n          for (; k + 16 < step; k += 16) {\n              vst1q_u8(good_for_rle + i - k - 1, vdupq_n_u8(1));\n          }\n          for (; k < step; ++k) {\n            good_for_rle[i - k - 1] = 1;\n          } 

    code

    工具执行状态码。

    • 0表示工具执行成功。
    • 非零值表示异常中断

    示例中“0”表明工具执行成功。

    recognizedPattern

    需要优化的代码段数量。

    示例中“2”表明有2处代码需要优化。

  3. 根据JSON报告进行代码改写。
    1. 查看advisor_items,其中包含了所有识别到的优化建议。
    2. 对于每个优化建议,查看begin_line和end_line字段,确定需要替换的代码范围。
    3. 将sample_code字段中的向量化代码复制并替换源文件对应位置的代码(第begin_line行至第end_line行)。
    4. 检查headerLocBegin和headerLocEnd字段:
      • 如果值为-1,表示头文件已存在,无需添加。
      • 如果为正整数,需要在指定位置添加headerFile中的头文件声明。
    5. 保存修改后的文件,即可完成自动化改写。