Debian Codegraph @

vicious/widgets/mem_freebsd.lua

raw symbols SCIP
1 -- RAM and swap usage widget type for FreeBSD
2 -- Copyright (C) 2017-2019 mutlusun <[email protected]>
3 -- Copyright (C) 2018 Andreas Geisenhainer <[email protected]>
4 -- Copyright (C) 2019 Nguyễn Gia Phong <[email protected]m>
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 tonumber = tonumber
23 local math = { floor = math.floor }
24 local string = {
25 match = string.match,
26 gmatch = string.gmatch,
27 find = string.find
28 }
29
30 local helpers = require("vicious.helpers")
31 local spawn = require("vicious.spawn")
32 -- }}}
33
34 -- Mem: provides RAM and Swap usage statistics
35 -- vicious.widgets.mem_freebsd
36 local mem_freebsd = {}
37
38
39 -- {{{ Memory widget type
40 function mem_freebsd.async(format, warg, callback)
41 helpers.sysctl_async({ "hw.pagesize",
42 "vm.stats.vm",
43 "vm.swap_total",
44 "vm.swap_enabled" },
45 function(ret)
46
47 local pgsz = tonumber(ret["hw.pagesize"])
48 local _mem = { buf = {}, total = nil }
49
50 -- Get memory space in bytes
51 _mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pgsz
52 _mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pgsz
53 _mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pgsz
54 _mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pgsz
55 _mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pgsz
56
57 -- Rework into megabytes
58 _mem.total = math.floor(_mem.total/1048576)
59 _mem.buf.free = math.floor(_mem.buf.free/1048576)
60 _mem.buf.laundry = math.floor(_mem.buf.laundry/1048576)
61 _mem.buf.cache = math.floor(_mem.buf.cache/1048576)
62 _mem.buf.wired = math.floor(_mem.buf.wired/1048576)
63
64 -- Calculate memory percentage
65 _mem.free = _mem.buf.free + _mem.buf.cache
66 -- used memory basically consists of active+inactive+wired
67 _mem.inuse = _mem.total - _mem.free
68 _mem.notfreeable = _mem.inuse - _mem.buf.laundry
69 _mem.wire = _mem.buf.wired
70
71 _mem.usep = math.floor(_mem.inuse / _mem.total * 100)
72 _mem.wirep = math.floor(_mem.wire / _mem.total * 100)
73 _mem.notfreeablep = math.floor(_mem.notfreeable / _mem.total * 100)
74
75 -- Get swap states
76 local vm_swap_total = tonumber(ret["vm.swap_total"])
77 local vm_swap_enabled = tonumber(ret["vm.swap_enabled"])
78 local _swp = { buf = {}, total = nil }
79
80 if vm_swap_enabled == 1 and vm_swap_total > 0 then
81 -- Initialise variables
82 _swp.usep = 0
83 _swp.inuse = 0
84 _swp.total = 0
85 _swp.buf.free = 0
86
87 -- Read output of swapinfo in Mbytes (from async function call)
88 -- Read content and sum up
89 spawn.with_line_callback("swapinfo -m", {
90 stdout = function(line)
91 if not string.find(line, "Device") then
92 local ltotal, lused, lfree = string.match(
93 line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
94 -- Add swap space in Mbytes
95 _swp.total = _swp.total + tonumber(ltotal)
96 _swp.inuse = _swp.inuse + tonumber(lused)
97 _swp.buf.free = _swp.buf.free + tonumber(lfree)
98 end
99 end,
100 output_done = function()
101 print(_swp.inuse, _swp.total)
102 _swp.usep = math.floor(_swp.inuse / _swp.total * 100)
103 callback({ _mem.usep,
104 _mem.inuse,
105 _mem.total,
106 _mem.free,
107 _swp.usep,
108 _swp.inuse,
109 _swp.total,
110 _swp.buf.free,
111 _mem.wirep,
112 _mem.wire,
113 _mem.notfreeablep,
114 _mem.notfreeable })
115 end
116 })
117 else
118 _swp.usep = -1
119 _swp.inuse = -1
120 _swp.total = -1
121 _swp.buf.free = -1
122 callback({ _mem.usep,
123 _mem.inuse,
124 _mem.total,
125 _mem.free,
126 _swp.usep,
127 _swp.inuse,
128 _swp.total,
129 _swp.buf.free,
130 _mem.wirep,
131 _mem.wire,
132 _mem.notfreeablep,
133 _mem.notfreeable })
134 end
135 end)
136 end
137 -- }}}
138
139 return helpers.setasyncall(mem_freebsd)