Skip to content

Commit

Permalink
Support reclaim with swappiness
Browse files Browse the repository at this point in the history
Summary:
https://lwn.net/Articles/956742/
We can now reclaim via:
```
echo 10M swappiness=0 > /sys/fs/cgroup/system.slice
```
Available on 6.4+ kernels

Reviewed By: antonis-m

Differential Revision: D57162912

fbshipit-source-id: d9ad3cd03f979b53a9f86e5349be9e036db79ee3
  • Loading branch information
lnyng authored and facebook-github-bot committed May 10, 2024
1 parent b2375be commit f78bcba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/oomd/util/Fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,14 @@ SystemMaybe<Unit> Fs::writeMemhightmpAt(
return noSystemError();
}

SystemMaybe<Unit> Fs::writeMemReclaimAt(const DirFd& dirfd, int64_t value) {
SystemMaybe<Unit> Fs::writeMemReclaimAt(
const DirFd& dirfd,
int64_t value,
std::optional<int64_t> swappiness) {
auto val_str = std::to_string(value);
if (swappiness) {
val_str += " swappiness=" + std::to_string(*swappiness);
}
auto ret = writeControlFileAt(
Fs::Fd::openat(dirfd, kMemReclaimFile, false), val_str);
if (!ret) {
Expand Down
5 changes: 4 additions & 1 deletion src/oomd/util/Fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ class Fs {
const DirFd& dirfd,
int64_t value,
std::chrono::microseconds duration);
static SystemMaybe<Unit> writeMemReclaimAt(const DirFd& dirfd, int64_t value);
static SystemMaybe<Unit> writeMemReclaimAt(
const DirFd& dirfd,
int64_t value,
std::optional<int64_t> swappiness);

static SystemMaybe<int64_t> getNrDyingDescendantsAt(const DirFd& dirfd);
static SystemMaybe<KillPreference> readKillPreferenceAt(const DirFd& path);
Expand Down
14 changes: 13 additions & 1 deletion src/oomd/util/FsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,24 @@ TEST_F(FsTest, WriteMemoryReclaim) {
F::materialize(F::makeDir(path, {F::makeFile("memory.reclaim")}));

auto dir = ASSERT_SYS_OK(Fs::DirFd::open(path));
ASSERT_SYS_OK(Fs::writeMemReclaimAt(dir, 54321));
ASSERT_SYS_OK(Fs::writeMemReclaimAt(dir, 54321, std::nullopt));
auto lines = ASSERT_SYS_OK(
Fs::readFileByLine(Fs::Fd::openat(dir, Fs::kMemReclaimFile)));
EXPECT_EQ(lines, std::vector{std::string("54321")});
}

TEST_F(FsTest, WriteMemoryReclaimWithSwappiness) {
using F = Fixture;
auto path = fixture_.cgroupDataDir() + "/write_test";
F::materialize(F::makeDir(path, {F::makeFile("memory.reclaim")}));

auto dir = ASSERT_SYS_OK(Fs::DirFd::open(path));
ASSERT_SYS_OK(Fs::writeMemReclaimAt(dir, 54321, 0));
auto lines = ASSERT_SYS_OK(
Fs::readFileByLine(Fs::Fd::openat(dir, Fs::kMemReclaimFile)));
EXPECT_EQ(lines, std::vector{std::string("54321 swappiness=0")});
}

TEST_F(FsTest, Swappiness) {
using F = Fixture;
auto path = fixture_.fsDataDir() + "/swappiness";
Expand Down

0 comments on commit f78bcba

Please sign in to comment.