os: unify memory/processes cgroup limiting func

This commit is contained in:
Mitchell Hashimoto
2024-08-11 15:37:54 -07:00
parent ba41f142ed
commit a158a1d45f
2 changed files with 12 additions and 38 deletions

View File

@ -66,16 +66,16 @@ pub fn init(app: *App) ![]const u8 {
// can be monitored by things like systemd-oomd to kill if needed,
// versus an instant hard kill.
if (app.config.@"linux-cgroup-memory-limit") |limit| {
try internal_os.cgroup.configureMemoryLimit(surfaces, .{
.high = limit,
try internal_os.cgroup.configureLimit(surfaces, .{
.memory_high = limit,
});
}
// Configure the "max" pids limit. This is a hard limit and cannot be
// exceeded.
if (app.config.@"linux-cgroup-processes-limit") |limit| {
try internal_os.cgroup.configureProcessesLimit(surfaces, .{
.processes = limit,
try internal_os.cgroup.configureLimit(surfaces, .{
.pids_max = limit,
});
}

View File

@ -180,45 +180,19 @@ pub fn configureControllers(
try file.writer().writeAll(v);
}
pub const MemoryLimit = union(enum) {
/// memory.high
high: usize,
pub const Limit = union(enum) {
memory_high: usize,
pids_max: usize,
};
/// Configure the memory limit for the given cgroup. Use the various
/// fields in MemoryLimit to configure a specific type of limit.
pub fn configureMemoryLimit(cgroup: []const u8, limit: MemoryLimit) !void {
/// Configure a limit for the given cgroup. Use the various
/// fields in Limit to configure a specific type of limit.
pub fn configureLimit(cgroup: []const u8, limit: Limit) !void {
assert(cgroup[0] == '/');
const filename, const size = switch (limit) {
.high => |v| .{ "memory.high", v },
};
// Open our file
var buf: [std.fs.max_path_bytes]u8 = undefined;
const path = try std.fmt.bufPrint(
&buf,
"/sys/fs/cgroup{s}/{s}",
.{ cgroup, filename },
);
const file = try std.fs.cwd().openFile(path, .{ .mode = .write_only });
defer file.close();
// Write our limit in bytes
try file.writer().print("{}", .{size});
}
pub const ProcessesLimit = union(enum) {
/// pids.max
processes: usize,
};
/// Configure the number of processes for the given cgroup.
pub fn configureProcessesLimit(cgroup: []const u8, limit: ProcessesLimit) !void {
assert(cgroup[0] == '/');
const filename, const size = switch (limit) {
.processes => |v| .{ "pids.max", v },
.memory_high => |v| .{ "memory.high", v },
.pids_max => |v| .{ "pids.max", v },
};
// Open our file