From 3b95214fffafe68978fb82dde033e9b764b5fb4b Mon Sep 17 00:00:00 2001 From: "Galantsev, Dmitrii" Date: Thu, 14 Sep 2023 11:30:47 -0500 Subject: [PATCH] rsmiBindings.py - Add initRsmiBindings() Library path was printed at all times even with --json flag. This commit adds a mandatory initRsmiBindings function which is a core component of the rsmiBindings.py library. It **MUST** be called on import. Change-Id: Ic6ae1ec5d1fabba288910e6aed6c4706e53e5cd7 Signed-off-by: Galantsev, Dmitrii --- .gitignore | 5 ++- python_smi_tools/rocm_smi.py | 11 ++++-- python_smi_tools/rsmiBindings.py.in | 59 ++++++++++++++++------------- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 91cbeef563..1629ea81d3 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,7 @@ build/ .cache/ # Simulated SYSFS - for early development or debug -device/ \ No newline at end of file +device/ + +# Misc +__pycache__ diff --git a/python_smi_tools/rocm_smi.py b/python_smi_tools/rocm_smi.py index 8834e518b5..a8b3e5a74a 100755 --- a/python_smi_tools/rocm_smi.py +++ b/python_smi_tools/rocm_smi.py @@ -3507,8 +3507,7 @@ def save(deviceList, savefilepath): # The code below is for when this script is run as an executable instead of when imported as a module if __name__ == '__main__': parser = argparse.ArgumentParser( - description='AMD ROCm System Management Interface | ROCM-SMI version: %s | Kernel version: %s' % ( - __version__, getVersion(None, rsmi_sw_component_t.RSMI_SW_COMP_DRIVER)), + description=f'AMD ROCm System Management Interface | ROCM-SMI version: {__version__}', formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=90, width=120)) groupDev = parser.add_argument_group() groupDisplayOpt = parser.add_argument_group('Display Options') @@ -3668,6 +3667,11 @@ if __name__ == '__main__': args = parser.parse_args() + # Must set PRINT_JSON early so the prints can be silenced + if args.json or args.csv: + PRINT_JSON = True + # Initialize rsmiBindings + rocmsmi = initRsmiBindings(silent=PRINT_JSON) # Initialize the rocm SMI library initializeRsmi() @@ -3703,8 +3707,7 @@ if __name__ == '__main__': sys.exit(1) # If we want JSON/CSV output, initialize the keys (devices) - if args.json or args.csv: - PRINT_JSON = True + if PRINT_JSON: for device in deviceList: JSON_DATA['card' + str(device)] = {} diff --git a/python_smi_tools/rsmiBindings.py.in b/python_smi_tools/rsmiBindings.py.in index 9ffcac138d..e6b141889f 100644 --- a/python_smi_tools/rsmiBindings.py.in +++ b/python_smi_tools/rsmiBindings.py.in @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """ROCm_SMI_LIB CLI Tool Python Bindings""" +# NOTE: You MUST call rsmiBindings.initRsmiBindings() when using this library! # TODO: Get most (or all) of these from rocm_smi.h to avoid mismatches and redundancy from __future__ import print_function @@ -14,36 +15,42 @@ import os # relative path changed accordingly. # if ROCM_SMI_LIB_PATH is set, we can load 'librocm_smi64.so' from that location # +# Library load is wrapped in a function so prints can be hidden for PRINT_JSON mode. path_librocm = str() -rocm_smi_lib_path = os.getenv('ROCM_SMI_LIB_PATH') -if (rocm_smi_lib_path != None): - path_librocm = rocm_smi_lib_path -else: - path_librocm = os.path.dirname(os.path.realpath(__file__)) + '/../../@CMAKE_INSTALL_LIBDIR@/librocm_smi64.so.@VERSION_MAJOR@' +def initRsmiBindings(silent=False): + def print_silent(*args): + if not silent: + print(args) -if not os.path.isfile(path_librocm): - print('Unable to find %s . Trying /opt/rocm*' % path_librocm) - for root, dirs, files in os.walk('/opt', followlinks=True): - if 'librocm_smi64.so.@VERSION_MAJOR@' in files: - path_librocm = os.path.join(os.path.realpath(root), 'librocm_smi64.so.@VERSION_MAJOR@') - if os.path.isfile(path_librocm): - print('Using lib from %s' % path_librocm) + rocm_smi_lib_path = os.getenv('ROCM_SMI_LIB_PATH') + if (rocm_smi_lib_path != None): + path_librocm = rocm_smi_lib_path else: - print('Unable to find librocm_smi64.so.@VERSION_MAJOR@') -else: - print('Library loaded from: %s ' % path_librocm) + path_librocm = os.path.dirname(os.path.realpath(__file__)) + '/../../@CMAKE_INSTALL_LIBDIR@/librocm_smi64.so.@VERSION_MAJOR@' -# ----------> TODO: Support static libs as well as SO -try: - cdll.LoadLibrary(path_librocm) - rocmsmi = CDLL(path_librocm) -except OSError: - print('Unable to load the rocm_smi library.\n'\ - 'Set LD_LIBRARY_PATH to the folder containing librocm_smi64.so.@VERSION_MAJOR@\n'\ - '{0}Please refer to https://github.com/'\ - 'RadeonOpenCompute/rocm_smi_lib for the installation guide.{1}'\ - .format('\33[33m', '\033[0m')) - exit() + if not os.path.isfile(path_librocm): + print_silent('Unable to find %s . Trying /opt/rocm*' % path_librocm) + for root, dirs, files in os.walk('/opt', followlinks=True): + if 'librocm_smi64.so.@VERSION_MAJOR@' in files: + path_librocm = os.path.join(os.path.realpath(root), 'librocm_smi64.so.@VERSION_MAJOR@') + if os.path.isfile(path_librocm): + print_silent('Using lib from %s' % path_librocm) + else: + print('Unable to find librocm_smi64.so.@VERSION_MAJOR@') + else: + print_silent('Library loaded from: %s ' % path_librocm) + + # ----------> TODO: Support static libs as well as SO + try: + cdll.LoadLibrary(path_librocm) + return CDLL(path_librocm) + except OSError: + print('Unable to load the rocm_smi library.\n'\ + 'Set LD_LIBRARY_PATH to the folder containing librocm_smi64.so.@VERSION_MAJOR@\n'\ + '{0}Please refer to https://github.com/'\ + 'RadeonOpenCompute/rocm_smi_lib for the installation guide.{1}'\ + .format('\33[33m', '\033[0m')) + exit() # Device ID dv_id = c_uint64()