0001-pmu-query.py-fix-python3-errors-add-linux-platform-s.patch 3.8 KB

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