blob: 7d4be65f8c2cf5e321203d25dd04dcef29316261 [file] [log] [blame]
Yunlian Jiang08617492015-10-23 10:43:45 -07001#!/usr/bin/python
2# 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
7import argparse
8import shutil
9import os
10import sys
11import tempfile
12from sets import Set
13
14from utils import command_executer
15from utils import misc
16
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()
34
35 def copyFileToChroot(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080036 self.tempDir = tempfile.mkdtemp(
37 prefix=os.path.join(self.chromeos_root, 'src/'))
Yunlian Jiang08617492015-10-23 10:43:45 -070038 self.temp_perf = os.path.join(self.tempDir, 'perf.data')
39 shutil.copy2(self.perf_data, self.temp_perf)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 self.temp_perf_inchroot = os.path.join('~/trunk/src',
41 os.path.basename(self.tempDir))
Yunlian Jiang08617492015-10-23 10:43:45 -070042
43 def getPerfReport(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 cmd = 'cd %s; perf report -D -i perf.data > perf_report.txt' % self.temp_perf_inchroot
Yunlian Jiang08617492015-10-23 10:43:45 -070045 retval = self.ce.ChrootRunCommand(self.chromeos_root, cmd)
46 if retval:
47 raise RuntimeError('Failed to generate perf report')
48 self.perf_report = os.path.join(self.tempDir, 'perf_report.txt')
49
50 def getBinaryBaseAddress(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 cmd = 'grep PERF_RECORD_MMAP %s | grep "%s$"' % (self.perf_report,
52 self.binary)
Luis Lozano036c9232015-12-10 10:47:01 -080053 retval, output, _ = self.ce.RunCommandWOutput(cmd)
Yunlian Jiang08617492015-10-23 10:43:45 -070054 if retval:
55 raise RuntimeError('Failed to run grep to get base address')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 baseAddresses = Set()
Yunlian Jiang08617492015-10-23 10:43:45 -070057 for line in output.strip().split('\n'):
58 head = line.split('[')[2]
59 address = head.split('(')[0]
60 baseAddresses.add(address)
61 if len(baseAddresses) > 1:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080062 raise RuntimeError(
63 'Multiple base address found, please disable ASLR and collect '
64 'profile again')
Yunlian Jiang08617492015-10-23 10:43:45 -070065 if not len(baseAddresses):
66 raise RuntimeError('Could not find the base address in the profile')
67 self.loading_address = baseAddresses.pop()
68
69 def RemoveFiles(self):
70 shutil.rmtree(self.tempDir)
71 if os.path.isfile(os.path.join(os.getcwd(), 'out.txt')):
72 os.remove(os.path.join(os.getcwd(), 'out.txt'))
73 if os.path.isfile(os.path.join(os.getcwd(), 'inst-histo.txt')):
74 os.remove(os.path.join(os.getcwd(), 'inst-histo.txt'))
75
76 def getHeatmap(self):
77 if not self.loading_address:
78 return
79 heatmap_script = os.path.join(self.dir, 'perf-to-inst-page.sh')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080080 cmd = '{0} {1} {2} {3} {4}'.format(heatmap_script, self.binary,
81 self.perf_report, self.loading_address,
Yunlian Jiang08617492015-10-23 10:43:45 -070082 self.page_size)
83 retval = self.ce.RunCommand(cmd)
84 if retval:
85 raise RuntimeError('Failed to run script to generate heatmap')
86
87
Yunlian Jiang08617492015-10-23 10:43:45 -070088def main(argv):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080089 """Parse the options.
Yunlian Jiang08617492015-10-23 10:43:45 -070090
91 Args:
92 argv: The options with which this script was invoked.
93
94 Returns:
95 0 unless an exception is raised.
96 """
97 parser = argparse.ArgumentParser()
98
Luis Lozanof2a3ef42015-12-15 13:49:30 -080099 parser.add_argument('--chromeos_root',
100 dest='chromeos_root',
101 required=True,
Yunlian Jiang08617492015-10-23 10:43:45 -0700102 help='ChromeOS root to use for generate heatmaps.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800103 parser.add_argument('--perf_data',
104 dest='perf_data',
105 required=True,
Yunlian Jiang08617492015-10-23 10:43:45 -0700106 help='The raw perf data.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800107 parser.add_argument('--binary',
108 dest='binary',
109 required=False,
110 help='The name of the binary.',
111 default='chrome')
112 parser.add_argument('--page_size',
113 dest='page_size',
114 required=False,
Yunlian Jiang08617492015-10-23 10:43:45 -0700115 help='The page size for heat maps.',
116 default=4096)
117 options = parser.parse_args(argv)
118
119 if not IsARepoRoot(options.chromeos_root):
120 parser.error('% does not contain .repo dir.' % options.chromeos_root)
121
122 if not os.path.isfile(options.perf_data):
123 parser.error('Cannot find perf_data: %s.' % options.perf_data)
124
125 heatmap_producer = HeatMapProducer(options.chromeos_root, options.perf_data,
126 options.page_size, options.binary)
127 try:
128 heatmap_producer.copyFileToChroot()
129 heatmap_producer.getPerfReport()
130 heatmap_producer.getBinaryBaseAddress()
131 heatmap_producer.getHeatmap()
132 print('\nheat map and time histgram genereated in the current directory '
133 'with name heat_map.png and timeline.png accordingly.')
134 except RuntimeError, e:
135 print e
136 finally:
137 heatmap_producer.RemoveFiles()
138
139
140if __name__ == '__main__':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 sys.exit(main(sys.argv[1:]))