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
3 // found in the LICENSE file.
5 #ifndef V8_PROFILE_GENERATOR_H_
6 #define V8_PROFILE_GENERATOR_H_
9 #include "include/v8-profiler.h"
10 #include "src/allocation.h"
11 #include "src/hashmap.h"
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.
20 class StringsStorage {
22 explicit StringsStorage(Heap* heap);
25 const char* GetCopy(const char* src);
26 const char* GetFormatted(const char* format, ...);
27 const char* GetVFormatted(const char* format, va_list args);
28 const char* GetName(Name* name);
29 const char* GetName(int index);
30 const char* GetFunctionName(Name* name);
31 const char* GetFunctionName(const char* name);
32 size_t GetUsedMemorySize() const;
35 static const int kMaxNameSize = 1024;
37 static bool StringsMatch(void* key1, void* key2);
38 const char* AddOrDisposeString(char* str, int len);
39 HashMap::Entry* GetEntry(const char* str, int len);
44 DISALLOW_COPY_AND_ASSIGN(StringsStorage);
48 // Provides a mapping from the offsets within generated code to
50 class JITLineInfoTable : public Malloced {
55 void SetPosition(int pc_offset, int line);
56 int GetSourceLineNumber(int pc_offset) const;
58 bool empty() const { return pc_offset_map_.empty(); }
61 // pc_offset -> source line
62 typedef std::map<int, int> PcOffsetMap;
63 PcOffsetMap pc_offset_map_;
64 DISALLOW_COPY_AND_ASSIGN(JITLineInfoTable);
69 // CodeEntry doesn't own name strings, just references them.
70 inline CodeEntry(Logger::LogEventsAndTags tag, const char* name,
71 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
72 const char* resource_name = CodeEntry::kEmptyResourceName,
73 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
74 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
75 JITLineInfoTable* line_info = NULL,
76 Address instruction_start = NULL);
79 bool is_js_function() const { return is_js_function_tag(tag()); }
80 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_; }
86 const JITLineInfoTable* line_info() const { return line_info_; }
87 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;
93 const char* bailout_reason() const { return bailout_reason_; }
95 void set_deopt_info(const char* deopt_reason, int location) {
96 DCHECK(!deopt_location_);
97 deopt_reason_ = deopt_reason;
98 deopt_location_ = location;
100 const char* deopt_reason() const { return deopt_reason_; }
101 int deopt_location() const { return deopt_location_; }
102 bool has_deopt_info() const { return deopt_location_; }
103 void clear_deopt_info() {
104 deopt_reason_ = kNoDeoptReason;
108 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag);
110 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
111 void set_no_frame_ranges(List<OffsetRange>* ranges) {
112 no_frame_ranges_ = ranges;
115 void SetBuiltinId(Builtins::Name id);
116 Builtins::Name builtin_id() const {
117 return BuiltinIdField::decode(bit_field_);
120 uint32_t GetCallUid() const;
121 bool IsSameAs(CodeEntry* entry) const;
123 int GetSourceLine(int pc_offset) const;
125 Address instruction_start() const { return instruction_start_; }
127 static const char* const kEmptyNamePrefix;
128 static const char* const kEmptyResourceName;
129 static const char* const kEmptyBailoutReason;
130 static const char* const kNoDeoptReason;
133 class TagField : public BitField<Logger::LogEventsAndTags, 0, 8> {};
134 class BuiltinIdField : public BitField<Builtins::Name, 8, 8> {};
135 Logger::LogEventsAndTags tag() const { return TagField::decode(bit_field_); }
138 const char* name_prefix_;
140 const char* resource_name_;
145 List<OffsetRange>* no_frame_ranges_;
146 const char* bailout_reason_;
147 const char* deopt_reason_;
149 JITLineInfoTable* line_info_;
150 Address instruction_start_;
152 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
161 DeoptInfo(const char* deopt_reason, int deopt_location)
162 : deopt_reason(deopt_reason), deopt_location(deopt_location) {}
163 DeoptInfo(const DeoptInfo& info)
164 : deopt_reason(info.deopt_reason),
165 deopt_location(info.deopt_location) {}
166 const char* deopt_reason;
171 inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
173 ProfileNode* FindChild(CodeEntry* entry);
174 ProfileNode* FindOrAddChild(CodeEntry* entry);
175 void IncrementSelfTicks() { ++self_ticks_; }
176 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
177 void IncrementLineTicks(int src_line);
179 CodeEntry* entry() const { return entry_; }
180 unsigned self_ticks() const { return self_ticks_; }
181 const List<ProfileNode*>* children() const { return &children_list_; }
182 unsigned id() const { return id_; }
183 unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
184 bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
185 unsigned int length) const;
186 void CollectDeoptInfo(CodeEntry* entry);
187 const List<DeoptInfo>& deopt_infos() const { return deopt_infos_; }
189 void Print(int indent);
192 static bool CodeEntriesMatch(void* entry1, void* entry2) {
193 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs(
194 reinterpret_cast<CodeEntry*>(entry2));
197 static uint32_t CodeEntryHash(CodeEntry* entry) {
198 return entry->GetCallUid();
201 static bool LineTickMatch(void* a, void* b) { return a == b; }
205 unsigned self_ticks_;
206 // Mapping from CodeEntry* to ProfileNode*
208 List<ProfileNode*> children_list_;
212 List<DeoptInfo> deopt_infos_;
213 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
222 ProfileNode* AddPathFromEnd(
223 const Vector<CodeEntry*>& path,
224 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
225 ProfileNode* root() const { return root_; }
226 unsigned next_node_id() { return next_node_id_++; }
233 template <typename Callback>
234 void TraverseDepthFirst(Callback* callback);
236 CodeEntry root_entry_;
237 unsigned next_node_id_;
240 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
246 CpuProfile(const char* title, bool record_samples);
248 // Add pc -> ... -> main() call path to the profile.
249 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path,
251 void CalculateTotalTicksAndSamplingRate();
253 const char* title() const { return title_; }
254 const ProfileTree* top_down() const { return &top_down_; }
256 int samples_count() const { return samples_.length(); }
257 ProfileNode* sample(int index) const { return samples_.at(index); }
258 base::TimeTicks sample_timestamp(int index) const {
259 return timestamps_.at(index);
262 base::TimeTicks start_time() const { return start_time_; }
263 base::TimeTicks end_time() const { return end_time_; }
265 void UpdateTicksScale();
271 bool record_samples_;
272 base::TimeTicks start_time_;
273 base::TimeTicks end_time_;
274 List<ProfileNode*> samples_;
275 List<base::TimeTicks> timestamps_;
276 ProfileTree top_down_;
278 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
284 CodeMap() : next_shared_id_(1) { }
285 void AddCode(Address addr, CodeEntry* entry, unsigned size);
286 void MoveCode(Address from, Address to);
287 CodeEntry* FindEntry(Address addr, Address* start = NULL);
288 int GetSharedId(Address addr);
293 struct CodeEntryInfo {
294 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
295 : entry(an_entry), size(a_size) { }
300 struct CodeTreeConfig {
302 typedef CodeEntryInfo Value;
303 static const Key kNoKey;
304 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
305 static int Compare(const Key& a, const Key& b) {
306 return a < b ? -1 : (a > b ? 1 : 0);
309 typedef SplayTree<CodeTreeConfig> CodeTree;
311 class CodeTreePrinter {
313 void Call(const Address& key, const CodeEntryInfo& value);
316 void DeleteAllCoveredCode(Address start, Address end);
318 // Fake CodeEntry pointer to distinguish shared function entries.
319 static CodeEntry* const kSharedFunctionCodeEntry;
324 DISALLOW_COPY_AND_ASSIGN(CodeMap);
328 class CpuProfilesCollection {
330 explicit CpuProfilesCollection(Heap* heap);
331 ~CpuProfilesCollection();
333 bool StartProfiling(const char* title, bool record_samples);
334 CpuProfile* StopProfiling(const char* title);
335 List<CpuProfile*>* profiles() { return &finished_profiles_; }
336 const char* GetName(Name* name) {
337 return function_and_resource_names_.GetName(name);
339 const char* GetName(int args_count) {
340 return function_and_resource_names_.GetName(args_count);
342 const char* GetFunctionName(Name* name) {
343 return function_and_resource_names_.GetFunctionName(name);
345 const char* GetFunctionName(const char* name) {
346 return function_and_resource_names_.GetFunctionName(name);
348 bool IsLastProfile(const char* title);
349 void RemoveProfile(CpuProfile* profile);
351 CodeEntry* NewCodeEntry(
352 Logger::LogEventsAndTags tag, const char* name,
353 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
354 const char* resource_name = CodeEntry::kEmptyResourceName,
355 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
356 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
357 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL);
359 // Called from profile generator thread.
360 void AddPathToCurrentProfiles(base::TimeTicks timestamp,
361 const Vector<CodeEntry*>& path, int src_line);
363 // Limits the number of profiles that can be simultaneously collected.
364 static const int kMaxSimultaneousProfiles = 100;
367 StringsStorage function_and_resource_names_;
368 List<CodeEntry*> code_entries_;
369 List<CpuProfile*> finished_profiles_;
371 // Accessed by VM thread and profile generator thread.
372 List<CpuProfile*> current_profiles_;
373 base::Semaphore current_profiles_semaphore_;
375 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
379 class ProfileGenerator {
381 explicit ProfileGenerator(CpuProfilesCollection* profiles);
383 void RecordTickSample(const TickSample& sample);
385 CodeMap* code_map() { return &code_map_; }
387 static const char* const kProgramEntryName;
388 static const char* const kIdleEntryName;
389 static const char* const kGarbageCollectorEntryName;
390 // Used to represent frames for which we have no reliable way to
392 static const char* const kUnresolvedFunctionName;
395 CodeEntry* EntryForVMState(StateTag tag);
397 CpuProfilesCollection* profiles_;
399 CodeEntry* program_entry_;
400 CodeEntry* idle_entry_;
401 CodeEntry* gc_entry_;
402 CodeEntry* unresolved_entry_;
404 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
408 } } // namespace v8::internal
410 #endif // V8_PROFILE_GENERATOR_H_