From b8b9d10590502c0df9a61747adc73aa2fa19eb9c Mon Sep 17 00:00:00 2001 From: "bmeurer@chromium.org" Date: Wed, 27 Aug 2014 08:29:22 +0000 Subject: [PATCH] Sync our homegrown SysInfo replacement with the one in Chrome base. Also fix several inconsistencies/bugs on the way. TEST=base-unittests R=svenpanne@chromium.org Review URL: https://codereview.chromium.org/510693003 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23435 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- BUILD.gn | 2 + src/base/platform/platform-posix.cc | 71 ---------- src/base/platform/platform-win32.cc | 24 ---- src/base/platform/platform.h | 10 -- src/base/sys-info.cc | 122 ++++++++++++++++++ src/base/sys-info.h | 30 +++++ src/d8.cc | 7 +- src/isolate.cc | 3 +- src/libplatform/default-platform.cc | 6 +- test/base-unittests/base-unittests.gyp | 1 + .../platform/platform-unittest.cc | 5 - test/base-unittests/sys-info-unittest.cc | 26 ++++ tools/gyp/v8.gyp | 2 + 13 files changed, 193 insertions(+), 116 deletions(-) create mode 100644 src/base/sys-info.cc create mode 100644 src/base/sys-info.h create mode 100644 test/base-unittests/sys-info-unittest.cc diff --git a/BUILD.gn b/BUILD.gn index 1ea6cd5cf6..91145dd5e9 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1183,6 +1183,8 @@ source_set("v8_libbase") { "src/base/safe_conversions_impl.h", "src/base/safe_math.h", "src/base/safe_math_impl.h", + "src/base/sys-info.cc", + "src/base/sys-info.h", "src/base/utils/random-number-generator.cc", "src/base/utils/random-number-generator.h", ] diff --git a/src/base/platform/platform-posix.cc b/src/base/platform/platform-posix.cc index 252d213755..ee5b72ef94 100644 --- a/src/base/platform/platform-posix.cc +++ b/src/base/platform/platform-posix.cc @@ -70,77 +70,6 @@ const char* g_gc_fake_mmap = NULL; } // namespace -int OS::NumberOfProcessorsOnline() { - return static_cast(sysconf(_SC_NPROCESSORS_ONLN)); -} - - -// Maximum size of the virtual memory. 0 means there is no artificial -// limit. - -intptr_t OS::MaxVirtualMemory() { - struct rlimit limit; - int result = getrlimit(RLIMIT_DATA, &limit); - if (result != 0) return 0; -#if V8_OS_NACL - // The NaCl compiler doesn't like resource.h constants. - if (static_cast(limit.rlim_cur) == -1) return 0; -#else - if (limit.rlim_cur == RLIM_INFINITY) return 0; -#endif - return limit.rlim_cur; -} - - -uint64_t OS::TotalPhysicalMemory() { -#if V8_OS_MACOSX - int mib[2]; - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - int64_t size = 0; - size_t len = sizeof(size); - if (sysctl(mib, 2, &size, &len, NULL, 0) != 0) { - UNREACHABLE(); - return 0; - } - return static_cast(size); -#elif V8_OS_FREEBSD - int pages, page_size; - size_t size = sizeof(pages); - sysctlbyname("vm.stats.vm.v_page_count", &pages, &size, NULL, 0); - sysctlbyname("vm.stats.vm.v_page_size", &page_size, &size, NULL, 0); - if (pages == -1 || page_size == -1) { - UNREACHABLE(); - return 0; - } - return static_cast(pages) * page_size; -#elif V8_OS_CYGWIN - MEMORYSTATUS memory_info; - memory_info.dwLength = sizeof(memory_info); - if (!GlobalMemoryStatus(&memory_info)) { - UNREACHABLE(); - return 0; - } - return static_cast(memory_info.dwTotalPhys); -#elif V8_OS_QNX - struct stat stat_buf; - if (stat("/proc", &stat_buf) != 0) { - UNREACHABLE(); - return 0; - } - return static_cast(stat_buf.st_size); -#else - intptr_t pages = sysconf(_SC_PHYS_PAGES); - intptr_t page_size = sysconf(_SC_PAGESIZE); - if (pages == -1 || page_size == -1) { - UNREACHABLE(); - return 0; - } - return static_cast(pages) * page_size; -#endif -} - - int OS::ActivationFrameAlignment() { #if V8_TARGET_ARCH_ARM // On EABI ARM targets this is required for fp correctness in the diff --git a/src/base/platform/platform-win32.cc b/src/base/platform/platform-win32.cc index 9f106785eb..dc31ff873d 100644 --- a/src/base/platform/platform-win32.cc +++ b/src/base/platform/platform-win32.cc @@ -113,11 +113,6 @@ bool g_hard_abort = false; } // namespace -intptr_t OS::MaxVirtualMemory() { - return 0; -} - - class TimezoneCache { public: TimezoneCache() : initialized_(false) { } @@ -1168,18 +1163,6 @@ void OS::SignalCodeMovingGC() { } -uint64_t OS::TotalPhysicalMemory() { - MEMORYSTATUSEX memory_info; - memory_info.dwLength = sizeof(memory_info); - if (!GlobalMemoryStatusEx(&memory_info)) { - UNREACHABLE(); - return 0; - } - - return static_cast(memory_info.ullTotalPhys); -} - - #else // __MINGW32__ std::vector OS::GetSharedLibraryAddresses() { return std::vector(); @@ -1190,13 +1173,6 @@ void OS::SignalCodeMovingGC() { } #endif // __MINGW32__ -int OS::NumberOfProcessorsOnline() { - SYSTEM_INFO info; - GetSystemInfo(&info); - return info.dwNumberOfProcessors; -} - - double OS::nan_value() { #ifdef _MSC_VER return std::numeric_limits::quiet_NaN(); diff --git a/src/base/platform/platform.h b/src/base/platform/platform.h index 8a54126268..9e20c084c6 100644 --- a/src/base/platform/platform.h +++ b/src/base/platform/platform.h @@ -284,16 +284,6 @@ class OS { // using --never-compact) if accurate profiling is desired. static void SignalCodeMovingGC(); - // Returns the number of processors online. - static int NumberOfProcessorsOnline(); - - // The total amount of physical memory available on the current system. - static uint64_t TotalPhysicalMemory(); - - // Maximum size of the virtual memory. 0 means there is no artificial - // limit. - static intptr_t MaxVirtualMemory(); - // Returns the double constant NAN static double nan_value(); diff --git a/src/base/sys-info.cc b/src/base/sys-info.cc new file mode 100644 index 0000000000..203b777bee --- /dev/null +++ b/src/base/sys-info.cc @@ -0,0 +1,122 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/base/sys-info.h" + +#if V8_OS_POSIX +#include +#include +#include +#include +#include +#endif + +#if V8_OS_BSD +#include +#endif + +#include + +#include "src/base/logging.h" +#include "src/base/macros.h" +#if V8_OS_WIN +#include "src/base/win32-headers.h" +#endif + +namespace v8 { +namespace base { + +// static +int SysInfo::NumberOfProcessors() { +#if V8_OS_OPENBSD + int mib[2] = {CTL_HW, HW_NCPU}; + int ncpu = 0; + size_t len = sizeof(ncpu); + if (sysctl(mib, arraysize(mib), &ncpu, &len, NULL, 0) != 0) { + UNREACHABLE(); + return 1; + } + return ncpu; +#elif V8_OS_POSIX + long result = sysconf(_SC_NPROCESSORS_ONLN); // NOLINT(runtime/int) + if (result == -1) { + UNREACHABLE(); + return 1; + } + return static_cast(result); +#elif V8_OS_WIN + SYSTEM_INFO system_info = {0}; + ::GetNativeSystemInfo(&system_info); + return static_cast(system_info.dwNumberOfProcessors); +#endif +} + + +// static +int64_t SysInfo::AmountOfPhysicalMemory() { +#if V8_OS_MACOSX + int mib[2] = {CTL_HW, HW_MEMSIZE}; + int64_t memsize = 0; + size_t len = sizeof(memsize); + if (sysctl(mib, arraysize(mib), &memsize, &len, NULL, 0) != 0) { + UNREACHABLE(); + return 0; + } + return memsize; +#elif V8_OS_FREEBSD + int pages, page_size; + size_t size = sizeof(pages); + sysctlbyname("vm.stats.vm.v_page_count", &pages, &size, NULL, 0); + sysctlbyname("vm.stats.vm.v_page_size", &page_size, &size, NULL, 0); + if (pages == -1 || page_size == -1) { + UNREACHABLE(); + return 0; + } + return static_cast(pages) * page_size; +#elif V8_OS_CYGWIN || V8_OS_WIN + MEMORYSTATUSEX memory_info; + memory_info.dwLength = sizeof(memory_info); + if (!GlobalMemoryStatusEx(&memory_info)) { + UNREACHABLE(); + return 0; + } + int64_t result = static_cast(memory_info.ullTotalPhys); + if (result < 0) result = std::numeric_limits::max(); + return result; +#elif V8_OS_QNX + struct stat stat_buf; + if (stat("/proc", &stat_buf) != 0) { + UNREACHABLE(); + return 0; + } + return static_cast(stat_buf.st_size); +#elif V8_OS_POSIX + long pages = sysconf(_SC_PHYS_PAGES); // NOLINT(runtime/int) + long page_size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int) + if (pages == -1 || page_size == -1) { + UNREACHABLE(); + return 0; + } + return static_cast(pages) * page_size; +#endif +} + + +// static +int64_t SysInfo::AmountOfVirtualMemory() { +#if V8_OS_NACL || V8_OS_WIN + return 0; +#elif V8_OS_POSIX + struct rlimit rlim; + int result = getrlimit(RLIMIT_DATA, &rlim); + if (result != 0) { + UNREACHABLE(); + return 0; + } + return (rlim.rlim_cur == RLIM_INFINITY) ? 0 : rlim.rlim_cur; +#endif +} + +} // namespace base +} // namespace v8 diff --git a/src/base/sys-info.h b/src/base/sys-info.h new file mode 100644 index 0000000000..95964d479d --- /dev/null +++ b/src/base/sys-info.h @@ -0,0 +1,30 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_BASE_SYS_INFO_H_ +#define V8_BASE_SYS_INFO_H_ + +#include "include/v8stdint.h" +#include "src/base/build_config.h" + +namespace v8 { +namespace base { + +class SysInfo V8_FINAL { + public: + // Returns the number of logical processors/core on the current machine. + static int NumberOfProcessors(); + + // Returns the number of bytes of physical memory on the current machine. + static int64_t AmountOfPhysicalMemory(); + + // Returns the number of bytes of virtual memory of this process. A return + // value of zero means that there is no limit on the available virtual memory. + static int64_t AmountOfVirtualMemory(); +}; + +} // namespace base +} // namespace v8 + +#endif // V8_BASE_SYS_INFO_H_ diff --git a/src/d8.cc b/src/d8.cc index d0dfbbb364..6ae2ceb8a2 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -45,6 +45,7 @@ #include "src/base/cpu.h" #include "src/base/logging.h" #include "src/base/platform/platform.h" +#include "src/base/sys-info.h" #include "src/d8-debug.h" #include "src/debug.h" #include "src/natives.h" @@ -1614,9 +1615,9 @@ int Shell::Main(int argc, char* argv[]) { Isolate* isolate = Isolate::New(); #ifndef V8_SHARED v8::ResourceConstraints constraints; - constraints.ConfigureDefaults(base::OS::TotalPhysicalMemory(), - base::OS::MaxVirtualMemory(), - base::OS::NumberOfProcessorsOnline()); + constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), + base::SysInfo::AmountOfVirtualMemory(), + base::SysInfo::NumberOfProcessors()); v8::SetResourceConstraints(isolate, &constraints); #endif DumbLineEditor dumb_line_editor(isolate); diff --git a/src/isolate.cc b/src/isolate.cc index 9b13982de4..13dea488b4 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -8,6 +8,7 @@ #include "src/ast.h" #include "src/base/platform/platform.h" +#include "src/base/sys-info.h" #include "src/base/utils/random-number-generator.h" #include "src/bootstrapper.h" #include "src/codegen.h" @@ -1947,7 +1948,7 @@ bool Isolate::Init(Deserializer* des) { if (max_available_threads_ < 1) { // Choose the default between 1 and 4. max_available_threads_ = - Max(Min(base::OS::NumberOfProcessorsOnline(), 4), 1); + Max(Min(base::SysInfo::NumberOfProcessors(), 4), 1); } if (!FLAG_job_based_sweeping) { diff --git a/src/libplatform/default-platform.cc b/src/libplatform/default-platform.cc index 9a503bc34c..b5b8571dbc 100644 --- a/src/libplatform/default-platform.cc +++ b/src/libplatform/default-platform.cc @@ -9,6 +9,7 @@ #include "src/base/logging.h" #include "src/base/platform/platform.h" +#include "src/base/sys-info.h" #include "src/libplatform/worker-thread.h" namespace v8 { @@ -58,8 +59,9 @@ DefaultPlatform::~DefaultPlatform() { void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { base::LockGuard guard(&lock_); DCHECK(thread_pool_size >= 0); - if (thread_pool_size < 1) - thread_pool_size = base::OS::NumberOfProcessorsOnline(); + if (thread_pool_size < 1) { + thread_pool_size = base::SysInfo::NumberOfProcessors(); + } thread_pool_size_ = std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); } diff --git a/test/base-unittests/base-unittests.gyp b/test/base-unittests/base-unittests.gyp index 6b69f1c984..1cf638f9d8 100644 --- a/test/base-unittests/base-unittests.gyp +++ b/test/base-unittests/base-unittests.gyp @@ -27,6 +27,7 @@ 'platform/platform-unittest.cc', 'platform/semaphore-unittest.cc', 'platform/time-unittest.cc', + 'sys-info-unittest.cc', 'utils/random-number-generator-unittest.cc', ], 'conditions': [ diff --git a/test/base-unittests/platform/platform-unittest.cc b/test/base-unittests/platform/platform-unittest.cc index 38b1eb92b6..b0cd4e18f7 100644 --- a/test/base-unittests/platform/platform-unittest.cc +++ b/test/base-unittests/platform/platform-unittest.cc @@ -28,11 +28,6 @@ TEST(OS, GetCurrentProcessId) { } -TEST(OS, NumberOfProcessorsOnline) { - EXPECT_GT(OS::NumberOfProcessorsOnline(), 0); -} - - namespace { class SelfJoinThread V8_FINAL : public Thread { diff --git a/test/base-unittests/sys-info-unittest.cc b/test/base-unittests/sys-info-unittest.cc new file mode 100644 index 0000000000..10bd91bb01 --- /dev/null +++ b/test/base-unittests/sys-info-unittest.cc @@ -0,0 +1,26 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/base/sys-info.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace v8 { +namespace base { + +TEST(SysInfoTest, NumberOfProcessors) { + EXPECT_LT(0, SysInfo::NumberOfProcessors()); +} + + +TEST(SysInfoTest, AmountOfPhysicalMemory) { + EXPECT_LT(0, SysInfo::AmountOfPhysicalMemory()); +} + + +TEST(SysInfoTest, AmountOfVirtualMemory) { + EXPECT_LE(0, SysInfo::AmountOfVirtualMemory()); +} + +} // namespace base +} // namespace v8 diff --git a/tools/gyp/v8.gyp b/tools/gyp/v8.gyp index dc812854af..96d9c9cd4e 100644 --- a/tools/gyp/v8.gyp +++ b/tools/gyp/v8.gyp @@ -1150,6 +1150,8 @@ '../../src/base/safe_conversions_impl.h', '../../src/base/safe_math.h', '../../src/base/safe_math_impl.h', + '../../src/base/sys-info.cc', + '../../src/base/sys-info.h', '../../src/base/utils/random-number-generator.cc', '../../src/base/utils/random-number-generator.h', ],