2016-12-20 09:35:05 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "src/heap/embedder-tracing.h"
|
2018-08-21 17:14:19 +00:00
|
|
|
#include "src/heap/heap.h"
|
2018-07-11 11:23:10 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
2016-12-20 09:35:05 +00:00
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2018-07-11 11:23:10 +00:00
|
|
|
|
|
|
|
using LocalEmbedderHeapTracerWithIsolate = TestWithIsolate;
|
|
|
|
|
2016-12-20 09:35:05 +00:00
|
|
|
namespace heap {
|
|
|
|
|
|
|
|
using testing::StrictMock;
|
|
|
|
using testing::_;
|
|
|
|
using testing::Return;
|
|
|
|
using v8::EmbedderHeapTracer;
|
|
|
|
using v8::internal::LocalEmbedderHeapTracer;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
LocalEmbedderHeapTracer::WrapperInfo CreateWrapperInfo() {
|
|
|
|
return LocalEmbedderHeapTracer::WrapperInfo(nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class MockEmbedderHeapTracer : public EmbedderHeapTracer {
|
|
|
|
public:
|
2020-06-10 11:08:40 +00:00
|
|
|
MOCK_METHOD(void, TracePrologue, (EmbedderHeapTracer::TraceFlags),
|
|
|
|
(override));
|
|
|
|
MOCK_METHOD(void, TraceEpilogue, (EmbedderHeapTracer::TraceSummary*),
|
|
|
|
(override));
|
|
|
|
MOCK_METHOD(void, EnterFinalPause, (EmbedderHeapTracer::EmbedderStackState),
|
|
|
|
(override));
|
|
|
|
MOCK_METHOD(bool, IsTracingDone, (), (override));
|
|
|
|
MOCK_METHOD(void, RegisterV8References,
|
|
|
|
((const std::vector<std::pair<void*, void*> >&)), (override));
|
|
|
|
MOCK_METHOD(bool, AdvanceTracing, (double deadline_in_ms), (override));
|
2016-12-20 09:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(LocalEmbedderHeapTracer, InUse) {
|
|
|
|
MockEmbedderHeapTracer mock_remote_tracer;
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.SetRemoteTracer(&mock_remote_tracer);
|
|
|
|
EXPECT_TRUE(local_tracer.InUse());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LocalEmbedderHeapTracer, NoRemoteTracer) {
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
// We should be able to call all functions without a remote tracer being
|
|
|
|
// attached.
|
|
|
|
EXPECT_FALSE(local_tracer.InUse());
|
2019-04-29 16:01:01 +00:00
|
|
|
local_tracer.TracePrologue(EmbedderHeapTracer::TraceFlags::kNoFlags);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
2018-08-21 17:14:19 +00:00
|
|
|
bool done = local_tracer.Trace(std::numeric_limits<double>::infinity());
|
|
|
|
EXPECT_TRUE(done);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.TraceEpilogue();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LocalEmbedderHeapTracer, TracePrologueForwards) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
2019-04-29 16:01:01 +00:00
|
|
|
EXPECT_CALL(remote_tracer, TracePrologue(_));
|
|
|
|
local_tracer.TracePrologue(EmbedderHeapTracer::TraceFlags::kNoFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LocalEmbedderHeapTracer, TracePrologueForwardsMemoryReducingFlag) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
EXPECT_CALL(remote_tracer,
|
|
|
|
TracePrologue(EmbedderHeapTracer::TraceFlags::kReduceMemory));
|
|
|
|
local_tracer.TracePrologue(EmbedderHeapTracer::TraceFlags::kReduceMemory);
|
2016-12-20 09:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LocalEmbedderHeapTracer, TraceEpilogueForwards) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
2019-05-22 09:33:08 +00:00
|
|
|
EXPECT_CALL(remote_tracer, TraceEpilogue(_));
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.TraceEpilogue();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LocalEmbedderHeapTracer, EnterFinalPauseForwards) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
2018-08-21 17:14:19 +00:00
|
|
|
EXPECT_CALL(remote_tracer, EnterFinalPause(_));
|
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
|
|
|
|
2018-11-26 15:24:39 +00:00
|
|
|
TEST(LocalEmbedderHeapTracer, IsRemoteTracingDoneForwards) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
EXPECT_CALL(remote_tracer, IsTracingDone());
|
|
|
|
local_tracer.IsRemoteTracingDone();
|
|
|
|
}
|
|
|
|
|
2018-08-21 17:14:19 +00:00
|
|
|
TEST(LocalEmbedderHeapTracer, EnterFinalPauseDefaultStackStateUnkown) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
// The default stack state is expected to be unkown.
|
2020-05-12 11:09:49 +00:00
|
|
|
EXPECT_CALL(
|
|
|
|
remote_tracer,
|
|
|
|
EnterFinalPause(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kMayContainHeapPointers));
|
2018-08-21 17:14:19 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:26:48 +00:00
|
|
|
TEST_F(LocalEmbedderHeapTracerWithIsolate,
|
|
|
|
EnterFinalPauseStackStateIsForwarded) {
|
2018-08-21 17:14:19 +00:00
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2020-01-14 11:26:48 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(isolate());
|
2018-08-21 17:14:19 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
local_tracer.SetEmbedderStackStateForNextFinalization(
|
2020-05-12 11:09:49 +00:00
|
|
|
EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers);
|
|
|
|
EXPECT_CALL(
|
|
|
|
remote_tracer,
|
|
|
|
EnterFinalPause(EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers));
|
2018-08-21 17:14:19 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:26:48 +00:00
|
|
|
TEST_F(LocalEmbedderHeapTracerWithIsolate, TemporaryEmbedderStackState) {
|
2018-10-10 17:36:45 +00:00
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2020-01-14 11:26:48 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(isolate());
|
2018-10-10 17:36:45 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
// Default is unknown, see above.
|
|
|
|
{
|
2020-05-12 11:09:49 +00:00
|
|
|
EmbedderStackStateScope scope(
|
|
|
|
&local_tracer, EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers);
|
|
|
|
EXPECT_CALL(remote_tracer,
|
|
|
|
EnterFinalPause(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers));
|
2018-10-10 17:36:45 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:26:48 +00:00
|
|
|
TEST_F(LocalEmbedderHeapTracerWithIsolate,
|
|
|
|
TemporaryEmbedderStackStateRestores) {
|
2018-10-10 17:36:45 +00:00
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2020-01-14 11:26:48 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(isolate());
|
2018-10-10 17:36:45 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
// Default is unknown, see above.
|
|
|
|
{
|
2020-05-12 11:09:49 +00:00
|
|
|
EmbedderStackStateScope scope(
|
|
|
|
&local_tracer, EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers);
|
2018-10-10 17:36:45 +00:00
|
|
|
{
|
2020-05-12 11:09:49 +00:00
|
|
|
EmbedderStackStateScope scope(
|
|
|
|
&local_tracer,
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kMayContainHeapPointers);
|
|
|
|
EXPECT_CALL(
|
|
|
|
remote_tracer,
|
|
|
|
EnterFinalPause(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kMayContainHeapPointers));
|
2018-10-10 17:36:45 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
2020-05-12 11:09:49 +00:00
|
|
|
EXPECT_CALL(remote_tracer,
|
|
|
|
EnterFinalPause(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers));
|
2018-10-10 17:36:45 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 11:26:48 +00:00
|
|
|
TEST_F(LocalEmbedderHeapTracerWithIsolate, EnterFinalPauseStackStateResets) {
|
2018-08-21 17:14:19 +00:00
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2020-01-14 11:26:48 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(isolate());
|
2018-08-21 17:14:19 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
local_tracer.SetEmbedderStackStateForNextFinalization(
|
2020-05-12 11:09:49 +00:00
|
|
|
EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers);
|
|
|
|
EXPECT_CALL(
|
|
|
|
remote_tracer,
|
|
|
|
EnterFinalPause(EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers));
|
2018-08-21 17:14:19 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
2020-05-12 11:09:49 +00:00
|
|
|
EXPECT_CALL(
|
|
|
|
remote_tracer,
|
|
|
|
EnterFinalPause(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kMayContainHeapPointers));
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.EnterFinalPause();
|
|
|
|
}
|
|
|
|
|
2018-07-06 12:25:51 +00:00
|
|
|
TEST(LocalEmbedderHeapTracer, IsRemoteTracingDoneIncludesRemote) {
|
2016-12-20 09:35:05 +00:00
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
2018-07-06 12:25:51 +00:00
|
|
|
EXPECT_CALL(remote_tracer, IsTracingDone());
|
|
|
|
local_tracer.IsRemoteTracingDone();
|
2016-12-20 09:35:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 15:24:39 +00:00
|
|
|
TEST(LocalEmbedderHeapTracer, RegisterV8ReferencesWithRemoteTracer) {
|
2016-12-20 09:35:05 +00:00
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
2018-07-11 11:23:10 +00:00
|
|
|
LocalEmbedderHeapTracer local_tracer(nullptr);
|
2016-12-20 09:35:05 +00:00
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
2018-11-26 15:24:39 +00:00
|
|
|
{
|
|
|
|
LocalEmbedderHeapTracer::ProcessingScope scope(&local_tracer);
|
|
|
|
scope.AddWrapperInfoForTesting(CreateWrapperInfo());
|
|
|
|
EXPECT_CALL(remote_tracer, RegisterV8References(_));
|
|
|
|
}
|
2018-07-06 12:25:51 +00:00
|
|
|
EXPECT_CALL(remote_tracer, IsTracingDone()).WillOnce(Return(false));
|
|
|
|
EXPECT_FALSE(local_tracer.IsRemoteTracingDone());
|
2016-12-20 09:35:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 11:23:10 +00:00
|
|
|
TEST_F(LocalEmbedderHeapTracerWithIsolate, SetRemoteTracerSetsIsolate) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
|
|
|
LocalEmbedderHeapTracer local_tracer(isolate());
|
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
EXPECT_EQ(isolate(), reinterpret_cast<Isolate*>(remote_tracer.isolate()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalEmbedderHeapTracerWithIsolate, DestructorClearsIsolate) {
|
|
|
|
StrictMock<MockEmbedderHeapTracer> remote_tracer;
|
|
|
|
{
|
|
|
|
LocalEmbedderHeapTracer local_tracer(isolate());
|
|
|
|
local_tracer.SetRemoteTracer(&remote_tracer);
|
|
|
|
EXPECT_EQ(isolate(), reinterpret_cast<Isolate*>(remote_tracer.isolate()));
|
|
|
|
}
|
|
|
|
EXPECT_EQ(nullptr, remote_tracer.isolate());
|
|
|
|
}
|
|
|
|
|
2016-12-20 09:35:05 +00:00
|
|
|
} // namespace heap
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|