Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: src/profile-generator.h

Issue 1079006: Add basic C++ implementation of CPU profiler. (Closed)
Patch Set: Comments addressed Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/platform.h ('k') | src/profile-generator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 18 matching lines...) Expand all
29 #define V8_PROFILE_GENERATOR_H_ 29 #define V8_PROFILE_GENERATOR_H_
30 30
31 #include "hashmap.h" 31 #include "hashmap.h"
32 32
33 namespace v8 { 33 namespace v8 {
34 namespace internal { 34 namespace internal {
35 35
36 36
37 class CodeEntry { 37 class CodeEntry {
38 public: 38 public:
39 virtual ~CodeEntry() { } 39 // CodeEntry doesn't own name strings, just references them.
40 INLINE(CodeEntry(Logger::LogEventsAndTags tag_,
41 const char* name_,
42 const char* resource_name_,
43 int line_number_));
40 44
41 virtual const char* name() = 0;
42 INLINE(bool is_js_function()); 45 INLINE(bool is_js_function());
43 46 INLINE(const char* name()) { return name_; }
44 protected:
45 INLINE(explicit CodeEntry(Logger::LogEventsAndTags tag))
46 : tag_(tag) { }
47 47
48 private: 48 private:
49 Logger::LogEventsAndTags tag_; 49 Logger::LogEventsAndTags tag_;
50 const char* name_;
51 const char* resource_name_;
52 int line_number_;
50 53
51 DISALLOW_COPY_AND_ASSIGN(CodeEntry); 54 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
52 }; 55 };
53 56
54 57
55 class StaticNameCodeEntry : public CodeEntry {
56 public:
57 INLINE(StaticNameCodeEntry(Logger::LogEventsAndTags tag,
58 const char* name));
59
60 INLINE(virtual const char* name()) { return name_ != NULL ? name_ : ""; }
61
62 private:
63 const char* name_;
64
65 DISALLOW_COPY_AND_ASSIGN(StaticNameCodeEntry);
66 };
67
68
69 class ManagedNameCodeEntry : public CodeEntry {
70 public:
71 INLINE(ManagedNameCodeEntry(Logger::LogEventsAndTags tag,
72 String* name,
73 const char* resource_name, int line_number));
74
75 INLINE(virtual const char* name()) { return !name_.is_empty() ? *name_ : ""; }
76
77 private:
78 SmartPointer<char> name_;
79 const char* resource_name_;
80 int line_number_;
81
82 DISALLOW_COPY_AND_ASSIGN(ManagedNameCodeEntry);
83 };
84
85
86 class ProfileNode { 58 class ProfileNode {
87 public: 59 public:
88 INLINE(explicit ProfileNode(CodeEntry* entry)); 60 INLINE(explicit ProfileNode(CodeEntry* entry));
89 61
90 ProfileNode* FindChild(CodeEntry* entry); 62 ProfileNode* FindChild(CodeEntry* entry);
91 ProfileNode* FindOrAddChild(CodeEntry* entry); 63 ProfileNode* FindOrAddChild(CodeEntry* entry);
92 INLINE(void IncrementSelfTicks()) { ++self_ticks_; } 64 INLINE(void IncrementSelfTicks()) { ++self_ticks_; }
93 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; } 65 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; }
94 66
67 INLINE(CodeEntry* entry()) { return entry_; }
95 INLINE(unsigned total_ticks()) { return total_ticks_; } 68 INLINE(unsigned total_ticks()) { return total_ticks_; }
96 INLINE(unsigned self_ticks()) { return self_ticks_; } 69 INLINE(unsigned self_ticks()) { return self_ticks_; }
97 70
98 void Print(int indent); 71 void Print(int indent);
99 72
100 private: 73 private:
101 INLINE(static bool CodeEntriesMatch(void* key1, void* key2)) { 74 INLINE(static bool CodeEntriesMatch(void* entry1, void* entry2)) {
102 return key1 == key2; 75 return entry1 == entry2;
103 } 76 }
104 77
105 INLINE(static bool CodeEntryHash(CodeEntry* entry)) { 78 INLINE(static uint32_t CodeEntryHash(CodeEntry* entry)) {
106 return static_cast<int32_t>(reinterpret_cast<intptr_t>(entry)); 79 return static_cast<int32_t>(reinterpret_cast<intptr_t>(entry));
107 } 80 }
108 81
109 CodeEntry* entry_; 82 CodeEntry* entry_;
110 unsigned total_ticks_; 83 unsigned total_ticks_;
111 unsigned self_ticks_; 84 unsigned self_ticks_;
112 // CodeEntry* -> ProfileNode* 85 // CodeEntry* -> ProfileNode*
113 HashMap children_; 86 HashMap children_;
114 87
115 friend class ProfileTree; 88 friend class ProfileTree;
(...skipping 21 matching lines...) Expand all
137 private: 110 private:
138 template <typename Callback> 111 template <typename Callback>
139 void TraverseBreadthFirstPostOrder(Callback* callback); 112 void TraverseBreadthFirstPostOrder(Callback* callback);
140 113
141 ProfileNode* root_; 114 ProfileNode* root_;
142 115
143 DISALLOW_COPY_AND_ASSIGN(ProfileTree); 116 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
144 }; 117 };
145 118
146 119
147 class CpuProfile BASE_EMBEDDED { 120 class CpuProfile {
148 public: 121 public:
149 CpuProfile() { } 122 CpuProfile() { }
150 // Add pc -> ... -> main() call path to the profile. 123 // Add pc -> ... -> main() call path to the profile.
151 void AddPath(const Vector<CodeEntry*>& path); 124 void AddPath(const Vector<CodeEntry*>& path);
152 void CalculateTotalTicks(); 125 void CalculateTotalTicks();
153 126
127 INLINE(ProfileTree* top_down()) { return &top_down_; }
128 INLINE(ProfileTree* bottom_up()) { return &bottom_up_; }
129
154 void ShortPrint(); 130 void ShortPrint();
155 void Print(); 131 void Print();
156 132
157 private: 133 private:
158 ProfileTree top_down_; 134 ProfileTree top_down_;
159 ProfileTree bottom_up_; 135 ProfileTree bottom_up_;
160 136
161 DISALLOW_COPY_AND_ASSIGN(CpuProfile); 137 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
162 }; 138 };
163 139
(...skipping 25 matching lines...) Expand all
189 } 165 }
190 }; 166 };
191 typedef SplayTree<CodeTreeConfig> CodeTree; 167 typedef SplayTree<CodeTreeConfig> CodeTree;
192 168
193 CodeTree tree_; 169 CodeTree tree_;
194 170
195 DISALLOW_COPY_AND_ASSIGN(CodeMap); 171 DISALLOW_COPY_AND_ASSIGN(CodeMap);
196 }; 172 };
197 173
198 174
175 class CpuProfilesCollection {
176 public:
177 CpuProfilesCollection();
178 ~CpuProfilesCollection();
179
180 void AddProfile(unsigned uid);
181
182 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
183 String* name, String* resource_name, int line_number);
184 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name);
185 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, int args_count);
186
187 INLINE(CpuProfile* profile()) { return profiles_.last(); }
188
189 private:
190 const char* GetName(String* name);
191 const char* GetName(int args_count);
192
193 INLINE(static bool StringsMatch(void* key1, void* key2)) {
194 return strcmp(reinterpret_cast<char*>(key1),
195 reinterpret_cast<char*>(key2)) == 0;
196 }
197
198 // String::Hash -> const char*
199 HashMap function_and_resource_names_;
200 // args_count -> char*
201 List<char*> args_count_names_;
202 List<CodeEntry*> code_entries_;
203 List<CpuProfile*> profiles_;
204
205 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
206 };
207
208
199 class ProfileGenerator { 209 class ProfileGenerator {
200 public: 210 public:
201 ProfileGenerator(); 211 explicit ProfileGenerator(CpuProfilesCollection* profiles);
202 ~ProfileGenerator();
203 212
204 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, 213 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
205 String* name, String* resource_name, int line_number); 214 String* name,
206 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name); 215 String* resource_name,
216 int line_number)) {
217 return profiles_->NewCodeEntry(tag, name, resource_name, line_number);
218 }
207 219
208 INLINE(CpuProfile* profile()) { return &profile_; } 220 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
221 const char* name)) {
222 return profiles_->NewCodeEntry(tag, name);
223 }
224
225 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
226 int args_count)) {
227 return profiles_->NewCodeEntry(tag, args_count);
228 }
229
230 void RecordTickSample(const TickSample& sample);
231
209 INLINE(CodeMap* code_map()) { return &code_map_; } 232 INLINE(CodeMap* code_map()) { return &code_map_; }
210 233
211 private: 234 private:
212 INLINE(static bool StringsMatch(void* key1, void* key2)) { 235 INLINE(CpuProfile* profile()) { return profiles_->profile(); }
213 return key1 == key2;
214 }
215 236
216 INLINE(static bool StringEntryHash(String* entry)) { 237 CpuProfilesCollection* profiles_;
217 return entry->Hash();
218 }
219
220 CpuProfile profile_;
221 CodeMap code_map_; 238 CodeMap code_map_;
222 typedef List<CodeEntry*> CodeEntryList;
223 CodeEntryList code_entries_;
224 // String::Hash -> const char*
225 HashMap resource_names_;
226 239
227 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); 240 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
228 }; 241 };
229 242
230 243
231 } } // namespace v8::internal 244 } } // namespace v8::internal
232 245
233 #endif // V8_PROFILE_GENERATOR_H_ 246 #endif // V8_PROFILE_GENERATOR_H_
OLDNEW
« no previous file with comments | « src/platform.h ('k') | src/profile-generator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698