Yunlian Jiang | 0861749 | 2015-10-23 10:43:45 -0700 | [diff] [blame] | 1 | #!/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. |
| 5 | |
| 6 | """Wrapper to generate heat maps for chrome.""" |
| 7 | |
| 8 | import argparse |
| 9 | import shutil |
| 10 | import os |
| 11 | import sys |
| 12 | import tempfile |
| 13 | from sets import Set |
| 14 | |
| 15 | from utils import command_executer |
| 16 | from utils import misc |
| 17 | |
| 18 | def 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 | |
| 22 | class HeatMapProducer(object): |
| 23 | """Class to produce heat map.""" |
| 24 | |
| 25 | def __init__(self, chromeos_root, perf_data, page_size, binary): |
| 26 | self.chromeos_root = os.path.realpath(chromeos_root) |
| 27 | self.perf_data = os.path.realpath(perf_data) |
| 28 | self.page_size = page_size |
| 29 | self.dir = os.path.dirname(os.path.realpath(__file__)) |
| 30 | self.binary = binary |
| 31 | self.tempDir = "" |
| 32 | self.ce = command_executer.GetCommandExecuter() |
| 33 | |
| 34 | def copyFileToChroot(self): |
| 35 | self.tempDir = tempfile.mkdtemp(prefix=os.path.join(self.chromeos_root, 'src/')) |
| 36 | self.temp_perf = os.path.join(self.tempDir, 'perf.data') |
| 37 | shutil.copy2(self.perf_data, self.temp_perf) |
| 38 | self.temp_perf_inchroot = os.path.join('~/trunk/src', os.path.basename(self.tempDir)) |
| 39 | |
| 40 | def getPerfReport(self): |
| 41 | cmd='cd %s; perf report -D -i perf.data > perf_report.txt' % self.temp_perf_inchroot |
| 42 | retval = self.ce.ChrootRunCommand(self.chromeos_root, cmd) |
| 43 | if retval: |
| 44 | raise RuntimeError('Failed to generate perf report') |
| 45 | self.perf_report = os.path.join(self.tempDir, 'perf_report.txt') |
| 46 | |
| 47 | def getBinaryBaseAddress(self): |
| 48 | cmd = 'grep PERF_RECORD_MMAP %s | grep "%s$"' % (self.perf_report, self.binary) |
| 49 | retval, output, _ = self.ce.RunCommand(cmd, return_output=True) |
| 50 | if retval: |
| 51 | raise RuntimeError('Failed to run grep to get base address') |
| 52 | baseAddresses = Set(); |
| 53 | for line in output.strip().split('\n'): |
| 54 | head = line.split('[')[2] |
| 55 | address = head.split('(')[0] |
| 56 | baseAddresses.add(address) |
| 57 | if len(baseAddresses) > 1: |
| 58 | raise RuntimeError('Multiple base address found, please disable ASLR and collect profile again') |
| 59 | if not len(baseAddresses): |
| 60 | raise RuntimeError('Could not find the base address in the profile') |
| 61 | self.loading_address = baseAddresses.pop() |
| 62 | |
| 63 | def RemoveFiles(self): |
| 64 | shutil.rmtree(self.tempDir) |
| 65 | if os.path.isfile(os.path.join(os.getcwd(), 'out.txt')): |
| 66 | os.remove(os.path.join(os.getcwd(), 'out.txt')) |
| 67 | if os.path.isfile(os.path.join(os.getcwd(), 'inst-histo.txt')): |
| 68 | os.remove(os.path.join(os.getcwd(), 'inst-histo.txt')) |
| 69 | |
| 70 | def getHeatmap(self): |
| 71 | if not self.loading_address: |
| 72 | return |
| 73 | heatmap_script = os.path.join(self.dir, 'perf-to-inst-page.sh') |
| 74 | cmd = '{0} {1} {2} {3} {4}'.format(heatmap_script, |
| 75 | self.binary, |
| 76 | self.perf_report, |
| 77 | self.loading_address, |
| 78 | self.page_size) |
| 79 | retval = self.ce.RunCommand(cmd) |
| 80 | if retval: |
| 81 | raise RuntimeError('Failed to run script to generate heatmap') |
| 82 | |
| 83 | |
| 84 | |
| 85 | def main(argv): |
| 86 | """ |
| 87 | Parse the options. |
| 88 | |
| 89 | Args: |
| 90 | argv: The options with which this script was invoked. |
| 91 | |
| 92 | Returns: |
| 93 | 0 unless an exception is raised. |
| 94 | """ |
| 95 | parser = argparse.ArgumentParser() |
| 96 | |
| 97 | parser.add_argument('--chromeos_root', dest='chromeos_root', required=True, |
| 98 | help='ChromeOS root to use for generate heatmaps.') |
| 99 | parser.add_argument('--perf_data', dest='perf_data', required=True, |
| 100 | help='The raw perf data.') |
| 101 | parser.add_argument('--binary', dest='binary', required=False, |
| 102 | help='The name of the binary.', default='chrome') |
| 103 | parser.add_argument('--page_size', dest='page_size', required=False, |
| 104 | help='The page size for heat maps.', |
| 105 | default=4096) |
| 106 | options = parser.parse_args(argv) |
| 107 | |
| 108 | if not IsARepoRoot(options.chromeos_root): |
| 109 | parser.error('% does not contain .repo dir.' % options.chromeos_root) |
| 110 | |
| 111 | if not os.path.isfile(options.perf_data): |
| 112 | parser.error('Cannot find perf_data: %s.' % options.perf_data) |
| 113 | |
| 114 | heatmap_producer = HeatMapProducer(options.chromeos_root, options.perf_data, |
| 115 | options.page_size, options.binary) |
| 116 | try: |
| 117 | heatmap_producer.copyFileToChroot() |
| 118 | heatmap_producer.getPerfReport() |
| 119 | heatmap_producer.getBinaryBaseAddress() |
| 120 | heatmap_producer.getHeatmap() |
| 121 | print('\nheat map and time histgram genereated in the current directory ' |
| 122 | 'with name heat_map.png and timeline.png accordingly.') |
| 123 | except RuntimeError, e: |
| 124 | print e |
| 125 | finally: |
| 126 | heatmap_producer.RemoveFiles() |
| 127 | |
| 128 | |
| 129 | if __name__ == '__main__': |
| 130 | sys.exit(main(sys.argv[1:])) |
| 131 | |