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