Craig Tiller | f7af2a9 | 2017-01-31 15:08:31 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python2.7 |
| 2 | # Copyright 2017, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | import multiprocessing |
| 32 | import os |
| 33 | import subprocess |
| 34 | import sys |
| 35 | |
| 36 | flamegraph_dir = os.path.join(os.path.expanduser('~'), 'FlameGraph') |
| 37 | |
| 38 | def fnize(s): |
| 39 | out = '' |
| 40 | for c in s: |
| 41 | if c in '<>, /': |
| 42 | if len(out) and out[-1] == '_': continue |
| 43 | out += '_' |
| 44 | else: |
| 45 | out += c |
| 46 | return out |
| 47 | |
| 48 | os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) |
| 49 | if not os.path.exists('reports'): |
| 50 | os.makedirs('reports') |
| 51 | |
| 52 | # index html |
| 53 | index_html = """ |
| 54 | <html> |
| 55 | <head> |
| 56 | <title>Microbenchmark Results</title> |
| 57 | </head> |
| 58 | <body> |
| 59 | """ |
| 60 | |
| 61 | def heading(name): |
| 62 | global index_html |
| 63 | index_html += "<h1>%s</h1>\n" % name |
| 64 | |
| 65 | def link(txt, tgt): |
| 66 | global index_html |
| 67 | index_html += "<p><a href=\"%s\">%s</a></p>\n" % (tgt, txt) |
| 68 | |
| 69 | for bm_name in sys.argv[1:]: |
| 70 | # generate latency profiles |
| 71 | heading('Latency Profiles: %s' % bm_name) |
| 72 | subprocess.check_call( |
| 73 | ['make', bm_name, |
| 74 | 'CONFIG=basicprof', '-j', '%d' % multiprocessing.cpu_count()]) |
| 75 | for line in subprocess.check_output(['bins/basicprof/%s' % bm_name, |
| 76 | '--benchmark_list_tests']).splitlines(): |
| 77 | link(line, 'reports/%s.txt' % fnize(line)) |
| 78 | with open('reports/%s.txt' % fnize(line), 'w') as f: |
| 79 | f.write(subprocess.check_output(['bins/basicprof/%s' % bm_name, |
| 80 | '--benchmark_filter=^%s$' % line])) |
| 81 | f.write('\n***********************************************************\n') |
| 82 | f.write(subprocess.check_output([ |
| 83 | sys.executable, 'tools/profiling/latency_profile/profile_analyzer.py', |
| 84 | '--source', 'latency_trace.txt', '--fmt', 'simple'])) |
| 85 | |
| 86 | # generate flamegraphs |
| 87 | heading('Flamegraphs: %s' % bm_name) |
| 88 | subprocess.check_call( |
| 89 | ['make', bm_name, |
| 90 | 'CONFIG=mutrace', '-j', '%d' % multiprocessing.cpu_count()]) |
| 91 | for line in subprocess.check_output(['bins/mutrace/%s' % bm_name, |
| 92 | '--benchmark_list_tests']).splitlines(): |
| 93 | subprocess.check_call(['sudo', 'perf', 'record', '-g', '-F', '99', |
| 94 | 'bins/mutrace/%s' % bm_name, |
| 95 | '--benchmark_filter=^%s$' % line, |
| 96 | '--benchmark_min_time=20']) |
| 97 | with open('/tmp/bm.perf', 'w') as f: |
| 98 | f.write(subprocess.check_output(['sudo', 'perf', 'script'])) |
| 99 | with open('/tmp/bm.folded', 'w') as f: |
| 100 | f.write(subprocess.check_output([ |
| 101 | '%s/stackcollapse-perf.pl' % flamegraph_dir, '/tmp/bm.perf'])) |
| 102 | link(line, 'reports/%s.svg' % fnize(line)) |
| 103 | with open('reports/%s.svg' % fnize(line), 'w') as f: |
| 104 | f.write(subprocess.check_output([ |
| 105 | '%s/flamegraph.pl' % flamegraph_dir, '/tmp/bm.folded'])) |
| 106 | |
| 107 | index_html += "</body>\n</html>\n" |
| 108 | with open('reports/index.html', 'w') as f: |
| 109 | w.write(index_html) |