1From 36b9aa5a8e071ac6349d2d7f9c23a25abcdc316d Mon Sep 17 00:00:00 2001 2From: Adam Duskett <aduskett@gmail.com> 3Date: Tue, 2 Nov 2021 10:30:55 -0700 4Subject: [PATCH] pmu-query.py: fix python3 errors, add linux platform support 5 6Unfortuantly, commit 0212b382624c744491a845c75dfb2a527d4a821f broke pmu-query 7in some unexpected ways. 8 9First, urllib.request.urlopen returns a bytes-object in Python3, which results 10in the csv.DictReader throwing the error: `TypeError: initial_value must be 11str or None, not HTTPResponse` A simple .read().decode('utf-8') appended to 12the end of urlopen fixes the error. 13 14Second, passing the map_file_raw string to DictReader results in a malformed 15dictionary. Fix this by wrapping the raw text string in io.StringIO(). 16 17Third: During the python2 -> python3 refactoring, I accidentally switched some 18logic in the pull request. `if core_path != ''` changed to `if not core_path`, 19which breaks the logic, the same goes for 20`if offcore_path != ''` -> `if not offcore_path`. Change these to 21`if core_path` and `if offcore_path` respectively. 22 23From upstream commit: 7a670261c2063595f2330e6cc2a7f19eb18b6ea8 24 25Signed-off-by: Adam Duskett <aduskett@gmail.com> 26--- 27 pmu-query.py | 20 ++++++++++++++------ 28 1 file changed, 14 insertions(+), 6 deletions(-) 29 30diff --git a/pmu-query.py b/pmu-query.py 31index 5595819..bc1e57b 100755 32--- a/pmu-query.py 33+++ b/pmu-query.py 34@@ -1,4 +1,5 @@ 35 #!/usr/bin/env python3 36+import io 37 import urllib.request 38 import urllib.parse 39 import json 40@@ -8,6 +9,7 @@ import sys 41 import platform 42 import getopt 43 import re 44+import shutil 45 46 all_flag = False 47 download_flag = False 48@@ -29,8 +31,8 @@ except getopt.GetoptError as err: 49 sys.exit(-2) 50 51 if filename is None: 52- map_file_raw = urllib.request.urlopen("https://download.01.org/perfmon/mapfile.csv") 53- map_dict = csv.DictReader(map_file_raw) 54+ map_file_raw = urllib.request.urlopen("https://download.01.org/perfmon/mapfile.csv").read().decode('utf-8') 55+ map_dict = csv.DictReader(io.StringIO(map_file_raw), delimiter=',') 56 map_file = [] 57 core_path = "" 58 offcore_path = "" 59@@ -45,20 +47,26 @@ if filename is None: 60 p = subprocess.Popen(["./pcm-core.exe -c"], stdout=subprocess.PIPE, shell=True) 61 elif platform.system() == "Windows": 62 p = subprocess.Popen(["pcm-core.exe", "-c"], stdout=subprocess.PIPE, shell=True) 63+ elif platform.system() == "Linux": 64+ pcm_core = shutil.which("pcm-core") 65+ if not pcm_core: 66+ print("Could not find pcm-core executable!") 67+ sys.exit(-1) 68+ p = subprocess.Popen([pcm_core, "-c"], stdout=subprocess.PIPE, shell=True) 69 else: 70 p = subprocess.Popen(["./pcm-core.x -c"], stdout=subprocess.PIPE, shell=True) 71 72 (output, err) = p.communicate() 73 p_status = p.wait() 74 for model in map_file: 75- if re.search(model["Family-model"], output): 76+ if re.search(model["Family-model"], output.decode("utf-8")): 77 if model["EventType"] == "core": 78 core_path = model["Filename"] 79 elif model["EventType"] == "offcore": 80 offcore_path = model["Filename"] 81 print(model) 82 83- if not core_path: 84+ if core_path: 85 json_core_data = urllib.request.urlopen( 86 "https://download.01.org/perfmon" + core_path 87 ) 88@@ -67,10 +75,10 @@ if filename is None: 89 with open(core_path.split("/")[-1], "w") as outfile: 90 json.dump(core_events, outfile, sort_keys=True, indent=4) 91 else: 92- print("no core event found for %s CPU, program abort..." % output) 93+ print("no core event found for %s CPU, program abort..." % output.decode("utf-8")) 94 sys.exit(-1) 95 96- if not offcore_path: 97+ if offcore_path: 98 json_offcore_data = urllib.request.urlopen( 99 "https://download.01.org/perfmon" + offcore_path 100 ) 101-- 1022.32.0 103 104