1
0
Fork 0

Fix battery memory usage.

This commit is contained in:
namedkitten 2020-07-10 12:08:45 +01:00
parent 34afae5d84
commit 1f09765eda
4 changed files with 38 additions and 64 deletions

View file

@ -49,7 +49,6 @@ pub const Bar = struct {
fn process(self: *Bar) !void { fn process(self: *Bar) !void {
while (self.running) { while (self.running) {
std.time.sleep(5000 * std.time.ns_per_ms); std.time.sleep(5000 * std.time.ns_per_ms);
return;
} }
} }
pub fn keep_running(self: *Bar) bool { pub fn keep_running(self: *Bar) bool {

View file

@ -11,7 +11,7 @@ const DebugAllocator = @import("debug_allocator.zig");
const Info = @import("types/info.zig").Info; const Info = @import("types/info.zig").Info;
pub fn main() !void { pub fn main() !void {
const debug: bool = true; const debug: bool = false;
var allocator: *std.mem.Allocator = undefined; var allocator: *std.mem.Allocator = undefined;
var dbgAlloc: *DebugAllocator = undefined; var dbgAlloc: *DebugAllocator = undefined;
if (debug) { if (debug) {
@ -25,11 +25,11 @@ pub fn main() !void {
var br = Bar.init(&bar); var br = Bar.init(&bar);
const widgets = [_]*Widget{ const widgets = [_]*Widget{
//&Widget.init(&textWidget.New("owo", "potato")), // 4KiB &Widget.init(&textWidget.New("owo", "potato")), // 4KiB
//&Widget.init(&textWidget.New("uwu", "tomato")), // 4KiB &Widget.init(&textWidget.New("uwu", "tomato")), // 4KiB
//&Widget.init(&weatherWidget.New(allocator, &br, "London")), // 16KiB &Widget.init(&weatherWidget.New(allocator, &br, "London")), // 16KiB
&Widget.init(&batteryWidget.New(allocator, &br)), // 756.15KiB &Widget.init(&batteryWidget.New(allocator, &br)), // 12.11KiB
//&Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiBx &Widget.init(&timeWidget.New(allocator, &br)), // 32.46KiB
}; };
bar.widgets = widgets[0..]; bar.widgets = widgets[0..];
try br.start(); try br.start();

View file

@ -5,7 +5,7 @@ const fs = std.fs;
const cwd = fs.cwd; const cwd = fs.cwd;
const colour = @import("../../formatting/colour.zig").colour; const colour = @import("../../formatting/colour.zig").colour;
pub fn compare_from_walker(allocator: *std.mem.Allocator, path: []const u8, start_path: []const u8, required_filename: []const u8) !bool { pub fn compare_path(allocator: *std.mem.Allocator, path: []const u8, start_path: []const u8, required_filename: []const u8) !bool {
if (path.len == start_path.len + 1 + required_filename.len) { if (path.len == start_path.len + 1 + required_filename.len) {
var full_path = try std.fmt.allocPrint(allocator, "{}/{}", .{ start_path, required_filename }); var full_path = try std.fmt.allocPrint(allocator, "{}/{}", .{ start_path, required_filename });
defer allocator.free(full_path); defer allocator.free(full_path);
@ -60,63 +60,38 @@ pub const BatteryWidget = struct {
var arena = std.heap.ArenaAllocator.init(provided_allocator); var arena = std.heap.ArenaAllocator.init(provided_allocator);
defer arena.deinit(); defer arena.deinit();
var allocator = &arena.allocator; var allocator = &arena.allocator;
var power_supply_walker = try fs.walkPath(allocator, "/sys/class/power_supply");
defer power_supply_walker.deinit();
var pp = PowerPaths{}; var pp = PowerPaths{};
while (try power_supply_walker.next()) |supply_entry| { var dir = try fs.cwd().openDir("/sys/class/power_supply", .{ .iterate = true });
switch (supply_entry.kind) { var iterate = dir.iterate();
.SymLink => { defer dir.close();
var filepath = supply_entry.path; while (try iterate.next()) |ent| {
var power_supply_file_walker = try fs.walkPath(allocator, filepath); var ps_dir = try std.fmt.allocPrint(provided_allocator, "/sys/class/power_supply/{}", .{ent.name});
defer power_supply_file_walker.deinit(); var supply_dir = try fs.cwd().openDir(ps_dir, .{ .iterate = true });
var supply_iterate = supply_dir.iterate();
while (try power_supply_file_walker.next()) |entry| { defer supply_dir.close();
switch (entry.kind) { while (try supply_iterate.next()) |entry| {
.File => { if (std.mem.eql(u8, entry.name, "status")) {
if (try compare_from_walker(allocator, entry.path, filepath, "status")) { pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); continue;
continue; }
} if (std.mem.eql(u8, entry.name, "power_now")) {
if (try compare_from_walker(allocator, entry.path, filepath, "power_now")) { pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.power_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); continue;
continue; }
} if (std.mem.eql(u8, entry.name, "capacity")) {
if (try compare_from_walker(allocator, entry.path, filepath, "capacity")) { pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.capacity_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); continue;
continue; }
} if (std.mem.eql(u8, entry.name, "current_now")) {
if (try compare_from_walker(allocator, entry.path, filepath, "current_now")) { pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.current_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); continue;
continue; }
} if (std.mem.eql(u8, entry.name, "voltage_now")) {
if (try compare_from_walker(allocator, entry.path, filepath, "voltage_now")) { pp.status_path = try std.fmt.allocPrint(provided_allocator, "{}/{}", .{ ps_dir, entry.name });
pp.voltage_now_path = try std.fmt.allocPrint(provided_allocator, "{}", .{entry.path}); continue;
continue; }
}
},
.SymLink,
.BlockDevice,
.CharacterDevice,
.Directory,
.NamedPipe,
.UnixDomainSocket,
.Whiteout,
.Unknown,
=> continue,
}
}
},
.File,
.BlockDevice,
.CharacterDevice,
.Directory,
.NamedPipe,
.UnixDomainSocket,
.Whiteout,
.Unknown,
=> continue,
} }
} }
@ -128,7 +103,6 @@ pub const BatteryWidget = struct {
defer pparena.deinit(); defer pparena.deinit();
var ppallocator = &pparena.allocator; var ppallocator = &pparena.allocator;
const pp = try self.get_power_paths(ppallocator); const pp = try self.get_power_paths(ppallocator);
if (true) return;
while (self.bar.keep_running()) { while (self.bar.keep_running()) {
var arena = std.heap.ArenaAllocator.init(self.allocator); var arena = std.heap.ArenaAllocator.init(self.allocator);
defer arena.deinit(); defer arena.deinit();
@ -205,7 +179,7 @@ pub const BatteryWidget = struct {
pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) BatteryWidget { pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar) BatteryWidget {
return BatteryWidget{ return BatteryWidget{
.allocator = &std.heap.loggingAllocator(allocator, std.io.getStdErr().writer()).allocator, .allocator = allocator,
.bar = bar, .bar = bar,
}; };
} }

View file

@ -86,6 +86,7 @@ pub const TimeWidget = struct {
.full_text = h, .full_text = h,
.markup = "pango", .markup = "pango",
}); });
std.time.sleep(200 * std.time.ns_per_ms); std.time.sleep(200 * std.time.ns_per_ms);
} }
} }