summaryrefslogtreecommitdiffstats
path: root/src/drop_caches.rs
blob: ad0765964568d6fce7b5274d05511794a9226279 (plain)
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
// -*- coding: utf-8 -*-
//
// disktest - Hard drive tester
//
// Copyright 2020 Michael Buesch <m@bues.ch>
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

use anyhow as ah;
use std::fs::File;
use std::path::Path;

#[cfg(target_os="linux")]
fn os_drop_file_caches(file: File,
                       _path: &Path,
                       offset: u64,
                       size: u64) -> ah::Result<()> {
    use libc::{posix_fadvise, POSIX_FADV_DONTNEED, off_t};
    use std::fs::OpenOptions;
    use std::io::Write;
    use std::os::unix::io::AsRawFd;

    // Try FADV_DONTNEED to drop caches.
    file.sync_all().ok();
    let ret = unsafe { posix_fadvise(file.as_raw_fd(),
                                     offset as off_t,
                                     size as off_t,
                                     POSIX_FADV_DONTNEED) };
    if ret == 0 {
        // fadvise success.
        Ok(())
    } else {
        // Try global drop_caches.

        drop(file);

        let proc_file = "/proc/sys/vm/drop_caches";
        let proc_value = "3\n";

        match OpenOptions::new().write(true).open(proc_file) {
            Ok(mut file) => {
                match file.write_all(proc_value.as_bytes()) {
                    Ok(_) => Ok(()),
                    Err(e) => Err(ah::format_err!("{}", e)),
                }
            },
            Err(e) => Err(ah::format_err!("{}", e)),
        }
    }
}

#[cfg(target_os="windows")]
fn os_drop_file_caches(file: File,
                       path: &Path,
                       _offset: u64,
                       _size: u64) -> ah::Result<()> {
    use winapi::um::{
        fileapi::{CreateFileA, OPEN_EXISTING},
        handleapi::{CloseHandle, INVALID_HANDLE_VALUE},
        winbase::FILE_FLAG_NO_BUFFERING,
        winnt::{GENERIC_READ, FILE_SHARE_READ},
    };
    use std::{
        ptr::null_mut,
        ffi::CString,
    };

    // Close the file before re-opening it.
    file.sync_all().ok();
    drop(file);

    // Open the file with FILE_FLAG_NO_BUFFERING.
    // That drops all caches.
    if let Some(path) = path.to_str() {
        if let Ok(path) = CString::new(path) {
            let h = unsafe { CreateFileA(path.as_ptr(),
                                         GENERIC_READ,
                                         FILE_SHARE_READ,
                                         null_mut(),
                                         OPEN_EXISTING,
                                         FILE_FLAG_NO_BUFFERING,
                                         null_mut()) };
            if h == INVALID_HANDLE_VALUE {
                Err(ah::format_err!("Failed to acquire file handle."))
            } else {
                unsafe { CloseHandle(h) };
                Ok(())
            }
        } else {
            Err(ah::format_err!("Failed to convert file name (CString)."))
        }
    } else {
        Err(ah::format_err!("Failed to convert file name (str)."))
    }
}

#[cfg(not(any(target_os="linux", target_os="windows")))]
fn os_drop_file_caches(_file: File,
                       _path: &Path,
                       _offset: u64,
                       _size: u64) -> ah::Result<()> {
    Err(ah::format_err!("Not supported on this operating system."))
}

/// Consume a file object, close it and try to drop all operating system caches.
pub fn drop_file_caches(file: File,
                        path: &Path,
                        offset: u64,
                        size: u64) -> ah::Result<()> {
    os_drop_file_caches(file, path, offset, size)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;
    use std::fs::File;
    use std::io::Write;

    #[test]
    fn test_drop_file_caches() {
        let tdir = tempdir().unwrap();
        let path = tdir.path().join("test_drop_file_caches");
        let mut file = File::create(&path).unwrap();
        file.write_all(&[42u8; 4096]).unwrap();
        drop_file_caches(file, &path, 0, 4096).unwrap();
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface