aboutsummaryrefslogtreecommitdiff
path: root/crates/tor-memquota/src/mtracker/total_qty_notifier.rs
blob: 107aae545a50dee789575b9dcb15d445760255ed (plain)
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
//! `TotalQtyNotifier`
//!
//! This newtype assures that we wake up the reclamation task when nceessary

use super::*;

/// Wrapper for `TotalQty`
#[derive(Deref, Debug)]
pub(super) struct TotalQtyNotifier {
    /// Total memory usage
    ///
    /// Invariant: equal to
    /// ```text
    ///    Σ        Σ         PRecord.used
    ///     ARecord  PRecord
    /// ```
    #[deref]
    total_used: TotalQty,

    /// Condvar to wake up the reclamation task
    ///
    /// The reclamation task has another clone of this
    reclamation_task_wakeup: mpsc::Sender<()>,
}

impl TotalQtyNotifier {
    /// Make a new `TotalQtyNotifier`, which will notify a specified condvar
    pub(super) fn new_zero(reclamation_task_wakeup: mpsc::Sender<()>) -> Self {
        TotalQtyNotifier {
            total_used: TotalQty::ZERO,
            reclamation_task_wakeup,
        }
    }

    /// Record that some memory has been (or will be) allocated by a participant
    ///
    /// Signals the wakeup task if we need to.
    pub(super) fn claim(
        &mut self,
        precord: &mut PRecord,
        want: Qty,
        config: &ConfigInner,
    ) -> crate::Result<ClaimedQty> {
        let got = self
            .total_used
            .claim(&mut precord.used, want)
            .ok_or_else(|| internal!("integer overflow attempting to add claim {}", want))?;
        self.maybe_wakeup(config);
        Ok(got)
    }

    /// Check to see if we need to wake up the reclamation task, and if so, do so
    pub(super) fn maybe_wakeup(&mut self, config: &ConfigInner) {
        if self.total_used > config.max {
            match self.reclamation_task_wakeup.try_send(()) {
                Ok(()) => {}
                Err(e) if e.is_full() => {}
                // reactor shutting down, having dropped reclamation task?
                Err(e) => debug!("could not notify reclamation task: {e}"),
            };
        }
    }

    /// Declare this poisoned, and prevent further claims
    pub(super) fn set_poisoned(&mut self) {
        self.total_used.set_poisoned();
    }

    /// Record that some memory has been (or will be) freed by a participant
    pub(super) fn release(&mut self, precord: &mut PRecord, have: ClaimedQty) // infallible
    {
        // TODO if the participant's usage underflows, tell it to reclaim
        // (and log some kind of internal error)
        self.total_used.release(&mut precord.used, have);
    }
}