xref: /netbsd-src/external/gpl3/gcc/dist/contrib/gen_autofdo_event.py (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1#!/usr/bin/python
2# Generate Intel taken branches Linux perf event script for autofdo profiling.
3
4# Copyright (C) 2016 Free Software Foundation, Inc.
5#
6# GCC is free software; you can redistribute it and/or modify it under
7# the terms of the GNU General Public License as published by the Free
8# Software Foundation; either version 3, or (at your option) any later
9# version.
10#
11# GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12# WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with GCC; see the file COPYING3.  If not see
18# <http://www.gnu.org/licenses/>.  */
19
20# Run it with perf record -b -e EVENT program ...
21# The Linux Kernel needs to support the PMU of the current CPU, and
22# It will likely not work in VMs.
23# Add --all to print for all cpus, otherwise for current cpu.
24# Add --script to generate shell script to run correct event.
25#
26# Requires internet (https) access. This may require setting up a proxy
27# with export https_proxy=...
28#
29import urllib2
30import sys
31import json
32import argparse
33import collections
34
35baseurl = "https://download.01.org/perfmon"
36
37target_events = (u'BR_INST_RETIRED.NEAR_TAKEN',
38                 u'BR_INST_EXEC.TAKEN',
39                 u'BR_INST_RETIRED.TAKEN_JCC',
40                 u'BR_INST_TYPE_RETIRED.COND_TAKEN')
41
42ap = argparse.ArgumentParser()
43ap.add_argument('--all', '-a', help='Print for all CPUs', action='store_true')
44ap.add_argument('--script', help='Generate shell script', action='store_true')
45args = ap.parse_args()
46
47eventmap = collections.defaultdict(list)
48
49def get_cpustr():
50    cpuinfo = os.getenv("CPUINFO")
51    if cpuinfo is None:
52        cpuinfo = '/proc/cpuinfo'
53    f = open(cpuinfo, 'r')
54    cpu = [None, None, None, None]
55    for j in f:
56        n = j.split()
57        if n[0] == 'vendor_id':
58            cpu[0] = n[2]
59        elif n[0] == 'model' and n[1] == ':':
60            cpu[2] = int(n[2])
61        elif n[0] == 'cpu' and n[1] == 'family':
62            cpu[1] = int(n[3])
63        elif n[0] == 'stepping' and n[1] == ':':
64            cpu[3] = int(n[2])
65        if all(v is not None for v in cpu):
66            break
67    # stepping for SKX only
68    stepping = cpu[0] == "GenuineIntel" and cpu[1] == 6 and cpu[2] == 0x55
69    if stepping:
70        return "%s-%d-%X-%X" % tuple(cpu)
71    return "%s-%d-%X" % tuple(cpu)[:3]
72
73def find_event(eventurl, model):
74    print >>sys.stderr, "Downloading", eventurl
75    u = urllib2.urlopen(eventurl)
76    events = json.loads(u.read())
77    u.close()
78
79    found = 0
80    for j in events:
81        if j[u'EventName'] in target_events:
82            event = "cpu/event=%s,umask=%s/" % (j[u'EventCode'], j[u'UMask'])
83            if u'PEBS' in j and j[u'PEBS'] > 0:
84                event += "p"
85            if args.script:
86                eventmap[event].append(model)
87            else:
88                print j[u'EventName'], "event for model", model, "is", event
89            found += 1
90    return found
91
92if not args.all:
93    cpu = get_cpu_str()
94    if not cpu:
95        sys.exit("Unknown CPU type")
96
97url = baseurl + "/mapfile.csv"
98print >>sys.stderr, "Downloading", url
99u = urllib2.urlopen(url)
100found = 0
101cpufound = 0
102for j in u:
103    n = j.rstrip().split(',')
104    if len(n) >= 4 and (args.all or n[0] == cpu) and n[3] == "core":
105        if args.all:
106            components = n[0].split("-")
107            model = components[2]
108            model = int(model, 16)
109        cpufound += 1
110        found += find_event(baseurl + n[2], model)
111u.close()
112
113if args.script:
114    print '''#!/bin/sh
115# Profile workload for gcc profile feedback (autofdo) using Linux perf.
116# Auto generated. To regenerate for new CPUs run
117# contrib/gen_autofdo_event.py --script --all in gcc source
118
119# usages:
120# gcc-auto-profile program             (profile program and children)
121# gcc-auto-profile -a sleep X          (profile all for X secs, may need root)
122# gcc-auto-profile -p PID sleep X      (profile PID)
123# gcc-auto-profile --kernel -a sleep X (profile kernel)
124# gcc-auto-profile --all -a sleep X    (profile kernel and user space)
125
126# Identify branches taken event for CPU.
127#
128
129FLAGS=u
130
131if [ "$1" = "--kernel" ] ; then
132  FLAGS=k
133  shift
134fi
135if [ "$1" = "--all" ] ; then
136  FLAGS=uk
137  shift
138fi
139
140if ! grep -q Intel /proc/cpuinfo ; then
141  echo >&2 "Only Intel CPUs supported"
142  exit 1
143fi
144
145if grep -q hypervisor /proc/cpuinfo ; then
146  echo >&2 "Warning: branch profiling may not be functional in VMs"
147fi
148
149case `egrep -q "^cpu family\s*: 6" /proc/cpuinfo &&
150  egrep "^model\s*:" /proc/cpuinfo | head -n1` in'''
151    for event, mod in eventmap.iteritems():
152        for m in mod[:-1]:
153            print "model*:\ %s|\\" % m
154        print 'model*:\ %s) E="%s$FLAGS" ;;' % (mod[-1], event)
155    print '''*)
156echo >&2 "Unknown CPU. Run contrib/gen_autofdo_event.py --all --script to update script."
157	exit 1 ;;'''
158    print "esac"
159    print "set -x"
160    print 'if ! perf record -e $E -b "$@" ; then'
161    print '  # PEBS may not actually be working even if the processor supports it'
162    print '  # (e.g., in a virtual machine). Trying to run without /p.'
163    print '  set +x'
164    print '  echo >&2 "Retrying without /p."'
165    print '  E="$(echo "${E}" | sed -e \'s/\/p/\//\')"'
166    print '  set -x'
167    print '  exec perf record -e $E -b "$@"'
168    print ' set +x'
169    print 'fi'
170
171if cpufound == 0 and not args.all:
172    sys.exit('CPU %s not found' % cpu)
173
174if found == 0:
175    sys.exit('Branch event not found')
176