oxwm and NetBSD
I cloned the oxwm repo and did a zig build in the netbsd-oxwm branch. Then in .xinitrc I added the line to launch oxwm.
exec dbus-run-session -- /home/rgeorgia/workspace/oxwm/zig-out/bin/oxwm
Everything works as expeted. I even used the default config.lua file. However, I did notice something that was a little off. The Ram block for the ‘status’ bar showed 13.5 GB for used RAM. Ram: 13.5/13.5 GB. Strange.
Wrong Ram Reading
I navigated to src/bar/blocks/ram.zig and found the code responisble for displaying the RAM data.
var total: u64 = 0;
var available: u64 = 0;
var lines = std.mem.splitScalar(u8, file_content, '\n');
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "MemTotal:")) {
total = parseMemValue(line);
} else if (std.mem.startsWith(u8, line, "MemAvailable:")) {
available = parseMemValue(line);
}
}
When executed total is 0 and available is 0. It reads the /proc/meminfo file grabbing the line that starts with “MemTotal”. The available variable is never changed so, const used = total - available; always returns the value assigned to total. The issue here is the structure for /proc/meminfo for NetBSD.
total: used: free: shared: buffers: cached:
Mem: 14480453632 3953782784 10526670848 0 1536184320 3478155264
Swap: 14972944384 0 14972944384
MemTotal: 14141068 kB
MemFree: 10279952 kB
MemShared: 0 kB
Buffers: 1500180 kB
Cached: 3396636 kB
SwapTotal: 14622016 kB
SwapFree: 14622016 kB
So, I simply added the following lines, shown here using git diff:
diff --git a/src/bar/blocks/ram.zig b/src/bar/blocks/ram.zig
index 065c68c..5b6fd31 100644
--- a/src/bar/blocks/ram.zig
+++ b/src/bar/blocks/ram.zig
@@ -31,6 +31,9 @@ pub const Ram = struct {
} else if (std.mem.startsWith(u8, line, "MemAvailable:")) {
available = parseMemValue(line);
}
+ if (std.mem.startsWith(u8, line, "MemFree:")) {
+ available = parseMemValue(line);
+ }
}
if (total == 0) return buffer[0..0];
Not a zig developer
I am not a zig developer, and I don’t play one on TV, but I am not sure why the if else if. If I understand correctly, if the line starts with MemTotal then the total var is set and available is never set. If the line starts with MemAvailable, then the available var is set and total stays at 0.
Then I got to thinking… Why not remove the else if in favor of just if and change the search criteria?
diff --git a/src/bar/blocks/ram.zig b/src/bar/blocks/ram.zig
index 065c68c..dbeeef9 100644
--- a/src/bar/blocks/ram.zig
+++ b/src/bar/blocks/ram.zig
@@ -28,7 +28,8 @@ pub const Ram = struct {
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "MemTotal:")) {
total = parseMemValue(line);
- } else if (std.mem.startsWith(u8, line, "MemAvailable:")) {
+ }
+ if (std.mem.startsWith(u8, line, "MemFree:")) {
available = parseMemValue(line);
}
}
I would like to chat a friend
I did run the fix through chatGPT and was given this suggestion, which I liked.
I recommend using MemFree as a fallback when MemAvailable is not present. That preserves the original Linux behavior while correcting the NetBSD behavior.
So, instead of totally removing MemAvailable, I’ll keep it to preserve it’s “linuxy” characteristic and add MemFree if MemAvailable is missing.
Now the diff looks like this.
diff --git a/src/bar/blocks/ram.zig b/src/bar/blocks/ram.zig
index 065c68c..87b088c 100644
--- a/src/bar/blocks/ram.zig
+++ b/src/bar/blocks/ram.zig
@@ -23,13 +23,20 @@ pub const Ram = struct {
var total: u64 = 0;
var available: u64 = 0;
+ var has_available = false;
var lines = std.mem.splitScalar(u8, file_content, '\n');
+
while (lines.next()) |line| {
if (std.mem.startsWith(u8, line, "MemTotal:")) {
total = parseMemValue(line);
} else if (std.mem.startsWith(u8, line, "MemAvailable:")) {
available = parseMemValue(line);
+ has_available = true;
+ } else if (std.mem.startsWith(u8, line, "MemFree:")) {
+ if (!has_available) {
+ available = parseMemValue(line);
+ }
}
}
Test are good, IF they’re good
I did as chat to help me write unit tests to verify the changes. Chat suggested 7 tests, 6 of them were useless so I ‘deep sixed’ them and kept the one.
test "read proc meminfo" {
const io = std.testing.io;
const file = std.Io.Dir.openFileAbsolute( io, "/proc/meminfo", .{})
catch |err| {
if (err == error.FileNotFound)
return error.SkipZigTest;
return err;
};
defer file.close(io);
var buf: [512]u8 = undefined;
const bytes_read = try file.readStreaming(
io, &.{&buf}
);
try std.testing.expect(bytes_read > 0);
}
I should be committed
I did fork Tony’s repo, created a “fix” branch and submitted a pull request. We will see what happens.