Skip to content

Commit d9287de

Browse files
authored
Merge pull request #56 from psycorama/master
FreeBSD memory widgets bugfixes and improvements
2 parents a81435e + 0c2313d commit d9287de

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,10 @@ Supported platforms: Linux, FreeBSD.
323323
total system memory, 4th as free memory, 5th as swap usage in percent, 6th
324324
as swap usage, 7th as total system swap, 8th as free swap and 9th as
325325
memory usage with buffers and cache
326-
* FreeBSD: see above, but 10th value as memory usage with buffers and cache
327-
as percent and 11th value as wired memory (memory used by kernel) is
328-
reported
326+
* FreeBSD: see above, but there are four more values: the 9th value is wired memory
327+
in percent, the 10th value is wired memory. The 11th and 12th value return
328+
'not freeable memory' (basically active+inactive+wired) in percent and megabytes,
329+
respectively.
329330

330331
**vicious.widgets.mpd**
331332

@@ -805,8 +806,8 @@ Format functions can be used as well:
805806
naughty.notify({ title = "Battery indicator",
806807
text = vicious.call(vicious.widgets.bat, function(widget, args)
807808
return string.format("%s: %10sh\n%s: %14d%%\n%s: %12dW",
808-
"Remaining time", args[3],
809-
"Wear level", args[4],
809+
"Remaining time", args[3],
810+
"Wear level", args[4],
810811
"Present rate", args[5])
811812
end, "0") })
812813
end)

widgets/bat_freebsd.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ local function worker(format, warg)
3030
state = ""
3131
elseif bat_info["State"] == "charging" then
3232
state = "+"
33+
elseif bat_info["State"] == "critical charging" then
34+
state = "+"
3335
elseif bat_info["State"] == "discharging" then
3436
state = "-"
3537
else

widgets/mem_freebsd.lua

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,56 @@ local function worker(format)
1616
local vm_stats = helpers.sysctl_table("vm.stats.vm")
1717
local _mem = { buf = {}, total = nil }
1818

19+
-- Get memory space in bytes
1920
_mem.total = tonumber(vm_stats.v_page_count) * pagesize
20-
_mem.buf.f = tonumber(vm_stats.v_free_count) * pagesize
21-
_mem.buf.a = tonumber(vm_stats.v_active_count) * pagesize
22-
_mem.buf.i = tonumber(vm_stats.v_inactive_count) * pagesize
23-
_mem.buf.c = tonumber(vm_stats.v_cache_count) * pagesize
24-
_mem.buf.w = tonumber(vm_stats.v_wire_count) * pagesize
21+
_mem.buf.free = tonumber(vm_stats.v_free_count) * pagesize
22+
_mem.buf.laundry = tonumber(vm_stats.v_laundry_count) * pagesize
23+
_mem.buf.cache = tonumber(vm_stats.v_cache_count) * pagesize
24+
_mem.buf.wired = tonumber(vm_stats.v_wire_count) * pagesize
2525

26-
-- rework into Megabytes
27-
_mem.total = math.floor(_mem.total/(1024*1024))
28-
_mem.buf.f = math.floor(_mem.buf.f/(1024*1024))
29-
_mem.buf.a = math.floor(_mem.buf.a/(1024*1024))
30-
_mem.buf.i = math.floor(_mem.buf.i/(1024*1024))
31-
_mem.buf.c = math.floor(_mem.buf.c/(1024*1024))
32-
_mem.buf.w = math.floor(_mem.buf.w/(1024*1024))
26+
-- Rework into megabytes
27+
_mem.total = math.floor(_mem.total/1048576)
28+
_mem.buf.free = math.floor(_mem.buf.free/1048576)
29+
_mem.buf.laundry = math.floor(_mem.buf.laundry/1048576)
30+
_mem.buf.cache = math.floor(_mem.buf.cache/1048576)
31+
_mem.buf.wired = math.floor(_mem.buf.wired/1048576)
3332

3433
-- Calculate memory percentage
35-
_mem.free = _mem.buf.f + _mem.buf.c
36-
_mem.inuse = _mem.buf.a + _mem.buf.i
37-
_mem.wire = _mem.buf.w
38-
_mem.bcuse = _mem.total - _mem.buf.f
34+
_mem.free = _mem.buf.free + _mem.buf.cache
35+
-- used memory basically consists of active+inactive+wired
36+
_mem.inuse = _mem.total - _mem.free
37+
_mem.notfreeable = _mem.inuse - _mem.buf.laundry
38+
_mem.wire = _mem.buf.wired
39+
3940
_mem.usep = math.floor(_mem.inuse / _mem.total * 100)
40-
_mem.inusep= math.floor(_mem.inuse / _mem.total * 100)
41-
_mem.buffp = math.floor(_mem.bcuse / _mem.total * 100)
42-
_mem.wirep = math.floor(_mem.wire / _mem.total * 100)
41+
_mem.wirep = math.floor(_mem.wire / _mem.total * 100)
42+
_mem.notfreeablep = math.floor(_mem.notfreeable / _mem.total * 100)
4343

4444
-- Get swap states
45-
local vm = helpers.sysctl_table("vm")
45+
local vm_swap_total = tonumber(helpers.sysctl("vm.swap_total"))
46+
local vm_swap_enabled = tonumber(helpers.sysctl("vm.swap_enabled"))
4647
local _swp = { buf = {}, total = nil }
47-
48-
if tonumber(vm.swap_enabled) == 1 and tonumber(vm.swap_total) > 0 then
49-
-- Get swap space
50-
_swp.total = tonumber(vm.swap_total)
51-
_swp.buf.f = _swp.total - tonumber(vm_stats.v_swapin)
48+
49+
if vm_swap_enabled == 1 and vm_swap_total > 0 then
50+
-- Get swap space in bytes
51+
_swp.total = vm_swap_total
52+
_swp.buf.free = _swp.total - tonumber(vm_stats.v_swapin)
5253
-- Rework into megabytes
53-
_swp.total = math.floor(_swp.total/(1024*1024))
54-
_swp.buf.f = math.floor(_swp.buf.f/(1024*1024))
54+
_swp.total = math.floor(_swp.total/1048576)
55+
_swp.buf.free = math.floor(_swp.buf.free/1048576)
5556
-- Calculate percentage
56-
_swp.inuse = _swp.total - _swp.buf.f
57+
_swp.inuse = _swp.total - _swp.buf.free
5758
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
5859
else
5960
_swp.usep = -1
6061
_swp.inuse = -1
6162
_swp.total = -1
62-
_swp.buf.f = -1
63+
_swp.buf.free = -1
6364
end
6465

6566
return { _mem.usep, _mem.inuse, _mem.total, _mem.free,
66-
_swp.usep, _swp.inuse, _swp.total, _swp.buf.f,
67-
_mem.bcuse, _mem.buffp, _mem.wirep }
67+
_swp.usep, _swp.inuse, _swp.total, _swp.buf.free,
68+
_mem.wirep, _mem.wire, _mem.notfreeablep, _mem.notfreeable }
6869
end
6970

7071
return setmetatable(mem_freebsd, { __call = function(_, ...) return worker(...) end })

0 commit comments

Comments
 (0)