1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_HEAP_EMBEDDER_TRACING_H_ 6 #define V8_HEAP_EMBEDDER_TRACING_H_ 7 8 #include "include/v8.h" 9 #include "src/flags.h" 10 #include "src/globals.h" 11 12 namespace v8 { 13 namespace internal { 14 15 class Heap; 16 17 class V8_EXPORT_PRIVATE LocalEmbedderHeapTracer final { 18 public: 19 typedef std::pair<void*, void*> WrapperInfo; 20 LocalEmbedderHeapTracer(Isolate * isolate)21 explicit LocalEmbedderHeapTracer(Isolate* isolate) : isolate_(isolate) {} 22 ~LocalEmbedderHeapTracer()23 ~LocalEmbedderHeapTracer() { 24 if (remote_tracer_) remote_tracer_->isolate_ = nullptr; 25 } 26 SetRemoteTracer(EmbedderHeapTracer * tracer)27 void SetRemoteTracer(EmbedderHeapTracer* tracer) { 28 if (remote_tracer_) remote_tracer_->isolate_ = nullptr; 29 30 remote_tracer_ = tracer; 31 if (remote_tracer_) 32 remote_tracer_->isolate_ = reinterpret_cast<v8::Isolate*>(isolate_); 33 } 34 InUse()35 bool InUse() const { return remote_tracer_ != nullptr; } 36 37 void TracePrologue(); 38 void TraceEpilogue(); 39 void AbortTracing(); 40 void EnterFinalPause(); 41 bool Trace(double deadline); 42 bool IsRemoteTracingDone(); 43 NumberOfCachedWrappersToTrace()44 size_t NumberOfCachedWrappersToTrace() { 45 return cached_wrappers_to_trace_.size(); 46 } AddWrapperToTrace(WrapperInfo entry)47 void AddWrapperToTrace(WrapperInfo entry) { 48 cached_wrappers_to_trace_.push_back(entry); 49 } ClearCachedWrappersToTrace()50 void ClearCachedWrappersToTrace() { cached_wrappers_to_trace_.clear(); } 51 void RegisterWrappersWithRemoteTracer(); 52 53 // In order to avoid running out of memory we force tracing wrappers if there 54 // are too many of them. 55 bool RequiresImmediateWrapperProcessing(); 56 NotifyV8MarkingWorklistWasEmpty()57 void NotifyV8MarkingWorklistWasEmpty() { 58 num_v8_marking_worklist_was_empty_++; 59 } ShouldFinalizeIncrementalMarking()60 bool ShouldFinalizeIncrementalMarking() { 61 static const size_t kMaxIncrementalFixpointRounds = 3; 62 return !FLAG_incremental_marking_wrappers || !InUse() || 63 IsRemoteTracingDone() || 64 num_v8_marking_worklist_was_empty_ > kMaxIncrementalFixpointRounds; 65 } 66 67 void SetEmbedderStackStateForNextFinalization( 68 EmbedderHeapTracer::EmbedderStackState stack_state); 69 70 private: 71 typedef std::vector<WrapperInfo> WrapperCache; 72 73 Isolate* const isolate_; 74 WrapperCache cached_wrappers_to_trace_; 75 EmbedderHeapTracer* remote_tracer_ = nullptr; 76 size_t num_v8_marking_worklist_was_empty_ = 0; 77 EmbedderHeapTracer::EmbedderStackState embedder_stack_state_ = 78 EmbedderHeapTracer::kUnknown; 79 80 friend class EmbedderStackStateScope; 81 }; 82 83 } // namespace internal 84 } // namespace v8 85 86 #endif // V8_HEAP_EMBEDDER_TRACING_H_ 87