File: cpu_identity.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (169 lines) | stat: -rw-r--r-- 5,524 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/metrics/perf/cpu_identity.h"

#include <string.h>

#include <algorithm>  // for std::lower_bound()

#include "base/compiler_specific.h"
#include "base/cpu.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"

namespace metrics {

namespace internal {

const CpuUarchTableEntry kCpuUarchTable[] = {
    // These were found on various sources on the Internet. Main ones are:
    // http://instlatx64.atw.hu/ for CPUID to model name and
    // http://www.cpu-world.com for model name to microarchitecture
    // clang-format off
    {"06_09", "Banias"},
    {"06_0D", "Dothan"},
    {"06_0F", "Merom"},
    {"06_16", "Merom"},
    {"06_17", "Nehalem"},
    {"06_1A", "Nehalem"},
    {"06_1C", "Bonnell"},     // Atom
    {"06_1D", "Nehalem"},
    {"06_1E", "Nehalem"},
    {"06_1F", "Nehalem"},
    {"06_25", "Westmere"},
    {"06_26", "Bonnell"},     // Atom
    {"06_2A", "SandyBridge"},
    {"06_2C", "Westmere"},
    {"06_2D", "SandyBridge"},
    {"06_2E", "Nehalem"},
    {"06_2F", "Westmere"},
    {"06_36", "Saltwell"},    // Atom
    {"06_37", "Silvermont"},
    {"06_3A", "IvyBridge"},
    {"06_3C", "Haswell"},
    {"06_3D", "Broadwell"},
    {"06_3E", "IvyBridge"},
    {"06_3F", "Haswell"},
    {"06_45", "Haswell"},
    {"06_46", "Haswell"},
    {"06_47", "Broadwell"},   // Broadwell-H
    {"06_4C", "Airmont"},     // Braswell
    {"06_4D", "Silvermont"},  // Avoton/Rangely
    {"06_4E", "Skylake"},
    {"06_55", "Skylake"},     // Skylake-X
    {"06_56", "Broadwell"},   // Broadwell-DE
    {"06_5C", "Goldmont"},
    {"06_5E", "Skylake"},
    {"06_5F", "Goldmont"},    // Denverton
    {"06_7A", "GoldmontPlus"},
    {"06_7E", "IceLake"},
    {"06_8C", "Tigerlake"},
    {"06_8D", "Tigerlake"},
    {"06_8E", "Kabylake"},
    {"06_97", "AlderLake"},
    {"06_9A", "AlderLake"},
    {"06_9C", "Tremont"},     // Jasperlake
    {"06_9E", "Kabylake"},
    {"06_A5", "CometLake"},
    {"06_A6", "CometLake"},
    {"06_B7", "RaptorLake"},
    {"06_BA", "RaptorLake"},
    {"06_BE", "Gracemont"},   // Alderlake-N
    {"06_BF", "RaptorLake"},
    {"0F_03", "Prescott"},
    {"0F_04", "Prescott"},
    {"0F_06", "Presler"},
    {"0F_68", "Athlon64"},
    {"0F_6B", "Athlon64"},
    {"10_02", "K10"},
    {"10_04", "K10"},
    {"10_05", "K10"},
    {"10_06", "K10"},
    {"12_01", "Llano"},
    {"14_01", "Bobcat"},
    {"14_02", "Bobcat"},
    {"15_01", "Bulldozer"},
    {"15_10", "Piledriver"},  // AMD Trinity
    {"15_13", "Piledriver"},  // AMD Richland
    {"15_30", "Steamroller"}, // AMD Kaveri
    {"15_38", "Steamroller"}, // AMD Godavari
    {"15_60", "Excavator"},   // AMD Carizzo
    {"15_65", "Excavator"},   // AMD Bristol Ridge
    {"15_70", "Excavator"},   // AMD Stoney Ridge
    {"16_00", "Jaguar"},      // AMD APUs Beema, Mullins, Steppe & Crowned Eagle
    {"16_30", "Puma"},        // AMD APUs Kabini, Temash, Kyoto
    {"17_01", "Zen"},         // AMD Summit Ridge
    {"17_08", "Zen+"},        // AMD Pinacle Ridge
    {"17_11", "Zen"},         // AMD Raven Ridge
    {"17_18", "Zen+"},        // AMD Picasso
    {"17_20", "Zen"},         // AMD Dali
    {"17_60", "Zen2"},        // AMD Renoir
    {"17_68", "Zen2"},        // AMD Lucienne
    {"17_71", "Zen2"},        // AMD Matisse
    {"17_A0", "Zen2"},        // AMD Mendocino
    {"19_50", "Zen3"},        // AMD Cezanne
    // clang-format on
};

const CpuUarchTableEntry* kCpuUarchTableEnd = std::end(kCpuUarchTable);

bool CpuUarchTableCmp(const CpuUarchTableEntry& a,
                      const CpuUarchTableEntry& b) {
  return UNSAFE_TODO(strcmp(a.family_model, b.family_model)) < 0;
}

}  // namespace internal

CPUIdentity::CPUIdentity() : family(0), model(0) {}

CPUIdentity::CPUIdentity(const CPUIdentity& other) = default;

CPUIdentity::~CPUIdentity() = default;

std::string GetCpuUarch(const CPUIdentity& cpuid) {
  if (cpuid.vendor != "GenuineIntel" && cpuid.vendor != "AuthenticAMD")
    return std::string();  // Non-Intel or -AMD

  std::string family_model =
      base::StringPrintf("%02X_%02X", cpuid.family, cpuid.model);
  const internal::CpuUarchTableEntry search_elem = {family_model.c_str(), ""};
  auto* bound = std::lower_bound(internal::kCpuUarchTable,
                                 internal::kCpuUarchTableEnd, search_elem,
                                 internal::CpuUarchTableCmp);
  if (bound == internal::kCpuUarchTableEnd ||
      bound->family_model != family_model)
    return std::string();  // Unknown uarch
  return bound->uarch;
}

CPUIdentity GetCPUIdentity() {
  CPUIdentity result = {};
  result.arch = base::SysInfo::OperatingSystemArchitecture();
  result.release =
#if BUILDFLAG(IS_CHROMEOS)
      base::SysInfo::KernelVersion();
#elif BUILDFLAG(IS_LINUX)
      base::SysInfo::OperatingSystemVersion();
#else
#error "Unsupported configuration"
#endif
  base::CPU cpuid;
  result.vendor = cpuid.vendor_name();
  result.family = cpuid.family();
  result.model = cpuid.model();
  result.model_name = cpuid.cpu_brand();
  return result;
}

std::string SimplifyCPUModelName(const std::string& model_name) {
  std::string result = model_name;
  std::replace(result.begin(), result.end(), ' ', '-');
  base::ReplaceSubstringsAfterOffset(&result, 0, "(R)", "");
  return base::ToLowerASCII(result);
}

}  // namespace metrics