code: def parse_nvidia_smi_result(smi, outputDir):
      old_umask = os.umask(0)
      try:
          xmldoc = minidom.parseString(smi)
          gpuList = xmldoc.getElementsByTagName("gpu")
          with open(
              os.path.join(outputDir, "gpu_metrics"), "a"
          ) as outputFile:
              outPut = {}
              outPut["Timestamp"] = time.asctime(time.localtime())
              outPut["gpuCount"] = len(gpuList)
              outPut["gpuInfos"] = []
              for gpuIndex, gpu in enumerate(gpuList):
                  gpuInfo = {}
                  gpuInfo["index"] = gpuIndex
                  gpuInfo["gpuUtil"] = (
                      gpu.getElementsByTagName("utilization")[0]
                      .getElementsByTagName("gpu_util")[0]
                      .childNodes[0]
                      .data.replace("%", "")
                      .strip()
                  )
                  gpuInfo["gpuMemUtil"] = (
                      gpu.getElementsByTagName("utilization")[0]
                      .getElementsByTagName("memory_util")[0]
                      .childNodes[0]
                      .data.replace("%", "")
                      .strip()
                  )
                  processes = gpu.getElementsByTagName("processes")
                  runningProNumber = len(
                      processes[0].getElementsByTagName("process_info")
                  )
                  gpuInfo["activeProcessNum"] = runningProNumber
  
                  outPut["gpuInfos"].append(gpuInfo)
              print(outPut)
              outputFile.write(
                  "{}\n".format(json.dumps(outPut, sort_keys=True))
              )
              outputFile.flush()
      except Exception as error:
          # e_info = sys.exc_info()
          print("gpu_metrics_collector error: %s" % error)
      finally:
          os.umask(old_umask)
  
file_path: nni/nni/tools/gpu_tool/gpu_metrics_collector.py
imports: import json
  import os
  import subprocess
  import sys
  import time
  import traceback
  from xml.dom import minidom
label_desc: Use image node_id 2 for calling a global function with example usage: parse_nvidia_smi_result(smi, outputDir) without return types