Skip to content

Commit 3c2edde

Browse files
committed
increase limit on open files on Linux
Change-Id: I0ce933e46b94d10b1e4926f2fe1f4d8472e5f955
1 parent 4309a0d commit 3c2edde

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/cpucounters.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#ifdef __linux__
5858
#include <sys/mman.h>
5959
#include <dirent.h>
60+
#include <sys/resource.h>
6061
#endif
6162
#endif
6263

@@ -2059,6 +2060,30 @@ std::ofstream* PCM::outfile = nullptr; // output file stream
20592060
std::streambuf* PCM::backup_ofile = nullptr; // backup of original output = cout
20602061
std::streambuf* PCM::backup_ofile_cerr = nullptr; // backup of original output = cerr
20612062

2063+
#ifdef __linux__
2064+
void increaseULimit()
2065+
{
2066+
rlimit lim{};
2067+
if (getrlimit(RLIMIT_NOFILE, &lim) == 0)
2068+
{
2069+
const rlim_t recommendedLimit = 1000000;
2070+
// std::cout << "file open limit: " << lim.rlim_cur << "," << lim.rlim_max << "\n";
2071+
if (lim.rlim_cur < recommendedLimit || lim.rlim_max < recommendedLimit)
2072+
{
2073+
lim.rlim_cur = lim.rlim_max = recommendedLimit;
2074+
if (setrlimit(RLIMIT_NOFILE, &lim) != 0)
2075+
{
2076+
std::cerr << "PCM Info: setrlimit for file limit " << recommendedLimit << " failed with error " << strerror(errno) << "\n";
2077+
}
2078+
}
2079+
}
2080+
else
2081+
{
2082+
std::cerr << "PCM Info: getrlimit for file limit failed with error " << strerror(errno) << "\n";
2083+
}
2084+
}
2085+
#endif
2086+
20622087
PCM::PCM() :
20632088
cpu_family(-1),
20642089
cpu_model(-1),
@@ -2115,6 +2140,9 @@ PCM::PCM() :
21152140
run_state(1),
21162141
needToRestoreNMIWatchdog(false)
21172142
{
2143+
#ifdef __linux__
2144+
increaseULimit();
2145+
#endif
21182146
#ifdef _MSC_VER
21192147
// WARNING: This driver code (msr.sys) is only for testing purposes, not for production use
21202148
Driver drv(Driver::msrLocalPath());

0 commit comments

Comments
 (0)