Skip to content

Commit

Permalink
system: implement gpu_usage function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanvir committed Jan 30, 2024
1 parent aa409a0 commit 1f7ce47
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,26 @@ function system.memory_info()
return mem
end

-- Get gpu usage info
-----------------------------------------------------------------------------------------------------------------------
function system.gpu_usage()
local gpu_info = { usage = 0 }

-- Get GPU usage using nvidia-smi (NVIDIA GPUs only)
local command = "nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits"
local handle = io.popen(command)
local output = handle:read("*a")
handle:close()

-- Parse the output to get GPU usage
local gpu_usage = tonumber(output)
if gpu_usage then
gpu_info.usage = gpu_usage
end

return gpu_info
end

-- Get cpu usage info
-----------------------------------------------------------------------------------------------------------------------
--local storage = { cpu_total = {}, cpu_active = {} } -- storage structure
Expand Down Expand Up @@ -619,16 +639,31 @@ end
-- CPU and memory usage formatted special for desktop widget
--------------------------------------------------------------------------------
function system.dformatted.cpumem(storage)
local mem = system.memory_info()
local cores = {}
for i, v in ipairs(system.cpu_usage(storage).core) do
table.insert(cores, { value = v, text = string.format("CORE%d %s%%", i - 1, v) })
end
local mem = system.memory_info()
local cores = {}
for i, v in ipairs(system.cpu_usage(storage).core) do
table.insert(cores, { value = v, text = string.format("CORE%d %s%%", i - 1, v) })
end

return {
bars = cores,
lines = { { mem.usep, mem.inuse }, { mem.swp.usep, mem.swp.inuse } },
}
end

return {
bars = cores,
lines = { { mem.usep, mem.inuse }, { mem.swp.usep, mem.swp.inuse } },
}
-- GPU usage formatted special for panel widget
--------------------------------------------------------------------------------
function system.pformatted.gpu(crit)
crit = crit or 75

return function()
local gpu_usage = system.gpu_usage().usage
return {
value = gpu_usage / 100,
text = gpu_usage .. "%",
alert = gpu_usage > crit,
}
end
end

-- CPU usage formatted special for panel widget
Expand Down

0 comments on commit 1f7ce47

Please sign in to comment.