Skip to contentSkip to conversation

tools/power: amd_pstate_trace: fix help path dependencies

PATCH In review Yousef Alhouseen 4 participants 5 messages Jun 24 – Jul 30 AMD pstate driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/power: amd_pstate_trace: fix help path dependencies

amd_pstate_trace imports Gnuplot and numpy before parsing command-line options. As a result, even "-h" fails if those optional runtime modules are not installed.

It also handles "-h" by printing a blank line and exiting. Use the existing help printer for "-h", and move the numpy import to the path that needs it. The direct Gnuplot import is unused, so remove it.

---
 tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +5 -6
index 875b08655..b1b201b2e 100755
@@ -27,8 +27,6 @@ import re
 import signal
 import sys
 import getopt
-import Gnuplot
-from numpy import *
 from decimal import *
 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "intel_pstate_tracer"))
 import intel_pstate_tracer as ipt
@@ -260,9 +258,6 @@ graph_data_present = False;
 valid1 = False
 valid2 = False
 
-cpu_mask = zeros((MAX_CPUS,), dtype=int)
-
-
 try:
     opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="])
 except getopt.GetoptError:
@@ -270,7 +265,7 @@ except getopt.GetoptError:
     sys.exit(2)
 for opt, arg in opts:
     if opt == '-h':
-        print()
+        ipt.print_help('amd_pstate')
         sys.exit()
     elif opt in ("-t", "--trace_file"):
         valid1 = True
@@ -291,6 +286,10 @@ if not (valid1 and valid2):
     ipt.print_help('amd_pstate')
     sys.exit()
 
+from numpy import zeros
+
+cpu_mask = zeros((MAX_CPUS,), dtype=int)
+
 if cpu_list:
     for p in re.split("[,]", cpu_list):
         if int(p) < MAX_CPUS :
Signature · 1 line
2.54.0
#1 lore raw
Quoted 10 lines · Yousef Alhouseen

amd_pstate_trace imports Gnuplot and numpy before parsing command-line options. As a result, even "-h" fails if those optional runtime modules are not installed.

It also handles "-h" by printing a blank line and exiting. Use the existing help printer for "-h", and move the numpy import to the path that needs it. The direct Gnuplot import is unused, so remove it.

AMD folks, if this change is fine with you, I can take it into pm-tools, but an ACK (or equivalent) is needed.

Thanks!

Quoted 51 lines
---
 tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +5 -7
index 875b08655..b1b201b2e 100755
@@ -27,8 +27,6 @@ import re
 import signal
 import sys
 import getopt
-import Gnuplot
-from numpy import *
 from decimal import *
 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "intel_pstate_tracer"))
 import intel_pstate_tracer as ipt
@@ -260,9 +258,6 @@ graph_data_present = False;
 valid1 = False
 valid2 = False
 
-cpu_mask = zeros((MAX_CPUS,), dtype=int)
-
-
 try:
     opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="])
 except getopt.GetoptError:
@@ -270,7 +265,7 @@ except getopt.GetoptError:
     sys.exit(2)
 for opt, arg in opts:
     if opt == '-h':
-        print()
+        ipt.print_help('amd_pstate')
         sys.exit()
     elif opt in ("-t", "--trace_file"):
         valid1 = True
@@ -291,6 +286,10 @@ if not (valid1 and valid2):
     ipt.print_help('amd_pstate')
     sys.exit()
 
+from numpy import zeros
+
+cpu_mask = zeros((MAX_CPUS,), dtype=int)
+
 if cpu_list:
     for p in re.split("[,]", cpu_list):
         if int(p) < MAX_CPUS :
--

2.54.0

#2 lore raw

Hello Rafael,

Quoted 7 lines · Rafael J. Wysocki (Intel)
Quoted 5 lines · Yousef Alhouseen

amd_pstate_trace imports Gnuplot and numpy before parsing command-line options. As a result, even "-h" fails if those optional runtime modules are not installed.

Quoted 8 lines
Quoted 5 lines

It also handles "-h" by printing a blank line and exiting. Use the existing help printer for "-h", and move the numpy import to the path that needs it. The direct Gnuplot import is unused, so remove it.

AMD folks, if this change is fine with you, I can take it into pm-tools, but an ACK (or equivalent) is needed.

Sorry for the delay in reviewing! I have some comments below for Yousef.

Quoted 7 lines

Thanks!

Quoted 4 lines
---
 tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
Quoted 9 lines

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py import re · line 30

Quoted 9 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -1
index 875b08655..b1b201b2e 100755
@@ -27,8 +27,6 @@ import re
 import signal
 import sys
 import getopt
-import Gnuplot

We don't use Gnuplot so removing that is fine ...

Quoted 1 line

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py import re · line 31

Quoted 1 line
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -1
-from numpy import *

... but we want to keep all the imports together at top to easily audit dependencies. Afacit, most python scripts in tools/power/x86/ follow that convention.

There exists a big fat comment on top of the file reading:

Prerequisites:
Python version 2.7.x or higher
gnuplot 5.0 or higher
gnuplot-py 1.8 or higher

which is a good indication of whatis actually required to run the script. If users prefer, we can add a requirements.txt that pip can consume to install the necessary dependencies and make it clear.

Quoted 26 lines
Quoted 26 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +1 -4
 from decimal import *
 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "intel_pstate_tracer"))
 import intel_pstate_tracer as ipt
@@ -260,9 +258,6 @@ graph_data_present = False;
 valid1 = False
 valid2 = False
 
-cpu_mask = zeros((MAX_CPUS,), dtype=int)
-
-
 try:
     opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="])
 except getopt.GetoptError:
@@ -270,7 +265,7 @@ except getopt.GetoptError:
     sys.exit(2)
 for opt, arg in opts:
     if opt == '-h':
-        print()
+        ipt.print_help('amd_pstate')
         sys.exit()
     elif opt in ("-t", "--trace_file"):
         valid1 = True
@@ -291,6 +286,10 @@ if not (valid1 and valid2):
     ipt.print_help('amd_pstate')
     sys.exit()
 
Quoted 1 line

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py if not (valid1 and valid2): · line 289

Quoted 1 line
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +1 -0
+from numpy import zeros

Having it here needs users to skim through the file to know what external packages the script needs and we don't want that.

Quoted 10 lines
Quoted 10 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +3 -1
+
+cpu_mask = zeros((MAX_CPUS,), dtype=int)
+
 if cpu_list:
     for p in re.split("[,]", cpu_list):
         if int(p) < MAX_CPUS :
--

2.54.0

Signature · 2 lines
Thanks and Regards,
Prateek
#3 lore raw
Quoted 14 lines · K Prateek Nayak

Hello Rafael,

Quoted 11 lines · Rafael J. Wysocki (Intel)
Quoted 9 lines · Yousef Alhouseen

amd_pstate_trace imports Gnuplot and numpy before parsing command-line options. As a result, even "-h" fails if those optional runtime modules are not installed.

It also handles "-h" by printing a blank line and exiting. Use the existing help printer for "-h", and move the numpy import to the path that needs it. The direct Gnuplot import is unused, so remove it.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

AMD folks, if this change is fine with you, I can take it into pm-tools, but an ACK (or equivalent) is needed.

Sorry for the delay in reviewing! I have some comments below for Yousef.

Yes; sorry this got buried in my inbox when I was out and I still haven't all the way dug out.

Quoted 28 lines
Quoted 8 lines

Thanks!

Quoted 4 lines
---
  tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py | 11 +++++------
  1 file changed, 5 insertions(+), 6 deletions(-)
Quoted 9 lines

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py import re · line 30

Quoted 9 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -1
index 875b08655..b1b201b2e 100755
@@ -27,8 +27,6 @@ import re
  import signal
  import sys
  import getopt
-import Gnuplot

We don't use Gnuplot so removing that is fine ...

Quoted 1 line

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py import re · line 31

Quoted 1 line
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -1
-from numpy import *

... but we want to keep all the imports together at top to easily audit dependencies. Afacit, most python scripts in tools/power/x86/ follow that convention.

There exists a big fat comment on top of the file reading:

Quoted 8 lines

Prerequisites:
Python version 2.7.x or higher
gnuplot 5.0 or higher
gnuplot-py 1.8 or higher

which is a good indication of whatis actually required to run the script. If users prefer, we can add a requirements.txt that pip can consume to install the necessary dependencies and make it clear.

I think a requirements.txt and a quick few steps of how to start a venv and use it is a great idea.

Quoted 43 lines
Quoted 27 lines
Quoted 27 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +1 -4
  from decimal import *
  sys.path.append(os.path.join(os.path.dirname(__file__), "..", "intel_pstate_tracer"))
  import intel_pstate_tracer as ipt
@@ -260,9 +258,6 @@ graph_data_present = False;
  valid1 = False
  valid2 = False
 
-cpu_mask = zeros((MAX_CPUS,), dtype=int)
-
-
  try:
      opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="])
  except getopt.GetoptError:
@@ -270,7 +265,7 @@ except getopt.GetoptError:
      sys.exit(2)
  for opt, arg in opts:
      if opt == '-h':
-        print()
+        ipt.print_help('amd_pstate')
          sys.exit()
      elif opt in ("-t", "--trace_file"):
          valid1 = True
@@ -291,6 +286,10 @@ if not (valid1 and valid2):
      ipt.print_help('amd_pstate')
      sys.exit()
 
Quoted 1 line

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py if not (valid1 and valid2): · line 289

Quoted 1 line
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +1 -0
+from numpy import zeros

Having it here needs users to skim through the file to know what external packages the script needs and we don't want that.

Quoted 10 lines
Quoted 10 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +3 -1
+
+cpu_mask = zeros((MAX_CPUS,), dtype=int)
+
  if cpu_list:
      for p in re.split("[,]", cpu_list):
          if int(p) < MAX_CPUS :
--

2.54.0

#4 lore raw

Hi Prateek, Mario,

Agreed. Moving the NumPy import below argument parsing is not the right way to manage a required dependency. Please drop this version.

I will keep the NumPy import at module scope, split out the narrow -h output fix, and handle dependency/venv documentation separately after verifying the shared requirements for both tracers. The unused Gnuplot import in amd_pstate_trace can still be removed as a separate cleanup.

Thanks for the guidance.

Regards,
Yousef

Quoted 108 lines · Mario Limonciello
Quoted 14 lines · K Prateek Nayak

Hello Rafael,

Quoted 11 lines · Rafael J. Wysocki (Intel)
Quoted 9 lines · Yousef Alhouseen

amd_pstate_trace imports Gnuplot and numpy before parsing command-line options. As a result, even "-h" fails if those optional runtime modules are not installed.

It also handles "-h" by printing a blank line and exiting. Use the existing help printer for "-h", and move the numpy import to the path that needs it. The direct Gnuplot import is unused, so remove it.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

AMD folks, if this change is fine with you, I can take it into pm-tools, but an ACK (or equivalent) is needed.

Sorry for the delay in reviewing! I have some comments below for Yousef.

Yes; sorry this got buried in my inbox when I was out and I still haven't all the way dug out.

Quoted 28 lines
Quoted 8 lines

Thanks!

Quoted 4 lines

---
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py | 11 +++++------

1 file changed, 5 insertions(+), 6 deletions(-)
Quoted 9 lines
Quoted 9 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -0
index 875b08655..b1b201b2e 100755
@@ -27,8 +27,6 @@ import re

import signal
import sys
import getopt
-import Gnuplot

We don't use Gnuplot so removing that is fine ...

Quoted 1 line
Quoted 1 line

-from numpy import *

... but we want to keep all the imports together at top to easily audit dependencies. Afacit, most python scripts in tools/power/x86/ follow that convention.

There exists a big fat comment on top of the file reading:

Quoted 8 lines

Prerequisites:
Python version 2.7.x or higher
gnuplot 5.0 or higher
gnuplot-py 1.8 or higher

which is a good indication of whatis actually required to run the script. If users prefer, we can add a requirements.txt that pip can consume to install the necessary dependencies and make it clear.

I think a requirements.txt and a quick few steps of how to start a venv and use it is a great idea.

Quoted 43 lines
Quoted 27 lines
Quoted 27 lines

from decimal import * sys.path.append(os.path.join(os.path.dirname(__file__), "..", "intel_pstate_tracer")) import intel_pstate_tracer as ipt

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -0
@@ -260,9 +258,6 @@ graph_data_present = False;

valid1 = False
valid2 = False

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -3
-cpu_mask = zeros((MAX_CPUS,), dtype=int)
-
-

try:
opts, args = getopt.getopt(sys.argv[1:],"ht:i:c:n:m:",["help","trace_file=","interval=","cpu=","name=","memory="]) except getopt.GetoptError:

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -0
@@ -270,7 +265,7 @@ except getopt.GetoptError:
 sys.exit(2)

for opt, arg in opts:
if opt == '-h':
- print()
+ ipt.print_help('amd_pstate')
sys.exit()
elif opt in ("-t", "--trace_file"):
valid1 = True

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +0 -0
@@ -291,6 +286,10 @@ if not (valid1 and valid2):
 ipt.print_help('amd_pstate')
 sys.exit()
 
Quoted 1 line

tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py if not (valid1 and valid2): · line 289

Quoted 1 line
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +1 -0
+from numpy import zeros

Having it here needs users to skim through the file to know what external packages the script needs and we don't want that.

Quoted 10 lines
Quoted 10 lines
tools/power/x86/amd_pstate_tracer/amd_pstate_trace.py +3 -0
+
+cpu_mask = zeros((MAX_CPUS,), dtype=int)
+

if cpu_list:
for p in re.split("[,]", cpu_list):
if int(p) < MAX_CPUS :
--
2.54.0