Skip to content

Commit

Permalink
Clean up core binding on main thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
porcuquine committed Oct 8, 2020
1 parent 9f33b57 commit fb5172f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
27 changes: 25 additions & 2 deletions storage-proofs/porep/src/stacked/vanilla/cores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ pub fn checkout_core_group() -> Option<MutexGuard<'static, CoreGroup>> {
}
}

pub fn bind_core(core_index: CoreIndex) -> Result<()> {
pub struct Cleanup {
prior_state: Option<hwloc::Bitmap>,
}

impl Drop for Cleanup {
fn drop(&mut self) {
match self.prior_state.take() {
Some(prior) => {
let child_topo = &TOPOLOGY;
let tid = get_thread_id();
let mut locked_topo = child_topo.lock().unwrap();
let _ = locked_topo.set_cpubind_for_thread(tid, prior, CPUBIND_THREAD);
}
None => (),
}
}
}

pub fn bind_core(core_index: CoreIndex) -> Result<Cleanup> {
let child_topo = &TOPOLOGY;
let tid = get_thread_id();
let mut locked_topo = child_topo.lock().unwrap();
Expand All @@ -59,6 +77,9 @@ pub fn bind_core(core_index: CoreIndex) -> Result<()> {
// Get only one logical processor (in case the core is SMT/hyper-threaded).
bind_to.singlify();

// Thread binding before explicit set.
let before = locked_topo.get_cpubind_for_thread(tid, CPUBIND_THREAD);

debug!("binding to {:?}", bind_to);
// Set the binding.
let result = locked_topo
Expand All @@ -69,7 +90,9 @@ pub fn bind_core(core_index: CoreIndex) -> Result<()> {
warn!("error in bind_core, {:?}", result);
}

result
Ok(Cleanup {
prior_state: before,
})
}

fn get_core_by_index<'a>(topo: &'a Topology, index: CoreIndex) -> Result<&'a TopologyObject> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,14 +439,13 @@ pub fn create_labels_for_encoding<Tree: 'static + MerkleTreeTrait, T: AsRef<[u8]

let core_group = Arc::new(checkout_core_group());

if let Some(group) = &*core_group {
// When `_cleanup_handle` is dropped, the previous binding of thread will be restored.
let _cleanup_handle = (*core_group).as_ref().map(|group| {
// This could fail, but we will ignore the error if so.
// It will be logged as a warning by `bind_core`.
debug!("binding core in main thread");
if let Some(&core_index) = group.get(0) {
let _ = bind_core(core_index);
};
}
group.get(0).map(|core_index| bind_core(*core_index))
});

// NOTE: this means we currently keep 2x sector size around, to improve speed
let (parents_cache, mut layer_labels, mut exp_labels) = setup_create_label_memory(
Expand Down

0 comments on commit fb5172f

Please sign in to comment.