Call getMaxFileDescriptorCount once

This commit is contained in:
Antonio Andelic 2024-01-18 09:11:50 +00:00
parent f349a7d0c0
commit 19ca00f233
4 changed files with 27 additions and 35 deletions

View File

@ -1,36 +1,19 @@
#include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromString.h>
#include <IO/ReadBufferFromFile.h>
#include <Common/ShellCommand.h>
#include <Common/getMaxFileDescriptorCount.h>
#include <filesystem>
#include <sys/resource.h>
int getMaxFileDescriptorCount()
std::optional<size_t> getMaxFileDescriptorCount()
{
namespace fs = std::filesystem;
int result = -1;
#if defined(OS_LINUX) || defined(OS_DARWIN)
using namespace DB;
if (fs::exists("/proc/sys/fs/file-max"))
static auto result = []() -> std::optional<size_t>
{
ReadBufferFromFile reader("/proc/sys/fs/file-max");
readIntText(result, reader);
}
else
{
auto command = ShellCommand::execute("ulimit -n");
try
{
readIntText(result, command->out);
command->wait();
}
catch (...) // NOLINT(bugprone-empty-catch)
{
}
}
#endif
#if defined(OS_LINUX) || defined(OS_DARWIN)
rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim))
return std::nullopt;
return rlim.rlim_max;
#else
return std::nullopt;
#endif
}();
return result;
}

View File

@ -1,6 +1,8 @@
#pragma once
/// Get process max file descriptor count
/// @return -1 if os does not support ulimit command or some error occurs
int getMaxFileDescriptorCount();
#include <optional>
/// Get process max file descriptor count
/// @return std::nullopt if os does not support getrlimit command or some error occurs
std::optional<size_t> getMaxFileDescriptorCount();

View File

@ -300,7 +300,11 @@ String MonitorCommand::run()
#if defined(OS_LINUX) || defined(OS_DARWIN)
print(ret, "open_file_descriptor_count", getCurrentProcessFDCount());
print(ret, "max_file_descriptor_count", getMaxFileDescriptorCount());
auto max_file_descriptor_count = getMaxFileDescriptorCount();
if (max_file_descriptor_count.has_value())
print(ret, "max_file_descriptor_count", *max_file_descriptor_count);
else
print(ret, "max_file_descriptor_count", -1);
#endif
if (keeper_info.is_leader)

View File

@ -22,7 +22,7 @@ void updateKeeperInformation(KeeperDispatcher & keeper_dispatcher, AsynchronousM
size_t key_arena_size = 0;
size_t latest_snapshot_size = 0;
size_t open_file_descriptor_count = 0;
size_t max_file_descriptor_count = 0;
std::optional<size_t> max_file_descriptor_count = 0;
size_t followers = 0;
size_t synced_followers = 0;
size_t zxid = 0;
@ -78,7 +78,10 @@ void updateKeeperInformation(KeeperDispatcher & keeper_dispatcher, AsynchronousM
new_values["KeeperLatestSnapshotSize"] = { latest_snapshot_size, "The uncompressed size in bytes of the latest snapshot created by ClickHouse Keeper." };
new_values["KeeperOpenFileDescriptorCount"] = { open_file_descriptor_count, "The number of open file descriptors in ClickHouse Keeper." };
new_values["KeeperMaxFileDescriptorCount"] = { max_file_descriptor_count, "The maximum number of open file descriptors in ClickHouse Keeper." };
if (max_file_descriptor_count.has_value())
new_values["KeeperMaxFileDescriptorCount"] = { *max_file_descriptor_count, "The maximum number of open file descriptors in ClickHouse Keeper." };
else
new_values["KeeperMaxFileDescriptorCount"] = { -1, "The maximum number of open file descriptors in ClickHouse Keeper." };
new_values["KeeperFollowers"] = { followers, "The number of followers of ClickHouse Keeper." };
new_values["KeeperSyncedFollowers"] = { synced_followers, "The number of followers of ClickHouse Keeper who are also in-sync." };