Coverage Report

Created: 2024-11-20 16:51

/root/doris/be/src/gutil/cpu.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
#pragma once
5
6
#include <string>
7
#include <tuple>
8
9
#if defined(__APPLE__)
10
#define OS_MACOSX 1
11
#elif defined(__ANDROID__)
12
#define OS_ANDROID 1
13
#elif defined(__linux__)
14
#define OS_LINUX 1
15
#elif defined(_WIN32)
16
#define OS_WIN 1
17
#endif
18
19
#if defined(_M_X64) || defined(__x86_64__)
20
#define ARCH_CPU_X86_FAMILY 1
21
#define ARCH_CPU_X86_64 1
22
#define ARCH_CPU_64_BITS 1
23
#elif defined(_M_IX86) || defined(__i386__)
24
#define ARCH_CPU_X86_FAMILY 1
25
#define ARCH_CPU_X86 1
26
#define ARCH_CPU_32_BITS 1
27
#elif defined(__ARMEL__)
28
#define ARCH_CPU_ARM_FAMILY 1
29
#define ARCH_CPU_ARMEL 1
30
#define ARCH_CPU_32_BITS 1
31
#elif defined(_M_ARM64) || defined(__aarch64__)
32
#define ARCH_CPU_ARM_FAMILY 1
33
#define ARCH_CPU_ARM64 1
34
#define ARCH_CPU_64_BITS 1
35
#endif
36
37
#if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_ANDROID)
38
#define OS_POSIX 1
39
#endif
40
41
namespace base {
42
#if defined(ARCH_CPU_X86_FAMILY)
43
namespace internal {
44
// Compute the CPU family and model based on the vendor and CPUID signature.
45
// Returns in order: family, model, extended family, extended model.
46
std::tuple<int, int, int, int> ComputeX86FamilyAndModel(const std::string& vendor, int signature);
47
} // namespace internal
48
#endif // defined(ARCH_CPU_X86_FAMILY)
49
// Query information about the processor.
50
class CPU final {
51
public:
52
    CPU();
53
    enum IntelMicroArchitecture {
54
        PENTIUM,
55
        SSE,
56
        SSE2,
57
        SSE3,
58
        SSSE3,
59
        SSE41,
60
        SSE42,
61
        AVX,
62
        AVX2,
63
        MAX_INTEL_MICRO_ARCHITECTURE
64
    };
65
    // Accessors for CPU information.
66
0
    const std::string& vendor_name() const { return cpu_vendor_; }
67
0
    int signature() const { return signature_; }
68
0
    int stepping() const { return stepping_; }
69
0
    int model() const { return model_; }
70
0
    int family() const { return family_; }
71
0
    int type() const { return type_; }
72
0
    int extended_model() const { return ext_model_; }
73
0
    int extended_family() const { return ext_family_; }
74
0
    bool has_mmx() const { return has_mmx_; }
75
0
    bool has_sse() const { return has_sse_; }
76
0
    bool has_sse2() const { return has_sse2_; }
77
0
    bool has_sse3() const { return has_sse3_; }
78
0
    bool has_ssse3() const { return has_ssse3_; }
79
0
    bool has_sse41() const { return has_sse41_; }
80
0
    bool has_sse42() const { return has_sse42_; }
81
0
    bool has_popcnt() const { return has_popcnt_; }
82
0
    bool has_avx() const { return has_avx_; }
83
1
    bool has_avx2() const { return has_avx2_; }
84
0
    bool has_aesni() const { return has_aesni_; }
85
0
    bool has_non_stop_time_stamp_counter() const { return has_non_stop_time_stamp_counter_; }
86
0
    bool is_running_in_vm() const { return is_running_in_vm_; }
87
    IntelMicroArchitecture GetIntelMicroArchitecture() const;
88
0
    const std::string& cpu_brand() const { return cpu_brand_; }
89
90
private:
91
    // Query the processor for CPUID information.
92
    void Initialize();
93
    int signature_; // raw form of type, family, model, and stepping
94
    int type_;      // process type
95
    int family_;    // family of the processor
96
    int model_;     // model of processor
97
    int stepping_;  // processor revision number
98
    int ext_model_;
99
    int ext_family_;
100
    bool has_mmx_;
101
    bool has_sse_;
102
    bool has_sse2_;
103
    bool has_sse3_;
104
    bool has_ssse3_;
105
    bool has_sse41_;
106
    bool has_sse42_;
107
    bool has_popcnt_;
108
    bool has_avx_;
109
    bool has_avx2_;
110
    bool has_aesni_;
111
    bool has_non_stop_time_stamp_counter_;
112
    bool is_running_in_vm_;
113
    std::string cpu_vendor_;
114
    std::string cpu_brand_;
115
};
116
} // namespace base