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_
8 #include "include/v8-profiler.h"
9 #include "src/allocation.h"
10 #include "src/hashmap.h"
17 // Provides a storage of strings allocated in C++ heap, to hold them
18 // forever, even if they disappear from JS heap or external storage.
19 class StringsStorage {
21 explicit StringsStorage(Heap* heap);
24 const char* GetCopy(const char* src);
25 const char* GetFormatted(const char* format, ...);
26 const char* GetVFormatted(const char* format, va_list args);
27 const char* GetName(Name* name);
28 const char* GetName(int index);
29 const char* GetFunctionName(Name* name);
30 const char* GetFunctionName(const char* name);
31 size_t GetUsedMemorySize() const;
34 static const int kMaxNameSize = 1024;
36 static bool StringsMatch(void* key1, void* key2);
37 const char* AddOrDisposeString(char* str, int len);
38 HashMap::Entry* GetEntry(const char* str, int len);
43 DISALLOW_COPY_AND_ASSIGN(StringsStorage);
49 // CodeEntry doesn't own name strings, just references them.
50 inline CodeEntry(Logger::LogEventsAndTags tag,
52 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
53 const char* resource_name = CodeEntry::kEmptyResourceName,
54 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
55 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo);
58 bool is_js_function() const { return is_js_function_tag(tag_); }
59 const char* name_prefix() const { return name_prefix_; }
60 bool has_name_prefix() const { return name_prefix_[0] != '\0'; }
61 const char* name() const { return name_; }
62 const char* resource_name() const { return resource_name_; }
63 int line_number() const { return line_number_; }
64 int column_number() const { return column_number_; }
65 void set_shared_id(int shared_id) { shared_id_ = shared_id; }
66 int script_id() const { return script_id_; }
67 void set_script_id(int script_id) { script_id_ = script_id; }
68 void set_bailout_reason(const char* bailout_reason) {
69 bailout_reason_ = bailout_reason;
71 const char* bailout_reason() const { return bailout_reason_; }
73 static inline bool is_js_function_tag(Logger::LogEventsAndTags tag);
75 List<OffsetRange>* no_frame_ranges() const { return no_frame_ranges_; }
76 void set_no_frame_ranges(List<OffsetRange>* ranges) {
77 no_frame_ranges_ = ranges;
80 void SetBuiltinId(Builtins::Name id);
81 Builtins::Name builtin_id() const { return builtin_id_; }
83 uint32_t GetCallUid() const;
84 bool IsSameAs(CodeEntry* entry) const;
86 static const char* const kEmptyNamePrefix;
87 static const char* const kEmptyResourceName;
88 static const char* const kEmptyBailoutReason;
91 Logger::LogEventsAndTags tag_ : 8;
92 Builtins::Name builtin_id_ : 8;
93 const char* name_prefix_;
95 const char* resource_name_;
100 List<OffsetRange>* no_frame_ranges_;
101 const char* bailout_reason_;
103 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
111 inline ProfileNode(ProfileTree* tree, CodeEntry* entry);
113 ProfileNode* FindChild(CodeEntry* entry);
114 ProfileNode* FindOrAddChild(CodeEntry* entry);
115 void IncrementSelfTicks() { ++self_ticks_; }
116 void IncreaseSelfTicks(unsigned amount) { self_ticks_ += amount; }
118 CodeEntry* entry() const { return entry_; }
119 unsigned self_ticks() const { return self_ticks_; }
120 const List<ProfileNode*>* children() const { return &children_list_; }
121 unsigned id() const { return id_; }
123 void Print(int indent);
126 static bool CodeEntriesMatch(void* entry1, void* entry2) {
127 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs(
128 reinterpret_cast<CodeEntry*>(entry2));
131 static uint32_t CodeEntryHash(CodeEntry* entry) {
132 return entry->GetCallUid();
137 unsigned self_ticks_;
138 // Mapping from CodeEntry* to ProfileNode*
140 List<ProfileNode*> children_list_;
143 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
152 ProfileNode* AddPathFromEnd(const Vector<CodeEntry*>& path);
153 void AddPathFromStart(const Vector<CodeEntry*>& path);
154 ProfileNode* root() const { return root_; }
155 unsigned next_node_id() { return next_node_id_++; }
162 template <typename Callback>
163 void TraverseDepthFirst(Callback* callback);
165 CodeEntry root_entry_;
166 unsigned next_node_id_;
169 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
175 CpuProfile(const char* title, bool record_samples);
177 // Add pc -> ... -> main() call path to the profile.
178 void AddPath(base::TimeTicks timestamp, const Vector<CodeEntry*>& path);
179 void CalculateTotalTicksAndSamplingRate();
181 const char* title() const { return title_; }
182 const ProfileTree* top_down() const { return &top_down_; }
184 int samples_count() const { return samples_.length(); }
185 ProfileNode* sample(int index) const { return samples_.at(index); }
186 base::TimeTicks sample_timestamp(int index) const {
187 return timestamps_.at(index);
190 base::TimeTicks start_time() const { return start_time_; }
191 base::TimeTicks end_time() const { return end_time_; }
193 void UpdateTicksScale();
199 bool record_samples_;
200 base::TimeTicks start_time_;
201 base::TimeTicks end_time_;
202 List<ProfileNode*> samples_;
203 List<base::TimeTicks> timestamps_;
204 ProfileTree top_down_;
206 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
212 CodeMap() : next_shared_id_(1) { }
213 void AddCode(Address addr, CodeEntry* entry, unsigned size);
214 void MoveCode(Address from, Address to);
215 CodeEntry* FindEntry(Address addr, Address* start = NULL);
216 int GetSharedId(Address addr);
221 struct CodeEntryInfo {
222 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
223 : entry(an_entry), size(a_size) { }
228 struct CodeTreeConfig {
230 typedef CodeEntryInfo Value;
231 static const Key kNoKey;
232 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
233 static int Compare(const Key& a, const Key& b) {
234 return a < b ? -1 : (a > b ? 1 : 0);
237 typedef SplayTree<CodeTreeConfig> CodeTree;
239 class CodeTreePrinter {
241 void Call(const Address& key, const CodeEntryInfo& value);
244 void DeleteAllCoveredCode(Address start, Address end);
246 // Fake CodeEntry pointer to distinguish shared function entries.
247 static CodeEntry* const kSharedFunctionCodeEntry;
252 DISALLOW_COPY_AND_ASSIGN(CodeMap);
256 class CpuProfilesCollection {
258 explicit CpuProfilesCollection(Heap* heap);
259 ~CpuProfilesCollection();
261 bool StartProfiling(const char* title, bool record_samples);
262 CpuProfile* StopProfiling(const char* title);
263 List<CpuProfile*>* profiles() { return &finished_profiles_; }
264 const char* GetName(Name* name) {
265 return function_and_resource_names_.GetName(name);
267 const char* GetName(int args_count) {
268 return function_and_resource_names_.GetName(args_count);
270 const char* GetFunctionName(Name* name) {
271 return function_and_resource_names_.GetFunctionName(name);
273 const char* GetFunctionName(const char* name) {
274 return function_and_resource_names_.GetFunctionName(name);
276 bool IsLastProfile(const char* title);
277 void RemoveProfile(CpuProfile* profile);
279 CodeEntry* NewCodeEntry(
280 Logger::LogEventsAndTags tag,
282 const char* name_prefix = CodeEntry::kEmptyNamePrefix,
283 const char* resource_name = CodeEntry::kEmptyResourceName,
284 int line_number = v8::CpuProfileNode::kNoLineNumberInfo,
285 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo);
287 // Called from profile generator thread.
288 void AddPathToCurrentProfiles(
289 base::TimeTicks timestamp, const Vector<CodeEntry*>& path);
291 // Limits the number of profiles that can be simultaneously collected.
292 static const int kMaxSimultaneousProfiles = 100;
295 StringsStorage function_and_resource_names_;
296 List<CodeEntry*> code_entries_;
297 List<CpuProfile*> finished_profiles_;
299 // Accessed by VM thread and profile generator thread.
300 List<CpuProfile*> current_profiles_;
301 base::Semaphore current_profiles_semaphore_;
303 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
307 class ProfileGenerator {
309 explicit ProfileGenerator(CpuProfilesCollection* profiles);
311 void RecordTickSample(const TickSample& sample);
313 CodeMap* code_map() { return &code_map_; }
315 static const char* const kProgramEntryName;
316 static const char* const kIdleEntryName;
317 static const char* const kGarbageCollectorEntryName;
318 // Used to represent frames for which we have no reliable way to
320 static const char* const kUnresolvedFunctionName;
323 CodeEntry* EntryForVMState(StateTag tag);
325 CpuProfilesCollection* profiles_;
327 CodeEntry* program_entry_;
328 CodeEntry* idle_entry_;
329 CodeEntry* gc_entry_;
330 CodeEntry* unresolved_entry_;
332 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
336 } } // namespace v8::internal
338 #endif // V8_PROFILE_GENERATOR_H_