blob: ae234b51ba585590cbc3e037d6be4dbbd16188ed [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/python2
Yunlian Jiang08617492015-10-23 10:43:45 -07002# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Yunlian Jiang08617492015-10-23 10:43:45 -07005"""Wrapper to generate heat maps for chrome."""
6
Caroline Tice88272d42016-01-13 09:48:29 -08007from __future__ import print_function
8
Yunlian Jiang08617492015-10-23 10:43:45 -07009import argparse
10import shutil
11import os
12import sys
13import tempfile
14from sets import Set
15
Caroline Tice88272d42016-01-13 09:48:29 -080016from cros_utils import command_executer
Luis Lozanof2a3ef42015-12-15 13:49:30 -080017
Yunlian Jiang08617492015-10-23 10:43:45 -070018def IsARepoRoot(directory):
19 """Returns True if directory is the root of a repo checkout."""
20 return os.path.exists(os.path.join(directory, '.repo'))
21
Luis Lozanof2a3ef42015-12-15 13:49:30 -080022
Yunlian Jiang08617492015-10-23 10:43:45 -070023class HeatMapProducer(object):
24 """Class to produce heat map."""
25
26 def __init__(self, chromeos_root, perf_data, page_size, binary):
27 self.chromeos_root = os.path.realpath(chromeos_root)
28 self.perf_data = os.path.realpath(perf_data)
29 self.page_size = page_size
30 self.dir = os.path.dirname(os.path.realpath(__file__))
31 self.binary = binary
Luis Lozanof2a3ef42015-12-15 13:49:30 -080032 self.tempDir = ''
Yunlian Jiang08617492015-10-23 10:43:45 -070033 self.ce = command_executer.GetCommandExecuter()
Caroline Tice88272d42016-01-13 09:48:29 -080034 self.loading_address = None
35 self.temp_perf = ''
36 self.temp_perf_inchroot = ''
37 self.perf_report = ''
Yunlian Jiang08617492015-10-23 10:43:45 -070038
39 def copyFileToChroot(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 self.tempDir = tempfile.mkdtemp(
41 prefix=os.path.join(self.chromeos_root, 'src/'))
Yunlian Jiang08617492015-10-23 10:43:45 -070042 self.temp_perf = os.path.join(self.tempDir, 'perf.data')
43 shutil.copy2(self.perf_data, self.temp_perf)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 self.temp_perf_inchroot = os.path.join('~/trunk/src',
45 os.path.basename(self.tempDir))
Yunlian Jiang08617492015-10-23 10:43:45 -070046
47 def getPerfReport(self):
Caroline Tice88272d42016-01-13 09:48:29 -080048 cmd = ('cd %s; perf report -D -i perf.data > perf_report.txt' %
49 self.temp_perf_inchroot)
Yunlian Jiang08617492015-10-23 10:43:45 -070050 retval = self.ce.ChrootRunCommand(self.chromeos_root, cmd)
51 if retval:
52 raise RuntimeError('Failed to generate perf report')
53 self.perf_report = os.path.join(self.tempDir, 'perf_report.txt')
54
55 def getBinaryBaseAddress(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 cmd = 'grep PERF_RECORD_MMAP %s | grep "%s$"' % (self.perf_report,
57 self.binary)
Luis Lozano036c9232015-12-10 10:47:01 -080058 retval, output, _ = self.ce.RunCommandWOutput(cmd)
Yunlian Jiang08617492015-10-23 10:43:45 -070059 if retval:
60 raise RuntimeError('Failed to run grep to get base address')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080061 baseAddresses = Set()
Yunlian Jiang08617492015-10-23 10:43:45 -070062 for line in output.strip().split('\n'):
63 head = line.split('[')[2]
64 address = head.split('(')[0]
65 baseAddresses.add(address)
66 if len(baseAddresses) > 1:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080067 raise RuntimeError(
68 'Multiple base address found, please disable ASLR and collect '
69 'profile again')
Yunlian Jiang08617492015-10-23 10:43:45 -070070 if not len(baseAddresses):
71 raise RuntimeError('Could not find the base address in the profile')
72 self.loading_address = baseAddresses.pop()
73
74 def RemoveFiles(self):
75 shutil.rmtree(self.tempDir)
76 if os.path.isfile(os.path.join(os.getcwd(), 'out.txt')):
77 os.remove(os.path.join(os.getcwd(), 'out.txt'))
78 if os.path.isfile(os.path.join(os.getcwd(), 'inst-histo.txt')):
79 os.remove(os.path.join(os.getcwd(), 'inst-histo.txt'))
80
81 def getHeatmap(self):
82 if not self.loading_address:
83 return
84 heatmap_script = os.path.join(self.dir, 'perf-to-inst-page.sh')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080085 cmd = '{0} {1} {2} {3} {4}'.format(heatmap_script, self.binary,
86 self.perf_report, self.loading_address,
Yunlian Jiang08617492015-10-23 10:43:45 -070087 self.page_size)
88 retval = self.ce.RunCommand(cmd)
89 if retval:
90 raise RuntimeError('Failed to run script to generate heatmap')
91
92
Yunlian Jiang08617492015-10-23 10:43:45 -070093def main(argv):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080094 """Parse the options.
Yunlian Jiang08617492015-10-23 10:43:45 -070095
96 Args:
Caroline Tice88272d42016-01-13 09:48:29 -080097 argv: The options with which this script was invoked.
Yunlian Jiang08617492015-10-23 10:43:45 -070098
99 Returns:
100 0 unless an exception is raised.
101 """
102 parser = argparse.ArgumentParser()
103
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 parser.add_argument('--chromeos_root',
105 dest='chromeos_root',
106 required=True,
Yunlian Jiang08617492015-10-23 10:43:45 -0700107 help='ChromeOS root to use for generate heatmaps.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 parser.add_argument('--perf_data',
109 dest='perf_data',
110 required=True,
Yunlian Jiang08617492015-10-23 10:43:45 -0700111 help='The raw perf data.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 parser.add_argument('--binary',
113 dest='binary',
114 required=False,
115 help='The name of the binary.',
116 default='chrome')
117 parser.add_argument('--page_size',
118 dest='page_size',
119 required=False,
Yunlian Jiang08617492015-10-23 10:43:45 -0700120 help='The page size for heat maps.',
121 default=4096)
122 options = parser.parse_args(argv)
123
124 if not IsARepoRoot(options.chromeos_root):
125 parser.error('% does not contain .repo dir.' % options.chromeos_root)
126
127 if not os.path.isfile(options.perf_data):
128 parser.error('Cannot find perf_data: %s.' % options.perf_data)
129
130 heatmap_producer = HeatMapProducer(options.chromeos_root, options.perf_data,
131 options.page_size, options.binary)
132 try:
133 heatmap_producer.copyFileToChroot()
134 heatmap_producer.getPerfReport()
135 heatmap_producer.getBinaryBaseAddress()
136 heatmap_producer.getHeatmap()
137 print('\nheat map and time histgram genereated in the current directory '
138 'with name heat_map.png and timeline.png accordingly.')
139 except RuntimeError, e:
Caroline Tice88272d42016-01-13 09:48:29 -0800140 print(e)
Yunlian Jiang08617492015-10-23 10:43:45 -0700141 finally:
142 heatmap_producer.RemoveFiles()
143
144
145if __name__ == '__main__':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 sys.exit(main(sys.argv[1:]))