OLD | NEW |
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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 | 13 |
| 14 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) |
| 15 #include "base/file_util.h" |
| 16 #include "base/lazy_instance.h" |
| 17 #endif |
| 18 |
14 #if defined(ARCH_CPU_X86_FAMILY) | 19 #if defined(ARCH_CPU_X86_FAMILY) |
15 #if defined(_MSC_VER) | 20 #if defined(_MSC_VER) |
16 #include <intrin.h> | 21 #include <intrin.h> |
17 #include <immintrin.h> // For _xgetbv() | 22 #include <immintrin.h> // For _xgetbv() |
18 #endif | 23 #endif |
19 #endif | 24 #endif |
20 | 25 |
21 namespace base { | 26 namespace base { |
22 | 27 |
23 CPU::CPU() | 28 CPU::CPU() |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 uint64 _xgetbv(uint32 xcr) { | 82 uint64 _xgetbv(uint32 xcr) { |
78 uint32 eax, edx; | 83 uint32 eax, edx; |
79 | 84 |
80 __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr)); | 85 __asm__ volatile ("xgetbv" : "=a" (eax), "=d" (edx) : "c" (xcr)); |
81 return (static_cast<uint64>(edx) << 32) | eax; | 86 return (static_cast<uint64>(edx) << 32) | eax; |
82 } | 87 } |
83 | 88 |
84 #endif // !_MSC_VER | 89 #endif // !_MSC_VER |
85 #endif // ARCH_CPU_X86_FAMILY | 90 #endif // ARCH_CPU_X86_FAMILY |
86 | 91 |
| 92 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) |
| 93 |
| 94 std::string ParseCpuInfo() { |
| 95 const char kProcessorPrefix[] = "Processor"; |
| 96 std::string contents, cpu_brand; |
| 97 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
| 98 DCHECK(!contents.empty()); |
| 99 if (!contents.empty()) { |
| 100 std::istringstream iss(contents); |
| 101 std::string line; |
| 102 while (std::getline(iss, line)) { |
| 103 if (line.compare(0, strlen(kProcessorPrefix), kProcessorPrefix) == 0) { |
| 104 size_t pos = line.find(": "); |
| 105 if (pos != std::string::npos) { |
| 106 cpu_brand.assign(line.substr(pos + 2)); |
| 107 break; |
| 108 } |
| 109 } |
| 110 } |
| 111 } |
| 112 return cpu_brand; |
| 113 } |
| 114 |
| 115 class LazyCpuInfoValue { |
| 116 public: |
| 117 LazyCpuInfoValue() : value_(ParseCpuInfo()) {} |
| 118 const std::string& value() { return value_; } |
| 119 |
| 120 private: |
| 121 const std::string value_; |
| 122 DISALLOW_COPY_AND_ASSIGN(LazyCpuInfoValue); |
| 123 }; |
| 124 |
| 125 base::LazyInstance<LazyCpuInfoValue> g_lazy_cpu_brand = |
| 126 LAZY_INSTANCE_INITIALIZER; |
| 127 |
| 128 const std::string& CpuBrandInfo() { |
| 129 return g_lazy_cpu_brand.Get().value(); |
| 130 } |
| 131 |
| 132 #endif // defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || |
| 133 // defined(OS_LINUX)) |
| 134 |
87 } // anonymous namespace | 135 } // anonymous namespace |
88 | 136 |
89 void CPU::Initialize() { | 137 void CPU::Initialize() { |
90 #if defined(ARCH_CPU_X86_FAMILY) | 138 #if defined(ARCH_CPU_X86_FAMILY) |
91 int cpu_info[4] = {-1}; | 139 int cpu_info[4] = {-1}; |
92 char cpu_string[48]; | 140 char cpu_string[48]; |
93 | 141 |
94 // __cpuid with an InfoType argument of 0 returns the number of | 142 // __cpuid with an InfoType argument of 0 returns the number of |
95 // valid Ids in CPUInfo[0] and the CPU identification string in | 143 // valid Ids in CPUInfo[0] and the CPU identification string in |
96 // the other three array elements. The CPU identification string is | 144 // the other three array elements. The CPU identification string is |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 cpu_string_ptr += sizeof(cpu_info); | 198 cpu_string_ptr += sizeof(cpu_info); |
151 } | 199 } |
152 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string); | 200 cpu_brand_.assign(cpu_string, cpu_string_ptr - cpu_string); |
153 } | 201 } |
154 | 202 |
155 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007; | 203 const int parameter_containing_non_stop_time_stamp_counter = 0x80000007; |
156 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) { | 204 if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) { |
157 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter); | 205 __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter); |
158 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0; | 206 has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0; |
159 } | 207 } |
160 #elif defined(ARCH_CPU_ARM_FAMILY) | 208 #elif defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX)) |
161 // TODO(piman): Expand this. ARM has a CPUID register, but it's not available | 209 cpu_brand_.assign(CpuBrandInfo()); |
162 // in user mode. /proc/cpuinfo has some information, but it's non standard, | |
163 // platform-specific, and not accessible from the sandbox. | |
164 // For some purposes, this first approximation is enough. | |
165 // crbug.com/313454 | |
166 cpu_brand_.assign("ARM"); | |
167 #endif | 210 #endif |
168 } | 211 } |
169 | 212 |
170 CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const { | 213 CPU::IntelMicroArchitecture CPU::GetIntelMicroArchitecture() const { |
171 if (has_avx()) return AVX; | 214 if (has_avx()) return AVX; |
172 if (has_sse42()) return SSE42; | 215 if (has_sse42()) return SSE42; |
173 if (has_sse41()) return SSE41; | 216 if (has_sse41()) return SSE41; |
174 if (has_ssse3()) return SSSE3; | 217 if (has_ssse3()) return SSSE3; |
175 if (has_sse3()) return SSE3; | 218 if (has_sse3()) return SSE3; |
176 if (has_sse2()) return SSE2; | 219 if (has_sse2()) return SSE2; |
177 if (has_sse()) return SSE; | 220 if (has_sse()) return SSE; |
178 return PENTIUM; | 221 return PENTIUM; |
179 } | 222 } |
180 | 223 |
181 } // namespace base | 224 } // namespace base |
OLD | NEW |