Yanyg - Software Engineer

FlameGraph/火焰图

目录

FlameGraph(火焰图)是Brendan Gregg提出来的:

http://www.brendangregg.com/flamegraphs.html:

Flame graphs are a visualization of profiled software, allowing the most frequent code-paths to be identified quickly and accurately. They can be generated using my open source programs on github.com/brendangregg/FlameGraph, which create interactive SVGs. See the Updates section for other implementations. Recently I've been helping with d3-flame-graph.

火焰图示例如下(dd+brd+ext4写入,生成方式参见本文后面的case):

Sorry, your browser does not support SVG.

其中:

如果存在"平顶"(plateaus),则该函数可能存在问题,至少是热点:大多数时间都花费在这个函数上了。

1 Debian install and test

~$ sudo apt-get install linux-perf-4.19
~$ sudo perf top
$ sudo perf top
Samples: 4K of event 'cycles:ppp', 4000 Hz, Event count (approx.): 1996089456
Overhead  Shared Object               Symbol
  77.57%  emacs-27.1                  [.] encode_coding_object
   1.35%  emacs-27.1                  [.] assq_no_quit
   0.50%  emacs-27.1                  [.] face_attr_equal_p
   0.48%  emacs-27.1                  [.] internal_equal.part.36
   0.41%  [kernel]                    [k] ___bpf_prog_run
   0.34%  [kernel]                    [k] inode_init_always
   0.33%  emacs-27.1                  [.] CHECK_BUFFER
   0.33%  emacs-27.1                  [.] get_visually_first_element
   0.28%  emacs-27.1                  [.] exec_byte_code
   0.27%  emacs-27.1                  [.] set_buffer_internal_2
   0.26%  emacs-27.1                  [.] fast_looking_at
   0.25%  libc-2.28.so                [.] 0x0000000000082528
   0.23%  emacs-27.1                  [.] do_symval_forwarding
   0.23%  emacs-27.1                  [.] line_draw_cost.isra.30

2 CPU火焰图

2.1 test dd for block and ext4

用brd生成4GB的block device,测试裸设备:

~$ sudo modprobe brd rd_size=4000000
~$ sudo dd if=/dev/urandom of=/dev/ram0 bs=512
# in another start perf:
~$ sudo perf record -F 99 -p $(pidof dd) -g -- sleep 10

查看数据:

# Children      Self       Samples  Command  Shared Object      Symbol
# ........  ........  ............  .......  .................  .......................................
#
    93.56%     0.26%             8  dd       [kernel.kallsyms]  [k] entry_SYSCALL_64_after_hwframe
            |
             --93.30%--entry_SYSCALL_64_after_hwframe
                       |
                        --93.27%--do_syscall_64
                                  |
                                  |--82.90%--ksys_read
                                  |          |
                                  |           --82.74%--vfs_read
                                  |                     |
                                  |                      --82.06%--urandom_read
                                  |                                |
                                  |                                |--54.42%--_extract_crng
                                  |                                |          |
                                  |                                |           --34.66%--chacha20_block
                                  |                                |
                                  |                                |--18.29%--_crng_backtrack_protect
                                  |                                |          |
                                  |                                |           --18.13%--_extract_crng
                                  |                                |                     |
                                  |                                |                      --11.02%--chacha20_block
                                  |                                |
                                  |                                |--4.45%--crng_backtrack_protect
                                  |                                |
                                  |                                 --4.15%--_copy_to_user

生成火焰图: