Debian Codegraph @

obvious/io/init.lua

raw symbols SCIP
1 -----------------------------------
2 -- Author: Uli Schlachter --
3 -- Copyright 2009 Uli Schlachter --
4 -----------------------------------
5
6 local io = io
7 local setmetatable = setmetatable
8 local tonumber = tonumber
9 local pairs = pairs
10 local lib = {
11 widget = require("obvious.lib.widget")
12 }
13
14 local function info(dev)
15 local f = io.open("/proc/diskstats")
16 local line
17
18 if f == nil then
19 return ret
20 end
21
22 line = f:read()
23
24 while line and not line:match(dev) do
25 line = f:read()
26 end
27
28 f:close()
29
30 if not line then
31 return nil
32 end
33
34 local ret = { }
35 -- each time matches() is called it returns the next number from line
36 local matches = line:gmatch("%d+")
37
38 -- First two are device numbers, skip them
39 matches()
40 matches()
41
42 ret.reads_completed = tonumber(matches())
43 ret.reads_merged = tonumber(matches())
44 ret.reads_sectors = tonumber(matches())
45 ret.reads_time_ms = tonumber(matches())
46 ret.writes_completed = tonumber(matches())
47 ret.writes_merged = tonumber(matches())
48 ret.writes_sectors = tonumber(matches())
49 ret.writes_time_ms = tonumber(matches())
50 ret.in_progress = tonumber(matches())
51 ret.time_ms = tonumber(matches())
52 ret.time_ms_weight = tonumber(matches())
53
54 return ret
55 end
56
57 local function get_increase(data)
58 local last = data.last
59 local cur = info(data.device)
60
61 if not cur then
62 return nil
63 end
64
65 data.last = cur
66
67 -- Fake for starting
68 if last == nil then
69 last = cur
70 end
71
72 local ret = { }
73 for k, v in pairs(cur) do
74 ret[k] = cur[k] - last[k]
75 end
76
77 return ret
78 end
79
80 local function get(data)
81 local val = get_increase(data)
82 if not val then
83 return
84 end
85 return val.writes_sectors + val.reads_sectors
86 end
87
88 local function get_data_source(device)
89 local device = device or "sda"
90 local ret = {}
91
92 ret.get = get
93 ret.device = device
94
95 return lib.widget.from_data_source(ret)
96 end
97
98 return setmetatable({}, { __call = function (_, ...) return get_data_source(...) end })
99
100 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et