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 os layer utilities.
16
17 use std::io;
18 use std::os::fd::AsRawFd;
19
20 use nix::unistd::sysconf;
21 use nix::unistd::SysconfVar;
22
23 const MEMINFO_PATH: &str = "/proc/meminfo";
24
25 /// [MeminfoApi] is a mockable interface for access to "/proc/meminfo".
26 #[cfg_attr(test, mockall::automock)]
27 pub trait MeminfoApi {
28 /// read "/proc/meminfo".
read_meminfo() -> io::Result<String>29 fn read_meminfo() -> io::Result<String>;
30 }
31
32 /// The implementation of [MeminfoApi].
33 pub struct MeminfoApiImpl;
34
35 impl MeminfoApi for MeminfoApiImpl {
read_meminfo() -> io::Result<String>36 fn read_meminfo() -> io::Result<String> {
37 std::fs::read_to_string(MEMINFO_PATH)
38 }
39 }
40
41 /// Mutex to synchronize tests using [MeminfoApi].
42 ///
43 /// mockall for static functions requires synchronization.
44 ///
45 /// https://docs.rs/mockall/latest/mockall/#static-methods
46 #[cfg(test)]
47 pub static MEMINFO_API_MTX: std::sync::Mutex<()> = std::sync::Mutex::new(());
48
49 /// Returns the page size of the system.
get_page_size() -> u6450 pub fn get_page_size() -> u64 {
51 // SAFETY: `sysconf` simply returns an integer.
52 unsafe { libc::sysconf(libc::_SC_PAGESIZE) as u64 }
53 }
54
55 /// Returns the page count of the system.
get_page_count() -> u6456 pub fn get_page_count() -> u64 {
57 sysconf(SysconfVar::_PHYS_PAGES)
58 .expect("PHYS_PAGES should be a valid sysconf variable")
59 .expect("PHYS_PAGES variable should be supported") as u64
60 }
61
62 /// Allocates file with the specified size.
63 /// Similar function nix::fcntl::fallocate is available but it was not compiled on android.
64 /// TODO: b/388993276 - Replace this with nix::fcntl::fallocate.
fallocate<F: AsRawFd>(file: &F, size: u64) -> std::io::Result<()>65 pub fn fallocate<F: AsRawFd>(file: &F, size: u64) -> std::io::Result<()> {
66 let len = if size > libc::off64_t::MAX as u64 {
67 return Err(io::Error::new(
68 io::ErrorKind::InvalidInput,
69 format!("File size too large: should be less than {}", libc::off64_t::MAX),
70 ));
71 } else {
72 size as libc::off64_t
73 };
74
75 // SAFETY: fd should be valid; fallocate mode and offset are harcoded valid values; len was validated
76 let res = unsafe { libc::fallocate64(file.as_raw_fd(), 0, 0, len) };
77 if res < 0 {
78 return Err(std::io::Error::last_os_error());
79 }
80 Ok(())
81 }
82