OLD | NEW |
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 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_PROFILE_GENERATOR_H_ | 28 #ifndef V8_PROFILE_GENERATOR_H_ |
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 #ifdef ENABLE_CPP_PROFILES_PROCESSOR |
36 | 37 |
37 class CodeEntry { | 38 class CodeEntry { |
38 public: | 39 public: |
39 // CodeEntry doesn't own name strings, just references them. | 40 // CodeEntry doesn't own name strings, just references them. |
40 INLINE(CodeEntry(Logger::LogEventsAndTags tag_, | 41 INLINE(CodeEntry(Logger::LogEventsAndTags tag_, |
41 const char* name_, | 42 const char* name_, |
42 const char* resource_name_, | 43 const char* resource_name_, |
43 int line_number_)); | 44 int line_number_)); |
44 | 45 |
45 INLINE(bool is_js_function()); | 46 INLINE(bool is_js_function()); |
46 INLINE(const char* name()) { return name_; } | 47 INLINE(const char* name()) { return name_; } |
| 48 INLINE(const char* resource_name()) { return name_; } |
| 49 INLINE(int line_number()) { return line_number_; } |
47 | 50 |
48 private: | 51 private: |
49 Logger::LogEventsAndTags tag_; | 52 Logger::LogEventsAndTags tag_; |
50 const char* name_; | 53 const char* name_; |
51 const char* resource_name_; | 54 const char* resource_name_; |
52 int line_number_; | 55 int line_number_; |
53 | 56 |
54 DISALLOW_COPY_AND_ASSIGN(CodeEntry); | 57 DISALLOW_COPY_AND_ASSIGN(CodeEntry); |
55 }; | 58 }; |
56 | 59 |
57 | 60 |
58 class ProfileNode { | 61 class ProfileNode { |
59 public: | 62 public: |
60 INLINE(explicit ProfileNode(CodeEntry* entry)); | 63 INLINE(explicit ProfileNode(CodeEntry* entry)); |
61 | 64 |
62 ProfileNode* FindChild(CodeEntry* entry); | 65 ProfileNode* FindChild(CodeEntry* entry); |
63 ProfileNode* FindOrAddChild(CodeEntry* entry); | 66 ProfileNode* FindOrAddChild(CodeEntry* entry); |
64 INLINE(void IncrementSelfTicks()) { ++self_ticks_; } | 67 INLINE(void IncrementSelfTicks()) { ++self_ticks_; } |
65 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; } | 68 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; } |
66 | 69 |
67 INLINE(CodeEntry* entry() const) { return entry_; } | 70 INLINE(CodeEntry* entry() const) { return entry_; } |
68 INLINE(unsigned total_ticks() const) { return total_ticks_; } | 71 INLINE(unsigned total_ticks() const) { return total_ticks_; } |
69 INLINE(unsigned self_ticks() const) { return self_ticks_; } | 72 INLINE(unsigned self_ticks() const) { return self_ticks_; } |
70 void GetChildren(List<ProfileNode*>* children); | 73 INLINE(const List<ProfileNode*>* children() const) { return &children_list_; } |
71 | 74 |
72 void Print(int indent); | 75 void Print(int indent); |
73 | 76 |
74 private: | 77 private: |
75 INLINE(static bool CodeEntriesMatch(void* entry1, void* entry2)) { | 78 INLINE(static bool CodeEntriesMatch(void* entry1, void* entry2)) { |
76 return entry1 == entry2; | 79 return entry1 == entry2; |
77 } | 80 } |
78 | 81 |
79 INLINE(static uint32_t CodeEntryHash(CodeEntry* entry)) { | 82 INLINE(static uint32_t CodeEntryHash(CodeEntry* entry)) { |
80 return static_cast<int32_t>(reinterpret_cast<intptr_t>(entry)); | 83 return static_cast<int32_t>(reinterpret_cast<intptr_t>(entry)); |
81 } | 84 } |
82 | 85 |
83 CodeEntry* entry_; | 86 CodeEntry* entry_; |
84 unsigned total_ticks_; | 87 unsigned total_ticks_; |
85 unsigned self_ticks_; | 88 unsigned self_ticks_; |
86 // CodeEntry* -> ProfileNode* | 89 // CodeEntry* -> ProfileNode* |
87 HashMap children_; | 90 HashMap children_; |
88 | 91 List<ProfileNode*> children_list_; |
89 friend class ProfileTree; | |
90 | 92 |
91 DISALLOW_COPY_AND_ASSIGN(ProfileNode); | 93 DISALLOW_COPY_AND_ASSIGN(ProfileNode); |
92 }; | 94 }; |
93 | 95 |
94 | 96 |
95 class ProfileTree BASE_EMBEDDED { | 97 class ProfileTree { |
96 public: | 98 public: |
97 ProfileTree() : root_(new ProfileNode(NULL)) { } | 99 ProfileTree() : root_(new ProfileNode(NULL)) { } |
98 ~ProfileTree(); | 100 ~ProfileTree(); |
99 | 101 |
100 void AddPathFromEnd(const Vector<CodeEntry*>& path); | 102 void AddPathFromEnd(const Vector<CodeEntry*>& path); |
101 void AddPathFromStart(const Vector<CodeEntry*>& path); | 103 void AddPathFromStart(const Vector<CodeEntry*>& path); |
102 void CalculateTotalTicks(); | 104 void CalculateTotalTicks(); |
103 | 105 |
104 ProfileNode* root() { return root_; } | 106 ProfileNode* root() const { return root_; } |
105 | 107 |
106 void ShortPrint(); | 108 void ShortPrint(); |
107 void Print() { | 109 void Print() { |
108 root_->Print(0); | 110 root_->Print(0); |
109 } | 111 } |
110 | 112 |
111 private: | 113 private: |
112 template <typename Callback> | 114 template <typename Callback> |
113 void TraverseBreadthFirstPostOrder(Callback* callback); | 115 void TraverseBreadthFirstPostOrder(Callback* callback); |
114 | 116 |
115 ProfileNode* root_; | 117 ProfileNode* root_; |
116 | 118 |
117 DISALLOW_COPY_AND_ASSIGN(ProfileTree); | 119 DISALLOW_COPY_AND_ASSIGN(ProfileTree); |
118 }; | 120 }; |
119 | 121 |
120 | 122 |
121 class CpuProfile { | 123 class CpuProfile { |
122 public: | 124 public: |
123 CpuProfile() { } | 125 CpuProfile(const char* title, unsigned uid) |
| 126 : title_(title), uid_(uid) { } |
| 127 |
124 // Add pc -> ... -> main() call path to the profile. | 128 // Add pc -> ... -> main() call path to the profile. |
125 void AddPath(const Vector<CodeEntry*>& path); | 129 void AddPath(const Vector<CodeEntry*>& path); |
126 void CalculateTotalTicks(); | 130 void CalculateTotalTicks(); |
127 | 131 |
128 INLINE(ProfileTree* top_down()) { return &top_down_; } | 132 INLINE(const char* title() const) { return title_; } |
129 INLINE(ProfileTree* bottom_up()) { return &bottom_up_; } | 133 INLINE(unsigned uid() const) { return uid_; } |
| 134 INLINE(const ProfileTree* top_down() const) { return &top_down_; } |
| 135 INLINE(const ProfileTree* bottom_up() const) { return &bottom_up_; } |
130 | 136 |
131 void ShortPrint(); | 137 void ShortPrint(); |
132 void Print(); | 138 void Print(); |
133 | 139 |
134 private: | 140 private: |
| 141 const char* title_; |
| 142 unsigned uid_; |
135 ProfileTree top_down_; | 143 ProfileTree top_down_; |
136 ProfileTree bottom_up_; | 144 ProfileTree bottom_up_; |
137 | 145 |
138 DISALLOW_COPY_AND_ASSIGN(CpuProfile); | 146 DISALLOW_COPY_AND_ASSIGN(CpuProfile); |
139 }; | 147 }; |
140 | 148 |
141 | 149 |
142 class CodeMap BASE_EMBEDDED { | 150 class CodeMap { |
143 public: | 151 public: |
144 CodeMap() { } | 152 CodeMap() { } |
145 INLINE(void AddCode(Address addr, CodeEntry* entry, unsigned size)); | 153 INLINE(void AddCode(Address addr, CodeEntry* entry, unsigned size)); |
146 INLINE(void MoveCode(Address from, Address to)); | 154 INLINE(void MoveCode(Address from, Address to)); |
147 INLINE(void DeleteCode(Address addr)); | 155 INLINE(void DeleteCode(Address addr)); |
148 void AddAlias(Address alias, Address addr); | 156 void AddAlias(Address alias, Address addr); |
149 CodeEntry* FindEntry(Address addr); | 157 CodeEntry* FindEntry(Address addr); |
150 | 158 |
| 159 void Print(); |
| 160 |
151 private: | 161 private: |
152 struct CodeEntryInfo { | 162 struct CodeEntryInfo { |
153 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size) | 163 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size) |
154 : entry(an_entry), size(a_size) { } | 164 : entry(an_entry), size(a_size) { } |
155 CodeEntry* entry; | 165 CodeEntry* entry; |
156 unsigned size; | 166 unsigned size; |
157 }; | 167 }; |
158 | 168 |
159 struct CodeTreeConfig { | 169 struct CodeTreeConfig { |
160 typedef Address Key; | 170 typedef Address Key; |
161 typedef CodeEntryInfo Value; | 171 typedef CodeEntryInfo Value; |
162 static const Key kNoKey; | 172 static const Key kNoKey; |
163 static const Value kNoValue; | 173 static const Value kNoValue; |
164 static int Compare(const Key& a, const Key& b) { | 174 static int Compare(const Key& a, const Key& b) { |
165 return a < b ? -1 : (a > b ? 1 : 0); | 175 return a < b ? -1 : (a > b ? 1 : 0); |
166 } | 176 } |
167 }; | 177 }; |
168 typedef SplayTree<CodeTreeConfig> CodeTree; | 178 typedef SplayTree<CodeTreeConfig> CodeTree; |
169 | 179 |
| 180 class CodeTreePrinter { |
| 181 public: |
| 182 void Call(const Address& key, const CodeEntryInfo& value); |
| 183 }; |
| 184 |
170 CodeTree tree_; | 185 CodeTree tree_; |
171 | 186 |
172 DISALLOW_COPY_AND_ASSIGN(CodeMap); | 187 DISALLOW_COPY_AND_ASSIGN(CodeMap); |
173 }; | 188 }; |
174 | 189 |
175 | 190 |
176 class CpuProfilesCollection { | 191 class CpuProfilesCollection { |
177 public: | 192 public: |
178 CpuProfilesCollection(); | 193 CpuProfilesCollection(); |
179 ~CpuProfilesCollection(); | 194 ~CpuProfilesCollection(); |
180 | 195 |
181 void AddProfile(unsigned uid); | 196 bool StartProfiling(const char* title, unsigned uid); |
| 197 bool StartProfiling(String* title, unsigned uid); |
| 198 CpuProfile* StopProfiling(const char* title); |
| 199 CpuProfile* StopProfiling(String* title); |
| 200 INLINE(List<CpuProfile*>* profiles()) { return &profiles_; } |
| 201 CpuProfile* GetProfile(unsigned uid); |
| 202 inline bool is_last_profile(); |
182 | 203 |
183 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 204 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
184 String* name, String* resource_name, int line_number); | 205 String* name, String* resource_name, int line_number); |
185 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name); | 206 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name); |
186 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, int args_count); | 207 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, int args_count); |
187 | 208 |
188 INLINE(CpuProfile* profile()) { return profiles_.last(); } | 209 // Called from profile generator thread. |
| 210 void AddPathToCurrentProfiles(const Vector<CodeEntry*>& path); |
| 211 |
| 212 // This will be moved to V8 API. |
| 213 static const int kNoLineNumberInfo = -1; |
189 | 214 |
190 private: | 215 private: |
191 const char* GetName(String* name); | 216 const char* GetName(String* name); |
192 const char* GetName(int args_count); | 217 const char* GetName(int args_count); |
193 | 218 |
194 INLINE(static bool StringsMatch(void* key1, void* key2)) { | 219 INLINE(static bool StringsMatch(void* key1, void* key2)) { |
195 return strcmp(reinterpret_cast<char*>(key1), | 220 return strcmp(reinterpret_cast<char*>(key1), |
196 reinterpret_cast<char*>(key2)) == 0; | 221 reinterpret_cast<char*>(key2)) == 0; |
197 } | 222 } |
198 | 223 |
| 224 INLINE(static bool CpuProfilesMatch(void* key1, void* key2)) { |
| 225 return key1 == key2; |
| 226 } |
| 227 |
199 // String::Hash -> const char* | 228 // String::Hash -> const char* |
200 HashMap function_and_resource_names_; | 229 HashMap function_and_resource_names_; |
201 // args_count -> char* | 230 // args_count -> char* |
202 List<char*> args_count_names_; | 231 List<char*> args_count_names_; |
203 List<CodeEntry*> code_entries_; | 232 List<CodeEntry*> code_entries_; |
204 List<CpuProfile*> profiles_; | 233 List<CpuProfile*> profiles_; |
| 234 // uid -> CpuProfile* |
| 235 HashMap profiles_uids_; |
| 236 |
| 237 // Accessed by VM thread and profile generator thread. |
| 238 List<CpuProfile*> current_profiles_; |
| 239 Semaphore* current_profiles_semaphore_; |
205 | 240 |
206 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection); | 241 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection); |
207 }; | 242 }; |
208 | 243 |
209 | 244 |
210 class ProfileGenerator { | 245 class ProfileGenerator { |
211 public: | 246 public: |
212 explicit ProfileGenerator(CpuProfilesCollection* profiles); | 247 explicit ProfileGenerator(CpuProfilesCollection* profiles); |
213 | 248 |
214 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 249 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
(...skipping 11 matching lines...) Expand all Loading... |
226 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, | 261 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, |
227 int args_count)) { | 262 int args_count)) { |
228 return profiles_->NewCodeEntry(tag, args_count); | 263 return profiles_->NewCodeEntry(tag, args_count); |
229 } | 264 } |
230 | 265 |
231 void RecordTickSample(const TickSample& sample); | 266 void RecordTickSample(const TickSample& sample); |
232 | 267 |
233 INLINE(CodeMap* code_map()) { return &code_map_; } | 268 INLINE(CodeMap* code_map()) { return &code_map_; } |
234 | 269 |
235 private: | 270 private: |
236 INLINE(CpuProfile* profile()) { return profiles_->profile(); } | |
237 | |
238 CpuProfilesCollection* profiles_; | 271 CpuProfilesCollection* profiles_; |
239 CodeMap code_map_; | 272 CodeMap code_map_; |
240 | 273 |
241 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); | 274 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator); |
242 }; | 275 }; |
243 | 276 |
| 277 #endif // ENABLE_CPP_PROFILES_PROCESSOR |
244 | 278 |
245 } } // namespace v8::internal | 279 } } // namespace v8::internal |
246 | 280 |
247 #endif // V8_PROFILE_GENERATOR_H_ | 281 #endif // V8_PROFILE_GENERATOR_H_ |
OLD | NEW |