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 {
53 ~JITLineInfoTable() {}
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 pc_offset_map_.insert(std::make_pair(pc_offset, line));
61 int GetSourceLineNumber(int pc_offset) const {
62 PcOffsetMap::const_iterator it = pc_offset_map_.lower_bound(pc_offset);
63 if (it == pc_offset_map_.end()) {
64 return v8::CpuProfileNode::kNoLineNumberInfo;
69 bool Empty() const { return pc_offset_map_.empty(); }
72 // pc_offset -> source line
73 typedef std::map<int, int> PcOffsetMap;
74 PcOffsetMap pc_offset_map_;
75 DISALLOW_COPY_AND_ASSIGN(JITLineInfoTable);
80 // CodeEntry doesn't own name strings, just references them.
81 inline CodeEntry(Logger::LogEventsAndTags tag,
83 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
84 const char* resource_name = CodeEntry::kEmptyResourceName,
85 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
86 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
87 JITLineInfoTable* line_info = NULL);
90 bool is_js_function() const { return is_js_function_tag(tag_); }
91 const char* name_prefix() const { return name_prefix_; }
92 bool has_name_prefix() const { return name_prefix_[0] != '\0'; }
93 const char* name() const { return name_; }
94 const char* resource_name() const { return resource_name_; }
95 int line_number() const { return line_number_; }
96 int column_number() const { return column_number_; }
97 const JITLineInfoTable* line_info() const { return line_info_; }
98 void set_shared_id(int shared_id) { shared_id_ = shared_id; }
99 int script_id() const { return script_id_; }
100 void set_script_id(int script_id) { script_id_ = script_id; }
101 void set_bailout_reason(const char* bailout_reason) {
102 bailout_reason_ = bailout_reason;
104 const char* bailout_reason() const { return bailout_reason_; }
106 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag);
108 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
109 void set_no_frame_ranges(List<OffsetRange>* ranges) {
110 no_frame_ranges_ = ranges;
113 void SetBuiltinId(Builtins::Name id);
114 Builtins::Name builtin_id() const { return builtin_id_; }
116 uint32_t GetCallUid() const;
117 bool IsSameAs(CodeEntry* entry) const;
119 int GetSourceLine(int pc_offset) const;
121 static const char* const kEmptyNamePrefix;
122 static const char* const kEmptyResourceName;
123 static const char* const kEmptyBailoutReason;
126 Logger::LogEventsAndTags tag_ : 8;
127 Builtins::Name builtin_id_ : 8;
128 const char* name_prefix_;
130 const char* resource_name_;
135 List<OffsetRange>* no_frame_ranges_;
136 const char* bailout_reason_;
137 JITLineInfoTable* line_info_;
139 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
147 inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
149 ProfileNode* FindChild(CodeEntry* entry);
150 ProfileNode* FindOrAddChild(CodeEntry* entry);
151 void IncrementSelfTicks() { ++self_ticks_; }
152 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
153 void IncrementLineTicks(int src_line);
155 CodeEntry* entry() const { return entry_; }
156 unsigned self_ticks() const { return self_ticks_; }
157 const List<ProfileNode*>* children() const { return &children_list_; }
158 unsigned id() const { return id_; }
159 unsigned int GetHitLineCount() const { return line_ticks_.occupancy(); }
160 bool GetLineTicks(v8::CpuProfileNode::LineTick* entries,
161 unsigned int length) const;
163 void Print(int indent);
166 static bool CodeEntriesMatch(void* entry1, void* entry2) {
167 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs(
168 reinterpret_cast<CodeEntry*>(entry2));
171 static uint32_t CodeEntryHash(CodeEntry* entry) {
172 return entry->GetCallUid();
177 unsigned self_ticks_;
178 // Mapping from CodeEntry* to ProfileNode*
180 List<ProfileNode*> children_list_;
184 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
193 ProfileNode* AddPathFromEnd(
194 const Vector<CodeEntry*>& path,
195 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
196 void AddPathFromStart(const Vector<CodeEntry*>& path,
197 int src_line = v8::CpuProfileNode::kNoLineNumberInfo);
198 ProfileNode* root() const { return root_; }
199 unsigned next_node_id() { return next_node_id_++; }
206 template <typename Callback>
207 void TraverseDepthFirst(Callback* callback);
209 CodeEntry root_entry_;
210 unsigned next_node_id_;
213 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
219 CpuProfile(const char* title, bool record_samples);
221 // Add pc -> ... -> main() call path to the profile.
222 void AddPath(base::TimeTicks timestamp,
223 const Vector<CodeEntry*>& path,
225 void CalculateTotalTicksAndSamplingRate();
227 const char* title() const { return title_; }
228 const ProfileTree* top_down() const { return &top_down_; }
230 int samples_count() const { return samples_.length(); }
231 ProfileNode* sample(int index) const { return samples_.at(index); }
232 base::TimeTicks sample_timestamp(int index) const {
233 return timestamps_.at(index);
236 base::TimeTicks start_time() const { return start_time_; }
237 base::TimeTicks end_time() const { return end_time_; }
239 void UpdateTicksScale();
245 bool record_samples_;
246 base::TimeTicks start_time_;
247 base::TimeTicks end_time_;
248 List<ProfileNode*> samples_;
249 List<base::TimeTicks> timestamps_;
250 ProfileTree top_down_;
252 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
258 CodeMap() : next_shared_id_(1) { }
259 void AddCode(Address addr, CodeEntry* entry, unsigned size);
260 void MoveCode(Address from, Address to);
261 CodeEntry* FindEntry(Address addr, Address* start = NULL);
262 int GetSharedId(Address addr);
267 struct CodeEntryInfo {
268 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
269 : entry(an_entry), size(a_size) { }
274 struct CodeTreeConfig {
276 typedef CodeEntryInfo Value;
277 static const Key kNoKey;
278 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
279 static int Compare(const Key& a, const Key& b) {
280 return a < b ? -1 : (a > b ? 1 : 0);
283 typedef SplayTree<CodeTreeConfig> CodeTree;
285 class CodeTreePrinter {
287 void Call(const Address& key, const CodeEntryInfo& value);
290 void DeleteAllCoveredCode(Address start, Address end);
292 // Fake CodeEntry pointer to distinguish shared function entries.
293 static CodeEntry* const kSharedFunctionCodeEntry;
298 DISALLOW_COPY_AND_ASSIGN(CodeMap);
302 class CpuProfilesCollection {
304 explicit CpuProfilesCollection(Heap* heap);
305 ~CpuProfilesCollection();
307 bool StartProfiling(const char* title, bool record_samples);
308 CpuProfile* StopProfiling(const char* title);
309 List<CpuProfile*>* profiles() { return &finished_profiles_; }
310 const char* GetName(Name* name) {
311 return function_and_resource_names_.GetName(name);
313 const char* GetName(int args_count) {
314 return function_and_resource_names_.GetName(args_count);
316 const char* GetFunctionName(Name* name) {
317 return function_and_resource_names_.GetFunctionName(name);
319 const char* GetFunctionName(const char* name) {
320 return function_and_resource_names_.GetFunctionName(name);
322 bool IsLastProfile(const char* title);
323 void RemoveProfile(CpuProfile* profile);
325 CodeEntry* NewCodeEntry(
326 Logger::LogEventsAndTags tag,
328 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
329 const char* resource_name = CodeEntry::kEmptyResourceName,
330 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
331 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo,
332 JITLineInfoTable* line_info = NULL);
334 // Called from profile generator thread.
335 void AddPathToCurrentProfiles(base::TimeTicks timestamp,
336 const Vector<CodeEntry*>& path,
339 // Limits the number of profiles that can be simultaneously collected.
340 static const int kMaxSimultaneousProfiles = 100;
343 StringsStorage function_and_resource_names_;
344 List<CodeEntry*> code_entries_;
345 List<CpuProfile*> finished_profiles_;
347 // Accessed by VM thread and profile generator thread.
348 List<CpuProfile*> current_profiles_;
349 base::Semaphore current_profiles_semaphore_;
351 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
355 class ProfileGenerator {
357 explicit ProfileGenerator(CpuProfilesCollection* profiles);
359 void RecordTickSample(const TickSample& sample);
361 CodeMap* code_map() { return &code_map_; }
363 static const char* const kProgramEntryName;
364 static const char* const kIdleEntryName;
365 static const char* const kGarbageCollectorEntryName;
366 // Used to represent frames for which we have no reliable way to
368 static const char* const kUnresolvedFunctionName;
371 CodeEntry* EntryForVMState(StateTag tag);
373 CpuProfilesCollection* profiles_;
375 CodeEntry* program_entry_;
376 CodeEntry* idle_entry_;
377 CodeEntry* gc_entry_;
378 CodeEntry* unresolved_entry_;
380 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
384 } } // namespace v8::internal
386 #endif // V8_PROFILE_GENERATOR_H_