OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/dart_api_impl.h" |
6 #include "vm/globals.h" | 7 #include "vm/globals.h" |
7 #include "vm/heap.h" | 8 #include "vm/heap.h" |
8 #include "vm/unit_test.h" | 9 #include "vm/unit_test.h" |
9 | 10 |
10 namespace dart { | 11 namespace dart { |
11 | 12 |
12 TEST_CASE(OldGC) { | 13 TEST_CASE(OldGC) { |
13 const char* kScriptChars = | 14 const char* kScriptChars = |
14 "main() {\n" | 15 "main() {\n" |
15 " return [1, 2, 3];\n" | 16 " return [1, 2, 3];\n" |
(...skipping 23 matching lines...) Expand all Loading... |
39 | 40 |
40 EXPECT_VALID(result); | 41 EXPECT_VALID(result); |
41 EXPECT(!Dart_IsNull(result)); | 42 EXPECT(!Dart_IsNull(result)); |
42 EXPECT(Dart_IsList(result)); | 43 EXPECT(Dart_IsList(result)); |
43 Isolate* isolate = Isolate::Current(); | 44 Isolate* isolate = Isolate::Current(); |
44 Heap* heap = isolate->heap(); | 45 Heap* heap = isolate->heap(); |
45 heap->CollectGarbage(Heap::kOld); | 46 heap->CollectGarbage(Heap::kOld); |
46 Dart_ExitScope(); | 47 Dart_ExitScope(); |
47 heap->CollectGarbage(Heap::kOld); | 48 heap->CollectGarbage(Heap::kOld); |
48 } | 49 } |
| 50 |
| 51 class ClassHeapStatsTestHelper { |
| 52 public: |
| 53 static ClassHeapStats* GetHeapStatsForCid(ClassTable* class_table, |
| 54 intptr_t cid) { |
| 55 return class_table->StatsAt(cid); |
| 56 } |
| 57 |
| 58 static void DumpClassHeapStats(ClassHeapStats* stats) { |
| 59 printf("%" Pd " ", stats->allocated_since_gc_new_space); |
| 60 printf("%" Pd " ", stats->live_after_gc_new_space); |
| 61 printf("%" Pd " ", stats->allocated_before_gc_new_space); |
| 62 printf("\n"); |
| 63 } |
| 64 }; |
| 65 |
| 66 |
| 67 static RawClass* GetClass(const Library& lib, const char* name) { |
| 68 const Class& cls = Class::Handle( |
| 69 lib.LookupClass(String::Handle(Symbols::New(name)))); |
| 70 EXPECT(!cls.IsNull()); // No ambiguity error expected. |
| 71 return cls.raw(); |
49 } | 72 } |
| 73 |
| 74 |
| 75 TEST_CASE(ClassHeapStats) { |
| 76 const char* kScriptChars = |
| 77 "class A {\n" |
| 78 " var a;\n" |
| 79 " var b;\n" |
| 80 "}\n" |
| 81 "" |
| 82 "main() {\n" |
| 83 " var x = new A();\n" |
| 84 " return new A();\n" |
| 85 "}\n"; |
| 86 Dart_Handle h_lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 87 Isolate* isolate = Isolate::Current(); |
| 88 ClassTable* class_table = isolate->class_table(); |
| 89 Heap* heap = isolate->heap(); |
| 90 Dart_EnterScope(); |
| 91 Dart_Handle result = Dart_Invoke(h_lib, NewString("main"), 0, NULL); |
| 92 EXPECT_VALID(result); |
| 93 EXPECT(!Dart_IsNull(result)); |
| 94 Library& lib = Library::Handle(); |
| 95 lib ^= Api::UnwrapHandle(h_lib); |
| 96 EXPECT(!lib.IsNull()); |
| 97 const Class& cls = Class::Handle(GetClass(lib, "A")); |
| 98 ASSERT(!cls.IsNull()); |
| 99 intptr_t cid = cls.id(); |
| 100 ClassHeapStats* class_stats = |
| 101 ClassHeapStatsTestHelper::GetHeapStatsForCid(class_table, |
| 102 cid); |
| 103 // Verify preconditions: |
| 104 EXPECT_EQ(0, class_stats->allocated_before_gc_old_space); |
| 105 EXPECT_EQ(0, class_stats->live_after_gc_old_space); |
| 106 EXPECT_EQ(0, class_stats->allocated_since_gc_old_space); |
| 107 EXPECT_EQ(0, class_stats->allocated_before_gc_new_space); |
| 108 EXPECT_EQ(0, class_stats->live_after_gc_new_space); |
| 109 // Class allocated twice since GC from new space. |
| 110 EXPECT_EQ(2, class_stats->allocated_since_gc_new_space); |
| 111 // Perform GC. |
| 112 heap->CollectGarbage(Heap::kNew); |
| 113 // Verify postconditions: |
| 114 EXPECT_EQ(0, class_stats->allocated_before_gc_old_space); |
| 115 EXPECT_EQ(0, class_stats->live_after_gc_old_space); |
| 116 EXPECT_EQ(0, class_stats->allocated_since_gc_old_space); |
| 117 // Total allocations before GC. |
| 118 EXPECT_EQ(2, class_stats->allocated_before_gc_new_space); |
| 119 // Only one survived. |
| 120 EXPECT_EQ(1, class_stats->live_after_gc_new_space); |
| 121 EXPECT_EQ(0, class_stats->allocated_since_gc_new_space); |
| 122 // Perform GC. The following is heavily dependent on the behaviour |
| 123 // of the GC: Retained instance of A will be promoted. |
| 124 heap->CollectGarbage(Heap::kNew); |
| 125 // Verify postconditions: |
| 126 EXPECT_EQ(0, class_stats->allocated_before_gc_old_space); |
| 127 EXPECT_EQ(0, class_stats->live_after_gc_old_space); |
| 128 // Promotion counted as an allocation from old space. |
| 129 EXPECT_EQ(1, class_stats->allocated_since_gc_old_space); |
| 130 // There was one instance allocated before GC. |
| 131 EXPECT_EQ(1, class_stats->allocated_before_gc_new_space); |
| 132 // There are no instances allocated in new space after GC. |
| 133 EXPECT_EQ(0, class_stats->live_after_gc_new_space); |
| 134 // No new allocations. |
| 135 EXPECT_EQ(0, class_stats->allocated_since_gc_new_space); |
| 136 // Perform a GC on new space. |
| 137 heap->CollectGarbage(Heap::kNew); |
| 138 // There were no instances allocated before GC. |
| 139 EXPECT_EQ(0, class_stats->allocated_before_gc_new_space); |
| 140 // There are no instances allocated in new space after GC. |
| 141 EXPECT_EQ(0, class_stats->live_after_gc_new_space); |
| 142 // No new allocations. |
| 143 EXPECT_EQ(0, class_stats->allocated_since_gc_new_space); |
| 144 heap->CollectGarbage(Heap::kOld); |
| 145 // Verify postconditions: |
| 146 EXPECT_EQ(1, class_stats->allocated_before_gc_old_space); |
| 147 EXPECT_EQ(1, class_stats->live_after_gc_old_space); |
| 148 EXPECT_EQ(0, class_stats->allocated_since_gc_old_space); |
| 149 // Exit scope, freeing instance. |
| 150 Dart_ExitScope(); |
| 151 // Perform GC. |
| 152 heap->CollectGarbage(Heap::kOld); |
| 153 // Verify postconditions: |
| 154 EXPECT_EQ(1, class_stats->allocated_before_gc_old_space); |
| 155 EXPECT_EQ(0, class_stats->live_after_gc_old_space); |
| 156 EXPECT_EQ(0, class_stats->allocated_since_gc_old_space); |
| 157 // Perform GC. |
| 158 heap->CollectGarbage(Heap::kOld); |
| 159 EXPECT_EQ(0, class_stats->allocated_before_gc_old_space); |
| 160 EXPECT_EQ(0, class_stats->live_after_gc_old_space); |
| 161 EXPECT_EQ(0, class_stats->allocated_since_gc_old_space); |
| 162 } |
| 163 |
| 164 } // namespace dart. |
OLD | NEW |