Debian Codegraph @

vicious/widgets/amdgpu_linux.lua

raw symbols SCIP
1 -- AMD GPU widget type for Linux
2 -- Copyright (C) 2022 Rémy Clouard <shikamaru shikamaru fr>
3 --
4 -- This file is part of Vicious.
5 --
6 -- Vicious is free software: you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as
8 -- published by the Free Software Foundation, either version 2 of the
9 -- License, or (at your option) any later version.
10 --
11 -- Vicious is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU General Public License for more details.
15 --
16 -- You should have received a copy of the GNU General Public License
17 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
18
19 local io = { open = io.open }
20 local string = { match = string.match }
21 local tonumber = tonumber
22 local type = type
23
24 local helpers = require("vicious.helpers")
25
26 local _mem = nil
27
28 -- {{{ AMDGPU widget type
29 return helpers.setcall(function (format, warg)
30 if not warg then return end
31
32 -- see https://www.kernel.org/doc/html/v5.9/gpu/amdgpu.html#busy-percent
33 local amdgpu = "/sys/class/drm/"..warg.."/device"
34 local _data = {}
35
36 local f = io.open(amdgpu .. "/gpu_busy_percent", "r")
37 if f then
38 _data["{gpu_usage}"] = f:read("*line")
39 f:close()
40 else
41 _data["{gpu_usage}"] = "N/A"
42 end
43
44 if _mem == nil then
45 f = io.open(amdgpu .. "/mem_info_vram_total", "r")
46 if f then
47 _mem = tonumber(string.match(f:read("*line"), "([%d]+)"))
48 f:close()
49 end
50 end
51
52 f = io.open(amdgpu .. "/mem_info_vram_used", "r")
53 if f then
54 local _used = tonumber(string.match(f:read("*line"), "([%d]+)"))
55 if type(_used) == 'number' and type(_mem) == 'number'
56 and _mem > 0 then
57 _data["{mem_usage}"] = _used/_mem*100
58 else
59 _data["{mem_usage}"] = "N/A"
60 end
61 f:close()
62 else
63 _data["{mem_usage}"] = "N/A"
64 end
65
66 return _data
67
68 end)
69 --}}}