From 3f5f2bc62e42345139a221b0b2dfea86f210f245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20CLOUARD?= Date: Sun, 25 Dec 2022 11:34:46 +0000 Subject: [PATCH] [amdgpu_linux] Add widget type --- docs/source/widgets.rst | 12 +++++++- widgets/amdgpu_linux.lua | 62 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 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..f3cdcf3 --- /dev/null +++ b/widgets/amdgpu_linux.lua @@ -0,0 +1,62 @@ +-- 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 helpers = require("vicious.helpers") + +-- {{{ AMDGPU widget type +return helpers.setcall(function (format, warg) + if not warg then return end + + local amdgpu = "/sys/class/drm/"..warg.."/device" + local _data = {} + local _mem = nil + + local f = io.open(amdgpu .. "/gpu_busy_percent", "r") + if f then + for line in f:lines() do + _data["{gpu_usage}"] = line + end + 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 + for line in f:lines() do + _mem = line + end + f:close() + end + end + + f = io.open(amdgpu .. "/mem_info_vram_used", "r") + if f then + for line in f:lines() do + _data["{mem_usage}"] = line/_mem*100 + end + f:close() + else + _data["{mem_usage}"] = "N/A" + end + + return _data + +end) +--}}}