rtl: add BitMap

Adds a new BitMap type in rtl for static bitmaps, now used to back
ke.CpuMask.
This commit is contained in:
Loïc Grégoire
2026-05-08 14:58:57 -04:00
parent 5d8a9a4646
commit 3a701067cd
4 changed files with 88 additions and 43 deletions
+1 -39
View File
@@ -67,42 +67,4 @@ pub fn ExportedCpuLocal(comptime T: type, comptime init: T, comptime name: []con
}
/// Bitmask of CPUs.
pub const CpuMask = struct {
const bits_per_word = @bitSizeOf(usize);
const num_words = (config.ncpus + bits_per_word - 1) / bits_per_word;
bits: [num_words]usize,
/// Return an empty `CpuMask`.
pub fn empty() CpuMask {
return CpuMask{ .bits = @splat(0) };
}
/// Set the bit corresponding to `cpu_id`.
pub fn set(self: *CpuMask, cpu_id: usize) void {
const word_index = cpu_id / bits_per_word;
const bit_index = cpu_id % bits_per_word;
self.bits[word_index] |= (@as(usize, 1) << @intCast(bit_index));
}
/// Clear the bit corresponding to `cpu_id`.
pub fn clear(self: *CpuMask, cpu_id: usize) void {
const word_index = cpu_id / bits_per_word;
const bit_index = cpu_id % bits_per_word;
self.bits[word_index] &= ~(@as(usize, 1) << @intCast(bit_index));
}
/// Check whether the bit corresponding to `cpu_id` is set.
pub fn is_set(self: *const CpuMask, cpu_id: usize) bool {
const word_index = cpu_id / bits_per_word;
const bit_index = cpu_id % bits_per_word;
return (self.bits[word_index] & (@as(usize, 1) << @intCast(bit_index))) != 0;
}
pub fn is_full(self: *const CpuMask) bool {
for (self.bits) |word| {
if (word != std.math.maxInt(usize)) return false;
}
return true;
}
};
pub const CpuMask = rtl.BitMap(config.ncpus);
+4 -4
View File
@@ -846,7 +846,7 @@ fn find_most_loaded(exclude: ?*ke.CpuMask) ?u32 {
for (0..ke.ncpus) |i| {
if (exclude) |mask| {
if (mask.is_set(i)) {
if (mask.get(i)) {
continue;
}
}
@@ -881,7 +881,7 @@ fn find_least_loaded(exclude: ?*ke.CpuMask) ?u32 {
for (0..ke.ncpus) |i| {
if (exclude) |mask| {
if (mask.is_set(i)) {
if (mask.get(i)) {
continue;
}
}
@@ -969,7 +969,7 @@ pub fn idle(_: ?*anyopaque) noreturn {
/// Called every second to balance work between CPUs on CPU0.
/// Takes a thread from the most loaded CPU and puts it on the least loaded one.
fn balance(_: ?*anyopaque) void {
var high_mask = ke.CpuMask.empty();
var high_mask = ke.CpuMask.init(false);
var low_mask: ke.CpuMask = undefined;
while (true) {
@@ -984,7 +984,7 @@ fn balance(_: ?*anyopaque) void {
high_mask.set(high.?);
low_mask = high_mask;
if (high_mask.is_full()) {
if (high_mask.is_all(true)) {
// All CPUs are masked, nothing to steal.
break;
}