Sync our homegrown SysInfo replacement with the one in Chrome base.
authorbmeurer@chromium.org <bmeurer@chromium.org>
Wed, 27 Aug 2014 08:29:22 +0000 (08:29 +0000)
committerbmeurer@chromium.org <bmeurer@chromium.org>
Wed, 27 Aug 2014 08:29:22 +0000 (08:29 +0000)
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

13 files changed:
BUILD.gn
src/base/platform/platform-posix.cc
src/base/platform/platform-win32.cc
src/base/platform/platform.h
src/base/sys-info.cc [new file with mode: 0644]
src/base/sys-info.h [new file with mode: 0644]
src/d8.cc
src/isolate.cc
src/libplatform/default-platform.cc
test/base-unittests/base-unittests.gyp
test/base-unittests/platform/platform-unittest.cc
test/base-unittests/sys-info-unittest.cc [new file with mode: 0644]
tools/gyp/v8.gyp

index 1ea6cd5..91145dd 100644 (file)
--- 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",
   ]
index 252d213..ee5b72e 100644 (file)
@@ -70,77 +70,6 @@ const char* g_gc_fake_mmap = NULL;
 }  // namespace
 
 
-int OS::NumberOfProcessorsOnline() {
-  return static_cast<int>(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<int>(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<uint64_t>(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<uint64_t>(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<uint64_t>(memory_info.dwTotalPhys);
-#elif V8_OS_QNX
-  struct stat stat_buf;
-  if (stat("/proc", &stat_buf) != 0) {
-    UNREACHABLE();
-    return 0;
-  }
-  return static_cast<uint64_t>(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<uint64_t>(pages) * page_size;
-#endif
-}
-
-
 int OS::ActivationFrameAlignment() {
 #if V8_TARGET_ARCH_ARM
   // On EABI ARM targets this is required for fp correctness in the
index 9f10678..dc31ff8 100644 (file)
@@ -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<uint64_t>(memory_info.ullTotalPhys);
-}
-
-
 #else  // __MINGW32__
 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
   return std::vector<OS::SharedLibraryAddress>();
@@ -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<double>::quiet_NaN();
index 8a54126..9e20c08 100644 (file)
@@ -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 (file)
index 0000000..203b777
--- /dev/null
@@ -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 <sys/resource.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+#if V8_OS_BSD
+#include <sys/sysctl.h>
+#endif
+
+#include <limits>
+
+#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<int>(result);
+#elif V8_OS_WIN
+  SYSTEM_INFO system_info = {0};
+  ::GetNativeSystemInfo(&system_info);
+  return static_cast<int>(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<int64_t>(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<int64_t>(memory_info.ullTotalPhys);
+  if (result < 0) result = std::numeric_limits<int64_t>::max();
+  return result;
+#elif V8_OS_QNX
+  struct stat stat_buf;
+  if (stat("/proc", &stat_buf) != 0) {
+    UNREACHABLE();
+    return 0;
+  }
+  return static_cast<int64_t>(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<int64_t>(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 (file)
index 0000000..95964d4
--- /dev/null
@@ -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_
index d0dfbbb..6ae2ceb 100644 (file)
--- 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);
index 9b13982..13dea48 100644 (file)
@@ -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) {
index 9a503bc..b5b8571 100644 (file)
@@ -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<base::Mutex> 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);
 }
index 6b69f1c..1cf638f 100644 (file)
@@ -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': [
index 38b1eb9..b0cd4e1 100644 (file)
@@ -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 (file)
index 0000000..10bd91b
--- /dev/null
@@ -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
index dc81285..96d9c9c 100644 (file)
         '../../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',
       ],