v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cpu.cc
Go to the documentation of this file.
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include "cpu.h"
29 
30 #if V8_LIBC_MSVCRT
31 #include <intrin.h> // __cpuid()
32 #endif
33 #if V8_OS_POSIX
34 #include <unistd.h> // sysconf()
35 #endif
36 #if V8_OS_QNX
37 #include <sys/syspage.h> // cpuinfo
38 #endif
39 
40 #include <ctype.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <algorithm>
46 
47 #include "checks.h"
48 #if V8_OS_WIN
49 #include "win32-headers.h"
50 #endif
51 
52 namespace v8 {
53 namespace internal {
54 
55 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
56 
57 // Define __cpuid() for non-MSVC libraries.
58 #if !V8_LIBC_MSVCRT
59 
60 static V8_INLINE void __cpuid(int cpu_info[4], int info_type) {
61 #if defined(__i386__) && defined(__pic__)
62  // Make sure to preserve ebx, which contains the pointer
63  // to the GOT in case we're generating PIC.
64  __asm__ volatile (
65  "mov %%ebx, %%edi\n\t"
66  "cpuid\n\t"
67  "xchg %%edi, %%ebx\n\t"
68  : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
69  : "a"(info_type)
70  );
71 #else
72  __asm__ volatile (
73  "cpuid \n\t"
74  : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
75  : "a"(info_type)
76  );
77 #endif // defined(__i386__) && defined(__pic__)
78 }
79 
80 #endif // !V8_LIBC_MSVCRT
81 
82 #elif V8_HOST_ARCH_ARM || V8_HOST_ARCH_MIPS
83 
84 #if V8_OS_LINUX
85 
86 #if V8_HOST_ARCH_ARM
87 
88 // See <uapi/asm/hwcap.h> kernel header.
89 /*
90  * HWCAP flags - for elf_hwcap (in kernel) and AT_HWCAP
91  */
92 #define HWCAP_SWP (1 << 0)
93 #define HWCAP_HALF (1 << 1)
94 #define HWCAP_THUMB (1 << 2)
95 #define HWCAP_26BIT (1 << 3) /* Play it safe */
96 #define HWCAP_FAST_MULT (1 << 4)
97 #define HWCAP_FPA (1 << 5)
98 #define HWCAP_VFP (1 << 6)
99 #define HWCAP_EDSP (1 << 7)
100 #define HWCAP_JAVA (1 << 8)
101 #define HWCAP_IWMMXT (1 << 9)
102 #define HWCAP_CRUNCH (1 << 10)
103 #define HWCAP_THUMBEE (1 << 11)
104 #define HWCAP_NEON (1 << 12)
105 #define HWCAP_VFPv3 (1 << 13)
106 #define HWCAP_VFPv3D16 (1 << 14) /* also set for VFPv4-D16 */
107 #define HWCAP_TLS (1 << 15)
108 #define HWCAP_VFPv4 (1 << 16)
109 #define HWCAP_IDIVA (1 << 17)
110 #define HWCAP_IDIVT (1 << 18)
111 #define HWCAP_VFPD32 (1 << 19) /* set if VFP has 32 regs (not 16) */
112 #define HWCAP_IDIV (HWCAP_IDIVA | HWCAP_IDIVT)
113 #define HWCAP_LPAE (1 << 20)
114 
115 #define AT_HWCAP 16
116 
117 // Read the ELF HWCAP flags by parsing /proc/self/auxv.
118 static uint32_t ReadELFHWCaps() {
119  uint32_t result = 0;
120  FILE* fp = fopen("/proc/self/auxv", "r");
121  if (fp != NULL) {
122  struct { uint32_t tag; uint32_t value; } entry;
123  for (;;) {
124  size_t n = fread(&entry, sizeof(entry), 1, fp);
125  if (n == 0 || (entry.tag == 0 && entry.value == 0)) {
126  break;
127  }
128  if (entry.tag == AT_HWCAP) {
129  result = entry.value;
130  break;
131  }
132  }
133  fclose(fp);
134  }
135  return result;
136 }
137 
138 #endif // V8_HOST_ARCH_ARM
139 
140 // Extract the information exposed by the kernel via /proc/cpuinfo.
141 class CPUInfo V8_FINAL BASE_EMBEDDED {
142  public:
143  CPUInfo() : datalen_(0) {
144  // Get the size of the cpuinfo file by reading it until the end. This is
145  // required because files under /proc do not always return a valid size
146  // when using fseek(0, SEEK_END) + ftell(). Nor can the be mmap()-ed.
147  static const char PATHNAME[] = "/proc/cpuinfo";
148  FILE* fp = fopen(PATHNAME, "r");
149  if (fp != NULL) {
150  for (;;) {
151  char buffer[256];
152  size_t n = fread(buffer, 1, sizeof(buffer), fp);
153  if (n == 0) {
154  break;
155  }
156  datalen_ += n;
157  }
158  fclose(fp);
159  }
160 
161  // Read the contents of the cpuinfo file.
162  data_ = new char[datalen_ + 1];
163  fp = fopen(PATHNAME, "r");
164  if (fp != NULL) {
165  for (size_t offset = 0; offset < datalen_; ) {
166  size_t n = fread(data_ + offset, 1, datalen_ - offset, fp);
167  if (n == 0) {
168  break;
169  }
170  offset += n;
171  }
172  fclose(fp);
173  }
174 
175  // Zero-terminate the data.
176  data_[datalen_] = '\0';
177  }
178 
179  ~CPUInfo() {
180  delete[] data_;
181  }
182 
183  // Extract the content of a the first occurence of a given field in
184  // the content of the cpuinfo file and return it as a heap-allocated
185  // string that must be freed by the caller using delete[].
186  // Return NULL if not found.
187  char* ExtractField(const char* field) const {
188  ASSERT(field != NULL);
189 
190  // Look for first field occurence, and ensure it starts the line.
191  size_t fieldlen = strlen(field);
192  char* p = data_;
193  for (;;) {
194  p = strstr(p, field);
195  if (p == NULL) {
196  return NULL;
197  }
198  if (p == data_ || p[-1] == '\n') {
199  break;
200  }
201  p += fieldlen;
202  }
203 
204  // Skip to the first colon followed by a space.
205  p = strchr(p + fieldlen, ':');
206  if (p == NULL || !isspace(p[1])) {
207  return NULL;
208  }
209  p += 2;
210 
211  // Find the end of the line.
212  char* q = strchr(p, '\n');
213  if (q == NULL) {
214  q = data_ + datalen_;
215  }
216 
217  // Copy the line into a heap-allocated buffer.
218  size_t len = q - p;
219  char* result = new char[len + 1];
220  if (result != NULL) {
221  memcpy(result, p, len);
222  result[len] = '\0';
223  }
224  return result;
225  }
226 
227  private:
228  char* data_;
229  size_t datalen_;
230 };
231 
232 
233 // Checks that a space-separated list of items contains one given 'item'.
234 static bool HasListItem(const char* list, const char* item) {
235  ssize_t item_len = strlen(item);
236  const char* p = list;
237  if (p != NULL) {
238  while (*p != '\0') {
239  // Skip whitespace.
240  while (isspace(*p)) ++p;
241 
242  // Find end of current list item.
243  const char* q = p;
244  while (*q != '\0' && !isspace(*q)) ++q;
245 
246  if (item_len == q - p && memcmp(p, item, item_len) == 0) {
247  return true;
248  }
249 
250  // Skip to next item.
251  p = q;
252  }
253  }
254  return false;
255 }
256 
257 #endif // V8_OS_LINUX
258 
259 #endif // V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
260 
261 CPU::CPU() : stepping_(0),
262  model_(0),
263  ext_model_(0),
264  family_(0),
265  ext_family_(0),
266  type_(0),
267  implementer_(0),
268  architecture_(0),
269  part_(0),
270  has_fpu_(false),
271  has_cmov_(false),
272  has_sahf_(false),
273  has_mmx_(false),
274  has_sse_(false),
275  has_sse2_(false),
276  has_sse3_(false),
277  has_ssse3_(false),
278  has_sse41_(false),
279  has_sse42_(false),
280  has_idiva_(false),
281  has_neon_(false),
282  has_thumbee_(false),
283  has_vfp_(false),
284  has_vfp3_(false),
285  has_vfp3_d32_(false) {
286  memcpy(vendor_, "Unknown", 8);
287 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
288  int cpu_info[4];
289 
290  // __cpuid with an InfoType argument of 0 returns the number of
291  // valid Ids in CPUInfo[0] and the CPU identification string in
292  // the other three array elements. The CPU identification string is
293  // not in linear order. The code below arranges the information
294  // in a human readable form. The human readable order is CPUInfo[1] |
295  // CPUInfo[3] | CPUInfo[2]. CPUInfo[2] and CPUInfo[3] are swapped
296  // before using memcpy to copy these three array elements to cpu_string.
297  __cpuid(cpu_info, 0);
298  unsigned num_ids = cpu_info[0];
299  std::swap(cpu_info[2], cpu_info[3]);
300  memcpy(vendor_, cpu_info + 1, 12);
301  vendor_[12] = '\0';
302 
303  // Interpret CPU feature information.
304  if (num_ids > 0) {
305  __cpuid(cpu_info, 1);
306  stepping_ = cpu_info[0] & 0xf;
307  model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
308  family_ = (cpu_info[0] >> 8) & 0xf;
309  type_ = (cpu_info[0] >> 12) & 0x3;
310  ext_model_ = (cpu_info[0] >> 16) & 0xf;
311  ext_family_ = (cpu_info[0] >> 20) & 0xff;
312  has_fpu_ = (cpu_info[3] & 0x00000001) != 0;
313  has_cmov_ = (cpu_info[3] & 0x00008000) != 0;
314  has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
315  has_sse_ = (cpu_info[3] & 0x02000000) != 0;
316  has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
317  has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
318  has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
319  has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
320  has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
321  }
322 
323  // Query extended IDs.
324  __cpuid(cpu_info, 0x80000000);
325  unsigned num_ext_ids = cpu_info[0];
326 
327  // Interpret extended CPU feature information.
328  if (num_ext_ids > 0x80000000) {
329  __cpuid(cpu_info, 0x80000001);
330  // SAHF is always available in compat/legacy mode,
331  // but must be probed in long mode.
332 #if V8_HOST_ARCH_IA32
333  has_sahf_ = true;
334 #else
335  has_sahf_ = (cpu_info[2] & 0x00000001) != 0;
336 #endif
337  }
338 
339 #elif V8_HOST_ARCH_ARM
340 
341 #if V8_OS_LINUX
342 
343  CPUInfo cpu_info;
344 
345  // Extract implementor from the "CPU implementer" field.
346  char* implementer = cpu_info.ExtractField("CPU implementer");
347  if (implementer != NULL) {
348  char* end ;
349  implementer_ = strtol(implementer, &end, 0);
350  if (end == implementer) {
351  implementer_ = 0;
352  }
353  delete[] implementer;
354  }
355 
356  // Extract part number from the "CPU part" field.
357  char* part = cpu_info.ExtractField("CPU part");
358  if (part != NULL) {
359  char* end ;
360  part_ = strtol(part, &end, 0);
361  if (end == part) {
362  part_ = 0;
363  }
364  delete[] part;
365  }
366 
367  // Extract architecture from the "CPU Architecture" field.
368  // The list is well-known, unlike the the output of
369  // the 'Processor' field which can vary greatly.
370  // See the definition of the 'proc_arch' array in
371  // $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
372  // same file.
373  char* architecture = cpu_info.ExtractField("CPU architecture");
374  if (architecture != NULL) {
375  char* end;
376  architecture_ = strtol(architecture, &end, 10);
377  if (end == architecture) {
378  architecture_ = 0;
379  }
380  delete[] architecture;
381 
382  // Unfortunately, it seems that certain ARMv6-based CPUs
383  // report an incorrect architecture number of 7!
384  //
385  // See http://code.google.com/p/android/issues/detail?id=10812
386  //
387  // We try to correct this by looking at the 'elf_format'
388  // field reported by the 'Processor' field, which is of the
389  // form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
390  // an ARMv6-one. For example, the Raspberry Pi is one popular
391  // ARMv6 device that reports architecture 7.
392  if (architecture_ == 7) {
393  char* processor = cpu_info.ExtractField("Processor");
394  if (HasListItem(processor, "(v6l)")) {
395  architecture_ = 6;
396  }
397  delete[] processor;
398  }
399  }
400 
401  // Try to extract the list of CPU features from ELF hwcaps.
402  uint32_t hwcaps = ReadELFHWCaps();
403  if (hwcaps != 0) {
404  has_idiva_ = (hwcaps & HWCAP_IDIVA) != 0;
405  has_neon_ = (hwcaps & HWCAP_NEON) != 0;
406  has_thumbee_ = (hwcaps & HWCAP_THUMBEE) != 0;
407  has_vfp_ = (hwcaps & HWCAP_VFP) != 0;
408  has_vfp3_ = (hwcaps & (HWCAP_VFPv3 | HWCAP_VFPv3D16 | HWCAP_VFPv4)) != 0;
409  has_vfp3_d32_ = (has_vfp3_ && ((hwcaps & HWCAP_VFPv3D16) == 0 ||
410  (hwcaps & HWCAP_VFPD32) != 0));
411  } else {
412  // Try to fallback to "Features" CPUInfo field.
413  char* features = cpu_info.ExtractField("Features");
414  has_idiva_ = HasListItem(features, "idiva");
415  has_neon_ = HasListItem(features, "neon");
416  has_thumbee_ = HasListItem(features, "thumbee");
417  has_vfp_ = HasListItem(features, "vfp");
418  if (HasListItem(features, "vfpv3")) {
419  has_vfp3_ = true;
420  has_vfp3_d32_ = true;
421  } else if (HasListItem(features, "vfpv3d16")) {
422  has_vfp3_ = true;
423  }
424  delete[] features;
425  }
426 
427  // Some old kernels will report vfp not vfpv3. Here we make an attempt
428  // to detect vfpv3 by checking for vfp *and* neon, since neon is only
429  // available on architectures with vfpv3. Checking neon on its own is
430  // not enough as it is possible to have neon without vfp.
431  if (has_vfp_ && has_neon_) {
432  has_vfp3_ = true;
433  }
434 
435  // VFPv3 implies ARMv7, see ARM DDI 0406B, page A1-6.
436  if (architecture_ < 7 && has_vfp3_) {
437  architecture_ = 7;
438  }
439 
440  // ARMv7 implies ThumbEE.
441  if (architecture_ >= 7) {
442  has_thumbee_ = true;
443  }
444 
445  // The earliest architecture with ThumbEE is ARMv6T2.
446  if (has_thumbee_ && architecture_ < 6) {
447  architecture_ = 6;
448  }
449 
450  // We don't support any FPUs other than VFP.
451  has_fpu_ = has_vfp_;
452 
453 #elif V8_OS_QNX
454 
455  uint32_t cpu_flags = SYSPAGE_ENTRY(cpuinfo)->flags;
456  if (cpu_flags & ARM_CPU_FLAG_V7) {
457  architecture_ = 7;
458  has_thumbee_ = true;
459  } else if (cpu_flags & ARM_CPU_FLAG_V6) {
460  architecture_ = 6;
461  // QNX doesn't say if ThumbEE is available.
462  // Assume false for the architectures older than ARMv7.
463  }
464  ASSERT(architecture_ >= 6);
465  has_fpu_ = (cpu_flags & CPU_FLAG_FPU) != 0;
466  has_vfp_ = has_fpu_;
467  if (cpu_flags & ARM_CPU_FLAG_NEON) {
468  has_neon_ = true;
469  has_vfp3_ = has_vfp_;
470 #ifdef ARM_CPU_FLAG_VFP_D32
471  has_vfp3_d32_ = (cpu_flags & ARM_CPU_FLAG_VFP_D32) != 0;
472 #endif
473  }
474  has_idiva_ = (cpu_flags & ARM_CPU_FLAG_IDIV) != 0;
475 
476 #endif // V8_OS_LINUX
477 
478 #elif V8_HOST_ARCH_MIPS
479 
480  // Simple detection of FPU at runtime for Linux.
481  // It is based on /proc/cpuinfo, which reveals hardware configuration
482  // to user-space applications. According to MIPS (early 2010), no similar
483  // facility is universally available on the MIPS architectures,
484  // so it's up to individual OSes to provide such.
485  CPUInfo cpu_info;
486  char* cpu_model = cpu_info.ExtractField("cpu model");
487  has_fpu_ = HasListItem(cpu_model, "FPU");
488  delete[] cpu_model;
489 
490 #endif
491 }
492 
493 
494 // static
495 int CPU::NumberOfProcessorsOnline() {
496 #if V8_OS_WIN
497  SYSTEM_INFO info;
498  GetSystemInfo(&info);
499  return info.dwNumberOfProcessors;
500 #else
501  return static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
502 #endif
503 }
504 
505 } } // namespace v8::internal
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
#define ASSERT(condition)
Definition: checks.h:329
#define BASE_EMBEDDED
Definition: allocation.h:68
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function info
Definition: flags.cc:317
ProfilerEventsProcessor * processor() const
Definition: cpu-profiler.h:263
const Register fp
#define V8_INLINE
Definition: v8config.h:316