blob: a2dc6ab27fc7e8f649dfd2dcc005092ee404de09 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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/resource.h>
9#include <sys/stat.h>
10#include <sys/time.h>
11#include <sys/types.h>
12#include <unistd.h>
13#endif
14
15#if V8_OS_BSD
16#include <sys/sysctl.h>
17#endif
18
19#include <limits>
20
21#include "src/base/logging.h"
22#include "src/base/macros.h"
23#if V8_OS_WIN
24#include "src/base/win32-headers.h"
25#endif
26
27namespace v8 {
28namespace base {
29
30// static
31int SysInfo::NumberOfProcessors() {
32#if V8_OS_OPENBSD
33 int mib[2] = {CTL_HW, HW_NCPU};
34 int ncpu = 0;
35 size_t len = sizeof(ncpu);
36 if (sysctl(mib, arraysize(mib), &ncpu, &len, NULL, 0) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 return 1;
38 }
39 return ncpu;
40#elif V8_OS_POSIX
41 long result = sysconf(_SC_NPROCESSORS_ONLN); // NOLINT(runtime/int)
42 if (result == -1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 return 1;
44 }
45 return static_cast<int>(result);
46#elif V8_OS_WIN
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 SYSTEM_INFO system_info = {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 ::GetNativeSystemInfo(&system_info);
49 return static_cast<int>(system_info.dwNumberOfProcessors);
50#endif
51}
52
53
54// static
55int64_t SysInfo::AmountOfPhysicalMemory() {
56#if V8_OS_MACOSX
57 int mib[2] = {CTL_HW, HW_MEMSIZE};
58 int64_t memsize = 0;
59 size_t len = sizeof(memsize);
60 if (sysctl(mib, arraysize(mib), &memsize, &len, NULL, 0) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 return 0;
62 }
63 return memsize;
64#elif V8_OS_FREEBSD
65 int pages, page_size;
66 size_t size = sizeof(pages);
67 sysctlbyname("vm.stats.vm.v_page_count", &pages, &size, NULL, 0);
68 sysctlbyname("vm.stats.vm.v_page_size", &page_size, &size, NULL, 0);
69 if (pages == -1 || page_size == -1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 return 0;
71 }
72 return static_cast<int64_t>(pages) * page_size;
73#elif V8_OS_CYGWIN || V8_OS_WIN
74 MEMORYSTATUSEX memory_info;
75 memory_info.dwLength = sizeof(memory_info);
76 if (!GlobalMemoryStatusEx(&memory_info)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 return 0;
78 }
79 int64_t result = static_cast<int64_t>(memory_info.ullTotalPhys);
80 if (result < 0) result = std::numeric_limits<int64_t>::max();
81 return result;
82#elif V8_OS_QNX
83 struct stat stat_buf;
84 if (stat("/proc", &stat_buf) != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 return 0;
86 }
87 return static_cast<int64_t>(stat_buf.st_size);
88#elif V8_OS_NACL
89 // No support for _SC_PHYS_PAGES, assume 2GB.
90 return static_cast<int64_t>(1) << 31;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091#elif V8_OS_AIX
92 int64_t result = sysconf(_SC_AIX_REALMEM);
93 return static_cast<int64_t>(result) * 1024L;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094#elif V8_OS_POSIX
95 long pages = sysconf(_SC_PHYS_PAGES); // NOLINT(runtime/int)
96 long page_size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
97 if (pages == -1 || page_size == -1) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 return 0;
99 }
100 return static_cast<int64_t>(pages) * page_size;
101#endif
102}
103
104
105// static
106int64_t SysInfo::AmountOfVirtualMemory() {
107#if V8_OS_NACL || V8_OS_WIN
108 return 0;
109#elif V8_OS_POSIX
110 struct rlimit rlim;
111 int result = getrlimit(RLIMIT_DATA, &rlim);
112 if (result != 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 return 0;
114 }
115 return (rlim.rlim_cur == RLIM_INFINITY) ? 0 : rlim.rlim_cur;
116#endif
117}
118
119} // namespace base
120} // namespace v8