• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024, The Android Open Source Project
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 //! This module provides policies to manage zram features.
16 
17 pub mod idle;
18 pub mod recompression;
19 pub mod setup;
20 pub mod stats;
21 pub mod writeback;
22 
23 use std::io;
24 
25 // Files for zram general information
26 const ZRAM_DISKSIZE_PATH: &str = "/sys/block/zram0/disksize";
27 const ZRAM_MM_STAT_PATH: &str = "/sys/block/zram0/mm_stat";
28 const ZRAM_COMP_ALGORITHM_PATH: &str = "/sys/block/zram0/comp_algorithm";
29 
30 // Files for memory tracking
31 const ZRAM_IDLE_PATH: &str = "/sys/block/zram0/idle";
32 
33 // Files for writeback
34 const ZRAM_BACKING_DEV_PATH: &str = "/sys/block/zram0/backing_dev";
35 const ZRAM_WRITEBACK_PATH: &str = "/sys/block/zram0/writeback";
36 const ZRAM_WRITEBACK_LIMIT_ENABLE_PATH: &str = "/sys/block/zram0/writeback_limit_enable";
37 const ZRAM_WRITEBACK_LIMIT_PATH: &str = "/sys/block/zram0/writeback_limit";
38 const ZRAM_BD_STAT_PATH: &str = "/sys/block/zram0/bd_stat";
39 
40 // Files for recompression
41 const ZRAM_RECOMP_ALGORITHM_PATH: &str = "/sys/block/zram0/recomp_algorithm";
42 const ZRAM_RECOMPRESS_PATH: &str = "/sys/block/zram0/recompress";
43 
44 /// [SysfsZramApi] is a mockable interface for access to files under
45 /// "/sys/block/zram0" which is system global.
46 ///
47 /// The naming convention: functions for files which is readable and writable
48 ///
49 /// * fn read_<file_name>() -> io::Result<String>
50 /// * fn write_<file_name>(contents: &str) -> io::Result<()>
51 ///
52 /// We don't have naming conventions for files which is writable only.
53 #[cfg_attr(any(test, feature = "test_utils"), mockall::automock)]
54 pub trait SysfsZramApi {
55     /// Read "/sys/block/zram0/disksize".
read_disksize() -> io::Result<String>56     fn read_disksize() -> io::Result<String>;
57     /// Write "/sys/block/zram0/disksize".
write_disksize(contents: &str) -> io::Result<()>58     fn write_disksize(contents: &str) -> io::Result<()>;
59     /// Read "/sys/block/zram0/mm_stat".
read_mm_stat() -> io::Result<String>60     fn read_mm_stat() -> io::Result<String>;
61 
62     /// Set compression algorithm.
write_comp_algorithm(contents: &str) -> io::Result<()>63     fn write_comp_algorithm(contents: &str) -> io::Result<()>;
64 
65     /// Write contents to "/sys/block/zram0/idle".
set_idle(contents: &str) -> io::Result<()>66     fn set_idle(contents: &str) -> io::Result<()>;
67 
68     /// Read "/sys/block/zram0/backing_dev".
read_backing_dev() -> io::Result<String>69     fn read_backing_dev() -> io::Result<String>;
70     /// Write "/sys/block/zram0/backing_dev".
write_backing_dev(contents: &str) -> io::Result<()>71     fn write_backing_dev(contents: &str) -> io::Result<()>;
72     /// Write contents to "/sys/block/zram0/writeback".
writeback(contents: &str) -> io::Result<()>73     fn writeback(contents: &str) -> io::Result<()>;
74     /// Write contents to "/sys/block/zram0/writeback_limit_enable".
write_writeback_limit_enable(contents: &str) -> io::Result<()>75     fn write_writeback_limit_enable(contents: &str) -> io::Result<()>;
76     /// Write contents to "/sys/block/zram0/writeback_limit".
write_writeback_limit(contents: &str) -> io::Result<()>77     fn write_writeback_limit(contents: &str) -> io::Result<()>;
78     /// Read "/sys/block/zram0/writeback_limit".
read_writeback_limit() -> io::Result<String>79     fn read_writeback_limit() -> io::Result<String>;
80     /// Read "/sys/block/zram0/bd_stat".
read_bd_stat() -> io::Result<String>81     fn read_bd_stat() -> io::Result<String>;
82 
83     /// Read "/sys/block/zram0/recomp_algorithm".
read_recomp_algorithm() -> io::Result<String>84     fn read_recomp_algorithm() -> io::Result<String>;
85     /// Write "/sys/block/zram0/recomp_algorithm".
write_recomp_algorithm(contents: &str) -> io::Result<()>86     fn write_recomp_algorithm(contents: &str) -> io::Result<()>;
87     /// Write contents to "/sys/block/zram0/recompress".
recompress(contents: &str) -> io::Result<()>88     fn recompress(contents: &str) -> io::Result<()>;
89 }
90 
91 /// The implementation of [SysfsZramApi].
92 pub struct SysfsZramApiImpl;
93 
94 impl SysfsZramApi for SysfsZramApiImpl {
read_disksize() -> io::Result<String>95     fn read_disksize() -> io::Result<String> {
96         std::fs::read_to_string(ZRAM_DISKSIZE_PATH)
97     }
98 
write_disksize(contents: &str) -> io::Result<()>99     fn write_disksize(contents: &str) -> io::Result<()> {
100         std::fs::write(ZRAM_DISKSIZE_PATH, contents)
101     }
102 
read_mm_stat() -> io::Result<String>103     fn read_mm_stat() -> io::Result<String> {
104         std::fs::read_to_string(ZRAM_MM_STAT_PATH)
105     }
106 
set_idle(contents: &str) -> io::Result<()>107     fn set_idle(contents: &str) -> io::Result<()> {
108         std::fs::write(ZRAM_IDLE_PATH, contents)
109     }
110 
read_backing_dev() -> io::Result<String>111     fn read_backing_dev() -> io::Result<String> {
112         std::fs::read_to_string(ZRAM_BACKING_DEV_PATH)
113     }
114 
write_backing_dev(contents: &str) -> io::Result<()>115     fn write_backing_dev(contents: &str) -> io::Result<()> {
116         std::fs::write(ZRAM_BACKING_DEV_PATH, contents)
117     }
118 
writeback(contents: &str) -> io::Result<()>119     fn writeback(contents: &str) -> io::Result<()> {
120         std::fs::write(ZRAM_WRITEBACK_PATH, contents)
121     }
122 
write_writeback_limit(contents: &str) -> io::Result<()>123     fn write_writeback_limit(contents: &str) -> io::Result<()> {
124         std::fs::write(ZRAM_WRITEBACK_LIMIT_PATH, contents)
125     }
126 
write_writeback_limit_enable(contents: &str) -> io::Result<()>127     fn write_writeback_limit_enable(contents: &str) -> io::Result<()> {
128         std::fs::write(ZRAM_WRITEBACK_LIMIT_ENABLE_PATH, contents)
129     }
130 
read_writeback_limit() -> io::Result<String>131     fn read_writeback_limit() -> io::Result<String> {
132         std::fs::read_to_string(ZRAM_WRITEBACK_LIMIT_PATH)
133     }
134 
read_bd_stat() -> io::Result<String>135     fn read_bd_stat() -> io::Result<String> {
136         std::fs::read_to_string(ZRAM_BD_STAT_PATH)
137     }
138 
read_recomp_algorithm() -> io::Result<String>139     fn read_recomp_algorithm() -> io::Result<String> {
140         std::fs::read_to_string(ZRAM_RECOMP_ALGORITHM_PATH)
141     }
142 
write_recomp_algorithm(contents: &str) -> io::Result<()>143     fn write_recomp_algorithm(contents: &str) -> io::Result<()> {
144         std::fs::write(ZRAM_RECOMP_ALGORITHM_PATH, contents)
145     }
146 
recompress(contents: &str) -> io::Result<()>147     fn recompress(contents: &str) -> io::Result<()> {
148         std::fs::write(ZRAM_RECOMPRESS_PATH, contents)
149     }
150 
write_comp_algorithm(contents: &str) -> io::Result<()>151     fn write_comp_algorithm(contents: &str) -> io::Result<()> {
152         std::fs::write(ZRAM_COMP_ALGORITHM_PATH, contents)
153     }
154 }
155 
156 /// Mutex to synchronize tests using [MockSysfsZramApi].
157 ///
158 /// mockall for static functions requires synchronization.
159 ///
160 /// https://docs.rs/mockall/latest/mockall/#static-methods
161 pub static ZRAM_API_MTX: std::sync::Mutex<()> = std::sync::Mutex::new(());
162