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_INSPECTOR_V8_HEAP_PROFILER_AGENT_IMPL_H_
6 #define V8_INSPECTOR_V8_HEAP_PROFILER_AGENT_IMPL_H_
7 
8 #include <memory>
9 
10 #include "src/base/macros.h"
11 #include "src/inspector/protocol/Forward.h"
12 #include "src/inspector/protocol/HeapProfiler.h"
13 
14 namespace v8 {
15 class Isolate;
16 }
17 
18 namespace v8_inspector {
19 
20 class V8InspectorSessionImpl;
21 
22 using protocol::Maybe;
23 using protocol::Response;
24 
25 class V8HeapProfilerAgentImpl : public protocol::HeapProfiler::Backend {
26  public:
27   V8HeapProfilerAgentImpl(V8InspectorSessionImpl*, protocol::FrontendChannel*,
28                           protocol::DictionaryValue* state);
29   ~V8HeapProfilerAgentImpl() override;
30   V8HeapProfilerAgentImpl(const V8HeapProfilerAgentImpl&) = delete;
31   V8HeapProfilerAgentImpl& operator=(const V8HeapProfilerAgentImpl&) = delete;
32   void restore();
33 
34   void collectGarbage(
35       std::unique_ptr<CollectGarbageCallback> callback) override;
36 
37   Response enable() override;
38   Response startTrackingHeapObjects(Maybe<bool> trackAllocations) override;
39   Response stopTrackingHeapObjects(Maybe<bool> reportProgress,
40                                    Maybe<bool> treatGlobalObjectsAsRoots,
41                                    Maybe<bool> captureNumericValue) override;
42 
43   Response disable() override;
44 
45   Response takeHeapSnapshot(Maybe<bool> reportProgress,
46                             Maybe<bool> treatGlobalObjectsAsRoots,
47                             Maybe<bool> captureNumericValue) override;
48 
49   Response getObjectByHeapObjectId(
50       const String16& heapSnapshotObjectId, Maybe<String16> objectGroup,
51       std::unique_ptr<protocol::Runtime::RemoteObject>* result) override;
52   Response addInspectedHeapObject(
53       const String16& inspectedHeapObjectId) override;
54   Response getHeapObjectId(const String16& objectId,
55                            String16* heapSnapshotObjectId) override;
56 
57   Response startSampling(Maybe<double> samplingInterval) override;
58   Response stopSampling(
59       std::unique_ptr<protocol::HeapProfiler::SamplingHeapProfile>*) override;
60   Response getSamplingProfile(
61       std::unique_ptr<protocol::HeapProfiler::SamplingHeapProfile>*) override;
62 
63  private:
64   struct AsyncGC;
65   class GCTask;
66 
67   void startTrackingHeapObjectsInternal(bool trackAllocations);
68   void stopTrackingHeapObjectsInternal();
69   void requestHeapStatsUpdate();
70   static void onTimer(void*);
71 
72   V8InspectorSessionImpl* m_session;
73   v8::Isolate* m_isolate;
74   protocol::HeapProfiler::Frontend m_frontend;
75   protocol::DictionaryValue* m_state;
76   bool m_hasTimer;
77   std::shared_ptr<AsyncGC> m_async_gc;
78 };
79 
80 }  // namespace v8_inspector
81 
82 #endif  // V8_INSPECTOR_V8_HEAP_PROFILER_AGENT_IMPL_H_
83