Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 16 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 17 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 18 | #include "common_runtime_test.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | #include "gc/accounting/card_table-inl.h" |
| 20 | #include "gc/accounting/space_bitmap-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 21 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/class-inl.h" |
| 23 | #include "mirror/object-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 24 | #include "mirror/object_array-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | namespace gc { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 30 | |
Mathieu Chartier | fa4ea82 | 2018-03-02 13:48:54 -0800 | [diff] [blame] | 31 | class HeapTest : public CommonRuntimeTest { |
| 32 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 33 | void SetUp() override { |
Mathieu Chartier | fa4ea82 | 2018-03-02 13:48:54 -0800 | [diff] [blame] | 34 | MemMap::Init(); |
| 35 | std::string error_msg; |
| 36 | // Reserve the preferred address to force the heap to use another one for testing. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 37 | reserved_ = MemMap::MapAnonymous("ReserveMap", |
| 38 | gc::Heap::kPreferredAllocSpaceBegin, |
| 39 | 16 * KB, |
| 40 | PROT_READ, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 41 | /*low_4gb=*/ true, |
Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 42 | /*reuse=*/ false, |
| 43 | /*reservation=*/ nullptr, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 44 | &error_msg); |
| 45 | ASSERT_TRUE(reserved_.IsValid()) << error_msg; |
Mathieu Chartier | fa4ea82 | 2018-03-02 13:48:54 -0800 | [diff] [blame] | 46 | CommonRuntimeTest::SetUp(); |
| 47 | } |
| 48 | |
| 49 | private: |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 50 | MemMap reserved_; |
Mathieu Chartier | fa4ea82 | 2018-03-02 13:48:54 -0800 | [diff] [blame] | 51 | }; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 52 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 53 | TEST_F(HeapTest, ClearGrowthLimit) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 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(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 60 | EXPECT_GE(max_memory_after, max_memory_before); |
| 61 | EXPECT_GE(total_memory_after, total_memory_before); |
| 62 | } |
| 63 | |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 64 | TEST_F(HeapTest, GarbageCollectClassLinkerInit) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | { |
| 66 | ScopedObjectAccess soa(Thread::Current()); |
| 67 | // garbage is created during ClassLinker::Init |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 68 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 69 | StackHandleScope<1> hs(soa.Self()); |
| 70 | Handle<mirror::Class> c( |
| 71 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"))); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | for (size_t i = 0; i < 1024; ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 73 | StackHandleScope<1> hs2(soa.Self()); |
| 74 | Handle<mirror::ObjectArray<mirror::Object>> array(hs2.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 75 | mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), c.Get(), 2048))); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 76 | for (size_t j = 0; j < 2048; ++j) { |
Vladimir Marko | 179b7c6 | 2019-03-22 13:38:57 +0000 | [diff] [blame] | 77 | ObjPtr<mirror::String> string = |
| 78 | mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello, world!"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 79 | // handle scope operator -> deferences the handle scope before running the method. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 80 | array->Set<false>(j, string); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 81 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 84 | Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Mathieu Chartier | 61c9cb5 | 2012-07-03 14:39:54 -0700 | [diff] [blame] | 87 | TEST_F(HeapTest, HeapBitmapCapacityTest) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 88 | uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x1000); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 89 | const size_t heap_capacity = kObjectAlignment * (sizeof(intptr_t) * 8 + 1); |
Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 90 | accounting::ContinuousSpaceBitmap bitmap( |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 91 | accounting::ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 92 | mirror::Object* fake_end_of_heap_object = |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 93 | reinterpret_cast<mirror::Object*>(&heap_begin[heap_capacity - kObjectAlignment]); |
Mathieu Chartier | 6f38201 | 2019-07-30 09:47:35 -0700 | [diff] [blame] | 94 | bitmap.Set(fake_end_of_heap_object); |
Mathieu Chartier | 61c9cb5 | 2012-07-03 14:39:54 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Mathieu Chartier | 1d49501 | 2017-04-11 17:50:00 -0700 | [diff] [blame] | 97 | TEST_F(HeapTest, DumpGCPerformanceOnShutdown) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 98 | Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false); |
Mathieu Chartier | 1d49501 | 2017-04-11 17:50:00 -0700 | [diff] [blame] | 99 | Runtime::Current()->SetDumpGCPerformanceOnShutdown(true); |
| 100 | } |
| 101 | |
Hiroshi Yamauchi | db1c9ac | 2015-03-12 15:40:53 -0700 | [diff] [blame] | 102 | class ZygoteHeapTest : public CommonRuntimeTest { |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 103 | void SetUpRuntimeOptions(RuntimeOptions* options) override { |
Hiroshi Yamauchi | db1c9ac | 2015-03-12 15:40:53 -0700 | [diff] [blame] | 104 | CommonRuntimeTest::SetUpRuntimeOptions(options); |
| 105 | options->push_back(std::make_pair("-Xzygote", nullptr)); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | TEST_F(ZygoteHeapTest, PreZygoteFork) { |
| 110 | // Exercise Heap::PreZygoteFork() to check it does not crash. |
| 111 | Runtime::Current()->GetHeap()->PreZygoteFork(); |
| 112 | } |
| 113 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 114 | } // namespace gc |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 115 | } // namespace art |