Debian Codegraph @

vicious/contrib/dio_linux.lua

raw symbols SCIP
1 -- contrib/dio_linux.lua
2 -- Copyright (C) 2010, Adrian C. <[email protected]>
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- {{{ Grab environment
21 local ipairs = ipairs
22 local setmetatable = setmetatable
23 local table = { insert = table.insert }
24 local string = { gmatch = string.gmatch }
25 local helpers = require("vicious.helpers")
26 -- }}}
27
28
29 -- Disk I/O: provides I/O statistics for requested storage devices
30 -- vicious.contrib.dio
31 local dio_linux = {}
32
33
34 -- Initialize function tables
35 local disk_usage = {}
36 local disk_total = {}
37 -- Variable definitions
38 local unit = { ["s"] = 1, ["kb"] = 2, ["mb"] = 2048 }
39
40 -- {{{ Disk I/O widget type
41 local function worker(format, disk)
42 if not disk then return end
43
44 local disk_lines = { [disk] = {} }
45 local disk_stats = helpers.pathtotable("/sys/block/" .. disk)
46
47 if disk_stats.stat then
48 local match = string.gmatch(disk_stats.stat, "[%s]+([%d]+)")
49 for i = 1, 11 do -- Store disk stats
50 table.insert(disk_lines[disk], match())
51 end
52 end
53
54 -- Ensure tables are initialized correctly
55 local diff_total = { [disk] = {} }
56 if not disk_total[disk] then
57 disk_usage[disk] = {}
58 disk_total[disk] = {}
59
60 while #disk_total[disk] < #disk_lines[disk] do
61 table.insert(disk_total[disk], 0)
62 end
63 end
64
65 for i, v in ipairs(disk_lines[disk]) do
66 -- Diskstats are absolute, substract our last reading
67 diff_total[disk][i] = v - disk_total[disk][i]
68
69 -- Store totals
70 disk_total[disk][i] = v
71 end
72
73 -- Calculate and store I/O
74 helpers.uformat(disk_usage[disk], "read", diff_total[disk][3], unit)
75 helpers.uformat(disk_usage[disk], "write", diff_total[disk][7], unit)
76 helpers.uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3], unit)
77
78 -- Store I/O scheduler
79 if disk_stats.queue and disk_stats.queue.scheduler then
80 disk_usage[disk]["{sched}"] = string.gmatch(disk_stats.queue.scheduler, "%[([%a]+)%]")
81 end
82
83 return disk_usage[disk]
84 end
85 -- }}}
86
87 return setmetatable(dio_linux, { __call = function(_, ...) return worker(...) end })