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_PROFILER_PROFILE_GENERATOR_H_
6 #define V8_PROFILER_PROFILE_GENERATOR_H_
9 #include "include/v8-profiler.h"
10 #include "src/allocation.h"
11 #include "src/compiler.h"
12 #include "src/hashmap.h"
13 #include "src/strings-storage.h"
20 // Provides a mapping from the offsets within generated code to
22 class JITLineInfoTable : public Malloced {
27 void SetPosition(int pc_offset, int line);
28 int GetSourceLineNumber(int pc_offset) const;
30 bool empty() const { return pc_offset_map_.empty(); }
33 // pc_offset -> source line
34 typedef std::map<int, int> PcOffsetMap;
35 PcOffsetMap pc_offset_map_;
36 DISALLOW_COPY_AND_ASSIGN(JITLineInfoTable);
42 // CodeEntry doesn't own name strings, just references them.
43 inline CodeEntry(Logger::LogEventsAndTags tag, const char* name,
44 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
45 const char* resource_name = CodeEntry::kEmptyResourceName,
46 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
47 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
48 JITLineInfoTable* line_info = NULL,
49 Address instruction_start = NULL);
52 const char* name_prefix() const { return name_prefix_; }
53 bool has_name_prefix() const { return name_prefix_[0] != '\0'; }
54 const char* name() const { return name_; }
55 const char* resource_name() const { return resource_name_; }
56 int line_number() const { return line_number_; }
57 int column_number() const { return column_number_; }
58 const JITLineInfoTable* line_info() const { return line_info_; }
59 int script_id() const { return script_id_; }
60 void set_script_id(int script_id) { script_id_ = script_id; }
61 int position() const { return position_; }
62 void set_position(int position) { position_ = position; }
63 void set_bailout_reason(const char* bailout_reason) {
64 bailout_reason_ = bailout_reason;
66 const char* bailout_reason() const { return bailout_reason_; }
68 void set_deopt_info(const char* deopt_reason, SourcePosition position,
70 DCHECK(deopt_position_.IsUnknown());
71 deopt_reason_ = deopt_reason;
72 deopt_position_ = position;
73 pc_offset_ = pc_offset;
75 CpuProfileDeoptInfo GetDeoptInfo();
76 const char* deopt_reason() const { return deopt_reason_; }
77 SourcePosition deopt_position() const { return deopt_position_; }
78 bool has_deopt_info() const { return !deopt_position_.IsUnknown(); }
79 void clear_deopt_info() {
80 deopt_reason_ = kNoDeoptReason;
81 deopt_position_ = SourcePosition::Unknown();
84 void FillFunctionInfo(SharedFunctionInfo* shared);
86 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
87 void set_no_frame_ranges(List<OffsetRange>* ranges) {
88 no_frame_ranges_ = ranges;
90 void set_inlined_function_infos(
91 const std::vector<InlinedFunctionInfo>& infos) {
92 inlined_function_infos_ = infos;
94 const std::vector<InlinedFunctionInfo> inlined_function_infos() {
95 return inlined_function_infos_;
98 void SetBuiltinId(Builtins::Name id);
99 Builtins::Name builtin_id() const {
100 return BuiltinIdField::decode(bit_field_);
103 uint32_t GetHash() const;
104 bool IsSameFunctionAs(CodeEntry* entry) const;
106 int GetSourceLine(int pc_offset) const;
108 Address instruction_start() const { return instruction_start_; }
110 static const char* const kEmptyNamePrefix;
111 static const char* const kEmptyResourceName;
112 static const char* const kEmptyBailoutReason;
113 static const char* const kNoDeoptReason;
116 class TagField : public BitField<Logger::LogEventsAndTags, 0, 8> {};
117 class BuiltinIdField : public BitField<Builtins::Name, 8, 8> {};
118 Logger::LogEventsAndTags tag() const { return TagField::decode(bit_field_); }
121 const char* name_prefix_;
123 const char* resource_name_;
128 List<OffsetRange>* no_frame_ranges_;
129 const char* bailout_reason_;
130 const char* deopt_reason_;
131 SourcePosition deopt_position_;
133 JITLineInfoTable* line_info_;
134 Address instruction_start_;
136 std::vector<InlinedFunctionInfo> inlined_function_infos_;
138 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
146 inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
148 ProfileNode* FindChild(CodeEntry* entry);
149 ProfileNode* FindOrAddChild(CodeEntry* entry);
150 void IncrementSelfTicks() { ++self_ticks_; }
151 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
152 void IncrementLineTicks(int src_line);
154 CodeEntry* entry() const { return entry_; }
155 unsigned self_ticks() const { return self_ticks_; }
156 const List<ProfileNode*>* children() const { return &children_list_; }
157 unsigned id() const { return id_; }
158 unsigned function_id() const;
159 unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
160 bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
161 unsigned int length) const;
162 void CollectDeoptInfo(CodeEntry* entry);
163 const std::vector<CpuProfileDeoptInfo>& deopt_infos() const {
167 void Print(int indent);
169 static bool CodeEntriesMatch(void* entry1, void* entry2) {
170 return reinterpret_cast<CodeEntry*>(entry1)
171 ->IsSameFunctionAs(reinterpret_cast<CodeEntry*>(entry2));
175 static uint32_t CodeEntryHash(CodeEntry* entry) { return entry->GetHash(); }
177 static bool LineTickMatch(void* a, void* b) { return a == b; }
181 unsigned self_ticks_;
182 // Mapping from CodeEntry* to ProfileNode*
184 List<ProfileNode*> children_list_;
188 std::vector<CpuProfileDeoptInfo> deopt_infos_;
190 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
199 ProfileNode* AddPathFromEnd(
200 const Vector<CodeEntry*>& path,
201 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
202 ProfileNode* root() const { return root_; }
203 unsigned next_node_id() { return next_node_id_++; }
204 unsigned GetFunctionId(const ProfileNode* node);
211 template <typename Callback>
212 void TraverseDepthFirst(Callback* callback);
214 CodeEntry root_entry_;
215 unsigned next_node_id_;
218 unsigned next_function_id_;
219 HashMap function_ids_;
221 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
227 CpuProfile(const char* title, bool record_samples);
229 // Add pc -> ... -> main() call path to the profile.
230 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path,
232 void CalculateTotalTicksAndSamplingRate();
234 const char* title() const { return title_; }
235 const ProfileTree* top_down() const { return &top_down_; }
237 int samples_count() const { return samples_.length(); }
238 ProfileNode* sample(int index) const { return samples_.at(index); }
239 base::TimeTicks sample_timestamp(int index) const {
240 return timestamps_.at(index);
243 base::TimeTicks start_time() const { return start_time_; }
244 base::TimeTicks end_time() const { return end_time_; }
246 void UpdateTicksScale();
252 bool record_samples_;
253 base::TimeTicks start_time_;
254 base::TimeTicks end_time_;
255 List<ProfileNode*> samples_;
256 List<base::TimeTicks> timestamps_;
257 ProfileTree top_down_;
259 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
267 void AddCode(Address addr, CodeEntry* entry, unsigned size);
268 void MoveCode(Address from, Address to);
269 CodeEntry* FindEntry(Address addr);
270 int GetSharedId(Address addr);
275 struct CodeEntryInfo {
276 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
277 : entry(an_entry), size(a_size) { }
282 struct CodeTreeConfig {
284 typedef CodeEntryInfo Value;
285 static const Key kNoKey;
286 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
287 static int Compare(const Key& a, const Key& b) {
288 return a < b ? -1 : (a > b ? 1 : 0);
291 typedef SplayTree<CodeTreeConfig> CodeTree;
293 class CodeTreePrinter {
295 void Call(const Address& key, const CodeEntryInfo& value);
298 void DeleteAllCoveredCode(Address start, Address end);
302 DISALLOW_COPY_AND_ASSIGN(CodeMap);
306 class CpuProfilesCollection {
308 explicit CpuProfilesCollection(Heap* heap);
309 ~CpuProfilesCollection();
311 bool StartProfiling(const char* title, bool record_samples);
312 CpuProfile* StopProfiling(const char* title);
313 List<CpuProfile*>* profiles() { return &finished_profiles_; }
314 const char* GetName(Name* name) {
315 return function_and_resource_names_.GetName(name);
317 const char* GetName(int args_count) {
318 return function_and_resource_names_.GetName(args_count);
320 const char* GetFunctionName(Name* name) {
321 return function_and_resource_names_.GetFunctionName(name);
323 const char* GetFunctionName(const char* name) {
324 return function_and_resource_names_.GetFunctionName(name);
326 bool IsLastProfile(const char* title);
327 void RemoveProfile(CpuProfile* profile);
329 CodeEntry* NewCodeEntry(
330 Logger::LogEventsAndTags tag, const char* name,
331 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
332 const char* resource_name = CodeEntry::kEmptyResourceName,
333 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
334 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
335 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL);
337 // Called from profile generator thread.
338 void AddPathToCurrentProfiles(base::TimeTicks timestamp,
339 const Vector<CodeEntry*>& path, int src_line);
341 // Limits the number of profiles that can be simultaneously collected.
342 static const int kMaxSimultaneousProfiles = 100;
345 StringsStorage function_and_resource_names_;
346 List<CodeEntry*> code_entries_;
347 List<CpuProfile*> finished_profiles_;
349 // Accessed by VM thread and profile generator thread.
350 List<CpuProfile*> current_profiles_;
351 base::Semaphore current_profiles_semaphore_;
353 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
357 class ProfileGenerator {
359 explicit ProfileGenerator(CpuProfilesCollection* profiles);
361 void RecordTickSample(const TickSample& sample);
363 CodeMap* code_map() { return &code_map_; }
365 static const char* const kProgramEntryName;
366 static const char* const kIdleEntryName;
367 static const char* const kGarbageCollectorEntryName;
368 // Used to represent frames for which we have no reliable way to
370 static const char* const kUnresolvedFunctionName;
373 CodeEntry* EntryForVMState(StateTag tag);
375 CpuProfilesCollection* profiles_;
377 CodeEntry* program_entry_;
378 CodeEntry* idle_entry_;
379 CodeEntry* gc_entry_;
380 CodeEntry* unresolved_entry_;
382 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
386 } // namespace internal
389 #endif // V8_PROFILER_PROFILE_GENERATOR_H_