Extract common

This commit is contained in:
Antonio Andelic 2024-07-28 11:51:09 +02:00
parent 6f2d25cc39
commit a9b947d9ac
6 changed files with 64 additions and 58 deletions

View File

@ -32,6 +32,7 @@ set (SRCS
StringRef.cpp
safeExit.cpp
throwError.cpp
Numa.cpp
)
add_library (common ${SRCS})

37
base/base/Numa.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <base/Numa.h>
#include "config.h"
#if USE_NUMACTL
# include <numa.h>
#endif
namespace DB
{
std::optional<size_t> getNumaNodesTotalMemory()
{
std::optional<size_t> total_memory;
#if USE_NUMACTL
if (numa_available() != -1)
{
auto * membind = numa_get_membind();
if (!numa_bitmask_equal(membind, numa_all_nodes_ptr))
{
total_memory.emplace(0);
auto max_node = numa_max_node();
for (int i = 0; i <= max_node; ++i)
{
if (numa_bitmask_isbitset(membind, i))
*total_memory += numa_node_size(i, nullptr);
}
}
numa_bitmask_free(membind);
}
#endif
return total_memory;
}
}

12
base/base/Numa.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <optional>
namespace DB
{
/// return total memory of NUMA nodes the process is bound to
/// if NUMA is not supported or process can use all nodes, std::nullopt is returned
std::optional<size_t> getNumaNodesTotalMemory();
}

View File

@ -2,6 +2,7 @@
#include <base/cgroupsv2.h>
#include <base/getPageSize.h>
#include <base/Numa.h>
#include <fstream>
@ -9,13 +10,6 @@
#include <sys/types.h>
#include <sys/param.h>
#include "config.h"
#if USE_NUMACTL
#include <numa.h>
#endif
namespace
{
@ -68,25 +62,8 @@ uint64_t getMemoryAmountOrZero()
uint64_t memory_amount = num_pages * page_size;
#if USE_NUMACTL
if (numa_available() != -1)
{
auto * membind = numa_get_membind();
if (!numa_bitmask_equal(membind, numa_all_nodes_ptr))
{
uint64_t total_numa_memory = 0;
auto max_node = numa_max_node();
for (int i = 0; i <= max_node; ++i)
{
if (numa_bitmask_isbitset(membind, i))
total_numa_memory += numa_node_size(i, nullptr);
}
memory_amount = total_numa_memory;
}
numa_bitmask_free(membind);
}
#endif
if (auto total_numa_memory = DB::getNumaNodesTotalMemory(); total_numa_memory.has_value())
memory_amount = *total_numa_memory;
/// Respect the memory limit set by cgroups v2.
auto limit_v2 = getCgroupsV2MemoryLimit();

View File

@ -19,6 +19,7 @@
#include <base/getMemoryAmount.h>
#include <base/scope_guard.h>
#include <base/safeExit.h>
#include <base/Numa.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/TCPServerParams.h>
#include <Poco/Net/TCPServer.h>
@ -311,6 +312,12 @@ try
MainThreadStatus::getInstance();
if (auto total_numa_memory = getNumaNodesTotalMemory(); total_numa_memory.has_value())
{
LOG_INFO(
log, "ClickHouse is bound to a subset of NUMA nodes. Total memory of all available nodes: {}", ReadableSize(*total_numa_memory));
}
#if !defined(NDEBUG) || !defined(__OPTIMIZE__)
LOG_WARNING(log, "Keeper was built in debug mode. It will work slowly.");
#endif

View File

@ -22,6 +22,7 @@
#include <base/coverage.h>
#include <base/getFQDNOrHostName.h>
#include <base/safeExit.h>
#include <base/Numa.h>
#include <Common/PoolId.h>
#include <Common/MemoryTracker.h>
#include <Common/ClickHouseRevision.h>
@ -140,10 +141,6 @@
# include <azure/core/diagnostics/logger.hpp>
#endif
#if USE_NUMACTL
#include <numa.h>
#endif
#include <incbin.h>
/// A minimal file used when the server is run without installation
@ -759,36 +756,11 @@ try
setenv("OPENSSL_CONF", config_dir.c_str(), true); /// NOLINT
}
#if USE_NUMACTL
if (numa_available() != -1)
if (auto total_numa_memory = getNumaNodesTotalMemory(); total_numa_memory.has_value())
{
auto * membind = numa_get_membind();
if (!numa_bitmask_equal(membind, numa_all_nodes_ptr))
{
uint64_t total_numa_memory = 0;
auto max_node = numa_max_node();
for (int i = 0; i <= max_node; ++i)
{
if (numa_bitmask_isbitset(membind, i))
total_numa_memory += numa_node_size(i, nullptr);
}
LOG_INFO(
log,
"ClickHouse is bound to a subset of NUMA nodes. Total memory of all available nodes: {}",
ReadableSize(total_numa_memory));
}
else
{
LOG_TRACE(
log,
"All NUMA nodes are used. Detected NUMA nodes: {}",
numa_num_configured_nodes());
}
numa_bitmask_free(membind);
LOG_INFO(
log, "ClickHouse is bound to a subset of NUMA nodes. Total memory of all available nodes: {}", ReadableSize(*total_numa_memory));
}
#endif
registerInterpreters();
registerFunctions();