OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ | 5 #ifndef V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ |
6 #define V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ | 6 #define V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include "include/v8-profiler.h" | 11 #include "include/v8-profiler.h" |
12 #include "src/heap/heap.h" | 12 #include "src/heap/heap.h" |
13 #include "src/profiler/strings-storage.h" | 13 #include "src/profiler/strings-storage.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 class RandomNumberGenerator; | 18 class RandomNumberGenerator; |
19 } | 19 } |
20 | 20 |
21 namespace internal { | 21 namespace internal { |
22 | 22 |
23 class SamplingAllocationObserver; | |
23 | 24 |
24 class AllocationProfile : public v8::AllocationProfile { | 25 class AllocationProfile : public v8::AllocationProfile { |
25 public: | 26 public: |
26 AllocationProfile() : nodes_() {} | 27 AllocationProfile() : nodes_() {} |
27 | 28 |
28 Node* GetRootNode() override { | 29 Node* GetRootNode() override { |
29 return nodes_.size() == 0 ? nullptr : &nodes_.front(); | 30 return nodes_.size() == 0 ? nullptr : &nodes_.front(); |
30 } | 31 } |
31 | 32 |
32 std::deque<Node>& nodes() { return nodes_; } | 33 std::deque<Node>& nodes() { return nodes_; } |
33 | 34 |
34 private: | 35 private: |
35 std::deque<Node> nodes_; | 36 std::deque<Node> nodes_; |
36 | 37 |
37 DISALLOW_COPY_AND_ASSIGN(AllocationProfile); | 38 DISALLOW_COPY_AND_ASSIGN(AllocationProfile); |
38 }; | 39 }; |
39 | 40 |
40 class SamplingHeapProfiler : public InlineAllocationObserver { | 41 |
42 class SamplingHeapProfiler { | |
41 public: | 43 public: |
42 SamplingHeapProfiler(Heap* heap, StringsStorage* names, uint64_t rate, | 44 SamplingHeapProfiler(Heap* heap, StringsStorage* names, uint64_t rate, |
43 int stack_depth); | 45 int stack_depth); |
44 ~SamplingHeapProfiler(); | 46 ~SamplingHeapProfiler(); |
45 | 47 |
46 v8::AllocationProfile* GetAllocationProfile(); | 48 v8::AllocationProfile* GetAllocationProfile(); |
47 | 49 |
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_; } | 50 StringsStorage* names() const { return names_; } |
54 | 51 |
55 class FunctionInfo { | 52 class FunctionInfo { |
56 public: | 53 public: |
57 FunctionInfo(SharedFunctionInfo* shared, StringsStorage* names); | 54 FunctionInfo(SharedFunctionInfo* shared, StringsStorage* names); |
58 explicit FunctionInfo(const char* name) | 55 explicit FunctionInfo(const char* name) |
59 : name_(name), | 56 : name_(name), |
60 script_name_(""), | 57 script_name_(""), |
61 script_id_(v8::UnboundScript::kNoScriptId), | 58 script_id_(v8::UnboundScript::kNoScriptId), |
62 start_position_(0) {} | 59 start_position_(0) {} |
(...skipping 29 matching lines...) Expand all Loading... | |
92 | 89 |
93 SamplingHeapProfiler* const sampling_heap_profiler_; | 90 SamplingHeapProfiler* const sampling_heap_profiler_; |
94 Global<Value> global_; | 91 Global<Value> global_; |
95 std::vector<FunctionInfo*> stack_; | 92 std::vector<FunctionInfo*> stack_; |
96 const size_t size_; | 93 const size_t size_; |
97 | 94 |
98 DISALLOW_COPY_AND_ASSIGN(SampledAllocation); | 95 DISALLOW_COPY_AND_ASSIGN(SampledAllocation); |
99 }; | 96 }; |
100 | 97 |
101 private: | 98 private: |
99 friend class SamplingAllocationObserver; | |
102 using Node = v8::AllocationProfile::Node; | 100 using Node = v8::AllocationProfile::Node; |
103 | 101 |
104 Heap* heap() const { return heap_; } | 102 Heap* heap() const { return heap_; } |
105 | 103 |
106 void SampleObject(Address soon_object, size_t size); | 104 void SampleObject(Address soon_object, size_t size); |
107 | 105 |
108 static intptr_t GetNextSampleInterval(base::RandomNumberGenerator* random, | |
109 uint64_t rate); | |
110 | |
111 // Methods that construct v8::AllocationProfile. | 106 // Methods that construct v8::AllocationProfile. |
112 Node* AddStack(AllocationProfile* profile, | 107 Node* AddStack(AllocationProfile* profile, |
113 const std::map<int, Script*>& scripts, | 108 const std::map<int, Script*>& scripts, |
114 const std::vector<FunctionInfo*>& stack); | 109 const std::vector<FunctionInfo*>& stack); |
115 Node* FindOrAddChildNode(AllocationProfile* profile, | 110 Node* FindOrAddChildNode(AllocationProfile* profile, |
116 const std::map<int, Script*>& scripts, Node* parent, | 111 const std::map<int, Script*>& scripts, Node* parent, |
117 FunctionInfo* function_info); | 112 FunctionInfo* function_info); |
118 Node* AllocateNode(AllocationProfile* profile, | 113 Node* AllocateNode(AllocationProfile* profile, |
119 const std::map<int, Script*>& scripts, | 114 const std::map<int, Script*>& scripts, |
120 FunctionInfo* function_info); | 115 FunctionInfo* function_info); |
121 | 116 |
122 Isolate* const isolate_; | 117 Isolate* const isolate_; |
123 Heap* const heap_; | 118 Heap* const heap_; |
124 base::RandomNumberGenerator* const random_; | 119 SamplingAllocationObserver* new_space_observer_; |
120 SamplingAllocationObserver* other_spaces_observer_; | |
125 StringsStorage* const names_; | 121 StringsStorage* const names_; |
126 std::set<SampledAllocation*> samples_; | 122 std::set<SampledAllocation*> samples_; |
127 const uint64_t rate_; | |
128 const int stack_depth_; | 123 const int stack_depth_; |
129 }; | 124 }; |
130 | 125 |
131 | 126 |
127 class SamplingAllocationObserver : public AllocationObserver { | |
128 public: | |
129 explicit SamplingAllocationObserver(Heap* heap, intptr_t step_size, | |
130 uint64_t rate, | |
131 SamplingHeapProfiler* profiler, | |
132 base::RandomNumberGenerator* random) | |
133 : AllocationObserver(heap, step_size), | |
134 profiler_(profiler), | |
135 rate_(rate), | |
136 random_(random) {} | |
137 virtual ~SamplingAllocationObserver() {} | |
138 | |
139 protected: | |
140 void Step(int bytes_allocated, Address soon_object, size_t size) override { | |
141 DCHECK(heap_->gc_state() == Heap::NOT_IN_GC); | |
142 DCHECK(soon_object); | |
143 profiler_->SampleObject(soon_object, size); | |
144 } | |
145 | |
146 intptr_t GetNextStepSize() override { | |
147 return GetNextSampleInterval(random_, rate_); | |
148 } | |
149 | |
150 private: | |
151 SamplingHeapProfiler* profiler_; | |
152 uint64_t rate_; | |
ofrobots
2016/01/23 16:16:31
Weak suggestion: prefer to group member together b
mattloring
2016/01/26 00:42:48
Done.
| |
153 base::RandomNumberGenerator* random_; | |
ofrobots
2016/01/23 16:16:31
Mark the above as const. E.g. SamplingHeapProfiler
mattloring
2016/01/26 00:42:48
Done.
| |
154 | |
155 intptr_t GetNextSampleInterval(base::RandomNumberGenerator* random, | |
156 uint64_t rate); | |
ofrobots
2016/01/23 16:16:31
Any reason you make this non-static here?
mattloring
2016/01/26 00:42:48
I was having problems with `incomplete type base::
ofrobots
2016/01/26 15:47:15
Move (keep) the function in the .cc file. This can
| |
157 }; | |
158 | |
159 | |
132 } // namespace internal | 160 } // namespace internal |
133 } // namespace v8 | 161 } // namespace v8 |
134 | 162 |
135 #endif // V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ | 163 #endif // V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ |
OLD | NEW |