Files
perfect/include/perfect/drop_caches.hpp
Carl Pearson d6c861719f Squashed commit of the following:
commit b96ddedf4ffbba57faaaf8bf18781a7abfb9d4c1
Author: Carl Pearson <pearson@illinois.edu>
Date:   Mon Sep 23 16:53:54 2019 -0500

    add newline to result.hpp

commit c7e9f6ff4775bf86f9af216cbe311f65bf985f63
Author: Carl Pearson <pearson@illinois.edu>
Date:   Mon Sep 23 16:53:09 2019 -0500

    add EPERM to fs operations

commit bac918fd022006cad0da899c06ac31e9db59a2fb
Author: Carl Pearson <pearson@illinois.edu>
Date:   Mon Sep 23 16:49:25 2019 -0500

    add filesystem cache interface
2019-09-23 16:54:33 -05:00

43 lines
952 B
C++

#pragma once
#include <unistd.h>
#include <fstream>
#include <iostream>
#include "result.hpp"
#include "init.hpp"
#include "detail/fs.hpp"
namespace perfect {
enum DropCaches_t {
PAGECACHE = 0x1,
ENTRIES = 0x2
};
// commit filesystem caches to disk
Result sync() {
// http://man7.org/linux/man-pages/man2/sync.2.html
::sync(); // always successful
return Result::SUCCESS;
}
Result drop_caches(const DropCaches_t mode) {
using detail::write_str;
const std::string path = "/proc/sys/vm/drop_caches";
if (mode & PAGECACHE & ENTRIES) {
PERFECT_SUCCESS_OR_RETURN(write_str(path, "3"));
} else if (mode & PAGECACHE) {
PERFECT_SUCCESS_OR_RETURN(write_str(path, "1"));
} else if (mode & ENTRIES) {
PERFECT_SUCCESS_OR_RETURN(write_str(path, "2"));
} else {
std::cerr << "unexpected mode: " << mode << "\n";
return Result::UNKNOWN;
}
return Result::SUCCESS;
}
}