zygote_space.cc revision 6fac447555dc94a935b78198479cce645c837b89
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
34a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  void operator()(mirror::Object* obj) 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,
43a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier                                 accounting::SpaceBitmap* live_bitmap,
44a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier                                 accounting::SpaceBitmap* 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() {
616fac447555dc94a935b78198479cce645c837b89Ian Rogers  LOG(FATAL) << "Unimplemented";
626fac447555dc94a935b78198479cce645c837b89Ian Rogers}
636fac447555dc94a935b78198479cce645c837b89Ian Rogers
64a1602f28c0e3127ad511712d4b08db89737ae901Mathieu ChartierZygoteSpace::ZygoteSpace(const std::string& name, MemMap* mem_map, size_t objects_allocated)
65a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    : ContinuousMemMapAllocSpace(name, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
66a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier                                 kGcRetentionPolicyFullCollect),
67a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      objects_allocated_(objects_allocated) {
68a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
69a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
70a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiervoid ZygoteSpace::Dump(std::ostream& os) const {
71a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  os << GetType()
72a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << " begin=" << reinterpret_cast<void*>(Begin())
73a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << ",end=" << reinterpret_cast<void*>(End())
74a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << ",size=" << PrettySize(Size())
75a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      << ",name=\"" << GetName() << "\"]";
76a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
77a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
786fac447555dc94a935b78198479cce645c837b89Ian Rogersmirror::Object* ZygoteSpace::Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
796fac447555dc94a935b78198479cce645c837b89Ian Rogers                                   size_t* usable_size) {
806fac447555dc94a935b78198479cce645c837b89Ian Rogers  LOG(FATAL) << "Unimplemented";
816fac447555dc94a935b78198479cce645c837b89Ian Rogers  return nullptr;
826fac447555dc94a935b78198479cce645c837b89Ian Rogers}
836fac447555dc94a935b78198479cce645c837b89Ian Rogers
846fac447555dc94a935b78198479cce645c837b89Ian Rogerssize_t ZygoteSpace::AllocationSize(mirror::Object* obj, size_t* usable_size) {
856fac447555dc94a935b78198479cce645c837b89Ian Rogers  LOG(FATAL) << "Unimplemented";
866fac447555dc94a935b78198479cce645c837b89Ian Rogers  return 0;
876fac447555dc94a935b78198479cce645c837b89Ian Rogers}
886fac447555dc94a935b78198479cce645c837b89Ian Rogers
896fac447555dc94a935b78198479cce645c837b89Ian Rogerssize_t ZygoteSpace::Free(Thread* self, mirror::Object* ptr) {
906fac447555dc94a935b78198479cce645c837b89Ian Rogers  LOG(FATAL) << "Unimplemented";
916fac447555dc94a935b78198479cce645c837b89Ian Rogers  return 0;
926fac447555dc94a935b78198479cce645c837b89Ian Rogers}
936fac447555dc94a935b78198479cce645c837b89Ian Rogers
946fac447555dc94a935b78198479cce645c837b89Ian Rogerssize_t ZygoteSpace::FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) {
956fac447555dc94a935b78198479cce645c837b89Ian Rogers  LOG(FATAL) << "Unimplemented";
966fac447555dc94a935b78198479cce645c837b89Ian Rogers  return 0;
976fac447555dc94a935b78198479cce645c837b89Ian Rogers}
986fac447555dc94a935b78198479cce645c837b89Ian Rogers
99a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartiervoid ZygoteSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
100a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
101a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  DCHECK(context->space->IsZygoteSpace());
102a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  ZygoteSpace* zygote_space = context->space->AsZygoteSpace();
103a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
104a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  accounting::CardTable* card_table = context->heap->GetCardTable();
105a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
106a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  // the bitmaps as an optimization.
107a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  if (!context->swap_bitmaps) {
108a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    accounting::SpaceBitmap* bitmap = zygote_space->GetLiveBitmap();
109a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    for (size_t i = 0; i < num_ptrs; ++i) {
110a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier      bitmap->Clear(ptrs[i]);
111a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    }
112a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  }
113a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  // We don't free any actual memory to avoid dirtying the shared zygote pages.
114a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  for (size_t i = 0; i < num_ptrs; ++i) {
115a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    // Need to mark the card since this will update the mod-union table next GC cycle.
116a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier    card_table->MarkCard(ptrs[i]);
117a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  }
118a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier  zygote_space->objects_allocated_.FetchAndSub(num_ptrs);
119a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}
120a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier
121a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}  // namespace space
122a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}  // namespace gc
123a1602f28c0e3127ad511712d4b08db89737ae901Mathieu Chartier}  // namespace art
124