blob: 2127a1e1ca78179be71aedab100cc152b9c7e8e7 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block6ded16b2010-05-10 14:33:55 +01004
5#ifndef V8_PROFILE_GENERATOR_H_
6#define V8_PROFILE_GENERATOR_H_
7
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <map>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "include/v8-profiler.h"
10#include "src/allocation.h"
11#include "src/hashmap.h"
Steve Block6ded16b2010-05-10 14:33:55 +010012
13namespace v8 {
14namespace internal {
15
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016struct OffsetRange;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010017
18// Provides a storage of strings allocated in C++ heap, to hold them
19// forever, even if they disappear from JS heap or external storage.
20class StringsStorage {
21 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022 explicit StringsStorage(Heap* heap);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010023 ~StringsStorage();
24
Steve Block44f0eee2011-05-26 01:26:41 +010025 const char* GetCopy(const char* src);
26 const char* GetFormatted(const char* format, ...);
27 const char* GetVFormatted(const char* format, va_list args);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 const char* GetName(Name* name);
Ben Murdochf87a2032010-10-22 12:50:53 +010029 const char* GetName(int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 const char* GetFunctionName(Name* name);
31 const char* GetFunctionName(const char* name);
32 size_t GetUsedMemorySize() const;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010033
34 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +010035 static const int kMaxNameSize = 1024;
36
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 static bool StringsMatch(void* key1, void* key2);
38 const char* AddOrDisposeString(char* str, int len);
39 HashMap::Entry* GetEntry(const char* str, int len);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010040
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 uint32_t hash_seed_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010042 HashMap names_;
43
44 DISALLOW_COPY_AND_ASSIGN(StringsStorage);
Leon Clarkef7060e22010-06-03 12:02:55 +010045};
46
47
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048// Provides a mapping from the offsets within generated code to
49// the source line.
50class JITLineInfoTable : public Malloced {
51 public:
52 JITLineInfoTable();
53 ~JITLineInfoTable();
54
55 void SetPosition(int pc_offset, int line);
56 int GetSourceLineNumber(int pc_offset) const;
57
58 bool empty() const { return pc_offset_map_.empty(); }
59
60 private:
61 // pc_offset -> source line
62 typedef std::map<int, int> PcOffsetMap;
63 PcOffsetMap pc_offset_map_;
64 DISALLOW_COPY_AND_ASSIGN(JITLineInfoTable);
65};
66
Steve Block6ded16b2010-05-10 14:33:55 +010067class CodeEntry {
68 public:
69 // CodeEntry doesn't own name strings, just references them.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040070 inline CodeEntry(Logger::LogEventsAndTags tag, const char* name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
72 const char* resource_name = CodeEntry::kEmptyResourceName,
73 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
75 JITLineInfoTable* line_info = NULL,
76 Address instruction_start = NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 ~CodeEntry();
Steve Block6ded16b2010-05-10 14:33:55 +010078
Emily Bernierd0a1eb72015-03-24 16:35:39 -040079 bool is_js_function() const { return is_js_function_tag(tag()); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 const char* name_prefix() const { return name_prefix_; }
81 bool has_name_prefix() const { return name_prefix_[0] != '\0'; }
82 const char* name() const { return name_; }
83 const char* resource_name() const { return resource_name_; }
84 int line_number() const { return line_number_; }
85 int column_number() const { return column_number_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040086 const JITLineInfoTable* line_info() const { return line_info_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 void set_shared_id(int shared_id) { shared_id_ = shared_id; }
88 int script_id() const { return script_id_; }
89 void set_script_id(int script_id) { script_id_ = script_id; }
90 void set_bailout_reason(const char* bailout_reason) {
91 bailout_reason_ = bailout_reason;
92 }
93 const char* bailout_reason() const { return bailout_reason_; }
Steve Block6ded16b2010-05-10 14:33:55 +010094
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag);
Steve Block6ded16b2010-05-10 14:33:55 +010096
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
98 void set_no_frame_ranges(List<OffsetRange>* ranges) {
99 no_frame_ranges_ = ranges;
100 }
101
102 void SetBuiltinId(Builtins::Name id);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400103 Builtins::Name builtin_id() const {
104 return BuiltinIdField::decode(bit_field_);
105 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100107 uint32_t GetCallUid() const;
108 bool IsSameAs(CodeEntry* entry) const;
Leon Clarkef7060e22010-06-03 12:02:55 +0100109
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400110 int GetSourceLine(int pc_offset) const;
111
112 Address instruction_start() const { return instruction_start_; }
113
Steve Block44f0eee2011-05-26 01:26:41 +0100114 static const char* const kEmptyNamePrefix;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 static const char* const kEmptyResourceName;
116 static const char* const kEmptyBailoutReason;
Steve Block6ded16b2010-05-10 14:33:55 +0100117
118 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400119 class TagField : public BitField<Logger::LogEventsAndTags, 0, 8> {};
120 class BuiltinIdField : public BitField<Builtins::Name, 8, 8> {};
121 Logger::LogEventsAndTags tag() const { return TagField::decode(bit_field_); }
122
123 uint32_t bit_field_;
Steve Block6ded16b2010-05-10 14:33:55 +0100124 const char* name_prefix_;
125 const char* name_;
126 const char* resource_name_;
127 int line_number_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 int column_number_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100129 int shared_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 int script_id_;
131 List<OffsetRange>* no_frame_ranges_;
132 const char* bailout_reason_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400133 JITLineInfoTable* line_info_;
134 Address instruction_start_;
Steve Block6ded16b2010-05-10 14:33:55 +0100135
Steve Block6ded16b2010-05-10 14:33:55 +0100136 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
137};
138
139
140class ProfileTree;
141
142class ProfileNode {
143 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
Steve Block6ded16b2010-05-10 14:33:55 +0100145
146 ProfileNode* FindChild(CodeEntry* entry);
147 ProfileNode* FindOrAddChild(CodeEntry* entry);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 void IncrementSelfTicks() { ++self_ticks_; }
149 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400150 void IncrementLineTicks(int src_line);
Steve Block6ded16b2010-05-10 14:33:55 +0100151
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 CodeEntry* entry() const { return entry_; }
153 unsigned self_ticks() const { return self_ticks_; }
154 const List<ProfileNode*>* children() const { return &children_list_; }
155 unsigned id() const { return id_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400156 unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
157 bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
158 unsigned int length) const;
Steve Block6ded16b2010-05-10 14:33:55 +0100159
160 void Print(int indent);
161
162 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 static bool CodeEntriesMatch(void* entry1, void* entry2) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100164 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs(
165 reinterpret_cast<CodeEntry*>(entry2));
Steve Block6ded16b2010-05-10 14:33:55 +0100166 }
167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 static uint32_t CodeEntryHash(CodeEntry* entry) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100169 return entry->GetCallUid();
Steve Block6ded16b2010-05-10 14:33:55 +0100170 }
171
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400172 static bool LineTickMatch(void* a, void* b) { return a == b; }
173
Steve Block6ded16b2010-05-10 14:33:55 +0100174 ProfileTree* tree_;
175 CodeEntry* entry_;
Steve Block6ded16b2010-05-10 14:33:55 +0100176 unsigned self_ticks_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100177 // Mapping from CodeEntry* to ProfileNode*
Steve Block6ded16b2010-05-10 14:33:55 +0100178 HashMap children_;
179 List<ProfileNode*> children_list_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 unsigned id_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400181 HashMap line_ticks_;
Steve Block6ded16b2010-05-10 14:33:55 +0100182
183 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
184};
185
186
187class ProfileTree {
188 public:
189 ProfileTree();
190 ~ProfileTree();
191
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400192 ProfileNode* AddPathFromEnd(
193 const Vector<CodeEntry*>& path,
194 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
195 void AddPathFromStart(const Vector<CodeEntry*>& path,
196 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
Steve Block6ded16b2010-05-10 14:33:55 +0100197 ProfileNode* root() const { return root_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 unsigned next_node_id() { return next_node_id_++; }
Steve Block6ded16b2010-05-10 14:33:55 +0100199
Steve Block6ded16b2010-05-10 14:33:55 +0100200 void Print() {
201 root_->Print(0);
202 }
203
204 private:
205 template <typename Callback>
Leon Clarkef7060e22010-06-03 12:02:55 +0100206 void TraverseDepthFirst(Callback* callback);
Steve Block6ded16b2010-05-10 14:33:55 +0100207
208 CodeEntry root_entry_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 unsigned next_node_id_;
Steve Block6ded16b2010-05-10 14:33:55 +0100210 ProfileNode* root_;
Steve Block6ded16b2010-05-10 14:33:55 +0100211
212 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
213};
214
215
216class CpuProfile {
217 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 CpuProfile(const char* title, bool record_samples);
Steve Block6ded16b2010-05-10 14:33:55 +0100219
220 // Add pc -> ... -> main() call path to the profile.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400221 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path,
222 int src_line);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 void CalculateTotalTicksAndSamplingRate();
Steve Block6ded16b2010-05-10 14:33:55 +0100224
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 const char* title() const { return title_; }
226 const ProfileTree* top_down() const { return &top_down_; }
227
228 int samples_count() const { return samples_.length(); }
229 ProfileNode* sample(int index) const { return samples_.at(index); }
230 base::TimeTicks sample_timestamp(int index) const {
231 return timestamps_.at(index);
232 }
233
234 base::TimeTicks start_time() const { return start_time_; }
235 base::TimeTicks end_time() const { return end_time_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100236
237 void UpdateTicksScale();
238
Steve Block6ded16b2010-05-10 14:33:55 +0100239 void Print();
240
241 private:
242 const char* title_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 bool record_samples_;
244 base::TimeTicks start_time_;
245 base::TimeTicks end_time_;
246 List<ProfileNode*> samples_;
247 List<base::TimeTicks> timestamps_;
Steve Block6ded16b2010-05-10 14:33:55 +0100248 ProfileTree top_down_;
Steve Block6ded16b2010-05-10 14:33:55 +0100249
250 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
251};
252
253
254class CodeMap {
255 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100256 CodeMap() : next_shared_id_(1) { }
Ben Murdoch589d6972011-11-30 16:04:58 +0000257 void AddCode(Address addr, CodeEntry* entry, unsigned size);
258 void MoveCode(Address from, Address to);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 CodeEntry* FindEntry(Address addr, Address* start = NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100260 int GetSharedId(Address addr);
Steve Block6ded16b2010-05-10 14:33:55 +0100261
262 void Print();
263
264 private:
265 struct CodeEntryInfo {
266 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
267 : entry(an_entry), size(a_size) { }
268 CodeEntry* entry;
269 unsigned size;
270 };
271
272 struct CodeTreeConfig {
273 typedef Address Key;
274 typedef CodeEntryInfo Value;
275 static const Key kNoKey;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100276 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
Steve Block6ded16b2010-05-10 14:33:55 +0100277 static int Compare(const Key& a, const Key& b) {
278 return a < b ? -1 : (a > b ? 1 : 0);
279 }
280 };
281 typedef SplayTree<CodeTreeConfig> CodeTree;
282
283 class CodeTreePrinter {
284 public:
285 void Call(const Address& key, const CodeEntryInfo& value);
286 };
287
Ben Murdoch589d6972011-11-30 16:04:58 +0000288 void DeleteAllCoveredCode(Address start, Address end);
289
Steve Block44f0eee2011-05-26 01:26:41 +0100290 // Fake CodeEntry pointer to distinguish shared function entries.
291 static CodeEntry* const kSharedFunctionCodeEntry;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100292
Steve Block6ded16b2010-05-10 14:33:55 +0100293 CodeTree tree_;
Steve Block44f0eee2011-05-26 01:26:41 +0100294 int next_shared_id_;
Steve Block6ded16b2010-05-10 14:33:55 +0100295
296 DISALLOW_COPY_AND_ASSIGN(CodeMap);
297};
298
299
300class CpuProfilesCollection {
301 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 explicit CpuProfilesCollection(Heap* heap);
Steve Block6ded16b2010-05-10 14:33:55 +0100303 ~CpuProfilesCollection();
304
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305 bool StartProfiling(const char* title, bool record_samples);
306 CpuProfile* StopProfiling(const char* title);
307 List<CpuProfile*>* profiles() { return &finished_profiles_; }
308 const char* GetName(Name* name) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100309 return function_and_resource_names_.GetName(name);
310 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100311 const char* GetName(int args_count) {
312 return function_and_resource_names_.GetName(args_count);
313 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 const char* GetFunctionName(Name* name) {
Steve Block791712a2010-08-27 10:21:07 +0100315 return function_and_resource_names_.GetFunctionName(name);
316 }
317 const char* GetFunctionName(const char* name) {
318 return function_and_resource_names_.GetFunctionName(name);
319 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 bool IsLastProfile(const char* title);
321 void RemoveProfile(CpuProfile* profile);
Steve Block6ded16b2010-05-10 14:33:55 +0100322
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323 CodeEntry* NewCodeEntry(
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400324 Logger::LogEventsAndTags tag, const char* name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
326 const char* resource_name = CodeEntry::kEmptyResourceName,
327 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400328 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
329 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL);
Steve Block6ded16b2010-05-10 14:33:55 +0100330
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 // Called from profile generator thread.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400332 void AddPathToCurrentProfiles(base::TimeTicks timestamp,
333 const Vector<CodeEntry*>& path, int src_line);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334
335 // Limits the number of profiles that can be simultaneously collected.
336 static const int kMaxSimultaneousProfiles = 100;
337
338 private:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100339 StringsStorage function_and_resource_names_;
Steve Block6ded16b2010-05-10 14:33:55 +0100340 List<CodeEntry*> code_entries_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 List<CpuProfile*> finished_profiles_;
Steve Block6ded16b2010-05-10 14:33:55 +0100342
343 // Accessed by VM thread and profile generator thread.
344 List<CpuProfile*> current_profiles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 base::Semaphore current_profiles_semaphore_;
Steve Block6ded16b2010-05-10 14:33:55 +0100346
347 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
348};
349
350
Steve Block6ded16b2010-05-10 14:33:55 +0100351class ProfileGenerator {
352 public:
353 explicit ProfileGenerator(CpuProfilesCollection* profiles);
354
Steve Block6ded16b2010-05-10 14:33:55 +0100355 void RecordTickSample(const TickSample& sample);
356
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 CodeMap* code_map() { return &code_map_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100358
Steve Block44f0eee2011-05-26 01:26:41 +0100359 static const char* const kProgramEntryName;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 static const char* const kIdleEntryName;
Steve Block44f0eee2011-05-26 01:26:41 +0100361 static const char* const kGarbageCollectorEntryName;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362 // Used to represent frames for which we have no reliable way to
363 // detect function.
364 static const char* const kUnresolvedFunctionName;
Steve Block6ded16b2010-05-10 14:33:55 +0100365
366 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 CodeEntry* EntryForVMState(StateTag tag);
Steve Block6ded16b2010-05-10 14:33:55 +0100368
369 CpuProfilesCollection* profiles_;
370 CodeMap code_map_;
371 CodeEntry* program_entry_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 CodeEntry* idle_entry_;
Steve Block6ded16b2010-05-10 14:33:55 +0100373 CodeEntry* gc_entry_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 CodeEntry* unresolved_entry_;
Steve Block6ded16b2010-05-10 14:33:55 +0100375
376 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
377};
378
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100379
Steve Block6ded16b2010-05-10 14:33:55 +0100380} } // namespace v8::internal
381
Steve Block6ded16b2010-05-10 14:33:55 +0100382#endif // V8_PROFILE_GENERATOR_H_