Browse Source

Add setting of oom_score_adj (#1504)

To limit risk of killing by OOM Killer.
pull/1505/head
trapexit 2 months ago
committed by GitHub
parent
commit
4439c2a030
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 25
      src/mergerfs.cpp
  2. 68
      src/oom.cpp
  3. 27
      src/oom.hpp

25
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)

68
src/oom.cpp

@ -0,0 +1,68 @@
/*
ISC License
Copyright (c) 2025, Antonio SJ Musumeci <trapexit@spawn.link>
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 <fstream>
#include <tuple>
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;
}

27
src/oom.hpp

@ -0,0 +1,27 @@
/*
ISC License
Copyright (c) 2025, Antonio SJ Musumeci <trapexit@spawn.link>
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);
};
Loading…
Cancel
Save