NativeHeapDumpViewer: Python 3 compatibility

Fix some minor compat issues. Also clean up file close.

Test: native_heapdump_viewer_tests
Change-Id: Ic43b5cc61f26dd11fffe4e9b7a06056e4ed1c676
This commit is contained in:
Andreas Gampe
2018-10-29 13:25:49 -07:00
parent 9240b454cc
commit 547631e12a
2 changed files with 73 additions and 70 deletions

View File

@@ -82,13 +82,13 @@ Usage:
elif sys.argv[i] == "--reverse": elif sys.argv[i] == "--reverse":
self.reverse_frames = True self.reverse_frames = True
elif sys.argv[i][0] == '-': elif sys.argv[i][0] == '-':
print "Invalid option " + sys.argv[i] print("Invalid option %s" % (sys.argv[i]))
else: else:
extra_args.append(sys.argv[i]) extra_args.append(sys.argv[i])
i += 1 i += 1
if len(extra_args) != 1: if len(extra_args) != 1:
print self._usage print(self._usage)
sys.exit(1) sys.exit(1)
self.native_heap = extra_args[0] self.native_heap = extra_args[0]
@@ -118,7 +118,8 @@ def GetVersion(native_heap):
re_line = re.compile("Android\s+Native\s+Heap\s+Dump\s+(?P<version>v\d+\.\d+)\s*$") re_line = re.compile("Android\s+Native\s+Heap\s+Dump\s+(?P<version>v\d+\.\d+)\s*$")
matched = 0 matched = 0
for line in open(native_heap, "r"): with open(native_heap, "r") as f:
for line in f:
m = re_line.match(line) m = re_line.match(line)
if m: if m:
return m.group('version') return m.group('version')
@@ -143,7 +144,8 @@ def GetNumFieldValidByParsingLines(native_heap):
re_line = re.compile("z\s+(?P<zygote>\d+)\s+sz\s+(?P<size>\d+)\s+num\s+(?P<num_allocations>\d+)") re_line = re.compile("z\s+(?P<zygote>\d+)\s+sz\s+(?P<size>\d+)\s+num\s+(?P<num_allocations>\d+)")
matched = 0 matched = 0
backtrace_size = 0 backtrace_size = 0
for line in open(native_heap, "r"): with open(native_heap, "r") as f:
for line in f:
if backtrace_size == 0: if backtrace_size == 0:
m = re_backtrace.match(line) m = re_backtrace.match(line)
if m: if m:
@@ -220,7 +222,8 @@ def ParseNativeHeap(native_heap, reverse_frames, num_field_valid, app_symboldir)
re_map = re.compile("(?P<start>[0-9a-f]+)-(?P<end>[0-9a-f]+) .... (?P<offset>[0-9a-f]+) [0-9a-f]+:[0-9a-f]+ [0-9]+ +(?P<name>.*)") re_map = re.compile("(?P<start>[0-9a-f]+)-(?P<end>[0-9a-f]+) .... (?P<offset>[0-9a-f]+) [0-9a-f]+:[0-9a-f]+ [0-9]+ +(?P<name>.*)")
for line in open(native_heap, "r"): with open(native_heap, "r") as f:
for line in f:
# Format of line: # Format of line:
# z 0 sz 50 num 1 bt 000000000000a100 000000000000b200 # z 0 sz 50 num 1 bt 000000000000a100 000000000000b200
parts = line.split() parts = line.split()
@@ -231,7 +234,7 @@ def ParseNativeHeap(native_heap, reverse_frames, num_field_valid, app_symboldir)
num_allocs = int(parts[5]) num_allocs = int(parts[5])
else: else:
num_allocs = 1 num_allocs = 1
frames = map(lambda x: int(x, 16), parts[7:]) frames = list(map(lambda x: int(x, 16), parts[7:]))
if reverse_frames: if reverse_frames:
frames = list(reversed(frames)) frames = list(reversed(frames))
backtraces.append(Backtrace(is_zygote, size, num_allocs, frames)) backtraces.append(Backtrace(is_zygote, size, num_allocs, frames))
@@ -300,7 +303,7 @@ def ResolveAddrs(html_output, symboldir, app_symboldir, backtraces, mappings):
# Resolve functions and line numbers. # Resolve functions and line numbers.
if html_output == False: if html_output == False:
print "Resolving symbols using directory %s..." % symboldir print("Resolving symbols using directory %s..." % symboldir)
for lib in addrs_by_lib: for lib in addrs_by_lib:
sofile = app_symboldir + lib sofile = app_symboldir + lib
@@ -335,7 +338,7 @@ def ResolveAddrs(html_output, symboldir, app_symboldir, backtraces, mappings):
resolved_addrs[addrs_by_lib[lib][x]] = FrameDescription("---", "---", lib) resolved_addrs[addrs_by_lib[lib][x]] = FrameDescription("---", "---", lib)
else: else:
if html_output == False: if html_output == False:
print "%s not found for symbol resolution" % lib print("%s not found for symbol resolution" % lib)
fd = FrameDescription("???", "???", lib) fd = FrameDescription("???", "???", lib)
for addr in addrs_by_lib[lib]: for addr in addrs_by_lib[lib]:
@@ -374,7 +377,7 @@ def Display(resolved_addrs, indent, total, parent_total, node):
parent_percent = 0 parent_percent = 0
if parent_total != 0: if parent_total != 0:
parent_percent = 100 * node.size / float(parent_total) parent_percent = 100 * node.size / float(parent_total)
print "%9d %6.2f%% %6.2f%% %8d %s%s %s %s %s" % (node.size, total_percent, parent_percent, node.number, indent, node.addr, fd.library, fd.function, fd.location) print("%9d %6.2f%% %6.2f%% %8d %s%s %s %s %s" % (node.size, total_percent, parent_percent, node.number, indent, node.addr, fd.library, fd.function, fd.location))
children = sorted(node.children.values(), key=lambda x: x.size, reverse=True) children = sorted(node.children.values(), key=lambda x: x.size, reverse=True)
for child in children: for child in children:
Display(resolved_addrs, indent + " ", total, node.size, child) Display(resolved_addrs, indent + " ", total, node.size, child)
@@ -395,23 +398,23 @@ def DisplayHtml(verbose, resolved_addrs, total, node, extra, label_count):
label = label.replace("<", "&lt;") label = label.replace("<", "&lt;")
label = label.replace(">", "&gt;") label = label.replace(">", "&gt;")
children = sorted(node.children.values(), key=lambda x: x.size, reverse=True) children = sorted(node.children.values(), key=lambda x: x.size, reverse=True)
print '<li>' print('<li>')
if len(children) > 0: if len(children) > 0:
print '<label for="' + str(label_count) + '">' + label + '</label>' print('<label for="' + str(label_count) + '">' + label + '</label>')
print '<input type="checkbox" id="' + str(label_count) + '"/>' print('<input type="checkbox" id="' + str(label_count) + '"/>')
print '<ol>' print('<ol>')
label_count += 1 label_count += 1
for child in children: for child in children:
label_count = DisplayHtml(verbose, resolved_addrs, total, child, "", label_count) label_count = DisplayHtml(verbose, resolved_addrs, total, child, "", label_count)
print '</ol>' print('</ol>')
else: else:
print label print(label)
print '</li>' print('</li>')
return label_count return label_count
def CreateHtml(verbose, app, zygote, resolved_addrs): def CreateHtml(verbose, app, zygote, resolved_addrs):
print """ print("""
<!DOCTYPE html> <!DOCTYPE html>
<html><head><style> <html><head><style>
li input { li input {
@@ -433,13 +436,13 @@ label {
</style></head><body>Native allocation HTML viewer<br><br> </style></head><body>Native allocation HTML viewer<br><br>
Click on an individual line to expand/collapse to see the details of the Click on an individual line to expand/collapse to see the details of the
allocation data<ol> allocation data<ol>
""" """)
label_count = 0 label_count = 0
label_count = DisplayHtml(verbose, resolved_addrs, app.size, app, "app ", label_count) label_count = DisplayHtml(verbose, resolved_addrs, app.size, app, "app ", label_count)
if zygote.size > 0: if zygote.size > 0:
DisplayHtml(verbose, resolved_addrs, zygote.size, zygote, "zygote ", label_count) DisplayHtml(verbose, resolved_addrs, zygote.size, zygote, "zygote ", label_count)
print "</ol></body></html>" print("</ol></body></html>")
def main(): def main():
args = Args() args = Args()
@@ -468,12 +471,12 @@ def main():
if args.html_output: if args.html_output:
CreateHtml(args.verbose, app, zygote, resolved_addrs) CreateHtml(args.verbose, app, zygote, resolved_addrs)
else: else:
print "" print("")
print "%9s %6s %6s %8s %s %s %s %s" % ("BYTES", "%TOTAL", "%PARENT", "COUNT", "ADDR", "LIBRARY", "FUNCTION", "LOCATION") print("%9s %6s %6s %8s %s %s %s %s" % ("BYTES", "%TOTAL", "%PARENT", "COUNT", "ADDR", "LIBRARY", "FUNCTION", "LOCATION"))
Display(resolved_addrs, "", app.size, app.size + zygote.size, app) Display(resolved_addrs, "", app.size, app.size + zygote.size, app)
print "" print("")
Display(resolved_addrs, "", zygote.size, app.size + zygote.size, zygote) Display(resolved_addrs, "", zygote.size, app.size + zygote.size, zygote)
print "" print("")
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -27,7 +27,7 @@ class NativeHeapdumpViewerTest(unittest.TestCase):
def CreateTmpFile(self, contents): def CreateTmpFile(self, contents):
fd, self._tmp_file_name = tempfile.mkstemp() fd, self._tmp_file_name = tempfile.mkstemp()
os.write(fd, contents) os.write(fd, contents.encode())
os.close(fd) os.close(fd)
return self._tmp_file_name return self._tmp_file_name
@@ -36,7 +36,7 @@ class NativeHeapdumpViewerTest(unittest.TestCase):
try: try:
os.unlink(self._tmp_file_name) os.unlink(self._tmp_file_name)
except Exception: except Exception:
print "Failed to delete " + heap print("Failed to delete %s" % (heap))
class GetNumFieldValidTest(NativeHeapdumpViewerTest): class GetNumFieldValidTest(NativeHeapdumpViewerTest):
_map_data = """ _map_data = """