public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eugene Rozenfeld <Eugene.Rozenfeld@microsoft.com>
To: Xi Ruoyao <xry111@xry111.site>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	Andi Kleen <ak@linux.intel.com>
Subject: RE: [EXTERNAL] [PATCH] contrib: modernize gen_autofdo_event.py
Date: Fri, 5 Aug 2022 23:07:54 +0000	[thread overview]
Message-ID: <MW2PR2101MB1770C86C516695879A958B3C919E9@MW2PR2101MB1770.namprd21.prod.outlook.com> (raw)
In-Reply-To: <0c00f719092529e0a902fab5a65152f055836348.camel@xry111.site>

The changes look good to me. Also adding Andi, the author of the script.

Eugene

-----Original Message-----
From: Gcc-patches <gcc-patches-bounces+erozen=microsoft.com@gcc.gnu.org> On Behalf Of Xi Ruoyao via Gcc-patches
Sent: Sunday, June 26, 2022 11:15 PM
To: gcc-patches@gcc.gnu.org
Subject: [EXTERNAL] [PATCH] 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.
---
 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 ;;
--
2.36.1


  reply	other threads:[~2022-08-05 23:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27  6:15 Xi Ruoyao
2022-08-05 23:07 ` Eugene Rozenfeld [this message]
2022-08-06  6:29   ` [EXTERNAL] " Andi Kleen
2022-08-26 22:20     ` Eugene Rozenfeld
2022-08-27  3:45       ` Xi Ruoyao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MW2PR2101MB1770C86C516695879A958B3C919E9@MW2PR2101MB1770.namprd21.prod.outlook.com \
    --to=eugene.rozenfeld@microsoft.com \
    --cc=ak@linux.intel.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).