From 54a7b7dc7362d9f91859a0194227d7fd0201dbd6 Mon Sep 17 00:00:00 2001 From: ferreo Date: Mon, 10 Feb 2025 18:11:06 +0000 Subject: [PATCH] Fix memory leak on error, fix power ppd being required for falcond to launch. --- .github/release-nest-v3 | 2 +- falcond/debian/changelog | 6 ++++++ falcond/src/clients/power_profiles.zig | 8 ++++++-- falcond/src/daemon.zig | 28 +++++++++++++++++--------- falcond/src/profile/manager.zig | 16 +++++++-------- 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/.github/release-nest-v3 b/.github/release-nest-v3 index 56a6051..d8263ee 100644 --- a/.github/release-nest-v3 +++ b/.github/release-nest-v3 @@ -1 +1 @@ -1 \ No newline at end of file +2 \ No newline at end of file diff --git a/falcond/debian/changelog b/falcond/debian/changelog index 0327a88..5d4974a 100644 --- a/falcond/debian/changelog +++ b/falcond/debian/changelog @@ -1,3 +1,9 @@ +falcond (1.1.0-101pika1) pika; urgency=low + + * Fix memory leak on error, fix power ppd being required for falcond to launch. + + -- ferreo Sun, 12 Jan 2025 13:48:00 +0300 + falcond (1.0.9-101pika2) pika; urgency=low * Config parsing fixes diff --git a/falcond/src/clients/power_profiles.zig b/falcond/src/clients/power_profiles.zig index 3c0ca86..e9ac6bb 100644 --- a/falcond/src/clients/power_profiles.zig +++ b/falcond/src/clients/power_profiles.zig @@ -13,7 +13,7 @@ pub const PowerProfiles = struct { original_profile: ?[]const u8, has_performance: bool, - pub fn init(allocator: std.mem.Allocator, config: Config) !*PowerProfiles { + pub fn init(allocator: std.mem.Allocator, config: Config) !?*PowerProfiles { var self = try allocator.create(PowerProfiles); errdefer allocator.destroy(self); @@ -37,7 +37,11 @@ pub const PowerProfiles = struct { .has_performance = false, }; - const profiles = try self.getAvailableProfiles(allocator); + const profiles = self.getAvailableProfiles(allocator) catch |err| { + std.log.err("Failed to get power profiles: {}, power profiles will be disabled", .{err}); + allocator.destroy(self); + return null; + }; defer { for (profiles) |profile| { allocator.free(profile); diff --git a/falcond/src/daemon.zig b/falcond/src/daemon.zig index 31799d1..a93c815 100644 --- a/falcond/src/daemon.zig +++ b/falcond/src/daemon.zig @@ -15,7 +15,7 @@ pub const Daemon = struct { profile_manager: ProfileManager, oneshot: bool, known_pids: ?std.AutoHashMap(u32, *const Profile), - power_profiles: *PowerProfiles, + power_profiles: ?*PowerProfiles, performance_mode: bool, last_profiles_check: i128, last_config_check: i128, @@ -30,13 +30,17 @@ pub const Daemon = struct { const system_conf_path_owned = try allocator.dupe(u8, system_conf_path); errdefer allocator.free(system_conf_path_owned); - const config = try Config.load(allocator, config_path, system_conf_path); - const power_profiles = try PowerProfiles.init(allocator, config); - errdefer power_profiles.deinit(); + var config = try Config.load(allocator, config_path, system_conf_path); + errdefer config.deinit(); - const performance_mode = power_profiles.isPerformanceAvailable(); - if (performance_mode) { - std.log.info("Performance profile available - power profile management enabled", .{}); + const power_profiles = try PowerProfiles.init(allocator, config); + var performance_mode = false; + + if (power_profiles) |pp| { + performance_mode = pp.isPerformanceAvailable(); + if (performance_mode) { + std.log.info("Performance profile available - power profile management enabled", .{}); + } } var profile_manager = ProfileManager.init(allocator, power_profiles, config); @@ -66,7 +70,9 @@ pub const Daemon = struct { pub fn deinit(self: *Self) void { scx_scheds.deinit(); self.profile_manager.deinit(); - self.power_profiles.deinit(); + if (self.power_profiles) |pp| { + pp.*.deinit(); + } if (self.known_pids) |*map| { map.deinit(); } @@ -86,7 +92,9 @@ pub const Daemon = struct { } self.profile_manager.deinit(); - self.power_profiles.deinit(); + if (self.power_profiles) |pp| { + pp.*.deinit(); + } self.config.deinit(); const config = try Config.load(self.allocator, self.config_path, self.system_conf_path); @@ -96,7 +104,7 @@ pub const Daemon = struct { self.config = config; self.power_profiles = power_profiles; - self.performance_mode = self.power_profiles.isPerformanceAvailable(); + self.performance_mode = if (power_profiles) |pp| pp.isPerformanceAvailable() else false; self.profile_manager = profile_manager; } diff --git a/falcond/src/profile/manager.zig b/falcond/src/profile/manager.zig index fa9bac4..375ad41 100644 --- a/falcond/src/profile/manager.zig +++ b/falcond/src/profile/manager.zig @@ -14,10 +14,10 @@ pub const ProfileManager = struct { proton_profile: ?*const Profile, active_profile: ?*const Profile = null, queued_profiles: std.ArrayList(*const Profile), - power_profiles: *PowerProfiles, + power_profiles: ?*PowerProfiles, config: Config, - pub fn init(allocator: std.mem.Allocator, power_profiles: *PowerProfiles, config: Config) ProfileManager { + pub fn init(allocator: std.mem.Allocator, power_profiles: ?*PowerProfiles, config: Config) ProfileManager { return .{ .allocator = allocator, .profiles = std.ArrayList(Profile).init(allocator), @@ -47,9 +47,9 @@ pub const ProfileManager = struct { std.log.info("Activating profile: {s}", .{profile.name}); self.active_profile = profile; - if (profile.performance_mode and self.power_profiles.isPerformanceAvailable()) { + if (profile.performance_mode and self.power_profiles != null and self.power_profiles.?.isPerformanceAvailable()) { std.log.info("Enabling performance mode for profile: {s}", .{profile.name}); - self.power_profiles.enablePerformanceMode(); + self.power_profiles.?.enablePerformanceMode(); } const effective_mode = if (self.config.vcache_mode != .none) @@ -80,9 +80,9 @@ pub const ProfileManager = struct { if (active == profile) { std.log.info("Deactivating profile: {s}", .{profile.name}); - if (profile.performance_mode and self.power_profiles.isPerformanceAvailable()) { + if (profile.performance_mode and self.power_profiles != null and self.power_profiles.?.isPerformanceAvailable()) { std.log.info("Disabling performance mode for profile: {s}", .{profile.name}); - self.power_profiles.disablePerformanceMode(); + self.power_profiles.?.disablePerformanceMode(); } vcache_setting.applyVCacheMode(.none); @@ -102,9 +102,9 @@ pub const ProfileManager = struct { if (active == profile) { std.log.info("Unloading profile: {s}", .{profile.name}); - if (profile.performance_mode and self.power_profiles.isPerformanceAvailable()) { + if (profile.performance_mode and self.power_profiles != null and self.power_profiles.?.isPerformanceAvailable()) { std.log.info("Disabling performance mode for profile: {s}", .{profile.name}); - self.power_profiles.disablePerformanceMode(); + self.power_profiles.?.disablePerformanceMode(); } vcache_setting.applyVCacheMode(.none);