Fix overflow in allocation throughput calculation.
authorhpayer <hpayer@chromium.org>
Tue, 26 May 2015 17:46:49 +0000 (10:46 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 26 May 2015 17:46:57 +0000 (17:46 +0000)
BUG=chromium:492021
LOG=n

Review URL: https://codereview.chromium.org/1148953009

Cr-Commit-Position: refs/heads/master@{#28638}

src/heap/gc-tracer.cc
src/heap/gc-tracer.h
test/cctest/test-heap.cc

index 4395ac5..11453b9 100644 (file)
@@ -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<size_t>(bytes * (time_ms / durations) + 0.5);
-  // Return at least 1 since 0 means "no data".
-  return std::max<size_t>(bytes, 1);
+  return static_cast<size_t>(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<size_t>((allocated_bytes / kThroughputTimeFrame) + 1);
+  return AllocationThroughputInBytesPerMillisecond(kThroughputTimeFrame);
 }
 
 
index e16f5d5..5d5500c 100644 (file)
@@ -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.
index 64d7a78..9894eda 100644 (file)
@@ -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);
 }