gpu.py 2.81 KB
Newer Older
xuanbaby's avatar
DTK-x  
xuanbaby committed
1
2
3
4
5
6
7
8
9
10
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import subprocess
import time
import traceback
from xml.dom import minidom


def collect_gpu_usage(node_id):
xuanbaby's avatar
DTK-203  
xuanbaby committed
11
    cmd = 'rocm-smi -a --json'.split()
xuanbaby's avatar
DTK-x  
xuanbaby committed
12
13
14
15
16
17
18
19
20
21
22
23
24
    info = None
    try:
        smi_output = subprocess.check_output(cmd)
        info = parse_nvidia_smi_result(smi_output)
    except Exception:
        traceback.print_exc()
        info = gen_empty_gpu_metric()
    return info


def parse_nvidia_smi_result(smi):
    try:
        output = {}
xuanbaby's avatar
DTK-203  
xuanbaby committed
25
26
27
28
#        xmldoc = minidom.parseString(smi)
#        gpuList = xmldoc.getElementsByTagName('gpu')
        smi = json.loads(smi)
        gpuList = smi.keys()
xuanbaby's avatar
DTK-x  
xuanbaby committed
29
30
31
32
        output["Timestamp"] = time.asctime(time.localtime())
        output["gpuCount"] = len(gpuList)
        output["gpuInfos"] = []
        for gpuIndex, gpu in enumerate(gpuList):
xuanbaby's avatar
DTK-203  
xuanbaby committed
33
            if gpu == 'system':  
xuanbaby's avatar
DTK-203  
xuanbaby committed
34
              continue
xuanbaby's avatar
DTK-x  
xuanbaby committed
35
36
            gpuInfo = {}
            gpuInfo['index'] = gpuIndex
xuanbaby's avatar
DTK-203  
xuanbaby committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
            gpuInfo['gpuUtil'] = smi[gpu]["GPU OverDrive value (%)"]
            gpuInfo['gpuMemUtil'] = smi[gpu]["GPU Memory OverDrive value (%)"]
#            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
            gpuInfo['gpuType'] = smi[gpu]["GPU ID"]
#            gpuInfo['gpuType'] = gpu.getElementsByTagName('product_name')[0]\
#                .childNodes[0].data
#            memUsage = gpu.getElementsByTagName('fb_memory_usage')[0]
            gpuInfo['gpuMemUsed'] = smi[gpu]["GPU use (%)"]
#            gpuInfo['gpuMemTotal'] = memUsage.getElementsByTagName('total')[0]\
#                .childNodes[0].data.replace("MiB", "").strip()
#            gpuInfo['gpuMemUsed'] = memUsage.getElementsByTagName('used')[0]\
#                .childNodes[0].data.replace("MiB", "").strip()
#            gpuInfo['gpuMemFree'] = memUsage.getElementsByTagName('free')[0]\
#                .childNodes[0].data.replace("MiB", "").strip()
xuanbaby's avatar
DTK-x  
xuanbaby committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

            output["gpuInfos"].append(gpuInfo)
    except Exception:
        traceback.print_exc()
        output = {}
    return output


def gen_empty_gpu_metric():
    try:
        output = {}
        output["Timestamp"] = time.asctime(time.localtime())
        output["gpuCount"] = 0
        output["gpuInfos"] = []
    except Exception:
        traceback.print_exc()
        output = {}
    return output