flamegraph

Fork of flamegraph without J***S*****.
git clone git://git.laack.co/flamegraph.git
Log | Files | Refs | README

commit 14f20f50b5e5d5b28b88b14cd62869e076f47b6e
parent f857ebc94bfe2a9bfdc4f1536ebacfb7466f69ba
Author: Brendan Gregg <brendan.d.gregg@gmail.com>
Date:   Sat, 16 Feb 2019 13:21:59 -0800

Merge pull request #189 from versable/opti

Optimize FlameGraphs by utilizing CSS and modern JS
Diffstat:
Mflamegraph.pl | 270+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 133 insertions(+), 137 deletions(-)

diff --git a/flamegraph.pl b/flamegraph.pl @@ -272,27 +272,26 @@ SVG my @g_attr = map { exists $attr->{$_} ? sprintf(qq/$_="%s"/, $attr->{$_}) : () - } qw(class style onmouseover onmouseout onclick); + } qw(id class); push @g_attr, $attr->{g_extra} if $attr->{g_extra}; - $self->{svg} .= sprintf qq/<g %s>\n/, join(' ', @g_attr); - - $self->{svg} .= sprintf qq/<title>%s<\/title>/, $attr->{title} - if $attr->{title}; # should be first element within g container - if ($attr->{href}) { my @a_attr; push @a_attr, sprintf qq/xlink:href="%s"/, $attr->{href} if $attr->{href}; # default target=_top else links will open within SVG <object> push @a_attr, sprintf qq/target="%s"/, $attr->{target} || "_top"; push @a_attr, $attr->{a_extra} if $attr->{a_extra}; - $self->{svg} .= sprintf qq/<a %s>/, join(' ', @a_attr); + $self->{svg} .= sprintf qq/<a %s>\n/, join(' ', (@a_attr, @g_attr)); + } else { + $self->{svg} .= sprintf qq/<g %s>\n/, join(' ', @g_attr); } + + $self->{svg} .= sprintf qq/<title>%s<\/title>/, $attr->{title} + if $attr->{title}; # should be first element within g container } sub group_end { my ($self, $attr) = @_; - $self->{svg} .= qq/<\/a>\n/ if $attr->{href}; - $self->{svg} .= qq/<\/g>\n/; + $self->{svg} .= $attr->{href} ? qq/<\/a>\n/ : qq/<\/g>\n/; } sub filledRectangle { @@ -306,11 +305,11 @@ SVG } sub stringTTF { - my ($self, $color, $font, $size, $angle, $x, $y, $str, $loc, $extra) = @_; + my ($self, $id, $x, $y, $str, $extra) = @_; $x = sprintf "%0.2f", $x; - $loc = defined $loc ? $loc : "left"; - $extra = defined $extra ? $extra : ""; - $self->{svg} .= qq/<text text-anchor="$loc" x="$x" y="$y" font-size="$size" font-family="$font" fill="$color" $extra >$str<\/text>\n/; + $id = defined $id ? qq/id="$id"/ : ""; + $extra ||= ""; + $self->{svg} .= qq/<text $id x="$x" y="$y" $extra>$str<\/text>\n/; } sub svg { @@ -663,9 +662,8 @@ unless ($time) { # emit an error message SVG, for tools automating flamegraph use my $imageheight = $fontsize * 5; $im->header($imagewidth, $imageheight); - $im->stringTTF($im->colorAllocate(0, 0, 0), $fonttype, $fontsize + 2, - 0.0, int($imagewidth / 2), $fontsize * 2, - "ERROR: No valid input provided to flamegraph.pl.", "middle"); + $im->stringTTF(undef, int($imagewidth / 2), $fontsize * 2, + "ERROR: No valid input provided to flamegraph.pl."); print $im->svg; exit 2; } @@ -695,37 +693,71 @@ while (my ($id, $node) = each %Node) { # draw canvas, and embed interactive JavaScript program my $imageheight = (($depthmax + 1) * $frameheight) + $ypad1 + $ypad2; $imageheight += $ypad3 if $subtitletext ne ""; +my $titlesize = $fontsize + 5; my $im = SVG->new(); +my ($black, $vdgrey, $dgrey) = ( + $im->colorAllocate(0, 0, 0), + $im->colorAllocate(160, 160, 160), + $im->colorAllocate(200, 200, 200), + ); $im->header($imagewidth, $imageheight); my $inc = <<INC; -<defs > +<defs> <linearGradient id="background" y1="0" y2="1" x1="0" x2="0" > <stop stop-color="$bgcolor1" offset="5%" /> <stop stop-color="$bgcolor2" offset="95%" /> </linearGradient> </defs> <style type="text/css"> - .func_g:hover { stroke:black; stroke-width:0.5; cursor:pointer; } + text { font-family:$fonttype; font-size:${fontsize}px; fill:$black; } + #search { opacity:0.1; cursor:pointer; } + #search:hover, #search.show { opacity:1; } + #subtitle { text-anchor:middle; font-color:$vdgrey; } + #title { text-anchor:middle; font-size:${titlesize}px} + #unzoom { cursor:pointer; } + #frames > *:hover { stroke:black; stroke-width:0.5; cursor:pointer; } + .hide { display:none; } + .parent { opacity:0.5; } </style> <script type="text/ecmascript"> <![CDATA[ - var details, searchbtn, matchedtxt, svg; + "use strict"; + var details, searchbtn, unzoombtn, matchedtxt, svg, searching; function init(evt) { details = document.getElementById("details").firstChild; searchbtn = document.getElementById("search"); + unzoombtn = document.getElementById("unzoom"); matchedtxt = document.getElementById("matched"); svg = document.getElementsByTagName("svg")[0]; searching = 0; } + window.addEventListener("click", function(e) { + var target = find_group(e.target); + if (target) { + if (target.nodeName == "a") { + if (e.ctrlKey === false) return; + e.preventDefault(); + } + if (target.classList.contains("parent")) unzoom(); + zoom(target); + } + else if (e.target.id == "unzoom") unzoom(); + else if (e.target.id == "search") search_prompt(); + }, false) + // mouse-over for info - function s(node) { // show - info = g_to_text(node); - details.nodeValue = "$nametype " + info; - } - function c() { // clear - details.nodeValue = ' '; - } + // show + window.addEventListener("mouseover", function(e) { + var target = find_group(e.target); + if (target) details.nodeValue = "$nametype " + g_to_text(target); + }, false) + + // clear + window.addEventListener("mouseout", function(e) { + var target = find_group(e.target); + if (target) details.nodeValue = ' '; + }, false) // ctrl-F for search window.addEventListener("keydown",function (e) { @@ -733,26 +765,29 @@ my $inc = <<INC; e.preventDefault(); search_prompt(); } - }) + }, false) // functions - function find_child(parent, name, attr) { - var children = parent.childNodes; - for (var i=0; i<children.length;i++) { - if (children[i].tagName == name) - return (attr != undefined) ? children[i].attributes[attr].value : children[i]; - } + function find_child(node, selector) { + var children = node.querySelectorAll(selector); + if (children.length) return children[0]; return; } + function find_group(node) { + var parent = node.parentElement; + if (!parent) return; + if (parent.id == "frames") return node; + return find_group(parent); + } function orig_save(e, attr, val) { - if (e.attributes["_orig_"+attr] != undefined) return; + if (e.attributes["_orig_" + attr] != undefined) return; if (e.attributes[attr] == undefined) return; if (val == undefined) val = e.attributes[attr].value; - e.setAttribute("_orig_"+attr, val); + e.setAttribute("_orig_" + attr, val); } function orig_load(e, attr) { if (e.attributes["_orig_"+attr] == undefined) return; - e.attributes[attr].value = e.attributes["_orig_"+attr].value; + e.attributes[attr].value = e.attributes["_orig_" + attr].value; e.removeAttribute("_orig_"+attr); } function g_to_text(e) { @@ -768,12 +803,12 @@ my $inc = <<INC; function update_text(e) { var r = find_child(e, "rect"); var t = find_child(e, "text"); - var w = parseFloat(r.attributes["width"].value) -3; + var w = parseFloat(r.attributes.width.value) -3; var txt = find_child(e, "title").textContent.replace(/\\([^(]*\\)\$/,""); - t.attributes["x"].value = parseFloat(r.attributes["x"].value) +3; + t.attributes.x.value = parseFloat(r.attributes.x.value) + 3; // Smaller than this size won't fit anything - if (w < 2*$fontsize*$fontwidth) { + if (w < 2 * $fontsize * $fontwidth) { t.textContent = ""; return; } @@ -783,9 +818,9 @@ my $inc = <<INC; if (/^ *\$/.test(txt) || t.getSubStringLength(0, txt.length) < w) return; - for (var x=txt.length-2; x>0; x--) { - if (t.getSubStringLength(0, x+2) <= w) { - t.textContent = txt.substring(0,x) + ".."; + for (var x = txt.length - 2; x > 0; x--) { + if (t.getSubStringLength(0, x + 2) <= w) { + t.textContent = txt.substring(0, x) + ".."; return; } } @@ -799,104 +834,101 @@ my $inc = <<INC; orig_load(e, "width"); } if (e.childNodes == undefined) return; - for(var i=0, c=e.childNodes; i<c.length; i++) { + for (var i = 0, c = e.childNodes; i < c.length; i++) { zoom_reset(c[i]); } } function zoom_child(e, x, ratio) { if (e.attributes != undefined) { - if (e.attributes["x"] != undefined) { + if (e.attributes.x != undefined) { orig_save(e, "x"); - e.attributes["x"].value = (parseFloat(e.attributes["x"].value) - x - $xpad) * ratio + $xpad; - if(e.tagName == "text") e.attributes["x"].value = find_child(e.parentNode, "rect", "x") + 3; + e.attributes.x.value = (parseFloat(e.attributes.x.value) - x - $xpad) * ratio + $xpad; + if (e.tagName == "text") + e.attributes.x.value = find_child(e.parentNode, "rect[x]").attributes.x.value + 3; } - if (e.attributes["width"] != undefined) { + if (e.attributes.width != undefined) { orig_save(e, "width"); - e.attributes["width"].value = parseFloat(e.attributes["width"].value) * ratio; + e.attributes.width.value = parseFloat(e.attributes.width.value) * ratio; } } if (e.childNodes == undefined) return; - for(var i=0, c=e.childNodes; i<c.length; i++) { - zoom_child(c[i], x-$xpad, ratio); + for (var i = 0, c = e.childNodes; i < c.length; i++) { + zoom_child(c[i], x - $xpad, ratio); } } function zoom_parent(e) { if (e.attributes) { - if (e.attributes["x"] != undefined) { + if (e.attributes.x != undefined) { orig_save(e, "x"); - e.attributes["x"].value = $xpad; + e.attributes.x.value = $xpad; } - if (e.attributes["width"] != undefined) { + if (e.attributes.width != undefined) { orig_save(e, "width"); - e.attributes["width"].value = parseInt(svg.width.baseVal.value) - ($xpad*2); + e.attributes.width.value = parseInt(svg.width.baseVal.value) - ($xpad * 2); } } if (e.childNodes == undefined) return; - for(var i=0, c=e.childNodes; i<c.length; i++) { + for (var i = 0, c = e.childNodes; i < c.length; i++) { zoom_parent(c[i]); } } function zoom(node) { var attr = find_child(node, "rect").attributes; - var width = parseFloat(attr["width"].value); - var xmin = parseFloat(attr["x"].value); + var width = parseFloat(attr.width.value); + var xmin = parseFloat(attr.x.value); var xmax = parseFloat(xmin + width); - var ymin = parseFloat(attr["y"].value); - var ratio = (svg.width.baseVal.value - 2*$xpad) / width; + var ymin = parseFloat(attr.y.value); + var ratio = (svg.width.baseVal.value - 2 * $xpad) / width; // XXX: Workaround for JavaScript float issues (fix me) var fudge = 0.0001; - var unzoombtn = document.getElementById("unzoom"); - unzoombtn.style["opacity"] = "1.0"; + unzoombtn.classList.remove("hide"); - var el = document.getElementsByTagName("g"); - for(var i=0;i<el.length;i++){ + var el = document.getElementById("frames").children; + for (var i = 0; i < el.length; i++) { var e = el[i]; var a = find_child(e, "rect").attributes; - var ex = parseFloat(a["x"].value); - var ew = parseFloat(a["width"].value); + var ex = parseFloat(a.x.value); + var ew = parseFloat(a.width.value); + var upstack; // Is it an ancestor if ($inverted == 0) { - var upstack = parseFloat(a["y"].value) > ymin; + upstack = parseFloat(a.y.value) > ymin; } else { - var upstack = parseFloat(a["y"].value) < ymin; + upstack = parseFloat(a.y.value) < ymin; } if (upstack) { // Direct ancestor if (ex <= xmin && (ex+ew+fudge) >= xmax) { - e.style["opacity"] = "0.5"; + e.classList.add("parent"); zoom_parent(e); - e.onclick = function(e){unzoom(); zoom(this);}; update_text(e); } // not in current path else - e.style["display"] = "none"; + e.classList.add("hide"); } // Children maybe else { // no common path if (ex < xmin || ex + fudge >= xmax) { - e.style["display"] = "none"; + e.classList.add("hide"); } else { zoom_child(e, xmin, ratio); - e.onclick = function(e){zoom(this);}; update_text(e); } } } } function unzoom() { - var unzoombtn = document.getElementById("unzoom"); - unzoombtn.style["opacity"] = "0.0"; - - var el = document.getElementsByTagName("g"); - for(i=0;i<el.length;i++) { - el[i].style["display"] = "block"; - el[i].style["opacity"] = "1"; + unzoombtn.classList.add("hide"); + var el = document.getElementById("frames").children; + for(var i = 0; i < el.length; i++) { + el[i].classList.remove("parent"); + el[i].classList.remove("hide"); zoom_reset(el[i]); update_text(el[i]); } @@ -904,8 +936,8 @@ my $inc = <<INC; // search function reset_search() { - var el = document.getElementsByTagName("rect"); - for (var i=0; i < el.length; i++) { + var el = document.querySelectorAll("#frames rect"); + for (var i = 0; i < el.length; i++) { orig_load(el[i], "fill") } } @@ -919,44 +951,34 @@ my $inc = <<INC; } else { reset_search(); searching = 0; - searchbtn.style["opacity"] = "0.1"; + searchbtn.classList.remove("show"); searchbtn.firstChild.nodeValue = "Search" - matchedtxt.style["opacity"] = "0.0"; + matchedtxt.classList.add("hide"); matchedtxt.firstChild.nodeValue = "" } } function search(term) { var re = new RegExp(term); - var el = document.getElementsByTagName("g"); + var el = document.getElementById("frames").children; var matches = new Object(); var maxwidth = 0; for (var i = 0; i < el.length; i++) { var e = el[i]; - if (e.attributes["class"].value != "func_g") - continue; var func = g_to_func(e); var rect = find_child(e, "rect"); - if (rect == null) { - // the rect might be wrapped in an anchor - // if nameattr href is being used - if (rect = find_child(e, "a")) { - rect = find_child(r, "rect"); - } - } if (func == null || rect == null) continue; // Save max width. Only works as we have a root frame - var w = parseFloat(rect.attributes["width"].value); + var w = parseFloat(rect.attributes.width.value); if (w > maxwidth) maxwidth = w; if (func.match(re)) { // highlight - var x = parseFloat(rect.attributes["x"].value); + var x = parseFloat(rect.attributes.x.value); orig_save(rect, "fill"); - rect.attributes["fill"].value = - "$searchcolor"; + rect.attributes.fill.value = "$searchcolor"; // remember matches if (matches[x] == undefined) { @@ -973,8 +995,8 @@ my $inc = <<INC; if (!searching) return; - searchbtn.style["opacity"] = "1.0"; - searchbtn.firstChild.nodeValue = "Reset Search" + searchbtn.classList.add("show"); + searchbtn.firstChild.nodeValue = "Reset Search"; // calculate percent matched, excluding vertical overlap var count = 0; @@ -1004,52 +1026,29 @@ my $inc = <<INC; } } // display matched percent - matchedtxt.style["opacity"] = "1.0"; - pct = 100 * count / maxwidth; - if (pct == 100) - pct = "100" - else - pct = pct.toFixed(1) + matchedtxt.classList.remove("hide"); + var pct = 100 * count / maxwidth; + if (pct != 100) pct = pct.toFixed(1) matchedtxt.firstChild.nodeValue = "Matched: " + pct + "%"; } - function searchover(e) { - searchbtn.style["opacity"] = "1.0"; - } - function searchout(e) { - if (searching) { - searchbtn.style["opacity"] = "1.0"; - } else { - searchbtn.style["opacity"] = "0.1"; - } - } ]]> </script> INC $im->include($inc); $im->filledRectangle(0, 0, $imagewidth, $imageheight, 'url(#background)'); -my ($white, $black, $vvdgrey, $vdgrey, $dgrey) = ( - $im->colorAllocate(255, 255, 255), - $im->colorAllocate(0, 0, 0), - $im->colorAllocate(40, 40, 40), - $im->colorAllocate(160, 160, 160), - $im->colorAllocate(200, 200, 200), - ); -$im->stringTTF($black, $fonttype, $fontsize + 5, 0.0, int($imagewidth / 2), $fontsize * 2, $titletext, "middle"); -if ($subtitletext ne "") { - $im->stringTTF($vdgrey, $fonttype, $fontsize, 0.0, int($imagewidth / 2), $fontsize * 4, $subtitletext, "middle"); -} -$im->stringTTF($black, $fonttype, $fontsize, 0.0, $xpad, $imageheight - ($ypad2 / 2), " ", "", 'id="details"'); -$im->stringTTF($black, $fonttype, $fontsize, 0.0, $xpad, $fontsize * 2, - "Reset Zoom", "", 'id="unzoom" onclick="unzoom()" style="opacity:0.0;cursor:pointer"'); -$im->stringTTF($black, $fonttype, $fontsize, 0.0, $imagewidth - $xpad - 100, - $fontsize * 2, "Search", "", 'id="search" onmouseover="searchover()" onmouseout="searchout()" onclick="search_prompt()" style="opacity:0.1;cursor:pointer"'); -$im->stringTTF($black, $fonttype, $fontsize, 0.0, $imagewidth - $xpad - 100, $imageheight - ($ypad2 / 2), " ", "", 'id="matched"'); +$im->stringTTF("title", int($imagewidth / 2), $fontsize * 2, $titletext); +$im->stringTTF("subtitle", int($imagewidth / 2), $fontsize * 4, $subtitletext) if $subtitletext ne ""; +$im->stringTTF("details", $xpad, $imageheight - ($ypad2 / 2), " "); +$im->stringTTF("unzoom", $xpad, $fontsize * 2, "Reset Zoom", 'class="hide"'); +$im->stringTTF("search", $imagewidth - $xpad - 100, $fontsize * 2, "Search"); +$im->stringTTF("matched", $imagewidth - $xpad - 100, $imageheight - ($ypad2 / 2), " "); if ($palette) { read_palette(); } # draw frames +$im->group_start({id => "frames"}); while (my ($id, $node) = each %Node) { my ($func, $depth, $etime) = split ";", $id; my $stime = $node->{stime}; @@ -1095,10 +1094,6 @@ while (my ($id, $node) = each %Node) { } my $nameattr = { %{ $nameattr{$func}||{} } }; # shallow clone - $nameattr->{class} ||= "func_g"; - $nameattr->{onmouseover} ||= "s(this)"; - $nameattr->{onmouseout} ||= "c()"; - $nameattr->{onclick} ||= "zoom(this)"; $nameattr->{title} ||= $info; $im->group_start($nameattr); @@ -1126,10 +1121,11 @@ while (my ($id, $node) = each %Node) { $text =~ s/</&lt;/g; $text =~ s/>/&gt;/g; } - $im->stringTTF($black, $fonttype, $fontsize, 0.0, $x1 + 3, 3 + ($y1 + $y2) / 2, $text, ""); + $im->stringTTF(undef, $x1 + 3, 3 + ($y1 + $y2) / 2, $text); $im->group_end($nameattr); } +$im->group_end(); print $im->svg;