1 // Copyright 2018 The Chromium 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_UNIFIED_HEAP_CONTROLLER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_UNIFIED_HEAP_CONTROLLER_H_
7 
8 #include "base/macros.h"
9 #include "third_party/blink/renderer/platform/heap/heap_stats_collector.h"
10 #include "third_party/blink/renderer/platform/platform_export.h"
11 #include "v8/include/v8.h"
12 
13 namespace blink {
14 
15 class ThreadState;
16 
17 // UnifiedHeapController ties V8's garbage collector to Oilpan for performing a
18 // garbage collection across both managed heaps.
19 //
20 // Unified heap garbage collections are triggered by V8 and mark the full
21 // transitive closure of V8 and Blink (Oilpan) objects. The garbage collection
22 // is initially triggered by V8. Both collecters report live references using
23 // the EmbedderHeapTracer APIs. V8 and Blink both run separate incremental
24 // marking steps to compute their live closures, respectively. The final atomic
25 // pause is then initiated by V8 and triggers a fixed-point computation between
26 // V8 and Blink where both GCs report live references to each other and drain
27 // their marking work lists until they are empty and no new references are
28 // found.
29 //
30 // Oilpan does not consider references from DOM wrappers (JavaScript objects on
31 // V8's heap) as roots for such garbage collections.
32 class PLATFORM_EXPORT UnifiedHeapController final
33     : public v8::EmbedderHeapTracer,
34       public ThreadHeapStatsObserver {
35   DISALLOW_IMPLICIT_CONSTRUCTORS(UnifiedHeapController);
36 
37  public:
38   explicit UnifiedHeapController(ThreadState*);
39   ~UnifiedHeapController() override;
40 
41   // v8::EmbedderHeapTracer implementation.
42   void TracePrologue(v8::EmbedderHeapTracer::TraceFlags) final;
43   void TraceEpilogue(v8::EmbedderHeapTracer::TraceSummary*) final;
44   void EnterFinalPause(EmbedderStackState) final;
45   void RegisterV8References(const std::vector<std::pair<void*, void*>>&) final;
46   bool AdvanceTracing(double) final;
47   bool IsTracingDone() final;
48   bool IsRootForNonTracingGC(const v8::TracedReference<v8::Value>&) final;
49   bool IsRootForNonTracingGC(const v8::TracedGlobal<v8::Value>&) final;
50   void ResetHandleInNonTracingGC(const v8::TracedReference<v8::Value>&) final;
51 
thread_state()52   ThreadState* thread_state() const { return thread_state_; }
53 
54   // ThreadHeapStatsObserver implementation.
55   void IncreaseAllocatedObjectSize(size_t) final;
56   void DecreaseAllocatedObjectSize(size_t) final;
57   // Not needed.
ResetAllocatedObjectSize(size_t)58   void ResetAllocatedObjectSize(size_t) final {}
IncreaseAllocatedSpace(size_t)59   void IncreaseAllocatedSpace(size_t) final {}
DecreaseAllocatedSpace(size_t)60   void DecreaseAllocatedSpace(size_t) final {}
61 
62  private:
63   void ReportBufferedAllocatedSizeIfPossible();
64 
65   ThreadState* const thread_state_;
66   // Returns whether the Blink heap has been fully processed.
67   bool is_tracing_done_ = false;
68 
69   // Buffered allocated size. Only positive values are forwarded to V8.
70   int64_t buffered_allocated_size_ = 0;
71 };
72 
73 }  // namespace blink
74 
75 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_IMPL_UNIFIED_HEAP_CONTROLLER_H_
76