83def get_gpu_data():
84 """get structure about gpus that can be jsonified"""
85 global _gpu_json
86 if not _gpu_json is None:
87 return _gpu_json
88 _gpu_json = {}
89 try:
90 r = subprocess.run("lspci -vmm |grep -B1 -A 6 -i '3d controller'",
91 shell=True,
92 capture_output=True,
93 text=True,
94 check=True
95 )
96 except subprocess.CalledProcessError:
97
98 return _gpu_json
99 gpu_count = 0
100 vendor: list[str] = []
101 device: list[str] = []
102 rev: list[str] = []
103 numa_node: list[int] = []
104 for line in r.stdout.splitlines():
105 if line[0] == '-':
106 continue
107 cp = line.split(":", 1)
108 name = cp[0]
109 if name == "Class":
110 cname = cp[1].strip()
111 if cname == "3D controller":
112 gpu_count += 1
113 continue
114 if name == "Vendor":
115 vendor.append(cp[1].strip())
116 continue
117 if name == "Device":
118 device.append(cp[1].strip())
119 continue
120 if name == "Rev":
121 rev.append(cp[1].strip())
122 continue
123 if name == "NUMANode":
124 numa_node.append(int(cp[1].strip()))
125 continue
126 sz = gpu_count
127 if sz != len(vendor) or sz != len(device) or sz != len(rev) or sz != len(numa_node):
128 log.logger.error("add_host_section:get_gpu_data: size mismatch")
129 return _gpu_json
130 _gpu_json["gpu_count"] = gpu_count
131 gpu_list = []
132 for i in range(0,gpu_count):
133 gpu_block = {"gpu_number": i,
134 "numa_node": numa_node[i],
135 "vendor": vendor[i],
136 "device": device[i],
137 "rev": rev[i]
138 }
139 gpu_list.append(gpu_block)
140 _gpu_json["gpulist"] = gpu_list
141 return _gpu_json
142