Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Side by Side Diff: base/cpu.cc

Issue 2667513003: Remove some LazyInstance use in base/ (Closed)
Patch Set: no message_window Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/debug/close_handle_hook_win.h » ('j') | base/trace_event/trace_log.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/cpu.h" 5 #include "base/cpu.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <string.h> 10 #include <string.h>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 16
17 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) 17 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
18 #include "base/files/file_util.h" 18 #include "base/files/file_util.h"
19 #include "base/lazy_instance.h"
20 #endif 19 #endif
21 20
22 #if defined(ARCH_CPU_X86_FAMILY) 21 #if defined(ARCH_CPU_X86_FAMILY)
23 #if defined(_MSC_VER) 22 #if defined(_MSC_VER)
24 #include <intrin.h> 23 #include <intrin.h>
25 #include <immintrin.h> // For _xgetbv() 24 #include <immintrin.h> // For _xgetbv()
26 #endif 25 #endif
27 #endif 26 #endif
28 27
29 namespace base { 28 namespace base {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 86
88 __asm__ volatile ( 87 __asm__ volatile (
89 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr)); 88 "xgetbv" : "=a"(eax), "=d"(edx) : "c"(xcr));
90 return (static_cast<uint64_t>(edx) << 32) | eax; 89 return (static_cast<uint64_t>(edx) << 32) | eax;
91 } 90 }
92 91
93 #endif // !_MSC_VER 92 #endif // !_MSC_VER
94 #endif // ARCH_CPU_X86_FAMILY 93 #endif // ARCH_CPU_X86_FAMILY
95 94
96 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) 95 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
97 class LazyCpuInfoValue { 96 std::string* CpuInfoBrand() {
98 public: 97 static std::string* brand = []() {
99 LazyCpuInfoValue() {
100 // This function finds the value from /proc/cpuinfo under the key "model 98 // This function finds the value from /proc/cpuinfo under the key "model
101 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7 99 // name" or "Processor". "model name" is used in Linux 3.8 and later (3.7
102 // and later for arm64) and is shown once per CPU. "Processor" is used in 100 // and later for arm64) and is shown once per CPU. "Processor" is used in
103 // earler versions and is shown only once at the top of /proc/cpuinfo 101 // earler versions and is shown only once at the top of /proc/cpuinfo
104 // regardless of the number CPUs. 102 // regardless of the number CPUs.
105 const char kModelNamePrefix[] = "model name\t: "; 103 const char kModelNamePrefix[] = "model name\t: ";
106 const char kProcessorPrefix[] = "Processor\t: "; 104 const char kProcessorPrefix[] = "Processor\t: ";
107 105
108 std::string contents; 106 std::string contents;
109 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); 107 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
110 DCHECK(!contents.empty()); 108 DCHECK(!contents.empty());
111 if (contents.empty()) { 109 if (contents.empty()) {
112 return; 110 return new std::string();
113 } 111 }
114 112
115 std::istringstream iss(contents); 113 std::istringstream iss(contents);
116 std::string line; 114 std::string line;
117 while (std::getline(iss, line)) { 115 while (std::getline(iss, line)) {
118 if (brand_.empty() && 116 if ((line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
119 (line.compare(0, strlen(kModelNamePrefix), kModelNamePrefix) == 0 ||
120 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) { 117 line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0)) {
121 brand_.assign(line.substr(strlen(kModelNamePrefix))); 118 return new std::string(line.substr(strlen(kModelNamePrefix)));
122 } 119 }
123 } 120 }
124 }
125 121
126 const std::string& brand() const { return brand_; } 122 return new std::string();
123 }();
127 124
128 private: 125 return brand;
129 std::string brand_; 126 }
130 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue);
131 };
132
133 base::LazyInstance<LazyCpuInfoValue>::Leaky g_lazy_cpuinfo =
134 LAZY_INSTANCE_INITIALIZER;
135
136 #endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || 127 #endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) ||
137 // defined(OS_LINUX)) 128 // defined(OS_LINUX))
138 129
139 } // anonymous namespace 130 } // anonymous namespace
140 131
141 void CPU::Initialize() { 132 void CPU::Initialize() {
142 #if defined(ARCH_CPU_X86_FAMILY) 133 #if defined(ARCH_CPU_X86_FAMILY)
143 int cpu_info[4] = {-1}; 134 int cpu_info[4] = {-1};
144 char cpu_string[48]; 135 char cpu_string[48];
145 136
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 205 }
215 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string); 206 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string);
216 } 207 }
217 208
218 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007; 209 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
219 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) { 210 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
220 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter); 211 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
221 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0; 212 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
222 } 213 }
223 #elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) 214 #elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
224 cpu_brand_.assign(g_lazy_cpuinfo.Get().brand()); 215 cpu_brand_.assign(*CpuInfoBrand());
225 #endif 216 #endif
226 } 217 }
227 218
228 CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const { 219 CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const {
229 if (has_avx2()) return AVX2; 220 if (has_avx2()) return AVX2;
230 if (has_avx()) return AVX; 221 if (has_avx()) return AVX;
231 if (has_sse42()) return SSE42; 222 if (has_sse42()) return SSE42;
232 if (has_sse41()) return SSE41; 223 if (has_sse41()) return SSE41;
233 if (has_ssse3()) return SSSE3; 224 if (has_ssse3()) return SSSE3;
234 if (has_sse3()) return SSE3; 225 if (has_sse3()) return SSE3;
235 if (has_sse2()) return SSE2; 226 if (has_sse2()) return SSE2;
236 if (has_sse()) return SSE; 227 if (has_sse()) return SSE;
237 return PENTIUM; 228 return PENTIUM;
238 } 229 }
239 230
240 } // namespace base 231 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/debug/close_handle_hook_win.h » ('j') | base/trace_event/trace_log.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698