From 6ea0c8b524a98ff5d94217e1dd404773b037dede Mon Sep 17 00:00:00 2001 From: Ori Messinger Date: Wed, 23 Sep 2020 16:33:01 -0400 Subject: [PATCH] ROCm SMI Python CLI: Check for amdgpu Driver Initialization The purpose of this patch is to check for amdgpu driver initialization before attempting to initialize rocmsmi in the CLI. Additionally, since the '--help' functionality does not rely on anything external to the CLI, it can now be called without the driver initialized. Change-Id: I2fcce60ca6d9f77835549e3558c4bb1747499c5c Signed-off-by: Ori Messinger [ROCm/amdsmi commit: e3c9aec71443e608369dc00b808b53fe61b8715c] --- projects/amdsmi/python_smi_tools/rocm_smi.py | 34 +++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/projects/amdsmi/python_smi_tools/rocm_smi.py b/projects/amdsmi/python_smi_tools/rocm_smi.py index 555f92f1c6..ed79d966e7 100755 --- a/projects/amdsmi/python_smi_tools/rocm_smi.py +++ b/projects/amdsmi/python_smi_tools/rocm_smi.py @@ -57,11 +57,18 @@ validClockNames = clk_type_names[1:-2] validClockNames.append('pcie') validClockNames.sort() -# Check for correct initialization value -ret_init = rocmsmi.rsmi_init(0) -if ret_init != 0: - logging.error('ROCm SMI returned %s (the expected value is 0)', ret_init) - exit(ret_init) +def driverInitialized(): + """ Returns true if amdgpu is found in the list of initialized modules + """ + driverInitialized = '' + try: + driverInitialized = str(subprocess.check_output("cat /proc/modules|grep amdgpu", shell=True)) + except subprocess.CalledProcessError: + pass + if len(driverInitialized) > 0: + return True + return False + def formatJson(device, log): """ Print out in JSON format @@ -2132,6 +2139,20 @@ def doesDeviceExist(device): return False +def initializeRsmi(): + """ initializes rocmsmi if the amdgpu driver is initialized + """ + # Check if amdgpu is initialized before initializing rsmi + if driverInitialized() is True: + ret_init = rocmsmi.rsmi_init(0) + if ret_init != 0: + logging.error('ROCm SMI returned %s (the expected value is 0)', ret_init) + exit(ret_init) + else: + logging.error('Driver not initialized (amdgpu not found in modules)') + exit(0) + + def isAmdDevice(device): """ Return whether the specified device is an AMD device or not @@ -2444,6 +2465,9 @@ if __name__ == '__main__': args = parser.parse_args() + # Initialize the rocm SMI library + initializeRsmi() + logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING) if args.loglevel is not None: numericLogLevel = getattr(logging, args.loglevel.upper(), logging.WARNING)