From e0e3c4c6ae15af88ac5fd5ab959adfe45a2e1dca Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 18 Jun 2024 21:52:55 +0200 Subject: [PATCH] compositor: bump nofile rlimits on launch ref #6584 --- src/Compositor.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 2f972335..af46b0ff 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -29,6 +29,7 @@ using namespace Hyprutils::String; #include #include +#include int handleCritSignal(int signo, void* data) { Debug::log(LOG, "Hyprland received signal {}", signo); @@ -70,6 +71,46 @@ void handleUserSignal(int sig) { } } +static void bumpNofile() { + unsigned long limit = 1024; + + try { + std::ifstream f("/proc/sys/fs/nr_open"); + if (!f.good()) + limit = 1073741816; + else { + std::string content((std::istreambuf_iterator(f)), (std::istreambuf_iterator())); + f.close(); + + limit = std::stoll(content); + } + + } catch (...) { limit = 1073741816; } + + struct rlimit rlimit_; + if (!getrlimit(RLIMIT_NOFILE, &rlimit_)) + Debug::log(LOG, "Old rlimit: soft -> {}, hard -> {}", rlimit_.rlim_cur, rlimit_.rlim_max); + + if (rlimit_.rlim_max <= 1024) + rlimit_.rlim_max = limit; + + unsigned long oldHardLimit = rlimit_.rlim_max; + + rlimit_.rlim_max = limit; + + if (setrlimit(RLIMIT_NOFILE, &rlimit_) < 0) { + Debug::log(LOG, "Failed bumping NOFILE limits higher, retrying with previous hard."); + rlimit_.rlim_max = oldHardLimit; + rlimit_.rlim_cur = std::clamp((unsigned long)limit, 1UL, (unsigned long)rlimit_.rlim_max); + + if (setrlimit(RLIMIT_NOFILE, &rlimit_) < 0) + Debug::log(LOG, "Failed bumping NOFILE limits higher for the second time."); + } + + if (!getrlimit(RLIMIT_NOFILE, &rlimit_)) + Debug::log(LOG, "New rlimit: soft -> {}, hard -> {}", rlimit_.rlim_cur, rlimit_.rlim_max); +} + CCompositor::CCompositor() { m_iHyprlandPID = getpid(); @@ -131,6 +172,8 @@ CCompositor::CCompositor() { setRandomSplash(); Debug::log(LOG, "\nCurrent splash: {}\n\n", m_szCurrentSplash); + + bumpNofile(); } CCompositor::~CCompositor() {