summaryrefslogtreecommitdiff
path: root/deps/v8/src/heap/embedder-tracing.cc
blob: ceac516f9c4eb9df979828557535abc658131885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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"

#include "include/v8-cppgc.h"
#include "src/base/logging.h"
#include "src/handles/global-handles.h"
#include "src/heap/embedder-tracing-inl.h"
#include "src/heap/gc-tracer.h"
#include "src/heap/marking-worklist-inl.h"

namespace v8::internal {

START_ALLOW_USE_DEPRECATED()

void LocalEmbedderHeapTracer::SetRemoteTracer(EmbedderHeapTracer* tracer) {
  CHECK_NULL(cpp_heap_);
  if (remote_tracer_) remote_tracer_->v8_isolate_ = nullptr;

  remote_tracer_ = tracer;
  default_embedder_roots_handler_.SetTracer(tracer);
  if (remote_tracer_)
    remote_tracer_->v8_isolate_ = reinterpret_cast<v8::Isolate*>(isolate_);
}

void LocalEmbedderHeapTracer::SetCppHeap(CppHeap* cpp_heap) {
  CHECK_NULL(remote_tracer_);
  cpp_heap_ = cpp_heap;
}

namespace {
CppHeap::GarbageCollectionFlags ConvertTraceFlags(
    EmbedderHeapTracer::TraceFlags flags) {
  CppHeap::GarbageCollectionFlags result;
  if (flags & EmbedderHeapTracer::TraceFlags::kForced)
    result |= CppHeap::GarbageCollectionFlagValues::kForced;
  if (flags & EmbedderHeapTracer::TraceFlags::kReduceMemory)
    result |= CppHeap::GarbageCollectionFlagValues::kReduceMemory;
  return result;
}
}  // namespace

void LocalEmbedderHeapTracer::PrepareForTrace(
    EmbedderHeapTracer::TraceFlags flags) {
  if (cpp_heap_)
    cpp_heap()->InitializeTracing(cppgc::internal::CollectionType::kMajor,
                                  ConvertTraceFlags(flags));
}

void LocalEmbedderHeapTracer::TracePrologue(
    EmbedderHeapTracer::TraceFlags flags) {
  if (!InUse()) return;

  embedder_worklist_empty_ = false;
  if (cpp_heap_)
    cpp_heap()->StartTracing();
  else
    remote_tracer_->TracePrologue(flags);
}

void LocalEmbedderHeapTracer::TraceEpilogue() {
  if (!InUse()) return;

  // Resetting to state unknown as there may be follow up garbage collections
  // triggered from callbacks that have a different stack state.
  embedder_stack_state_ =
      EmbedderHeapTracer::EmbedderStackState::kMayContainHeapPointers;

  if (cpp_heap_) {
    cpp_heap()->TraceEpilogue();
  } else {
    EmbedderHeapTracer::TraceSummary summary;
    remote_tracer_->TraceEpilogue(&summary);
    UpdateRemoteStats(summary.allocated_size, summary.time);
  }
}

void LocalEmbedderHeapTracer::UpdateRemoteStats(size_t allocated_size,
                                                double time) {
  remote_stats_.used_size = allocated_size;
  // Force a check next time increased memory is reported. This allows for
  // setting limits close to actual heap sizes.
  remote_stats_.allocated_size_limit_for_check = 0;
  constexpr double kMinReportingTimeMs = 0.5;
  if (time > kMinReportingTimeMs) {
    isolate_->heap()->tracer()->RecordEmbedderSpeed(allocated_size, time);
  }
}

void LocalEmbedderHeapTracer::EnterFinalPause() {
  if (!InUse()) return;

  if (cpp_heap_)
    cpp_heap()->EnterFinalPause(embedder_stack_state_);
  else
    remote_tracer_->EnterFinalPause(embedder_stack_state_);
}

bool LocalEmbedderHeapTracer::Trace(double max_duration) {
  if (!InUse()) return true;

  return cpp_heap_ ? cpp_heap_->AdvanceTracing(max_duration)
                   : remote_tracer_->AdvanceTracing(max_duration);
}

bool LocalEmbedderHeapTracer::IsRemoteTracingDone() {
  return !InUse() || (cpp_heap_ ? cpp_heap()->IsTracingDone()
                                : remote_tracer_->IsTracingDone());
}

LocalEmbedderHeapTracer::ProcessingScope::ProcessingScope(
    LocalEmbedderHeapTracer* tracer)
    : tracer_(tracer), wrapper_descriptor_(tracer->wrapper_descriptor_) {
  DCHECK(!tracer_->cpp_heap_);
  wrapper_cache_.reserve(kWrapperCacheSize);
}

LocalEmbedderHeapTracer::ProcessingScope::~ProcessingScope() {
  DCHECK(!tracer_->cpp_heap_);
  if (!wrapper_cache_.empty()) {
    tracer_->remote_tracer_->RegisterV8References(std::move(wrapper_cache_));
  }
}

LocalEmbedderHeapTracer::WrapperInfo
LocalEmbedderHeapTracer::ExtractWrapperInfo(Isolate* isolate,
                                            JSObject js_object) {
  WrapperInfo info;
  if (ExtractWrappableInfo(isolate, js_object, wrapper_descriptor(), &info)) {
    return info;
  }
  return {nullptr, nullptr};
}

void LocalEmbedderHeapTracer::ProcessingScope::TracePossibleWrapper(
    JSObject js_object) {
  DCHECK(js_object.MayHaveEmbedderFields());
  WrapperInfo info;
  if (ExtractWrappableInfo(tracer_->isolate_, js_object, wrapper_descriptor_,
                           &info)) {
    wrapper_cache_.push_back(std::move(info));
    FlushWrapperCacheIfFull();
  }
}

void LocalEmbedderHeapTracer::ProcessingScope::FlushWrapperCacheIfFull() {
  DCHECK(!tracer_->cpp_heap_);
  if (wrapper_cache_.size() == wrapper_cache_.capacity()) {
    tracer_->remote_tracer_->RegisterV8References(std::move(wrapper_cache_));
    wrapper_cache_.clear();
    wrapper_cache_.reserve(kWrapperCacheSize);
  }
}

void LocalEmbedderHeapTracer::ProcessingScope::AddWrapperInfoForTesting(
    WrapperInfo info) {
  wrapper_cache_.push_back(info);
  FlushWrapperCacheIfFull();
}

void LocalEmbedderHeapTracer::StartIncrementalMarkingIfNeeded() {
  if (!v8_flags.global_gc_scheduling || !v8_flags.incremental_marking) return;

  Heap* heap = isolate_->heap();
  heap->StartIncrementalMarkingIfAllocationLimitIsReached(
      heap->GCFlagsForIncrementalMarking(),
      kGCCallbackScheduleIdleGarbageCollection);
  if (heap->AllocationLimitOvershotByLargeMargin()) {
    heap->FinalizeIncrementalMarkingAtomically(
        i::GarbageCollectionReason::kExternalFinalize);
  }
}

void LocalEmbedderHeapTracer::EmbedderWriteBarrier(Heap* heap,
                                                   JSObject js_object) {
  DCHECK(InUse());
  DCHECK(js_object.MayHaveEmbedderFields());
  if (cpp_heap_) {
    DCHECK_NOT_NULL(heap->mark_compact_collector());
    const EmbedderDataSlot type_slot(js_object,
                                     wrapper_descriptor_.wrappable_type_index);
    const EmbedderDataSlot instance_slot(
        js_object, wrapper_descriptor_.wrappable_instance_index);
    heap->mark_compact_collector()
        ->local_marking_worklists()
        ->cpp_marking_state()
        ->MarkAndPush(type_slot, instance_slot);
    return;
  }
  LocalEmbedderHeapTracer::ProcessingScope scope(this);
  scope.TracePossibleWrapper(js_object);
}

bool DefaultEmbedderRootsHandler::IsRoot(
    const v8::TracedReference<v8::Value>& handle) {
  return !tracer_ || tracer_->IsRootForNonTracingGC(handle);
}

void DefaultEmbedderRootsHandler::ResetRoot(
    const v8::TracedReference<v8::Value>& handle) {
  // Resetting is only called when IsRoot() returns false which
  // can only happen the EmbedderHeapTracer is set on API level.
  DCHECK(tracer_);
  tracer_->ResetHandleInNonTracingGC(handle);
}

END_ALLOW_USE_DEPRECATED()

}  // namespace v8::internal