Skip to content

Commit e423f00

Browse files
ulanCommit Bot
authored andcommitted
[api] Add a way to specify the max heap size in ResourceConstraints
The new API function is called ConfigureDefaultsFromHeapSize and accepts two parameters: the initial and the maximum heap size. Based on the given limits the function computes the default size for the young and the old generation. The patch also cleans up the existing functions to make them consistent in terms of units and heap structure. Bug: v8:9306 Change-Id: If2200a9cdb45b0b818a373207efe4e6426f7b688 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1631593 Commit-Queue: Ulan Degenbaev <[email protected]> Reviewed-by: Jakob Gruber <[email protected]> Reviewed-by: Michael Lippautz <[email protected]> Cr-Commit-Position: refs/heads/master@{#62017}
1 parent ea79fa4 commit e423f00

12 files changed

Lines changed: 381 additions & 121 deletions

File tree

include/v8.h

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6405,7 +6405,19 @@ V8_INLINE Local<Boolean> False(Isolate* isolate);
64056405
*/
64066406
class V8_EXPORT ResourceConstraints {
64076407
public:
6408-
ResourceConstraints();
6408+
/**
6409+
* Configures the constraints with reasonable default values based on the
6410+
* provided heap size limit. The heap size includes both the young and
6411+
* the old generation.
6412+
*
6413+
* \param maximum_heap_size_in_bytes The hard limit for the heap size.
6414+
* When the heap size approaches this limit, V8 will perform series of
6415+
* garbage collections and invoke the NearHeapLimitCallback.
6416+
* If the garbage collections do not help and the callback does not
6417+
* increase the limit, then V8 will crash with V8::FatalProcessOutOfMemory.
6418+
*/
6419+
void ConfigureDefaultsFromHeapSize(size_t initial_heap_size_in_bytes,
6420+
size_t maximum_heap_size_in_bytes);
64096421

64106422
/**
64116423
* Configures the constraints with reasonable default values based on the
@@ -6419,26 +6431,81 @@ class V8_EXPORT ResourceConstraints {
64196431
void ConfigureDefaults(uint64_t physical_memory,
64206432
uint64_t virtual_memory_limit);
64216433

6422-
// Returns the max semi-space size in KB.
6423-
size_t max_semi_space_size_in_kb() const {
6424-
return max_semi_space_size_in_kb_;
6434+
/**
6435+
* The address beyond which the VM's stack may not grow.
6436+
*/
6437+
uint32_t* stack_limit() const { return stack_limit_; }
6438+
void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
6439+
6440+
/**
6441+
* The amount of virtual memory reserved for generated code. This is relevant
6442+
* for 64-bit architectures that rely on code range for calls in code.
6443+
*/
6444+
size_t code_range_size_in_bytes() const { return code_range_size_; }
6445+
void set_code_range_size_in_bytes(size_t limit) { code_range_size_ = limit; }
6446+
6447+
/**
6448+
* The maximum size of the old generation.
6449+
* When the old generation approaches this limit, V8 will perform series of
6450+
* garbage collections and invoke the NearHeapLimitCallback.
6451+
* If the garbage collections do not help and the callback does not
6452+
* increase the limit, then V8 will crash with V8::FatalProcessOutOfMemory.
6453+
*/
6454+
size_t max_old_generation_size_in_bytes() const {
6455+
return max_old_generation_size_;
6456+
}
6457+
void set_max_old_generation_size_in_bytes(size_t limit) {
6458+
max_old_generation_size_ = limit;
6459+
}
6460+
6461+
/**
6462+
* The maximum size of the young generation, which consists of two semi-spaces
6463+
* and a large object space. This affects frequency of Scavenge garbage
6464+
* collections and should be typically much smaller that the old generation.
6465+
*/
6466+
size_t max_young_generation_size_in_bytes() const {
6467+
return max_young_generation_size_;
6468+
}
6469+
void set_max_young_generation_size_in_bytes(size_t limit) {
6470+
max_young_generation_size_ = limit;
64256471
}
64266472

6427-
// Sets the max semi-space size in KB.
6428-
void set_max_semi_space_size_in_kb(size_t limit_in_kb) {
6429-
max_semi_space_size_in_kb_ = limit_in_kb;
6473+
size_t initial_old_generation_size_in_bytes() const {
6474+
return initial_old_generation_size_;
6475+
}
6476+
void set_initial_old_generation_size_in_bytes(size_t initial_size) {
6477+
initial_old_generation_size_ = initial_size;
64306478
}
64316479

6432-
size_t max_old_space_size() const { return max_old_space_size_; }
6433-
void set_max_old_space_size(size_t limit_in_mb) {
6434-
max_old_space_size_ = limit_in_mb;
6480+
size_t initial_young_generation_size_in_bytes() const {
6481+
return initial_young_generation_size_;
64356482
}
6436-
uint32_t* stack_limit() const { return stack_limit_; }
6437-
// Sets an address beyond which the VM's stack may not grow.
6438-
void set_stack_limit(uint32_t* value) { stack_limit_ = value; }
6439-
size_t code_range_size() const { return code_range_size_; }
6440-
void set_code_range_size(size_t limit_in_mb) {
6441-
code_range_size_ = limit_in_mb;
6483+
void set_initial_young_generation_size_in_bytes(size_t initial_size) {
6484+
initial_young_generation_size_ = initial_size;
6485+
}
6486+
6487+
/**
6488+
* Deprecated functions. Do not use in new code.
6489+
*/
6490+
V8_DEPRECATE_SOON("Use code_range_size_in_bytes.",
6491+
size_t code_range_size() const) {
6492+
return code_range_size_ / kMB;
6493+
}
6494+
V8_DEPRECATE_SOON("Use set_code_range_size_in_bytes.",
6495+
void set_code_range_size(size_t limit_in_mb)) {
6496+
code_range_size_ = limit_in_mb * kMB;
6497+
}
6498+
V8_DEPRECATE_SOON("Use max_young_generation_size_in_bytes.",
6499+
size_t max_semi_space_size_in_kb() const);
6500+
V8_DEPRECATE_SOON("Use set_max_young_generation_size_in_bytes.",
6501+
void set_max_semi_space_size_in_kb(size_t limit_in_kb));
6502+
V8_DEPRECATE_SOON("Use max_old_generation_size_in_bytes.",
6503+
size_t max_old_space_size() const) {
6504+
return max_old_generation_size_ / kMB;
6505+
}
6506+
V8_DEPRECATE_SOON("Use set_max_old_generation_size_in_bytes.",
6507+
void set_max_old_space_size(size_t limit_in_mb)) {
6508+
max_old_generation_size_ = limit_in_mb * kMB;
64426509
}
64436510
V8_DEPRECATE_SOON("Zone does not pool memory any more.",
64446511
size_t max_zone_pool_size() const) {
@@ -6450,14 +6517,14 @@ class V8_EXPORT ResourceConstraints {
64506517
}
64516518

64526519
private:
6453-
// max_semi_space_size_ is in KB
6454-
size_t max_semi_space_size_in_kb_;
6455-
6456-
// The remaining limits are in MB
6457-
size_t max_old_space_size_;
6458-
uint32_t* stack_limit_;
6459-
size_t code_range_size_;
6460-
size_t max_zone_pool_size_;
6520+
static constexpr size_t kMB = 1048576u;
6521+
size_t code_range_size_ = 0;
6522+
size_t max_old_generation_size_ = 0;
6523+
size_t max_young_generation_size_ = 0;
6524+
size_t max_zone_pool_size_ = 0;
6525+
size_t initial_old_generation_size_ = 0;
6526+
size_t initial_young_generation_size_ = 0;
6527+
uint32_t* stack_limit_ = nullptr;
64616528
};
64626529

64636530

src/api/api.cc

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -963,30 +963,59 @@ Extension::Extension(const char* name, const char* source, int dep_count,
963963
CHECK(source != nullptr || source_length_ == 0);
964964
}
965965

966-
ResourceConstraints::ResourceConstraints()
967-
: max_semi_space_size_in_kb_(0),
968-
max_old_space_size_(0),
969-
stack_limit_(nullptr),
970-
code_range_size_(0),
971-
max_zone_pool_size_(0) {}
966+
void ResourceConstraints::ConfigureDefaultsFromHeapSize(
967+
size_t initial_heap_size_in_bytes, size_t maximum_heap_size_in_bytes) {
968+
CHECK_LE(initial_heap_size_in_bytes, maximum_heap_size_in_bytes);
969+
if (maximum_heap_size_in_bytes == 0) {
970+
return;
971+
}
972+
size_t young_generation, old_generation;
973+
i::Heap::GenerationSizesFromHeapSize(maximum_heap_size_in_bytes,
974+
&young_generation, &old_generation);
975+
set_max_young_generation_size_in_bytes(
976+
i::Max(young_generation, i::Heap::MinYoungGenerationSize()));
977+
set_max_old_generation_size_in_bytes(
978+
i::Max(old_generation, i::Heap::MinOldGenerationSize()));
979+
if (initial_heap_size_in_bytes > 0) {
980+
i::Heap::GenerationSizesFromHeapSize(initial_heap_size_in_bytes,
981+
&young_generation, &old_generation);
982+
// We do not set lower bounds for the initial sizes.
983+
set_initial_young_generation_size_in_bytes(young_generation);
984+
set_initial_old_generation_size_in_bytes(old_generation);
985+
}
986+
if (i::kRequiresCodeRange) {
987+
set_code_range_size_in_bytes(
988+
i::Min(i::kMaximalCodeRangeSize, maximum_heap_size_in_bytes));
989+
}
990+
}
972991

973992
void ResourceConstraints::ConfigureDefaults(uint64_t physical_memory,
974993
uint64_t virtual_memory_limit) {
975-
size_t old_space_size, semi_space_size;
976-
i::Heap::ComputeMaxSpaceSizes(physical_memory, &old_space_size,
977-
&semi_space_size);
978-
set_max_semi_space_size_in_kb(semi_space_size / i::KB);
979-
set_max_old_space_size(old_space_size / i::MB);
994+
size_t heap_size = i::Heap::HeapSizeFromPhysicalMemory(physical_memory);
995+
size_t young_generation, old_generation;
996+
i::Heap::GenerationSizesFromHeapSize(heap_size, &young_generation,
997+
&old_generation);
998+
set_max_young_generation_size_in_bytes(young_generation);
999+
set_max_old_generation_size_in_bytes(old_generation);
9801000

9811001
if (virtual_memory_limit > 0 && i::kRequiresCodeRange) {
982-
// Reserve no more than 1/8 of the memory for the code range, but at most
983-
// kMaximalCodeRangeSize.
984-
set_code_range_size(
985-
i::Min(i::kMaximalCodeRangeSize / i::MB,
986-
static_cast<size_t>((virtual_memory_limit >> 3) / i::MB)));
1002+
set_code_range_size_in_bytes(
1003+
i::Min(i::kMaximalCodeRangeSize,
1004+
static_cast<size_t>(virtual_memory_limit / 8)));
9871005
}
9881006
}
9891007

1008+
size_t ResourceConstraints::max_semi_space_size_in_kb() const {
1009+
return i::Heap::SemiSpaceSizeFromYoungGenerationSize(
1010+
max_young_generation_size_) /
1011+
i::KB;
1012+
}
1013+
1014+
void ResourceConstraints::set_max_semi_space_size_in_kb(size_t limit_in_kb) {
1015+
set_max_young_generation_size_in_bytes(
1016+
i::Heap::YoungGenerationSizeFromSemiSpaceSize(limit_in_kb * i::KB));
1017+
}
1018+
9901019
i::Address* V8::GlobalizeReference(i::Isolate* isolate, i::Address* obj) {
9911020
LOG_API(isolate, Persistent, New);
9921021
i::Handle<i::Object> result = isolate->global_handles()->Create(*obj);

0 commit comments

Comments
 (0)