Skip to content

Commit

Permalink
[DeviceMemory] Limit the possibile reported value by the DeviceMemory…
Browse files Browse the repository at this point in the history
… APIs

In this CL, we revert back to using a single most significant bit
instead of 2 bits (i.e. resulting values should only be powers of 2).

We also limit the max reported value to 8GB, beyond that the value is
not relevant.

Bug: 718622
Change-Id: Id83482a4b77dd3f6b8c49af34029564ab20dd8fe
Reviewed-on: https://chromium-review.googlesource.com/717020
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Shubhie Panicker <panicker@chromium.org>
Commit-Queue: Fadi Meawad <fmeawad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#508561}
  • Loading branch information
fmeawad authored and Commit Bot committed Oct 13, 2017
1 parent f0e95a5 commit b5f22e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ TEST_F(FrameFetchContextHintsTest, MonitorDeviceMemoryHints) {
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(2048);
ExpectHeader("http://www.example.com/1.gif", "Device-Memory", true, "2");
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(64385);
ExpectHeader("http://www.example.com/1.gif", "Device-Memory", true, "64");
ExpectHeader("http://www.example.com/1.gif", "Device-Memory", true, "8");
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(768);
ExpectHeader("http://www.example.com/1.gif", "Device-Memory", true, "0.75");
ExpectHeader("http://www.example.com/1.gif", "Device-Memory", true, "0.5");
ExpectHeader("http://www.example.com/1.gif", "DPR", false, "");
ExpectHeader("http://www.example.com/1.gif", "Width", false, "");
ExpectHeader("http://www.example.com/1.gif", "Viewport-Width", false, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ void ApproximatedDeviceMemory::CalculateAndSetApproximatedDeviceMemory() {
int lower_bound = physical_memory_mb_;
int power = 0;

// Extract the most significant 2-bits and their location.
while (lower_bound >= 4) {
// Extract the most-significant-bit and its location.
while (lower_bound > 1) {
lower_bound >>= 1;
power++;
}
// The lower_bound value is either 0b10 or 0b11.
DCHECK(lower_bound & 2);
// The remaining should always be equal to exactly 1.
DCHECK_EQ(lower_bound, 1);

int64_t upper_bound = lower_bound + 1;
lower_bound = lower_bound << power;
Expand All @@ -51,6 +51,11 @@ void ApproximatedDeviceMemory::CalculateAndSetApproximatedDeviceMemory() {
approximated_device_memory_gb_ = static_cast<float>(lower_bound) / 1024.0;
else
approximated_device_memory_gb_ = static_cast<float>(upper_bound) / 1024.0;

// The exact value beyond 8GB is irrelvant, hence we max-limit the reported
// value to 8GB.
if (approximated_device_memory_gb_ > 8)
approximated_device_memory_gb_ = 8.0;
}

// static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ TEST_F(ApproximatedDeviceMemoryTest, GetApproximatedDeviceMemory) {
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(640); // 512+128MB
EXPECT_EQ(0.5, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(768); // 512+256MB
EXPECT_EQ(0.75, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
EXPECT_EQ(0.5, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(1000); // <1GB
EXPECT_EQ(1, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(1024); // 1GB
EXPECT_EQ(1, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(1536); // 1.5GB
EXPECT_EQ(1.5, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
EXPECT_EQ(1, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(2000); // <2GB
EXPECT_EQ(2, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(2048); // 2GB
EXPECT_EQ(2, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(3000); // <3GB
EXPECT_EQ(3, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
EXPECT_EQ(2, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(5120); // 5GB
EXPECT_EQ(4, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(8192); // 8GB
EXPECT_EQ(8, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(16384); // 16GB
EXPECT_EQ(16, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
EXPECT_EQ(8, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(32768); // 32GB
EXPECT_EQ(32, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
EXPECT_EQ(8, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
ApproximatedDeviceMemory::SetPhysicalMemoryMBForTesting(64385); // <64GB
EXPECT_EQ(64, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
EXPECT_EQ(8, ApproximatedDeviceMemory::GetApproximatedDeviceMemory());
}

} // namespace
Expand Down

0 comments on commit b5f22e2

Please sign in to comment.