转自:http://www.brendangregg.com/blog/2017-07-30/coloring-flamegraphs-code-type.html

I recently improved flame graph code coloring. If you're automating or implementing flame graphs, this is a small detail that may interest you. (For an intro to flame graphs, see my website and github.)

First, a confession. Code-type coloring was a regex hack that took five minutes. In late 2014 I was modifying the JDK to preserve the frame pointer so that traditional stack walkers and profilers would work (an example of the problem is here, where Java methods lack ancestry). After I fixed the frame pointer, profiling Java looked like this (SVG):

It worked! Java methods now had ancesty (stack depth), and appear as towers.

I was delighted and showed my colleagues straight away. Amer Ather, another performance engineer at Netflix, suggested I color the Java and kernel frames differently. He was only back at his desk for five minutes when I called him back (SVG):

Done. (I also stripped the extra L from Java symbols.)

My hack was the following eight lines of code:

        if (defined $type and $type eq "java") {
if ($name =~ /::/) { # C++
$type = "yellow";
} elsif ($name =~ m:/:) { # Java (match "/" in path)
$type = "green"
} else { # system
$type = "red";
}

The "java" $type is from the command line option: --color=java. The $name is the function name. Here are some sample function names:

  • Java
    io/netty/channel/nio/NioEventLoop;.run
    org/mozilla/classfile/ClassFileWriter;.addLoadConstant
  • C++JavaCalls::call_helper
    JavaThread::thread_main_inner
  • Ctcp_v4_do_rcv
    start_thread
    write

If you cast your regular expression eye over these, you'll quickly see patterns. If it contains "::" it's C++, "/" it's Java, else it's C. And that's what I coded.

It mostly worked. But I've noticed the odd case where it gets things wrong. Sometimes the profiled Java symbols use "." instead of "/" as a delimiter. Or, somehow, I have Java methods that lack any package delimiter, so were colored red. I had similar issues with JIT'd code for Node.js.

Revisiting how flame graphs for Linux perf are generated (full instructions in Java Flame Graphs):

perf record -F 49 -a -g -- sleep 30; ./jmaps
perf script | ./stackcollapse-perf.pl | grep -v cpu_idle | ./flamegraph.pl --color=java > out.svg

It's beginning with the output of perf script (later perf versions added a way to emit a folded summary directly). Here is some truncated perf script output:

java  4811 cpu-clock:
ffffffff8100122a hypercall_page ([kernel.kallsyms])
ffffffff8100aca2 check_events ([kernel.kallsyms])
ffffffff8104dffe __wake_up_sync_key ([kernel.kallsyms])
ffffffff8152f86e sock_def_readable ([kernel.kallsyms])
[...]
ffffffff81662142 system_call_fastpath ([kernel.kallsyms])
7f62aadf2f7d write (/lib/x86_64-linux-gnu/libc-2.15.so)
7f62961a5e8b Lsun/nio/ch/FileDispatcherImpl;.write0(Ljava/io/FileDescriptor;JI)I (/tmp/perf-4637.map)
7f629619dd64 Lsun/nio/ch/SocketDispatcher;.write(Ljava/io/FileDescriptor;JI)I (/tmp/perf-4637.map)
7f62961b3330 Lsun/nio/ch/IOUtil;.writeFromNativeBuffer(Ljava/io/FileDescriptor;Ljava/nio/ByteBuffer;JLsun/nio/ch/NativeDispatcher;)I (/tmp/perf-4637.map)
[...]
7f62aa3b1618 JavaThread::thread_main_inner() (/mnt/openjdk8/build/linux-x86_64-normal-server-release/jdk/lib/amd64/server/libjvm.so)
7f62aa3b186c JavaThread::run() (/mnt/openjdk8/build/linux-x86_64-normal-server-release/jdk/lib/amd64/server/libjvm.so)
7f62aa272bf2 java_start(Thread*) (/mnt/openjdk8/build/linux-x86_64-normal-server-release/jdk/lib/amd64/server/libjvm.so)
7f62aa8f2e9a start_thread (/lib/x86_64-linux-gnu/libpthread-2.15.so)

The stackcollapse-perf.pl tool plucks out the symbol name (second column) and discards everything else. But the last column – the segment printed in ( ) – provides more details for identifying code types. Eg:

  • [kernel.kallsyms]: kernel code (I could also match the addr vs the kernel base address for this)
  • /tmp/perf-PID.map: JIT'd code (Java, Node.js, ...)

This is what I made use of recently, by adding an --all option to stackcollapse-perf.pl to turn on all annotations. Annotations are inspired by the "[k]" annotations seen in perf report --stdiooutput. I append them after the function name, so tcp_sengmsg becomes tcp_sengmsg_[k], and that annotation is used and then stripped by flamegraph.pl.

Annontation suffixes:

  • _[k]: kernel
  • _[j]: JIT
  • _[i]: inlined function
  • _[w]: waker stack (for offwake or chain graphs)

Making use of both annotations and pattern matching, the "java" palette is now:

  • green: JIT (Java, Node.js, ...)
  • aqua: inlined
  • yellow: C++
  • orange: kernel
  • red: native (user-level)

If you're automating flame graphs using my original tools, you might want to consider adding --all to the normal workflow for annotations. These are currently used by the "java" and "js" palettes. Eg:

perf record -F 49 -a -g -- sleep 30; ./jmaps
perf script | ./stackcollapse-perf.pl --all | grep -v cpu_idle | ./flamegraph.pl --color=java > out.svg

If you are using a different profiler (not Linux perf), you might want to consider enhancing its stackcollapse program to have an option to turn on annotations (or I can do it next time I use them). If you are implementing your own flame graph software, you might want to add similar color hues for code types.

Finally, it should be clear that changing the hue of code based on a regex is a trivial change to flamegraph.pl. You could add custom rules to your version to highlight your team's code, for example.

Coloring Flame Graphs: Code Hues的更多相关文章

  1. New JVM Option Enables Generation of Mixed-Mode Flame Graphs

    转自 https://www.infoq.com/news/2015/08/JVM-Option-mixed-mode-profiles Java has added a new launch opt ...

  2. tunning-Instruments and Flame Graphs

    On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...

  3. Flame Graphs

    http://www.brendangregg.com/flamegraphs.html Flame graphs are a visualization of profiled software, ...

  4. 火焰图(Flame Graphs)的安装和基本用法

    火焰图(Flame Graphs) 一.概述: 火焰图(flame graph)是性能分析的利器,通过它可以快速定位性能瓶颈点. perf 命令(performance 的缩写)是 Linux 系统原 ...

  5. 使用Flame Graph进行系统性能分析

    关键词:Flame Graph.perf.perl. FlameGraph是由BrendanGregg开发的一款开源可视化性能分析工具,形象的成为火焰图. 从底向上像火苗一样逐渐变小,也反映了相互之间 ...

  6. linux perf and tracer ,java Flame Graph

    http://www.brendangregg.com/flamegraphs.html http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/rev/e826 ...

  7. 【Xamarin 跨平台机制原理剖析】

    原文:[Xamarin 跨平台机制原理剖析] [看了请推荐,推荐满100后,将发补丁地址] Xamarin项目从喊口号到现在,好几个年头了,在内地没有火起来,原因无非有三,1.授权费贵 2.贵 3.原 ...

  8. Choosing a Linux Tracer ------Brendan Gregg's Blog

    home Choosing a Linux Tracer (2015) 08 Jul 2015 Linux Tracing is Magic! A tracer is an advanced perf ...

  9. 【Xamain 跨平台机制原理剖析】

    原文:[Xamain 跨平台机制原理剖析] [看了请推荐,推荐满100后,将发补丁地址] Xamarin项目从喊口号到现在,好几个年头了,在内地没有火起来,原因无非有三,1.授权费贵 2.贵 3.原生 ...

随机推荐

  1. ASP.NET MVC 4源代码分析之怎样定位控制器

    利用少有的空余时间.具体的浏览了下ASP.NET MVC 4的源代码.照着之前的步伐继续前进(尽管博客园已经存在非常多大牛对MVC源代码分析的博客,可是从个人出发.还是希望自己可以摸索出这些). 首先 ...

  2. [Node.js] Setup Local Configuration with Node.js Applications

    Github To stop having to change configuration settings in production code and to stop secure informa ...

  3. 寒城攻略:Listo 教你用 Swift 写IOS UI 项目计算器

    之前总结过 Swift 的语言攻略,这里就不做赘述了,如今做一个实例计算器项目来介绍一下 Swift 的应用.(凝视已经全然.直接上代码) 先看一下效果图: 以下是详细的代码和解释: 分享快乐.开源中 ...

  4. 菜鸟学Java(二十二)——又一次认识泛型

    泛型是Java SE 1.5的新特性,泛型的本质是參数化类型,也就是说所操作的数据类型被指定为一个參数.这样的參数类型能够用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Java语言 ...

  5. ytu2572——猜灯谜

    题目描写叙述 A 村的元宵节灯会上有一迷题: 请猜谜 * 请猜谜 = 请边赏灯边猜 小明想,一定是每一个汉字代表一个数字,不同的汉字代表不同的数字. 请你帮小明把全部的可能的数都找出来吧. 输入 没有 ...

  6. 利用jquery将页面中所有目标为“#‘的链接改为其他URL

    有一张页面草稿,开始时为了方便,里面差不多所有的<a>都指向了"#".现在要修改为另一个地址,难道要全部修改吗?用jquery 改改就好.代码如下: <scrip ...

  7. 10.3arcmap矢量数据制作步骤

    注意:在制作之前需要点将图放到原本大小.并且保存一下不然容易造成数据丢失. 在数据制作之前需要新建一个页面,用来放需要处理的原始数据 勾选ArcMap中Customize——ArcMap Option ...

  8. codeforces 898F Hash

    F. Restoring the Expression time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路

    直接最短路板子,dij堆优化. 题干: 题目描述 贝茜在和约翰玩一个“捉迷藏”的游戏. 她正要找出所有适合她躲藏的安全牛棚.一共有N(≤N≤)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发. ...

  10. 8.19noip模拟题

      2017 8.19 NOIP模拟赛 by coolyangzc 共3道题目,时间3小时 题目名 高级打字机 不等数列 经营与开发 源文件 type.cpp/c/pas num.cpp/c/pas ...