Skip to main content

Module mtracker

Module mtracker 

Source
Expand description

Memory quota tracker, core and low-level API

Β§Example

use std::{collections::VecDeque, sync::{Arc, Mutex}};
use tor_rtcompat::{CoarseInstant, CoarseTimeProvider, PreferredRuntime};
use tor_memquota::{mtracker, MemoryQuotaTracker, MemoryReclaimedError, EnabledToken};
use void::{ResultVoidExt, Void};

#[derive(Debug)]
struct TrackingQueue(Mutex<Result<Inner, MemoryReclaimedError>>);
#[derive(Debug)]
struct Inner {
    partn: mtracker::Participation,
    data: VecDeque<(Box<[u8]>, CoarseInstant)>,
}

impl TrackingQueue {
    fn push(&self, now: CoarseInstant, bytes: Box<[u8]>) -> Result<(), MemoryReclaimedError> {
        let mut inner = self.0.lock().unwrap();
        let inner = inner.as_mut().map_err(|e| e.clone())?;
        inner.partn.claim(bytes.len())?;
        inner.data.push_back((bytes, now));
        Ok(())
    }
}

impl mtracker::IsParticipant for TrackingQueue {
    fn get_oldest(&self, _: EnabledToken) -> Option<CoarseInstant> {
        let inner = self.0.lock().unwrap();
        Some(inner.as_ref().ok()?.data.front()?.1)
    }
    fn reclaim(self: Arc<Self>, _: EnabledToken) -> mtracker::ReclaimFuture {
        let mut inner = self.0.lock().unwrap();
        *inner = Err(MemoryReclaimedError::new());
        Box::pin(async { mtracker::Reclaimed::Collapsing })
    }
}

let runtime = PreferredRuntime::create().unwrap();
let config  = tor_memquota::Config::builder().max(1024*1024*1024).build().unwrap();
let trk = MemoryQuotaTracker::new(&runtime, config).unwrap();

let account = trk.new_account(None).unwrap();

let queue: Arc<TrackingQueue> = account.register_participant_with(
    runtime.now_coarse(),
    |partn| {
        Ok::<_, Void>((Arc::new(TrackingQueue(Mutex::new(Ok(Inner {
            partn,
            data: VecDeque::new(),
        })))), ()))
    },
).unwrap().void_unwrap().0;

queue.push(runtime.now_coarse(), Box::new([0; 24])).unwrap();

ModulesΒ§

bookkeeping πŸ”’
Quantity bookkeeping
reclaim πŸ”’
Reclamation algorithm
total_qty_notifier πŸ”’
TotalQtyNotifier

MacrosΒ§

find_in_tracker πŸ”’
Given a &Weak<MemoryQuotaTracker>, find an account and maybe participant
find_in_tracker_eh πŸ”’
Error handling helper for find_in_tracker

StructsΒ§

AId πŸ”’
Identifies an Account
ARecord πŸ”’
Account record, within State.accounts
Account
Handle onto an Account
AccountInner
Contents of an enabled Account
Global πŸ”’
Global parts of State
MemoryQuotaTracker
Memory data tracker
PId πŸ”’
Identifies a Participant within an Account
PRecord πŸ”’
Participant record, within ARecord.ps
Participation
Handle onto a participant’s participation in a tracker
ParticipationInner
Contents of an enabled Participation
State πŸ”’
Memory tracker inner, including mutable state
WeakAccount
Weak handle onto an Account
WeakAccountInner
Contents of an enabled WeakAccount

EnumsΒ§

Reclaimed
Outcome of IsParticipant::reclaim

ConstantsΒ§

MAX_CACHE πŸ”’
Maximum amount we’ll β€œcache” locally in a Participation
TARGET_CACHE_CLAIMING πŸ”’
Target cache size when we seem to be claiming
TARGET_CACHE_RELEASING πŸ”’
Target cache size when we seem to be releasing

TraitsΒ§

IsParticipant
Participants provide an impl of the hooks in this trait

Type AliasesΒ§

ReclaimFuture
Future returned by the IsParticipant::reclaim reclamation request