test-incremental-marking.cc 4.16 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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.

#include <stdlib.h>

7 8
#include "src/heap/safepoint.h"

9 10 11 12 13 14 15 16 17 18
#ifdef __linux__
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#include <utility>

19
#include "src/handles/global-handles.h"
20
#include "src/heap/gc-tracer.h"
21 22
#include "src/heap/incremental-marking.h"
#include "src/heap/spaces.h"
23
#include "src/init/v8.h"
24
#include "src/objects/objects-inl.h"
25
#include "test/cctest/cctest.h"
26
#include "test/cctest/heap/heap-utils.h"
27 28 29 30 31

using v8::IdleTask;
using v8::Task;
using v8::Isolate;

32 33
namespace v8 {
namespace internal {
34
namespace heap {
35

36
class MockPlatform : public TestPlatform {
37
 public:
38 39 40
  MockPlatform()
      : taskrunner_(new MockTaskRunner()),
        old_platform_(i::V8::GetCurrentPlatform()) {
41 42
    // Now that it's completely constructed, make this the current platform.
    i::V8::SetPlatformForTesting(this);
43
  }
44
  ~MockPlatform() override {
45
    i::V8::SetPlatformForTesting(old_platform_);
46 47
    for (auto& task : worker_tasks_) {
      old_platform_->CallOnWorkerThread(std::move(task));
48 49
    }
    worker_tasks_.clear();
50
  }
51

52 53 54
  std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner(
      v8::Isolate* isolate) override {
    return taskrunner_;
55 56
  }

57 58
  void CallOnWorkerThread(std::unique_ptr<Task> task) override {
    worker_tasks_.push_back(std::move(task));
59 60
  }

61
  bool IdleTasksEnabled(v8::Isolate* isolate) override { return false; }
62

63
  bool PendingTask() { return taskrunner_->PendingTask(); }
64

65
  void PerformTask() { taskrunner_->PerformTask(); }
66 67

 private:
68 69 70 71 72 73
  class MockTaskRunner : public v8::TaskRunner {
   public:
    void PostTask(std::unique_ptr<v8::Task> task) override {
      task_ = std::move(task);
    }

74 75 76 77
    void PostNonNestableTask(std::unique_ptr<Task> task) override {
      PostTask(std::move(task));
    }

78 79
    void PostDelayedTask(std::unique_ptr<Task> task,
                         double delay_in_seconds) override {
80 81 82 83 84 85
      PostTask(std::move(task));
    }

    void PostNonNestableDelayedTask(std::unique_ptr<Task> task,
                                    double delay_in_seconds) override {
      PostTask(std::move(task));
86
    }
87 88 89 90 91

    void PostIdleTask(std::unique_ptr<IdleTask> task) override {
      UNREACHABLE();
    }

92
    bool IdleTasksEnabled() override { return false; }
93 94
    bool NonNestableTasksEnabled() const override { return true; }
    bool NonNestableDelayedTasksEnabled() const override { return true; }
95 96 97 98 99 100 101 102 103 104 105 106 107

    bool PendingTask() { return task_ != nullptr; }

    void PerformTask() {
      std::unique_ptr<Task> task = std::move(task_);
      task->Run();
    }

   private:
    std::unique_ptr<Task> task_;
  };

  std::shared_ptr<MockTaskRunner> taskrunner_;
108
  std::vector<std::unique_ptr<Task>> worker_tasks_;
109
  v8::Platform* old_platform_;
110 111
};

112
UNINITIALIZED_TEST(IncrementalMarkingUsingTasks) {
113
  if (!i::FLAG_incremental_marking) return;
114
  FLAG_stress_concurrent_allocation = false;  // For SimulateFullSpace.
115
  FLAG_stress_incremental_marking = false;
116
  MockPlatform platform;
117 118 119
  v8::Isolate::CreateParams create_params;
  create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
  v8::Isolate* isolate = v8::Isolate::New(create_params);
120
  {
121 122 123 124 125 126 127 128 129 130 131
    v8::HandleScope handle_scope(isolate);
    v8::Local<v8::Context> context = CcTest::NewContext(isolate);
    v8::Context::Scope context_scope(context);
    Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
    Heap* heap = i_isolate->heap();

    i::heap::SimulateFullSpace(heap->old_space());
    i::IncrementalMarking* marking = heap->incremental_marking();
    marking->Stop();
    {
      SafepointScope scope(heap);
132 133 134
      heap->tracer()->StartCycle(
          GarbageCollector::MARK_COMPACTOR, GarbageCollectionReason::kTesting,
          "collector cctest", GCTracer::MarkingType::kIncremental);
135 136 137 138 139 140 141
      marking->Start(i::GarbageCollectionReason::kTesting);
    }
    CHECK(platform.PendingTask());
    while (platform.PendingTask()) {
      platform.PerformTask();
    }
    CHECK(marking->IsStopped());
142
  }
143
  isolate->Dispose();
144 145
}

146
}  // namespace heap
147 148
}  // namespace internal
}  // namespace v8