From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 3A749385740D for ; Mon, 27 Jun 2022 06:15:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3A749385740D Received: from localhost.localdomain (xry111.site [IPv6:2001:470:683e::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 110C16680A for ; Mon, 27 Jun 2022 02:15:23 -0400 (EDT) Message-ID: <0c00f719092529e0a902fab5a65152f055836348.camel@xry111.site> Subject: [PATCH] contrib: modernize gen_autofdo_event.py From: Xi Ruoyao To: gcc-patches@gcc.gnu.org Date: Mon, 27 Jun 2022 14:15:22 +0800 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.2 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, FROM_SUSPICIOUS_NTLD_FP, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP, T_PDS_OTHER_BAD_TLD, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Jun 2022 06:15:27 -0000 Python 2 has been EOL'ed for two years. egrep has been deprecated for many years and the next grep release will start to print warning if it is used. -E option may be unsupported by some non-POSIX grep implementations, but gcc-auto-profile won't work on non-Linux systems anyway. contrib/ChangeLog: * gen_autofdo_event.py: Port to Python 3, and use grep -E instead of egrep. gcc/ChangeLog: * config/i386/gcc-auto-profile: Regenerate. --- contrib/gen_autofdo_event.py | 80 ++++++++++++++++---------------- gcc/config/i386/gcc-auto-profile | 31 +++++++------ 2 files changed, 57 insertions(+), 54 deletions(-) diff --git a/contrib/gen_autofdo_event.py b/contrib/gen_autofdo_event.py index 1eb6f1d6d85..7da2876530d 100755 --- a/contrib/gen_autofdo_event.py +++ b/contrib/gen_autofdo_event.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # Generate Intel taken branches Linux perf event script for autofdo profil= ing. =20 # Copyright (C) 2016 Free Software Foundation, Inc. @@ -26,18 +26,19 @@ # Requires internet (https) access. This may require setting up a proxy # with export https_proxy=3D... # -import urllib2 +import urllib.request import sys import json import argparse import collections +import os =20 baseurl =3D "https://download.01.org/perfmon" =20 -target_events =3D (u'BR_INST_RETIRED.NEAR_TAKEN', - u'BR_INST_EXEC.TAKEN', - u'BR_INST_RETIRED.TAKEN_JCC', - u'BR_INST_TYPE_RETIRED.COND_TAKEN') +target_events =3D ('BR_INST_RETIRED.NEAR_TAKEN', + 'BR_INST_EXEC.TAKEN', + 'BR_INST_RETIRED.TAKEN_JCC', + 'BR_INST_TYPE_RETIRED.COND_TAKEN') =20 ap =3D argparse.ArgumentParser() ap.add_argument('--all', '-a', help=3D'Print for all CPUs', action=3D'stor= e_true') @@ -71,47 +72,46 @@ def get_cpustr(): return "%s-%d-%X" % tuple(cpu)[:3] =20 def find_event(eventurl, model): - print >>sys.stderr, "Downloading", eventurl - u =3D urllib2.urlopen(eventurl) + print("Downloading", eventurl, file =3D sys.stderr) + u =3D urllib.request.urlopen(eventurl) events =3D json.loads(u.read()) u.close() =20 found =3D 0 for j in events: - if j[u'EventName'] in target_events: - event =3D "cpu/event=3D%s,umask=3D%s/" % (j[u'EventCode'], j[u= 'UMask']) - if u'PEBS' in j and j[u'PEBS'] > 0: + if j['EventName'] in target_events: + event =3D "cpu/event=3D%s,umask=3D%s/" % (j['EventCode'], j['U= Mask']) + if 'PEBS' in j and int(j['PEBS']) > 0: event +=3D "p" if args.script: eventmap[event].append(model) else: - print j[u'EventName'], "event for model", model, "is", eve= nt + print(j['EventName'], "event for model", model, "is", even= t) found +=3D 1 return found =20 if not args.all: - cpu =3D get_cpu_str() + cpu =3D get_cpustr() if not cpu: sys.exit("Unknown CPU type") =20 url =3D baseurl + "/mapfile.csv" -print >>sys.stderr, "Downloading", url -u =3D urllib2.urlopen(url) +print("Downloading", url, file =3D sys.stderr) +u =3D urllib.request.urlopen(url) found =3D 0 cpufound =3D 0 for j in u: - n =3D j.rstrip().split(',') + n =3D j.rstrip().decode().split(',') if len(n) >=3D 4 and (args.all or n[0] =3D=3D cpu) and n[3] =3D=3D "co= re": - if args.all: - components =3D n[0].split("-") - model =3D components[2] - model =3D int(model, 16) + components =3D n[0].split("-") + model =3D components[2] + model =3D int(model, 16) cpufound +=3D 1 found +=3D find_event(baseurl + n[2], model) u.close() =20 if args.script: - print '''#!/bin/sh + print('''#!/bin/sh # Profile workload for gcc profile feedback (autofdo) using Linux perf. # Auto generated. To regenerate for new CPUs run # contrib/gen_autofdo_event.py --script --all in gcc source @@ -146,27 +146,27 @@ if grep -q hypervisor /proc/cpuinfo ; then echo >&2 "Warning: branch profiling may not be functional in VMs" fi =20 -case `egrep -q "^cpu family\s*: 6" /proc/cpuinfo && - egrep "^model\s*:" /proc/cpuinfo | head -n1` in''' - for event, mod in eventmap.iteritems(): +case `grep -E -q "^cpu family\s*: 6" /proc/cpuinfo && + grep -E "^model\s*:" /proc/cpuinfo | head -n1` in''') + for event, mod in eventmap.items(): for m in mod[:-1]: - print "model*:\ %s|\\" % m - print 'model*:\ %s) E=3D"%s$FLAGS" ;;' % (mod[-1], event) - print '''*) + print("model*:\ %s|\\" % m) + print('model*:\ %s) E=3D"%s$FLAGS" ;;' % (mod[-1], event)) + print('''*) echo >&2 "Unknown CPU. Run contrib/gen_autofdo_event.py --all --script to = update script." - exit 1 ;;''' - print "esac" - print "set -x" - print 'if ! perf record -e $E -b "$@" ; then' - print ' # PEBS may not actually be working even if the processor supp= orts it' - print ' # (e.g., in a virtual machine). Trying to run without /p.' - print ' set +x' - print ' echo >&2 "Retrying without /p."' - print ' E=3D"$(echo "${E}" | sed -e \'s/\/p/\//\')"' - print ' set -x' - print ' exec perf record -e $E -b "$@"' - print ' set +x' - print 'fi' + exit 1 ;;''') + print("esac") + print("set -x") + print('if ! perf record -e $E -b "$@" ; then') + print(' # PEBS may not actually be working even if the processor supp= orts it') + print(' # (e.g., in a virtual machine). Trying to run without /p.') + print(' set +x') + print(' echo >&2 "Retrying without /p."') + print(' E=3D"$(echo "${E}" | sed -e \'s/\/p/\//\')"') + print(' set -x') + print(' exec perf record -e $E -b "$@"') + print(' set +x') + print('fi') =20 if cpufound =3D=3D 0 and not args.all: sys.exit('CPU %s not found' % cpu) diff --git a/gcc/config/i386/gcc-auto-profile b/gcc/config/i386/gcc-auto-pr= ofile index 56f64cbff1f..5ab224b041b 100755 --- a/gcc/config/i386/gcc-auto-profile +++ b/gcc/config/i386/gcc-auto-profile @@ -33,8 +33,15 @@ if grep -q hypervisor /proc/cpuinfo ; then echo >&2 "Warning: branch profiling may not be functional in VMs" fi =20 -case `egrep -q "^cpu family\s*: 6" /proc/cpuinfo && - egrep "^model\s*:" /proc/cpuinfo | head -n1` in +case `grep -E -q "^cpu family\s*: 6" /proc/cpuinfo && + grep -E "^model\s*:" /proc/cpuinfo | head -n1` in +model*:\ 46|\ +model*:\ 30|\ +model*:\ 31|\ +model*:\ 26|\ +model*:\ 47|\ +model*:\ 37|\ +model*:\ 44) E=3D"cpu/event=3D0x88,umask=3D0x40/$FLAGS" ;; model*:\ 55|\ model*:\ 77|\ model*:\ 76|\ @@ -43,6 +50,11 @@ model*:\ 95|\ model*:\ 87|\ model*:\ 133|\ model*:\ 122) E=3D"cpu/event=3D0xC4,umask=3D0xFE/p$FLAGS" ;; +model*:\ 28|\ +model*:\ 38|\ +model*:\ 39|\ +model*:\ 54|\ +model*:\ 53) E=3D"cpu/event=3D0x88,umask=3D0x41/$FLAGS" ;; model*:\ 42|\ model*:\ 45|\ model*:\ 58|\ @@ -63,23 +75,14 @@ model*:\ 165|\ model*:\ 166|\ model*:\ 85|\ model*:\ 85) E=3D"cpu/event=3D0xC4,umask=3D0x20/p$FLAGS" ;; -model*:\ 46|\ -model*:\ 30|\ -model*:\ 31|\ -model*:\ 26|\ -model*:\ 47|\ -model*:\ 37|\ -model*:\ 44) E=3D"cpu/event=3D0x88,umask=3D0x40/p$FLAGS" ;; -model*:\ 28|\ -model*:\ 38|\ -model*:\ 39|\ -model*:\ 54|\ -model*:\ 53) E=3D"cpu/event=3D0x88,umask=3D0x41/p$FLAGS" ;; model*:\ 126|\ model*:\ 140|\ model*:\ 141|\ +model*:\ 143|\ model*:\ 106|\ model*:\ 108) E=3D"cpu/event=3D0xc4,umask=3D0x20/p$FLAGS" ;; +model*:\ 134|\ +model*:\ 150) E=3D"cpu/event=3D0xc4,umask=3D0xfe/p$FLAGS" ;; *) echo >&2 "Unknown CPU. Run contrib/gen_autofdo_event.py --all --script to = update script." exit 1 ;; --=20 2.36.1