common_memory_manager/lib.rs
1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Generic memory management for resource-constrained operations.
16//!
17//! This crate provides a reusable memory quota system based on semaphores,
18//! allowing different subsystems (compaction, flush, index build, etc.) to
19//! share the same allocation logic while using their own metrics.
20
21mod error;
22mod granularity;
23mod guard;
24mod manager;
25mod policy;
26
27#[cfg(test)]
28mod tests;
29
30pub use error::{Error, Result};
31pub use granularity::PermitGranularity;
32pub use guard::MemoryGuard;
33pub use manager::{MemoryManager, MemoryMetrics};
34pub use policy::{DEFAULT_MEMORY_WAIT_TIMEOUT, OnExhaustedPolicy};
35
36/// No-op metrics implementation for testing.
37#[derive(Clone, Copy, Debug, Default)]
38pub struct NoOpMetrics;
39
40impl MemoryMetrics for NoOpMetrics {
41 #[inline(always)]
42 fn set_limit(&self, _: i64) {}
43
44 #[inline(always)]
45 fn set_in_use(&self, _: i64) {}
46
47 #[inline(always)]
48 fn inc_rejected(&self, _: &str) {}
49}