From 3e9c664b8c2d661e616e96c7b0db4be65f12b612 Mon Sep 17 00:00:00 2001 From: hpayer Date: Tue, 26 May 2015 10:46:49 -0700 Subject: [PATCH] Fix overflow in allocation throughput calculation. BUG=chromium:492021 LOG=n Review URL: https://codereview.chromium.org/1148953009 Cr-Commit-Position: refs/heads/master@{#28638} --- src/heap/gc-tracer.cc | 11 ++++------- src/heap/gc-tracer.h | 5 +++-- test/cctest/test-heap.cc | 24 ++++++++++++------------ 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/heap/gc-tracer.cc b/src/heap/gc-tracer.cc index 4395ac5..11453b9 100644 --- a/src/heap/gc-tracer.cc +++ b/src/heap/gc-tracer.cc @@ -615,7 +615,8 @@ size_t GCTracer::NewSpaceAllocationThroughputInBytesPerMillisecond() const { } -size_t GCTracer::AllocatedBytesInLast(double time_ms) const { +size_t GCTracer::AllocationThroughputInBytesPerMillisecond( + double time_ms) const { size_t bytes = new_space_allocation_in_bytes_since_gc_ + old_generation_allocation_in_bytes_since_gc_; double durations = allocation_duration_since_gc_; @@ -630,17 +631,13 @@ size_t GCTracer::AllocatedBytesInLast(double time_ms) const { if (durations == 0.0) return 0; - bytes = static_cast(bytes * (time_ms / durations) + 0.5); - // Return at least 1 since 0 means "no data". - return std::max(bytes, 1); + return static_cast(bytes / durations + 0.5); } size_t GCTracer::CurrentAllocationThroughputInBytesPerMillisecond() const { static const double kThroughputTimeFrame = 5000; - size_t allocated_bytes = AllocatedBytesInLast(kThroughputTimeFrame); - if (allocated_bytes == 0) return 0; - return static_cast((allocated_bytes / kThroughputTimeFrame) + 1); + return AllocationThroughputInBytesPerMillisecond(kThroughputTimeFrame); } diff --git a/src/heap/gc-tracer.h b/src/heap/gc-tracer.h index e16f5d5..5d5500c 100644 --- a/src/heap/gc-tracer.h +++ b/src/heap/gc-tracer.h @@ -386,9 +386,10 @@ class GCTracer { // Returns 0 if no allocation events have been recorded. size_t NewSpaceAllocationThroughputInBytesPerMillisecond() const; - // Bytes allocated in heap in the specified time. + // Allocation throughput in heap in bytes/millisecond in the last time_ms + // milliseconds. // Returns 0 if no allocation events have been recorded. - size_t AllocatedBytesInLast(double time_ms) const; + size_t AllocationThroughputInBytesPerMillisecond(double time_ms) const; // Allocation throughput in heap in bytes/milliseconds in // the last five seconds. diff --git a/test/cctest/test-heap.cc b/test/cctest/test-heap.cc index 64d7a78..9894eda 100644 --- a/test/cctest/test-heap.cc +++ b/test/cctest/test-heap.cc @@ -5548,13 +5548,13 @@ TEST(NewSpaceAllocationThroughput2) { int time2 = 200; size_t counter2 = 2000; tracer->SampleAllocation(time2, counter2, 0); - size_t bytes = tracer->AllocatedBytesInLast(1000); - CHECK_EQ(10000, bytes); + size_t throughput = tracer->AllocationThroughputInBytesPerMillisecond(100); + CHECK_EQ((counter2 - counter1) / (time2 - time1), throughput); int time3 = 1000; size_t counter3 = 30000; tracer->SampleAllocation(time3, counter3, 0); - bytes = tracer->AllocatedBytesInLast(100); - CHECK_EQ((counter3 - counter1) * 100 / (time3 - time1), bytes); + throughput = tracer->AllocationThroughputInBytesPerMillisecond(100); + CHECK_EQ((counter3 - counter1) / (time3 - time1), throughput); } @@ -5611,13 +5611,13 @@ TEST(OldGenerationAllocationThroughput) { int time2 = 200; size_t counter2 = 2000; tracer->SampleAllocation(time2, 0, counter2); - size_t bytes = tracer->AllocatedBytesInLast(1000); - CHECK_EQ(10000, bytes); + size_t throughput = tracer->AllocationThroughputInBytesPerMillisecond(100); + CHECK_EQ((counter2 - counter1) / (time2 - time1), throughput); int time3 = 1000; size_t counter3 = 30000; tracer->SampleAllocation(time3, 0, counter3); - bytes = tracer->AllocatedBytesInLast(100); - CHECK_EQ((counter3 - counter1) * 100 / (time3 - time1), bytes); + throughput = tracer->AllocationThroughputInBytesPerMillisecond(100); + CHECK_EQ((counter3 - counter1) / (time3 - time1), throughput); } @@ -5633,11 +5633,11 @@ TEST(AllocationThroughput) { int time2 = 200; size_t counter2 = 2000; tracer->SampleAllocation(time2, counter2, counter2); - size_t bytes = tracer->AllocatedBytesInLast(1000); - CHECK_EQ(20000, bytes); + size_t throughput = tracer->AllocationThroughputInBytesPerMillisecond(100); + CHECK_EQ(2 * (counter2 - counter1) / (time2 - time1), throughput); int time3 = 1000; size_t counter3 = 30000; tracer->SampleAllocation(time3, counter3, counter3); - bytes = tracer->AllocatedBytesInLast(100); - CHECK_EQ(2 * (counter3 - counter1) * 100 / (time3 - time1), bytes); + throughput = tracer->AllocationThroughputInBytesPerMillisecond(100); + CHECK_EQ(2 * (counter3 - counter1) / (time3 - time1), throughput); } -- 2.7.4