5 #ifndef V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ 6 #define V8_PROFILER_SAMPLING_HEAP_PROFILER_H_ 11 #include <unordered_map> 12 #include "include/v8-profiler.h" 13 #include "src/heap/heap.h" 14 #include "src/profiler/strings-storage.h" 19 class RandomNumberGenerator;
24 class SamplingAllocationObserver;
31 return nodes_.size() == 0 ? nullptr : &nodes_.front();
34 const std::vector<v8::AllocationProfile::Sample>& GetSamples()
override {
39 std::deque<v8::AllocationProfile::Node> nodes_;
40 std::vector<v8::AllocationProfile::Sample> samples_;
42 friend class SamplingHeapProfiler;
51 typedef uint64_t FunctionId;
55 script_id_(script_id),
56 script_position_(start_position),
61 auto it = children_.find(
id);
62 return it != children_.end() ? it->second.get() :
nullptr;
66 std::unique_ptr<AllocationNode> node) {
67 return children_.emplace(
id, std::move(node)).first->second.get();
70 static FunctionId function_id(
int script_id,
int start_position,
76 if (script_id == v8::UnboundScript::kNoScriptId) {
77 return reinterpret_cast<intptr_t
>(name) | 1;
82 DCHECK(static_cast<unsigned>(start_position) < (1u << 31));
83 return (static_cast<uint64_t>(script_id) << 32) + (start_position << 1);
89 std::map<size_t, unsigned int> allocations_;
90 std::map<FunctionId, std::unique_ptr<AllocationNode>> children_;
93 const int script_position_;
94 const char*
const name_;
109 reinterpret_cast<v8::Isolate*>(profiler_->isolate_), local_)),
111 sample_id(sample_id) {}
112 ~
Sample() { global.Reset(); }
117 const uint64_t sample_id;
120 DISALLOW_COPY_AND_ASSIGN(
Sample);
124 int stack_depth, v8::HeapProfiler::SamplingFlags flags);
131 void SampleObject(Address soon_object,
size_t size);
133 const std::vector<v8::AllocationProfile::Sample> BuildSamples()
const;
135 AllocationNode* FindOrAddChildNode(AllocationNode* parent,
const char* name,
136 int script_id,
int start_position);
139 uint32_t next_node_id() {
return ++last_node_id_; }
140 uint64_t next_sample_id() {
return ++last_sample_id_; }
150 AllocationProfile* profile, SamplingHeapProfiler::AllocationNode* node,
151 const std::map<
int, Handle<Script>>& scripts);
153 unsigned int count)
const;
154 AllocationNode* AddStack();
156 Isolate*
const isolate_;
158 uint64_t last_sample_id_ = 0;
160 std::unique_ptr<SamplingAllocationObserver> new_space_observer_;
161 std::unique_ptr<SamplingAllocationObserver> other_spaces_observer_;
162 StringsStorage*
const names_;
163 AllocationNode profile_root_;
164 std::unordered_map<Sample*, std::unique_ptr<Sample>> samples_;
165 const int stack_depth_;
166 const uint64_t rate_;
167 v8::HeapProfiler::SamplingFlags flags_;
169 friend class SamplingAllocationObserver;
171 DISALLOW_COPY_AND_ASSIGN(SamplingHeapProfiler);
187 void Step(
int bytes_allocated,
Address soon_object,
size_t size)
override {
189 DCHECK(heap_->gc_state() == Heap::NOT_IN_GC);
193 profiler_->SampleObject(soon_object, size);
197 intptr_t GetNextStepSize()
override {
return GetNextSampleInterval(rate_); }
200 intptr_t GetNextSampleInterval(uint64_t rate);
204 uint64_t
const rate_;
210 #endif // V8_PROFILER_SAMPLING_HEAP_PROFILER_H_
v8::AllocationProfile::Node * GetRootNode() override