1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "class_linker-inl.h"
18 #include "common_runtime_test.h"
19 #include "gc/accounting/card_table-inl.h"
20 #include "gc/accounting/space_bitmap-inl.h"
21 #include "handle_scope-inl.h"
22 #include "mirror/class-inl.h"
23 #include "mirror/object-inl.h"
24 #include "mirror/object_array-alloc-inl.h"
25 #include "mirror/object_array-inl.h"
26 #include "scoped_thread_state_change-inl.h"
27 
28 namespace art {
29 namespace gc {
30 
31 class HeapTest : public CommonRuntimeTest {
32  public:
SetUp()33   void SetUp() override {
34     MemMap::Init();
35     std::string error_msg;
36     // Reserve the preferred address to force the heap to use another one for testing.
37     reserved_ = MemMap::MapAnonymous("ReserveMap",
38                                      gc::Heap::kPreferredAllocSpaceBegin,
39                                      16 * KB,
40                                      PROT_READ,
41                                      /*low_4gb=*/ true,
42                                      /*reuse=*/ false,
43                                      /*reservation=*/ nullptr,
44                                      &error_msg);
45     ASSERT_TRUE(reserved_.IsValid()) << error_msg;
46     CommonRuntimeTest::SetUp();
47   }
48 
49  private:
50   MemMap reserved_;
51 };
52 
TEST_F(HeapTest,ClearGrowthLimit)53 TEST_F(HeapTest, ClearGrowthLimit) {
54   Heap* heap = Runtime::Current()->GetHeap();
55   int64_t max_memory_before = heap->GetMaxMemory();
56   int64_t total_memory_before = heap->GetTotalMemory();
57   heap->ClearGrowthLimit();
58   int64_t max_memory_after = heap->GetMaxMemory();
59   int64_t total_memory_after = heap->GetTotalMemory();
60   EXPECT_GE(max_memory_after, max_memory_before);
61   EXPECT_GE(total_memory_after, total_memory_before);
62 }
63 
TEST_F(HeapTest,GarbageCollectClassLinkerInit)64 TEST_F(HeapTest, GarbageCollectClassLinkerInit) {
65   {
66     ScopedObjectAccess soa(Thread::Current());
67     // garbage is created during ClassLinker::Init
68 
69     StackHandleScope<1> hs(soa.Self());
70     Handle<mirror::Class> c(
71         hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
72     for (size_t i = 0; i < 1024; ++i) {
73       StackHandleScope<1> hs2(soa.Self());
74       Handle<mirror::ObjectArray<mirror::Object>> array(hs2.NewHandle(
75           mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), c.Get(), 2048)));
76       for (size_t j = 0; j < 2048; ++j) {
77         ObjPtr<mirror::String> string =
78             mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello, world!");
79         // handle scope operator -> deferences the handle scope before running the method.
80         array->Set<false>(j, string);
81       }
82     }
83   }
84   Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
85 }
86 
TEST_F(HeapTest,HeapBitmapCapacityTest)87 TEST_F(HeapTest, HeapBitmapCapacityTest) {
88   uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x1000);
89   const size_t heap_capacity = kObjectAlignment * (sizeof(intptr_t) * 8 + 1);
90   accounting::ContinuousSpaceBitmap bitmap(
91       accounting::ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
92   mirror::Object* fake_end_of_heap_object =
93       reinterpret_cast<mirror::Object*>(&heap_begin[heap_capacity - kObjectAlignment]);
94   bitmap.Set(fake_end_of_heap_object);
95 }
96 
TEST_F(HeapTest,DumpGCPerformanceOnShutdown)97 TEST_F(HeapTest, DumpGCPerformanceOnShutdown) {
98   Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
99   Runtime::Current()->SetDumpGCPerformanceOnShutdown(true);
100 }
101 
102 class ZygoteHeapTest : public CommonRuntimeTest {
SetUpRuntimeOptions(RuntimeOptions * options)103   void SetUpRuntimeOptions(RuntimeOptions* options) override {
104     CommonRuntimeTest::SetUpRuntimeOptions(options);
105     options->push_back(std::make_pair("-Xzygote", nullptr));
106   }
107 };
108 
TEST_F(ZygoteHeapTest,PreZygoteFork)109 TEST_F(ZygoteHeapTest, PreZygoteFork) {
110   // Exercise Heap::PreZygoteFork() to check it does not crash.
111   Runtime::Current()->GetHeap()->PreZygoteFork();
112 }
113 
114 }  // namespace gc
115 }  // namespace art
116