forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an experiment for enabling V8's memory savings mode on Android.
This feature works in conjunction with --site-per-process. When a renderer process has only subframes, Blink's main thread isolate will be put into a memory savings mode. If a main frame is created in the process, the isolate will be taken out of memory savings mode for as long as a main frame exists. Bug: 846360 Change-Id: I09c362cce648b734fb01907a5ad758f0bdb7f752 Reviewed-on: https://chromium-review.googlesource.com/1076954 Reviewed-by: Brian White <bcwhite@chromium.org> Reviewed-by: Nasko Oskov <nasko@chromium.org> Commit-Queue: Robert Sesek <rsesek@chromium.org> Cr-Commit-Position: refs/heads/master@{#564881}
- Loading branch information
Showing
15 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2018 The Chromium 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 "content/renderer/low_memory_mode_controller.h" | ||
|
||
#include "base/metrics/histogram_macros.h" | ||
#include "third_party/blink/public/web/blink.h" | ||
#include "v8/include/v8.h" | ||
|
||
namespace content { | ||
|
||
LowMemoryModeController::LowMemoryModeController() = default; | ||
|
||
LowMemoryModeController::~LowMemoryModeController() = default; | ||
|
||
void LowMemoryModeController::OnFrameCreated(bool is_main_frame) { | ||
if (is_main_frame) { | ||
// If the process is gaining its first main frame, disable memory | ||
// savings mode. | ||
if (++main_frame_count_ == 1) { | ||
Disable(); | ||
} | ||
} else if (main_frame_count_ == 0) { | ||
// The process is getting a new frame and none is main, enable | ||
// memory savings mode (if not already on). | ||
Enable(); | ||
} | ||
} | ||
|
||
void LowMemoryModeController::OnFrameDestroyed(bool is_main_frame) { | ||
// If the process is losing its last main frame, enable memory | ||
// savings mode. | ||
if (is_main_frame && --main_frame_count_ == 0) { | ||
Enable(); | ||
} | ||
} | ||
|
||
void LowMemoryModeController::Enable() { | ||
if (is_enabled_) | ||
return; | ||
|
||
blink::MainThreadIsolate()->EnableMemorySavingsMode(); | ||
RecordHistogram(true); | ||
is_enabled_ = true; | ||
} | ||
|
||
void LowMemoryModeController::Disable() { | ||
if (!is_enabled_) | ||
return; | ||
|
||
blink::MainThreadIsolate()->DisableMemorySavingsMode(); | ||
RecordHistogram(false); | ||
is_enabled_ = false; | ||
} | ||
|
||
void LowMemoryModeController::RecordHistogram(bool enabled) { | ||
UMA_HISTOGRAM_BOOLEAN("SiteIsolation.LowMemoryMode.Transition", enabled); | ||
} | ||
|
||
} // namespace content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2018 The Chromium 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 CONTENT_RENDERER_LOW_MEMORY_MODE_CONTROLLER_H_ | ||
#define CONTENT_RENDERER_LOW_MEMORY_MODE_CONTROLLER_H_ | ||
|
||
#include <memory> | ||
|
||
#include "base/macros.h" | ||
#include "content/common/content_export.h" | ||
|
||
namespace content { | ||
|
||
// The LowMemoryModeController manages for a renderer process the blink | ||
// main thread isolate's memory savings mode state. This is only enabled | ||
// if the V8LowMemoryModeForNonMainFrames feature and --site-per-process | ||
// are enabled. | ||
// | ||
// When a process only contains subframes, the memory saving mode is | ||
// enabled. If a main frame is later created, then the mode is disabled | ||
// for the duration of the main frame's existence. | ||
// | ||
// The default state after initialization is to not enable low memory mode. | ||
class CONTENT_EXPORT LowMemoryModeController { | ||
public: | ||
LowMemoryModeController(); | ||
~LowMemoryModeController(); | ||
|
||
// Notifies the controller that a frame has either been created or | ||
// destroyed. A transition to the memory saving mode may occur as a result. | ||
void OnFrameCreated(bool is_main_frame); | ||
void OnFrameDestroyed(bool is_main_frame); | ||
|
||
bool is_enabled() const { return is_enabled_; } | ||
|
||
private: | ||
// Puts the main thread isolate into memory savings mode if it is not | ||
// currently enabled. | ||
void Enable(); | ||
|
||
// Takes the main thread isolate out of memory savings mode if it is | ||
// currently enabled. | ||
void Disable(); | ||
|
||
// Records an UMA histogram marking an Enabled->Disabled state transition, | ||
// or vice versa. | ||
void RecordHistogram(bool enabled); | ||
|
||
int main_frame_count_ = 0; | ||
bool is_enabled_ = false; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(LowMemoryModeController); | ||
}; | ||
|
||
} // namespace content | ||
|
||
#endif // CONTENT_RENDERER_LOW_MEMORY_MODE_CONTROLLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2018 The Chromium 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 "content/renderer/low_memory_mode_controller.h" | ||
|
||
#include "base/test/histogram_tester.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace content { | ||
namespace { | ||
|
||
class LowMemoryModeControllerTest : public testing::Test { | ||
public: | ||
LowMemoryModeController* controller() { return &controller_; } | ||
|
||
void ExpectTransitionCount(int enabled_count, int disabled_count) { | ||
static constexpr char kHistogram[] = | ||
"SiteIsolation.LowMemoryMode.Transition"; | ||
histogram_tester_.ExpectBucketCount(kHistogram, true, enabled_count); | ||
histogram_tester_.ExpectBucketCount(kHistogram, false, disabled_count); | ||
histogram_tester_.ExpectTotalCount(kHistogram, | ||
enabled_count + disabled_count); | ||
} | ||
|
||
private: | ||
base::HistogramTester histogram_tester_; | ||
LowMemoryModeController controller_; | ||
}; | ||
|
||
TEST_F(LowMemoryModeControllerTest, CreateMainFrames) { | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameCreated(true); | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameCreated(true); | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameDestroyed(true); | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameDestroyed(true); | ||
EXPECT_TRUE(controller()->is_enabled()); | ||
|
||
ExpectTransitionCount(1, 0); | ||
} | ||
|
||
TEST_F(LowMemoryModeControllerTest, MainFrameAddSubframe) { | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameCreated(true); | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameCreated(false); | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameDestroyed(true); | ||
EXPECT_TRUE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameDestroyed(false); | ||
EXPECT_TRUE(controller()->is_enabled()); | ||
|
||
ExpectTransitionCount(1, 0); | ||
} | ||
|
||
TEST_F(LowMemoryModeControllerTest, SubFrameAddMainFrame) { | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameCreated(false); | ||
EXPECT_TRUE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameCreated(true); | ||
EXPECT_FALSE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameDestroyed(true); | ||
EXPECT_TRUE(controller()->is_enabled()); | ||
|
||
controller()->OnFrameDestroyed(false); | ||
EXPECT_TRUE(controller()->is_enabled()); | ||
|
||
ExpectTransitionCount(2, 1); | ||
} | ||
|
||
} // namespace | ||
} // namespace content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.