[amdgpu_linux] Add AMD GPU widget for linux

This commit is contained in:
Rémy CLOUARD 2023-01-03 07:15:36 +01:00 committed by GitHub
parent 6a633cb4bb
commit 47ae72ee80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 1 deletions

View File

@ -8,6 +8,16 @@ Widget types consist of worker functions that take two arguments
passed to :lua:func:`vicious.register`, and return a table of values
to be formatted by ``format``.
vicious.widgets.amdgpu
----------------------
Provides GPU and VRAM usage statistics for AMD graphics cards.
Supported platforms: GNU/Linux (require ``sysfs``)
* ``warg`` (from now on will be called *argument*): card ID, e.g. ``"card0"``
* Returns a table with string keys: ``${gpu_usage}``, ``${mem_usage}``
vicious.widgets.bat
-------------------
@ -16,7 +26,7 @@ Provides state, charge, and remaining time for a requested battery.
Supported platforms: GNU/Linux (require ``sysfs``),
FreeBSD (require ``acpiconf``) and OpenBSD (no extra requirements).
* ``warg`` (from now on will be called *argument*):
* Argument:
* On GNU/Linux: battery ID, e.g. ``"BAT0"``
* On FreeBSD (optional): battery ID, e.g. ``"batt"`` or ``"0"``

66
widgets/amdgpu_linux.lua Normal file
View File

@ -0,0 +1,66 @@
-- contrib/amdgpu_linux.lua
-- Copyright (C) 2022 Rémy Clouard <shikamaru shikamaru fr>
--
-- This file is part of Vicious.
--
-- Vicious is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as
-- published by the Free Software Foundation, either version 2 of the
-- License, or (at your option) any later version.
--
-- Vicious is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
local io = { open = io.open }
local tonumber = tonumber
local string = { match = string.match }
local helpers = require("vicious.helpers")
local _mem = nil
-- {{{ AMDGPU widget type
return helpers.setcall(function (format, warg)
if not warg then return end
-- see https://www.kernel.org/doc/html/v5.9/gpu/amdgpu.html#busy-percent
local amdgpu = "/sys/class/drm/"..warg.."/device"
local _data = {}
local f = io.open(amdgpu .. "/gpu_busy_percent", "r")
if f then
_data["{gpu_usage}"] = f:read("*line")
f:close()
else
_data["{gpu_usage}"] = "N/A"
end
if _mem == nil then
f = io.open(amdgpu .. "/mem_info_vram_total", "r")
if f then
_mem = tonumber(string.match(f:read("*line"), "([%d]+)"))
f:close()
end
end
f = io.open(amdgpu .. "/mem_info_vram_used", "r")
if f then
local _used = tonumber(string.match(f:read("*line"), "([%d]+)"))
if type(_used) == 'number' and type(_mem) == 'number'
and _mem > 0 then
_data["{mem_usage}"] = _used/_mem*100
else
_data["{mem_usage}"] = "N/A"
end
f:close()
else
_data["{mem_usage}"] = "N/A"
end
return _data
end)
--}}}