From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7856) id B9C91382EA36; Sat, 27 Aug 2022 03:44:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B9C91382EA36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661571893; bh=65PU6PYcx5KbnJ8bLMcK9L8RvzTtjetAHMxv6Q4MhD4=; h=From:To:Subject:Date:From; b=IFdg8nrJR9FjewvSYsuh6Six8nsbG0ZdsjW8oGRN2xvquDECzXMfxk7ylImS14VTz aXn1RrKPCBMFG2KiF2TWHUKup/S1uc5mkCP6gM3Rz6ETucBDHfC6e616XVJw4ZXs+W G9TQpK0OmYeVY8R3HEpHu28ELe/I9wyzXjcclJoc= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Xi Ruoyao To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2234] contrib: modernize gen_autofdo_event.py X-Act-Checkin: gcc X-Git-Author: Xi Ruoyao X-Git-Refname: refs/heads/master X-Git-Oldrev: 16f542d6b866828e23e699e294e617ca428b04ee X-Git-Newrev: 152895769581468e98807bbb8835ef63cc07727d Message-Id: <20220827034453.B9C91382EA36@sourceware.org> Date: Sat, 27 Aug 2022 03:44:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:152895769581468e98807bbb8835ef63cc07727d commit r13-2234-g152895769581468e98807bbb8835ef63cc07727d Author: Xi Ruoyao Date: Mon Jun 27 14:15:22 2022 +0800 contrib: modernize gen_autofdo_event.py 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. Diff: --- 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 profiling. # 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=... # -import urllib2 +import urllib.request import sys import json import argparse import collections +import os baseurl = "https://download.01.org/perfmon" -target_events = (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 = ('BR_INST_RETIRED.NEAR_TAKEN', + 'BR_INST_EXEC.TAKEN', + 'BR_INST_RETIRED.TAKEN_JCC', + 'BR_INST_TYPE_RETIRED.COND_TAKEN') ap = argparse.ArgumentParser() ap.add_argument('--all', '-a', help='Print for all CPUs', action='store_true') @@ -71,47 +72,46 @@ def get_cpustr(): return "%s-%d-%X" % tuple(cpu)[:3] def find_event(eventurl, model): - print >>sys.stderr, "Downloading", eventurl - u = urllib2.urlopen(eventurl) + print("Downloading", eventurl, file = sys.stderr) + u = urllib.request.urlopen(eventurl) events = json.loads(u.read()) u.close() found = 0 for j in events: - if j[u'EventName'] in target_events: - event = "cpu/event=%s,umask=%s/" % (j[u'EventCode'], j[u'UMask']) - if u'PEBS' in j and j[u'PEBS'] > 0: + if j['EventName'] in target_events: + event = "cpu/event=%s,umask=%s/" % (j['EventCode'], j['UMask']) + if 'PEBS' in j and int(j['PEBS']) > 0: event += "p" if args.script: eventmap[event].append(model) else: - print j[u'EventName'], "event for model", model, "is", event + print(j['EventName'], "event for model", model, "is", event) found += 1 return found if not args.all: - cpu = get_cpu_str() + cpu = get_cpustr() if not cpu: sys.exit("Unknown CPU type") url = baseurl + "/mapfile.csv" -print >>sys.stderr, "Downloading", url -u = urllib2.urlopen(url) +print("Downloading", url, file = sys.stderr) +u = urllib.request.urlopen(url) found = 0 cpufound = 0 for j in u: - n = j.rstrip().split(',') + n = j.rstrip().decode().split(',') if len(n) >= 4 and (args.all or n[0] == cpu) and n[3] == "core": - if args.all: - components = n[0].split("-") - model = components[2] - model = int(model, 16) + components = n[0].split("-") + model = components[2] + model = int(model, 16) cpufound += 1 found += find_event(baseurl + n[2], model) u.close() 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 -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="%s$FLAGS" ;;' % (mod[-1], event) - print '''*) + print("model*:\ %s|\\" % m) + print('model*:\ %s) E="%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 supports it' - print ' # (e.g., in a virtual machine). Trying to run without /p.' - print ' set +x' - print ' echo >&2 "Retrying without /p."' - print ' E="$(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 supports it') + print(' # (e.g., in a virtual machine). Trying to run without /p.') + print(' set +x') + print(' echo >&2 "Retrying without /p."') + print(' E="$(echo "${E}" | sed -e \'s/\/p/\//\')"') + print(' set -x') + print(' exec perf record -e $E -b "$@"') + print(' set +x') + print('fi') if cpufound == 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-profile 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 -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="cpu/event=0x88,umask=0x40/$FLAGS" ;; model*:\ 55|\ model*:\ 77|\ model*:\ 76|\ @@ -43,6 +50,11 @@ model*:\ 95|\ model*:\ 87|\ model*:\ 133|\ model*:\ 122) E="cpu/event=0xC4,umask=0xFE/p$FLAGS" ;; +model*:\ 28|\ +model*:\ 38|\ +model*:\ 39|\ +model*:\ 54|\ +model*:\ 53) E="cpu/event=0x88,umask=0x41/$FLAGS" ;; model*:\ 42|\ model*:\ 45|\ model*:\ 58|\ @@ -63,23 +75,14 @@ model*:\ 165|\ model*:\ 166|\ model*:\ 85|\ model*:\ 85) E="cpu/event=0xC4,umask=0x20/p$FLAGS" ;; -model*:\ 46|\ -model*:\ 30|\ -model*:\ 31|\ -model*:\ 26|\ -model*:\ 47|\ -model*:\ 37|\ -model*:\ 44) E="cpu/event=0x88,umask=0x40/p$FLAGS" ;; -model*:\ 28|\ -model*:\ 38|\ -model*:\ 39|\ -model*:\ 54|\ -model*:\ 53) E="cpu/event=0x88,umask=0x41/p$FLAGS" ;; model*:\ 126|\ model*:\ 140|\ model*:\ 141|\ +model*:\ 143|\ model*:\ 106|\ model*:\ 108) E="cpu/event=0xc4,umask=0x20/p$FLAGS" ;; +model*:\ 134|\ +model*:\ 150) E="cpu/event=0xc4,umask=0xfe/p$FLAGS" ;; *) echo >&2 "Unknown CPU. Run contrib/gen_autofdo_event.py --all --script to update script." exit 1 ;;