vips/cache.rs
1//! Cache and memory traces: Encapsulating 'vips_cache_*' and 'vips_tracked_*'
2//!
3//! Reference:
4//! - <https://www.libvips.org/API/current/libvips-cache.html>
5//! - <https://www.libvips.org/API/current/libvips-memory.html>
6
7/// Sets the maximum number of compute nodes that can be retained in the cache
8///
9/// # Arguments
10/// * `n` - The maximum number of operations to retain
11///
12pub fn set_max_operations(n: i32) {
13 unsafe { vips_sys::vips_cache_set_max(n) }
14}
15
16/// Set the maximum memory allowed in bytes for the cache
17///
18/// # Arguments
19/// * `n` - The maximum memory in bytes
20///
21pub fn set_max_mem_bytes(n: usize) {
22 unsafe { vips_sys::vips_cache_set_max_mem(n) }
23}
24
25/// Set the maximum number of files allowed to open at the same time
26///
27/// # Arguments
28/// * `n` - The maximum number of open files
29///
30pub fn set_max_files(n: i32) {
31 unsafe { vips_sys::vips_cache_set_max_files(n) }
32}
33
34/// Gets the number of compute nodes in the current cache
35///
36/// # Returns
37/// The current size of the operations cache
38///
39pub fn size_operations() -> i32 {
40 unsafe { vips_sys::vips_cache_get_size() }
41}
42
43/// Gets the maximum number of compute nodes configured
44///
45/// # Returns
46/// The maximum size of the operations cache
47///
48pub fn max_operations() -> i32 {
49 unsafe { vips_sys::vips_cache_get_max() }
50}
51
52/// Get the maximum cache memory (bytes) configured
53///
54/// # Returns
55/// The maximum memory in bytes for the cache
56///
57pub fn max_mem_bytes() -> usize {
58 unsafe { vips_sys::vips_cache_get_max_mem() }
59}
60
61/// Get the maximum number of files configured
62///
63/// # Returns
64/// The maximum number of open files allowed
65///
66pub fn max_files() -> i32 {
67 unsafe { vips_sys::vips_cache_get_max_files() }
68}
69
70/// Currently tracked memory (bytes)
71///
72/// # Returns
73/// The current memory usage in bytes tracked by libvips
74///
75pub fn tracked_mem_bytes() -> usize {
76 unsafe { vips_sys::vips_tracked_get_mem() }
77}
78
79/// Historical Peak Memory Usage (bytes)
80///
81/// # Returns
82/// The highwater mark of memory usage in bytes tracked by libvips
83///
84pub fn tracked_mem_highwater_bytes() -> usize {
85 unsafe { vips_sys::vips_tracked_get_mem_highwater() }
86}
87
88/// Print the diagnostic information for each cache hit/build (1 on, 0 off)
89///
90/// # Arguments
91/// * `trace` - Enable or disable cache tracing
92///
93pub fn set_trace(trace: bool) {
94 unsafe { vips_sys::vips_cache_set_trace(if trace { 1 } else { 0 }) }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use crate::init;
101
102 #[test]
103 fn version_works() {
104 // Version queries do not require mandatory init
105 let v = crate::version::version();
106 assert!(v.0 >= 8);
107 assert!(crate::version::version_string().contains("8."));
108 }
109
110 #[test]
111 fn init_is_idempotent() {
112 init::init(Some("test-app")).unwrap();
113 init::init(Some("test-app")).unwrap();
114 assert!(init::is_initialized());
115 }
116
117 #[test]
118 fn cache_controls() {
119 // No enforcement init: These interfaces do not rely on explicit initialization in most cases, but are safe to do with init
120 let _ = init::init(Some("cache-test"));
121 set_max_operations(256);
122 set_max_mem_bytes(64 * 1024 * 1024);
123 set_max_files(64);
124 assert!(max_operations() >= 1);
125 assert!(max_mem_bytes() >= 1);
126 assert!(max_files() >= 1);
127 }
128}