import json
import xml.dom.minidom
txt = """\
<?xml version="1.0" encoding="UTF-8" ?>
<nvidia_smi_log>
<timestamp>Thu Aug 29 13:45:26 2019</timestamp>
<driver_version>387.34</driver_version>
<attached_gpus>9</attached_gpus>
<gpu id="00000000:03:00.0">
<product_name>P106-090</product_name>
<product_brand>GeForce</product_brand>
<display_mode>Disabled</display_mode>
<display_active>Disabled</display_active>
<persistence_mode>Enabled</persistence_mode>
<accounting_mode>Disabled</accounting_mode>
<accounting_mode_buffer_size>1920</accounting_mode_buffer_size>
<driver_model>
<current_dm>N/A</current_dm>
<pending_dm>N/A</pending_dm>
</driver_model>
<serial>N/A</serial>
</gpu>
<gpu id="00000000:81:00.0">
<product_name>P106-090</product_name>
<product_brand>GeForce</product_brand>
<display_mode>Disabled</display_mode>
<display_active>Disabled</display_active>
<persistence_mode>Enabled</persistence_mode>
<accounting_mode>Disabled</accounting_mode>
<accounting_mode_buffer_size>1920</accounting_mode_buffer_size>
<driver_model>
<current_dm>N/A</current_dm>
<pending_dm>N/A</pending_dm>
</driver_model>
<serial>N/A</serial>
</gpu>
</nvidia_smi_log>
"""
def get_cnode_data(pnode, cnode_name):
cnode = pnode.getElementsByTagName(cnode_name)[0]
return cnode.childNodes[0].data
gpu_list = []
domTree = xml.dom.minidom.parseString(txt)
nvidia_smi_log = domTree.documentElement
gpus = nvidia_smi_log.getElementsByTagName("gpu")
for i, gpu in enumerate(gpus):
gpu_dict = {}
if gpu.hasAttribute("id"):
print("id:", gpu.getAttribute("id"))
else:
continue
gpu_dict.update({
"gpu_idx": str(i),
"driver_version": get_cnode_data(nvidia_smi_log, "driver_version"),
"product_name": get_cnode_data(gpu, "product_name"),
"product_brand": get_cnode_data(gpu, "product_brand"),
})
gpu_list.append(json.dumps(gpu_dict))
print(gpu_list)
|