Reviewers: mstarzinger_chroium.org,

Description:
Allow the embedder to pass the virtual memory limit to v8

The getrlimit() call might be sandboxed, so it's not safe to use it.

BUG=none
[email protected]
LOG=y

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

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

Affected files (+40, -18 lines):
  M include/v8.h
  M src/api.cc
  M src/d8.cc
  M src/heap.h
  M src/heap.cc
  M test/cctest/test-api.cc
  M test/cctest/test-mark-compact.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 392a3615dd556016f38b7e6840f6a009a3f5c831..98f9dd5d959980cea7d523ade87d91478c7de8dc 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -3951,11 +3951,19 @@ class V8_EXPORT ResourceConstraints {
    *
* \param physical_memory The total amount of physical memory on the current
    *   device, in bytes.
+ * \param virtual_memory_limit The amount of virtual memory on the current
+   *   device, in bytes, or zero, if there is no limit.
* \param number_of_processors The number of CPUs available on the current
    *   device.
    */
   void ConfigureDefaults(uint64_t physical_memory,
+                         uint64_t virtual_memory_limit,
                          uint32_t number_of_processors);
+  // Deprecated.
+  void ConfigureDefaults(uint64_t physical_memory,
+                         uint32_t number_of_processors) {
+    ConfigureDefaults(physical_memory, 0, number_of_processors);
+  }

   int max_young_space_size() const { return max_young_space_size_; }
void set_max_young_space_size(int value) { max_young_space_size_ = value; }
@@ -3971,6 +3979,10 @@ class V8_EXPORT ResourceConstraints {
   void set_max_available_threads(int value) {
     max_available_threads_ = value;
   }
+  int code_range_size() const { return code_range_size_; }
+  void set_code_range_size(int value) {
+    code_range_size_ = value;
+  }

  private:
   int max_young_space_size_;
@@ -3978,6 +3990,7 @@ class V8_EXPORT ResourceConstraints {
   int max_executable_size_;
   uint32_t* stack_limit_;
   int max_available_threads_;
+  int code_range_size_;
 };


Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index eafc86c2d7f5752f6ef37a78fffffced490575cf..05fb0a9a3fa33c1de0e4e541fa31a4d2d055ec5b 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -464,9 +464,11 @@ ResourceConstraints::ResourceConstraints()
       max_old_space_size_(0),
       max_executable_size_(0),
       stack_limit_(NULL),
-      max_available_threads_(0) { }
+      max_available_threads_(0),
+      code_range_size_(i::kIs64BitArch ? 512 * i::MB : 0) { }

 void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
+                                            uint64_t virtual_memory_limit,
uint32_t number_of_processors) {
   const int lump_of_memory = (i::kPointerSize / 4) * i::MB;
 #if V8_OS_ANDROID
@@ -502,6 +504,12 @@ void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
   }

   set_max_available_threads(i::Max(i::Min(number_of_processors, 4u), 1u));
+
+  if (virtual_memory_limit > 0 && code_range_size() > 0) {
+    // Reserve no more than 1/8 of the memory for the code range.
+    set_code_range_size(
+ i::Min(code_range_size(), static_cast<int>(virtual_memory_limit >> 3)));
+  }
 }


@@ -511,12 +519,15 @@ bool SetResourceConstraints(Isolate* v8_isolate,
   int young_space_size = constraints->max_young_space_size();
   int old_gen_size = constraints->max_old_space_size();
   int max_executable_size = constraints->max_executable_size();
- if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0) {
+  int code_range_size = constraints->code_range_size();
+ if (young_space_size != 0 || old_gen_size != 0 || max_executable_size != 0 ||
+      code_range_size != 0) {
     // After initialization it's too late to change Heap constraints.
     ASSERT(!isolate->IsInitialized());
     bool result = isolate->heap()->ConfigureHeap(young_space_size / 2,
                                                  old_gen_size,
-                                                 max_executable_size);
+                                                 max_executable_size,
+                                                 code_range_size);
     if (!result) return false;
   }
   if (constraints->stack_limit() != NULL) {
Index: src/d8.cc
diff --git a/src/d8.cc b/src/d8.cc
index 7ac0c6546aae188ed931306095f824568a7049b1..4e14cd5b0aa65f1607f3b2a9491f3970a4e95095 100644
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -1696,6 +1696,7 @@ int Shell::Main(int argc, char* argv[]) {
 #ifndef V8_SHARED
   v8::ResourceConstraints constraints;
   constraints.ConfigureDefaults(i::OS::TotalPhysicalMemory(),
+                                i::OS::MaxVirtualMemory(),
                                 i::CPU::NumberOfProcessorsOnline());
   v8::SetResourceConstraints(isolate, &constraints);
 #endif
Index: src/heap.cc
diff --git a/src/heap.cc b/src/heap.cc
index 56a0dd7e6d81c98e59d7e6fc401f4ad072f6b930..d9ba758351ecd3bf7c677aca74750d4ec82c4045 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -68,7 +68,7 @@ namespace internal {

 Heap::Heap()
     : isolate_(NULL),
-      code_range_size_(kIs64BitArch ? 512 * MB : 0),
+      code_range_size_(0),
// semispace_size_ should be a power of 2 and old_generation_size_ should be
 // a multiple of Page::kPageSize.
       reserved_semispace_size_(8 * (kPointerSize / 4) * MB),
@@ -165,15 +165,6 @@ Heap::Heap()
   // Ensure old_generation_size_ is a multiple of kPageSize.
   ASSERT(MB >= Page::kPageSize);

-  intptr_t max_virtual = OS::MaxVirtualMemory();
-
-  if (max_virtual > 0) {
-    if (code_range_size_ > 0) {
-      // Reserve no more than 1/8 of the memory for the code range.
-      code_range_size_ = Min(code_range_size_, max_virtual >> 3);
-    }
-  }
-
   memset(roots_, 0, sizeof(roots_[0]) * kRootListLength);
   native_contexts_list_ = NULL;
   array_buffers_list_ = Smi::FromInt(0);
@@ -6227,7 +6218,8 @@ void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) {
 // size is not big enough to fit all the initial objects.
 bool Heap::ConfigureHeap(int max_semispace_size,
                          intptr_t max_old_gen_size,
-                         intptr_t max_executable_size) {
+                         intptr_t max_executable_size,
+                         intptr_t code_range_size) {
   if (HasBeenSetUp()) return false;

   if (FLAG_stress_compaction) {
@@ -6301,6 +6293,8 @@ bool Heap::ConfigureHeap(int max_semispace_size,
           FixedArray::SizeFor(JSObject::kInitialMaxFastElementArray) +
           AllocationMemento::kSize));

+  code_range_size_ = code_range_size;
+
   configured_ = true;
   return true;
 }
@@ -6309,7 +6303,8 @@ bool Heap::ConfigureHeap(int max_semispace_size,
 bool Heap::ConfigureHeapDefault() {
return ConfigureHeap(static_cast<intptr_t>(FLAG_max_new_space_size / 2) * KB,
                        static_cast<intptr_t>(FLAG_max_old_space_size) * MB,
- static_cast<intptr_t>(FLAG_max_executable_size) * MB); + static_cast<intptr_t>(FLAG_max_executable_size) * MB,
+                       static_cast<intptr_t>(kIs64BitArch ? 512 * MB : 0));
 }


Index: src/heap.h
diff --git a/src/heap.h b/src/heap.h
index 758213719c3792678e8b2f89446ef6d34c53e5b9..41ef89123cc86a44d8217ffc78505a903bd8b8e2 100644
--- a/src/heap.h
+++ b/src/heap.h
@@ -570,7 +570,8 @@ class Heap {
   // set up already.
   bool ConfigureHeap(int max_semispace_size,
                      intptr_t max_old_gen_size,
-                     intptr_t max_executable_size);
+                     intptr_t max_executable_size,
+                     intptr_t code_range_size);
   bool ConfigureHeapDefault();

// Prepares the heap, setting up memory areas that are needed in the isolate
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index f13a0a6e7eba58a2e5221b731c870341bf9ae46b..2a9795d6ca59f37513a99d31dc9523d509df2e4d 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -17769,6 +17769,7 @@ TEST(SetResourceConstraintsInThread) {
     // Set stack limit.
     v8::ResourceConstraints constraints;
     constraints.set_stack_limit(set_limit);
+    constraints.set_code_range_size(0);
     CHECK(v8::SetResourceConstraints(CcTest::isolate(), &constraints));

     // Execute a script.
Index: test/cctest/test-mark-compact.cc
diff --git a/test/cctest/test-mark-compact.cc b/test/cctest/test-mark-compact.cc index 1a95ae172b68584d8ab935716b705e5639a9ce42..785c77d078fd70e496a332a97cfb28c020118951 100644
--- a/test/cctest/test-mark-compact.cc
+++ b/test/cctest/test-mark-compact.cc
@@ -76,7 +76,7 @@ TEST(MarkingDeque) {
 TEST(Promotion) {
   CcTest::InitializeVM();
   Heap* heap = CcTest::heap();
-  heap->ConfigureHeap(2*256*KB, 1*MB, 1*MB);
+  heap->ConfigureHeap(2*256*KB, 1*MB, 1*MB, 0);

   v8::HandleScope sc(CcTest::isolate());

@@ -101,7 +101,7 @@ TEST(Promotion) {
 TEST(NoPromotion) {
   CcTest::InitializeVM();
   Heap* heap = CcTest::heap();
-  heap->ConfigureHeap(2*256*KB, 1*MB, 1*MB);
+  heap->ConfigureHeap(2*256*KB, 1*MB, 1*MB, 0);

   v8::HandleScope sc(CcTest::isolate());



--
--
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/d/optout.

Reply via email to