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 <dmitrii.galantsev@amd.com>
Bu işleme şunda yer alıyor:
Galantsev, Dmitrii
2023-09-14 11:30:47 -05:00
işlemeyi yapan: Dmitrii Galantsev
ebeveyn 5c574ac79c
işleme 3b95214fff
3 değiştirilmiş dosya ile 44 ekleme ve 31 silme
+4 -1
Dosyayı Görüntüle
@@ -17,4 +17,7 @@ build/
.cache/
# Simulated SYSFS - for early development or debug
device/
device/
# Misc
__pycache__
+7 -4
Dosyayı Görüntüle
@@ -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)] = {}
+33 -26
Dosyayı Görüntüle
@@ -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()