-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpufreq.lua
118 lines (104 loc) · 3.74 KB
/
gpufreq.lua
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
local utils = require 'mp.utils'
local msg = require 'mp.msg'
-- Splits a string into lines
function spliterator(s)
if s:sub(-1) ~= "\n" then
s = s .. "\n"
end
return s:gmatch("(.-)\n")
end
function get_sys_value(variable)
local f = assert(io.open(variable, "r"))
local val = string.gsub(f:read("*all"), "\n$", "")
f:close()
return val
end
function bytes_to_mib_str(b)
return string.format("%.2f", b / 1024 / 1024)
end
local freqgetter = {}
function show_gpu_freq(card)
local vendor = identify_card(card)
if(vendor == nil) then
mp.osd_message("Could not identify GPU vendor for card " .. card)
return
end
local frequencies = freqgetter[vendor](freqgetter, card)
display_gpu_freq(frequencies)
end
function card_path(card)
return "/sys/class/drm/" .. card .. "/"
end
function identify_card(card)
local fi = utils.file_info(card_path(card) .. "gt_act_freq_mhz")
-- I don't remember if intel has the vendor value h-haha
if(fi ~= nil and fi.is_file) then
return "intel"
end
vendor = get_sys_value(card_path(card) .. "device/vendor")
if(vendor == "0x1002") then
return "amd"
end
return nil
end
function freqgetter:intel(card)
local freq_cur = get_sys_value(card_path(card) .. "gt_act_freq_mhz")
local freq_max = get_sys_value(card_path(card) .. "gt_max_freq_mhz")
local freq_req = get_sys_value(card_path(card) .. "gt_cur_freq_mhz")
return {["core"] = {cur = freq_cur, max = freq_max, req = freq_req}}
end
--[[ YOU HAVE ENTERED THE CURSED AMD ZONE ]]--
-- Returns the parsed frequencies from an AMD card
function amd_pp_consumer(val)
local fmax = 0
local fcur = 0
for line in spliterator(val) do
local freq = tonumber(line:match("%d: (%d+)Mhz"))
local is_cur = line:match("%*")
if is_cur ~= nil then
fcur = freq
end
if freq > fmax then
fmax = freq
end
end
return fcur, fmax
end
function freqgetter:amd(card)
local scuffed = get_sys_value(card_path(card) .. "device/pp_dpm_sclk")
local scuffedmem = get_sys_value(card_path(card) .. "device/pp_dpm_mclk")
--local core_cur, core_max = amd_pp_consumer(scuffed)
local cur, max = amd_pp_consumer(scuffed)
local core = {["cur"] = cur, ["max"] = max}
local cur_mem, max_mem = amd_pp_consumer(scuffedmem)
local mem = {["cur"] = cur_mem, ["max"] = max_mem,
["used"] = tonumber(get_sys_value(card_path(card) .. "device/mem_info_vram_used")),
["total"] = tonumber(get_sys_value(card_path(card) .. "device/mem_info_vram_total"))}
local link = {
["speed"] = get_sys_value(card_path(card) .. "device/current_link_speed"),
["width"] = get_sys_value(card_path(card) .. "device/current_link_width")
}
return {["core"] = core, ["mem"] = mem, ["link"] = link}
end
--[[ YOU ARE NOW LEAVING THE CURSED AMD ZONE ]]--
function display_gpu_freq(freqs)
assert(freqs["core"])
local cfreqs = freqs["core"]
local msg = "Core: "
msg = msg .. cfreqs["cur"] .. "/" .. cfreqs["max"] .. " MHz"
if cfreqs["req"] then
msg = msg .. " (requested " .. cfreqs["req"] .. " MHz)"
end
if freqs["mem"] ~= nil then
msg = msg .. "\n"
msg = msg .. "Memory: " .. freqs["mem"]["cur"] .. "/" .. freqs["mem"]["max"] .. " MHz\n"
msg = msg .. "Memory Usage: " .. bytes_to_mib_str(freqs["mem"]["used"]) .. "/"
msg = msg .. bytes_to_mib_str(freqs["mem"]["total"]) .. " MiB"
end
if freqs["link"] ~= nil then
msg = msg .. "\n"
msg = msg .. "Link: " .. freqs["link"]["speed"] .. " x" .. freqs["link"]["width"]
end
mp.osd_message(msg)
end
mp.register_script_message("show-gpu-freq", show_gpu_freq)