Debian Codegraph @

vicious/widgets/dio_linux.lua

raw symbols SCIP
1 -- disk I/O widget type for GNU/Linux
2 -- Copyright (C) 2011 Jörg T. <[email protected]>
3 -- Copyright (C) 2017 Elric Milon <[email protected]>
4 -- Copyright (C) 2017 mutlusun <[email protected]>
5 --
6 -- This file is part of Vicious.
7 --
8 -- Vicious is free software: you can redistribute it and/or modify
9 -- it under the terms of the GNU General Public License as
10 -- published by the Free Software Foundation, either version 2 of the
11 -- License, or (at your option) any later version.
12 --
13 -- Vicious is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 -- GNU General Public License for more details.
17 --
18 -- You should have received a copy of the GNU General Public License
19 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
20
21 -- {{{ Grab environment
22 local pairs = pairs
23 local io = { lines = io.lines }
24 local os = { time = os.time, difftime = os.difftime }
25
26 local helpers = require"vicious.helpers"
27 -- }}}
28
29 -- Initialize function tables
30 local disk_usage = {}
31 local disk_stats = {}
32 local disk_time = 0
33 -- Constant definitions
34 local unit = { ["s"] = 1, ["kb"] = 2, ["mb"] = 2048 }
35 local time_unit = { ["ms"] = 1, ["s"] = 1000 }
36
37 -- {{{ Disk I/O widget type
38 return helpers.setcall(function ()
39 local disk_lines = {}
40
41 for line in io.lines("/proc/diskstats") do
42 local device, read, write, iotime =
43 -- Linux kernel documentation: Documentation/iostats.txt
44 line:match"([^%s]+) %d+ %d+ (%d+) %d+ %d+ %d+ (%d+) %d+ %d+ (%d+)"
45 disk_lines[device] = { read, write, iotime }
46 end
47
48 local time = os.time()
49 local interval = os.difftime(time, disk_time)
50 if interval == 0 then interval = 1 end
51
52 for device, stats in pairs(disk_lines) do
53 -- Avoid insane values on startup
54 local last_stats = disk_stats[device] or stats
55
56 -- Check for overflows and counter resets (> 2^32)
57 if stats[1] < last_stats[1] or stats[2] < last_stats[2] then
58 for i = 1,3 do last_stats[i] = stats[i] end
59 end
60
61 -- Diskstats are absolute, substract our last reading
62 -- * divide by timediff because we don't know the timer value
63 local read = (stats[1] - last_stats[1]) / interval
64 local write = (stats[2] - last_stats[2]) / interval
65 local iotime = (stats[3] - last_stats[3]) / interval
66
67 -- Calculate and store I/O
68 helpers.uformat(disk_usage, device.." read", read, unit)
69 helpers.uformat(disk_usage, device.." write", write, unit)
70 helpers.uformat(disk_usage, device.." total", read + write, unit)
71 helpers.uformat(disk_usage, device.." iotime", iotime, time_unit)
72 end
73
74 disk_time = time
75 disk_stats = disk_lines
76
77 return disk_usage
78 end)
79 -- }}}