OLD | NEW |
| (Empty) |
1 // Copyright 2015 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_PROFILER_SAMPLING_HEAP_PROFILER_H_ | |
6 #define V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ | |
7 | |
8 #include <deque> | |
9 #include <map> | |
10 #include <set> | |
11 #include "include/v8-profiler.h" | |
12 #include "src/heap/heap.h" | |
13 #include "src/profiler/strings-storage.h" | |
14 | |
15 namespace v8 { | |
16 | |
17 namespace base { | |
18 class RandomNumberGenerator; | |
19 } | |
20 | |
21 namespace internal { | |
22 | |
23 | |
24 class AllocationProfile : public v8::AllocationProfile { | |
25 public: | |
26 AllocationProfile() : nodes_() {} | |
27 | |
28 Node* GetRootNode() override { | |
29 return nodes_.size() == 0 ? nullptr : &nodes_.front(); | |
30 } | |
31 | |
32 std::deque<Node>& nodes() { return nodes_; } | |
33 | |
34 private: | |
35 std::deque<Node> nodes_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(AllocationProfile); | |
38 }; | |
39 | |
40 class SamplingHeapProfiler : public InlineAllocationObserver { | |
41 public: | |
42 SamplingHeapProfiler(Heap* heap, StringsStorage* names, uint64_t rate, | |
43 int stack_depth); | |
44 ~SamplingHeapProfiler(); | |
45 | |
46 v8::AllocationProfile* GetAllocationProfile(); | |
47 | |
48 void Step(int bytes_allocated, Address soon_object, size_t size) override; | |
49 intptr_t GetNextStepSize() override { | |
50 return GetNextSampleInterval(random_, rate_); | |
51 } | |
52 | |
53 StringsStorage* names() const { return names_; } | |
54 | |
55 class FunctionInfo { | |
56 public: | |
57 FunctionInfo(SharedFunctionInfo* shared, StringsStorage* names); | |
58 explicit FunctionInfo(const char* name) | |
59 : name_(name), | |
60 script_name_(""), | |
61 script_id_(v8::UnboundScript::kNoScriptId), | |
62 start_position_(0) {} | |
63 | |
64 const char* get_name() const { return name_; } | |
65 const char* get_script_name() const { return script_name_; } | |
66 int get_script_id() const { return script_id_; } | |
67 int get_start_position() const { return start_position_; } | |
68 | |
69 private: | |
70 const char* const name_; | |
71 const char* script_name_; | |
72 int script_id_; | |
73 const int start_position_; | |
74 }; | |
75 | |
76 class SampledAllocation { | |
77 public: | |
78 SampledAllocation(SamplingHeapProfiler* sampling_heap_profiler, | |
79 Isolate* isolate, Local<Value> local, size_t size, | |
80 int max_frames); | |
81 ~SampledAllocation() { | |
82 for (auto info : stack_) { | |
83 delete info; | |
84 } | |
85 global_.Reset(); // drop the reference. | |
86 } | |
87 size_t get_size() const { return size_; } | |
88 const std::vector<FunctionInfo*>& get_stack() const { return stack_; } | |
89 | |
90 private: | |
91 static void OnWeakCallback(const WeakCallbackInfo<SampledAllocation>& data); | |
92 | |
93 SamplingHeapProfiler* const sampling_heap_profiler_; | |
94 Global<Value> global_; | |
95 std::vector<FunctionInfo*> stack_; | |
96 const size_t size_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(SampledAllocation); | |
99 }; | |
100 | |
101 private: | |
102 using Node = v8::AllocationProfile::Node; | |
103 | |
104 Heap* heap() const { return heap_; } | |
105 | |
106 void SampleObject(Address soon_object, size_t size); | |
107 | |
108 static intptr_t GetNextSampleInterval(base::RandomNumberGenerator* random, | |
109 uint64_t rate); | |
110 | |
111 // Methods that construct v8::AllocationProfile. | |
112 Node* AddStack(AllocationProfile* profile, | |
113 const std::map<int, Script*>& scripts, | |
114 const std::vector<FunctionInfo*>& stack); | |
115 Node* FindOrAddChildNode(AllocationProfile* profile, | |
116 const std::map<int, Script*>& scripts, Node* parent, | |
117 FunctionInfo* function_info); | |
118 Node* AllocateNode(AllocationProfile* profile, | |
119 const std::map<int, Script*>& scripts, | |
120 FunctionInfo* function_info); | |
121 | |
122 Isolate* const isolate_; | |
123 Heap* const heap_; | |
124 base::RandomNumberGenerator* const random_; | |
125 StringsStorage* const names_; | |
126 std::set<SampledAllocation*> samples_; | |
127 const uint64_t rate_; | |
128 const int stack_depth_; | |
129 }; | |
130 | |
131 | |
132 } // namespace internal | |
133 } // namespace v8 | |
134 | |
135 #endif // V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ | |
OLD | NEW |