OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cmath> | 5 #include <cmath> |
6 #include <limits> | 6 #include <limits> |
7 | 7 |
| 8 #include "src/globals.h" |
8 #include "src/heap/gc-tracer.h" | 9 #include "src/heap/gc-tracer.h" |
| 10 #include "src/isolate.h" |
| 11 #include "test/unittests/test-utils.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
10 | 13 |
11 namespace v8 { | 14 namespace v8 { |
12 namespace internal { | 15 namespace internal { |
13 | 16 |
| 17 typedef TestWithContext GCTracerTest; |
| 18 |
14 TEST(GCTracer, AverageSpeed) { | 19 TEST(GCTracer, AverageSpeed) { |
15 RingBuffer<BytesAndDuration> buffer; | 20 RingBuffer<BytesAndDuration> buffer; |
16 EXPECT_EQ(100 / 2, | 21 EXPECT_EQ(100 / 2, |
17 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 0)); | 22 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 0)); |
18 buffer.Push(MakeBytesAndDuration(100, 8)); | 23 buffer.Push(MakeBytesAndDuration(100, 8)); |
19 EXPECT_EQ(100 / 2, | 24 EXPECT_EQ(100 / 2, |
20 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 2)); | 25 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 2)); |
21 EXPECT_EQ(200 / 10, | 26 EXPECT_EQ(200 / 10, |
22 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 3)); | 27 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(100, 2), 3)); |
23 const int max_speed = 1024 * MB; | 28 const int max_speed = 1024 * MB; |
(...skipping 14 matching lines...) Expand all Loading... |
38 } | 43 } |
39 EXPECT_EQ( | 44 EXPECT_EQ( |
40 sum * 1.0 / buffer.kSize, | 45 sum * 1.0 / buffer.kSize, |
41 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), buffer.kSize)); | 46 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), buffer.kSize)); |
42 buffer.Push(MakeBytesAndDuration(100, 1)); | 47 buffer.Push(MakeBytesAndDuration(100, 1)); |
43 EXPECT_EQ( | 48 EXPECT_EQ( |
44 (sum * 1.0 - 1 + 100) / buffer.kSize, | 49 (sum * 1.0 - 1 + 100) / buffer.kSize, |
45 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), buffer.kSize)); | 50 GCTracer::AverageSpeed(buffer, MakeBytesAndDuration(0, 0), buffer.kSize)); |
46 } | 51 } |
47 | 52 |
| 53 namespace { |
| 54 |
| 55 void SampleAndAddAllocaton(v8::internal::GCTracer* tracer, double time_ms, |
| 56 size_t new_space_counter_bytes, |
| 57 size_t old_generation_counter_bytes) { |
| 58 tracer->SampleAllocation(time_ms, new_space_counter_bytes, |
| 59 old_generation_counter_bytes); |
| 60 tracer->AddAllocation(time_ms); |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 TEST_F(GCTracerTest, AllocationThroughput) { |
| 66 GCTracer* tracer = i_isolate()->heap()->tracer(); |
| 67 tracer->ResetForTesting(); |
| 68 |
| 69 int time1 = 100; |
| 70 size_t counter1 = 1000; |
| 71 // First sample creates baseline but is not part of the recorded samples. |
| 72 tracer->SampleAllocation(time1, counter1, counter1); |
| 73 SampleAndAddAllocaton(tracer, time1, counter1, counter1); |
| 74 int time2 = 200; |
| 75 size_t counter2 = 2000; |
| 76 SampleAndAddAllocaton(tracer, time2, counter2, counter2); |
| 77 // Will only consider the current sample. |
| 78 size_t throughput = static_cast<size_t>( |
| 79 tracer->AllocationThroughputInBytesPerMillisecond(100)); |
| 80 EXPECT_EQ(2 * (counter2 - counter1) / (time2 - time1), throughput); |
| 81 int time3 = 1000; |
| 82 size_t counter3 = 30000; |
| 83 SampleAndAddAllocaton(tracer, time3, counter3, counter3); |
| 84 // Considers last 2 samples. |
| 85 throughput = tracer->AllocationThroughputInBytesPerMillisecond(801); |
| 86 EXPECT_EQ(2 * (counter3 - counter1) / (time3 - time1), throughput); |
| 87 } |
| 88 |
| 89 TEST_F(GCTracerTest, NewSpaceAllocationThroughput) { |
| 90 GCTracer* tracer = i_isolate()->heap()->tracer(); |
| 91 tracer->ResetForTesting(); |
| 92 |
| 93 int time1 = 100; |
| 94 size_t counter1 = 1000; |
| 95 SampleAndAddAllocaton(tracer, time1, counter1, 0); |
| 96 int time2 = 200; |
| 97 size_t counter2 = 2000; |
| 98 SampleAndAddAllocaton(tracer, time2, counter2, 0); |
| 99 size_t throughput = |
| 100 tracer->NewSpaceAllocationThroughputInBytesPerMillisecond(); |
| 101 EXPECT_EQ((counter2 - counter1) / (time2 - time1), throughput); |
| 102 int time3 = 1000; |
| 103 size_t counter3 = 30000; |
| 104 SampleAndAddAllocaton(tracer, time3, counter3, 0); |
| 105 throughput = tracer->NewSpaceAllocationThroughputInBytesPerMillisecond(); |
| 106 EXPECT_EQ((counter3 - counter1) / (time3 - time1), throughput); |
| 107 } |
| 108 |
| 109 TEST_F(GCTracerTest, NewSpaceAllocationThroughputWithProvidedTime) { |
| 110 GCTracer* tracer = i_isolate()->heap()->tracer(); |
| 111 tracer->ResetForTesting(); |
| 112 |
| 113 int time1 = 100; |
| 114 size_t counter1 = 1000; |
| 115 // First sample creates baseline but is not part of the recorded samples. |
| 116 SampleAndAddAllocaton(tracer, time1, counter1, 0); |
| 117 int time2 = 200; |
| 118 size_t counter2 = 2000; |
| 119 SampleAndAddAllocaton(tracer, time2, counter2, 0); |
| 120 // Will only consider the current sample. |
| 121 size_t throughput = |
| 122 tracer->NewSpaceAllocationThroughputInBytesPerMillisecond(100); |
| 123 EXPECT_EQ((counter2 - counter1) / (time2 - time1), throughput); |
| 124 int time3 = 1000; |
| 125 size_t counter3 = 30000; |
| 126 SampleAndAddAllocaton(tracer, time3, counter3, 0); |
| 127 // Considers last 2 samples. |
| 128 throughput = tracer->NewSpaceAllocationThroughputInBytesPerMillisecond(801); |
| 129 EXPECT_EQ((counter3 - counter1) / (time3 - time1), throughput); |
| 130 } |
| 131 |
| 132 TEST_F(GCTracerTest, OldGenerationAllocationThroughputWithProvidedTime) { |
| 133 GCTracer* tracer = i_isolate()->heap()->tracer(); |
| 134 tracer->ResetForTesting(); |
| 135 |
| 136 int time1 = 100; |
| 137 size_t counter1 = 1000; |
| 138 // First sample creates baseline but is not part of the recorded samples. |
| 139 SampleAndAddAllocaton(tracer, time1, 0, counter1); |
| 140 int time2 = 200; |
| 141 size_t counter2 = 2000; |
| 142 SampleAndAddAllocaton(tracer, time2, 0, counter2); |
| 143 // Will only consider the current sample. |
| 144 size_t throughput = static_cast<size_t>( |
| 145 tracer->OldGenerationAllocationThroughputInBytesPerMillisecond(100)); |
| 146 EXPECT_EQ((counter2 - counter1) / (time2 - time1), throughput); |
| 147 int time3 = 1000; |
| 148 size_t counter3 = 30000; |
| 149 SampleAndAddAllocaton(tracer, time3, 0, counter3); |
| 150 // Considers last 2 samples. |
| 151 throughput = static_cast<size_t>( |
| 152 tracer->OldGenerationAllocationThroughputInBytesPerMillisecond(801)); |
| 153 EXPECT_EQ((counter3 - counter1) / (time3 - time1), throughput); |
| 154 } |
| 155 |
| 156 TEST_F(GCTracerTest, RegularScope) { |
| 157 GCTracer* tracer = i_isolate()->heap()->tracer(); |
| 158 |
| 159 { |
| 160 // Sample not added because it's not within a started tracer. |
| 161 tracer->AddScopeSample(GCTracer::Scope::MC_MARK, 100); |
| 162 } |
| 163 tracer->Start(MARK_COMPACTOR, "gc unittest", "collector unittest"); |
| 164 { tracer->AddScopeSample(GCTracer::Scope::MC_MARK, 100); } |
| 165 tracer->Stop(MARK_COMPACTOR); |
| 166 EXPECT_EQ( |
| 167 static_cast<size_t>(tracer->current_.scopes[GCTracer::Scope::MC_MARK]), |
| 168 100u); |
| 169 } |
| 170 |
| 171 TEST_F(GCTracerTest, IncrementalScope) { |
| 172 GCTracer* tracer = i_isolate()->heap()->tracer(); |
| 173 |
| 174 { |
| 175 // Sample is added because its ScopeId is listed as incremental sample. |
| 176 tracer->AddScopeSample(GCTracer::Scope::MC_INCREMENTAL_FINALIZE, 100); |
| 177 } |
| 178 tracer->Start(MARK_COMPACTOR, "gc unittest", "collector unittest"); |
| 179 // Switch to incremental MC to enable writing back incremental scopes. |
| 180 tracer->current_.type = GCTracer::Event::INCREMENTAL_MARK_COMPACTOR; |
| 181 { tracer->AddScopeSample(GCTracer::Scope::MC_INCREMENTAL_FINALIZE, 100); } |
| 182 tracer->Stop(MARK_COMPACTOR); |
| 183 EXPECT_EQ( |
| 184 static_cast<size_t>( |
| 185 tracer->current_.scopes[GCTracer::Scope::MC_INCREMENTAL_FINALIZE]), |
| 186 200u); |
| 187 } |
| 188 |
48 } // namespace internal | 189 } // namespace internal |
49 } // namespace v8 | 190 } // namespace v8 |
OLD | NEW |