embedder-tracing.h 3.37 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

#ifndef V8_HEAP_EMBEDDER_TRACING_H_
#define V8_HEAP_EMBEDDER_TRACING_H_

#include "include/v8.h"
9
#include "src/flags.h"
10 11 12 13 14 15
#include "src/globals.h"

namespace v8 {
namespace internal {

class Heap;
16
class JSObject;
17 18 19 20

class V8_EXPORT_PRIVATE LocalEmbedderHeapTracer final {
 public:
  typedef std::pair<void*, void*> WrapperInfo;
21
  typedef std::vector<WrapperInfo> WrapperCache;
22

23 24 25 26
  class V8_EXPORT_PRIVATE ProcessingScope {
   public:
    explicit ProcessingScope(LocalEmbedderHeapTracer* tracer);
    ~ProcessingScope();
27

28
    void TracePossibleWrapper(JSObject js_object);
29

30
    void AddWrapperInfoForTesting(WrapperInfo info);
31

32 33 34 35 36 37 38 39
   private:
    static constexpr size_t kWrapperCacheSize = 1000;

    void FlushWrapperCacheIfFull();

    LocalEmbedderHeapTracer* const tracer_;
    WrapperCache wrapper_cache_;
  };
40

41 42 43 44
  explicit LocalEmbedderHeapTracer(Isolate* isolate) : isolate_(isolate) {}

  ~LocalEmbedderHeapTracer() {
    if (remote_tracer_) remote_tracer_->isolate_ = nullptr;
45 46 47
  }

  bool InUse() const { return remote_tracer_ != nullptr; }
48
  EmbedderHeapTracer* remote_tracer() const { return remote_tracer_; }
49

50
  void SetRemoteTracer(EmbedderHeapTracer* tracer);
51 52 53
  void TracePrologue();
  void TraceEpilogue();
  void EnterFinalPause();
54
  bool Trace(double deadline);
55
  bool IsRemoteTracingDone();
56

57 58 59 60
  bool IsRootForNonTracingGC(const v8::TracedGlobal<v8::Value>& handle) {
    return !InUse() || remote_tracer_->IsRootForNonTracingGC(handle);
  }

61 62 63
  void NotifyV8MarkingWorklistWasEmpty() {
    num_v8_marking_worklist_was_empty_++;
  }
64

65 66 67
  bool ShouldFinalizeIncrementalMarking() {
    static const size_t kMaxIncrementalFixpointRounds = 3;
    return !FLAG_incremental_marking_wrappers || !InUse() ||
68
           (IsRemoteTracingDone() && embedder_worklist_empty_) ||
69
           num_v8_marking_worklist_was_empty_ > kMaxIncrementalFixpointRounds;
70 71
  }

72 73 74
  void SetEmbedderStackStateForNextFinalization(
      EmbedderHeapTracer::EmbedderStackState stack_state);

75 76 77
  void SetEmbedderWorklistEmpty(bool is_empty) {
    embedder_worklist_empty_ = is_empty;
  }
78

79
 private:
80
  Isolate* const isolate_;
81
  EmbedderHeapTracer* remote_tracer_ = nullptr;
82

83 84 85
  size_t num_v8_marking_worklist_was_empty_ = 0;
  EmbedderHeapTracer::EmbedderStackState embedder_stack_state_ =
      EmbedderHeapTracer::kUnknown;
86 87 88 89
  // Indicates whether the embedder worklist was observed empty on the main
  // thread. This is opportunistic as concurrent marking tasks may hold local
  // segments of potential embedder fields to move to the main thread.
  bool embedder_worklist_empty_ = false;
90

91
  friend class EmbedderStackStateScope;
92 93
};

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
class V8_EXPORT_PRIVATE EmbedderStackStateScope final {
 public:
  EmbedderStackStateScope(LocalEmbedderHeapTracer* local_tracer,
                          EmbedderHeapTracer::EmbedderStackState stack_state)
      : local_tracer_(local_tracer),
        old_stack_state_(local_tracer_->embedder_stack_state_) {
    local_tracer_->embedder_stack_state_ = stack_state;
  }

  ~EmbedderStackStateScope() {
    local_tracer_->embedder_stack_state_ = old_stack_state_;
  }

 private:
  LocalEmbedderHeapTracer* const local_tracer_;
  const EmbedderHeapTracer::EmbedderStackState old_stack_state_;
};

112 113 114 115
}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_EMBEDDER_TRACING_H_