vicious/widgets/dio_linux.lua

77 lines
2.4 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2011, Jörg T. <jthalheim@gmail.com>
2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- {{{ Grab environment
local pairs = pairs
local io = { lines = io.lines }
local setmetatable = setmetatable
2011-03-29 16:41:24 +02:00
local string = { match = string.match }
local helpers = require("vicious.helpers")
local os = {
time = os.time,
difftime = os.difftime
}
-- }}}
-- Disk I/O: provides I/O statistics for requested storage devices
-- vicious.widgets.dio
2017-01-25 17:50:55 +01:00
local dio_linux = {}
-- Initialize function tables
local disk_usage = {}
local disk_stats = {}
local disk_time = 0
-- Constant definitions
local unit = { ["s"] = 1, ["kb"] = 2, ["mb"] = 2048 }
2017-01-20 20:48:06 +01:00
local time_unit = { ["ms"] = 1, ["s"] = 1000 }
-- {{{ Disk I/O widget type
local function worker(format)
local disk_lines = {}
for line in io.lines("/proc/diskstats") do
2017-01-20 20:48:06 +01:00
local device, read, write, iotime =
2011-03-29 16:41:24 +02:00
-- Linux kernel documentation: Documentation/iostats.txt
2017-01-20 20:48:06 +01:00
string.match(line, "([^%s]+) %d+ %d+ (%d+) %d+ %d+ %d+ (%d+) %d+ %d+ (%d+)")
disk_lines[device] = { read, write, iotime }
end
local time = os.time()
local interval = os.difftime(time, disk_time)
if interval == 0 then interval = 1 end
for device, stats in pairs(disk_lines) do
-- Avoid insane values on startup
local last_stats = disk_stats[device] or stats
-- Check for overflows and counter resets (> 2^32)
if stats[1] < last_stats[1] or stats[2] < last_stats[2] then
2017-01-20 20:48:06 +01:00
last_stats[1], last_stats[2], last_stats[3] = stats[1], stats[2], stats[3]
end
-- Diskstats are absolute, substract our last reading
-- * divide by timediff because we don't know the timer value
local read = (stats[1] - last_stats[1]) / interval
local write = (stats[2] - last_stats[2]) / interval
2017-01-20 20:48:06 +01:00
local iotime = (stats[3] - last_stats[3]) / interval
-- Calculate and store I/O
helpers.uformat(disk_usage, device.." read", read, unit)
helpers.uformat(disk_usage, device.." write", write, unit)
helpers.uformat(disk_usage, device.." total", read + write, unit)
2017-01-20 20:48:06 +01:00
helpers.uformat(disk_usage, device.." iotime", iotime, time_unit)
end
disk_time = time
disk_stats = disk_lines
return disk_usage
end
-- }}}
2017-01-25 17:50:55 +01:00
return setmetatable(dio_linux, { __call = function(_, ...) return worker(...) end })