| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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_CPU_PROFILER_H_ | 5 #ifndef V8_PROFILER_PROFILER_LISTENER_H_ |
| 6 #define V8_PROFILER_CPU_PROFILER_H_ | 6 #define V8_PROFILER_PROFILER_LISTENER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "src/allocation.h" | 10 #include "src/code-events.h" |
| 11 #include "src/base/atomic-utils.h" | 11 #include "src/profiler/profile-generator.h" |
| 12 #include "src/base/atomicops.h" | |
| 13 #include "src/base/platform/time.h" | |
| 14 #include "src/compiler.h" | |
| 15 #include "src/isolate.h" | |
| 16 #include "src/libsampler/v8-sampler.h" | |
| 17 #include "src/locked-queue.h" | |
| 18 #include "src/profiler/circular-queue.h" | |
| 19 #include "src/profiler/tick-sample.h" | |
| 20 | 12 |
| 21 namespace v8 { | 13 namespace v8 { |
| 22 namespace internal { | 14 namespace internal { |
| 23 | 15 |
| 24 // Forward declarations. | 16 class CodeEventsContainer; |
| 25 class CodeEntry; | |
| 26 class CodeMap; | |
| 27 class CpuProfile; | |
| 28 class CpuProfilesCollection; | |
| 29 class ProfileGenerator; | |
| 30 | 17 |
| 31 #define CODE_EVENTS_TYPE_LIST(V) \ | 18 class CodeEventObserver { |
| 32 V(CODE_CREATION, CodeCreateEventRecord) \ | |
| 33 V(CODE_MOVE, CodeMoveEventRecord) \ | |
| 34 V(CODE_DISABLE_OPT, CodeDisableOptEventRecord) \ | |
| 35 V(CODE_DEOPT, CodeDeoptEventRecord) \ | |
| 36 V(REPORT_BUILTIN, ReportBuiltinEventRecord) | |
| 37 | |
| 38 | |
| 39 class CodeEventRecord { | |
| 40 public: | 19 public: |
| 41 #define DECLARE_TYPE(type, ignore) type, | 20 virtual void CodeEventHandler(const CodeEventsContainer& evt_rec) = 0; |
| 42 enum Type { | 21 virtual ~CodeEventObserver() {} |
| 43 NONE = 0, | |
| 44 CODE_EVENTS_TYPE_LIST(DECLARE_TYPE) | |
| 45 NUMBER_OF_TYPES | |
| 46 }; | |
| 47 #undef DECLARE_TYPE | |
| 48 | |
| 49 Type type; | |
| 50 mutable unsigned order; | |
| 51 }; | 22 }; |
| 52 | 23 |
| 24 class ProfilerListener : public CodeEventListener { |
| 25 public: |
| 26 explicit ProfilerListener(Isolate* isolate); |
| 27 ~ProfilerListener() override; |
| 53 | 28 |
| 54 class CodeCreateEventRecord : public CodeEventRecord { | |
| 55 public: | |
| 56 Address start; | |
| 57 CodeEntry* entry; | |
| 58 unsigned size; | |
| 59 | |
| 60 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
| 61 }; | |
| 62 | |
| 63 | |
| 64 class CodeMoveEventRecord : public CodeEventRecord { | |
| 65 public: | |
| 66 Address from; | |
| 67 Address to; | |
| 68 | |
| 69 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
| 70 }; | |
| 71 | |
| 72 | |
| 73 class CodeDisableOptEventRecord : public CodeEventRecord { | |
| 74 public: | |
| 75 Address start; | |
| 76 const char* bailout_reason; | |
| 77 | |
| 78 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
| 79 }; | |
| 80 | |
| 81 | |
| 82 class CodeDeoptEventRecord : public CodeEventRecord { | |
| 83 public: | |
| 84 Address start; | |
| 85 const char* deopt_reason; | |
| 86 SourcePosition position; | |
| 87 int deopt_id; | |
| 88 | |
| 89 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
| 90 }; | |
| 91 | |
| 92 | |
| 93 class ReportBuiltinEventRecord : public CodeEventRecord { | |
| 94 public: | |
| 95 Address start; | |
| 96 Builtins::Name builtin_id; | |
| 97 | |
| 98 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
| 99 }; | |
| 100 | |
| 101 | |
| 102 class TickSampleEventRecord { | |
| 103 public: | |
| 104 // The parameterless constructor is used when we dequeue data from | |
| 105 // the ticks buffer. | |
| 106 TickSampleEventRecord() { } | |
| 107 explicit TickSampleEventRecord(unsigned order) : order(order) { } | |
| 108 | |
| 109 unsigned order; | |
| 110 TickSample sample; | |
| 111 }; | |
| 112 | |
| 113 | |
| 114 class CodeEventsContainer { | |
| 115 public: | |
| 116 explicit CodeEventsContainer( | |
| 117 CodeEventRecord::Type type = CodeEventRecord::NONE) { | |
| 118 generic.type = type; | |
| 119 } | |
| 120 union { | |
| 121 CodeEventRecord generic; | |
| 122 #define DECLARE_CLASS(ignore, type) type type##_; | |
| 123 CODE_EVENTS_TYPE_LIST(DECLARE_CLASS) | |
| 124 #undef DECLARE_TYPE | |
| 125 }; | |
| 126 }; | |
| 127 | |
| 128 | |
| 129 // This class implements both the profile events processor thread and | |
| 130 // methods called by event producers: VM and stack sampler threads. | |
| 131 class ProfilerEventsProcessor : public base::Thread { | |
| 132 public: | |
| 133 ProfilerEventsProcessor(ProfileGenerator* generator, | |
| 134 sampler::Sampler* sampler, | |
| 135 base::TimeDelta period); | |
| 136 virtual ~ProfilerEventsProcessor(); | |
| 137 | |
| 138 // Thread control. | |
| 139 virtual void Run(); | |
| 140 void StopSynchronously(); | |
| 141 INLINE(bool running()) { return !!base::NoBarrier_Load(&running_); } | |
| 142 void Enqueue(const CodeEventsContainer& event); | |
| 143 | |
| 144 // Puts current stack into tick sample events buffer. | |
| 145 void AddCurrentStack(Isolate* isolate, bool update_stats = false); | |
| 146 void AddDeoptStack(Isolate* isolate, Address from, int fp_to_sp_delta); | |
| 147 | |
| 148 // Tick sample events are filled directly in the buffer of the circular | |
| 149 // queue (because the structure is of fixed width, but usually not all | |
| 150 // stack frame entries are filled.) This method returns a pointer to the | |
| 151 // next record of the buffer. | |
| 152 inline TickSample* StartTickSample(); | |
| 153 inline void FinishTickSample(); | |
| 154 | |
| 155 // SamplingCircularQueue has stricter alignment requirements than a normal new | |
| 156 // can fulfil, so we need to provide our own new/delete here. | |
| 157 void* operator new(size_t size); | |
| 158 void operator delete(void* ptr); | |
| 159 | |
| 160 private: | |
| 161 // Called from events processing thread (Run() method.) | |
| 162 bool ProcessCodeEvent(); | |
| 163 | |
| 164 enum SampleProcessingResult { | |
| 165 OneSampleProcessed, | |
| 166 FoundSampleForNextCodeEvent, | |
| 167 NoSamplesInQueue | |
| 168 }; | |
| 169 SampleProcessingResult ProcessOneSample(); | |
| 170 | |
| 171 ProfileGenerator* generator_; | |
| 172 sampler::Sampler* sampler_; | |
| 173 base::Atomic32 running_; | |
| 174 const base::TimeDelta period_; // Samples & code events processing period. | |
| 175 LockedQueue<CodeEventsContainer> events_buffer_; | |
| 176 static const size_t kTickSampleBufferSize = 1 * MB; | |
| 177 static const size_t kTickSampleQueueLength = | |
| 178 kTickSampleBufferSize / sizeof(TickSampleEventRecord); | |
| 179 SamplingCircularQueue<TickSampleEventRecord, | |
| 180 kTickSampleQueueLength> ticks_buffer_; | |
| 181 LockedQueue<TickSampleEventRecord> ticks_from_vm_buffer_; | |
| 182 base::AtomicNumber<unsigned> last_code_event_id_; | |
| 183 unsigned last_processed_code_event_id_; | |
| 184 }; | |
| 185 | |
| 186 class CpuProfiler : public CodeEventListener { | |
| 187 public: | |
| 188 explicit CpuProfiler(Isolate* isolate); | |
| 189 | |
| 190 CpuProfiler(Isolate* isolate, CpuProfilesCollection* profiles, | |
| 191 ProfileGenerator* test_generator, | |
| 192 ProfilerEventsProcessor* test_processor); | |
| 193 | |
| 194 ~CpuProfiler() override; | |
| 195 | |
| 196 void set_sampling_interval(base::TimeDelta value); | |
| 197 void CollectSample(); | |
| 198 void StartProfiling(const char* title, bool record_samples = false); | |
| 199 void StartProfiling(String* title, bool record_samples); | |
| 200 CpuProfile* StopProfiling(const char* title); | |
| 201 CpuProfile* StopProfiling(String* title); | |
| 202 int GetProfilesCount(); | |
| 203 CpuProfile* GetProfile(int index); | |
| 204 void DeleteAllProfiles(); | |
| 205 void DeleteProfile(CpuProfile* profile); | |
| 206 | |
| 207 // Invoked from stack sampler (thread or signal handler.) | |
| 208 inline TickSample* StartTickSample(); | |
| 209 inline void FinishTickSample(); | |
| 210 | |
| 211 // Must be called via PROFILE macro, otherwise will crash when | |
| 212 // profiling is not enabled. | |
| 213 void CallbackEvent(Name* name, Address entry_point) override; | 29 void CallbackEvent(Name* name, Address entry_point) override; |
| 214 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 30 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
| 215 const char* comment) override; | 31 AbstractCode* code, const char* comment) override; |
| 216 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 32 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
| 217 Name* name) override; | 33 AbstractCode* code, Name* name) override; |
| 218 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 34 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
| 219 SharedFunctionInfo* shared, Name* script_name) override; | 35 AbstractCode* code, SharedFunctionInfo* shared, |
| 220 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 36 Name* script_name) override; |
| 221 SharedFunctionInfo* shared, Name* script_name, int line, | 37 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
| 222 int column) override; | 38 AbstractCode* code, SharedFunctionInfo* shared, |
| 223 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 39 Name* script_name, int line, int column) override; |
| 224 int args_count) override; | 40 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
| 41 AbstractCode* code, int args_count) override; |
| 225 void CodeMovingGCEvent() override {} | 42 void CodeMovingGCEvent() override {} |
| 226 void CodeMoveEvent(AbstractCode* from, Address to) override; | 43 void CodeMoveEvent(AbstractCode* from, Address to) override; |
| 227 void CodeDisableOptEvent(AbstractCode* code, | 44 void CodeDisableOptEvent(AbstractCode* code, |
| 228 SharedFunctionInfo* shared) override; | 45 SharedFunctionInfo* shared) override; |
| 229 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) override; | 46 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) override; |
| 230 void GetterCallbackEvent(Name* name, Address entry_point) override; | 47 void GetterCallbackEvent(Name* name, Address entry_point) override; |
| 231 void RegExpCodeCreateEvent(AbstractCode* code, String* source) override; | 48 void RegExpCodeCreateEvent(AbstractCode* code, String* source) override; |
| 232 void SetterCallbackEvent(Name* name, Address entry_point) override; | 49 void SetterCallbackEvent(Name* name, Address entry_point) override; |
| 233 void SharedFunctionInfoMoveEvent(Address from, Address to) override {} | 50 void SharedFunctionInfoMoveEvent(Address from, Address to) override {} |
| 234 | 51 |
| 235 bool is_profiling() const { return is_profiling_; } | 52 CodeEntry* NewCodeEntry( |
| 53 CodeEventListener::LogEventsAndTags tag, const char* name, |
| 54 const char* name_prefix = CodeEntry::kEmptyNamePrefix, |
| 55 const char* resource_name = CodeEntry::kEmptyResourceName, |
| 56 int line_number = v8::CpuProfileNode::kNoLineNumberInfo, |
| 57 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo, |
| 58 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL); |
| 236 | 59 |
| 237 ProfileGenerator* generator() const { return generator_.get(); } | 60 void AddObserver(CodeEventObserver* observer); |
| 238 ProfilerEventsProcessor* processor() const { return processor_.get(); } | 61 void RemoveObserver(CodeEventObserver* observer); |
| 239 Isolate* isolate() const { return isolate_; } | 62 V8_INLINE bool HasObservers() { return !observers_.empty(); } |
| 63 |
| 64 const char* GetName(Name* name) { |
| 65 return function_and_resource_names_.GetName(name); |
| 66 } |
| 67 const char* GetName(int args_count) { |
| 68 return function_and_resource_names_.GetName(args_count); |
| 69 } |
| 70 const char* GetFunctionName(Name* name) { |
| 71 return function_and_resource_names_.GetFunctionName(name); |
| 72 } |
| 73 const char* GetFunctionName(const char* name) { |
| 74 return function_and_resource_names_.GetFunctionName(name); |
| 75 } |
| 240 | 76 |
| 241 private: | 77 private: |
| 242 void StartProcessorIfNotStarted(); | |
| 243 void StopProcessorIfLastProfile(const char* title); | |
| 244 void StopProcessor(); | |
| 245 void ResetProfiles(); | |
| 246 void LogBuiltins(); | |
| 247 void RecordInliningInfo(CodeEntry* entry, AbstractCode* abstract_code); | 78 void RecordInliningInfo(CodeEntry* entry, AbstractCode* abstract_code); |
| 248 void RecordDeoptInlinedFrames(CodeEntry* entry, AbstractCode* abstract_code); | 79 void RecordDeoptInlinedFrames(CodeEntry* entry, AbstractCode* abstract_code); |
| 249 Name* InferScriptName(Name* name, SharedFunctionInfo* info); | 80 Name* InferScriptName(Name* name, SharedFunctionInfo* info); |
| 81 V8_INLINE void DispatchCodeEvent(const CodeEventsContainer& evt_rec) { |
| 82 for (auto observer : observers_) { |
| 83 observer->CodeEventHandler(evt_rec); |
| 84 } |
| 85 } |
| 250 | 86 |
| 251 Isolate* const isolate_; | 87 StringsStorage function_and_resource_names_; |
| 252 base::TimeDelta sampling_interval_; | 88 std::vector<CodeEntry*> code_entries_; |
| 253 std::unique_ptr<CpuProfilesCollection> profiles_; | 89 std::vector<CodeEventObserver*> observers_; |
| 254 std::unique_ptr<ProfileGenerator> generator_; | |
| 255 std::unique_ptr<ProfilerEventsProcessor> processor_; | |
| 256 bool saved_is_logging_; | |
| 257 bool is_profiling_; | |
| 258 | 90 |
| 259 DISALLOW_COPY_AND_ASSIGN(CpuProfiler); | 91 DISALLOW_COPY_AND_ASSIGN(ProfilerListener); |
| 260 }; | 92 }; |
| 261 | 93 |
| 262 } // namespace internal | 94 } // namespace internal |
| 263 } // namespace v8 | 95 } // namespace v8 |
| 264 | 96 |
| 265 | 97 #endif // V8_PROFILER_PROFILER_LISTENER_H_ |
| 266 #endif // V8_PROFILER_CPU_PROFILER_H_ | |
| OLD | NEW |