blob: 5ea0c52a95dc0eadcc38e928230ee89273ef2ab0 [file] [log] [blame]
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fschneider@chromium.org086aac62010-03-17 13:18:24 +000027//
28// Tests of profiles generator and utilities.
29
30#include "v8.h"
31#include "profile-generator-inl.h"
32#include "cctest.h"
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000033#include "../include/v8-profiler.h"
fschneider@chromium.org086aac62010-03-17 13:18:24 +000034
fschneider@chromium.org086aac62010-03-17 13:18:24 +000035using i::CodeEntry;
36using i::CodeMap;
lrn@chromium.org25156de2010-04-06 13:10:27 +000037using i::CpuProfile;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000038using i::CpuProfiler;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000039using i::CpuProfilesCollection;
fschneider@chromium.org086aac62010-03-17 13:18:24 +000040using i::ProfileNode;
41using i::ProfileTree;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000042using i::ProfileGenerator;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000043using i::SampleRateCalculator;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000044using i::TickSample;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000045using i::TokenEnumerator;
fschneider@chromium.org086aac62010-03-17 13:18:24 +000046using i::Vector;
47
48
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000049namespace v8 {
50namespace internal {
51
52class TokenEnumeratorTester {
53 public:
54 static i::List<bool>* token_removed(TokenEnumerator* te) {
55 return &te->token_removed_;
56 }
57};
58
59} } // namespace v8::internal
60
61TEST(TokenEnumerator) {
62 TokenEnumerator te;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000063 CHECK_EQ(TokenEnumerator::kNoSecurityToken, te.GetTokenId(NULL));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000064 v8::HandleScope hs;
ricow@chromium.org55ee8072011-09-08 16:33:10 +000065 v8::Local<v8::String> token1(v8::String::New("1x"));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000066 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
67 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
ricow@chromium.org55ee8072011-09-08 16:33:10 +000068 v8::Local<v8::String> token2(v8::String::New("2x"));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000069 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
70 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
71 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
72 {
73 v8::HandleScope hs;
ricow@chromium.org55ee8072011-09-08 16:33:10 +000074 v8::Local<v8::String> token3(v8::String::New("3x"));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000075 CHECK_EQ(2, te.GetTokenId(*v8::Utils::OpenHandle(*token3)));
76 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
77 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
78 }
79 CHECK(!i::TokenEnumeratorTester::token_removed(&te)->at(2));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000080 HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000081 CHECK(i::TokenEnumeratorTester::token_removed(&te)->at(2));
82 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
83 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
84}
85
86
fschneider@chromium.org086aac62010-03-17 13:18:24 +000087TEST(ProfileNodeFindOrAddChild) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000088 ProfileNode node(NULL, NULL);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000089 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
90 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +000091 ProfileNode* childNode1 = node.FindOrAddChild(&entry1);
92 CHECK_NE(NULL, childNode1);
93 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000094 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
95 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +000096 ProfileNode* childNode2 = node.FindOrAddChild(&entry2);
97 CHECK_NE(NULL, childNode2);
98 CHECK_NE(childNode1, childNode2);
99 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
100 CHECK_EQ(childNode2, node.FindOrAddChild(&entry2));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000101 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
102 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000103 ProfileNode* childNode3 = node.FindOrAddChild(&entry3);
104 CHECK_NE(NULL, childNode3);
105 CHECK_NE(childNode1, childNode3);
106 CHECK_NE(childNode2, childNode3);
107 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
108 CHECK_EQ(childNode2, node.FindOrAddChild(&entry2));
109 CHECK_EQ(childNode3, node.FindOrAddChild(&entry3));
110}
111
112
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000113TEST(ProfileNodeFindOrAddChildForSameFunction) {
114 const char* empty = "";
115 const char* aaa = "aaa";
116 ProfileNode node(NULL, NULL);
117 CodeEntry entry1(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0,
118 TokenEnumerator::kNoSecurityToken);
119 ProfileNode* childNode1 = node.FindOrAddChild(&entry1);
120 CHECK_NE(NULL, childNode1);
121 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
122 // The same function again.
123 CodeEntry entry2(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0,
124 TokenEnumerator::kNoSecurityToken);
125 CHECK_EQ(childNode1, node.FindOrAddChild(&entry2));
126 // Now with a different security token.
127 CodeEntry entry3(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0,
128 TokenEnumerator::kNoSecurityToken + 1);
129 CHECK_EQ(childNode1, node.FindOrAddChild(&entry3));
130}
131
132
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000133namespace {
134
135class ProfileTreeTestHelper {
136 public:
lrn@chromium.org25156de2010-04-06 13:10:27 +0000137 explicit ProfileTreeTestHelper(const ProfileTree* tree)
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000138 : tree_(tree) { }
139
140 ProfileNode* Walk(CodeEntry* entry1,
141 CodeEntry* entry2 = NULL,
142 CodeEntry* entry3 = NULL) {
143 ProfileNode* node = tree_->root();
144 node = node->FindChild(entry1);
145 if (node == NULL) return NULL;
146 if (entry2 != NULL) {
147 node = node->FindChild(entry2);
148 if (node == NULL) return NULL;
149 }
150 if (entry3 != NULL) {
151 node = node->FindChild(entry3);
152 }
153 return node;
154 }
155
156 private:
lrn@chromium.org25156de2010-04-06 13:10:27 +0000157 const ProfileTree* tree_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000158};
159
160} // namespace
161
162TEST(ProfileTreeAddPathFromStart) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000163 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
164 TokenEnumerator::kNoSecurityToken);
165 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
166 TokenEnumerator::kNoSecurityToken);
167 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
168 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000169 ProfileTree tree;
170 ProfileTreeTestHelper helper(&tree);
171 CHECK_EQ(NULL, helper.Walk(&entry1));
172 CHECK_EQ(NULL, helper.Walk(&entry2));
173 CHECK_EQ(NULL, helper.Walk(&entry3));
174
175 CodeEntry* path[] = {NULL, &entry1, NULL, &entry2, NULL, NULL, &entry3, NULL};
176 Vector<CodeEntry*> path_vec(path, sizeof(path) / sizeof(path[0]));
177 tree.AddPathFromStart(path_vec);
178 CHECK_EQ(NULL, helper.Walk(&entry2));
179 CHECK_EQ(NULL, helper.Walk(&entry3));
180 ProfileNode* node1 = helper.Walk(&entry1);
181 CHECK_NE(NULL, node1);
182 CHECK_EQ(0, node1->total_ticks());
183 CHECK_EQ(0, node1->self_ticks());
184 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
185 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
186 ProfileNode* node2 = helper.Walk(&entry1, &entry2);
187 CHECK_NE(NULL, node2);
188 CHECK_NE(node1, node2);
189 CHECK_EQ(0, node2->total_ticks());
190 CHECK_EQ(0, node2->self_ticks());
191 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
192 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry2));
193 ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3);
194 CHECK_NE(NULL, node3);
195 CHECK_NE(node1, node3);
196 CHECK_NE(node2, node3);
197 CHECK_EQ(0, node3->total_ticks());
198 CHECK_EQ(1, node3->self_ticks());
199
200 tree.AddPathFromStart(path_vec);
201 CHECK_EQ(node1, helper.Walk(&entry1));
202 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
203 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
204 CHECK_EQ(0, node1->total_ticks());
205 CHECK_EQ(0, node1->self_ticks());
206 CHECK_EQ(0, node2->total_ticks());
207 CHECK_EQ(0, node2->self_ticks());
208 CHECK_EQ(0, node3->total_ticks());
209 CHECK_EQ(2, node3->self_ticks());
210
211 CodeEntry* path2[] = {&entry1, &entry2, &entry2};
212 Vector<CodeEntry*> path2_vec(path2, sizeof(path2) / sizeof(path2[0]));
213 tree.AddPathFromStart(path2_vec);
214 CHECK_EQ(NULL, helper.Walk(&entry2));
215 CHECK_EQ(NULL, helper.Walk(&entry3));
216 CHECK_EQ(node1, helper.Walk(&entry1));
217 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
218 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
219 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
220 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
221 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
222 CHECK_EQ(0, node3->total_ticks());
223 CHECK_EQ(2, node3->self_ticks());
224 ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2);
225 CHECK_NE(NULL, node4);
226 CHECK_NE(node3, node4);
227 CHECK_EQ(0, node4->total_ticks());
228 CHECK_EQ(1, node4->self_ticks());
229}
230
231
232TEST(ProfileTreeAddPathFromEnd) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000233 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
234 TokenEnumerator::kNoSecurityToken);
235 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
236 TokenEnumerator::kNoSecurityToken);
237 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
238 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000239 ProfileTree tree;
240 ProfileTreeTestHelper helper(&tree);
241 CHECK_EQ(NULL, helper.Walk(&entry1));
242 CHECK_EQ(NULL, helper.Walk(&entry2));
243 CHECK_EQ(NULL, helper.Walk(&entry3));
244
245 CodeEntry* path[] = {NULL, &entry3, NULL, &entry2, NULL, NULL, &entry1, NULL};
246 Vector<CodeEntry*> path_vec(path, sizeof(path) / sizeof(path[0]));
247 tree.AddPathFromEnd(path_vec);
248 CHECK_EQ(NULL, helper.Walk(&entry2));
249 CHECK_EQ(NULL, helper.Walk(&entry3));
250 ProfileNode* node1 = helper.Walk(&entry1);
251 CHECK_NE(NULL, node1);
252 CHECK_EQ(0, node1->total_ticks());
253 CHECK_EQ(0, node1->self_ticks());
254 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
255 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
256 ProfileNode* node2 = helper.Walk(&entry1, &entry2);
257 CHECK_NE(NULL, node2);
258 CHECK_NE(node1, node2);
259 CHECK_EQ(0, node2->total_ticks());
260 CHECK_EQ(0, node2->self_ticks());
261 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
262 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry2));
263 ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3);
264 CHECK_NE(NULL, node3);
265 CHECK_NE(node1, node3);
266 CHECK_NE(node2, node3);
267 CHECK_EQ(0, node3->total_ticks());
268 CHECK_EQ(1, node3->self_ticks());
269
270 tree.AddPathFromEnd(path_vec);
271 CHECK_EQ(node1, helper.Walk(&entry1));
272 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
273 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
274 CHECK_EQ(0, node1->total_ticks());
275 CHECK_EQ(0, node1->self_ticks());
276 CHECK_EQ(0, node2->total_ticks());
277 CHECK_EQ(0, node2->self_ticks());
278 CHECK_EQ(0, node3->total_ticks());
279 CHECK_EQ(2, node3->self_ticks());
280
281 CodeEntry* path2[] = {&entry2, &entry2, &entry1};
282 Vector<CodeEntry*> path2_vec(path2, sizeof(path2) / sizeof(path2[0]));
283 tree.AddPathFromEnd(path2_vec);
284 CHECK_EQ(NULL, helper.Walk(&entry2));
285 CHECK_EQ(NULL, helper.Walk(&entry3));
286 CHECK_EQ(node1, helper.Walk(&entry1));
287 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
288 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
289 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
290 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
291 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
292 CHECK_EQ(0, node3->total_ticks());
293 CHECK_EQ(2, node3->self_ticks());
294 ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2);
295 CHECK_NE(NULL, node4);
296 CHECK_NE(node3, node4);
297 CHECK_EQ(0, node4->total_ticks());
298 CHECK_EQ(1, node4->self_ticks());
299}
300
301
302TEST(ProfileTreeCalculateTotalTicks) {
303 ProfileTree empty_tree;
304 CHECK_EQ(0, empty_tree.root()->total_ticks());
305 CHECK_EQ(0, empty_tree.root()->self_ticks());
306 empty_tree.CalculateTotalTicks();
307 CHECK_EQ(0, empty_tree.root()->total_ticks());
308 CHECK_EQ(0, empty_tree.root()->self_ticks());
309 empty_tree.root()->IncrementSelfTicks();
310 CHECK_EQ(0, empty_tree.root()->total_ticks());
311 CHECK_EQ(1, empty_tree.root()->self_ticks());
312 empty_tree.CalculateTotalTicks();
313 CHECK_EQ(1, empty_tree.root()->total_ticks());
314 CHECK_EQ(1, empty_tree.root()->self_ticks());
315
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000316 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
317 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000318 CodeEntry* e1_path[] = {&entry1};
319 Vector<CodeEntry*> e1_path_vec(
320 e1_path, sizeof(e1_path) / sizeof(e1_path[0]));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000321
322 ProfileTree single_child_tree;
323 single_child_tree.AddPathFromStart(e1_path_vec);
324 single_child_tree.root()->IncrementSelfTicks();
325 CHECK_EQ(0, single_child_tree.root()->total_ticks());
326 CHECK_EQ(1, single_child_tree.root()->self_ticks());
327 ProfileTreeTestHelper single_child_helper(&single_child_tree);
328 ProfileNode* node1 = single_child_helper.Walk(&entry1);
329 CHECK_NE(NULL, node1);
330 CHECK_EQ(0, node1->total_ticks());
331 CHECK_EQ(1, node1->self_ticks());
332 single_child_tree.CalculateTotalTicks();
333 CHECK_EQ(2, single_child_tree.root()->total_ticks());
334 CHECK_EQ(1, single_child_tree.root()->self_ticks());
335 CHECK_EQ(1, node1->total_ticks());
336 CHECK_EQ(1, node1->self_ticks());
337
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000338 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
339 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000340 CodeEntry* e1_e2_path[] = {&entry1, &entry2};
341 Vector<CodeEntry*> e1_e2_path_vec(
342 e1_e2_path, sizeof(e1_e2_path) / sizeof(e1_e2_path[0]));
343
344 ProfileTree flat_tree;
345 ProfileTreeTestHelper flat_helper(&flat_tree);
346 flat_tree.AddPathFromStart(e1_path_vec);
347 flat_tree.AddPathFromStart(e1_path_vec);
348 flat_tree.AddPathFromStart(e1_e2_path_vec);
349 flat_tree.AddPathFromStart(e1_e2_path_vec);
350 flat_tree.AddPathFromStart(e1_e2_path_vec);
351 // Results in {root,0,0} -> {entry1,0,2} -> {entry2,0,3}
352 CHECK_EQ(0, flat_tree.root()->total_ticks());
353 CHECK_EQ(0, flat_tree.root()->self_ticks());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000354 node1 = flat_helper.Walk(&entry1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000355 CHECK_NE(NULL, node1);
356 CHECK_EQ(0, node1->total_ticks());
357 CHECK_EQ(2, node1->self_ticks());
358 ProfileNode* node2 = flat_helper.Walk(&entry1, &entry2);
359 CHECK_NE(NULL, node2);
360 CHECK_EQ(0, node2->total_ticks());
361 CHECK_EQ(3, node2->self_ticks());
362 flat_tree.CalculateTotalTicks();
363 // Must calculate {root,5,0} -> {entry1,5,2} -> {entry2,3,3}
364 CHECK_EQ(5, flat_tree.root()->total_ticks());
365 CHECK_EQ(0, flat_tree.root()->self_ticks());
366 CHECK_EQ(5, node1->total_ticks());
367 CHECK_EQ(2, node1->self_ticks());
368 CHECK_EQ(3, node2->total_ticks());
369 CHECK_EQ(3, node2->self_ticks());
370
371 CodeEntry* e2_path[] = {&entry2};
372 Vector<CodeEntry*> e2_path_vec(
373 e2_path, sizeof(e2_path) / sizeof(e2_path[0]));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000374 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
375 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000376 CodeEntry* e3_path[] = {&entry3};
377 Vector<CodeEntry*> e3_path_vec(
378 e3_path, sizeof(e3_path) / sizeof(e3_path[0]));
379
380 ProfileTree wide_tree;
381 ProfileTreeTestHelper wide_helper(&wide_tree);
382 wide_tree.AddPathFromStart(e1_path_vec);
383 wide_tree.AddPathFromStart(e1_path_vec);
384 wide_tree.AddPathFromStart(e1_e2_path_vec);
385 wide_tree.AddPathFromStart(e2_path_vec);
386 wide_tree.AddPathFromStart(e2_path_vec);
387 wide_tree.AddPathFromStart(e2_path_vec);
388 wide_tree.AddPathFromStart(e3_path_vec);
389 wide_tree.AddPathFromStart(e3_path_vec);
390 wide_tree.AddPathFromStart(e3_path_vec);
391 wide_tree.AddPathFromStart(e3_path_vec);
392 // Results in -> {entry1,0,2} -> {entry2,0,1}
393 // {root,0,0} -> {entry2,0,3}
394 // -> {entry3,0,4}
395 CHECK_EQ(0, wide_tree.root()->total_ticks());
396 CHECK_EQ(0, wide_tree.root()->self_ticks());
397 node1 = wide_helper.Walk(&entry1);
398 CHECK_NE(NULL, node1);
399 CHECK_EQ(0, node1->total_ticks());
400 CHECK_EQ(2, node1->self_ticks());
401 ProfileNode* node1_2 = wide_helper.Walk(&entry1, &entry2);
402 CHECK_NE(NULL, node1_2);
403 CHECK_EQ(0, node1_2->total_ticks());
404 CHECK_EQ(1, node1_2->self_ticks());
405 node2 = wide_helper.Walk(&entry2);
406 CHECK_NE(NULL, node2);
407 CHECK_EQ(0, node2->total_ticks());
408 CHECK_EQ(3, node2->self_ticks());
409 ProfileNode* node3 = wide_helper.Walk(&entry3);
410 CHECK_NE(NULL, node3);
411 CHECK_EQ(0, node3->total_ticks());
412 CHECK_EQ(4, node3->self_ticks());
413 wide_tree.CalculateTotalTicks();
414 // Calculates -> {entry1,3,2} -> {entry2,1,1}
415 // {root,10,0} -> {entry2,3,3}
416 // -> {entry3,4,4}
417 CHECK_EQ(10, wide_tree.root()->total_ticks());
418 CHECK_EQ(0, wide_tree.root()->self_ticks());
419 CHECK_EQ(3, node1->total_ticks());
420 CHECK_EQ(2, node1->self_ticks());
421 CHECK_EQ(1, node1_2->total_ticks());
422 CHECK_EQ(1, node1_2->self_ticks());
423 CHECK_EQ(3, node2->total_ticks());
424 CHECK_EQ(3, node2->self_ticks());
425 CHECK_EQ(4, node3->total_ticks());
426 CHECK_EQ(4, node3->self_ticks());
427}
428
429
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000430TEST(ProfileTreeFilteredClone) {
431 ProfileTree source_tree;
432 const int token0 = 0, token1 = 1, token2 = 2;
433 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, token0);
434 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, token1);
435 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, token0);
436 CodeEntry entry4(
437 i::Logger::FUNCTION_TAG, "", "ddd", "", 0,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000438 TokenEnumerator::kInheritsSecurityToken);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000439
440 {
441 CodeEntry* e1_e2_path[] = {&entry1, &entry2};
442 Vector<CodeEntry*> e1_e2_path_vec(
443 e1_e2_path, sizeof(e1_e2_path) / sizeof(e1_e2_path[0]));
444 source_tree.AddPathFromStart(e1_e2_path_vec);
445 CodeEntry* e2_e4_path[] = {&entry2, &entry4};
446 Vector<CodeEntry*> e2_e4_path_vec(
447 e2_e4_path, sizeof(e2_e4_path) / sizeof(e2_e4_path[0]));
448 source_tree.AddPathFromStart(e2_e4_path_vec);
449 CodeEntry* e3_e1_path[] = {&entry3, &entry1};
450 Vector<CodeEntry*> e3_e1_path_vec(
451 e3_e1_path, sizeof(e3_e1_path) / sizeof(e3_e1_path[0]));
452 source_tree.AddPathFromStart(e3_e1_path_vec);
453 CodeEntry* e3_e2_path[] = {&entry3, &entry2};
454 Vector<CodeEntry*> e3_e2_path_vec(
455 e3_e2_path, sizeof(e3_e2_path) / sizeof(e3_e2_path[0]));
456 source_tree.AddPathFromStart(e3_e2_path_vec);
457 source_tree.CalculateTotalTicks();
458 // Results in -> {entry1,0,1,0} -> {entry2,1,1,1}
459 // {root,0,4,-1} -> {entry2,0,1,1} -> {entry4,1,1,inherits}
460 // -> {entry3,0,2,0} -> {entry1,1,1,0}
461 // -> {entry2,1,1,1}
462 CHECK_EQ(4, source_tree.root()->total_ticks());
463 CHECK_EQ(0, source_tree.root()->self_ticks());
464 }
465
466 {
467 ProfileTree token0_tree;
468 token0_tree.FilteredClone(&source_tree, token0);
469 // Should be -> {entry1,1,1,0}
470 // {root,1,4,-1} -> {entry3,1,2,0} -> {entry1,1,1,0}
471 // [self ticks from filtered nodes are attributed to their parents]
472 CHECK_EQ(4, token0_tree.root()->total_ticks());
473 CHECK_EQ(1, token0_tree.root()->self_ticks());
474 ProfileTreeTestHelper token0_helper(&token0_tree);
475 ProfileNode* node1 = token0_helper.Walk(&entry1);
476 CHECK_NE(NULL, node1);
477 CHECK_EQ(1, node1->total_ticks());
478 CHECK_EQ(1, node1->self_ticks());
479 CHECK_EQ(NULL, token0_helper.Walk(&entry2));
480 ProfileNode* node3 = token0_helper.Walk(&entry3);
481 CHECK_NE(NULL, node3);
482 CHECK_EQ(2, node3->total_ticks());
483 CHECK_EQ(1, node3->self_ticks());
484 ProfileNode* node3_1 = token0_helper.Walk(&entry3, &entry1);
485 CHECK_NE(NULL, node3_1);
486 CHECK_EQ(1, node3_1->total_ticks());
487 CHECK_EQ(1, node3_1->self_ticks());
488 CHECK_EQ(NULL, token0_helper.Walk(&entry3, &entry2));
489 }
490
491 {
492 ProfileTree token1_tree;
493 token1_tree.FilteredClone(&source_tree, token1);
494 // Should be
495 // {root,1,4,-1} -> {entry2,2,3,1} -> {entry4,1,1,inherits}
496 // [child nodes referring to the same entry get merged and
497 // their self times summed up]
498 CHECK_EQ(4, token1_tree.root()->total_ticks());
499 CHECK_EQ(1, token1_tree.root()->self_ticks());
500 ProfileTreeTestHelper token1_helper(&token1_tree);
501 CHECK_EQ(NULL, token1_helper.Walk(&entry1));
502 CHECK_EQ(NULL, token1_helper.Walk(&entry3));
503 ProfileNode* node2 = token1_helper.Walk(&entry2);
504 CHECK_NE(NULL, node2);
505 CHECK_EQ(3, node2->total_ticks());
506 CHECK_EQ(2, node2->self_ticks());
507 ProfileNode* node2_4 = token1_helper.Walk(&entry2, &entry4);
508 CHECK_NE(NULL, node2_4);
509 CHECK_EQ(1, node2_4->total_ticks());
510 CHECK_EQ(1, node2_4->self_ticks());
511 }
512
513 {
514 ProfileTree token2_tree;
515 token2_tree.FilteredClone(&source_tree, token2);
516 // Should be
517 // {root,4,4,-1}
518 // [no nodes, all ticks get migrated into root node]
519 CHECK_EQ(4, token2_tree.root()->total_ticks());
520 CHECK_EQ(4, token2_tree.root()->self_ticks());
521 ProfileTreeTestHelper token2_helper(&token2_tree);
522 CHECK_EQ(NULL, token2_helper.Walk(&entry1));
523 CHECK_EQ(NULL, token2_helper.Walk(&entry2));
524 CHECK_EQ(NULL, token2_helper.Walk(&entry3));
525 }
526}
527
528
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000529static inline i::Address ToAddress(int n) {
530 return reinterpret_cast<i::Address>(n);
531}
532
533TEST(CodeMapAddCode) {
534 CodeMap code_map;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000535 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
536 TokenEnumerator::kNoSecurityToken);
537 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
538 TokenEnumerator::kNoSecurityToken);
539 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
540 TokenEnumerator::kNoSecurityToken);
541 CodeEntry entry4(i::Logger::FUNCTION_TAG, "", "ddd", "", 0,
542 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000543 code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
544 code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
545 code_map.AddCode(ToAddress(0x1900), &entry3, 0x50);
546 code_map.AddCode(ToAddress(0x1950), &entry4, 0x10);
547 CHECK_EQ(NULL, code_map.FindEntry(0));
548 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1500 - 1)));
549 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
550 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x100)));
551 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x200 - 1)));
552 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
553 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x50)));
554 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x100 - 1)));
555 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1700 + 0x100)));
556 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1900 - 1)));
557 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900)));
558 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900 + 0x28)));
559 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950)));
560 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x7)));
561 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x10 - 1)));
562 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1950 + 0x10)));
563 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0xFFFFFFFF)));
564}
565
566
567TEST(CodeMapMoveAndDeleteCode) {
568 CodeMap code_map;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000569 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
570 TokenEnumerator::kNoSecurityToken);
571 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
572 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000573 code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
574 code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
575 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
576 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
lrn@chromium.org34e60782011-09-15 07:25:40 +0000577 code_map.MoveCode(ToAddress(0x1500), ToAddress(0x1700)); // Deprecate bbb.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000578 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1500)));
lrn@chromium.org34e60782011-09-15 07:25:40 +0000579 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1700)));
580 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
581 TokenEnumerator::kNoSecurityToken);
582 code_map.AddCode(ToAddress(0x1750), &entry3, 0x100);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000583 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1700)));
lrn@chromium.org34e60782011-09-15 07:25:40 +0000584 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1750)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000585}
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000586
587
ager@chromium.org357bf652010-04-12 11:30:10 +0000588namespace {
589
590class TestSetup {
591 public:
592 TestSetup()
593 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
594 i::FLAG_prof_browser_mode = false;
595 }
596
597 ~TestSetup() {
598 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
599 }
600
601 private:
602 bool old_flag_prof_browser_mode_;
603};
604
605} // namespace
606
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000607TEST(RecordTickSample) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000608 TestSetup test_setup;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000609 CpuProfilesCollection profiles;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000610 profiles.StartProfiling("", 1);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000611 ProfileGenerator generator(&profiles);
612 CodeEntry* entry1 = generator.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
613 CodeEntry* entry2 = generator.NewCodeEntry(i::Logger::FUNCTION_TAG, "bbb");
614 CodeEntry* entry3 = generator.NewCodeEntry(i::Logger::FUNCTION_TAG, "ccc");
615 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
616 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
617 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
618
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000619 // We are building the following calls tree:
620 // -> aaa - sample1
621 // aaa -> bbb -> ccc - sample2
622 // -> ccc -> aaa - sample3
623 TickSample sample1;
624 sample1.pc = ToAddress(0x1600);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000625 sample1.tos = ToAddress(0x1500);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000626 sample1.stack[0] = ToAddress(0x1510);
627 sample1.frames_count = 1;
628 generator.RecordTickSample(sample1);
629 TickSample sample2;
630 sample2.pc = ToAddress(0x1925);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000631 sample2.tos = ToAddress(0x1900);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000632 sample2.stack[0] = ToAddress(0x1780);
633 sample2.stack[1] = ToAddress(0x10000); // non-existent.
634 sample2.stack[2] = ToAddress(0x1620);
635 sample2.frames_count = 3;
636 generator.RecordTickSample(sample2);
637 TickSample sample3;
638 sample3.pc = ToAddress(0x1510);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000639 sample3.tos = ToAddress(0x1500);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000640 sample3.stack[0] = ToAddress(0x1910);
641 sample3.stack[1] = ToAddress(0x1610);
642 sample3.frames_count = 2;
643 generator.RecordTickSample(sample3);
644
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000645 CpuProfile* profile =
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000646 profiles.StopProfiling(TokenEnumerator::kNoSecurityToken, "", 1);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000647 CHECK_NE(NULL, profile);
648 ProfileTreeTestHelper top_down_test_helper(profile->top_down());
649 CHECK_EQ(NULL, top_down_test_helper.Walk(entry2));
650 CHECK_EQ(NULL, top_down_test_helper.Walk(entry3));
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000651 ProfileNode* node1 = top_down_test_helper.Walk(entry1);
652 CHECK_NE(NULL, node1);
653 CHECK_EQ(entry1, node1->entry());
654 ProfileNode* node2 = top_down_test_helper.Walk(entry1, entry1);
655 CHECK_NE(NULL, node2);
656 CHECK_EQ(entry1, node2->entry());
657 ProfileNode* node3 = top_down_test_helper.Walk(entry1, entry2, entry3);
658 CHECK_NE(NULL, node3);
659 CHECK_EQ(entry3, node3->entry());
660 ProfileNode* node4 = top_down_test_helper.Walk(entry1, entry3, entry1);
661 CHECK_NE(NULL, node4);
662 CHECK_EQ(entry1, node4->entry());
663}
lrn@chromium.org25156de2010-04-06 13:10:27 +0000664
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000665
666TEST(SampleRateCalculator) {
667 const double kSamplingIntervalMs = i::Logger::kSamplingIntervalMs;
668
669 // Verify that ticking exactly in query intervals results in the
670 // initial sampling interval.
671 double time = 0.0;
672 SampleRateCalculator calc1;
673 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
674 calc1.UpdateMeasurements(time);
675 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
676 time += SampleRateCalculator::kWallTimeQueryIntervalMs;
677 calc1.UpdateMeasurements(time);
678 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
679 time += SampleRateCalculator::kWallTimeQueryIntervalMs;
680 calc1.UpdateMeasurements(time);
681 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
682 time += SampleRateCalculator::kWallTimeQueryIntervalMs;
683 calc1.UpdateMeasurements(time);
684 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
685
686 SampleRateCalculator calc2;
687 time = 0.0;
688 CHECK_EQ(kSamplingIntervalMs, calc2.ticks_per_ms());
689 calc2.UpdateMeasurements(time);
690 CHECK_EQ(kSamplingIntervalMs, calc2.ticks_per_ms());
691 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 0.5;
692 calc2.UpdateMeasurements(time);
693 // (1.0 + 2.0) / 2
694 CHECK_EQ(kSamplingIntervalMs * 1.5, calc2.ticks_per_ms());
695 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 0.75;
696 calc2.UpdateMeasurements(time);
697 // (1.0 + 2.0 + 2.0) / 3
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000698 CHECK_EQ(kSamplingIntervalMs * 5.0, floor(calc2.ticks_per_ms() * 3.0 + 0.5));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000699
700 SampleRateCalculator calc3;
701 time = 0.0;
702 CHECK_EQ(kSamplingIntervalMs, calc3.ticks_per_ms());
703 calc3.UpdateMeasurements(time);
704 CHECK_EQ(kSamplingIntervalMs, calc3.ticks_per_ms());
705 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 2;
706 calc3.UpdateMeasurements(time);
707 // (1.0 + 0.5) / 2
708 CHECK_EQ(kSamplingIntervalMs * 0.75, calc3.ticks_per_ms());
709 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 1.5;
710 calc3.UpdateMeasurements(time);
711 // (1.0 + 0.5 + 0.5) / 3
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000712 CHECK_EQ(kSamplingIntervalMs * 2.0, floor(calc3.ticks_per_ms() * 3.0 + 0.5));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000713}
714
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000715
716// --- P r o f i l e r E x t e n s i o n ---
717
718class ProfilerExtension : public v8::Extension {
719 public:
720 ProfilerExtension() : v8::Extension("v8/profiler", kSource) { }
721 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
722 v8::Handle<v8::String> name);
723 static v8::Handle<v8::Value> StartProfiling(const v8::Arguments& args);
724 static v8::Handle<v8::Value> StopProfiling(const v8::Arguments& args);
725 private:
726 static const char* kSource;
727};
728
729
730const char* ProfilerExtension::kSource =
731 "native function startProfiling();"
732 "native function stopProfiling();";
733
734v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction(
735 v8::Handle<v8::String> name) {
736 if (name->Equals(v8::String::New("startProfiling"))) {
737 return v8::FunctionTemplate::New(ProfilerExtension::StartProfiling);
738 } else if (name->Equals(v8::String::New("stopProfiling"))) {
739 return v8::FunctionTemplate::New(ProfilerExtension::StopProfiling);
740 } else {
741 CHECK(false);
742 return v8::Handle<v8::FunctionTemplate>();
743 }
744}
745
746
747v8::Handle<v8::Value> ProfilerExtension::StartProfiling(
748 const v8::Arguments& args) {
749 if (args.Length() > 0)
750 v8::CpuProfiler::StartProfiling(args[0].As<v8::String>());
751 else
752 v8::CpuProfiler::StartProfiling(v8::String::New(""));
753 return v8::Undefined();
754}
755
756
757v8::Handle<v8::Value> ProfilerExtension::StopProfiling(
758 const v8::Arguments& args) {
759 if (args.Length() > 0)
760 v8::CpuProfiler::StopProfiling(args[0].As<v8::String>());
761 else
762 v8::CpuProfiler::StopProfiling(v8::String::New(""));
763 return v8::Undefined();
764}
765
766
767static ProfilerExtension kProfilerExtension;
768v8::DeclareExtension kProfilerExtensionDeclaration(&kProfilerExtension);
769static v8::Persistent<v8::Context> env;
770
771static const ProfileNode* PickChild(const ProfileNode* parent,
772 const char* name) {
773 for (int i = 0; i < parent->children()->length(); ++i) {
774 const ProfileNode* child = parent->children()->at(i);
775 if (strcmp(child->entry()->name(), name) == 0) return child;
776 }
777 return NULL;
778}
779
780
781TEST(RecordStackTraceAtStartProfiling) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000782 // This test does not pass with inlining enabled since inlined functions
783 // don't appear in the stack trace.
784 i::FLAG_use_inlining = false;
785
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000786 if (env.IsEmpty()) {
787 v8::HandleScope scope;
788 const char* extensions[] = { "v8/profiler" };
789 v8::ExtensionConfiguration config(1, extensions);
790 env = v8::Context::New(&config);
791 }
792 v8::HandleScope scope;
793 env->Enter();
794
795 CHECK_EQ(0, CpuProfiler::GetProfilesCount());
796 CompileRun(
797 "function c() { startProfiling(); }\n"
798 "function b() { c(); }\n"
799 "function a() { b(); }\n"
800 "a();\n"
801 "stopProfiling();");
802 CHECK_EQ(1, CpuProfiler::GetProfilesCount());
803 CpuProfile* profile =
804 CpuProfiler::GetProfile(NULL, 0);
805 const ProfileTree* topDown = profile->top_down();
806 const ProfileNode* current = topDown->root();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000807 const_cast<ProfileNode*>(current)->Print(0);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000808 // The tree should look like this:
809 // (root)
810 // (anonymous function)
811 // a
812 // b
813 // c
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000814 // There can also be:
815 // startProfiling
816 // if the sampler managed to get a tick.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000817 current = PickChild(current, "(anonymous function)");
818 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
819 current = PickChild(current, "a");
820 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
821 current = PickChild(current, "b");
822 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
823 current = PickChild(current, "c");
824 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000825 CHECK(current->children()->length() == 0 ||
826 current->children()->length() == 1);
827 if (current->children()->length() == 1) {
828 current = PickChild(current, "startProfiling");
829 CHECK_EQ(0, current->children()->length());
830 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000831}
832
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000833
834TEST(Issue51919) {
835 CpuProfilesCollection collection;
836 i::EmbeddedVector<char*,
837 CpuProfilesCollection::kMaxSimultaneousProfiles> titles;
838 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) {
839 i::Vector<char> title = i::Vector<char>::New(16);
840 i::OS::SNPrintF(title, "%d", i);
841 CHECK(collection.StartProfiling(title.start(), i + 1)); // UID must be > 0.
842 titles[i] = title.start();
843 }
844 CHECK(!collection.StartProfiling(
845 "maximum", CpuProfilesCollection::kMaxSimultaneousProfiles + 1));
846 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i)
847 i::DeleteArray(titles[i]);
848}