• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 the V8 project 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 
5 #include "src/base/sys-info.h"
6 
7 #if V8_OS_POSIX
8 #include <sys/stat.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #if !V8_OS_FUCHSIA
13 #include <sys/resource.h>
14 #endif
15 #endif
16 
17 #if V8_OS_BSD
18 #include <sys/sysctl.h>
19 #endif
20 
21 #include <limits>
22 
23 #include "src/base/logging.h"
24 #include "src/base/macros.h"
25 #if V8_OS_WIN
26 #include <windows.h>
27 
28 #include "src/base/win32-headers.h"
29 #endif
30 
31 #if V8_OS_STARBOARD
32 #include "starboard/system.h"
33 #endif
34 
35 namespace v8 {
36 namespace base {
37 
38 // static
NumberOfProcessors()39 int SysInfo::NumberOfProcessors() {
40 #if V8_OS_OPENBSD
41   int mib[2] = {CTL_HW, HW_NCPU};
42   int ncpu = 0;
43   size_t len = sizeof(ncpu);
44   if (sysctl(mib, arraysize(mib), &ncpu, &len, nullptr, 0) != 0) {
45     return 1;
46   }
47   return ncpu;
48 #elif V8_OS_POSIX
49   long result = sysconf(_SC_NPROCESSORS_ONLN);  // NOLINT(runtime/int)
50   if (result == -1) {
51     return 1;
52   }
53   return static_cast<int>(result);
54 #elif V8_OS_WIN
55   SYSTEM_INFO system_info = {};
56   ::GetNativeSystemInfo(&system_info);
57   return static_cast<int>(system_info.dwNumberOfProcessors);
58 #elif V8_OS_STARBOARD
59   return SbSystemGetNumberOfProcessors();
60 #endif
61 }
62 
63 
64 // static
AmountOfPhysicalMemory()65 int64_t SysInfo::AmountOfPhysicalMemory() {
66 #if V8_OS_DARWIN
67   int mib[2] = {CTL_HW, HW_MEMSIZE};
68   int64_t memsize = 0;
69   size_t len = sizeof(memsize);
70   if (sysctl(mib, arraysize(mib), &memsize, &len, nullptr, 0) != 0) {
71     return 0;
72   }
73   return memsize;
74 #elif V8_OS_FREEBSD
75   int pages, page_size;
76   size_t size = sizeof(pages);
77   sysctlbyname("vm.stats.vm.v_page_count", &pages, &size, nullptr, 0);
78   sysctlbyname("vm.stats.vm.v_page_size", &page_size, &size, nullptr, 0);
79   if (pages == -1 || page_size == -1) {
80     return 0;
81   }
82   return static_cast<int64_t>(pages) * page_size;
83 #elif V8_OS_CYGWIN || V8_OS_WIN
84   MEMORYSTATUSEX memory_info;
85   memory_info.dwLength = sizeof(memory_info);
86   if (!GlobalMemoryStatusEx(&memory_info)) {
87     return 0;
88   }
89   int64_t result = static_cast<int64_t>(memory_info.ullTotalPhys);
90   if (result < 0) result = std::numeric_limits<int64_t>::max();
91   return result;
92 #elif V8_OS_QNX
93   struct stat stat_buf;
94   if (stat("/proc", &stat_buf) != 0) {
95     return 0;
96   }
97   return static_cast<int64_t>(stat_buf.st_size);
98 #elif V8_OS_AIX
99   int64_t result = sysconf(_SC_AIX_REALMEM);
100   return static_cast<int64_t>(result) * 1024L;
101 #elif V8_OS_POSIX
102   long pages = sysconf(_SC_PHYS_PAGES);    // NOLINT(runtime/int)
103   long page_size = sysconf(_SC_PAGESIZE);  // NOLINT(runtime/int)
104   if (pages == -1 || page_size == -1) {
105     return 0;
106   }
107   return static_cast<int64_t>(pages) * page_size;
108 #elif V8_OS_STARBOARD
109   return SbSystemGetTotalCPUMemory();
110 #endif
111 }
112 
113 
114 // static
AmountOfVirtualMemory()115 int64_t SysInfo::AmountOfVirtualMemory() {
116 #if V8_OS_WIN || V8_OS_FUCHSIA
117   return 0;
118 #elif V8_OS_POSIX
119   struct rlimit rlim;
120   int result = getrlimit(RLIMIT_DATA, &rlim);
121   if (result != 0) {
122     return 0;
123   }
124   return (rlim.rlim_cur == RLIM_INFINITY) ? 0 : rlim.rlim_cur;
125 #elif V8_OS_STARBOARD
126   return 0;
127 #endif
128 }
129 
130 }  // namespace base
131 }  // namespace v8
132