1*288bf522SAndroid Build Coastguard Worker# 2*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2016 The Android Open Source Project 3*288bf522SAndroid Build Coastguard Worker# 4*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 5*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 6*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at 7*288bf522SAndroid Build Coastguard Worker# 8*288bf522SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 9*288bf522SAndroid Build Coastguard Worker# 10*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 11*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 12*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 14*288bf522SAndroid Build Coastguard Worker# limitations under the License. 15*288bf522SAndroid Build Coastguard Worker# 16*288bf522SAndroid Build Coastguard Worker 17*288bf522SAndroid Build Coastguard Workerimport sys 18*288bf522SAndroid Build Coastguard Worker 19*288bf522SAndroid Build Coastguard WorkerSVG_NODE_HEIGHT = 17 20*288bf522SAndroid Build Coastguard WorkerFONT_SIZE = 12 21*288bf522SAndroid Build Coastguard Worker 22*288bf522SAndroid Build Coastguard WorkerUNZOOM_NODE_ORIGIN_X = 10 23*288bf522SAndroid Build Coastguard WorkerUNZOOM_NODE_WIDTH = 80 24*288bf522SAndroid Build Coastguard WorkerINFO_NODE_ORIGIN_X = 120 25*288bf522SAndroid Build Coastguard WorkerINFO_NODE_WIDTH = 800 26*288bf522SAndroid Build Coastguard WorkerPERCENT_NODE_ORIGIN_X = 930 27*288bf522SAndroid Build Coastguard WorkerPERCENT_NODE_WIDTH = 250 28*288bf522SAndroid Build Coastguard WorkerSEARCH_NODE_ORIGIN_X = 1190 29*288bf522SAndroid Build Coastguard WorkerSEARCH_NODE_WIDTH = 80 30*288bf522SAndroid Build Coastguard WorkerRECT_TEXT_PADDING = 10 31*288bf522SAndroid Build Coastguard Worker 32*288bf522SAndroid Build Coastguard Worker 33*288bf522SAndroid Build Coastguard Workerdef hash_to_float(string): 34*288bf522SAndroid Build Coastguard Worker return hash(string) / float(sys.maxsize) 35*288bf522SAndroid Build Coastguard Worker 36*288bf522SAndroid Build Coastguard Worker 37*288bf522SAndroid Build Coastguard Workerdef get_legacy_color(method): 38*288bf522SAndroid Build Coastguard Worker r = 175 + int(50 * hash_to_float(reversed(method))) 39*288bf522SAndroid Build Coastguard Worker g = 60 + int(180 * hash_to_float(method)) 40*288bf522SAndroid Build Coastguard Worker b = 60 + int(55 * hash_to_float(reversed(method))) 41*288bf522SAndroid Build Coastguard Worker return (r, g, b) 42*288bf522SAndroid Build Coastguard Worker 43*288bf522SAndroid Build Coastguard Worker 44*288bf522SAndroid Build Coastguard Workerdef get_dso_color(method): 45*288bf522SAndroid Build Coastguard Worker r = 170 + int(80 * hash_to_float(reversed(method))) 46*288bf522SAndroid Build Coastguard Worker g = 180 + int(70 * hash_to_float((method))) 47*288bf522SAndroid Build Coastguard Worker b = 170 + int(80 * hash_to_float(reversed(method))) 48*288bf522SAndroid Build Coastguard Worker return (r, g, b) 49*288bf522SAndroid Build Coastguard Worker 50*288bf522SAndroid Build Coastguard Worker 51*288bf522SAndroid Build Coastguard Workerdef get_heat_color(callsite, total_weight): 52*288bf522SAndroid Build Coastguard Worker r = 245 + 10 * (1 - callsite.weight() / total_weight) 53*288bf522SAndroid Build Coastguard Worker g = 110 + 105 * (1 - callsite.weight() / total_weight) 54*288bf522SAndroid Build Coastguard Worker b = 100 55*288bf522SAndroid Build Coastguard Worker return (r, g, b) 56*288bf522SAndroid Build Coastguard Worker 57*288bf522SAndroid Build Coastguard Workerdef get_proper_scaled_time_string(value): 58*288bf522SAndroid Build Coastguard Worker if value >= 1e9: 59*288bf522SAndroid Build Coastguard Worker return '%.3f s' % (value / 1e9) 60*288bf522SAndroid Build Coastguard Worker if value >= 1e6: 61*288bf522SAndroid Build Coastguard Worker return '%.3f ms' % (value / 1e6) 62*288bf522SAndroid Build Coastguard Worker if value >= 1e3: 63*288bf522SAndroid Build Coastguard Worker return '%.3f us' % (value / 1e3) 64*288bf522SAndroid Build Coastguard Worker return '%.0f ns' % value 65*288bf522SAndroid Build Coastguard Worker 66*288bf522SAndroid Build Coastguard Workerdef create_svg_node(process, callsite, depth, f, total_weight, height, color_scheme, nav): 67*288bf522SAndroid Build Coastguard Worker x = float(callsite.offset) / total_weight * 100 68*288bf522SAndroid Build Coastguard Worker y = height - (depth + 1) * SVG_NODE_HEIGHT 69*288bf522SAndroid Build Coastguard Worker width = callsite.weight() / total_weight * 100 70*288bf522SAndroid Build Coastguard Worker 71*288bf522SAndroid Build Coastguard Worker method = callsite.method.replace(">", ">").replace("<", "<") 72*288bf522SAndroid Build Coastguard Worker if width <= 0: 73*288bf522SAndroid Build Coastguard Worker return 74*288bf522SAndroid Build Coastguard Worker 75*288bf522SAndroid Build Coastguard Worker if color_scheme == "dso": 76*288bf522SAndroid Build Coastguard Worker r, g, b = get_dso_color(callsite.dso) 77*288bf522SAndroid Build Coastguard Worker elif color_scheme == "legacy": 78*288bf522SAndroid Build Coastguard Worker r, g, b = get_legacy_color(method) 79*288bf522SAndroid Build Coastguard Worker else: 80*288bf522SAndroid Build Coastguard Worker r, g, b = get_heat_color(callsite, total_weight) 81*288bf522SAndroid Build Coastguard Worker 82*288bf522SAndroid Build Coastguard Worker r_border, g_border, b_border = [max(0, color - 50) for color in [r, g, b]] 83*288bf522SAndroid Build Coastguard Worker 84*288bf522SAndroid Build Coastguard Worker if process.props['trace_offcpu']: 85*288bf522SAndroid Build Coastguard Worker weight_str = get_proper_scaled_time_string(callsite.weight()) 86*288bf522SAndroid Build Coastguard Worker else: 87*288bf522SAndroid Build Coastguard Worker weight_str = "{:,}".format(int(callsite.weight())) + ' events' 88*288bf522SAndroid Build Coastguard Worker 89*288bf522SAndroid Build Coastguard Worker f.write( 90*288bf522SAndroid Build Coastguard Worker """<g id=%d class="n" onclick="zoom(this);" onmouseenter="select(this);" nav="%s"> 91*288bf522SAndroid Build Coastguard Worker <title>%s | %s (%s: %3.2f%%)</title> 92*288bf522SAndroid Build Coastguard Worker <rect x="%f%%" y="%f" ox="%f" oy="%f" width="%f%%" owidth="%f" height="15.0" 93*288bf522SAndroid Build Coastguard Worker ofill="rgb(%d,%d,%d)" fill="rgb(%d,%d,%d)" style="stroke:rgb(%d,%d,%d)"/> 94*288bf522SAndroid Build Coastguard Worker <text x="%f%%" y="%f" font-size="%d" font-family="Monospace"></text> 95*288bf522SAndroid Build Coastguard Worker </g>""" % 96*288bf522SAndroid Build Coastguard Worker (callsite.id, 97*288bf522SAndroid Build Coastguard Worker ','.join(str(x) for x in nav), 98*288bf522SAndroid Build Coastguard Worker method, 99*288bf522SAndroid Build Coastguard Worker callsite.dso, 100*288bf522SAndroid Build Coastguard Worker weight_str, 101*288bf522SAndroid Build Coastguard Worker callsite.weight() / total_weight * 100, 102*288bf522SAndroid Build Coastguard Worker x, 103*288bf522SAndroid Build Coastguard Worker y, 104*288bf522SAndroid Build Coastguard Worker x, 105*288bf522SAndroid Build Coastguard Worker y, 106*288bf522SAndroid Build Coastguard Worker width, 107*288bf522SAndroid Build Coastguard Worker width, 108*288bf522SAndroid Build Coastguard Worker r, 109*288bf522SAndroid Build Coastguard Worker g, 110*288bf522SAndroid Build Coastguard Worker b, 111*288bf522SAndroid Build Coastguard Worker r, 112*288bf522SAndroid Build Coastguard Worker g, 113*288bf522SAndroid Build Coastguard Worker b, 114*288bf522SAndroid Build Coastguard Worker r_border, 115*288bf522SAndroid Build Coastguard Worker g_border, 116*288bf522SAndroid Build Coastguard Worker b_border, 117*288bf522SAndroid Build Coastguard Worker x, 118*288bf522SAndroid Build Coastguard Worker y + 12, 119*288bf522SAndroid Build Coastguard Worker FONT_SIZE)) 120*288bf522SAndroid Build Coastguard Worker 121*288bf522SAndroid Build Coastguard Worker 122*288bf522SAndroid Build Coastguard Workerdef render_svg_nodes(process, flamegraph, depth, f, total_weight, height, color_scheme): 123*288bf522SAndroid Build Coastguard Worker for i, child in enumerate(flamegraph.children): 124*288bf522SAndroid Build Coastguard Worker # Prebuild navigation target for wasd 125*288bf522SAndroid Build Coastguard Worker 126*288bf522SAndroid Build Coastguard Worker if i == 0: 127*288bf522SAndroid Build Coastguard Worker left_index = 0 128*288bf522SAndroid Build Coastguard Worker else: 129*288bf522SAndroid Build Coastguard Worker left_index = flamegraph.children[i - 1].id 130*288bf522SAndroid Build Coastguard Worker 131*288bf522SAndroid Build Coastguard Worker if i == len(flamegraph.children) - 1: 132*288bf522SAndroid Build Coastguard Worker right_index = 0 133*288bf522SAndroid Build Coastguard Worker else: 134*288bf522SAndroid Build Coastguard Worker right_index = flamegraph.children[i + 1].id 135*288bf522SAndroid Build Coastguard Worker 136*288bf522SAndroid Build Coastguard Worker up_index = max(child.children, key=lambda x: x.weight()).id if child.children else 0 137*288bf522SAndroid Build Coastguard Worker 138*288bf522SAndroid Build Coastguard Worker # up, left, down, right 139*288bf522SAndroid Build Coastguard Worker nav = [up_index, left_index, flamegraph.id, right_index] 140*288bf522SAndroid Build Coastguard Worker 141*288bf522SAndroid Build Coastguard Worker create_svg_node(process, child, depth, f, total_weight, height, color_scheme, nav) 142*288bf522SAndroid Build Coastguard Worker # Recurse down 143*288bf522SAndroid Build Coastguard Worker render_svg_nodes(process, child, depth + 1, f, total_weight, height, color_scheme) 144*288bf522SAndroid Build Coastguard Worker 145*288bf522SAndroid Build Coastguard Worker 146*288bf522SAndroid Build Coastguard Workerdef render_search_node(f): 147*288bf522SAndroid Build Coastguard Worker f.write( 148*288bf522SAndroid Build Coastguard Worker """<rect id="search_rect" style="stroke:rgb(0,0,0);" onclick="search(this);" class="t" 149*288bf522SAndroid Build Coastguard Worker rx="10" ry="10" x="%d" y="10" width="%d" height="30" fill="rgb(255,255,255)""/> 150*288bf522SAndroid Build Coastguard Worker <text id="search_text" class="t" x="%d" y="30" onclick="search(this);">Search</text> 151*288bf522SAndroid Build Coastguard Worker """ % (SEARCH_NODE_ORIGIN_X, SEARCH_NODE_WIDTH, SEARCH_NODE_ORIGIN_X + RECT_TEXT_PADDING)) 152*288bf522SAndroid Build Coastguard Worker 153*288bf522SAndroid Build Coastguard Worker 154*288bf522SAndroid Build Coastguard Workerdef render_unzoom_node(f): 155*288bf522SAndroid Build Coastguard Worker f.write( 156*288bf522SAndroid Build Coastguard Worker """<rect id="zoom_rect" style="display:none;stroke:rgb(0,0,0);" class="t" 157*288bf522SAndroid Build Coastguard Worker onclick="unzoom(this);" rx="10" ry="10" x="%d" y="10" width="%d" height="30" 158*288bf522SAndroid Build Coastguard Worker fill="rgb(255,255,255)"/> 159*288bf522SAndroid Build Coastguard Worker <text id="zoom_text" style="display:none;" class="t" x="%d" y="30" 160*288bf522SAndroid Build Coastguard Worker onclick="unzoom(this);">Zoom out</text> 161*288bf522SAndroid Build Coastguard Worker """ % (UNZOOM_NODE_ORIGIN_X, UNZOOM_NODE_WIDTH, UNZOOM_NODE_ORIGIN_X + RECT_TEXT_PADDING)) 162*288bf522SAndroid Build Coastguard Worker 163*288bf522SAndroid Build Coastguard Worker 164*288bf522SAndroid Build Coastguard Workerdef render_info_node(f): 165*288bf522SAndroid Build Coastguard Worker f.write( 166*288bf522SAndroid Build Coastguard Worker """<clipPath id="info_clip_path"> <rect id="info_rect" style="stroke:rgb(0,0,0);" 167*288bf522SAndroid Build Coastguard Worker rx="10" ry="10" x="%d" y="10" width="%d" height="30" fill="rgb(255,255,255)"/> 168*288bf522SAndroid Build Coastguard Worker </clipPath> 169*288bf522SAndroid Build Coastguard Worker <rect id="info_rect" style="stroke:rgb(0,0,0);" 170*288bf522SAndroid Build Coastguard Worker rx="10" ry="10" x="%d" y="10" width="%d" height="30" fill="rgb(255,255,255)"/> 171*288bf522SAndroid Build Coastguard Worker <text clip-path="url(#info_clip_path)" id="info_text" x="%d" y="30"></text> 172*288bf522SAndroid Build Coastguard Worker """ % (INFO_NODE_ORIGIN_X, INFO_NODE_WIDTH, INFO_NODE_ORIGIN_X, INFO_NODE_WIDTH, 173*288bf522SAndroid Build Coastguard Worker INFO_NODE_ORIGIN_X + RECT_TEXT_PADDING)) 174*288bf522SAndroid Build Coastguard Worker 175*288bf522SAndroid Build Coastguard Worker 176*288bf522SAndroid Build Coastguard Workerdef render_percent_node(f): 177*288bf522SAndroid Build Coastguard Worker f.write( 178*288bf522SAndroid Build Coastguard Worker """<rect id="percent_rect" style="stroke:rgb(0,0,0);" 179*288bf522SAndroid Build Coastguard Worker rx="10" ry="10" x="%d" y="10" width="%d" height="30" fill="rgb(255,255,255)"/> 180*288bf522SAndroid Build Coastguard Worker <text id="percent_text" text-anchor="end" x="%d" y="30">100.00%%</text> 181*288bf522SAndroid Build Coastguard Worker """ % (PERCENT_NODE_ORIGIN_X, PERCENT_NODE_WIDTH, 182*288bf522SAndroid Build Coastguard Worker PERCENT_NODE_ORIGIN_X + PERCENT_NODE_WIDTH - RECT_TEXT_PADDING)) 183*288bf522SAndroid Build Coastguard Worker 184*288bf522SAndroid Build Coastguard Worker 185*288bf522SAndroid Build Coastguard Workerdef render_svg(process, flamegraph, f, color_scheme): 186*288bf522SAndroid Build Coastguard Worker height = (flamegraph.get_max_depth() + 2) * SVG_NODE_HEIGHT 187*288bf522SAndroid Build Coastguard Worker f.write("""<div class="flamegraph_block" style="width:100%%; height:%dpx;"> 188*288bf522SAndroid Build Coastguard Worker """ % height) 189*288bf522SAndroid Build Coastguard Worker f.write("""<svg xmlns="http://www.w3.org/2000/svg" 190*288bf522SAndroid Build Coastguard Worker xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" 191*288bf522SAndroid Build Coastguard Worker width="100%%" height="100%%" style="border: 1px solid black;" 192*288bf522SAndroid Build Coastguard Worker rootid="%d"> 193*288bf522SAndroid Build Coastguard Worker """ % (flamegraph.children[0].id)) 194*288bf522SAndroid Build Coastguard Worker f.write("""<defs > <linearGradient id="background_gradiant" y1="0" y2="1" x1="0" x2="0" > 195*288bf522SAndroid Build Coastguard Worker <stop stop-color="#eeeeee" offset="5%" /> <stop stop-color="#efefb1" offset="90%" /> 196*288bf522SAndroid Build Coastguard Worker </linearGradient> </defs>""") 197*288bf522SAndroid Build Coastguard Worker f.write("""<rect x="0.0" y="0" width="100%" height="100%" fill="url(#background_gradiant)" /> 198*288bf522SAndroid Build Coastguard Worker """) 199*288bf522SAndroid Build Coastguard Worker render_svg_nodes(process, flamegraph, 0, f, flamegraph.weight(), height, color_scheme) 200*288bf522SAndroid Build Coastguard Worker render_search_node(f) 201*288bf522SAndroid Build Coastguard Worker render_unzoom_node(f) 202*288bf522SAndroid Build Coastguard Worker render_info_node(f) 203*288bf522SAndroid Build Coastguard Worker render_percent_node(f) 204*288bf522SAndroid Build Coastguard Worker f.write("</svg></div><br/>\n\n") 205