OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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_PROFILE_GENERATOR_H_ | 5 #ifndef V8_PROFILE_GENERATOR_H_ |
6 #define V8_PROFILE_GENERATOR_H_ | 6 #define V8_PROFILE_GENERATOR_H_ |
7 | 7 |
| 8 #include <map> |
8 #include "include/v8-profiler.h" | 9 #include "include/v8-profiler.h" |
9 #include "src/allocation.h" | 10 #include "src/allocation.h" |
10 #include "src/hashmap.h" | 11 #include "src/hashmap.h" |
11 | 12 |
12 namespace v8 { | 13 namespace v8 { |
13 namespace internal { | 14 namespace internal { |
14 | 15 |
15 struct OffsetRange; | 16 struct OffsetRange; |
16 | 17 |
17 // Provides a storage of strings allocated in C++ heap, to hold them | 18 // Provides a storage of strings allocated in C++ heap, to hold them |
(...skipping 19 matching lines...) Expand all Loading... |
37 const char* AddOrDisposeString(char* str, int len); | 38 const char* AddOrDisposeString(char* str, int len); |
38 HashMap::Entry* GetEntry(const char* str, int len); | 39 HashMap::Entry* GetEntry(const char* str, int len); |
39 | 40 |
40 uint32_t hash_seed_; | 41 uint32_t hash_seed_; |
41 HashMap names_; | 42 HashMap names_; |
42 | 43 |
43 DISALLOW_COPY_AND_ASSIGN(StringsStorage); | 44 DISALLOW_COPY_AND_ASSIGN(StringsStorage); |
44 }; | 45 }; |
45 | 46 |
46 | 47 |
| 48 // Provides a mapping from the offsets within generated code to |
| 49 // the source line. |
| 50 class JITLineInfoTable : public Malloced { |
| 51 public: |
| 52 JITLineInfoTable() {} |
| 53 ~JITLineInfoTable() {} |
| 54 |
| 55 void SetPosition(int pc_offset, int line) { |
| 56 DCHECK(pc_offset >= 0); |
| 57 DCHECK(line > 0); // The 1-based number of the source line. |
| 58 if (GetSourceLineNumber(pc_offset) != line) { |
| 59 pc_offset_map_.insert(std::make_pair(pc_offset, line)); |
| 60 } |
| 61 } |
| 62 |
| 63 int GetSourceLineNumber(int pc_offset) const { |
| 64 PcOffsetMap::const_iterator it = pc_offset_map_.lower_bound(pc_offset); |
| 65 if (it == pc_offset_map_.end()) { |
| 66 return pc_offset_map_.empty() ? v8::CpuProfileNode::kNoLineNumberInfo |
| 67 : (--pc_offset_map_.end())->second; |
| 68 } |
| 69 return it->second; |
| 70 } |
| 71 |
| 72 bool empty() const { return pc_offset_map_.empty(); } |
| 73 |
| 74 private: |
| 75 // pc_offset -> source line |
| 76 typedef std::map<int, int> PcOffsetMap; |
| 77 PcOffsetMap pc_offset_map_; |
| 78 DISALLOW_COPY_AND_ASSIGN(JITLineInfoTable); |
| 79 }; |
| 80 |
47 class CodeEntry { | 81 class CodeEntry { |
48 public: | 82 public: |
49 // CodeEntry doesn't own name strings, just references them. | 83 // CodeEntry doesn't own name strings, just references them. |
50 inline CodeEntry(Logger::LogEventsAndTags tag, | 84 inline CodeEntry(Logger::LogEventsAndTags tag, const char* name, |
51 const char* name, | |
52 const char* name_prefix = CodeEntry::kEmptyNamePrefix, | 85 const char* name_prefix = CodeEntry::kEmptyNamePrefix, |
53 const char* resource_name = CodeEntry::kEmptyResourceName, | 86 const char* resource_name = CodeEntry::kEmptyResourceName, |
54 int line_number = v8::CpuProfileNode::kNoLineNumberInfo, | 87 int line_number = v8::CpuProfileNode::kNoLineNumberInfo, |
55 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo); | 88 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo, |
| 89 JITLineInfoTable* line_info = NULL, |
| 90 Address instruction_start = NULL); |
56 ~CodeEntry(); | 91 ~CodeEntry(); |
57 | 92 |
58 bool is_js_function() const { return is_js_function_tag(tag_); } | 93 bool is_js_function() const { return is_js_function_tag(tag_); } |
59 const char* name_prefix() const { return name_prefix_; } | 94 const char* name_prefix() const { return name_prefix_; } |
60 bool has_name_prefix() const { return name_prefix_[0] != '\0'; } | 95 bool has_name_prefix() const { return name_prefix_[0] != '\0'; } |
61 const char* name() const { return name_; } | 96 const char* name() const { return name_; } |
62 const char* resource_name() const { return resource_name_; } | 97 const char* resource_name() const { return resource_name_; } |
63 int line_number() const { return line_number_; } | 98 int line_number() const { return line_number_; } |
64 int column_number() const { return column_number_; } | 99 int column_number() const { return column_number_; } |
| 100 const JITLineInfoTable* line_info() const { return line_info_; } |
65 void set_shared_id(int shared_id) { shared_id_ = shared_id; } | 101 void set_shared_id(int shared_id) { shared_id_ = shared_id; } |
66 int script_id() const { return script_id_; } | 102 int script_id() const { return script_id_; } |
67 void set_script_id(int script_id) { script_id_ = script_id; } | 103 void set_script_id(int script_id) { script_id_ = script_id; } |
68 void set_bailout_reason(const char* bailout_reason) { | 104 void set_bailout_reason(const char* bailout_reason) { |
69 bailout_reason_ = bailout_reason; | 105 bailout_reason_ = bailout_reason; |
70 } | 106 } |
71 const char* bailout_reason() const { return bailout_reason_; } | 107 const char* bailout_reason() const { return bailout_reason_; } |
72 | 108 |
73 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag); | 109 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag); |
74 | 110 |
75 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; } | 111 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; } |
76 void set_no_frame_ranges(List<OffsetRange>* ranges) { | 112 void set_no_frame_ranges(List<OffsetRange>* ranges) { |
77 no_frame_ranges_ = ranges; | 113 no_frame_ranges_ = ranges; |
78 } | 114 } |
79 | 115 |
80 void SetBuiltinId(Builtins::Name id); | 116 void SetBuiltinId(Builtins::Name id); |
81 Builtins::Name builtin_id() const { return builtin_id_; } | 117 Builtins::Name builtin_id() const { return builtin_id_; } |
82 | 118 |
83 uint32_t GetCallUid() const; | 119 uint32_t GetCallUid() const; |
84 bool IsSameAs(CodeEntry* entry) const; | 120 bool IsSameAs(CodeEntry* entry) const; |
85 | 121 |
| 122 int GetSourceLine(int pc_offset) const; |
| 123 |
| 124 Address instruction_start() const { return instruction_start_; } |
| 125 |
86 static const char* const kEmptyNamePrefix; | 126 static const char* const kEmptyNamePrefix; |
87 static const char* const kEmptyResourceName; | 127 static const char* const kEmptyResourceName; |
88 static const char* const kEmptyBailoutReason; | 128 static const char* const kEmptyBailoutReason; |
89 | 129 |
90 private: | 130 private: |
91 Logger::LogEventsAndTags tag_ : 8; | 131 Logger::LogEventsAndTags tag_ : 8; |
92 Builtins::Name builtin_id_ : 8; | 132 Builtins::Name builtin_id_ : 8; |
93 const char* name_prefix_; | 133 const char* name_prefix_; |
94 const char* name_; | 134 const char* name_; |
95 const char* resource_name_; | 135 const char* resource_name_; |
96 int line_number_; | 136 int line_number_; |
97 int column_number_; | 137 int column_number_; |
98 int shared_id_; | 138 int shared_id_; |
99 int script_id_; | 139 int script_id_; |
100 List<OffsetRange>* no_frame_ranges_; | 140 List<OffsetRange>* no_frame_ranges_; |
101 const char* bailout_reason_; | 141 const char* bailout_reason_; |
| 142 JITLineInfoTable* line_info_; |
| 143 Address instruction_start_; |
102 | 144 |
103 DISALLOW_COPY_AND_ASSIGN(CodeEntry); | 145 DISALLOW_COPY_AND_ASSIGN(CodeEntry); |
104 }; | 146 }; |
105 | 147 |
106 | 148 |
107 class ProfileTree; | 149 class ProfileTree; |
108 | 150 |
109 class ProfileNode { | 151 class ProfileNode { |
110 public: | 152 public: |
111 inline ProfileNode(ProfileTree* tree, CodeEntry* entry); | 153 inline ProfileNode(ProfileTree* tree, CodeEntry* entry); |
112 | 154 |
113 ProfileNode* FindChild(CodeEntry* entry); | 155 ProfileNode* FindChild(CodeEntry* entry); |
114 ProfileNode* FindOrAddChild(CodeEntry* entry); | 156 ProfileNode* FindOrAddChild(CodeEntry* entry); |
115 void IncrementSelfTicks() { ++self_ticks_; } | 157 void IncrementSelfTicks() { ++self_ticks_; } |
116 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; } | 158 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; } |
| 159 void IncrementLineTicks(int src_line); |
117 | 160 |
118 CodeEntry* entry() const { return entry_; } | 161 CodeEntry* entry() const { return entry_; } |
119 unsigned self_ticks() const { return self_ticks_; } | 162 unsigned self_ticks() const { return self_ticks_; } |
120 const List<ProfileNode*>* children() const { return &children_list_; } | 163 const List<ProfileNode*>* children() const { return &children_list_; } |
121 unsigned id() const { return id_; } | 164 unsigned id() const { return id_; } |
| 165 unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); } |
| 166 bool GetLineTicks(v8::CpuProfileNode::LineTick* entries, |
| 167 unsigned int length) const; |
122 | 168 |
123 void Print(int indent); | 169 void Print(int indent); |
124 | 170 |
125 private: | 171 private: |
126 static bool CodeEntriesMatch(void* entry1, void* entry2) { | 172 static bool CodeEntriesMatch(void* entry1, void* entry2) { |
127 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs( | 173 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs( |
128 reinterpret_cast<CodeEntry*>(entry2)); | 174 reinterpret_cast<CodeEntry*>(entry2)); |
129 } | 175 } |
130 | 176 |
131 static uint32_t CodeEntryHash(CodeEntry* entry) { | 177 static uint32_t CodeEntryHash(CodeEntry* entry) { |
132 return entry->GetCallUid(); | 178 return entry->GetCallUid(); |
133 } | 179 } |
134 | 180 |
135 ProfileTree* tree_; | 181 ProfileTree* tree_; |
136 CodeEntry* entry_; | 182 CodeEntry* entry_; |
137 unsigned self_ticks_; | 183 unsigned self_ticks_; |
138 // Mapping from CodeEntry* to ProfileNode* | 184 // Mapping from CodeEntry* to ProfileNode* |
139 HashMap children_; | 185 HashMap children_; |
140 List<ProfileNode*> children_list_; | 186 List<ProfileNode*> children_list_; |
141 unsigned id_; | 187 unsigned id_; |
| 188 HashMap line_ticks_; |
142 | 189 |
143 DISALLOW_COPY_AND_ASSIGN(ProfileNode); | 190 DISALLOW_COPY_AND_ASSIGN(ProfileNode); |
144 }; | 191 }; |
145 | 192 |
146 | 193 |
147 class ProfileTree { | 194 class ProfileTree { |
148 public: | 195 public: |
149 ProfileTree(); | 196 ProfileTree(); |
150 ~ProfileTree(); | 197 ~ProfileTree(); |
151 | 198 |
152 ProfileNode* AddPathFromEnd(const Vector<CodeEntry*>& path); | 199 ProfileNode* AddPathFromEnd( |
153 void AddPathFromStart(const Vector<CodeEntry*>& path); | 200 const Vector<CodeEntry*>& path, |
| 201 int src_line = v8::CpuProfileNode::kNoLineNumberInfo); |
| 202 void AddPathFromStart(const Vector<CodeEntry*>& path, |
| 203 int src_line = v8::CpuProfileNode::kNoLineNumberInfo); |
154 ProfileNode* root() const { return root_; } | 204 ProfileNode* root() const { return root_; } |
155 unsigned next_node_id() { return next_node_id_++; } | 205 unsigned next_node_id() { return next_node_id_++; } |
156 | 206 |
157 void Print() { | 207 void Print() { |
158 root_->Print(0); | 208 root_->Print(0); |
159 } | 209 } |
160 | 210 |
161 private: | 211 private: |
162 template <typename Callback> | 212 template <typename Callback> |
163 void TraverseDepthFirst(Callback* callback); | 213 void TraverseDepthFirst(Callback* callback); |
164 | 214 |
165 CodeEntry root_entry_; | 215 CodeEntry root_entry_; |
166 unsigned next_node_id_; | 216 unsigned next_node_id_; |
167 ProfileNode* root_; | 217 ProfileNode* root_; |
168 | 218 |
169 DISALLOW_COPY_AND_ASSIGN(ProfileTree); | 219 DISALLOW_COPY_AND_ASSIGN(ProfileTree); |
170 }; | 220 }; |
171 | 221 |
172 | 222 |
173 class CpuProfile { | 223 class CpuProfile { |
174 public: | 224 public: |
175 CpuProfile(const char* title, bool record_samples); | 225 CpuProfile(const char* title, bool record_samples); |
176 | 226 |
177 // Add pc -> ... -> main() call path to the profile. | 227 // Add pc -> ... -> main() call path to the profile. |
178 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path); | 228 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path, |
| 229 int src_line); |
179 void CalculateTotalTicksAndSamplingRate(); | 230 void CalculateTotalTicksAndSamplingRate(); |
180 | 231 |
181 const char* title() const { return title_; } | 232 const char* title() const { return title_; } |
182 const ProfileTree* top_down() const { return &top_down_; } | 233 const ProfileTree* top_down() const { return &top_down_; } |
183 | 234 |
184 int samples_count() const { return samples_.length(); } | 235 int samples_count() const { return samples_.length(); } |
185 ProfileNode* sample(int index) const { return samples_.at(index); } | 236 ProfileNode* sample(int index) const { return samples_.at(index); } |
186 base::TimeTicks sample_timestamp(int index) const { | 237 base::TimeTicks sample_timestamp(int index) const { |
187 return timestamps_.at(index); | 238 return timestamps_.at(index); |
188 } | 239 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 const char* GetFunctionName(Name* name) { | 321 const char* GetFunctionName(Name* name) { |
271 return function_and_resource_names_.GetFunctionName(name); | 322 return function_and_resource_names_.GetFunctionName(name); |
272 } | 323 } |
273 const char* GetFunctionName(const char* name) { | 324 const char* GetFunctionName(const char* name) { |
274 return function_and_resource_names_.GetFunctionName(name); | 325 return function_and_resource_names_.GetFunctionName(name); |
275 } | 326 } |
276 bool IsLastProfile(const char* title); | 327 bool IsLastProfile(const char* title); |
277 void RemoveProfile(CpuProfile* profile); | 328 void RemoveProfile(CpuProfile* profile); |
278 | 329 |
279 CodeEntry* NewCodeEntry( | 330 CodeEntry* NewCodeEntry( |
280 Logger::LogEventsAndTags tag, | 331 Logger::LogEventsAndTags tag, const char* name, |
281 const char* name, | |
282 const char* name_prefix = CodeEntry::kEmptyNamePrefix, | 332 const char* name_prefix = CodeEntry::kEmptyNamePrefix, |
283 const char* resource_name = CodeEntry::kEmptyResourceName, | 333 const char* resource_name = CodeEntry::kEmptyResourceName, |
284 int line_number = v8::CpuProfileNode::kNoLineNumberInfo, | 334 int line_number = v8::CpuProfileNode::kNoLineNumberInfo, |
285 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo); | 335 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo, |
| 336 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL); |
286 | 337 |
287 // Called from profile generator thread. | 338 // Called from profile generator thread. |
288 void AddPathToCurrentProfiles( | 339 void AddPathToCurrentProfiles(base::TimeTicks timestamp, |
289 base::TimeTicks timestamp, const Vector<CodeEntry*>& path); | 340 const Vector<CodeEntry*>& path, int src_line); |
290 | 341 |
291 // Limits the number of profiles that can be simultaneously collected. | 342 // Limits the number of profiles that can be simultaneously collected. |
292 static const int kMaxSimultaneousProfiles = 100; | 343 static const int kMaxSimultaneousProfiles = 100; |
293 | 344 |
294 private: | 345 private: |
295 StringsStorage function_and_resource_names_; | 346 StringsStorage function_and_resource_names_; |
296 List<CodeEntry*> code_entries_; | 347 List<CodeEntry*> code_entries_; |
297 List<CpuProfile*> finished_profiles_; | 348 List<CpuProfile*> finished_profiles_; |
298 | 349 |
299 // Accessed by VM thread and profile generator thread. | 350 // Accessed by VM thread and profile generator thread. |
(...skipping 29 matching lines...) Expand all Loading... |
329 CodeEntry* gc_entry_; | 380 CodeEntry* gc_entry_; |
330 CodeEntry* unresolved_entry_; | 381 CodeEntry* unresolved_entry_; |
331 | 382 |
332 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); | 383 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); |
333 }; | 384 }; |
334 | 385 |
335 | 386 |
336 } } // namespace v8::internal | 387 } } // namespace v8::internal |
337 | 388 |
338 #endif // V8_PROFILE_GENERATOR_H_ | 389 #endif // V8_PROFILE_GENERATOR_H_ |
OLD | NEW |