Skip to content

Commit cde5a08

Browse files
committed
ARROW-16478: [C++] Refine cpu info detection
This patch separates OS and ARCH depdendent code and removes CPU frequency detection (cycles_per_ms()) which is brittle and not very useful in practice. There are still many caveats, especially for Arm platform. It's better to adopt a mature library if we want more complete functionalities. E.g., github.com/pytorch/cpuinfo. Below are examples of cpu info detected on various platforms (some from virtual machines). Intel, Linux ------------ Vendor: Intel Model: Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz Features (set bits): 0 1 2 3 4 5 6 7 8 9 10 11 12 Cache sizes: 32768 1048576 37486592 AMD, Linux ---------- Vendor: AMD Model: AMD EPYC 7251 8-Core Processor Features (set bits): 0 1 2 3 4 5 11 12 Cache sizes: 32768 524288 33554432 Intel, MacOS ------------ Vendor: Unknown Model: Unknown Features (set bits): 0 1 2 3 4 Cache sizes: 32768 262144 12582912 Intel, Windows -------------- Vendor: Intel Model: Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz\0\0 Features (set bits): 0 1 2 3 4 5 6 7 8 9 10 11 12 Cache sizes: 131072 2097152 37486592 Intel, MinGW ------------ Vendor: Intel Model: Intel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz\0\0\0\0\0\0\0 Features (set bits): 0 1 2 3 4 5 11 12 Cache sizes: 131072 524288 52428800 Arm, Linux ---------- Vendor: Unknown Model: Unknown Features (set bits): 32 Cache sizes: 65536 1048576 Unknown Arm, MacOS ---------- Vendor: Unknown Model: Unknown Features (set bits): 32 Cache sizes: 65536 4194304 Unknown Closes apache#13112 from cyb70289/cpuinfo-refine Authored-by: Yibo Cai <yibo.cai@arm.com> Signed-off-by: Yibo Cai <yibo.cai@arm.com>
1 parent ed084f6 commit cde5a08

8 files changed

Lines changed: 459 additions & 436 deletions

File tree

cpp/src/arrow/compute/exec.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ ExecContext::ExecContext(MemoryPool* pool, ::arrow::internal::Executor* executor
10461046
this->func_registry_ = func_registry == nullptr ? GetFunctionRegistry() : func_registry;
10471047
}
10481048

1049-
CpuInfo* ExecContext::cpu_info() const { return CpuInfo::GetInstance(); }
1049+
const CpuInfo* ExecContext::cpu_info() const { return CpuInfo::GetInstance(); }
10501050

10511051
// ----------------------------------------------------------------------
10521052
// SelectionVector

cpp/src/arrow/compute/exec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ARROW_EXPORT ExecContext {
6868
/// default_memory_pool().
6969
MemoryPool* memory_pool() const { return pool_; }
7070

71-
::arrow::internal::CpuInfo* cpu_info() const;
71+
const ::arrow::internal::CpuInfo* cpu_info() const;
7272

7373
/// \brief An Executor which may be used to parallelize execution.
7474
::arrow::internal::Executor* executor() const { return executor_; }

cpp/src/arrow/io/memory_benchmark.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
namespace arrow {
2929

3030
using internal::CpuInfo;
31-
static CpuInfo* cpu_info = CpuInfo::GetInstance();
31+
static const CpuInfo* cpu_info = CpuInfo::GetInstance();
3232

3333
static const int kNumCores = cpu_info->num_cores();
34-
static const int64_t kL1Size = cpu_info->CacheSize(CpuInfo::L1_CACHE);
35-
static const int64_t kL2Size = cpu_info->CacheSize(CpuInfo::L2_CACHE);
36-
static const int64_t kL3Size = cpu_info->CacheSize(CpuInfo::L3_CACHE);
34+
static const int64_t kL1Size = cpu_info->CacheSize(CpuInfo::CacheLevel::L1);
35+
static const int64_t kL2Size = cpu_info->CacheSize(CpuInfo::CacheLevel::L2);
36+
static const int64_t kL3Size = cpu_info->CacheSize(CpuInfo::CacheLevel::L3);
3737

3838
constexpr size_t kMemoryPerCore = 32 * 1024 * 1024;
3939
using BufferPtr = std::shared_ptr<Buffer>;

cpp/src/arrow/io/transform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
// Slow stream implementations, mainly for testing and benchmarking
18+
// Transform stream implementations
1919

2020
#pragma once
2121

cpp/src/arrow/util/benchmark_util.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ namespace arrow {
2727

2828
using internal::CpuInfo;
2929

30-
static CpuInfo* cpu_info = CpuInfo::GetInstance();
30+
static const CpuInfo* cpu_info = CpuInfo::GetInstance();
3131

32-
static const int64_t kL1Size = cpu_info->CacheSize(CpuInfo::L1_CACHE);
33-
static const int64_t kL2Size = cpu_info->CacheSize(CpuInfo::L2_CACHE);
34-
static const int64_t kL3Size = cpu_info->CacheSize(CpuInfo::L3_CACHE);
32+
static const int64_t kL1Size = cpu_info->CacheSize(CpuInfo::CacheLevel::L1);
33+
static const int64_t kL2Size = cpu_info->CacheSize(CpuInfo::CacheLevel::L2);
34+
static const int64_t kL3Size = cpu_info->CacheSize(CpuInfo::CacheLevel::L3);
3535
static const int64_t kCantFitInL3Size = kL3Size * 4;
3636
static const std::vector<int64_t> kMemorySizes = {kL1Size, kL2Size, kL3Size,
3737
kCantFitInL3Size};

0 commit comments

Comments
 (0)