| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (C) 2018 The Android Open Source Project |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
# Make sure that simpleperf's inferno is on the PYTHONPATH, e.g., run as |
| 18 |
# PYTHONPATH=$PYTHONPATH:$ANDROID_BUILD_TOP/system/extras/simpleperf/scripts/inferno python .. |
| 19 |
|
| 20 |
import argparse |
| 21 |
import itertools |
| 22 |
import sqlite3 |
| 23 |
|
| 24 |
|
| 25 |
class Callsite(object): |
| 26 |
def __init__(self, dso_id, sym_id): |
| 27 |
self.dso_id = dso_id |
| 28 |
self.sym_id = sym_id |
| 29 |
self.count = 0 |
| 30 |
self.child_map = {} |
| 31 |
self.id = self._get_next_callsite_id() |
| 32 |
|
| 33 |
def add(self, dso_id, sym_id): |
| 34 |
if (dso_id, sym_id) in self.child_map: |
| 35 |
return self.child_map[(dso_id, sym_id)] |
| 36 |
new_callsite = Callsite(dso_id, sym_id) |
| 37 |
self.child_map[(dso_id, sym_id)] = new_callsite |
| 38 |
return new_callsite |
| 39 |
|
| 40 |
def child_count_to_self(self): |
| 41 |
self.count = reduce(lambda x, y: x + y[1].count, self.child_map.iteritems(), 0) |
| 42 |
|
| 43 |
def trim(self, local_threshold_in_percent, global_threshold): |
| 44 |
local_threshold = local_threshold_in_percent * 0.01 * self.count |
| 45 |
threshold = max(local_threshold, global_threshold) |
| 46 |
for k, v in self.child_map.items(): |
| 47 |
if v.count < threshold: |
| 48 |
del self.child_map[k] |
| 49 |
for _, v in self.child_map.iteritems(): |
| 50 |
v.trim(local_threshold_in_percent, global_threshold) |
| 51 |
|
| 52 |
def _get_str(self, id, m): |
| 53 |
if id in m: |
| 54 |
return m[id] |
| 55 |
return str(id) |
| 56 |
|
| 57 |
def print_callsite_ascii(self, depth, indent, dsos, syms): |
| 58 |
|
| 59 |
print ' ' * indent + "%s (%s) [%d]" % (self._get_str(self.sym_id, syms), |
| 60 |
self._get_str(self.dso_id, dsos), |
| 61 |
self.count) |
| 62 |
if depth == 0: |
| 63 |
return |
| 64 |
for v in sorted(self.child_map.itervalues, key=lambda x: x.count, reverse=True): |
| 65 |
v.print_callsite_ascii(depth - 1, indent + 1, dsos, syms) |
| 66 |
|
| 67 |
# Functions for flamegraph compatibility. |
| 68 |
|
| 69 |
callsite_counter = 0 |
| 70 |
|
| 71 |
@classmethod |
| 72 |
def _get_next_callsite_id(cls): |
| 73 |
cls.callsite_counter += 1 |
| 74 |
return cls.callsite_counter |
| 75 |
|
| 76 |
def create_children_list(self): |
| 77 |
self.children = sorted(self.child_map.itervalues(), key=lambda x: x.count, reverse=True) |
| 78 |
|
| 79 |
def generate_offset(self, start_offset): |
| 80 |
self.offset = start_offset |
| 81 |
child_offset = start_offset |
| 82 |
for child in self.children: |
| 83 |
child_offset = child.generate_offset(child_offset) |
| 84 |
return self.offset + self.count |
| 85 |
|
| 86 |
def svgrenderer_compat(self, dsos, syms): |
| 87 |
self.create_children_list() |
| 88 |
self.method = self._get_str(self.sym_id, syms) |
| 89 |
self.dso = self._get_str(self.dso_id, dsos) |
| 90 |
self.offset = 0 |
| 91 |
for c in self.children: |
| 92 |
c.svgrenderer_compat(dsos, syms) |
| 93 |
|
| 94 |
def weight(self): |
| 95 |
return float(self.count) |
| 96 |
|
| 97 |
def get_max_depth(self): |
| 98 |
if self.child_map: |
| 99 |
return max([c.get_max_depth() for c in self.child_map.itervalues()]) + 1 |
| 100 |
return 1 |
| 101 |
|
| 102 |
|
| 103 |
class SqliteReader(object): |
| 104 |
def __init__(self): |
| 105 |
self.root = Callsite("root", "root") |
| 106 |
self.dsos = {} |
| 107 |
self.syms = {} |
| 108 |
|
| 109 |
def open(self, f): |
| 110 |
self._conn = sqlite3.connect(f) |
| 111 |
self._c = self._conn.cursor() |
| 112 |
|
| 113 |
def close(self): |
| 114 |
self._conn.close() |
| 115 |
|
| 116 |
def read(self, local_threshold_in_percent, global_threshold_in_percent, limit, |
| 117 |
skip_simpleperf): |
| 118 |
# Read aux tables first, as we need to find the kernel symbols. |
| 119 |
def read_table(name, dest_table): |
| 120 |
self._c.execute('select id, name from %s' % (name)) |
| 121 |
while True: |
| 122 |
rows = self._c.fetchmany(100) |
| 123 |
if not rows: |
| 124 |
break |
| 125 |
for row in rows: |
| 126 |
dest_table[row[0]] = row[1] |
| 127 |
|
| 128 |
print 'Reading DSOs' |
| 129 |
read_table('dsos', self.dsos) |
| 130 |
|
| 131 |
print 'Reading symbol strings' |
| 132 |
read_table('syms', self.syms) |
| 133 |
|
| 134 |
kernel_sym_id = None |
| 135 |
for i, v in self.syms.iteritems(): |
| 136 |
if v == '[kernel]': |
| 137 |
kernel_sym_id = i |
| 138 |
break |
| 139 |
|
| 140 |
skip_query_str = "" |
| 141 |
if skip_simpleperf: |
| 142 |
self._c.execute('select id from pids where name = "simpleperf"') |
| 143 |
pid_row = self._c.fetchone() |
| 144 |
if pid_row: |
| 145 |
skip_query_join = "as st join samples sa on st.sample_id = sa.id " |
| 146 |
skip_query_str = skip_query_join + "where sa.pid_id != %d" % (pid_row[0]) |
| 147 |
|
| 148 |
query_prefix = 'select sample_id, depth, dso_id, sym_id from stacks ' |
| 149 |
query_suffix = ' order by sample_id asc, depth desc' |
| 150 |
|
| 151 |
print 'Reading samples' |
| 152 |
self._c.execute(query_prefix + skip_query_str + query_suffix) |
| 153 |
|
| 154 |
last_sample_id = None |
| 155 |
chain = None |
| 156 |
count = 0 |
| 157 |
while True: |
| 158 |
rows = self._c.fetchmany(100) |
| 159 |
|
| 160 |
if not rows: |
| 161 |
break |
| 162 |
for row in rows: |
| 163 |
if row[3] == kernel_sym_id and row[1] == 0: |
| 164 |
# Skip kernel. |
| 165 |
continue |
| 166 |
if row[0] != last_sample_id: |
| 167 |
last_sample_id = row[0] |
| 168 |
chain = self.root |
| 169 |
chain = chain.add(row[2], row[3]) |
| 170 |
chain.count = chain.count + 1 |
| 171 |
count = count + len(rows) |
| 172 |
if limit is not None and count >= limit: |
| 173 |
print 'Breaking as limit is reached' |
| 174 |
break |
| 175 |
|
| 176 |
self.root.child_count_to_self() |
| 177 |
global_threshold = global_threshold_in_percent * 0.01 * self.root.count |
| 178 |
self.root.trim(local_threshold_in_percent, global_threshold) |
| 179 |
|
| 180 |
def print_data_ascii(self, depth): |
| 181 |
self.root.print_callsite_ascii(depth, 0, self.dsos, self.syms) |
| 182 |
|
| 183 |
def get_script_js(self): |
| 184 |
# Try to get the data directly (if packaged with embedded_loader). |
| 185 |
import os.path |
| 186 |
import sys |
| 187 |
if '__loader__' in globals(): |
| 188 |
try: |
| 189 |
js = __loader__.get_data(os.path.join(os.path.dirname(__file__), "script.js")) |
| 190 |
if js is not None and len(js) > 0: |
| 191 |
return js |
| 192 |
except: |
| 193 |
pass |
| 194 |
# See if we can find it another way. |
| 195 |
rel_paths = [ |
| 196 |
# Maybe we're being run packaged. |
| 197 |
"script.js", |
| 198 |
# Maybe we're being run directly. |
| 199 |
"../../simpleperf/scripts/inferno/script.js", |
| 200 |
] |
| 201 |
for rel_path in rel_paths: |
| 202 |
script_js = os.path.join(os.path.dirname(__file__), rel_path) |
| 203 |
if os.path.exists(script_js): |
| 204 |
with open(script_js, 'r') as script_f: |
| 205 |
return script_f.read() |
| 206 |
return None |
| 207 |
|
| 208 |
def print_svg(self, filename, depth): |
| 209 |
from svg_renderer import render_svg |
| 210 |
self.root.svgrenderer_compat(self.dsos, self.syms) |
| 211 |
self.root.generate_offset(0) |
| 212 |
f = open(filename, 'w') |
| 213 |
f.write(''' |
| 214 |
<html> |
| 215 |
<body> |
| 216 |
<div id='flamegraph_id' style='font-family: Monospace;'> |
| 217 |
<style type="text/css"> .s { stroke:black; stroke-width:0.5; cursor:pointer;} </style> |
| 218 |
<style type="text/css"> .t:hover { cursor:pointer; } </style> |
| 219 |
''') |
| 220 |
|
| 221 |
class FakeProcess: |
| 222 |
def __init__(self): |
| 223 |
self.props = {'trace_offcpu': False} |
| 224 |
fake_process = FakeProcess() |
| 225 |
render_svg(fake_process, self.root, f, 'hot') |
| 226 |
|
| 227 |
f.write(''' |
| 228 |
</div> |
| 229 |
''') |
| 230 |
|
| 231 |
# Emit script.js, if we can find it. |
| 232 |
script_data = self.get_script_js() |
| 233 |
if script_data is not None: |
| 234 |
f.write('<script>\n') |
| 235 |
f.write(script_data) |
| 236 |
f.write(''' |
| 237 |
</script> |
| 238 |
<br/><br/> |
| 239 |
<div>Navigate with WASD, zoom in with SPACE, zoom out with BACKSPACE.</div> |
| 240 |
<script>document.addEventListener('DOMContentLoaded', flamegraphInit);</script> |
| 241 |
</body> |
| 242 |
</html> |
| 243 |
''') |
| 244 |
f.close() |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == "__main__": |
| 248 |
parser = argparse.ArgumentParser(description='''Translate a perfprofd database into a flame |
| 249 |
representation''') |
| 250 |
|
| 251 |
parser.add_argument('file', help='the sqlite database to use', metavar='file', type=str) |
| 252 |
|
| 253 |
parser.add_argument('--html-out', help='output file for HTML flame graph', type=str) |
| 254 |
parser.add_argument('--threshold', help='child threshold in percent', type=float, default=5) |
| 255 |
parser.add_argument('--global-threshold', help='global threshold in percent', type=float, |
| 256 |
default=.1) |
| 257 |
parser.add_argument('--depth', help='depth to print to', type=int, default=10) |
| 258 |
parser.add_argument('--limit', help='limit to given number of stack trace entries', type=int) |
| 259 |
parser.add_argument('--skip-simpleperf', help='skip simpleperf samples', action='store_const', |
| 260 |
const=True) |
| 261 |
|
| 262 |
args = parser.parse_args() |
| 263 |
if args is not None: |
| 264 |
sql_out = SqliteReader() |
| 265 |
sql_out.open(args.file) |
| 266 |
sql_out.read(args.threshold, args.global_threshold, args.limit, |
| 267 |
args.skip_simpleperf is not None) |
| 268 |
if args.html_out is None: |
| 269 |
sql_out.print_data_ascii(args.depth) |
| 270 |
else: |
| 271 |
sql_out.print_svg(args.html_out, args.depth) |
| 272 |
sql_out.close() |