forked from ROCm/ROC-smi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocm_smi.py
executable file
·1524 lines (1288 loc) · 57.1 KB
/
rocm_smi.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
""" ROCm-SMI (System Management Interface) Tool
This tool provides a user-friendly interface for manipulating
the ROCK (Radeon Open Compute Kernel) via sysfs files.
Please view the README.md file for more information
"""
from __future__ import print_function
import os
import argparse
import re
import sys
import subprocess
from subprocess import check_output
import json
import collections
if hasattr(__builtins__, 'raw_input'):
input = raw_input
# Version of the JSON output used to save clocks
JSON_VERSION = 1
# Set to 1 if an error occurs
RETCODE = 0
def relaunchAsSudo():
""" Relaunch the SMI as sudo
To write to sysfs, the SMI requires root access. Use execvp to relaunch the
script with sudo privileges
"""
if os.geteuid() != 0:
os.execvp('sudo', ['sudo'] + sys.argv)
drmprefix = '/sys/class/drm'
hwmonprefix = '/sys/class/hwmon'
powerprefix = '/sys/kernel/debug/dri/'
headerSpacer = '='*24
logSpacer = headerSpacer * 4
valuePaths = {
'id' : {'prefix' : drmprefix, 'filepath' : 'device', 'needsparse' : True},
'vbios' : {'prefix' : drmprefix, 'filepath' : 'vbios_version', 'needsparse' : False},
'perf' : {'prefix' : drmprefix, 'filepath' : 'power_dpm_force_performance_level', 'needsparse' : False},
'sclk_od' : {'prefix' : drmprefix, 'filepath' : 'pp_sclk_od', 'needsparse' : False},
'mclk_od' : {'prefix' : drmprefix, 'filepath' : 'pp_mclk_od', 'needsparse' : False},
'sclk' : {'prefix' : drmprefix, 'filepath' : 'pp_dpm_sclk', 'needsparse' : False},
'mclk' : {'prefix' : drmprefix, 'filepath' : 'pp_dpm_mclk', 'needsparse' : False},
'pclk' : {'prefix' : drmprefix, 'filepath' : 'pp_dpm_pcie', 'needsparse' : False},
'clk_voltage' : {'prefix' : drmprefix, 'filepath' : 'pp_od_clk_voltage', 'needsparse' : False},
'profile' : {'prefix' : drmprefix, 'filepath' : 'pp_power_profile_mode', 'needsparse' : False},
'use' : {'prefix' : drmprefix, 'filepath' : 'gpu_busy_percent', 'needsparse' : False},
'fan' : {'prefix' : hwmonprefix, 'filepath' : 'pwm1', 'needsparse' : False},
'fanmax' : {'prefix' : hwmonprefix, 'filepath' : 'pwm1_max', 'needsparse' : False},
'fanmode' : {'prefix' : hwmonprefix, 'filepath' : 'pwm1_enable', 'needsparse' : False},
'temp' : {'prefix' : hwmonprefix, 'filepath' : 'temp1_input', 'needsparse' : True},
'power' : {'prefix' : hwmonprefix, 'filepath' : 'power1_average', 'needsparse' : True},
'power_cap' : {'prefix' : hwmonprefix, 'filepath' : 'power1_cap', 'needsparse' : False},
'power_cap_max' : {'prefix' : hwmonprefix, 'filepath' : 'power1_cap_max', 'needsparse' : False},
'power_cap_min' : {'prefix' : hwmonprefix, 'filepath' : 'power1_cap_min', 'needsparse' : False}
}
def getSysfsValue(device, key):
""" Return the desired SysFS value for a specified device
Parameters:
device -- Device to return the desired value
value -- SysFS value to return (defined in dict above)
"""
pathDict = valuePaths[key]
fileValue = ''
filePath = os.path.join(pathDict['prefix'], device, 'device', pathDict['filepath'])
if pathDict['prefix'] == hwmonprefix:
# HW Monitor values have a different path structure
if not getHwmonFromDevice(device):
return None
filePath = os.path.join(getHwmonFromDevice(device), pathDict['filepath'])
if not os.path.isfile(filePath):
return None
# Use try since some sysfs files like power1_average will throw -EINVAL
# instead of giving something useful.
try:
with open(filePath, 'r') as fileContents:
fileValue = fileContents.read().rstrip('\n')
except:
printLog(device, 'WARNING: Unable to read ' + filePath)
return None
# Some sysfs files aren't a single line of text
if pathDict['needsparse']:
fileValue = parseSysfsValue(key, fileValue)
if fileValue == '':
printLog(device, 'WARNING: Empty SysFS value: ' + key)
return fileValue
def parseSysfsValue(key, value):
""" Parse the sysfs value string
Parameters:
value -- SysFS value to parse
Some SysFS files aren't a single line/string, so we need to parse it
to get the desired value
"""
if key == 'id':
# Strip the 0x prefix
return value[2:]
if key == 'temp':
# Convert from millidegrees
return int(value) / 1000
if key == 'power':
# power1_average returns the value in microwatts. However, if power is not
# available, it will return "Invalid Argument"
if value.isdigit():
return float(value) / 1000 / 1000
return ''
def parseDeviceNumber(deviceNum):
""" Parse the device number, returning the format of card#
Parameters:
deviceNum -- Device number to parse
"""
return 'card' + str(deviceNum)
def parseDeviceName(deviceName):
""" Parse the device name, which is of the format card#.
Parameters:
deviceName -- Device name to parse
"""
return deviceName[4:]
def printLog(device, log):
""" Print out to the SMI log.
Parameters:
device -- Device that the log will reference
log -- String to print to the log
"""
for line in log.split('\n'):
print('GPU[', parseDeviceName(device), '] \t\t: ', line, sep='')
def doesDeviceExist(device):
""" Check whether the specified device exists in sysfs.
Parameters:
device -- Device to check for existence
"""
if os.path.exists(os.path.join(drmprefix, device)) == 0:
return False
return True
def getPid(name):
""" Get the process id of a specific application """
return check_output(["pidof", name])
def confirmOutOfSpecWarning(autoRespond):
""" Print the warning for running outside of specification and prompt user to accept the terms.
Parameters:
autoRespond -- Response to automatically provide for all prompts
"""
print('''
******WARNING******\n
Operating your AMD GPU outside of official AMD specifications or outside of
factory settings, including but not limited to the conducting of overclocking,
over-volting or under-volting (including use of this interface software,
even if such software has been directly or indirectly provided by AMD or otherwise
affiliated in any way with AMD), may cause damage to your AMD GPU, system components
and/or result in system failure, as well as cause other problems.
DAMAGES CAUSED BY USE OF YOUR AMD GPU OUTSIDE OF OFFICIAL AMD SPECIFICATIONS OR
OUTSIDE OF FACTORY SETTINGS ARE NOT COVERED UNDER ANY AMD PRODUCT WARRANTY AND
MAY NOT BE COVERED BY YOUR BOARD OR SYSTEM MANUFACTURER'S WARRANTY.
Please use this utility with caution.
''')
if not autoRespond:
user_input = input('Do you accept these terms? [y/N] ')
else:
user_input = autoRespond
if user_input in ['Yes', 'yes', 'y', 'Y', 'YES']:
return
else:
sys.exit('Confirmation not given. Exiting without setting value')
def isDPMAvailable(device):
""" Check if DPM is available for a specified device.
Parameters:
device -- Device to check for DPM availability
"""
if not doesDeviceExist(device) or os.path.isfile(os.path.join(drmprefix, device, 'device', 'power_dpm_state')) == 0:
return False
return True
def getNumProfileArgs(device):
""" Get the number of Power Profile fields for a specific device
This varies per ASIC, so ensure that we get the right number of arguments
Parameters:
device -- Device to get the number of Power Profile fields
"""
profile = getSysfsValue(device, 'profile')
numHiddenFields = 0
if not profile:
return 0
# Get the 1st line (column names)
fields = profile.splitlines()[0]
# SMU7 has 2 hidden fields for SclkProfileEnable and MclkProfileEnable
if 'SCLK_UP_HYST' in fields:
numHiddenFields = 2
# Subtract 2 to remove NUM and MODE NAME, since they're not valid Profile fields
return len(fields.split()) - 2 + numHiddenFields
def verifySetProfile(device, profile):
""" Verify data from user to set as Power Profile.
Ensure that we can set the profile, with Profiles being supported and
the profile being passed in being valid
Parameters:
device -- Device to verify Profile variables
"""
global RETCODE
if not isDPMAvailable(device):
printLog(device, 'DPM not available - cannot specify profile.')
RETCODE = 1
return False
# If it's 1 number, we're setting the level, not the Custom Profile
if profile.isdigit():
maxProfileLevel = getMaxLevel(device, 'profile')
if int(profile) > maxProfileLevel:
printLog(device, 'Cannot set profile to level ' + str(profile) + ', max level is ' + str(maxProfileLevel))
return False
return True
# If we get a string, split it into elements to make it a list
elif isinstance(profile, str):
if profile == 'reset':
printLog(device, 'Reset no longer accepted as a Power Profile')
return False
else:
profileList = profile.strip().split(' ')
elif isinstance(profile, collections.Iterable):
profileList = profile
else:
printLog(device, 'Unsupported profile argument : ' + str(profile))
return False
numProfileArgs = getNumProfileArgs(device)
if numProfileArgs == 0:
printLog(device, 'Power Profiles not supported')
return False
if len(profileList) != numProfileArgs:
printLog(device, 'Cannot set profile, must be 1 or ' + str(numProfileArgs) + ' values')
RETCODE = 1
return False
return True
def getProfile(device):
""" Get either the current profile level, or the custom profile
The CUSTOM profile might be set, or a specific profile level may have been selected
Return either a single digit for a non-CUSTOM profile, or return the CUSTOM profile
Parameters:
device -- Device to return the current profile
"""
profiles = getSysfsValue(device, 'profile')
profile = ''
custom = ''
asic = ''
level = ''
numArgs = getNumProfileArgs(device)
for line in profiles.splitlines():
if re.match(r'.*SCLK_UP_HYST./*', line):
asic = 'SMU7'
continue
if re.match(r'.*\*.*', line):
level = line.split()[0]
if re.match(r'.*CUSTOM.*', line):
# Ditch the NUM and NAME, which end with a : before the profile values
# Then put it into single words via split
custom = line.split(':')[1].split()
break
if not custom:
return level
# We need some special parsing for SMU7 if it's a CUSTOM profile
if asic == 'SMU7' and custom:
sclk = custom[0:3]
mclk = custom[3:]
if sclk[0] == '-':
sclkStr = '0 0 0 0'
else:
sclkStr = '1 ' + ' '.join(sclk)
if mclk[0] == '-':
mclkStr = '0 0 0 0'
else:
mclkStr = '1 ' + ' '.join(mclk)
customStr = sclkStr + ' ' + mclkStr
else:
customStr = ' '.join(custom[-numArgs:])
return customStr
def writeProfileSysfs(device, value):
""" Write to the Power Profile sysfs file
This function is different from a regular sysfs file as it could involve
parsing of the data first.
Parameters:
device -- Device to write the Power Profile info
value -- Value to write to the sysfs file
"""
if not verifySetProfile(device, value):
return
# Perf Level must be set to manual for a Power Profile to be specified
# This is new compared to previous versions of the Power Profile
setPerfLevel(device, 'manual')
profilePath = os.path.join(drmprefix, device, 'device', 'pp_power_profile_mode')
maxLevel = getMaxLevel(device, 'profile')
# If it's a single number, then we're choosing the Power Profile, not setting CUSTOM
if isinstance(value, str) and len(value) == 1:
profileString = value
# Otherwise, we're setting the CUSTOM profile
elif value.isdigit():
profileString = str(value)
elif isinstance(value, str) and len(value) > 1:
# Prepend the Max Level of Profiles since that will always be the CUSTOM profile
profileString = str(maxLevel) + value
else:
printLog(device, 'Invalid input argument ' + value)
return False
if writeToSysfs(profilePath, profileString):
return True
return False
def writeToSysfs(fsFile, fsValue):
""" Write to a sysfs file.
Parameters:
fsFile -- Path to the sysfs file to modify
fsValue -- Value to write to the sysfs file
"""
global RETCODE
if not os.path.isfile(fsFile):
print('Cannot write to sysfs file ' + fsFile + '. File does not exist', sep='')
RETCODE = 1
return False
try:
with open(fsFile, 'w') as fs:
fs.write(fsValue + '\n') # Certain sysfs files require \n at the end
except (IOError, OSError):
print('Unable to write to sysfs file' + fsFile)
RETCODE = 1
return False
return True
def listDevices():
""" Return a list of GPU devices."""
devicelist = [device for device in os.listdir(drmprefix) if re.match(r'^card\d+$', device)]
devicelist.sort()
return devicelist
def listAmdHwMons():
"""Return a list of AMD HW Monitors."""
hwmons = []
for mon in os.listdir(hwmonprefix):
tempname = os.path.join(hwmonprefix, mon, 'name')
if os.path.isfile(tempname):
with open(tempname, 'r') as tempmon:
drivername = tempmon.read().rstrip('\n')
if drivername in ['radeon', 'amdgpu']:
hwmons.append(os.path.join(hwmonprefix, mon))
return hwmons
def getHwmonFromDevice(device):
""" Return the corresponding HW Monitor for a specified GPU device.
Parameters:
device -- Device to return the corresponding HW Monitor
"""
drmdev = os.path.realpath(os.path.join(drmprefix, device, 'device'))
for hwmon in listAmdHwMons():
if os.path.realpath(os.path.join(hwmon, 'device')) == drmdev:
return hwmon
return None
def getFanSpeed(device):
""" Return the fan speed (%) for a specified device.
Parameters:
device -- Device to return the current fan speed
"""
fanLevel = getSysfsValue(device, 'fan')
fanMax = getSysfsValue(device, 'fanmax')
if not fanLevel or not fanMax:
return 0
return round((float(fanLevel) / float(fanMax)) * 100, 2)
def getCurrentClock(device, clock, clocktype):
""" Return the current clock frequency for a specified device.
Parameters:
device -- Device to return the clock frequency
clock -- [gpu|mem|pcie] Return the GPU (gpu), GPU Memory (mem) or PCIE (pcie) clock frequency
clocktype -- [freq|level] Return either the clock frequency (freq) or clock level (level)
"""
currClk = ''
if clock == 'gpu':
currClocks = getSysfsValue(device, 'sclk')
elif clock == 'mem':
currClocks = getSysfsValue(device, 'mclk')
elif clock == 'pcie':
currClocks = getSysfsValue(device, 'pclk')
else:
currClocks = None
if not currClocks:
return None
# Since the current clock line is of the format 'X: #Mhz *', we either want the
# first character for level, or the 3rd-to-2nd-last characters for speed
for line in currClocks.splitlines():
if re.match(r'.*\*$', line):
if clocktype == 'freq':
currClk = line[3:-2]
else:
currClk = line[0]
break
return currClk
def getMaxLevel(device, leveltype):
""" Return the maximum level for a specified device.
Parameters:
device -- Device to return the maximum level
leveltype -- [gpu|mem|pcie|profile] Return the maximum GPU (gpu), GPU Memory (mem) level,
PCIe level, or the highest numbered Power Profiles
"""
global RETCODE
levelmap = {'gpu' : 'sclk', 'mem' : 'mclk', 'pcie' : 'pclk', 'profile' : 'profile'}
try:
key = levelmap[leveltype]
except KeyError:
printLog(device, 'Invalid level type ' + leveltype)
RETCODE = 1
return ''
levels = getSysfsValue(device, key)
if not levels:
return 0
# lstrip since there are leading spaces for this sysfs file, but no others
if leveltype == 'profile':
levels = levels.splitlines()[-1].lstrip(' ')
return int(levels.splitlines()[-1][0])
def setPerfLevel(device, level):
""" Set the Performance Level for a specified device.
Parameters:
device -- Device to modify the current Performance Level
level -- Performance Level to set
"""
global RETCODE
validLevels = ['auto', 'low', 'high', 'manual']
perfPath = os.path.join(drmprefix, device, 'device', 'power_dpm_force_performance_level')
if level not in validLevels:
print(device, 'Invalid Performance level:' + level)
RETCODE = 1
return False
if not os.path.isfile(perfPath):
return False
writeToSysfs(perfPath, level)
return True
def showId(deviceList):
""" Display the device ID for a list of devices.
Parameters:
deviceList -- List of devices to return the device ID (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
printLog(device, 'GPU ID: 0x' + getSysfsValue(device, 'id'))
print(logSpacer)
def showVbiosVersion(deviceList):
""" Display the VBIOS version for a list of devices.
Parameters:
deviceList -- List of devices to return the VBIOS version (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
vbios = getSysfsValue(device, 'vbios')
if vbios:
printLog(device, 'VBIOS version: ' + vbios)
else:
printLog(device, 'Cannot get VBIOS version')
print(logSpacer)
def showCurrentGpuClocks(deviceList):
""" Display the current GPU clock frequencies for a list of devices.
Parameters:
deviceList -- List of devices to return the current clock frequencies (can be a single-item list)
"""
global RETCODE
print(logSpacer)
for device in deviceList:
if not isDPMAvailable(device):
printLog(device, 'DPM not available - Cannot display GPU clocks')
continue
gpuclk = getCurrentClock(device, 'gpu', 'freq')
gpulevel = getCurrentClock(device, 'gpu', 'level')
if gpuclk == '':
printLog(device, 'Unable to determine current GPU clocks. Check dmesg or GPU temperature')
RETCODE = 1
return
printLog(device, 'GPU Clock Level: ' + str(gpulevel) + ' (' + str(gpuclk) + ')')
print(logSpacer)
def showCurrentClocks(deviceList):
""" Display the current GPU and GPU Memory clock frequencies for a list of devices.
Parameters:
deviceList -- List of devices to return the current clock frequencies (can be a single-item list)
"""
global RETCODE
print(logSpacer)
for device in deviceList:
if not isDPMAvailable(device):
printLog(device, 'DPM not available - Cannot display clocks')
continue
gpuclk = getCurrentClock(device, 'gpu', 'freq')
gpulevel = getCurrentClock(device, 'gpu', 'level')
memclk = getCurrentClock(device, 'mem', 'freq')
memlevel = getCurrentClock(device, 'mem', 'level')
pcieclk = getCurrentClock(device, 'pcie', 'freq')
pcielevel = getCurrentClock(device, 'pcie', 'level')
if gpuclk == '':
printLog(device, 'Unable to determine current clocks. Check dmesg or GPU temperature')
RETCODE = 1
return
printLog(device, 'GPU Clock Level: ' + str(gpulevel) + ' (' + str(gpuclk) + ')')
printLog(device, 'GPU Memory Clock Level: ' + str(memlevel) + ' (' + str(memclk) + ')')
printLog(device, 'PCIE Clock Level: ' + str(pcielevel) + ' (' + str(pcieclk) + ')')
print(logSpacer)
def showCurrentTemps(deviceList):
""" Display the current temperature for a list of devices.
Parameters:
deviceList -- List of devices to return the current temperature (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
temp = getSysfsValue(device, 'temp')
if not temp:
printLog(device, 'Unable to display temperature')
continue
printLog(device, 'Temperature: ' + str(temp) + 'c')
print(logSpacer)
def showCurrentFans(deviceList):
""" Display the current fan speed for a list of devices.
Parameters:
deviceList -- List of devices to return the current fan speed (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
fanlevel = getSysfsValue(device, 'fan')
fanspeed = getFanSpeed(device)
if not fanspeed or not fanlevel:
printLog(device, 'Unable to determine current fan speed')
continue
printLog(device, 'Fan Level: ' + str(fanlevel) + ' (' + str(fanspeed) + ')%')
print(logSpacer)
def showClocks(deviceList):
""" Display current GPU and GPU Memory clock frequencies for a list of devices.
Parameters:
deviceList -- List of devices to display current clock frequencies (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
devpath = os.path.join(drmprefix, device, 'device')
sclkPath = os.path.join(devpath, 'pp_dpm_sclk')
mclkPath = os.path.join(devpath, 'pp_dpm_mclk')
pclkPath = os.path.join(devpath, 'pp_dpm_pcie')
if not isDPMAvailable(device):
printLog(device, 'DPM not available - Cannot display clocks')
continue
if not os.path.isfile(sclkPath) or not os.path.isfile(mclkPath):
continue
with open(sclkPath, 'r') as sclk:
sclkLog = 'Supported GPU clock frequencies on GPU' + parseDeviceName(device) + '\n' + sclk.read()
with open(mclkPath, 'r') as mclk:
mclkLog = 'Supported GPU Memory clock frequencies on GPU' + parseDeviceName(device) + '\n' + mclk.read()
with open(pclkPath, 'r') as pclk:
pclkLog = 'Supported PCIE clock frequencies on GPU' + parseDeviceName(device) + '\n' + pclk.read()
printLog(device, sclkLog)
printLog(device, mclkLog)
printLog(device, pclkLog)
print(logSpacer)
def showPowerPlayTable(deviceList):
""" Display current GPU and GPU Memory clock frequencies and voltages for a list of devices.
Parameters:
deviceList -- List of devices to display current clock frequencies and voltages (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
if not isDPMAvailable(device):
printLog(device, 'DPM not available - Cannot display voltages')
continue
table = getSysfsValue(device, 'clk_voltage')
if not table:
printLog(device, 'Cannot display voltage, clk_voltage is empty')
continue
printLog(device, table)
print(logSpacer)
def showPerformanceLevel(deviceList):
""" Display current Performance Level for a list of devices.
Parameters:
deviceList -- List of devices to display current Performance Level (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
level = getSysfsValue(device, 'perf')
if not level:
printLog(device, 'Cannot get Performance Level: Performance Level not supported')
else:
printLog(device, 'Current Performance Level: ' + level)
print(logSpacer)
def showOverDrive(deviceList, odtype):
""" Display current OverDrive level for a list of devices.
Parameters:
deviceList -- List of devices to display current OverDrive values (can be a single-item list)
odtype -- Which OverDrive to display (gpu|mem)
"""
print(logSpacer)
for device in deviceList:
if odtype == 'gpu':
od = getSysfsValue(device, 'sclk_od')
odStr = 'GPU'
elif odtype == 'mem':
od = getSysfsValue(device, 'mclk_od')
odStr = 'GPU Memory'
if not od or int(od) < 0:
printLog(device, 'Cannot get ' + odStr + ' OverDrive value: OverDrive not supported')
else:
printLog(device, 'Current ' + odStr + ' OverDrive value: ' + str(od) + '%')
print(logSpacer)
def showProfile(deviceList):
""" Display available Power Profiles for a list of devices.
Parameters:
deviceList -- List of devices to display available Power Profile attributes (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
if not isDPMAvailable(device):
printLog(device, 'DPM not available - Power Profiles not supported')
continue
profile = getSysfsValue(device, 'profile')
if not profile:
printLog(device, 'Unable to get Power Profiles')
continue
if len(profile) > 1:
printLog(device, '\n' + profile)
else:
printLog(device, 'Invalid return value from Power Profile SysFS file')
print(logSpacer)
def showPower(deviceList):
""" Display current Average Graphics Package Power Consumption for a list of devices.
Parameters:
deviceList -- List of devices to display current Average Graphics Package Power Consumption (can be a single-item list)
"""
print(logSpacer)
try:
getPid("atitool")
print('WARNING: Please terminate ATItool to use this functionality')
except subprocess.CalledProcessError:
for device in deviceList:
power = getSysfsValue(device, 'power')
if not power:
printLog(device, 'Cannot get Average Graphics Package Power Consumption: Average GPU Power not supported')
else:
printLog(device, 'Average Graphics Package Power: ' + str(power) + 'W')
print(logSpacer)
def showMaxPower(deviceList):
""" Display the maximum Graphics Package Power that this GPU will attempt to consume
before it begins throttling performance.
Parameters:
deviceList -- List of devices to display maximum Graphics Package Power Consumption (can be a single-item list)
"""
print(logSpacer)
for device in deviceList:
hwmon = getHwmonFromDevice(device)
if not hwmon:
printLog(device, 'No corresponding HW Monitor found')
continue
power_cap = getSysfsValue(device, 'power_cap')
if not power_cap:
printLog(device, 'Cannot get maximum Graphics Package Power: Max GPU Power reading not supported')
else:
power_cap = str(int(getSysfsValue(device, 'power_cap')) / 1000000)
printLog(device, 'Max Graphics Package Power: ' + power_cap + 'W')
print(logSpacer)
def showGpuUse(deviceList):
""" Display GPU use for a list of devices.
Parameters:
deviceList -- List of all devices
"""
print(logSpacer)
for device in deviceList:
use = getSysfsValue(device, 'use')
if use == None:
printLog(device, 'Cannot get GPU use.')
else:
printLog(device, 'Current GPU use: ' + use + '%')
print(logSpacer)
def showAllConciseHw(deviceList):
""" Display critical Hardware info for all devices in a concise format.
Parameters:
deviceList -- List of all devices
"""
print(logSpacer)
print(' GPU DID ECC VBIOS')
for device in deviceList:
gpuid = getSysfsValue(device, 'id')
# To support later
ecc = 'N/A'
vbios = getSysfsValue(device, 'vbios')
print(" %-4s%-7s%-6s%-17s" % (device[4:], gpuid, ecc, vbios))
def showAllConcise(deviceList):
""" Display critical info for all devices in a concise format.
Parameters:
deviceList -- List of all devices
"""
print(logSpacer)
print('GPU Temp AvgPwr SCLK MCLK PCLK Fan Perf PwrCap SCLK OD MCLK OD GPU%')
for device in deviceList:
temp = getSysfsValue(device, 'temp')
if not temp:
temp = 'N/A'
else:
temp = str(temp) + 'c'
power = getSysfsValue(device, 'power')
if not power:
power = 'N/A'
else:
power = str(power) + 'W'
sclk = getCurrentClock(device, 'gpu', 'freq')
if not sclk:
sclk = 'N/A'
mclk = getCurrentClock(device, 'mem', 'freq')
if not mclk:
mclk = 'N/A'
pclk = getCurrentClock(device, 'pcie', 'freq')
if not pclk:
pclk = 'N/A'
fan = str(getFanSpeed(device))
if not fan:
fan = 'N/A'
else:
fan = fan + '%'
perf = getSysfsValue(device, 'perf')
if not perf:
perf = 'N/A'
power_cap = getSysfsValue(device, 'power_cap')
if not power_cap:
power_cap = 'N/A'
else:
power_cap = str(int(power_cap)/1000000) + 'W'
sclk_od = getSysfsValue(device, 'sclk_od')
if not sclk_od or sclk_od == '-1':
sclk_od = 'N/A'
else:
sclk_od = sclk_od + '%'
mclk_od = getSysfsValue(device, 'mclk_od')
if not mclk_od or mclk_od == '-1':
mclk_od = 'N/A'
else:
mclk_od = mclk_od + '%'
use = getSysfsValue(device, 'use')
if use == None:
use = 'N/A'
else:
use = use + '%'
print("%-6s%-7s%-9s%-8s%-8s%-15s%-8s%-8s%-9s%-10s%-9s%-9s" % (device[4:], temp, power, sclk, mclk, pclk, fan, perf, power_cap, sclk_od, mclk_od, use))
print(logSpacer)
def setPerformanceLevel(deviceList, level):
""" Set the Performance Level for a list of devices.
Parameters:
deviceList -- List of devices to set the current Performance Level (can be a single-item list)
level -- Specific Performance Level to set
"""
print(logSpacer)
for device in deviceList:
if setPerfLevel(device, level):
printLog(device, 'Successfully set current Performance Level to ' + level)
else:
printLog(device, 'Unable to set current Performance Level to ' + level)
print(logSpacer)
def setClocks(deviceList, clktype, clk):
""" Set clock frequency level for a list of devices.
Parameters:
deviceList -- List of devices to set the clock frequency (can be a single-item list)
clktype -- [gpu|mem|pcie] Set the GPU (gpu), GPU Memory (mem), or PCIE clock frequency level
clk -- Clock frequency level to set
"""
global RETCODE
if not clk:
print('Invalid clock frequency')
RETCODE = 1
return
value = ''.join(map(str, clk))
try:
int(value)
except ValueError:
print('Cannot set Clock level to value', value, ', non-integer characters are present!')
RETCODE = 1
return
for device in deviceList:
if not isDPMAvailable(device):
printLog(device, 'DPM not available - Cannot set clocks')
RETCODE = 1
continue
devpath = os.path.join(drmprefix, device, 'device')
if clktype == 'gpu':
clkFile = os.path.join(devpath, 'pp_dpm_sclk')
elif clktype == 'mem':
clkFile = os.path.join(devpath, 'pp_dpm_mclk')
elif clktype == 'pcie':
clkFile = os.path.join(devpath, 'pp_dpm_pcie')
else:
printLog(device, 'Invalid clock type ' + clktype)
RETCODE = 1
return
# If maxLevel is empty, it means that the sysfs file is empty, so quit
maxLevel = getMaxLevel(device, clktype)
if not maxLevel:
printLog(device, 'Unable to set clock for type ' + clktype + ', Sysfs file is empty')
RETCODE = 1
continue
# GPU clocks can be set to multiple levels at the same time (of the format
# 4 5 6 for levels 4, 5 and 6). Don't compare against the max level for gpu
# clocks in this case
if any(int(item) > getMaxLevel(device, clktype) for item in clk):
printLog(device, 'Unable to set clock to unsupported Level - Max Level is ' + str(getMaxLevel(device, clktype)))
RETCODE = 1
continue
setPerfLevel(device, 'manual')
if writeToSysfs(clkFile, value):
if clktype == 'gpu':
printLog(device, 'Successfully set GPU Clock frequency mask to Level ' + value)
else:
printLog(device, 'Successfully set GPU Memory Clock frequency mask to Level ' + value)
else:
printLog(device, 'Unable to set ' + clktype + ' clock to Level ' + value)
RETCODE = 1
def setPowerPlayTableLevel(deviceList, clktype, levelList, autoRespond):
""" Set clock frequency and voltage for a level in the PowerPlay table for a list of devices.
Parameters:
deviceList -- List of devices to set the clock frequency (can be a single-item list)
clktype -- [gpu|mem] Set the GPU (gpu) or GPU Memory (mem) clock frequency level
levelList -- Clock frequency level to set
autoRespond -- Response to automatically provide for all prompts
"""
global RETCODE
if not levelList:
print('Invalid clock state')
RETCODE = 1
return
value = ' '.join(map(str, levelList))
try:
all(int(item) for item in levelList)
except ValueError:
print('Cannot set PowerPlay table level to ', levelList, ', non-integer characters are present!')
RETCODE = 1
return
if clktype == 'gpu':
value = 's ' + value
else:
value = 'm ' + value
for device in deviceList:
if not isDPMAvailable(device):
printLog(device, 'DPM not enabled - Cannot set voltages')
RETCODE = 1
continue
devpath = os.path.join(drmprefix, device, 'device')
clkFile = os.path.join(devpath, 'pp_od_clk_voltage')
confirmOutOfSpecWarning(autoRespond)