Skip to content

Commit d6c5a66

Browse files
perf(kernel): do not wrap allocator in Once
Not required. Signed-off-by: Anhad Singh <[email protected]>
1 parent eaaf127 commit d6c5a66

File tree

4 files changed

+54
-42
lines changed

4 files changed

+54
-42
lines changed

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ QEMU_PATH ?= $(shell dirname $(shell which qemu-system-x86_64))
4646
qemu: $(KERNEL_TARGET) $(USERLAND_TARGET)
4747
${QEMU_PATH}/qemu-system-x86_64 -cdrom target/aero.iso -m 8G -serial stdio --boot d -s -enable-kvm -cpu host,+vmx -drive file=target/disk.img,if=none,id=NVME1,format=raw -device nvme,drive=NVME1,serial=nvme
4848

49-
# .PHONY: qemu_perf
50-
# qemu_perf:
51-
# ${QEMU_PATH}/qemu-system-x86_64 -cdrom target/aero.iso -m 8G -serial stdio --boot d -s -cpu host,+vmx -drive file=target/disk.img,if=none,id=NVME1,format=raw -device nvme,drive=NVME1,serial=nvme -plugin './target/kern-profile.so,out=raw-data,delay=1'
49+
.PHONY: qemu_perf
50+
qemu_perf: $(KERNEL_TARGET) $(USERLAND_TARGET)
51+
${QEMU_PATH}/qemu-system-x86_64 -cdrom target/aero.iso -m 8G -serial stdio --boot d -s -drive file=target/disk.img,if=none,id=NVME1,format=raw -device nvme,drive=NVME1,serial=nvme -plugin './target/kern-profile.so,out=raw-data,delay=30' -d plugin -cpu max
52+
53+
.PHONY: qemu_p
54+
qemu_p:
55+
${QEMU_PATH}/qemu-system-x86_64 -cdrom target/aero.iso -m 8G -serial stdio --boot d -s -drive file=target/disk.img,if=none,id=NVME1,format=raw -device nvme,drive=NVME1,serial=nvme -d plugin -cpu max -qmp unix:/tmp/qmp.sock,server,nowait
5256

5357
.PHONY: doc
5458
doc:

src/aero_kernel/src/mem/paging/frame.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,52 @@ const fn order_from_size(size: u64) -> usize {
6363
unreachable!()
6464
}
6565

66-
pub struct LockedFrameAllocator(Once<Mutex<GlobalFrameAllocator>>);
66+
pub struct LockedFrameAllocator(Mutex<GlobalFrameAllocator>);
6767

6868
impl LockedFrameAllocator {
6969
/// Constructs a new uninitialized and locked version of the global frame
7070
/// allocator.
7171
pub(super) const fn new_uninit() -> Self {
72-
Self(Once::new())
72+
let bstrap_ref = BootAllocRef {
73+
inner: core::ptr::null(),
74+
};
75+
76+
Self(Mutex::new(GlobalFrameAllocator {
77+
buddies: [
78+
Bitmap::empty(bstrap_ref),
79+
Bitmap::empty(bstrap_ref),
80+
Bitmap::empty(bstrap_ref),
81+
Bitmap::empty(bstrap_ref),
82+
Bitmap::empty(bstrap_ref),
83+
Bitmap::empty(bstrap_ref),
84+
Bitmap::empty(bstrap_ref),
85+
Bitmap::empty(bstrap_ref),
86+
Bitmap::empty(bstrap_ref),
87+
Bitmap::empty(bstrap_ref),
88+
],
89+
free: [0; 10],
90+
91+
base: PhysAddr::zero(),
92+
end: PhysAddr::zero(),
93+
}))
7394
}
7495

7596
/// Initializes the inner locked global frame allocator.
7697
pub(super) fn init(&self, memory_map: &mut limine::response::MemoryMapResponse) {
77-
self.0
78-
.call_once(|| Mutex::new(GlobalFrameAllocator::new(memory_map)));
98+
*self.0.lock_irq() = GlobalFrameAllocator::new(memory_map);
7999
}
80100

81101
pub fn dealloc(&self, addr: PhysAddr, size_bytes: usize) {
82102
let order = order_from_size(size_bytes as u64);
83103

84-
let mut allocator = self.0.get().unwrap().lock_irq();
104+
let mut allocator = self.0.lock_irq();
85105
allocator.deallocate_frame_inner(addr, order);
86106
}
87107

88108
pub fn alloc(&self, size_bytes: usize) -> Option<PhysAddr> {
89109
let order = order_from_size(size_bytes as u64);
90110

91-
let mut allocator = self.0.get()?.lock_irq();
111+
let mut allocator = self.0.lock_irq();
92112
allocator.allocate_frame_inner(order)
93113
}
94114

@@ -108,12 +128,8 @@ unsafe impl FrameAllocator<Size4KiB> for LockedFrameAllocator {
108128

109129
fn deallocate_frame(&self, frame: PhysFrame<Size4KiB>) {
110130
self.0
111-
.get()
112-
.map(|m| {
113-
m.lock_irq()
114-
.deallocate_frame_inner(frame.start_address(), order_from_size(Size4KiB::SIZE))
115-
})
116-
.unwrap()
131+
.lock_irq()
132+
.deallocate_frame_inner(frame.start_address(), order_from_size(Size4KiB::SIZE))
117133
}
118134
}
119135

@@ -125,12 +141,8 @@ unsafe impl FrameAllocator<Size2MiB> for LockedFrameAllocator {
125141

126142
fn deallocate_frame(&self, frame: PhysFrame<Size2MiB>) {
127143
self.0
128-
.get()
129-
.map(|m| {
130-
m.lock_irq()
131-
.deallocate_frame_inner(frame.start_address(), order_from_size(Size2MiB::SIZE))
132-
})
133-
.unwrap()
144+
.lock_irq()
145+
.deallocate_frame_inner(frame.start_address(), order_from_size(Size2MiB::SIZE))
134146
}
135147
}
136148

@@ -188,7 +200,7 @@ pub fn pmm_alloc(order: BuddyOrdering) -> PhysAddr {
188200
debug_assert!(order <= BUDDY_SIZE.len());
189201

190202
super::FRAME_ALLOCATOR
191-
.alloc_zeroed(BUDDY_SIZE[order] as _)
203+
.alloc(BUDDY_SIZE[order] as _)
192204
.unwrap()
193205
}
194206

@@ -232,13 +244,13 @@ impl BootAlloc {
232244
}
233245
}
234246

235-
#[derive(Debug, Clone)]
247+
#[derive(Debug, Clone, Copy)]
236248
struct BootAllocRef {
237249
inner: *const BootAlloc,
238250
}
239251

240252
impl BootAllocRef {
241-
fn new(inner: &BootAlloc) -> Self {
253+
const fn new(inner: &BootAlloc) -> Self {
242254
Self {
243255
inner: inner as *const _,
244256
}
@@ -357,16 +369,16 @@ impl GlobalFrameAllocator {
357369
end,
358370

359371
buddies: [
360-
Bitmap::empty(bref.clone()),
361-
Bitmap::empty(bref.clone()),
362-
Bitmap::empty(bref.clone()),
363-
Bitmap::empty(bref.clone()),
364-
Bitmap::empty(bref.clone()),
365-
Bitmap::empty(bref.clone()),
366-
Bitmap::empty(bref.clone()),
367-
Bitmap::empty(bref.clone()),
368-
Bitmap::empty(bref.clone()),
369-
Bitmap::empty(bref.clone()),
372+
Bitmap::empty(bref),
373+
Bitmap::empty(bref),
374+
Bitmap::empty(bref),
375+
Bitmap::empty(bref),
376+
Bitmap::empty(bref),
377+
Bitmap::empty(bref),
378+
Bitmap::empty(bref),
379+
Bitmap::empty(bref),
380+
Bitmap::empty(bref),
381+
Bitmap::empty(bref),
370382
],
371383
free: [0; 10],
372384
};
@@ -376,7 +388,7 @@ impl GlobalFrameAllocator {
376388
// Allocate the buddies using prealloc:
377389
for (i, bsize) in BUDDY_SIZE.iter().enumerate() {
378390
let chunk = size / bsize;
379-
this.buddies[i] = Bitmap::new_in(bref.clone(), chunk as usize);
391+
this.buddies[i] = Bitmap::new_in(bref, chunk as usize);
380392
}
381393

382394
for region in bref.get_inner().memory_ranges.lock().iter() {
@@ -545,12 +557,7 @@ impl GlobalFrameAllocator {
545557

546558
pub fn init_vm_frames() {
547559
VM_FRAMES.call_once(|| {
548-
let frame_count = super::FRAME_ALLOCATOR
549-
.0
550-
.get()
551-
.unwrap()
552-
.lock_irq()
553-
.frame_count();
560+
let frame_count = super::FRAME_ALLOCATOR.0.lock_irq().frame_count();
554561

555562
let mut frames = Vec::<VmFrame>::new();
556563
frames.resize_with(frame_count, VmFrame::new);

src/aero_kernel/src/mem/paging/page_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl PageTableEntry {
5757
}
5858

5959
/// Returns whether this entry is zero.
60+
#[inline]
6061
pub const fn is_unused(&self) -> bool {
6162
self.entry == 0
6263
}

src/aero_kernel/src/utils/bitmap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<A: Allocator> Bitmap<A> {
6060
/// ```rust
6161
/// let bitmap = Bitmap::new();
6262
/// ```
63-
pub fn empty(alloc: A) -> Self {
63+
pub const fn empty(alloc: A) -> Self {
6464
Self {
6565
bitmap: Vec::new_in(alloc),
6666
}

0 commit comments

Comments
 (0)