From 47ae72ee80698540bff68e571cd2ce3df8b077df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20CLOUARD?= Date: Tue, 3 Jan 2023 07:15:36 +0100 Subject: [PATCH] [amdgpu_linux] Add AMD GPU widget for linux --- docs/source/widgets.rst | 12 +++++++- widgets/amdgpu_linux.lua | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 widgets/amdgpu_linux.lua diff --git a/docs/source/widgets.rst b/docs/source/widgets.rst index cd45beb..f7098f4 100644 --- a/docs/source/widgets.rst +++ b/docs/source/widgets.rst @@ -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"`` diff --git a/widgets/amdgpu_linux.lua b/widgets/amdgpu_linux.lua new file mode 100644 index 0000000..5334799 --- /dev/null +++ b/widgets/amdgpu_linux.lua @@ -0,0 +1,66 @@ +-- contrib/amdgpu_linux.lua +-- Copyright (C) 2022 Rémy Clouard +-- +-- 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 . +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) +--}}}