test-profile-generator.cc 26.1 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 28 29
//
// Tests of profiles generator and utilities.

30
#include "include/v8-profiler.h"
31
#include "src/api.h"
32 33
#include "src/profiler/cpu-profiler.h"
#include "src/profiler/profile-generator-inl.h"
34
#include "src/v8.h"
35 36
#include "test/cctest/cctest.h"
#include "test/cctest/profiler-extension.h"
37 38 39

using i::CodeEntry;
using i::CodeMap;
40
using i::CpuProfile;
41
using i::CpuProfiler;
42
using i::CpuProfilesCollection;
43 44
using i::ProfileNode;
using i::ProfileTree;
45 46
using i::ProfileGenerator;
using i::TickSample;
47 48 49 50
using i::Vector;


TEST(ProfileNodeFindOrAddChild) {
51 52
  CcTest::InitializeVM();
  ProfileTree tree(CcTest::i_isolate());
53
  ProfileNode* node = tree.root();
54
  CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
55
  ProfileNode* childNode1 = node->FindOrAddChild(&entry1);
56
  CHECK(childNode1);
57
  CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
58
  CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
59
  ProfileNode* childNode2 = node->FindOrAddChild(&entry2);
60
  CHECK(childNode2);
61
  CHECK_NE(childNode1, childNode2);
62 63
  CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
  CHECK_EQ(childNode2, node->FindOrAddChild(&entry2));
64
  CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
65
  ProfileNode* childNode3 = node->FindOrAddChild(&entry3);
66
  CHECK(childNode3);
67 68
  CHECK_NE(childNode1, childNode3);
  CHECK_NE(childNode2, childNode3);
69 70 71
  CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
  CHECK_EQ(childNode2, node->FindOrAddChild(&entry2));
  CHECK_EQ(childNode3, node->FindOrAddChild(&entry3));
72 73 74
}


75
TEST(ProfileNodeFindOrAddChildForSameFunction) {
76
  CcTest::InitializeVM();
77
  const char* aaa = "aaa";
78
  ProfileTree tree(CcTest::i_isolate());
79
  ProfileNode* node = tree.root();
80
  CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, aaa);
81
  ProfileNode* childNode1 = node->FindOrAddChild(&entry1);
82
  CHECK(childNode1);
83
  CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
84
  // The same function again.
85
  CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, aaa);
86
  CHECK_EQ(childNode1, node->FindOrAddChild(&entry2));
87
  // Now with a different security token.
88
  CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, aaa);
89
  CHECK_EQ(childNode1, node->FindOrAddChild(&entry3));
90 91 92
}


93 94 95 96
namespace {

class ProfileTreeTestHelper {
 public:
97
  explicit ProfileTreeTestHelper(const ProfileTree* tree)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      : tree_(tree) { }

  ProfileNode* Walk(CodeEntry* entry1,
                    CodeEntry* entry2 = NULL,
                    CodeEntry* entry3 = NULL) {
    ProfileNode* node = tree_->root();
    node = node->FindChild(entry1);
    if (node == NULL) return NULL;
    if (entry2 != NULL) {
      node = node->FindChild(entry2);
      if (node == NULL) return NULL;
    }
    if (entry3 != NULL) {
      node = node->FindChild(entry3);
    }
    return node;
  }

 private:
117
  const ProfileTree* tree_;
118 119 120 121 122 123
};

}  // namespace


TEST(ProfileTreeAddPathFromEnd) {
124
  CcTest::InitializeVM();
125 126 127
  CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
  CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
  CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
128
  ProfileTree tree(CcTest::i_isolate());
129
  ProfileTreeTestHelper helper(&tree);
130 131 132
  CHECK(!helper.Walk(&entry1));
  CHECK(!helper.Walk(&entry2));
  CHECK(!helper.Walk(&entry3));
133 134

  CodeEntry* path[] = {NULL, &entry3, NULL, &entry2, NULL, NULL, &entry1, NULL};
135
  std::vector<CodeEntry*> path_vec(path, path + arraysize(path));
136
  tree.AddPathFromEnd(path_vec);
137 138
  CHECK(!helper.Walk(&entry2));
  CHECK(!helper.Walk(&entry3));
139
  ProfileNode* node1 = helper.Walk(&entry1);
140 141 142 143
  CHECK(node1);
  CHECK_EQ(0u, node1->self_ticks());
  CHECK(!helper.Walk(&entry1, &entry1));
  CHECK(!helper.Walk(&entry1, &entry3));
144
  ProfileNode* node2 = helper.Walk(&entry1, &entry2);
145
  CHECK(node2);
146
  CHECK_NE(node1, node2);
147 148 149
  CHECK_EQ(0u, node2->self_ticks());
  CHECK(!helper.Walk(&entry1, &entry2, &entry1));
  CHECK(!helper.Walk(&entry1, &entry2, &entry2));
150
  ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3);
151
  CHECK(node3);
152 153
  CHECK_NE(node1, node3);
  CHECK_NE(node2, node3);
154
  CHECK_EQ(1u, node3->self_ticks());
155 156 157 158 159

  tree.AddPathFromEnd(path_vec);
  CHECK_EQ(node1, helper.Walk(&entry1));
  CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
  CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
160 161 162
  CHECK_EQ(0u, node1->self_ticks());
  CHECK_EQ(0u, node2->self_ticks());
  CHECK_EQ(2u, node3->self_ticks());
163 164

  CodeEntry* path2[] = {&entry2, &entry2, &entry1};
165
  std::vector<CodeEntry*> path2_vec(path2, path2 + arraysize(path2));
166
  tree.AddPathFromEnd(path2_vec);
167 168
  CHECK(!helper.Walk(&entry2));
  CHECK(!helper.Walk(&entry3));
169
  CHECK_EQ(node1, helper.Walk(&entry1));
170 171
  CHECK(!helper.Walk(&entry1, &entry1));
  CHECK(!helper.Walk(&entry1, &entry3));
172
  CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
173
  CHECK(!helper.Walk(&entry1, &entry2, &entry1));
174
  CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
175
  CHECK_EQ(2u, node3->self_ticks());
176
  ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2);
177
  CHECK(node4);
178
  CHECK_NE(node3, node4);
179
  CHECK_EQ(1u, node4->self_ticks());
180 181 182 183
}


TEST(ProfileTreeCalculateTotalTicks) {
184 185
  CcTest::InitializeVM();
  ProfileTree empty_tree(CcTest::i_isolate());
186
  CHECK_EQ(0u, empty_tree.root()->self_ticks());
187
  empty_tree.root()->IncrementSelfTicks();
188
  CHECK_EQ(1u, empty_tree.root()->self_ticks());
189

190
  CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
191
  CodeEntry* e1_path[] = {&entry1};
192
  std::vector<CodeEntry*> e1_path_vec(e1_path, e1_path + arraysize(e1_path));
193

194
  ProfileTree single_child_tree(CcTest::i_isolate());
195
  single_child_tree.AddPathFromEnd(e1_path_vec);
196
  single_child_tree.root()->IncrementSelfTicks();
197
  CHECK_EQ(1u, single_child_tree.root()->self_ticks());
198 199
  ProfileTreeTestHelper single_child_helper(&single_child_tree);
  ProfileNode* node1 = single_child_helper.Walk(&entry1);
200 201 202
  CHECK(node1);
  CHECK_EQ(1u, single_child_tree.root()->self_ticks());
  CHECK_EQ(1u, node1->self_ticks());
203

204
  CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
205
  CodeEntry* e2_e1_path[] = {&entry2, &entry1};
206 207
  std::vector<CodeEntry*> e2_e1_path_vec(e2_e1_path,
                                         e2_e1_path + arraysize(e2_e1_path));
208

209
  ProfileTree flat_tree(CcTest::i_isolate());
210
  ProfileTreeTestHelper flat_helper(&flat_tree);
211 212 213 214 215
  flat_tree.AddPathFromEnd(e1_path_vec);
  flat_tree.AddPathFromEnd(e1_path_vec);
  flat_tree.AddPathFromEnd(e2_e1_path_vec);
  flat_tree.AddPathFromEnd(e2_e1_path_vec);
  flat_tree.AddPathFromEnd(e2_e1_path_vec);
216
  // Results in {root,0,0} -> {entry1,0,2} -> {entry2,0,3}
217
  CHECK_EQ(0u, flat_tree.root()->self_ticks());
218
  node1 = flat_helper.Walk(&entry1);
219 220
  CHECK(node1);
  CHECK_EQ(2u, node1->self_ticks());
221
  ProfileNode* node2 = flat_helper.Walk(&entry1, &entry2);
222 223
  CHECK(node2);
  CHECK_EQ(3u, node2->self_ticks());
224
  // Must calculate {root,5,0} -> {entry1,5,2} -> {entry2,3,3}
225 226
  CHECK_EQ(0u, flat_tree.root()->self_ticks());
  CHECK_EQ(2u, node1->self_ticks());
227 228

  CodeEntry* e2_path[] = {&entry2};
229
  std::vector<CodeEntry*> e2_path_vec(e2_path, e2_path + arraysize(e2_path));
230
  CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
231
  CodeEntry* e3_path[] = {&entry3};
232
  std::vector<CodeEntry*> e3_path_vec(e3_path, e3_path + arraysize(e3_path));
233

234
  ProfileTree wide_tree(CcTest::i_isolate());
235
  ProfileTreeTestHelper wide_helper(&wide_tree);
236 237 238 239 240 241 242 243 244 245
  wide_tree.AddPathFromEnd(e1_path_vec);
  wide_tree.AddPathFromEnd(e1_path_vec);
  wide_tree.AddPathFromEnd(e2_e1_path_vec);
  wide_tree.AddPathFromEnd(e2_path_vec);
  wide_tree.AddPathFromEnd(e2_path_vec);
  wide_tree.AddPathFromEnd(e2_path_vec);
  wide_tree.AddPathFromEnd(e3_path_vec);
  wide_tree.AddPathFromEnd(e3_path_vec);
  wide_tree.AddPathFromEnd(e3_path_vec);
  wide_tree.AddPathFromEnd(e3_path_vec);
246 247 248
  // Results in            -> {entry1,0,2} -> {entry2,0,1}
  //            {root,0,0} -> {entry2,0,3}
  //                       -> {entry3,0,4}
249
  CHECK_EQ(0u, wide_tree.root()->self_ticks());
250
  node1 = wide_helper.Walk(&entry1);
251 252
  CHECK(node1);
  CHECK_EQ(2u, node1->self_ticks());
253
  ProfileNode* node1_2 = wide_helper.Walk(&entry1, &entry2);
254 255
  CHECK(node1_2);
  CHECK_EQ(1u, node1_2->self_ticks());
256
  node2 = wide_helper.Walk(&entry2);
257 258
  CHECK(node2);
  CHECK_EQ(3u, node2->self_ticks());
259
  ProfileNode* node3 = wide_helper.Walk(&entry3);
260 261
  CHECK(node3);
  CHECK_EQ(4u, node3->self_ticks());
262 263 264
  // Calculates             -> {entry1,3,2} -> {entry2,1,1}
  //            {root,10,0} -> {entry2,3,3}
  //                        -> {entry3,4,4}
265 266 267 268 269
  CHECK_EQ(0u, wide_tree.root()->self_ticks());
  CHECK_EQ(2u, node1->self_ticks());
  CHECK_EQ(1u, node1_2->self_ticks());
  CHECK_EQ(3u, node2->self_ticks());
  CHECK_EQ(4u, node3->self_ticks());
270 271 272 273 274 275 276
}


static inline i::Address ToAddress(int n) {
  return reinterpret_cast<i::Address>(n);
}

277

278 279
TEST(CodeMapAddCode) {
  CodeMap code_map;
280 281 282 283
  CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
  CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
  CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
  CodeEntry entry4(i::CodeEventListener::FUNCTION_TAG, "ddd");
284 285 286 287
  code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
  code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
  code_map.AddCode(ToAddress(0x1900), &entry3, 0x50);
  code_map.AddCode(ToAddress(0x1950), &entry4, 0x10);
288 289
  CHECK(!code_map.FindEntry(0));
  CHECK(!code_map.FindEntry(ToAddress(0x1500 - 1)));
290 291 292 293 294 295
  CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
  CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x100)));
  CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x200 - 1)));
  CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
  CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x50)));
  CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x100 - 1)));
296 297
  CHECK(!code_map.FindEntry(ToAddress(0x1700 + 0x100)));
  CHECK(!code_map.FindEntry(ToAddress(0x1900 - 1)));
298 299 300 301 302
  CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900)));
  CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900 + 0x28)));
  CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950)));
  CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x7)));
  CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x10 - 1)));
303 304
  CHECK(!code_map.FindEntry(ToAddress(0x1950 + 0x10)));
  CHECK(!code_map.FindEntry(ToAddress(0xFFFFFFFF)));
305 306 307 308 309
}


TEST(CodeMapMoveAndDeleteCode) {
  CodeMap code_map;
310 311
  CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
  CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
312 313 314 315
  code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
  code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
  CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
  CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
316
  code_map.MoveCode(ToAddress(0x1500), ToAddress(0x1700));  // Deprecate bbb.
317
  CHECK(!code_map.FindEntry(ToAddress(0x1500)));
318
  CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1700)));
319
  CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
320
  code_map.AddCode(ToAddress(0x1750), &entry3, 0x100);
321
  CHECK(!code_map.FindEntry(ToAddress(0x1700)));
322
  CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1750)));
323
}
324 325


326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
namespace {

class TestSetup {
 public:
  TestSetup()
      : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
    i::FLAG_prof_browser_mode = false;
  }

  ~TestSetup() {
    i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
  }

 private:
  bool old_flag_prof_browser_mode_;
};

}  // namespace

345
TEST(RecordTickSample) {
346
  TestSetup test_setup;
347
  CpuProfilesCollection profiles(CcTest::i_isolate());
348 349
  CpuProfiler profiler(CcTest::i_isolate());
  profiles.set_cpu_profiler(&profiler);
350
  profiles.StartProfiling("", false);
351
  ProfileGenerator generator(&profiles);
lpy's avatar
lpy committed
352 353 354
  CodeEntry* entry1 = new CodeEntry(i::Logger::FUNCTION_TAG, "aaa");
  CodeEntry* entry2 = new CodeEntry(i::Logger::FUNCTION_TAG, "bbb");
  CodeEntry* entry3 = new CodeEntry(i::Logger::FUNCTION_TAG, "ccc");
355 356 357 358 359 360 361 362 363 364
  generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
  generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
  generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);

  // We are building the following calls tree:
  //      -> aaa         - sample1
  //  aaa -> bbb -> ccc  - sample2
  //      -> ccc -> aaa  - sample3
  TickSample sample1;
  sample1.pc = ToAddress(0x1600);
365
  sample1.tos = ToAddress(0x1500);
366 367 368 369 370
  sample1.stack[0] = ToAddress(0x1510);
  sample1.frames_count = 1;
  generator.RecordTickSample(sample1);
  TickSample sample2;
  sample2.pc = ToAddress(0x1925);
371
  sample2.tos = ToAddress(0x1900);
372 373 374 375 376 377 378
  sample2.stack[0] = ToAddress(0x1780);
  sample2.stack[1] = ToAddress(0x10000);  // non-existent.
  sample2.stack[2] = ToAddress(0x1620);
  sample2.frames_count = 3;
  generator.RecordTickSample(sample2);
  TickSample sample3;
  sample3.pc = ToAddress(0x1510);
379
  sample3.tos = ToAddress(0x1500);
380 381 382 383 384
  sample3.stack[0] = ToAddress(0x1910);
  sample3.stack[1] = ToAddress(0x1610);
  sample3.frames_count = 2;
  generator.RecordTickSample(sample3);

385
  CpuProfile* profile = profiles.StopProfiling("");
386
  CHECK(profile);
387
  ProfileTreeTestHelper top_down_test_helper(profile->top_down());
388 389
  CHECK(!top_down_test_helper.Walk(entry2));
  CHECK(!top_down_test_helper.Walk(entry3));
390
  ProfileNode* node1 = top_down_test_helper.Walk(entry1);
391
  CHECK(node1);
392 393
  CHECK_EQ(entry1, node1->entry());
  ProfileNode* node2 = top_down_test_helper.Walk(entry1, entry1);
394
  CHECK(node2);
395
  CHECK_EQ(entry1, node2->entry());
396
  ProfileNode* node3 = top_down_test_helper.Walk(entry1, entry2, entry3);
397
  CHECK(node3);
398
  CHECK_EQ(entry3, node3->entry());
399
  ProfileNode* node4 = top_down_test_helper.Walk(entry1, entry3, entry1);
400
  CHECK(node4);
401
  CHECK_EQ(entry1, node4->entry());
lpy's avatar
lpy committed
402 403 404 405

  delete entry1;
  delete entry2;
  delete entry3;
406
}
407

408

409
static void CheckNodeIds(ProfileNode* node, unsigned* expectedId) {
410 411 412 413 414 415
  CHECK_EQ((*expectedId)++, node->id());
  for (int i = 0; i < node->children()->length(); i++) {
    CheckNodeIds(node->children()->at(i), expectedId);
  }
}

416

417 418
TEST(SampleIds) {
  TestSetup test_setup;
419
  CpuProfilesCollection profiles(CcTest::i_isolate());
420 421
  CpuProfiler profiler(CcTest::i_isolate());
  profiles.set_cpu_profiler(&profiler);
422
  profiles.StartProfiling("", true);
423
  ProfileGenerator generator(&profiles);
lpy's avatar
lpy committed
424 425 426
  CodeEntry* entry1 = new CodeEntry(i::Logger::FUNCTION_TAG, "aaa");
  CodeEntry* entry2 = new CodeEntry(i::Logger::FUNCTION_TAG, "bbb");
  CodeEntry* entry3 = new CodeEntry(i::Logger::FUNCTION_TAG, "ccc");
427 428 429 430 431 432 433 434 435
  generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
  generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
  generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);

  // We are building the following calls tree:
  //                    -> aaa #3           - sample1
  // (root)#1 -> aaa #2 -> bbb #4 -> ccc #5 - sample2
  //                    -> ccc #6 -> aaa #7 - sample3
  TickSample sample1;
436
  sample1.timestamp = v8::base::TimeTicks::HighResolutionNow();
437 438 439 440 441
  sample1.pc = ToAddress(0x1600);
  sample1.stack[0] = ToAddress(0x1510);
  sample1.frames_count = 1;
  generator.RecordTickSample(sample1);
  TickSample sample2;
442
  sample2.timestamp = v8::base::TimeTicks::HighResolutionNow();
443 444 445 446 447 448 449
  sample2.pc = ToAddress(0x1925);
  sample2.stack[0] = ToAddress(0x1780);
  sample2.stack[1] = ToAddress(0x10000);  // non-existent.
  sample2.stack[2] = ToAddress(0x1620);
  sample2.frames_count = 3;
  generator.RecordTickSample(sample2);
  TickSample sample3;
450
  sample3.timestamp = v8::base::TimeTicks::HighResolutionNow();
451 452 453 454 455 456
  sample3.pc = ToAddress(0x1510);
  sample3.stack[0] = ToAddress(0x1910);
  sample3.stack[1] = ToAddress(0x1610);
  sample3.frames_count = 2;
  generator.RecordTickSample(sample3);

457
  CpuProfile* profile = profiles.StopProfiling("");
458
  unsigned nodeId = 1;
459
  CheckNodeIds(profile->top_down()->root(), &nodeId);
460
  CHECK_EQ(7u, nodeId - 1);
461 462

  CHECK_EQ(3, profile->samples_count());
463
  unsigned expected_id[] = {3, 5, 7};
464 465 466
  for (int i = 0; i < 3; i++) {
    CHECK_EQ(expected_id[i], profile->sample(i)->id());
  }
lpy's avatar
lpy committed
467 468 469 470

  delete entry1;
  delete entry2;
  delete entry3;
471 472 473 474 475
}


TEST(NoSamples) {
  TestSetup test_setup;
476
  CpuProfilesCollection profiles(CcTest::i_isolate());
477 478
  CpuProfiler profiler(CcTest::i_isolate());
  profiles.set_cpu_profiler(&profiler);
479
  profiles.StartProfiling("", false);
480
  ProfileGenerator generator(&profiles);
lpy's avatar
lpy committed
481
  CodeEntry* entry1 = new CodeEntry(i::Logger::FUNCTION_TAG, "aaa");
482 483 484 485 486 487 488 489 490 491
  generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);

  // We are building the following calls tree:
  // (root)#1 -> aaa #2 -> aaa #3 - sample1
  TickSample sample1;
  sample1.pc = ToAddress(0x1600);
  sample1.stack[0] = ToAddress(0x1510);
  sample1.frames_count = 1;
  generator.RecordTickSample(sample1);

492
  CpuProfile* profile = profiles.StopProfiling("");
493
  unsigned nodeId = 1;
494
  CheckNodeIds(profile->top_down()->root(), &nodeId);
495
  CHECK_EQ(3u, nodeId - 1);
496 497

  CHECK_EQ(0, profile->samples_count());
lpy's avatar
lpy committed
498 499

  delete entry1;
500 501 502
}


503 504 505 506 507 508 509 510 511 512 513
static const ProfileNode* PickChild(const ProfileNode* parent,
                                    const char* name) {
  for (int i = 0; i < parent->children()->length(); ++i) {
    const ProfileNode* child = parent->children()->at(i);
    if (strcmp(child->entry()->name(), name) == 0) return child;
  }
  return NULL;
}


TEST(RecordStackTraceAtStartProfiling) {
514 515
  // This test does not pass with inlining enabled since inlined functions
  // don't appear in the stack trace.
516
  i::FLAG_turbo_inlining = false;
517 518
  i::FLAG_use_inlining = false;

519 520 521
  v8::HandleScope scope(CcTest::isolate());
  v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
  v8::Context::Scope context_scope(env);
522 523 524
  std::unique_ptr<i::CpuProfiler> iprofiler(
      new i::CpuProfiler(CcTest::i_isolate()));
  i::ProfilerExtension::set_profiler(iprofiler.get());
525 526 527 528 529 530 531

  CompileRun(
      "function c() { startProfiling(); }\n"
      "function b() { c(); }\n"
      "function a() { b(); }\n"
      "a();\n"
      "stopProfiling();");
532 533
  CHECK_EQ(1, iprofiler->GetProfilesCount());
  CpuProfile* profile = iprofiler->GetProfile(0);
534 535
  const ProfileTree* topDown = profile->top_down();
  const ProfileNode* current = topDown->root();
536
  const_cast<ProfileNode*>(current)->Print(0);
537 538
  // The tree should look like this:
  //  (root)
539
  //   ""
540 541 542
  //     a
  //       b
  //         c
543 544 545
  // There can also be:
  //           startProfiling
  // if the sampler managed to get a tick.
546
  current = PickChild(current, "");
547
  CHECK(const_cast<ProfileNode*>(current));
548
  current = PickChild(current, "a");
549
  CHECK(const_cast<ProfileNode*>(current));
550
  current = PickChild(current, "b");
551
  CHECK(const_cast<ProfileNode*>(current));
552
  current = PickChild(current, "c");
553
  CHECK(const_cast<ProfileNode*>(current));
554 555 556 557 558 559
  CHECK(current->children()->length() == 0 ||
        current->children()->length() == 1);
  if (current->children()->length() == 1) {
    current = PickChild(current, "startProfiling");
    CHECK_EQ(0, current->children()->length());
  }
560 561
}

562 563

TEST(Issue51919) {
564
  CpuProfilesCollection collection(CcTest::i_isolate());
565 566
  CpuProfiler profiler(CcTest::i_isolate());
  collection.set_cpu_profiler(&profiler);
567 568 569 570
  i::EmbeddedVector<char*,
      CpuProfilesCollection::kMaxSimultaneousProfiles> titles;
  for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) {
    i::Vector<char> title = i::Vector<char>::New(16);
571
    i::SNPrintF(title, "%d", i);
572
    CHECK(collection.StartProfiling(title.start(), false));
573 574
    titles[i] = title.start();
  }
575
  CHECK(!collection.StartProfiling("maximum", false));
576 577 578
  for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i)
    i::DeleteArray(titles[i]);
}
579 580 581 582 583 584


static const v8::CpuProfileNode* PickChild(const v8::CpuProfileNode* parent,
                                           const char* name) {
  for (int i = 0; i < parent->GetChildrenCount(); ++i) {
    const v8::CpuProfileNode* child = parent->GetChild(i);
585
    v8::String::Utf8Value function_name(child->GetFunctionName());
586 587 588 589 590 591 592 593 594
    if (strcmp(*function_name, name) == 0) return child;
  }
  return NULL;
}


TEST(ProfileNodeScriptId) {
  // This test does not pass with inlining enabled since inlined functions
  // don't appear in the stack trace.
595
  i::FLAG_turbo_inlining = false;
596 597
  i::FLAG_use_inlining = false;

598 599 600
  v8::HandleScope scope(CcTest::isolate());
  v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
  v8::Context::Scope context_scope(env);
601 602
  std::unique_ptr<CpuProfiler> iprofiler(new CpuProfiler(CcTest::i_isolate()));
  i::ProfilerExtension::set_profiler(iprofiler.get());
603

604 605 606 607 608 609 610 611 612 613
  v8::Local<v8::Script> script_a =
      v8_compile(v8_str("function a() { startProfiling(); }\n"));
  script_a->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
      .ToLocalChecked();
  v8::Local<v8::Script> script_b =
      v8_compile(v8_str("function b() { a(); }\n"
                        "b();\n"
                        "stopProfiling();\n"));
  script_b->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
      .ToLocalChecked();
614
  CHECK_EQ(1, iprofiler->GetProfilesCount());
615
  const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
616 617 618 619 620
  const v8::CpuProfileNode* current = profile->GetTopDownRoot();
  reinterpret_cast<ProfileNode*>(
      const_cast<v8::CpuProfileNode*>(current))->Print(0);
  // The tree should look like this:
  //  (root)
621
  //   ""
622 623 624 625 626
  //     b
  //       a
  // There can also be:
  //         startProfiling
  // if the sampler managed to get a tick.
627
  current = PickChild(current, "");
628
  CHECK(const_cast<v8::CpuProfileNode*>(current));
629 630

  current = PickChild(current, "b");
631
  CHECK(const_cast<v8::CpuProfileNode*>(current));
632
  CHECK_EQ(script_b->GetUnboundScript()->GetId(), current->GetScriptId());
633 634

  current = PickChild(current, "a");
635
  CHECK(const_cast<v8::CpuProfileNode*>(current));
636
  CHECK_EQ(script_a->GetUnboundScript()->GetId(), current->GetScriptId());
637 638
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652
static const char* line_number_test_source_existing_functions =
"function foo_at_the_first_line() {\n"
"}\n"
"foo_at_the_first_line();\n"
"function lazy_func_at_forth_line() {}\n";

static const char* line_number_test_source_profile_time_functions =
"// Empty first line\n"
"function bar_at_the_second_line() {\n"
"  foo_at_the_first_line();\n"
"}\n"
"bar_at_the_second_line();\n"
"function lazy_func_at_6th_line() {}";

653 654 655
int GetFunctionLineNumber(CpuProfiler& profiler, LocalContext& env,
                          const char* name) {
  CodeMap* code_map = profiler.generator()->code_map();
656
  i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
657
      v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
658
          env->Global()->Get(env.local(), v8_str(name)).ToLocalChecked())));
659
  CodeEntry* func_entry = code_map->FindEntry(func->abstract_code()->address());
660 661 662 663 664 665 666 667 668 669
  if (!func_entry)
    FATAL(name);
  return func_entry->line_number();
}

TEST(LineNumber) {
  i::FLAG_use_inlining = false;

  CcTest::InitializeVM();
  LocalContext env;
670
  i::Isolate* isolate = CcTest::i_isolate();
671 672 673 674 675 676
  TestSetup test_setup;

  i::HandleScope scope(isolate);

  CompileRun(line_number_test_source_existing_functions);

677 678
  CpuProfiler profiler(isolate);
  profiler.StartProfiling("LineNumber");
679 680 681

  CompileRun(line_number_test_source_profile_time_functions);

682
  profiler.processor()->StopSynchronously();
683

684
  bool is_lazy = i::FLAG_lazy && !(i::FLAG_ignition && i::FLAG_ignition_eager);
685
  CHECK_EQ(1, GetFunctionLineNumber(profiler, env, "foo_at_the_first_line"));
686
  CHECK_EQ(is_lazy ? 0 : 4,
687 688
           GetFunctionLineNumber(profiler, env, "lazy_func_at_forth_line"));
  CHECK_EQ(2, GetFunctionLineNumber(profiler, env, "bar_at_the_second_line"));
689
  CHECK_EQ(is_lazy ? 0 : 6,
690
           GetFunctionLineNumber(profiler, env, "lazy_func_at_6th_line"));
691

692
  profiler.StopProfiling("LineNumber");
693
}
694 695

TEST(BailoutReason) {
696 697 698
  v8::HandleScope scope(CcTest::isolate());
  v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
  v8::Context::Scope context_scope(env);
699 700
  std::unique_ptr<CpuProfiler> iprofiler(new CpuProfiler(CcTest::i_isolate()));
  i::ProfilerExtension::set_profiler(iprofiler.get());
701

702
  CHECK_EQ(0, iprofiler->GetProfilesCount());
703 704 705 706 707
  v8::Local<v8::Script> script =
      v8_compile(v8_str("function Debugger() {\n"
                        "  debugger;\n"
                        "  startProfiling();\n"
                        "}\n"
708
                        "Debugger();\n"
709 710
                        "stopProfiling();"));
  script->Run(v8::Isolate::GetCurrent()->GetCurrentContext()).ToLocalChecked();
711
  CHECK_EQ(1, iprofiler->GetProfilesCount());
712
  const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
713
  CHECK(profile);
714 715 716 717 718
  const v8::CpuProfileNode* current = profile->GetTopDownRoot();
  reinterpret_cast<ProfileNode*>(
      const_cast<v8::CpuProfileNode*>(current))->Print(0);
  // The tree should look like this:
  //  (root)
719
  //   ""
720
  //     kDebuggerStatement
721
  current = PickChild(current, "");
722
  CHECK(const_cast<v8::CpuProfileNode*>(current));
723

724
  current = PickChild(current, "Debugger");
725
  CHECK(const_cast<v8::CpuProfileNode*>(current));
726
  CHECK(!strcmp("DebuggerStatement", current->GetBailoutReason()));
727
}