From 0633962e19e6318ee9faafee8fd82deccf391c07 Mon Sep 17 00:00:00 2001 From: xzhangxian1008 Date: Thu, 8 Dec 2022 15:30:04 +0800 Subject: [PATCH] This is an automated cherry-pick of #6430 Signed-off-by: ti-chi-bot --- .../src/Common/getNumberOfLogicalCPUCores.cpp | 34 +++++++ ...PUCores.h => getNumberOfLogicalCPUCores.h} | 13 +++ .../Common/getNumberOfPhysicalCPUCores.cpp | 65 ------------ dbms/src/Flash/FlashService.cpp | 7 +- dbms/src/Flash/Mpp/MinTSOScheduler.cpp | 10 ++ dbms/src/Interpreters/SettingsCommon.h | 13 +-- dbms/src/Server/Server.cpp | 23 ++++- dbms/src/Server/ServerInfo.h | 98 +++++++++++++++++++ 8 files changed, 186 insertions(+), 77 deletions(-) create mode 100644 dbms/src/Common/getNumberOfLogicalCPUCores.cpp rename dbms/src/Common/{getNumberOfPhysicalCPUCores.h => getNumberOfLogicalCPUCores.h} (59%) delete mode 100644 dbms/src/Common/getNumberOfPhysicalCPUCores.cpp create mode 100644 dbms/src/Server/ServerInfo.h diff --git a/dbms/src/Common/getNumberOfLogicalCPUCores.cpp b/dbms/src/Common/getNumberOfLogicalCPUCores.cpp new file mode 100644 index 00000000000..16854909636 --- /dev/null +++ b/dbms/src/Common/getNumberOfLogicalCPUCores.cpp @@ -0,0 +1,34 @@ +// Copyright 2022 PingCAP, Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +namespace CPUCores +{ +UInt16 number_of_logical_cpu_cores = std::thread::hardware_concurrency(); +} // namespace CPUCores + + +UInt16 getNumberOfLogicalCPUCores() +{ + return CPUCores::number_of_logical_cpu_cores; +} + +// We should call this function before Context has been created, +// which will call `getNumberOfLogicalCPUCores`, or we can not +// set cpu cores any more. +void setNumberOfLogicalCPUCores(UInt16 max_logical_cpu_cores) +{ + CPUCores::number_of_logical_cpu_cores = max_logical_cpu_cores; +} diff --git a/dbms/src/Common/getNumberOfPhysicalCPUCores.h b/dbms/src/Common/getNumberOfLogicalCPUCores.h similarity index 59% rename from dbms/src/Common/getNumberOfPhysicalCPUCores.h rename to dbms/src/Common/getNumberOfLogicalCPUCores.h index 6f7eaef4bb4..3eae129c9f5 100644 --- a/dbms/src/Common/getNumberOfPhysicalCPUCores.h +++ b/dbms/src/Common/getNumberOfLogicalCPUCores.h @@ -14,5 +14,18 @@ #pragma once +<<<<<<< HEAD:dbms/src/Common/getNumberOfPhysicalCPUCores.h /// Get number of CPU cores without hyper-threading. unsigned getNumberOfPhysicalCPUCores(); +======= +#include + +#include + +UInt16 getNumberOfLogicalCPUCores(); + +// We should call this function before Context has been created, +// which will call `getNumberOfLogicalCPUCores`, or we can not +// set cpu cores any more. +void setNumberOfLogicalCPUCores(UInt16 max_logical_cpu_cores); +>>>>>>> 966e7e228e (Get correct cpu cores in k8s pod (#6430)):dbms/src/Common/getNumberOfLogicalCPUCores.h diff --git a/dbms/src/Common/getNumberOfPhysicalCPUCores.cpp b/dbms/src/Common/getNumberOfPhysicalCPUCores.cpp deleted file mode 100644 index de98f4faa10..00000000000 --- a/dbms/src/Common/getNumberOfPhysicalCPUCores.cpp +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2022 PingCAP, Ltd. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include - -#include - -#if defined(__x86_64__) -#include -#endif - -namespace DB -{ -namespace ErrorCodes -{ -extern const int CPUID_ERROR; -} -} // namespace DB - -unsigned getNumberOfPhysicalCPUCores() -{ - unsigned res = 0; -#if defined(__x86_64__) - - cpu_raw_data_t raw_data; - cpu_id_t data; - if (0 == cpuid_get_raw_data(&raw_data) && 0 == cpu_identify(&raw_data, &data) && data.num_logical_cpus != 0) - { - res = data.num_cores * data.total_logical_cpus / data.num_logical_cpus; - } - - /// Also, libcpuid gives strange result on Google Compute Engine VMs. - /// Example: - /// num_cores = 12, /// number of physical cores on current CPU socket - /// total_logical_cpus = 1, /// total number of logical cores on all sockets - /// num_logical_cpus = 24. /// number of logical cores on current CPU socket - /// It means two-way hyper-threading (24 / 12), but contradictory, 'total_logical_cpus' == 1. - if (res != 0) - { - return res; - } - // else fallback -#endif - - /// As a fallback (also for non-x86 architectures) assume there are no hyper-threading on the system. - /// (Actually, only Aarch64 is supported). - res = std::thread::hardware_concurrency(); - if (res == 0) - { - throw DB::Exception("Cannot Identify CPU: Unsupported processor", DB::ErrorCodes::CPUID_ERROR); - } - return res; -} diff --git a/dbms/src/Flash/FlashService.cpp b/dbms/src/Flash/FlashService.cpp index 422c6b47cfa..fad8bdac500 100644 --- a/dbms/src/Flash/FlashService.cpp +++ b/dbms/src/Flash/FlashService.cpp @@ -16,6 +16,11 @@ #include #include #include +<<<<<<< HEAD +======= +#include +#include +>>>>>>> 966e7e228e (Get correct cpu cores in k8s pod (#6430)) #include #include #include @@ -55,7 +60,7 @@ FlashService::FlashService(IServer & server_) auto settings = server_.context().getSettingsRef(); enable_local_tunnel = settings.enable_local_tunnel; enable_async_grpc_client = settings.enable_async_grpc_client; - const size_t default_size = 2 * getNumberOfPhysicalCPUCores(); + const size_t default_size = getNumberOfLogicalCPUCores(); auto cop_pool_size = static_cast(settings.cop_pool_size); cop_pool_size = cop_pool_size ? cop_pool_size : default_size; diff --git a/dbms/src/Flash/Mpp/MinTSOScheduler.cpp b/dbms/src/Flash/Mpp/MinTSOScheduler.cpp index 940aca349eb..c7b619d9950 100644 --- a/dbms/src/Flash/Mpp/MinTSOScheduler.cpp +++ b/dbms/src/Flash/Mpp/MinTSOScheduler.cpp @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include @@ -28,8 +29,17 @@ MinTSOScheduler::MinTSOScheduler(UInt64 soft_limit, UInt64 hard_limit) , estimated_thread_usage(0) , log(&Poco::Logger::get("MinTSOScheduler")) { +<<<<<<< HEAD auto cores = getNumberOfPhysicalCPUCores(); active_set_soft_limit = (cores + 2) / 2; /// at least 1 +======= + auto cores = static_cast(getNumberOfLogicalCPUCores() / 2); + if (active_set_soft_limit == 0 || active_set_soft_limit > 10 * cores) + { + /// set active_set_soft_limit to a reasonable value + active_set_soft_limit = (cores + 2) / 2; /// at least 1 + } +>>>>>>> 966e7e228e (Get correct cpu cores in k8s pod (#6430)) if (isDisabled()) { LOG_FMT_INFO(log, "MinTSOScheduler is disabled!"); diff --git a/dbms/src/Interpreters/SettingsCommon.h b/dbms/src/Interpreters/SettingsCommon.h index cdc7b1b6fbd..4510159da57 100644 --- a/dbms/src/Interpreters/SettingsCommon.h +++ b/dbms/src/Interpreters/SettingsCommon.h @@ -16,7 +16,7 @@ #include #include -#include +#include #include #include #include @@ -26,7 +26,6 @@ #include #include - namespace DB { namespace ErrorCodes @@ -166,18 +165,12 @@ struct SettingMaxThreads is_auto = true; } - UInt64 getAutoValue() const + static UInt64 getAutoValue() { - static auto res = getAutoValueImpl(); + static auto res = getNumberOfLogicalCPUCores(); return res; } - /// Executed once for all time. Executed from one thread. - UInt64 getAutoValueImpl() const - { - return getNumberOfPhysicalCPUCores(); - } - UInt64 get() const { return value; diff --git a/dbms/src/Server/Server.cpp b/dbms/src/Server/Server.cpp index a71830ef390..993c86fa68e 100644 --- a/dbms/src/Server/Server.cpp +++ b/dbms/src/Server/Server.cpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #include #include @@ -1049,7 +1049,28 @@ int Server::main(const std::vector & /*args*/) LOG_FMT_INFO(log, "tiflash proxy thread is joined"); }); +<<<<<<< HEAD CurrentMetrics::set(CurrentMetrics::Revision, ClickHouseRevision::get()); +======= + /// get CPU/memory/disk info of this server + if (tiflash_instance_wrap.proxy_helper) + { + diagnosticspb::ServerInfoRequest request; + request.set_tp(static_cast(1)); + diagnosticspb::ServerInfoResponse response; + std::string req = request.SerializeAsString(); + auto * helper = tiflash_instance_wrap.proxy_helper; + helper->fn_server_info(helper->proxy_ptr, strIntoView(&req), &response); + server_info.parseSysInfo(response); + setNumberOfLogicalCPUCores(server_info.cpu_info.logical_cores); + LOG_INFO(log, "ServerInfo: {}", server_info.debugString()); + } + else + { + setNumberOfLogicalCPUCores(std::thread::hardware_concurrency()); + LOG_INFO(log, "TiFlashRaftProxyHelper is null, failed to get server info"); + } +>>>>>>> 966e7e228e (Get correct cpu cores in k8s pod (#6430)) // print necessary grpc log. grpc_log = &Poco::Logger::get("grpc"); diff --git a/dbms/src/Server/ServerInfo.h b/dbms/src/Server/ServerInfo.h new file mode 100644 index 00000000000..d9731e37bb8 --- /dev/null +++ b/dbms/src/Server/ServerInfo.h @@ -0,0 +1,98 @@ +// Copyright 2022 PingCAP, Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once +#include +#include + +#include +#include + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#ifdef __clang__ +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif +#include +#pragma GCC diagnostic pop + +namespace DB +{ +class ServerInfo +{ +public: + struct CPUInfo + { + /// number of logical CPU cores + UInt16 logical_cores = std::thread::hardware_concurrency(); + /// number of physical CPU cores + UInt16 physical_cores = std::thread::hardware_concurrency() / 2; + /// number of L1 cache size + /// units: Byte + UInt32 l1_cache_size = 16384; // 16KB (typical value) + /// number of L2 cache size + /// units: Byte + UInt32 l2_cache_size = 65536; // 64KB (typical value) + /// number of L3 cache size + /// units: Byte + UInt32 l3_cache_size = 2097152; // 2MB (typical value) + /// number of L1 cache line size + UInt8 l1_cache_line_size = 64; // 64B (typical value) + /// number of L2 cache line size + UInt8 l2_cache_line_size = 64; // 64B (typical value) + /// number of L3 cache line size + UInt8 l3_cache_line_size = 64; // 64B (typical value) + /// CPU architecture + String arch; + /// CPU frequency + String frequency; + }; + + struct Disk + { + String name; + enum DiskType + { + UNKNOWN = 0, + HDD = 1, + SSD = 2 + }; + DiskType disk_type; + UInt64 total_space = 0; + UInt64 free_space = 0; + String mount_point; + String fs_type; + }; + using DiskInfo = std::vector; + + struct MemoryInfo + { + /// total memory size + /// units: Byte + UInt64 capacity = getMemoryAmount(); + }; + + ServerInfo() = default; + ~ServerInfo() = default; + void parseCPUInfo(const diagnosticspb::ServerInfoItem & cpu_info_item); + void parseDiskInfo(const diagnosticspb::ServerInfoItem & disk_info_item); + void parseMemoryInfo(const diagnosticspb::ServerInfoItem & memory_info_item); + void parseSysInfo(const diagnosticspb::ServerInfoResponse & sys_info_response); + String debugString() const; + + CPUInfo cpu_info; + DiskInfo disk_infos; + MemoryInfo memory_info; +}; +} // namespace DB