Coloring Flame Graphs: Code Hues

转自: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:
Javaio/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; ./jmapsperf 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 --stdio
output. 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; ./jmapsperf 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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,260评论 0 10
  • 今天在纽瓦克机场登机回北京,结果飞机晚点三小时,从上午起飞推迟到下午,美联航给了每个乘客10美元的午餐消费券,去指...
    一世惊鸿阅读 146评论 0 0
  • 推荐于 2017-04-13 阴虚火旺属虚火,多由精亏血少,阴液大伤,阴虚阳亢,则虚热虚火内生。一般来说,阴虚内热...
    秋舞阅读 438评论 0 1
  • 如果时间久了!你所做为他做的一切都被他当做理所应当!因为当初选择接婚的时候!你一无所有!我还是一无反顾的嫁给了你!...
    香菜小辣椒阅读 300评论 0 0
  • 壹 立秋之后的秋老虎已过完,秋意渐渐浓起来,一场秋风一波凉意。 贰 儿子开学在即,工资却迟迟未到,心里不免着急了起...
    知蕊阅读 138评论 0 0