1a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier/*
2a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * Copyright (C) 2014 The Android Open Source Project
3a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier *
4a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License");
5a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * you may not use this file except in compliance with the License.
6a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * You may obtain a copy of the License at
7a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier *
8a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier *      http://www.apache.org/licenses/LICENSE-2.0
9a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier *
10a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * Unless required by applicable law or agreed to in writing, software
11a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS,
12a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * See the License for the specific language governing permissions and
14a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier * limitations under the License.
15a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier */
16a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
17a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier#include "zygote_space.h"
18a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
19a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier#include "gc/accounting/card_table-inl.h"
20a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier#include "gc/accounting/space_bitmap-inl.h"
21a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier#include "gc/heap.h"
22a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier#include "thread-inl.h"
23a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier#include "utils.h"
24a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
25a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiernamespace art {
26a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiernamespace gc {
27a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiernamespace space {
28a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
29a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartierclass CountObjectsAllocated {
30a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier public:
31a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  explicit CountObjectsAllocated(size_t* objects_allocated)
32a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      : objects_allocated_(objects_allocated) {}
33a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
344b8f1ecd3aa5a29ec1463ff88fee9db365f257dcRoland Levillain  void operator()(mirror::Object* obj ATTRIBUTE_UNUSED) const {
35a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    ++*objects_allocated_;
36a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  }
37a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
38a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier private:
39a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  size_t* const objects_allocated_;
40a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier};
41a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
42a1602f28c0e3127ad511712d4b08db89737ae901Mathieu ChartierZygoteSpace* ZygoteSpace::Create(const std::string& name, MemMap* mem_map,
43a8e8f9c0a8e259a807d7b99a148d14104c24209dMathieu Chartier                                 accounting::ContinuousSpaceBitmap* live_bitmap,
44a8e8f9c0a8e259a807d7b99a148d14104c24209dMathieu Chartier                                 accounting::ContinuousSpaceBitmap* mark_bitmap) {
45a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  DCHECK(live_bitmap != nullptr);
46a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  DCHECK(mark_bitmap != nullptr);
47a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  size_t objects_allocated = 0;
48a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  CountObjectsAllocated visitor(&objects_allocated);
49a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
50a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(mem_map->Begin()),
51a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier                                reinterpret_cast<uintptr_t>(mem_map->End()), visitor);
52a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  ZygoteSpace* zygote_space = new ZygoteSpace(name, mem_map, objects_allocated);
53a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  CHECK(zygote_space->live_bitmap_.get() == nullptr);
54a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  CHECK(zygote_space->mark_bitmap_.get() == nullptr);
55a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  zygote_space->live_bitmap_.reset(live_bitmap);
56a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  zygote_space->mark_bitmap_.reset(mark_bitmap);
57a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  return zygote_space;
58a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
59a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
606fac447555dc94a935b78198479cce645c837b89Ian Rogersvoid ZygoteSpace::Clear() {
612c4257be8191c5eefde744e8965fcefc80a0a97dIan Rogers  UNIMPLEMENTED(FATAL);
622c4257be8191c5eefde744e8965fcefc80a0a97dIan Rogers  UNREACHABLE();
636fac447555dc94a935b78198479cce645c837b89Ian Rogers}
646fac447555dc94a935b78198479cce645c837b89Ian Rogers
65a1602f28c0e3127ad511712d4b08db89737ae901Mathieu ChartierZygoteSpace::ZygoteSpace(const std::string& name, MemMap* mem_map, size_t objects_allocated)
66a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
67a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier                                 kGcRetentionPolicyFullCollect),
68a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      objects_allocated_(objects_allocated) {
69a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
70a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
71a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiervoid ZygoteSpace::Dump(std::ostream& os) const {
72a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  os << GetType()
73a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << " begin=" << reinterpret_cast<void*>(Begin())
74a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << ",end=" << reinterpret_cast<void*>(End())
75a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << ",size=" << PrettySize(Size())
76a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << ",name=\"" << GetName() << "\"]";
77a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
78a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
794460a84be92b5a94ecfb5c650aef4945ab849c93Hiroshi Yamauchimirror::Object* ZygoteSpace::Alloc(Thread*, size_t, size_t*, size_t*, size_t*) {
80b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier  UNIMPLEMENTED(FATAL);
816a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNREACHABLE();
826fac447555dc94a935b78198479cce645c837b89Ian Rogers}
836fac447555dc94a935b78198479cce645c837b89Ian Rogers
846a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogerssize_t ZygoteSpace::AllocationSize(mirror::Object*, size_t*) {
85b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier  UNIMPLEMENTED(FATAL);
866a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNREACHABLE();
876fac447555dc94a935b78198479cce645c837b89Ian Rogers}
886fac447555dc94a935b78198479cce645c837b89Ian Rogers
896a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogerssize_t ZygoteSpace::Free(Thread*, mirror::Object*) {
90b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier  UNIMPLEMENTED(FATAL);
916a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNREACHABLE();
926fac447555dc94a935b78198479cce645c837b89Ian Rogers}
936fac447555dc94a935b78198479cce645c837b89Ian Rogers
946a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogerssize_t ZygoteSpace::FreeList(Thread*, size_t, mirror::Object**) {
95b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier  UNIMPLEMENTED(FATAL);
966a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNREACHABLE();
976fac447555dc94a935b78198479cce645c837b89Ian Rogers}
986fac447555dc94a935b78198479cce645c837b89Ian Rogers
996a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogersvoid ZygoteSpace::LogFragmentationAllocFailure(std::ostream&, size_t) {
100b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier  UNIMPLEMENTED(FATAL);
1016a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNREACHABLE();
102b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier}
103b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier
104a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiervoid ZygoteSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
105a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
106a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  DCHECK(context->space->IsZygoteSpace());
107a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  ZygoteSpace* zygote_space = context->space->AsZygoteSpace();
108a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
109bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier  accounting::CardTable* card_table = Runtime::Current()->GetHeap()->GetCardTable();
110a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
111a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  // the bitmaps as an optimization.
112a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  if (!context->swap_bitmaps) {
113a8e8f9c0a8e259a807d7b99a148d14104c24209dMathieu Chartier    accounting::ContinuousSpaceBitmap* bitmap = zygote_space->GetLiveBitmap();
114a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    for (size_t i = 0; i < num_ptrs; ++i) {
115a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      bitmap->Clear(ptrs[i]);
116a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    }
117a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  }
118a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  // We don't free any actual memory to avoid dirtying the shared zygote pages.
119a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  for (size_t i = 0; i < num_ptrs; ++i) {
120a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    // Need to mark the card since this will update the mod-union table next GC cycle.
121a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    card_table->MarkCard(ptrs[i]);
122a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  }
1233e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers  zygote_space->objects_allocated_.FetchAndSubSequentiallyConsistent(num_ptrs);
124a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
125a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
126a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}  // namespace space
127a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}  // namespace gc
128a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}  // namespace art
129