1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
mod allocators;
mod bind_core;
mod resource;
pub(crate) use resource::*;

pub use allocators::*;
use std::fmt::{Debug, Formatter};

use crate::bind_core::{bind_to_cpu_set, to_cpu_set};
use anyhow::Result;
use anyhow::{anyhow, Context};
use log::*;
use nix::sched::CpuSet;
use std::marker::PhantomData;
use std::mem::forget;
use std::rc::Rc;
use std::sync::{Arc, TryLockError};

pub trait CoreAllocator: Sync {
    fn allocate_core(&self) -> Option<CoreGroup>;
}

pub struct CoreGroup {
    _lock: Option<ResourceHandle>,
    inner: CoreGroupInner,
}

impl CoreGroup {
    pub fn any_core() -> Self {
        Self {
            _lock: None,
            inner: CoreGroupInner::AnyCore,
        }
    }
    pub fn cores(lock: ResourceHandle, cores: Vec<Arc<ManagedCore>>) -> Self {
        Self {
            _lock: Some(lock),
            inner: CoreGroupInner::Cores(cores),
        }
    }
    pub fn reserve(&self) -> Vec<ManagedCore> {
        match &self.inner {
            CoreGroupInner::AnyCore => {
                vec![] // no dedicated cores
            }
            CoreGroupInner::Cores(cores) => cores.iter().map(|x| x.reserve().unwrap()).collect(),
        }
    }
    pub fn bind_nth(&self, index: usize) -> Result<Cleanup> {
        match &self.inner {
            CoreGroupInner::AnyCore => Ok(Cleanup::new(None, None)),
            CoreGroupInner::Cores(cores) => {
                let core = cores.get(index).with_context(|| {
                    format!("Could not find {}th core in the group {:?}", index, cores)
                })?;
                core.bind()
            }
        }
    }

    /// This may block due to Mutex
    pub fn get_cores(&self) -> Option<Vec<usize>> {
        match &self.inner {
            CoreGroupInner::AnyCore => None,
            CoreGroupInner::Cores(cores) => Some(cores.iter().map(|x| x.index).collect()),
        }
    }
}

enum CoreGroupInner {
    AnyCore,
    Cores(Vec<Arc<ManagedCore>>),
}
impl Debug for CoreGroup {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.inner {
            CoreGroupInner::AnyCore => f.write_str("AnyCore"),
            CoreGroupInner::Cores(cores) => {
                let mut numbers = vec![];
                for c in cores {
                    numbers.push(c.get_raw());
                }
                f.write_fmt(format_args!("Cores({:?})", numbers))
            }
        }
    }
}
pub struct ManagedCore {
    index: usize,
    taken: Resource,
}
impl Debug for ManagedCore {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self.index, f)
    }
}
impl PartialEq for ManagedCore {
    fn eq(&self, other: &Self) -> bool {
        self.index.eq(&other.index)
    }
}
impl ManagedCore {
    pub fn new(index: usize) -> Self {
        Self {
            index,
            taken: Resource::new(),
        }
    }
    pub fn get_raw(&self) -> usize {
        self.index
    }
    fn bind(&self) -> anyhow::Result<Cleanup> {
        let guard = self.taken.try_lock().map_err(|x| match x {
            TryLockError::Poisoned(_) => {
                anyhow!("Poisoned")
            }
            TryLockError::WouldBlock => {
                anyhow!("Core already bound: {}", self.index)
            }
        })?;
        let prior = bind_to_cpu_set(to_cpu_set(Some(self.get_raw())))?;
        Ok(Cleanup::new(Some(prior), Some(guard)))
    }
    pub fn reserve(&self) -> anyhow::Result<ManagedCore> {
        let guard = self.taken.try_lock().map_err(|x| match x {
            TryLockError::Poisoned(_) => {
                anyhow!("Poisoned")
            }
            TryLockError::WouldBlock => {
                anyhow!("Core already bound: {}", self.index)
            }
        })?;
        forget(guard);
        Ok(ManagedCore::new(self.index))
    }
}

pub struct Cleanup<'a> {
    prior_state: Option<CpuSet>,
    alive: Option<ResourceHandle>,
    _phantom: PhantomData<(Rc<()>, &'a ())>,
}
impl<'a> Cleanup<'a> {
    pub fn new(prior_state: Option<CpuSet>, alive: Option<ResourceHandle>) -> Self {
        Self {
            prior_state,
            alive,
            _phantom: Default::default(),
        }
    }
    pub fn detach(mut self) {
        self.prior_state.take();
        forget(self.alive.take());
    }
}

impl<'a> Drop for Cleanup<'a> {
    fn drop(&mut self) {
        if let Some(prior) = self.prior_state.take() {
            if let Err(err) = bind_to_cpu_set(prior) {
                error!("Error restoring state: {:?}", err);
            }
        }
    }
}