blob: 817c87681d3433e270fbd0044c1a6d59f0824b8d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom1f870082011-08-23 16:02:11 -070016
Vladimir Marko3481ba22015-04-13 12:22:36 +010017#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080018#include "common_runtime_test.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070019#include "gc/accounting/card_table-inl.h"
20#include "gc/accounting/space_bitmap-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class-inl.h"
23#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070024#include "mirror/object_array-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/object_array-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027
28namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
Brian Carlstrom1f870082011-08-23 16:02:11 -070030
Mathieu Chartierfa4ea822018-03-02 13:48:54 -080031class HeapTest : public CommonRuntimeTest {
32 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010033 void SetUp() override {
Mathieu Chartierfa4ea822018-03-02 13:48:54 -080034 MemMap::Init();
35 std::string error_msg;
36 // Reserve the preferred address to force the heap to use another one for testing.
Vladimir Markoc34bebf2018-08-16 16:12:49 +010037 reserved_ = MemMap::MapAnonymous("ReserveMap",
38 gc::Heap::kPreferredAllocSpaceBegin,
39 16 * KB,
40 PROT_READ,
Andreas Gampe98ea9d92018-10-19 14:06:15 -070041 /*low_4gb=*/ true,
Vladimir Marko11306592018-10-26 14:22:59 +010042 /*reuse=*/ false,
43 /*reservation=*/ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +010044 &error_msg);
45 ASSERT_TRUE(reserved_.IsValid()) << error_msg;
Mathieu Chartierfa4ea822018-03-02 13:48:54 -080046 CommonRuntimeTest::SetUp();
47 }
48
49 private:
Vladimir Markoc34bebf2018-08-16 16:12:49 +010050 MemMap reserved_;
Mathieu Chartierfa4ea822018-03-02 13:48:54 -080051};
Brian Carlstrom1f870082011-08-23 16:02:11 -070052
jeffhaoc1160702011-10-27 15:48:45 -070053TEST_F(HeapTest, ClearGrowthLimit) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080054 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();
jeffhaoc1160702011-10-27 15:48:45 -070060 EXPECT_GE(max_memory_after, max_memory_before);
61 EXPECT_GE(total_memory_after, total_memory_before);
62}
63
Brian Carlstrom693267a2011-09-06 09:25:34 -070064TEST_F(HeapTest, GarbageCollectClassLinkerInit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 {
66 ScopedObjectAccess soa(Thread::Current());
67 // garbage is created during ClassLinker::Init
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070068
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070069 StackHandleScope<1> hs(soa.Self());
70 Handle<mirror::Class> c(
71 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 for (size_t i = 0; i < 1024; ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -080073 StackHandleScope<1> hs2(soa.Self());
74 Handle<mirror::ObjectArray<mirror::Object>> array(hs2.NewHandle(
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070075 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), c.Get(), 2048)));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 for (size_t j = 0; j < 2048; ++j) {
Vladimir Marko179b7c62019-03-22 13:38:57 +000077 ObjPtr<mirror::String> string =
78 mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello, world!");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070079 // handle scope operator -> deferences the handle scope before running the method.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010080 array->Set<false>(j, string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070082 }
83 }
Andreas Gampe98ea9d92018-10-19 14:06:15 -070084 Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
Brian Carlstrom1f870082011-08-23 16:02:11 -070085}
86
Mathieu Chartier61c9cb52012-07-03 14:39:54 -070087TEST_F(HeapTest, HeapBitmapCapacityTest) {
Ian Rogers13735952014-10-08 12:43:28 -070088 uint8_t* heap_begin = reinterpret_cast<uint8_t*>(0x1000);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070089 const size_t heap_capacity = kObjectAlignment * (sizeof(intptr_t) * 8 + 1);
Mathieu Chartier6f382012019-07-30 09:47:35 -070090 accounting::ContinuousSpaceBitmap bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070091 accounting::ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
Ian Rogers1d54e732013-05-02 21:10:01 -070092 mirror::Object* fake_end_of_heap_object =
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070093 reinterpret_cast<mirror::Object*>(&heap_begin[heap_capacity - kObjectAlignment]);
Mathieu Chartier6f382012019-07-30 09:47:35 -070094 bitmap.Set(fake_end_of_heap_object);
Mathieu Chartier61c9cb52012-07-03 14:39:54 -070095}
96
Mathieu Chartier1d495012017-04-11 17:50:00 -070097TEST_F(HeapTest, DumpGCPerformanceOnShutdown) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -070098 Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references= */ false);
Mathieu Chartier1d495012017-04-11 17:50:00 -070099 Runtime::Current()->SetDumpGCPerformanceOnShutdown(true);
100}
101
Hiroshi Yamauchidb1c9ac2015-03-12 15:40:53 -0700102class ZygoteHeapTest : public CommonRuntimeTest {
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700103 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Hiroshi Yamauchidb1c9ac2015-03-12 15:40:53 -0700104 CommonRuntimeTest::SetUpRuntimeOptions(options);
105 options->push_back(std::make_pair("-Xzygote", nullptr));
106 }
107};
108
109TEST_F(ZygoteHeapTest, PreZygoteFork) {
110 // Exercise Heap::PreZygoteFork() to check it does not crash.
111 Runtime::Current()->GetHeap()->PreZygoteFork();
112}
113
Ian Rogers1d54e732013-05-02 21:10:01 -0700114} // namespace gc
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700115} // namespace art