-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintelgputop.py
executable file
·179 lines (153 loc) · 6.16 KB
/
intelgputop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
""" IntelGPUTop class, encapsulates data from intel_gpu_top utility
"""
from subprocess import Popen, run, PIPE, CalledProcessError
from re import findall
import json
from threading import Thread, Lock
class IntelGPUTop:
def __init__(self):
self.lock = Lock()
self.count = 0
command = r'lspci -vvnn | grep -A 2 "\[0300\]" | grep -A 2 Intel'
intel_gpu_data = run([command], shell=True, check=True, stdout=PIPE, stderr=PIPE)
intel_gpu_data = intel_gpu_data.stdout
intel_gpu_data = intel_gpu_data.decode('utf-8').split("\n--\n")
self.count = len(intel_gpu_data)
pattern = r"\[(\w{4}:\w{4})\]"
intel_devs = []
intel_subvens = []
for intel_gpu in intel_gpu_data:
vendev, subvendev = findall(pattern=pattern, string=intel_gpu)
intel_devs.append(vendev.split(":")[1])
intel_subvens.append(subvendev.split(":")[0])
command = "sudo intel_gpu_top -L | grep 8086 | awk '{print $3}'"
intel_pci_strs = run([command], shell=True, check=True, stdout=PIPE, stderr=PIPE)
intel_pci_strs = intel_pci_strs.stdout.decode('utf-8').split("\n")
intel_pci_strs = [x for x in intel_pci_strs if x]
self.data = {}
for i in range(self.count):
name = f"Device_{str(intel_devs[i])}-{i}"
command = f"cat inteldevids | grep -i \"{intel_devs[i]},\""
try:
output = run([command],
shell=True,
check=True,
stdout=PIPE,
stderr=PIPE).stdout.decode('utf-8')
output = output.replace("\n", "")
name = f"{output[5:]}-{i}"
except CalledProcessError:
pass
subven = str(intel_subvens[i])
command = f"cat venids | grep -i \"{subven},\""
try:
output = run([command],
shell=True,
check=True,
stdout=PIPE,
stderr=PIPE).stdout.decode('utf-8')
subven = output[5:]
except CalledProcessError:
pass
self.data[name] = {}
self.running = True
self.threads = []
for pci_str in intel_pci_strs:
self.threads.append(Thread(target=self.monitor, args=(pci_str,name)))
for thread in self.threads:
thread.start()
def monitor(self, dev, name):
with Popen(['sudo', 'intel_gpu_top', '-J', '-s', '10000', '-d', dev],
stdout=PIPE,
stderr=PIPE,
text=True,
bufsize=1,
universal_newlines=True) as process:
sample = ""
balance = 0
started = False # This flag will help us ignore data until the first {
for line in iter(process.stdout.readline, ''):
print(line)
if not self.running:
process.terminate()
return
if '{' in line:
started = True
if not started:
continue
sample += line
balance += line.count('{') - line.count('}')
print(balance)
if balance == 0:
# Extract samples from the sample string by splitting it at the comma
# followed by an open brace
samples = sample.strip('[]').split('},{')
samples = ['{' + s + '}' for s in samples]
for single_sample in samples:
with open("single_sample", 'a', encoding='utf-8') as file:
file.write(single_sample)
with self.lock:
self.data[name] = json.loads(single_sample)
sample = ""
started = False
error_output = process.stderr.read()
if error_output:
print("Error:", error_output)
def get_gpu_names(self):
return list(self.data.keys())
def print_data(self):
print(self.data)
# Period methods
def get_period_duration(self, name):
with self.lock:
ret = self.data[name].get('period', {})
return ret.get('duration', None)
def get_period_unit(self, name):
with self.lock:
ret = self.data[name].get('period', {})
return ret.get('unit', None)
# Frequency methods
def get_frequency_requested(self, name):
with self.lock:
ret = self.data[name].get('frequency', {})
return ret.get('requested', None)
def get_frequency_actual(self, name):
with self.lock:
return self.data[name]['frequency']['actual']
def get_frequency_unit(self, name):
with self.lock:
return self.data[name]['frequency']['unit']
# Interrupts methods
def get_interrupts_count(self, name):
with self.lock:
return self.data[name]['interrupts']['count']
def get_interrupts_unit(self, name):
with self.lock:
return self.data[name]['interrupts']['unit']
# rc6 methods
def get_rc6_value(self, name):
with self.lock:
return self.data[name]['rc6']['value']
def get_rc6_unit(self, name):
with self.lock:
return self.data[name]['rc6']['unit']
# Power methods
def get_power_GPU(self, name):
with self.lock:
return self.data[name]['power']['GPU']
def get_power_package(self, name):
with self.lock:
return self.data[name]['power']['Package']
def get_power_unit(self, name):
with self.lock:
return self.data[name]['power']['unit']
# Engines methods
def get_engine_data(self, name, engine_name):
with self.lock:
return self.data[name]['engines'].get(engine_name, {})
def stop_monitoring(self):
self.running = False
for thread in self.threads:
thread.join()
def __del__(self):
self.stop_monitoring()