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
12namespace v8 {
13namespace internal {
14
15class Heap;
16
17class V8_EXPORT_PRIVATE LocalEmbedderHeapTracer final {
18 public:
19  typedef std::pair<void*, void*> WrapperInfo;
20
21  LocalEmbedderHeapTracer()
22      : remote_tracer_(nullptr), num_v8_marking_deque_was_empty_(0) {}
23
24  void SetRemoteTracer(EmbedderHeapTracer* tracer) { remote_tracer_ = tracer; }
25  bool InUse() { return remote_tracer_ != nullptr; }
26
27  void TracePrologue();
28  void TraceEpilogue();
29  void AbortTracing();
30  void EnterFinalPause();
31  bool Trace(double deadline,
32             EmbedderHeapTracer::AdvanceTracingActions actions);
33
34  size_t NumberOfWrappersToTrace();
35  size_t NumberOfCachedWrappersToTrace() {
36    return cached_wrappers_to_trace_.size();
37  }
38  void AddWrapperToTrace(WrapperInfo entry) {
39    cached_wrappers_to_trace_.push_back(entry);
40  }
41  void ClearCachedWrappersToTrace() { cached_wrappers_to_trace_.clear(); }
42  void RegisterWrappersWithRemoteTracer();
43
44  // In order to avoid running out of memory we force tracing wrappers if there
45  // are too many of them.
46  bool RequiresImmediateWrapperProcessing();
47
48  void NotifyV8MarkingDequeWasEmpty() { num_v8_marking_deque_was_empty_++; }
49  bool ShouldFinalizeIncrementalMarking() {
50    static const size_t kMaxIncrementalFixpointRounds = 3;
51    return !FLAG_incremental_marking_wrappers || !InUse() ||
52           NumberOfWrappersToTrace() == 0 ||
53           num_v8_marking_deque_was_empty_ > kMaxIncrementalFixpointRounds;
54  }
55
56 private:
57  typedef std::vector<WrapperInfo> WrapperCache;
58
59  EmbedderHeapTracer* remote_tracer_;
60  WrapperCache cached_wrappers_to_trace_;
61  size_t num_v8_marking_deque_was_empty_;
62};
63
64}  // namespace internal
65}  // namespace v8
66
67#endif  // V8_HEAP_EMBEDDER_TRACING_H_
68