Reviewers: Benedikt Meurer,

Message:
Benedikt: please review this CL.

Description:
Avoid calling OS::TotalPhysicalMemory in defaults.cc, since this fails in the
chrome renderer sandbox.

Add an InitializeDefaultsForCurrentPlatform, which the embedder must call before
requesting platform-default ResourceConstraints.

BUG=None

Please review this at https://codereview.chromium.org/40233002/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files (+34, -5 lines):
  M include/v8-defaults.h
  M src/d8.cc
  M src/defaults.cc


Index: include/v8-defaults.h
diff --git a/include/v8-defaults.h b/include/v8-defaults.h
index 381a48210d1b9895fbda4e4c89eb0e51fa61b6d0..4edba558a186a7e572cfe58058631be5b886dd52 100644
--- a/include/v8-defaults.h
+++ b/include/v8-defaults.h
@@ -35,6 +35,16 @@
  */
 namespace v8 {

+
+/**
+ * Initializes the v8-defaults subsystem.  Should be called before
+ * ConfigureResourceConstraintsForCurrentPlatform or
+ * SetDefaultResourceConstraintsForCurrentPlatform.
+ */
+void V8_EXPORT InitializeDefaultsForCurrentPlatform(
+    uint64_t total_physical_memory);
+
+
 /**
  * Configures the constraints with reasonable default values based on the
  * capabilities of the current device the VM is running on.
Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index 96407a897c98b1284a3f9b1dd78bb457c5b08bd7..818c7e9097558f2a9142ecfe6ea8f3404e6a2372 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -1660,7 +1660,8 @@ int Shell::Main(int argc, char* argv[]) {
 #else
   SetStandaloneFlagsViaCommandLine();
 #endif
-  v8::SetDefaultResourceConstraintsForCurrentPlatform();
+  v8::InitializeDefaultsForCurrentPlatform(i::OS::TotalPhysicalMemory());
+  ASSERT(v8::SetDefaultResourceConstraintsForCurrentPlatform());
   ShellArrayBufferAllocator array_buffer_allocator;
   v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
   int result = 0;
Index: src/defaults.cc
diff --git a/src/defaults.cc b/src/defaults.cc
index cbbe53729ebd171772106b589ae8b8c6fb4488d1..196ec776af1111406bac5a97aee10cc0b18a4ac2 100644
--- a/src/defaults.cc
+++ b/src/defaults.cc
@@ -30,12 +30,14 @@
 #undef USING_V8_SHARED
 #include "../include/v8-defaults.h"

+#include "lazy-instance.h"
 #include "platform.h"
 #include "globals.h"
 #include "v8.h"

 namespace v8 {

+namespace internal {

 #if V8_OS_ANDROID
 const bool kOsHasSwap = false;
@@ -43,6 +45,17 @@ const bool kOsHasSwap = false;
 const bool kOsHasSwap = true;
 #endif

+static uint64_t g_total_physical_memory = 0;
+static LazyInstance<Mutex>::type g_mutex = LAZY_INSTANCE_INITIALIZER;
+
+}
+
+
+void InitializeDefaultsForCurrentPlatform(uint64_t total_physical_memory) {
+  i::LockGuard<i::Mutex>(i::g_mutex.Pointer());
+  i::g_total_physical_memory = total_physical_memory;
+}
+

 bool ConfigureResourceConstraintsForCurrentPlatform(
     ResourceConstraints* constraints) {
@@ -50,20 +63,25 @@ bool ConfigureResourceConstraintsForCurrentPlatform(
     return false;
   }

-  uint64_t physical_memory = i::OS::TotalPhysicalMemory();
+  i::LockGuard<i::Mutex>(i::g_mutex.Pointer());
+  uint64_t physical_memory = i::g_total_physical_memory;
   int lump_of_memory = (i::kPointerSize / 4) * i::MB;

// The young_space_size should be a power of 2 and old_generation_size should
   // be a multiple of Page::kPageSize.
-  if (physical_memory <= 512ul * i::MB) {
+  if (physical_memory == 0) {
+    // InitializeDefaultsForCurrentPlatform has not been called, leave
+ // ResourceConstraints empty so that non-platform default values are used.
+    return false;
+  } else if (physical_memory <= 512ul * i::MB) {
     constraints->set_max_young_space_size(2 * lump_of_memory);
     constraints->set_max_old_space_size(128 * lump_of_memory);
     constraints->set_max_executable_size(96 * lump_of_memory);
- } else if (physical_memory <= (kOsHasSwap ? 768ul * i::MB : 1ul * i::GB)) { + } else if (physical_memory <= (i::kOsHasSwap ? 768ul * i::MB : 1ul * i::GB)) {
     constraints->set_max_young_space_size(8 * lump_of_memory);
     constraints->set_max_old_space_size(256 * lump_of_memory);
     constraints->set_max_executable_size(192 * lump_of_memory);
-  } else if (physical_memory <= (kOsHasSwap ? 1ul * i::GB : 2ul * i::GB)) {
+ } else if (physical_memory <= (i::kOsHasSwap ? 1ul * i::GB : 2ul * i::GB)) {
     constraints->set_max_young_space_size(16 * lump_of_memory);
     constraints->set_max_old_space_size(512 * lump_of_memory);
     constraints->set_max_executable_size(256 * lump_of_memory);


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to