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 v8::AllocationProfile::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<v8::AllocationProfile::Node>& nodes() { return nodes_; } |
33 | 34 |
34 private: | 35 private: |
35 std::deque<Node> nodes_; | 36 std::deque<v8::AllocationProfile::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 class SamplingHeapProfiler { |
41 public: | 42 public: |
42 SamplingHeapProfiler(Heap* heap, StringsStorage* names, uint64_t rate, | 43 SamplingHeapProfiler(Heap* heap, StringsStorage* names, uint64_t rate, |
43 int stack_depth); | 44 int stack_depth); |
44 ~SamplingHeapProfiler(); | 45 ~SamplingHeapProfiler(); |
45 | 46 |
46 v8::AllocationProfile* GetAllocationProfile(); | 47 v8::AllocationProfile* GetAllocationProfile(); |
47 | 48 |
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_; } | 49 StringsStorage* names() const { return names_; } |
54 | 50 |
55 class FunctionInfo { | 51 class FunctionInfo { |
56 public: | 52 public: |
57 FunctionInfo(SharedFunctionInfo* shared, StringsStorage* names); | 53 FunctionInfo(SharedFunctionInfo* shared, StringsStorage* names); |
58 explicit FunctionInfo(const char* name) | 54 explicit FunctionInfo(const char* name) |
59 : name_(name), | 55 : name_(name), |
60 script_name_(""), | 56 script_name_(""), |
61 script_id_(v8::UnboundScript::kNoScriptId), | 57 script_id_(v8::UnboundScript::kNoScriptId), |
62 start_position_(0) {} | 58 start_position_(0) {} |
(...skipping 29 matching lines...) Expand all Loading... |
92 | 88 |
93 SamplingHeapProfiler* const sampling_heap_profiler_; | 89 SamplingHeapProfiler* const sampling_heap_profiler_; |
94 Global<Value> global_; | 90 Global<Value> global_; |
95 std::vector<FunctionInfo*> stack_; | 91 std::vector<FunctionInfo*> stack_; |
96 const size_t size_; | 92 const size_t size_; |
97 | 93 |
98 DISALLOW_COPY_AND_ASSIGN(SampledAllocation); | 94 DISALLOW_COPY_AND_ASSIGN(SampledAllocation); |
99 }; | 95 }; |
100 | 96 |
101 private: | 97 private: |
102 using Node = v8::AllocationProfile::Node; | |
103 | |
104 Heap* heap() const { return heap_; } | 98 Heap* heap() const { return heap_; } |
105 | 99 |
106 void SampleObject(Address soon_object, size_t size); | 100 void SampleObject(Address soon_object, size_t size); |
107 | 101 |
108 static intptr_t GetNextSampleInterval(base::RandomNumberGenerator* random, | |
109 uint64_t rate); | |
110 | |
111 // Methods that construct v8::AllocationProfile. | 102 // Methods that construct v8::AllocationProfile. |
112 Node* AddStack(AllocationProfile* profile, | 103 v8::AllocationProfile::Node* AddStack( |
113 const std::map<int, Script*>& scripts, | 104 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
114 const std::vector<FunctionInfo*>& stack); | 105 const std::vector<FunctionInfo*>& stack); |
115 Node* FindOrAddChildNode(AllocationProfile* profile, | 106 v8::AllocationProfile::Node* FindOrAddChildNode( |
116 const std::map<int, Script*>& scripts, Node* parent, | 107 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
117 FunctionInfo* function_info); | 108 v8::AllocationProfile::Node* parent, FunctionInfo* function_info); |
118 Node* AllocateNode(AllocationProfile* profile, | 109 v8::AllocationProfile::Node* AllocateNode( |
119 const std::map<int, Script*>& scripts, | 110 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
120 FunctionInfo* function_info); | 111 FunctionInfo* function_info); |
121 | 112 |
122 Isolate* const isolate_; | 113 Isolate* const isolate_; |
123 Heap* const heap_; | 114 Heap* const heap_; |
124 base::RandomNumberGenerator* const random_; | 115 base::SmartPointer<SamplingAllocationObserver> new_space_observer_; |
| 116 base::SmartPointer<SamplingAllocationObserver> other_spaces_observer_; |
125 StringsStorage* const names_; | 117 StringsStorage* const names_; |
126 std::set<SampledAllocation*> samples_; | 118 std::set<SampledAllocation*> samples_; |
127 const uint64_t rate_; | |
128 const int stack_depth_; | 119 const int stack_depth_; |
| 120 |
| 121 friend class SamplingAllocationObserver; |
129 }; | 122 }; |
130 | 123 |
| 124 class SamplingAllocationObserver : public AllocationObserver { |
| 125 public: |
| 126 SamplingAllocationObserver(Heap* heap, intptr_t step_size, uint64_t rate, |
| 127 SamplingHeapProfiler* profiler, |
| 128 base::RandomNumberGenerator* random) |
| 129 : AllocationObserver(step_size), |
| 130 profiler_(profiler), |
| 131 heap_(heap), |
| 132 random_(random), |
| 133 rate_(rate) {} |
| 134 virtual ~SamplingAllocationObserver() {} |
| 135 |
| 136 protected: |
| 137 void Step(int bytes_allocated, Address soon_object, size_t size) override { |
| 138 USE(heap_); |
| 139 DCHECK(heap_->gc_state() == Heap::NOT_IN_GC); |
| 140 DCHECK(soon_object); |
| 141 profiler_->SampleObject(soon_object, size); |
| 142 } |
| 143 |
| 144 intptr_t GetNextStepSize() override { return GetNextSampleInterval(rate_); } |
| 145 |
| 146 private: |
| 147 intptr_t GetNextSampleInterval(uint64_t rate); |
| 148 SamplingHeapProfiler* const profiler_; |
| 149 Heap* const heap_; |
| 150 base::RandomNumberGenerator* const random_; |
| 151 uint64_t const rate_; |
| 152 }; |
131 | 153 |
132 } // namespace internal | 154 } // namespace internal |
133 } // namespace v8 | 155 } // namespace v8 |
134 | 156 |
135 #endif // V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ | 157 #endif // V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ |
OLD | NEW |