test-page-promotion.cc 7 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/execution/isolate.h"
6
#include "src/heap/factory.h"
7
#include "src/heap/spaces-inl.h"
8
#include "src/objects/objects-inl.h"
9
#include "test/cctest/cctest.h"
10
#include "test/cctest/heap/heap-tester.h"
11 12
#include "test/cctest/heap/heap-utils.h"

13 14
namespace v8 {
namespace internal {
15
namespace heap {
16

17 18 19
// Tests don't work when --optimize-for-size is set.
#ifndef V8_LITE_MODE

20 21
namespace {

22 23
v8::Isolate* NewIsolateForPagePromotion(int min_semi_space_size = 8,
                                        int max_semi_space_size = 8) {
24 25 26
  // Parallel evacuation messes with fragmentation in a way that objects that
  // should be copied in semi space are promoted to old space because of
  // fragmentation.
27 28 29
  FLAG_parallel_compaction = false;
  FLAG_page_promotion = true;
  FLAG_page_promotion_threshold = 0;
30 31
  // Parallel scavenge introduces too much fragmentation.
  FLAG_parallel_scavenge = false;
32
  FLAG_min_semi_space_size = min_semi_space_size;
33 34
  // We cannot optimize for size as we require a new space with more than one
  // page.
35
  FLAG_optimize_for_size = false;
36 37
  // Set max_semi_space_size because it could've been initialized by an
  // implication of optimize_for_size.
38
  FLAG_max_semi_space_size = max_semi_space_size;
39 40 41 42 43 44
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
  return isolate;
}

45
Page* FindLastPageInNewSpace(const std::vector<Handle<FixedArray>>& handles) {
46
  for (auto rit = handles.rbegin(); rit != handles.rend(); ++rit) {
47 48
    // One deref gets the Handle, the second deref gets the FixedArray.
    Page* candidate = Page::FromHeapObject(**rit);
49 50 51 52
    if (candidate->InNewSpace()) return candidate;
  }
  return nullptr;
}
53

54
}  // namespace
55 56

UNINITIALIZED_TEST(PagePromotion_NewToOld) {
57
  if (i::FLAG_single_generation) return;
58
  if (!i::FLAG_incremental_marking) return;
59
  if (!i::FLAG_page_promotion) return;
60
  ManualGCScope manual_gc_scope;
61

62 63 64 65 66 67 68 69
  v8::Isolate* isolate = NewIsolateForPagePromotion();
  i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
  {
    v8::Isolate::Scope isolate_scope(isolate);
    v8::HandleScope handle_scope(isolate);
    v8::Context::New(isolate)->Enter();
    Heap* heap = i_isolate->heap();

70 71 72 73 74
    // Ensure that the new space is empty so that the page to be promoted
    // does not contain the age mark.
    heap->CollectGarbage(NEW_SPACE, i::GarbageCollectionReason::kTesting);
    heap->CollectGarbage(NEW_SPACE, i::GarbageCollectionReason::kTesting);

75 76
    std::vector<Handle<FixedArray>> handles;
    heap::SimulateFullSpace(heap->new_space(), &handles);
77
    heap->CollectGarbage(NEW_SPACE, i::GarbageCollectionReason::kTesting);
78
    CHECK_GT(handles.size(), 0u);
79 80
    Page* const to_be_promoted_page = FindLastPageInNewSpace(handles);
    CHECK_NOT_NULL(to_be_promoted_page);
81
    CHECK(!to_be_promoted_page->Contains(heap->new_space()->age_mark()));
82 83 84
    // To perform a sanity check on live bytes we need to mark the heap.
    heap::SimulateIncrementalMarking(heap, true);
    // Sanity check that the page meets the requirements for promotion.
85 86 87
    const int threshold_bytes = static_cast<int>(
        FLAG_page_promotion_threshold *
        MemoryChunkLayout::AllocatableMemoryInDataPage() / 100);
88
    CHECK_GE(heap->incremental_marking()->marking_state()->live_bytes(
89
                 to_be_promoted_page),
90
             threshold_bytes);
91 92 93

    // Actual checks: The page is in new space first, but is moved to old space
    // during a full GC.
94 95
    CHECK(heap->new_space()->ContainsSlow(to_be_promoted_page->address()));
    CHECK(!heap->old_space()->ContainsSlow(to_be_promoted_page->address()));
96
    heap::GcAndSweep(heap, OLD_SPACE);
97 98
    CHECK(!heap->new_space()->ContainsSlow(to_be_promoted_page->address()));
    CHECK(heap->old_space()->ContainsSlow(to_be_promoted_page->address()));
99
  }
100
  isolate->Dispose();
101 102 103
}

UNINITIALIZED_TEST(PagePromotion_NewToNew) {
104
  if (!i::FLAG_page_promotion || FLAG_always_promote_young_mc) return;
105

106 107 108 109 110 111 112 113 114 115 116
  v8::Isolate* isolate = NewIsolateForPagePromotion();
  Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
  {
    v8::Isolate::Scope isolate_scope(isolate);
    v8::HandleScope handle_scope(isolate);
    v8::Context::New(isolate)->Enter();
    Heap* heap = i_isolate->heap();

    std::vector<Handle<FixedArray>> handles;
    heap::SimulateFullSpace(heap->new_space(), &handles);
    CHECK_GT(handles.size(), 0u);
117 118
    // Last object in handles should definitely be on a page that does not
    // contain the age mark, thus qualifying for moving.
119
    Handle<FixedArray> last_object = handles.back();
120
    Page* to_be_promoted_page = Page::FromHeapObject(*last_object);
121
    CHECK(!to_be_promoted_page->Contains(heap->new_space()->age_mark()));
122 123 124 125 126 127
    CHECK(to_be_promoted_page->Contains(last_object->address()));
    CHECK(heap->new_space()->ToSpaceContainsSlow(last_object->address()));
    heap::GcAndSweep(heap, OLD_SPACE);
    CHECK(heap->new_space()->ToSpaceContainsSlow(last_object->address()));
    CHECK(to_be_promoted_page->Contains(last_object->address()));
  }
128
  isolate->Dispose();
129 130
}

131
UNINITIALIZED_HEAP_TEST(Regress658718) {
132
  if (!i::FLAG_page_promotion || FLAG_always_promote_young_mc) return;
133

134 135 136 137 138 139 140 141
  v8::Isolate* isolate = NewIsolateForPagePromotion(4, 8);
  Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
  {
    v8::Isolate::Scope isolate_scope(isolate);
    v8::HandleScope handle_scope(isolate);
    v8::Context::New(isolate)->Enter();
    Heap* heap = i_isolate->heap();
    heap->delay_sweeper_tasks_for_testing_ = true;
142
    GrowNewSpace(heap);
143 144 145 146 147
    {
      v8::HandleScope inner_handle_scope(isolate);
      std::vector<Handle<FixedArray>> handles;
      heap::SimulateFullSpace(heap->new_space(), &handles);
      CHECK_GT(handles.size(), 0u);
148 149
      // Last object in handles should definitely be on a page that does not
      // contain the age mark, thus qualifying for moving.
150
      Handle<FixedArray> last_object = handles.back();
151
      Page* to_be_promoted_page = Page::FromHeapObject(*last_object);
152
      CHECK(!to_be_promoted_page->Contains(heap->new_space()->age_mark()));
153 154 155 156 157 158 159 160
      CHECK(to_be_promoted_page->Contains(last_object->address()));
      CHECK(heap->new_space()->ToSpaceContainsSlow(last_object->address()));
      heap->CollectGarbage(OLD_SPACE, i::GarbageCollectionReason::kTesting);
      CHECK(heap->new_space()->ToSpaceContainsSlow(last_object->address()));
      CHECK(to_be_promoted_page->Contains(last_object->address()));
    }
    heap->CollectGarbage(NEW_SPACE, i::GarbageCollectionReason::kTesting);
    heap->new_space()->Shrink();
161
    heap->memory_allocator()->unmapper()->EnsureUnmappingCompleted();
162
    heap->delay_sweeper_tasks_for_testing_ = false;
163
    heap->mark_compact_collector()->sweeper()->StartSweeperTasks();
164 165
    heap->mark_compact_collector()->EnsureSweepingCompleted();
  }
166
  isolate->Dispose();
167 168
}

169 170
#endif  // V8_LITE_MODE

171
}  // namespace heap
172 173
}  // namespace internal
}  // namespace v8