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
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use cfx_types::H256;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use malloc_size_of_derive::MallocSizeOf as DeriveMallocSizeOf;
use std::{
    collections::{HashSet, VecDeque},
    hash::Hash,
};

const COLLECTION_QUEUE_SIZE: usize = 8;

#[derive(Debug, Hash, Eq, PartialEq, Clone, DeriveMallocSizeOf)]
pub enum CacheId {
    Block(H256),
    BlockHeader(H256),
    CompactBlock(H256),
    BlockReceipts(H256),
    BlockRewards(H256),
    BlockTraces(H256),
    TransactionAddress(H256),
    LocalBlockInfo(H256),
    BlamedHeaderVerifiedRoots(u64),
    HashByBlockNumber(u64),
}

pub struct CacheManager<T> {
    pref_cache_size: usize,
    max_cache_size: usize,
    bytes_per_cache_entry: usize,
    cache_usage: VecDeque<HashSet<T>>,
}

impl<T: MallocSizeOf + Eq + Hash> MallocSizeOf for CacheManager<T> {
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        self.cache_usage.size_of(ops)
    }
}

impl<T> CacheManager<T>
where T: Eq + Hash
{
    pub fn new(
        pref_cache_size: usize, max_cache_size: usize,
        bytes_per_cache_entry: usize,
    ) -> Self {
        CacheManager {
            pref_cache_size,
            max_cache_size,
            bytes_per_cache_entry,
            cache_usage: (0..COLLECTION_QUEUE_SIZE)
                .into_iter()
                .map(|_| Default::default())
                .collect(),
        }
    }

    pub fn note_used(&mut self, id: T) {
        if !self.cache_usage[0].contains(&id) {
            if let Some(c) = self
                .cache_usage
                .iter_mut()
                .skip(1)
                .find(|e| e.contains(&id))
            {
                c.remove(&id);
            }
            self.cache_usage[0].insert(id);
        }
    }

    /// Collects unused objects from cache.
    /// First params is the current size of the cache.
    /// Second one is an with objects to remove. It should also return new size
    /// of the cache.
    pub fn collect_garbage<F>(
        &mut self, current_size: usize, mut notify_unused: F,
    ) where F: FnMut(HashSet<T>) -> usize {
        if current_size < self.pref_cache_size {
            self.rotate_cache_if_needed();
            return;
        }

        for _ in 0..COLLECTION_QUEUE_SIZE {
            if let Some(back) = self.cache_usage.pop_back() {
                let current_size = notify_unused(back);
                debug!("Cache Manager new_size={}", current_size);
                self.cache_usage.push_front(Default::default());
                if current_size < self.max_cache_size {
                    break;
                }
            }
        }
    }

    fn rotate_cache_if_needed(&mut self) {
        if self.cache_usage.is_empty() {
            return;
        }

        if self.cache_usage[0].len() * self.bytes_per_cache_entry
            > self.pref_cache_size / COLLECTION_QUEUE_SIZE
        {
            if let Some(cache) = self.cache_usage.pop_back() {
                self.cache_usage.push_front(cache);
            }
        }
    }
}

#[derive(Debug)]
pub struct CacheSize {
    /// Blocks header cache size.
    pub block_headers: usize,
    /// Blocks cache size.
    pub blocks: usize,
    /// Compact blocks cache size.
    pub compact_blocks: usize,
    /// Block Receipts cache size.
    pub block_receipts: usize,
    /// Block Rewards cache size.
    pub block_rewards: usize,
    /// Block Traces cache size.
    pub block_traces: usize,
    /// Transaction indices cache size.
    pub transaction_indices: usize,
    /// Local block info cache size.
    pub local_block_infos: usize,
    /// Block number index cache size.
    pub hash_by_block_number: usize,
}

impl CacheSize {
    /// Total amount used by the cache.
    pub fn total(&self) -> usize {
        self.block_headers
            + self.blocks
            + self.compact_blocks
            + self.block_receipts
            + self.block_rewards
            + self.block_traces
            + self.transaction_indices
            + self.local_block_infos
    }
}