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/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);
41 const char* deopt_reason;
46 std::vector<Frame> stack;
52 // CodeEntry doesn't own name strings, just references them.
53 inline CodeEntry(Logger::LogEventsAndTags tag, const char* name,
54 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
55 const char* resource_name = CodeEntry::kEmptyResourceName,
56 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
57 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
58 JITLineInfoTable* line_info = NULL,
59 Address instruction_start = NULL);
62 bool is_js_function() const { return is_js_function_tag(tag()); }
63 const char* name_prefix() const { return name_prefix_; }
64 bool has_name_prefix() const { return name_prefix_[0] != '\0'; }
65 const char* name() const { return name_; }
66 const char* resource_name() const { return resource_name_; }
67 int line_number() const { return line_number_; }
68 int column_number() const { return column_number_; }
69 const JITLineInfoTable* line_info() const { return line_info_; }
70 int script_id() const { return script_id_; }
71 void set_script_id(int script_id) { script_id_ = script_id; }
72 int position() const { return position_; }
73 void set_position(int position) { position_ = position; }
74 void set_bailout_reason(const char* bailout_reason) {
75 bailout_reason_ = bailout_reason;
77 const char* bailout_reason() const { return bailout_reason_; }
79 void set_deopt_info(const char* deopt_reason, SourcePosition position,
81 DCHECK(deopt_position_.IsUnknown());
82 deopt_reason_ = deopt_reason;
83 deopt_position_ = position;
84 pc_offset_ = pc_offset;
86 DeoptInfo GetDeoptInfo();
87 const char* deopt_reason() const { return deopt_reason_; }
88 SourcePosition deopt_position() const { return deopt_position_; }
89 bool has_deopt_info() const { return !deopt_position_.IsUnknown(); }
90 void clear_deopt_info() {
91 deopt_reason_ = kNoDeoptReason;
92 deopt_position_ = SourcePosition::Unknown();
95 void FillFunctionInfo(SharedFunctionInfo* shared);
97 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag);
99 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
100 void set_no_frame_ranges(List<OffsetRange>* ranges) {
101 no_frame_ranges_ = ranges;
103 void set_inlined_function_infos(
104 const std::vector<InlinedFunctionInfo>& infos) {
105 inlined_function_infos_ = infos;
107 const std::vector<InlinedFunctionInfo> inlined_function_infos() {
108 return inlined_function_infos_;
111 void SetBuiltinId(Builtins::Name id);
112 Builtins::Name builtin_id() const {
113 return BuiltinIdField::decode(bit_field_);
116 uint32_t GetHash() const;
117 bool IsSameFunctionAs(CodeEntry* entry) const;
119 int GetSourceLine(int pc_offset) const;
121 Address instruction_start() const { return instruction_start_; }
123 static const char* const kEmptyNamePrefix;
124 static const char* const kEmptyResourceName;
125 static const char* const kEmptyBailoutReason;
126 static const char* const kNoDeoptReason;
129 class TagField : public BitField<Logger::LogEventsAndTags, 0, 8> {};
130 class BuiltinIdField : public BitField<Builtins::Name, 8, 8> {};
131 Logger::LogEventsAndTags tag() const { return TagField::decode(bit_field_); }
134 const char* name_prefix_;
136 const char* resource_name_;
141 List<OffsetRange>* no_frame_ranges_;
142 const char* bailout_reason_;
143 const char* deopt_reason_;
144 SourcePosition deopt_position_;
146 JITLineInfoTable* line_info_;
147 Address instruction_start_;
149 std::vector<InlinedFunctionInfo> inlined_function_infos_;
151 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
159 inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
161 ProfileNode* FindChild(CodeEntry* entry);
162 ProfileNode* FindOrAddChild(CodeEntry* entry);
163 void IncrementSelfTicks() { ++self_ticks_; }
164 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
165 void IncrementLineTicks(int src_line);
167 CodeEntry* entry() const { return entry_; }
168 unsigned self_ticks() const { return self_ticks_; }
169 const List<ProfileNode*>* children() const { return &children_list_; }
170 unsigned id() const { return id_; }
171 unsigned function_id() const;
172 unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
173 bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
174 unsigned int length) const;
175 void CollectDeoptInfo(CodeEntry* entry);
176 const std::vector<DeoptInfo>& deopt_infos() const { return deopt_infos_; }
178 void Print(int indent);
180 static bool CodeEntriesMatch(void* entry1, void* entry2) {
181 return reinterpret_cast<CodeEntry*>(entry1)
182 ->IsSameFunctionAs(reinterpret_cast<CodeEntry*>(entry2));
186 static uint32_t CodeEntryHash(CodeEntry* entry) { return entry->GetHash(); }
188 static bool LineTickMatch(void* a, void* b) { return a == b; }
192 unsigned self_ticks_;
193 // Mapping from CodeEntry* to ProfileNode*
195 List<ProfileNode*> children_list_;
199 std::vector<DeoptInfo> deopt_infos_;
201 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
210 ProfileNode* AddPathFromEnd(
211 const Vector<CodeEntry*>& path,
212 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
213 ProfileNode* root() const { return root_; }
214 unsigned next_node_id() { return next_node_id_++; }
215 unsigned GetFunctionId(const ProfileNode* node);
222 template <typename Callback>
223 void TraverseDepthFirst(Callback* callback);
225 CodeEntry root_entry_;
226 unsigned next_node_id_;
229 unsigned next_function_id_;
230 HashMap function_ids_;
232 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
238 CpuProfile(const char* title, bool record_samples);
240 // Add pc -> ... -> main() call path to the profile.
241 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path,
243 void CalculateTotalTicksAndSamplingRate();
245 const char* title() const { return title_; }
246 const ProfileTree* top_down() const { return &top_down_; }
248 int samples_count() const { return samples_.length(); }
249 ProfileNode* sample(int index) const { return samples_.at(index); }
250 base::TimeTicks sample_timestamp(int index) const {
251 return timestamps_.at(index);
254 base::TimeTicks start_time() const { return start_time_; }
255 base::TimeTicks end_time() const { return end_time_; }
257 void UpdateTicksScale();
263 bool record_samples_;
264 base::TimeTicks start_time_;
265 base::TimeTicks end_time_;
266 List<ProfileNode*> samples_;
267 List<base::TimeTicks> timestamps_;
268 ProfileTree top_down_;
270 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
277 void AddCode(Address addr, CodeEntry* entry, unsigned size);
278 void MoveCode(Address from, Address to);
279 CodeEntry* FindEntry(Address addr, Address* start = NULL);
280 int GetSharedId(Address addr);
285 struct CodeEntryInfo {
286 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
287 : entry(an_entry), size(a_size) { }
292 struct CodeTreeConfig {
294 typedef CodeEntryInfo Value;
295 static const Key kNoKey;
296 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
297 static int Compare(const Key& a, const Key& b) {
298 return a < b ? -1 : (a > b ? 1 : 0);
301 typedef SplayTree<CodeTreeConfig> CodeTree;
303 class CodeTreePrinter {
305 void Call(const Address& key, const CodeEntryInfo& value);
308 void DeleteAllCoveredCode(Address start, Address end);
312 DISALLOW_COPY_AND_ASSIGN(CodeMap);
316 class CpuProfilesCollection {
318 explicit CpuProfilesCollection(Heap* heap);
319 ~CpuProfilesCollection();
321 bool StartProfiling(const char* title, bool record_samples);
322 CpuProfile* StopProfiling(const char* title);
323 List<CpuProfile*>* profiles() { return &finished_profiles_; }
324 const char* GetName(Name* name) {
325 return function_and_resource_names_.GetName(name);
327 const char* GetName(int args_count) {
328 return function_and_resource_names_.GetName(args_count);
330 const char* GetFunctionName(Name* name) {
331 return function_and_resource_names_.GetFunctionName(name);
333 const char* GetFunctionName(const char* name) {
334 return function_and_resource_names_.GetFunctionName(name);
336 bool IsLastProfile(const char* title);
337 void RemoveProfile(CpuProfile* profile);
339 CodeEntry* NewCodeEntry(
340 Logger::LogEventsAndTags tag, const char* name,
341 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
342 const char* resource_name = CodeEntry::kEmptyResourceName,
343 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
344 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
345 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL);
347 // Called from profile generator thread.
348 void AddPathToCurrentProfiles(base::TimeTicks timestamp,
349 const Vector<CodeEntry*>& path, int src_line);
351 // Limits the number of profiles that can be simultaneously collected.
352 static const int kMaxSimultaneousProfiles = 100;
355 StringsStorage function_and_resource_names_;
356 List<CodeEntry*> code_entries_;
357 List<CpuProfile*> finished_profiles_;
359 // Accessed by VM thread and profile generator thread.
360 List<CpuProfile*> current_profiles_;
361 base::Semaphore current_profiles_semaphore_;
363 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
367 class ProfileGenerator {
369 explicit ProfileGenerator(CpuProfilesCollection* profiles);
371 void RecordTickSample(const TickSample& sample);
373 CodeMap* code_map() { return &code_map_; }
375 static const char* const kProgramEntryName;
376 static const char* const kIdleEntryName;
377 static const char* const kGarbageCollectorEntryName;
378 // Used to represent frames for which we have no reliable way to
380 static const char* const kUnresolvedFunctionName;
383 CodeEntry* EntryForVMState(StateTag tag);
385 CpuProfilesCollection* profiles_;
387 CodeEntry* program_entry_;
388 CodeEntry* idle_entry_;
389 CodeEntry* gc_entry_;
390 CodeEntry* unresolved_entry_;
392 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
396 } } // namespace v8::internal
398 #endif // V8_PROFILE_GENERATOR_H_