blob: 9e882a898e1af2514395e62f9d271bc2d978213c [file] [log] [blame]
Mathieu Chartiera1602f22014-01-13 17:19:19 -08001/*
2 * Copyright (C) 2014 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 "zygote_space.h"
18
19#include "gc/accounting/card_table-inl.h"
20#include "gc/accounting/space_bitmap-inl.h"
21#include "gc/heap.h"
22#include "thread-inl.h"
23#include "utils.h"
24
25namespace art {
26namespace gc {
27namespace space {
28
29class CountObjectsAllocated {
30 public:
31 explicit CountObjectsAllocated(size_t* objects_allocated)
32 : objects_allocated_(objects_allocated) {}
33
34 void operator()(mirror::Object* obj) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070035 UNUSED(obj);
Mathieu Chartiera1602f22014-01-13 17:19:19 -080036 ++*objects_allocated_;
37 }
38
39 private:
40 size_t* const objects_allocated_;
41};
42
43ZygoteSpace* ZygoteSpace::Create(const std::string& name, MemMap* mem_map,
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070044 accounting::ContinuousSpaceBitmap* live_bitmap,
45 accounting::ContinuousSpaceBitmap* mark_bitmap) {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080046 DCHECK(live_bitmap != nullptr);
47 DCHECK(mark_bitmap != nullptr);
48 size_t objects_allocated = 0;
49 CountObjectsAllocated visitor(&objects_allocated);
50 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
51 live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(mem_map->Begin()),
52 reinterpret_cast<uintptr_t>(mem_map->End()), visitor);
53 ZygoteSpace* zygote_space = new ZygoteSpace(name, mem_map, objects_allocated);
54 CHECK(zygote_space->live_bitmap_.get() == nullptr);
55 CHECK(zygote_space->mark_bitmap_.get() == nullptr);
56 zygote_space->live_bitmap_.reset(live_bitmap);
57 zygote_space->mark_bitmap_.reset(mark_bitmap);
58 return zygote_space;
59}
60
Ian Rogers6fac4472014-02-25 17:01:10 -080061void ZygoteSpace::Clear() {
Ian Rogers2c4257b2014-10-24 14:20:06 -070062 UNIMPLEMENTED(FATAL);
63 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080064}
65
Mathieu Chartiera1602f22014-01-13 17:19:19 -080066ZygoteSpace::ZygoteSpace(const std::string& name, MemMap* mem_map, size_t objects_allocated)
67 : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
68 kGcRetentionPolicyFullCollect),
69 objects_allocated_(objects_allocated) {
70}
71
72void ZygoteSpace::Dump(std::ostream& os) const {
73 os << GetType()
74 << " begin=" << reinterpret_cast<void*>(Begin())
75 << ",end=" << reinterpret_cast<void*>(End())
76 << ",size=" << PrettySize(Size())
77 << ",name=\"" << GetName() << "\"]";
78}
79
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -070080mirror::Object* ZygoteSpace::Alloc(Thread*, size_t, size_t*, size_t*, size_t*) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070081 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080083}
84
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070085size_t ZygoteSpace::AllocationSize(mirror::Object*, size_t*) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070086 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070087 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080088}
89
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070090size_t ZygoteSpace::Free(Thread*, mirror::Object*) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070091 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070092 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080093}
94
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070095size_t ZygoteSpace::FreeList(Thread*, size_t, mirror::Object**) {
Mathieu Chartierb363f662014-07-16 13:28:58 -070096 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070097 UNREACHABLE();
Ian Rogers6fac4472014-02-25 17:01:10 -080098}
99
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700100void ZygoteSpace::LogFragmentationAllocFailure(std::ostream&, size_t) {
Mathieu Chartierb363f662014-07-16 13:28:58 -0700101 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700102 UNREACHABLE();
Mathieu Chartierb363f662014-07-16 13:28:58 -0700103}
104
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800105void ZygoteSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
106 SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
107 DCHECK(context->space->IsZygoteSpace());
108 ZygoteSpace* zygote_space = context->space->AsZygoteSpace();
109 Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
Mathieu Chartierbbd695c2014-04-16 09:48:48 -0700110 accounting::CardTable* card_table = Runtime::Current()->GetHeap()->GetCardTable();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800111 // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
112 // the bitmaps as an optimization.
113 if (!context->swap_bitmaps) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700114 accounting::ContinuousSpaceBitmap* bitmap = zygote_space->GetLiveBitmap();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800115 for (size_t i = 0; i < num_ptrs; ++i) {
116 bitmap->Clear(ptrs[i]);
117 }
118 }
119 // We don't free any actual memory to avoid dirtying the shared zygote pages.
120 for (size_t i = 0; i < num_ptrs; ++i) {
121 // Need to mark the card since this will update the mod-union table next GC cycle.
122 card_table->MarkCard(ptrs[i]);
123 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700124 zygote_space->objects_allocated_.FetchAndSubSequentiallyConsistent(num_ptrs);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800125}
126
127} // namespace space
128} // namespace gc
129} // namespace art