diff --git a/src/mergerfs.cpp b/src/mergerfs.cpp index d66e88d4..301305b7 100644 --- a/src/mergerfs.cpp +++ b/src/mergerfs.cpp @@ -23,13 +23,14 @@ #include "fs_umount2.hpp" #include "fs_wait_for_mount.hpp" #include "gidcache.hpp" +#include "maintenance_thread.hpp" +#include "oom.hpp" #include "option_parser.hpp" #include "procfs.hpp" #include "resources.hpp" #include "strvec.hpp" #include "syslog.hpp" #include "version.hpp" -#include "maintenance_thread.hpp" #include "fuse_access.hpp" #include "fuse_bmap.hpp" @@ -164,6 +165,27 @@ namespace l resources::setpriority(scheduling_priority_); } + static + void + set_oom_score_adj() + { + int rv; + int orig; + int score; + + if(!oom::has_oom_score_adj()) + return; + + score = -990; + + orig = oom::get_oom_score_adj(); + rv = oom::set_oom_score_adj(score); + + SysLog::info("set oom_score_adj to {}, originally {}", + score, + orig); + } + static bool wait_for_mount(const Config::Read &cfg_) @@ -318,6 +340,7 @@ namespace l }); l::setup_resources(cfg->scheduling_priority); l::setup_signal_handlers(); + l::set_oom_score_adj(); l::get_fuse_operations(ops,cfg->nullrw); if(cfg->lazy_umount_mountpoint) diff --git a/src/oom.cpp b/src/oom.cpp new file mode 100644 index 00000000..0cfaf730 --- /dev/null +++ b/src/oom.cpp @@ -0,0 +1,68 @@ +/* + ISC License + + Copyright (c) 2025, Antonio SJ Musumeci + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "oom.hpp" + +#include "fs_exists.hpp" + +#include "fmt/core.h" + +#include +#include + + +constexpr char SELF_OOM_SCORE_ADJ[] = "/proc/self/oom_score_adj"; + +bool +oom::has_oom_score_adj() +{ + return fs::exists(SELF_OOM_SCORE_ADJ); +} + +int +oom::get_oom_score_adj() +{ + int score; + std::ifstream ifs; + + ifs.open(SELF_OOM_SCORE_ADJ); + if(!ifs) + return 0; + + ifs >> score; + + ifs.close(); + + return score; +} + +int +oom::set_oom_score_adj(const int score_) +{ + std::ofstream ofs; + + ofs.open(SELF_OOM_SCORE_ADJ); + if(!ofs) + return -errno; + + ofs << fmt::format("{}",score_); + + ofs.close(); + + return 0; +} diff --git a/src/oom.hpp b/src/oom.hpp new file mode 100644 index 00000000..2410a9de --- /dev/null +++ b/src/oom.hpp @@ -0,0 +1,27 @@ +/* + ISC License + + Copyright (c) 2025, Antonio SJ Musumeci + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#pragma once + + +namespace oom +{ + bool has_oom_score_adj(); + int get_oom_score_adj(); + int set_oom_score_adj(const int score); +};