Commit fb436a5e authored by Hannes Payer's avatar Hannes Payer Committed by Commit Bot

Adding an API to enable and disable the memory savings mode.

Bug: chromium:846360
Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: I5376d4f6f9c8df768d60c63e0a767bf41b51b8b3
Reviewed-on: https://chromium-review.googlesource.com/1071531Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Hannes Payer <hpayer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53345}
parent cffe6247
......@@ -7860,6 +7860,18 @@ class V8_EXPORT Isolate {
*/
void IsolateInBackgroundNotification();
/**
* Optional notification which will enable the memory savings mode.
* V8 uses this notification to guide heuristics which may result in a
* smaller memory footprint at the cost of reduced runtime performance.
*/
void EnableMemorySavingsMode();
/**
* Optional notification which will disable the memory savings mode.
*/
void DisableMemorySavingsMode();
/**
* Optional notification to tell V8 the current performance requirements
* of the embedder based on RAIL.
......
......@@ -8725,6 +8725,16 @@ void Isolate::MemoryPressureNotification(MemoryPressureLevel level) {
on_isolate_thread);
}
void Isolate::EnableMemorySavingsMode() {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
isolate->EnableMemorySavingsMode();
}
void Isolate::DisableMemorySavingsMode() {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
isolate->DisableMemorySavingsMode();
}
void Isolate::SetRAILMode(RAILMode rail_mode) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
return isolate->SetRAILMode(rail_mode);
......
......@@ -406,8 +406,7 @@ class HistogramTimerScope BASE_EMBEDDED {
explicit HistogramTimerScope(HistogramTimer* timer,
bool allow_nesting = false)
#ifdef DEBUG
: timer_(timer),
skipped_timer_start_(false) {
: timer_(timer), skipped_timer_start_(false) {
if (timer_->timer()->IsStarted() && allow_nesting) {
skipped_timer_start_ = true;
} else {
......@@ -436,6 +435,27 @@ class HistogramTimerScope BASE_EMBEDDED {
#endif
};
enum class OptionalHistogramTimerScopeMode { TAKE_TIME, DONT_TAKE_TIME };
// Helper class for scoping a HistogramTimer.
// It will not take time if take_time is set to false.
class OptionalHistogramTimerScope BASE_EMBEDDED {
public:
OptionalHistogramTimerScope(HistogramTimer* timer,
OptionalHistogramTimerScopeMode mode)
: timer_(timer), mode_(mode) {
if (mode == OptionalHistogramTimerScopeMode::TAKE_TIME) timer_->Start();
}
~OptionalHistogramTimerScope() {
if (mode_ == OptionalHistogramTimerScopeMode::TAKE_TIME) timer_->Stop();
}
private:
HistogramTimer* timer_;
OptionalHistogramTimerScopeMode mode_;
};
// A histogram timer that can aggregate events within a larger scope.
//
// Intended use of this timer is to have an outer (aggregating) and an inner
......
......@@ -1384,8 +1384,12 @@ bool Heap::CollectGarbage(AllocationSpace space,
TRACE_EVENT0("v8", gc_type_timer->name());
HistogramTimer* gc_type_priority_timer = GCTypePriorityTimer(collector);
HistogramTimerScope histogram_timer_priority_scope(
gc_type_priority_timer);
OptionalHistogramTimerScopeMode mode =
isolate_->IsMemorySavingsModeActive()
? OptionalHistogramTimerScopeMode::DONT_TAKE_TIME
: OptionalHistogramTimerScopeMode::TAKE_TIME;
OptionalHistogramTimerScope histogram_timer_priority_scope(
gc_type_priority_timer, mode);
next_gc_likely_to_collect_more =
PerformGarbageCollection(collector, gc_callback_flags);
......@@ -3044,7 +3048,8 @@ bool Heap::HasHighFragmentation(size_t used, size_t committed) {
bool Heap::ShouldOptimizeForMemoryUsage() {
const size_t kOldGenerationSlack = max_old_generation_size_ / 8;
return FLAG_optimize_for_size || isolate()->IsIsolateInBackground() ||
HighMemoryPressure() || !CanExpandOldGeneration(kOldGenerationSlack);
isolate()->IsMemorySavingsModeActive() || HighMemoryPressure() ||
!CanExpandOldGeneration(kOldGenerationSlack);
}
void Heap::ActivateMemoryReducerIfNeeded() {
......
......@@ -2524,6 +2524,7 @@ Isolate::Isolate()
initialized_from_snapshot_(false),
is_tail_call_elimination_enabled_(true),
is_isolate_in_background_(false),
memory_savings_mode_active_(false),
cpu_profiler_(nullptr),
heap_profiler_(nullptr),
code_event_dispatcher_(new CodeEventDispatcher()),
......
......@@ -1344,6 +1344,12 @@ class Isolate : private HiddenFactory {
bool IsIsolateInBackground() { return is_isolate_in_background_; }
void EnableMemorySavingsMode() { memory_savings_mode_active_ = true; }
void DisableMemorySavingsMode() { memory_savings_mode_active_ = false; }
bool IsMemorySavingsModeActive() { return memory_savings_mode_active_; }
PRINTF_FORMAT(2, 3) void PrintWithTimestamp(const char* format, ...);
void set_allow_atomics_wait(bool set) { allow_atomics_wait_ = set; }
......@@ -1563,6 +1569,10 @@ class Isolate : private HiddenFactory {
// to prioritize between memory usage and latency.
bool is_isolate_in_background_;
// True if the isolate is in memory savings mode. This flag is used to
// favor memory over runtime performance.
bool memory_savings_mode_active_;
// Time stamp at initialization.
double time_millis_at_init_;
......
......@@ -18598,6 +18598,17 @@ TEST(TestIdleNotification) {
CHECK_LT(final_size, initial_size + 1);
}
TEST(TestMemorySavingsMode) {
LocalContext context;
v8::Isolate* isolate = context->GetIsolate();
v8::internal::Isolate* i_isolate =
reinterpret_cast<v8::internal::Isolate*>(isolate);
CHECK(!i_isolate->IsMemorySavingsModeActive());
isolate->EnableMemorySavingsMode();
CHECK(i_isolate->IsMemorySavingsModeActive());
isolate->DisableMemorySavingsMode();
CHECK(!i_isolate->IsMemorySavingsModeActive());
}
TEST(Regress2333) {
LocalContext env;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment