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