2020-03-11 13:35:39 +00:00
|
|
|
// Copyright 2020 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 "src/heap/local-heap.h"
|
2021-02-05 13:15:49 +00:00
|
|
|
|
|
|
|
#include "src/base/platform/condition-variable.h"
|
|
|
|
#include "src/base/platform/mutex.h"
|
2020-03-11 13:35:39 +00:00
|
|
|
#include "src/heap/heap.h"
|
2021-02-05 13:15:49 +00:00
|
|
|
#include "src/heap/parked-scope.h"
|
2020-03-17 07:18:09 +00:00
|
|
|
#include "src/heap/safepoint.h"
|
2020-03-11 13:35:39 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
using LocalHeapTest = TestWithIsolate;
|
|
|
|
|
|
|
|
TEST_F(LocalHeapTest, Initialize) {
|
|
|
|
Heap* heap = i_isolate()->heap();
|
2020-11-17 10:16:09 +00:00
|
|
|
CHECK(heap->safepoint()->ContainsAnyLocalHeap());
|
2020-07-07 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalHeapTest, Current) {
|
|
|
|
Heap* heap = i_isolate()->heap();
|
|
|
|
|
|
|
|
CHECK_NULL(LocalHeap::Current());
|
|
|
|
|
|
|
|
{
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap lh(heap, ThreadKind::kMain);
|
2020-11-17 10:16:09 +00:00
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-07-07 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-03-11 13:35:39 +00:00
|
|
|
|
2020-07-07 15:34:26 +00:00
|
|
|
{
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap lh(heap, ThreadKind::kMain);
|
2020-11-17 10:16:09 +00:00
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-07-07 15:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_NULL(LocalHeap::Current());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class BackgroundThread final : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
explicit BackgroundThread(Heap* heap)
|
|
|
|
: v8::base::Thread(base::Thread::Options("BackgroundThread")),
|
|
|
|
heap_(heap) {}
|
|
|
|
|
|
|
|
void Run() override {
|
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-03-11 13:35:39 +00:00
|
|
|
{
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap lh(heap_, ThreadKind::kBackground);
|
2020-07-07 15:34:26 +00:00
|
|
|
CHECK_EQ(&lh, LocalHeap::Current());
|
2020-03-11 13:35:39 +00:00
|
|
|
}
|
2020-07-07 15:34:26 +00:00
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-03-11 13:35:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 15:34:26 +00:00
|
|
|
Heap* heap_;
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
TEST_F(LocalHeapTest, CurrentBackground) {
|
|
|
|
Heap* heap = i_isolate()->heap();
|
|
|
|
CHECK_NULL(LocalHeap::Current());
|
|
|
|
{
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap lh(heap, ThreadKind::kMain);
|
2020-07-07 15:34:26 +00:00
|
|
|
auto thread = std::make_unique<BackgroundThread>(heap);
|
|
|
|
CHECK(thread->Start());
|
2020-11-17 10:16:09 +00:00
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-07-07 15:34:26 +00:00
|
|
|
thread->Join();
|
2020-11-17 10:16:09 +00:00
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-07-07 15:34:26 +00:00
|
|
|
}
|
|
|
|
CHECK_NULL(LocalHeap::Current());
|
2020-03-11 13:35:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:15:49 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GCEpilogue {
|
|
|
|
public:
|
|
|
|
static void Callback(void* data) {
|
|
|
|
reinterpret_cast<GCEpilogue*>(data)->was_invoked_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyStarted() {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
started_ = true;
|
|
|
|
cv_.NotifyOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitUntilStarted() {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
while (!started_) {
|
|
|
|
cv_.Wait(&mutex_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void RequestStop() {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
stop_requested_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StopRequested() {
|
|
|
|
base::LockGuard<base::Mutex> lock_guard(&mutex_);
|
|
|
|
return stop_requested_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WasInvoked() { return was_invoked_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool was_invoked_ = false;
|
|
|
|
bool started_ = false;
|
|
|
|
bool stop_requested_ = false;
|
|
|
|
base::Mutex mutex_;
|
|
|
|
base::ConditionVariable cv_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BackgroundThreadForGCEpilogue final : public v8::base::Thread {
|
|
|
|
public:
|
2021-02-10 14:00:59 +00:00
|
|
|
explicit BackgroundThreadForGCEpilogue(Heap* heap, bool parked,
|
|
|
|
GCEpilogue* epilogue)
|
2021-02-05 13:15:49 +00:00
|
|
|
: v8::base::Thread(base::Thread::Options("BackgroundThread")),
|
|
|
|
heap_(heap),
|
2021-02-10 14:00:59 +00:00
|
|
|
parked_(parked),
|
2021-02-05 13:15:49 +00:00
|
|
|
epilogue_(epilogue) {}
|
|
|
|
|
|
|
|
void Run() override {
|
|
|
|
LocalHeap lh(heap_, ThreadKind::kBackground);
|
2021-02-10 14:00:59 +00:00
|
|
|
base::Optional<UnparkedScope> unparked_scope;
|
|
|
|
if (!parked_) {
|
|
|
|
unparked_scope.emplace(&lh);
|
|
|
|
}
|
2021-02-10 21:51:01 +00:00
|
|
|
{
|
2021-09-24 15:00:41 +00:00
|
|
|
base::Optional<UnparkedScope> nested_unparked_scope;
|
|
|
|
if (parked_) nested_unparked_scope.emplace(&lh);
|
2021-02-10 21:51:01 +00:00
|
|
|
lh.AddGCEpilogueCallback(&GCEpilogue::Callback, epilogue_);
|
|
|
|
}
|
2021-02-12 09:16:03 +00:00
|
|
|
epilogue_->NotifyStarted();
|
2021-02-05 13:15:49 +00:00
|
|
|
while (!epilogue_->StopRequested()) {
|
|
|
|
lh.Safepoint();
|
|
|
|
}
|
2021-02-10 21:51:01 +00:00
|
|
|
{
|
2021-09-24 15:00:41 +00:00
|
|
|
base::Optional<UnparkedScope> nested_unparked_scope;
|
|
|
|
if (parked_) nested_unparked_scope.emplace(&lh);
|
2021-02-10 21:51:01 +00:00
|
|
|
lh.RemoveGCEpilogueCallback(&GCEpilogue::Callback, epilogue_);
|
|
|
|
}
|
2021-02-05 13:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Heap* heap_;
|
2021-02-10 14:00:59 +00:00
|
|
|
bool parked_;
|
2021-02-05 13:15:49 +00:00
|
|
|
GCEpilogue* epilogue_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
TEST_F(LocalHeapTest, GCEpilogue) {
|
|
|
|
Heap* heap = i_isolate()->heap();
|
|
|
|
LocalHeap lh(heap, ThreadKind::kMain);
|
|
|
|
std::array<GCEpilogue, 3> epilogue;
|
2021-02-10 21:51:01 +00:00
|
|
|
{
|
|
|
|
UnparkedScope unparked(&lh);
|
|
|
|
lh.AddGCEpilogueCallback(&GCEpilogue::Callback, &epilogue[0]);
|
|
|
|
}
|
2021-02-05 13:15:49 +00:00
|
|
|
auto thread1 =
|
2021-02-10 14:00:59 +00:00
|
|
|
std::make_unique<BackgroundThreadForGCEpilogue>(heap, true, &epilogue[1]);
|
|
|
|
auto thread2 = std::make_unique<BackgroundThreadForGCEpilogue>(heap, false,
|
|
|
|
&epilogue[2]);
|
2021-02-05 13:15:49 +00:00
|
|
|
CHECK(thread1->Start());
|
|
|
|
CHECK(thread2->Start());
|
|
|
|
epilogue[1].WaitUntilStarted();
|
|
|
|
epilogue[2].WaitUntilStarted();
|
2021-03-29 09:04:50 +00:00
|
|
|
{
|
|
|
|
UnparkedScope scope(&lh);
|
|
|
|
heap->PreciseCollectAllGarbage(Heap::kNoGCFlags,
|
|
|
|
GarbageCollectionReason::kTesting);
|
|
|
|
}
|
2021-02-05 13:15:49 +00:00
|
|
|
epilogue[1].RequestStop();
|
|
|
|
epilogue[2].RequestStop();
|
|
|
|
thread1->Join();
|
|
|
|
thread2->Join();
|
2021-02-10 21:51:01 +00:00
|
|
|
{
|
|
|
|
UnparkedScope unparked(&lh);
|
|
|
|
lh.RemoveGCEpilogueCallback(&GCEpilogue::Callback, &epilogue[0]);
|
|
|
|
}
|
2021-02-05 13:15:49 +00:00
|
|
|
for (auto& e : epilogue) {
|
|
|
|
CHECK(e.WasInvoked());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:39 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|