| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 const char* name_; | 63 const char* name_; |
| 64 const char* resource_name_; | 64 const char* resource_name_; |
| 65 int line_number_; | 65 int line_number_; |
| 66 | 66 |
| 67 static unsigned next_call_uid_; | 67 static unsigned next_call_uid_; |
| 68 | 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(CodeEntry); | 69 DISALLOW_COPY_AND_ASSIGN(CodeEntry); |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 | 72 |
| 73 class ProfileTree; |
| 74 |
| 73 class ProfileNode { | 75 class ProfileNode { |
| 74 public: | 76 public: |
| 75 INLINE(explicit ProfileNode(CodeEntry* entry)); | 77 INLINE(ProfileNode(ProfileTree* tree, CodeEntry* entry)); |
| 76 | 78 |
| 77 ProfileNode* FindChild(CodeEntry* entry); | 79 ProfileNode* FindChild(CodeEntry* entry); |
| 78 ProfileNode* FindOrAddChild(CodeEntry* entry); | 80 ProfileNode* FindOrAddChild(CodeEntry* entry); |
| 79 INLINE(void IncrementSelfTicks()) { ++self_ticks_; } | 81 INLINE(void IncrementSelfTicks()) { ++self_ticks_; } |
| 80 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; } | 82 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; } |
| 81 | 83 |
| 82 INLINE(CodeEntry* entry() const) { return entry_; } | 84 INLINE(CodeEntry* entry() const) { return entry_; } |
| 85 INLINE(unsigned self_ticks() const) { return self_ticks_; } |
| 83 INLINE(unsigned total_ticks() const) { return total_ticks_; } | 86 INLINE(unsigned total_ticks() const) { return total_ticks_; } |
| 84 INLINE(unsigned self_ticks() const) { return self_ticks_; } | |
| 85 INLINE(const List<ProfileNode*>* children() const) { return &children_list_; } | 87 INLINE(const List<ProfileNode*>* children() const) { return &children_list_; } |
| 88 double GetSelfMillis() const; |
| 89 double GetTotalMillis() const; |
| 86 | 90 |
| 87 void Print(int indent); | 91 void Print(int indent); |
| 88 | 92 |
| 89 private: | 93 private: |
| 90 INLINE(static bool CodeEntriesMatch(void* entry1, void* entry2)) { | 94 INLINE(static bool CodeEntriesMatch(void* entry1, void* entry2)) { |
| 91 return entry1 == entry2; | 95 return entry1 == entry2; |
| 92 } | 96 } |
| 93 | 97 |
| 94 INLINE(static uint32_t CodeEntryHash(CodeEntry* entry)) { | 98 INLINE(static uint32_t CodeEntryHash(CodeEntry* entry)) { |
| 95 return static_cast<int32_t>(reinterpret_cast<intptr_t>(entry)); | 99 return static_cast<int32_t>(reinterpret_cast<intptr_t>(entry)); |
| 96 } | 100 } |
| 97 | 101 |
| 102 ProfileTree* tree_; |
| 98 CodeEntry* entry_; | 103 CodeEntry* entry_; |
| 99 unsigned total_ticks_; | 104 unsigned total_ticks_; |
| 100 unsigned self_ticks_; | 105 unsigned self_ticks_; |
| 101 // CodeEntry* -> ProfileNode* | 106 // CodeEntry* -> ProfileNode* |
| 102 HashMap children_; | 107 HashMap children_; |
| 103 List<ProfileNode*> children_list_; | 108 List<ProfileNode*> children_list_; |
| 104 | 109 |
| 105 DISALLOW_COPY_AND_ASSIGN(ProfileNode); | 110 DISALLOW_COPY_AND_ASSIGN(ProfileNode); |
| 106 }; | 111 }; |
| 107 | 112 |
| 108 | 113 |
| 109 class ProfileTree { | 114 class ProfileTree { |
| 110 public: | 115 public: |
| 111 ProfileTree(); | 116 ProfileTree(); |
| 112 ~ProfileTree(); | 117 ~ProfileTree(); |
| 113 | 118 |
| 114 void AddPathFromEnd(const Vector<CodeEntry*>& path); | 119 void AddPathFromEnd(const Vector<CodeEntry*>& path); |
| 115 void AddPathFromStart(const Vector<CodeEntry*>& path); | 120 void AddPathFromStart(const Vector<CodeEntry*>& path); |
| 116 void CalculateTotalTicks(); | 121 void CalculateTotalTicks(); |
| 117 | 122 |
| 123 double TicksToMillis(unsigned ticks) const { |
| 124 return ticks * ms_to_ticks_scale_; |
| 125 } |
| 118 ProfileNode* root() const { return root_; } | 126 ProfileNode* root() const { return root_; } |
| 127 void SetTickRatePerMs(double ticks_per_ms); |
| 119 | 128 |
| 120 void ShortPrint(); | 129 void ShortPrint(); |
| 121 void Print() { | 130 void Print() { |
| 122 root_->Print(0); | 131 root_->Print(0); |
| 123 } | 132 } |
| 124 | 133 |
| 125 private: | 134 private: |
| 126 template <typename Callback> | 135 template <typename Callback> |
| 127 void TraverseBreadthFirstPostOrder(Callback* callback); | 136 void TraverseDepthFirstPostOrder(Callback* callback); |
| 128 | 137 |
| 129 CodeEntry root_entry_; | 138 CodeEntry root_entry_; |
| 130 ProfileNode* root_; | 139 ProfileNode* root_; |
| 140 double ms_to_ticks_scale_; |
| 131 | 141 |
| 132 DISALLOW_COPY_AND_ASSIGN(ProfileTree); | 142 DISALLOW_COPY_AND_ASSIGN(ProfileTree); |
| 133 }; | 143 }; |
| 134 | 144 |
| 135 | 145 |
| 136 class CpuProfile { | 146 class CpuProfile { |
| 137 public: | 147 public: |
| 138 CpuProfile(const char* title, unsigned uid) | 148 CpuProfile(const char* title, unsigned uid) |
| 139 : title_(title), uid_(uid) { } | 149 : title_(title), uid_(uid) { } |
| 140 | 150 |
| 141 // Add pc -> ... -> main() call path to the profile. | 151 // Add pc -> ... -> main() call path to the profile. |
| 142 void AddPath(const Vector<CodeEntry*>& path); | 152 void AddPath(const Vector<CodeEntry*>& path); |
| 143 void CalculateTotalTicks(); | 153 void CalculateTotalTicks(); |
| 154 void SetActualSamplingRate(double actual_sampling_rate); |
| 144 | 155 |
| 145 INLINE(const char* title() const) { return title_; } | 156 INLINE(const char* title() const) { return title_; } |
| 146 INLINE(unsigned uid() const) { return uid_; } | 157 INLINE(unsigned uid() const) { return uid_; } |
| 147 INLINE(const ProfileTree* top_down() const) { return &top_down_; } | 158 INLINE(const ProfileTree* top_down() const) { return &top_down_; } |
| 148 INLINE(const ProfileTree* bottom_up() const) { return &bottom_up_; } | 159 INLINE(const ProfileTree* bottom_up() const) { return &bottom_up_; } |
| 149 | 160 |
| 161 void UpdateTicksScale(); |
| 162 |
| 150 void ShortPrint(); | 163 void ShortPrint(); |
| 151 void Print(); | 164 void Print(); |
| 152 | 165 |
| 153 private: | 166 private: |
| 154 const char* title_; | 167 const char* title_; |
| 155 unsigned uid_; | 168 unsigned uid_; |
| 156 ProfileTree top_down_; | 169 ProfileTree top_down_; |
| 157 ProfileTree bottom_up_; | 170 ProfileTree bottom_up_; |
| 158 | 171 |
| 159 DISALLOW_COPY_AND_ASSIGN(CpuProfile); | 172 DISALLOW_COPY_AND_ASSIGN(CpuProfile); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 }; | 214 }; |
| 202 | 215 |
| 203 | 216 |
| 204 class CpuProfilesCollection { | 217 class CpuProfilesCollection { |
| 205 public: | 218 public: |
| 206 CpuProfilesCollection(); | 219 CpuProfilesCollection(); |
| 207 ~CpuProfilesCollection(); | 220 ~CpuProfilesCollection(); |
| 208 | 221 |
| 209 bool StartProfiling(const char* title, unsigned uid); | 222 bool StartProfiling(const char* title, unsigned uid); |
| 210 bool StartProfiling(String* title, unsigned uid); | 223 bool StartProfiling(String* title, unsigned uid); |
| 211 CpuProfile* StopProfiling(const char* title); | 224 CpuProfile* StopProfiling(const char* title, double actual_sampling_rate); |
| 212 CpuProfile* StopProfiling(String* title); | 225 CpuProfile* StopProfiling(String* title, double actual_sampling_rate); |
| 213 INLINE(List<CpuProfile*>* profiles()) { return &profiles_; } | 226 INLINE(List<CpuProfile*>* profiles()) { return &profiles_; } |
| 214 CpuProfile* GetProfile(unsigned uid); | 227 CpuProfile* GetProfile(unsigned uid); |
| 215 inline bool is_last_profile(); | 228 inline bool is_last_profile(); |
| 216 | 229 |
| 217 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 230 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
| 218 String* name, String* resource_name, int line_number); | 231 String* name, String* resource_name, int line_number); |
| 219 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name); | 232 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name); |
| 220 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 233 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
| 221 const char* name_prefix, String* name); | 234 const char* name_prefix, String* name); |
| 222 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, int args_count); | 235 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, int args_count); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 249 HashMap profiles_uids_; | 262 HashMap profiles_uids_; |
| 250 | 263 |
| 251 // Accessed by VM thread and profile generator thread. | 264 // Accessed by VM thread and profile generator thread. |
| 252 List<CpuProfile*> current_profiles_; | 265 List<CpuProfile*> current_profiles_; |
| 253 Semaphore* current_profiles_semaphore_; | 266 Semaphore* current_profiles_semaphore_; |
| 254 | 267 |
| 255 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection); | 268 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection); |
| 256 }; | 269 }; |
| 257 | 270 |
| 258 | 271 |
| 272 class SampleRateCalculator { |
| 273 public: |
| 274 SampleRateCalculator() |
| 275 : result_(Logger::kSamplingIntervalMs * kResultScale), |
| 276 ticks_per_ms_(Logger::kSamplingIntervalMs), |
| 277 measurements_count_(0), |
| 278 wall_time_query_countdown_(1) { |
| 279 } |
| 280 |
| 281 double ticks_per_ms() { |
| 282 return result_ / static_cast<double>(kResultScale); |
| 283 } |
| 284 void Tick(); |
| 285 void UpdateMeasurements(double current_time); |
| 286 |
| 287 // Instead of querying current wall time each tick, |
| 288 // we use this constant to control query intervals. |
| 289 static const unsigned kWallTimeQueryIntervalMs = 100; |
| 290 |
| 291 private: |
| 292 // As the result needs to be accessed from a different thread, we |
| 293 // use type that guarantees atomic writes to memory. There should |
| 294 // be <= 1000 ticks per second, thus storing a value of a 10 ** 5 |
| 295 // order should provide enough precision while keeping away from a |
| 296 // potential overflow. |
| 297 static const int kResultScale = 100000; |
| 298 |
| 299 AtomicWord result_; |
| 300 // All other fields are accessed only from the sampler thread. |
| 301 double ticks_per_ms_; |
| 302 unsigned measurements_count_; |
| 303 unsigned wall_time_query_countdown_; |
| 304 double last_wall_time_; |
| 305 }; |
| 306 |
| 307 |
| 259 class ProfileGenerator { | 308 class ProfileGenerator { |
| 260 public: | 309 public: |
| 261 explicit ProfileGenerator(CpuProfilesCollection* profiles); | 310 explicit ProfileGenerator(CpuProfilesCollection* profiles); |
| 262 | 311 |
| 263 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 312 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
| 264 String* name, | 313 String* name, |
| 265 String* resource_name, | 314 String* resource_name, |
| 266 int line_number)) { | 315 int line_number)) { |
| 267 return profiles_->NewCodeEntry(tag, name, resource_name, line_number); | 316 return profiles_->NewCodeEntry(tag, name, resource_name, line_number); |
| 268 } | 317 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 280 | 329 |
| 281 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 330 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
| 282 int args_count)) { | 331 int args_count)) { |
| 283 return profiles_->NewCodeEntry(tag, args_count); | 332 return profiles_->NewCodeEntry(tag, args_count); |
| 284 } | 333 } |
| 285 | 334 |
| 286 void RecordTickSample(const TickSample& sample); | 335 void RecordTickSample(const TickSample& sample); |
| 287 | 336 |
| 288 INLINE(CodeMap* code_map()) { return &code_map_; } | 337 INLINE(CodeMap* code_map()) { return &code_map_; } |
| 289 | 338 |
| 339 INLINE(void Tick()) { sample_rate_calc_.Tick(); } |
| 340 INLINE(double actual_sampling_rate()) { |
| 341 return sample_rate_calc_.ticks_per_ms(); |
| 342 } |
| 343 |
| 290 static const char* kAnonymousFunctionName; | 344 static const char* kAnonymousFunctionName; |
| 291 static const char* kProgramEntryName; | 345 static const char* kProgramEntryName; |
| 292 static const char* kGarbageCollectorEntryName; | 346 static const char* kGarbageCollectorEntryName; |
| 293 | 347 |
| 294 private: | 348 private: |
| 295 INLINE(CodeEntry* EntryForVMState(StateTag tag)); | 349 INLINE(CodeEntry* EntryForVMState(StateTag tag)); |
| 296 | 350 |
| 297 CpuProfilesCollection* profiles_; | 351 CpuProfilesCollection* profiles_; |
| 298 CodeMap code_map_; | 352 CodeMap code_map_; |
| 299 CodeEntry* program_entry_; | 353 CodeEntry* program_entry_; |
| 300 CodeEntry* gc_entry_; | 354 CodeEntry* gc_entry_; |
| 355 SampleRateCalculator sample_rate_calc_; |
| 301 | 356 |
| 302 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); | 357 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); |
| 303 }; | 358 }; |
| 304 | 359 |
| 305 } } // namespace v8::internal | 360 } } // namespace v8::internal |
| 306 | 361 |
| 307 #endif // ENABLE_LOGGING_AND_PROFILING | 362 #endif // ENABLE_LOGGING_AND_PROFILING |
| 308 | 363 |
| 309 #endif // V8_PROFILE_GENERATOR_H_ | 364 #endif // V8_PROFILE_GENERATOR_H_ |
| OLD | NEW |