ClickHouse/src/Common/getMaxFileDescriptorCount.cpp

37 lines
775 B
C++
Raw Normal View History

2021-11-12 12:48:42 +00:00
#include <IO/ReadHelpers.h>
2021-10-27 12:26:42 +00:00
#include <IO/WriteBufferFromString.h>
2021-11-19 09:30:58 +00:00
#include <IO/ReadBufferFromFile.h>
2021-11-12 12:48:42 +00:00
#include <Common/ShellCommand.h>
#include <Common/getMaxFileDescriptorCount.h>
2021-11-19 09:30:58 +00:00
#include <filesystem>
2021-10-27 12:26:42 +00:00
int getMaxFileDescriptorCount()
{
2021-11-19 09:30:58 +00:00
namespace fs = std::filesystem;
2021-11-17 12:44:07 +00:00
int result = -1;
#if defined(OS_LINUX) || defined(OS_DARWIN)
2021-10-27 12:26:42 +00:00
using namespace DB;
2021-11-19 09:30:58 +00:00
if (fs::exists("/proc/sys/fs/file-max"))
2021-11-12 12:48:42 +00:00
{
2021-11-19 09:30:58 +00:00
ReadBufferFromFile reader("/proc/sys/fs/file-max");
readIntText(result, reader);
2021-11-12 12:48:42 +00:00
}
2021-11-19 09:30:58 +00:00
else
2021-11-12 12:48:42 +00:00
{
2021-11-19 09:30:58 +00:00
auto command = ShellCommand::execute("ulimit -n");
try
{
readIntText(result, command->out);
command->wait();
}
catch (...)
{
}
2021-11-12 12:48:42 +00:00
}
2021-10-27 12:26:42 +00:00
#endif
2021-11-17 12:44:07 +00:00
return result;
2021-10-27 12:26:42 +00:00
}