V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
sys-info.cc
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 "src/base/win32-headers.h"
27 #endif
28 
29 namespace v8 {
30 namespace base {
31 
32 // static
33 int SysInfo::NumberOfProcessors() {
34 #if V8_OS_OPENBSD
35  int mib[2] = {CTL_HW, HW_NCPU};
36  int ncpu = 0;
37  size_t len = sizeof(ncpu);
38  if (sysctl(mib, arraysize(mib), &ncpu, &len, nullptr, 0) != 0) {
39  return 1;
40  }
41  return ncpu;
42 #elif V8_OS_POSIX
43  long result = sysconf(_SC_NPROCESSORS_ONLN); // NOLINT(runtime/int)
44  if (result == -1) {
45  return 1;
46  }
47  return static_cast<int>(result);
48 #elif V8_OS_WIN
49  SYSTEM_INFO system_info = {};
50  ::GetNativeSystemInfo(&system_info);
51  return static_cast<int>(system_info.dwNumberOfProcessors);
52 #endif
53 }
54 
55 
56 // static
57 int64_t SysInfo::AmountOfPhysicalMemory() {
58 #if V8_OS_MACOSX
59  int mib[2] = {CTL_HW, HW_MEMSIZE};
60  int64_t memsize = 0;
61  size_t len = sizeof(memsize);
62  if (sysctl(mib, arraysize(mib), &memsize, &len, nullptr, 0) != 0) {
63  return 0;
64  }
65  return memsize;
66 #elif V8_OS_FREEBSD
67  int pages, page_size;
68  size_t size = sizeof(pages);
69  sysctlbyname("vm.stats.vm.v_page_count", &pages, &size, nullptr, 0);
70  sysctlbyname("vm.stats.vm.v_page_size", &page_size, &size, nullptr, 0);
71  if (pages == -1 || page_size == -1) {
72  return 0;
73  }
74  return static_cast<int64_t>(pages) * page_size;
75 #elif V8_OS_CYGWIN || V8_OS_WIN
76  MEMORYSTATUSEX memory_info;
77  memory_info.dwLength = sizeof(memory_info);
78  if (!GlobalMemoryStatusEx(&memory_info)) {
79  return 0;
80  }
81  int64_t result = static_cast<int64_t>(memory_info.ullTotalPhys);
82  if (result < 0) result = std::numeric_limits<int64_t>::max();
83  return result;
84 #elif V8_OS_QNX
85  struct stat stat_buf;
86  if (stat("/proc", &stat_buf) != 0) {
87  return 0;
88  }
89  return static_cast<int64_t>(stat_buf.st_size);
90 #elif V8_OS_AIX
91  int64_t result = sysconf(_SC_AIX_REALMEM);
92  return static_cast<int64_t>(result) * 1024L;
93 #elif V8_OS_POSIX
94  long pages = sysconf(_SC_PHYS_PAGES); // NOLINT(runtime/int)
95  long page_size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
96  if (pages == -1 || page_size == -1) {
97  return 0;
98  }
99  return static_cast<int64_t>(pages) * page_size;
100 #endif
101 }
102 
103 
104 // static
105 int64_t SysInfo::AmountOfVirtualMemory() {
106 #if V8_OS_WIN || V8_OS_FUCHSIA
107  return 0;
108 #elif V8_OS_POSIX
109  struct rlimit rlim;
110  int result = getrlimit(RLIMIT_DATA, &rlim);
111  if (result != 0) {
112  return 0;
113  }
114  return (rlim.rlim_cur == RLIM_INFINITY) ? 0 : rlim.rlim_cur;
115 #endif
116 }
117 
118 } // namespace base
119 } // namespace v8
Definition: libplatform.h:13