Skip to content

Commit

Permalink
Reverted changes to the memory allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
hunhoffe committed Dec 6, 2023
1 parent 474cf7a commit b9622df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
40 changes: 12 additions & 28 deletions usr/init/src/dynrep/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
use alloc::sync::Arc;
use core::slice::from_raw_parts_mut;
use core::sync::atomic::AtomicU8;
use core::sync::atomic::Ordering;
use lazy_static::lazy_static;

use kpi::system::NodeId;
use spin::Mutex;
use x86::bits64::paging::LARGE_PAGE_SIZE;

use core::{
alloc::{AllocError, Allocator, Layout},
ptr::NonNull,
};
use log::info;

pub const BASE: u64 = 0x0510_0000_0000;
pub const MAX_FRAMES: u64 = 600;

lazy_static! {
pub(crate) static ref ALLOC_AFFINITY: Arc<Mutex<NodeId>> = Arc::new(Mutex::new(0));
}
static NODE_ID: AtomicU8 = AtomicU8::new(1);

#[derive(Clone, Copy)]
pub struct MyAllocator;

impl MyAllocator {
fn allocate_pages(node_id: NodeId) {
fn allocate_pages(node_id: u8) {
let mut allocated = 0;
let node_offset = (node_id - 1) as u64 * LARGE_PAGE_SIZE as u64 * MAX_FRAMES;
while allocated < MAX_FRAMES {
Expand All @@ -33,12 +30,7 @@ impl MyAllocator {
let (frame_id, paddr) =
vibrio::syscalls::PhysicalMemory::allocate_large_page(node_id as usize)
.expect("Failed to get physical memory large page");
log::trace!(
"large frame id={:?}, paddr={:?}, mid={:?}",
frame_id,
paddr,
node_id
);
info!("large frame id={:?}, paddr={:?}", frame_id, paddr);

// Map allocated physical memory into user space so we can actually access it.
unsafe {
Expand All @@ -50,34 +42,26 @@ impl MyAllocator {
}
allocated += 1;
}
info!("# Allocated {} frames on {}", allocated, node_id);
}
}

unsafe impl Allocator for MyAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
// Check argument
log::info!("{:?}", layout);
let node_id = NODE_ID.fetch_add(1, Ordering::SeqCst);
let node_offset = (node_id - 1) as u64 * LARGE_PAGE_SIZE as u64 * MAX_FRAMES;
MyAllocator::allocate_pages(node_id);
info!("# Allocating {:?}", layout);
if layout.size() > LARGE_PAGE_SIZE * MAX_FRAMES as usize {
return Err(AllocError);
}

// Grab affinity lock
let affinity = (*ALLOC_AFFINITY).lock();
log::info!("Affinity for alloc is: {:?}", *affinity + 1);

// Allocate and map pages
MyAllocator::allocate_pages(*affinity + 1);

// Get ptr to mapped memory
let node_offset = *affinity as u64 * LARGE_PAGE_SIZE as u64 * MAX_FRAMES;
let slice = unsafe { from_raw_parts_mut((BASE + node_offset) as *mut u8, layout.size()) };

log::info!("Finished allocating on {:?}", *affinity + 1);
Ok(NonNull::from(slice))
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
log::info!("Deallocating {:?}", layout);
info!("# Deallocating {:?}", layout);
/*for i in 0..MAX_FRAMES {
vibrio::syscalls::VSpace::unmap((BASE + (i * LARGE_PAGE_SIZE as u64)) as u64, LARGE_PAGE_SIZE as u64)
.expect("Failed to unmap base page");
Expand Down
25 changes: 2 additions & 23 deletions usr/init/src/dynrep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use x86::bits64::paging::VAddr;
use x86::random::rdrand64;

mod allocator;
use allocator::{MyAllocator, ALLOC_AFFINITY};
use allocator::MyAllocator;

pub const NUM_ENTRIES: u64 = 50_000_000;

Expand Down Expand Up @@ -176,28 +176,7 @@ pub fn userspace_dynrep_test() {
// TODO: change this to change number of replicas
let num_replicas = NonZeroUsize::new(nnodes).unwrap(); // NonZeroUsize::new(1).unwrap();

let replicas = Arc::new(
NodeReplicated::<HashTable>::new(num_replicas, |afc: AffinityChange| {
log::trace!("Got AffinityChange: {:?}", afc);
match afc {
AffinityChange::Replica(r) => {
let mut affinity = (*ALLOC_AFFINITY).lock();
let old_affinity = *affinity;
*affinity = r;
log::trace!("Set alloc affinity to {:?}", r + 1);
return old_affinity;
}
AffinityChange::Revert(orig) => {
//pcm.set_mem_affinity(orig).expect("Can't set affinity");
let mut affinity = (*ALLOC_AFFINITY).lock();
*affinity = orig;
log::trace!("Restored alloc affinity to {:?}", orig + 1);
return 0;
}
}
})
.unwrap(),
);
let replicas = Arc::new(NodeReplicated::<HashTable>::new(num_replicas, |_| 0).unwrap());

let s = &vibrio::upcalls::PROCESS_SCHEDULER;
let mut gtids = Vec::with_capacity(ncores);
Expand Down

0 comments on commit b9622df

Please sign in to comment.