Skip to content

Commit

Permalink
compositor: bump nofile rlimits on launch
Browse files Browse the repository at this point in the history
ref #6584
  • Loading branch information
vaxerski committed Jun 18, 2024
1 parent b98e087 commit e0e3c4c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using namespace Hyprutils::String;

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>

int handleCritSignal(int signo, void* data) {
Debug::log(LOG, "Hyprland received signal {}", signo);
Expand Down Expand Up @@ -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<char>(f)), (std::istreambuf_iterator<char>()));
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();

Expand Down Expand Up @@ -131,6 +172,8 @@ CCompositor::CCompositor() {
setRandomSplash();

Debug::log(LOG, "\nCurrent splash: {}\n\n", m_szCurrentSplash);

bumpNofile();
}

CCompositor::~CCompositor() {
Expand Down

0 comments on commit e0e3c4c

Please sign in to comment.