blob: 48633f5da1eb07a13f58d5540a8d20eeeeb0e97f [file] [log] [blame]
Steve Block6ded16b2010-05-10 14:33:55 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +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.
Steve Block6ded16b2010-05-10 14:33:55 +010027//
28// Tests of profiles generator and utilities.
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/v8.h"
31
32#include "include/v8-profiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033#include "src/profiler/cpu-profiler.h"
34#include "src/profiler/profile-generator-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035#include "test/cctest/cctest.h"
36#include "test/cctest/profiler-extension.h"
Steve Block6ded16b2010-05-10 14:33:55 +010037
Steve Block6ded16b2010-05-10 14:33:55 +010038using i::CodeEntry;
39using i::CodeMap;
40using i::CpuProfile;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010041using i::CpuProfiler;
Steve Block6ded16b2010-05-10 14:33:55 +010042using i::CpuProfilesCollection;
43using i::ProfileNode;
44using i::ProfileTree;
45using i::ProfileGenerator;
Steve Block6ded16b2010-05-10 14:33:55 +010046using i::TickSample;
47using i::Vector;
48
49
50TEST(ProfileNodeFindOrAddChild) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051 CcTest::InitializeVM();
52 ProfileTree tree(CcTest::i_isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 ProfileNode* node = tree.root();
54 CodeEntry entry1(i::Logger::FUNCTION_TAG, "aaa");
55 ProfileNode* childNode1 = node->FindOrAddChild(&entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 CHECK(childNode1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
58 CodeEntry entry2(i::Logger::FUNCTION_TAG, "bbb");
59 ProfileNode* childNode2 = node->FindOrAddChild(&entry2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 CHECK(childNode2);
Steve Block6ded16b2010-05-10 14:33:55 +010061 CHECK_NE(childNode1, childNode2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
63 CHECK_EQ(childNode2, node->FindOrAddChild(&entry2));
64 CodeEntry entry3(i::Logger::FUNCTION_TAG, "ccc");
65 ProfileNode* childNode3 = node->FindOrAddChild(&entry3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 CHECK(childNode3);
Steve Block6ded16b2010-05-10 14:33:55 +010067 CHECK_NE(childNode1, childNode3);
68 CHECK_NE(childNode2, childNode3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
70 CHECK_EQ(childNode2, node->FindOrAddChild(&entry2));
71 CHECK_EQ(childNode3, node->FindOrAddChild(&entry3));
Steve Block6ded16b2010-05-10 14:33:55 +010072}
73
74
Kristian Monsen0d5e1162010-09-30 15:31:59 +010075TEST(ProfileNodeFindOrAddChildForSameFunction) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 CcTest::InitializeVM();
Kristian Monsen0d5e1162010-09-30 15:31:59 +010077 const char* aaa = "aaa";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 ProfileTree tree(CcTest::i_isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 ProfileNode* node = tree.root();
80 CodeEntry entry1(i::Logger::FUNCTION_TAG, aaa);
81 ProfileNode* childNode1 = node->FindOrAddChild(&entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000082 CHECK(childNode1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 CHECK_EQ(childNode1, node->FindOrAddChild(&entry1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +010084 // The same function again.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 CodeEntry entry2(i::Logger::FUNCTION_TAG, aaa);
86 CHECK_EQ(childNode1, node->FindOrAddChild(&entry2));
Kristian Monsen0d5e1162010-09-30 15:31:59 +010087 // Now with a different security token.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 CodeEntry entry3(i::Logger::FUNCTION_TAG, aaa);
89 CHECK_EQ(childNode1, node->FindOrAddChild(&entry3));
Kristian Monsen0d5e1162010-09-30 15:31:59 +010090}
91
92
Steve Block6ded16b2010-05-10 14:33:55 +010093namespace {
94
95class ProfileTreeTestHelper {
96 public:
97 explicit ProfileTreeTestHelper(const ProfileTree* tree)
98 : tree_(tree) { }
99
100 ProfileNode* Walk(CodeEntry* entry1,
101 CodeEntry* entry2 = NULL,
102 CodeEntry* entry3 = NULL) {
103 ProfileNode* node = tree_->root();
104 node = node->FindChild(entry1);
105 if (node == NULL) return NULL;
106 if (entry2 != NULL) {
107 node = node->FindChild(entry2);
108 if (node == NULL) return NULL;
109 }
110 if (entry3 != NULL) {
111 node = node->FindChild(entry3);
112 }
113 return node;
114 }
115
116 private:
117 const ProfileTree* tree_;
118};
119
120} // namespace
121
Steve Block6ded16b2010-05-10 14:33:55 +0100122
123TEST(ProfileTreeAddPathFromEnd) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124 CcTest::InitializeVM();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125 CodeEntry entry1(i::Logger::FUNCTION_TAG, "aaa");
126 CodeEntry entry2(i::Logger::FUNCTION_TAG, "bbb");
127 CodeEntry entry3(i::Logger::FUNCTION_TAG, "ccc");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 ProfileTree tree(CcTest::i_isolate());
Steve Block6ded16b2010-05-10 14:33:55 +0100129 ProfileTreeTestHelper helper(&tree);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 CHECK(!helper.Walk(&entry1));
131 CHECK(!helper.Walk(&entry2));
132 CHECK(!helper.Walk(&entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100133
134 CodeEntry* path[] = {NULL, &entry3, NULL, &entry2, NULL, NULL, &entry1, NULL};
Ben Murdochda12d292016-06-02 14:46:10 +0100135 std::vector<CodeEntry*> path_vec(path, path + arraysize(path));
Steve Block6ded16b2010-05-10 14:33:55 +0100136 tree.AddPathFromEnd(path_vec);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 CHECK(!helper.Walk(&entry2));
138 CHECK(!helper.Walk(&entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100139 ProfileNode* node1 = helper.Walk(&entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 CHECK(node1);
141 CHECK_EQ(0u, node1->self_ticks());
142 CHECK(!helper.Walk(&entry1, &entry1));
143 CHECK(!helper.Walk(&entry1, &entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100144 ProfileNode* node2 = helper.Walk(&entry1, &entry2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000145 CHECK(node2);
Steve Block6ded16b2010-05-10 14:33:55 +0100146 CHECK_NE(node1, node2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 CHECK_EQ(0u, node2->self_ticks());
148 CHECK(!helper.Walk(&entry1, &entry2, &entry1));
149 CHECK(!helper.Walk(&entry1, &entry2, &entry2));
Steve Block6ded16b2010-05-10 14:33:55 +0100150 ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 CHECK(node3);
Steve Block6ded16b2010-05-10 14:33:55 +0100152 CHECK_NE(node1, node3);
153 CHECK_NE(node2, node3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 CHECK_EQ(1u, node3->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100155
156 tree.AddPathFromEnd(path_vec);
157 CHECK_EQ(node1, helper.Walk(&entry1));
158 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
159 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160 CHECK_EQ(0u, node1->self_ticks());
161 CHECK_EQ(0u, node2->self_ticks());
162 CHECK_EQ(2u, node3->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100163
164 CodeEntry* path2[] = {&entry2, &entry2, &entry1};
Ben Murdochda12d292016-06-02 14:46:10 +0100165 std::vector<CodeEntry*> path2_vec(path2, path2 + arraysize(path2));
Steve Block6ded16b2010-05-10 14:33:55 +0100166 tree.AddPathFromEnd(path2_vec);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 CHECK(!helper.Walk(&entry2));
168 CHECK(!helper.Walk(&entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100169 CHECK_EQ(node1, helper.Walk(&entry1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170 CHECK(!helper.Walk(&entry1, &entry1));
171 CHECK(!helper.Walk(&entry1, &entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100172 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 CHECK(!helper.Walk(&entry1, &entry2, &entry1));
Steve Block6ded16b2010-05-10 14:33:55 +0100174 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175 CHECK_EQ(2u, node3->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100176 ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177 CHECK(node4);
Steve Block6ded16b2010-05-10 14:33:55 +0100178 CHECK_NE(node3, node4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179 CHECK_EQ(1u, node4->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100180}
181
182
183TEST(ProfileTreeCalculateTotalTicks) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 CcTest::InitializeVM();
185 ProfileTree empty_tree(CcTest::i_isolate());
186 CHECK_EQ(0u, empty_tree.root()->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100187 empty_tree.root()->IncrementSelfTicks();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 CHECK_EQ(1u, empty_tree.root()->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100189
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 CodeEntry entry1(i::Logger::FUNCTION_TAG, "aaa");
Steve Block6ded16b2010-05-10 14:33:55 +0100191 CodeEntry* e1_path[] = {&entry1};
Ben Murdochda12d292016-06-02 14:46:10 +0100192 std::vector<CodeEntry*> e1_path_vec(e1_path, e1_path + arraysize(e1_path));
Leon Clarkef7060e22010-06-03 12:02:55 +0100193
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194 ProfileTree single_child_tree(CcTest::i_isolate());
195 single_child_tree.AddPathFromEnd(e1_path_vec);
Leon Clarkef7060e22010-06-03 12:02:55 +0100196 single_child_tree.root()->IncrementSelfTicks();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 CHECK_EQ(1u, single_child_tree.root()->self_ticks());
Leon Clarkef7060e22010-06-03 12:02:55 +0100198 ProfileTreeTestHelper single_child_helper(&single_child_tree);
199 ProfileNode* node1 = single_child_helper.Walk(&entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200 CHECK(node1);
201 CHECK_EQ(1u, single_child_tree.root()->self_ticks());
202 CHECK_EQ(1u, node1->self_ticks());
Leon Clarkef7060e22010-06-03 12:02:55 +0100203
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 CodeEntry entry2(i::Logger::FUNCTION_TAG, "bbb");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205 CodeEntry* e2_e1_path[] = {&entry2, &entry1};
Ben Murdochda12d292016-06-02 14:46:10 +0100206 std::vector<CodeEntry*> e2_e1_path_vec(e2_e1_path,
207 e2_e1_path + arraysize(e2_e1_path));
Steve Block6ded16b2010-05-10 14:33:55 +0100208
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209 ProfileTree flat_tree(CcTest::i_isolate());
Steve Block6ded16b2010-05-10 14:33:55 +0100210 ProfileTreeTestHelper flat_helper(&flat_tree);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000211 flat_tree.AddPathFromEnd(e1_path_vec);
212 flat_tree.AddPathFromEnd(e1_path_vec);
213 flat_tree.AddPathFromEnd(e2_e1_path_vec);
214 flat_tree.AddPathFromEnd(e2_e1_path_vec);
215 flat_tree.AddPathFromEnd(e2_e1_path_vec);
Steve Block6ded16b2010-05-10 14:33:55 +0100216 // Results in {root,0,0} -> {entry1,0,2} -> {entry2,0,3}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217 CHECK_EQ(0u, flat_tree.root()->self_ticks());
Leon Clarkef7060e22010-06-03 12:02:55 +0100218 node1 = flat_helper.Walk(&entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000219 CHECK(node1);
220 CHECK_EQ(2u, node1->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100221 ProfileNode* node2 = flat_helper.Walk(&entry1, &entry2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000222 CHECK(node2);
223 CHECK_EQ(3u, node2->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100224 // Must calculate {root,5,0} -> {entry1,5,2} -> {entry2,3,3}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000225 CHECK_EQ(0u, flat_tree.root()->self_ticks());
226 CHECK_EQ(2u, node1->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100227
228 CodeEntry* e2_path[] = {&entry2};
Ben Murdochda12d292016-06-02 14:46:10 +0100229 std::vector<CodeEntry*> e2_path_vec(e2_path, e2_path + arraysize(e2_path));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 CodeEntry entry3(i::Logger::FUNCTION_TAG, "ccc");
Steve Block6ded16b2010-05-10 14:33:55 +0100231 CodeEntry* e3_path[] = {&entry3};
Ben Murdochda12d292016-06-02 14:46:10 +0100232 std::vector<CodeEntry*> e3_path_vec(e3_path, e3_path + arraysize(e3_path));
Steve Block6ded16b2010-05-10 14:33:55 +0100233
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234 ProfileTree wide_tree(CcTest::i_isolate());
Steve Block6ded16b2010-05-10 14:33:55 +0100235 ProfileTreeTestHelper wide_helper(&wide_tree);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000236 wide_tree.AddPathFromEnd(e1_path_vec);
237 wide_tree.AddPathFromEnd(e1_path_vec);
238 wide_tree.AddPathFromEnd(e2_e1_path_vec);
239 wide_tree.AddPathFromEnd(e2_path_vec);
240 wide_tree.AddPathFromEnd(e2_path_vec);
241 wide_tree.AddPathFromEnd(e2_path_vec);
242 wide_tree.AddPathFromEnd(e3_path_vec);
243 wide_tree.AddPathFromEnd(e3_path_vec);
244 wide_tree.AddPathFromEnd(e3_path_vec);
245 wide_tree.AddPathFromEnd(e3_path_vec);
Steve Block6ded16b2010-05-10 14:33:55 +0100246 // Results in -> {entry1,0,2} -> {entry2,0,1}
247 // {root,0,0} -> {entry2,0,3}
248 // -> {entry3,0,4}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 CHECK_EQ(0u, wide_tree.root()->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100250 node1 = wide_helper.Walk(&entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000251 CHECK(node1);
252 CHECK_EQ(2u, node1->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100253 ProfileNode* node1_2 = wide_helper.Walk(&entry1, &entry2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 CHECK(node1_2);
255 CHECK_EQ(1u, node1_2->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100256 node2 = wide_helper.Walk(&entry2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 CHECK(node2);
258 CHECK_EQ(3u, node2->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100259 ProfileNode* node3 = wide_helper.Walk(&entry3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000260 CHECK(node3);
261 CHECK_EQ(4u, node3->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100262 // Calculates -> {entry1,3,2} -> {entry2,1,1}
263 // {root,10,0} -> {entry2,3,3}
264 // -> {entry3,4,4}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265 CHECK_EQ(0u, wide_tree.root()->self_ticks());
266 CHECK_EQ(2u, node1->self_ticks());
267 CHECK_EQ(1u, node1_2->self_ticks());
268 CHECK_EQ(3u, node2->self_ticks());
269 CHECK_EQ(4u, node3->self_ticks());
Steve Block6ded16b2010-05-10 14:33:55 +0100270}
271
272
273static inline i::Address ToAddress(int n) {
274 return reinterpret_cast<i::Address>(n);
275}
276
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277
Steve Block6ded16b2010-05-10 14:33:55 +0100278TEST(CodeMapAddCode) {
279 CodeMap code_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 CodeEntry entry1(i::Logger::FUNCTION_TAG, "aaa");
281 CodeEntry entry2(i::Logger::FUNCTION_TAG, "bbb");
282 CodeEntry entry3(i::Logger::FUNCTION_TAG, "ccc");
283 CodeEntry entry4(i::Logger::FUNCTION_TAG, "ddd");
Steve Block6ded16b2010-05-10 14:33:55 +0100284 code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
285 code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
286 code_map.AddCode(ToAddress(0x1900), &entry3, 0x50);
287 code_map.AddCode(ToAddress(0x1950), &entry4, 0x10);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000288 CHECK(!code_map.FindEntry(0));
289 CHECK(!code_map.FindEntry(ToAddress(0x1500 - 1)));
Steve Block6ded16b2010-05-10 14:33:55 +0100290 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
291 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x100)));
292 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x200 - 1)));
293 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
294 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x50)));
295 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x100 - 1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000296 CHECK(!code_map.FindEntry(ToAddress(0x1700 + 0x100)));
297 CHECK(!code_map.FindEntry(ToAddress(0x1900 - 1)));
Steve Block6ded16b2010-05-10 14:33:55 +0100298 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900)));
299 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900 + 0x28)));
300 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950)));
301 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x7)));
302 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x10 - 1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 CHECK(!code_map.FindEntry(ToAddress(0x1950 + 0x10)));
304 CHECK(!code_map.FindEntry(ToAddress(0xFFFFFFFF)));
Steve Block6ded16b2010-05-10 14:33:55 +0100305}
306
307
308TEST(CodeMapMoveAndDeleteCode) {
309 CodeMap code_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 CodeEntry entry1(i::Logger::FUNCTION_TAG, "aaa");
311 CodeEntry entry2(i::Logger::FUNCTION_TAG, "bbb");
Steve Block6ded16b2010-05-10 14:33:55 +0100312 code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
313 code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
314 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
315 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
Ben Murdoch589d6972011-11-30 16:04:58 +0000316 code_map.MoveCode(ToAddress(0x1500), ToAddress(0x1700)); // Deprecate bbb.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000317 CHECK(!code_map.FindEntry(ToAddress(0x1500)));
Ben Murdoch589d6972011-11-30 16:04:58 +0000318 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1700)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 CodeEntry entry3(i::Logger::FUNCTION_TAG, "ccc");
Ben Murdoch589d6972011-11-30 16:04:58 +0000320 code_map.AddCode(ToAddress(0x1750), &entry3, 0x100);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321 CHECK(!code_map.FindEntry(ToAddress(0x1700)));
Ben Murdoch589d6972011-11-30 16:04:58 +0000322 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1750)));
Steve Block6ded16b2010-05-10 14:33:55 +0100323}
324
325
326namespace {
327
328class TestSetup {
329 public:
330 TestSetup()
331 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
332 i::FLAG_prof_browser_mode = false;
333 }
334
335 ~TestSetup() {
336 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
337 }
338
339 private:
340 bool old_flag_prof_browser_mode_;
341};
342
343} // namespace
344
345TEST(RecordTickSample) {
346 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 CpuProfilesCollection profiles(CcTest::heap());
348 profiles.StartProfiling("", false);
Steve Block6ded16b2010-05-10 14:33:55 +0100349 ProfileGenerator generator(&profiles);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350 CodeEntry* entry1 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
351 CodeEntry* entry2 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "bbb");
352 CodeEntry* entry3 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "ccc");
Steve Block6ded16b2010-05-10 14:33:55 +0100353 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
354 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
355 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
356
357 // We are building the following calls tree:
358 // -> aaa - sample1
359 // aaa -> bbb -> ccc - sample2
360 // -> ccc -> aaa - sample3
361 TickSample sample1;
362 sample1.pc = ToAddress(0x1600);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100363 sample1.tos = ToAddress(0x1500);
Steve Block6ded16b2010-05-10 14:33:55 +0100364 sample1.stack[0] = ToAddress(0x1510);
365 sample1.frames_count = 1;
366 generator.RecordTickSample(sample1);
367 TickSample sample2;
368 sample2.pc = ToAddress(0x1925);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100369 sample2.tos = ToAddress(0x1900);
Steve Block6ded16b2010-05-10 14:33:55 +0100370 sample2.stack[0] = ToAddress(0x1780);
371 sample2.stack[1] = ToAddress(0x10000); // non-existent.
372 sample2.stack[2] = ToAddress(0x1620);
373 sample2.frames_count = 3;
374 generator.RecordTickSample(sample2);
375 TickSample sample3;
376 sample3.pc = ToAddress(0x1510);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100377 sample3.tos = ToAddress(0x1500);
Steve Block6ded16b2010-05-10 14:33:55 +0100378 sample3.stack[0] = ToAddress(0x1910);
379 sample3.stack[1] = ToAddress(0x1610);
380 sample3.frames_count = 2;
381 generator.RecordTickSample(sample3);
382
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 CpuProfile* profile = profiles.StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000384 CHECK(profile);
Steve Block6ded16b2010-05-10 14:33:55 +0100385 ProfileTreeTestHelper top_down_test_helper(profile->top_down());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 CHECK(!top_down_test_helper.Walk(entry2));
387 CHECK(!top_down_test_helper.Walk(entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100388 ProfileNode* node1 = top_down_test_helper.Walk(entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 CHECK(node1);
Steve Block6ded16b2010-05-10 14:33:55 +0100390 CHECK_EQ(entry1, node1->entry());
391 ProfileNode* node2 = top_down_test_helper.Walk(entry1, entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000392 CHECK(node2);
Steve Block6ded16b2010-05-10 14:33:55 +0100393 CHECK_EQ(entry1, node2->entry());
394 ProfileNode* node3 = top_down_test_helper.Walk(entry1, entry2, entry3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000395 CHECK(node3);
Steve Block6ded16b2010-05-10 14:33:55 +0100396 CHECK_EQ(entry3, node3->entry());
397 ProfileNode* node4 = top_down_test_helper.Walk(entry1, entry3, entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000398 CHECK(node4);
Steve Block6ded16b2010-05-10 14:33:55 +0100399 CHECK_EQ(entry1, node4->entry());
400}
401
402
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403static void CheckNodeIds(ProfileNode* node, unsigned* expectedId) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 CHECK_EQ((*expectedId)++, node->id());
405 for (int i = 0; i < node->children()->length(); i++) {
406 CheckNodeIds(node->children()->at(i), expectedId);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100407 }
408}
409
410
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411TEST(SampleIds) {
412 TestSetup test_setup;
413 CpuProfilesCollection profiles(CcTest::heap());
414 profiles.StartProfiling("", true);
415 ProfileGenerator generator(&profiles);
416 CodeEntry* entry1 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
417 CodeEntry* entry2 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "bbb");
418 CodeEntry* entry3 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "ccc");
419 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
420 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
421 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
422
423 // We are building the following calls tree:
424 // -> aaa #3 - sample1
425 // (root)#1 -> aaa #2 -> bbb #4 -> ccc #5 - sample2
426 // -> ccc #6 -> aaa #7 - sample3
427 TickSample sample1;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100428 sample1.timestamp = v8::base::TimeTicks::HighResolutionNow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 sample1.pc = ToAddress(0x1600);
430 sample1.stack[0] = ToAddress(0x1510);
431 sample1.frames_count = 1;
432 generator.RecordTickSample(sample1);
433 TickSample sample2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100434 sample2.timestamp = v8::base::TimeTicks::HighResolutionNow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435 sample2.pc = ToAddress(0x1925);
436 sample2.stack[0] = ToAddress(0x1780);
437 sample2.stack[1] = ToAddress(0x10000); // non-existent.
438 sample2.stack[2] = ToAddress(0x1620);
439 sample2.frames_count = 3;
440 generator.RecordTickSample(sample2);
441 TickSample sample3;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100442 sample3.timestamp = v8::base::TimeTicks::HighResolutionNow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 sample3.pc = ToAddress(0x1510);
444 sample3.stack[0] = ToAddress(0x1910);
445 sample3.stack[1] = ToAddress(0x1610);
446 sample3.frames_count = 2;
447 generator.RecordTickSample(sample3);
448
449 CpuProfile* profile = profiles.StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450 unsigned nodeId = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 CheckNodeIds(profile->top_down()->root(), &nodeId);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000452 CHECK_EQ(7u, nodeId - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453
454 CHECK_EQ(3, profile->samples_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000455 unsigned expected_id[] = {3, 5, 7};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000456 for (int i = 0; i < 3; i++) {
457 CHECK_EQ(expected_id[i], profile->sample(i)->id());
458 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100459}
460
461
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462TEST(NoSamples) {
463 TestSetup test_setup;
464 CpuProfilesCollection profiles(CcTest::heap());
465 profiles.StartProfiling("", false);
466 ProfileGenerator generator(&profiles);
467 CodeEntry* entry1 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
468 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
469
470 // We are building the following calls tree:
471 // (root)#1 -> aaa #2 -> aaa #3 - sample1
472 TickSample sample1;
473 sample1.pc = ToAddress(0x1600);
474 sample1.stack[0] = ToAddress(0x1510);
475 sample1.frames_count = 1;
476 generator.RecordTickSample(sample1);
477
478 CpuProfile* profile = profiles.StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479 unsigned nodeId = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 CheckNodeIds(profile->top_down()->root(), &nodeId);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481 CHECK_EQ(3u, nodeId - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482
483 CHECK_EQ(0, profile->samples_count());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100484}
485
486
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100487static const ProfileNode* PickChild(const ProfileNode* parent,
488 const char* name) {
489 for (int i = 0; i < parent->children()->length(); ++i) {
490 const ProfileNode* child = parent->children()->at(i);
491 if (strcmp(child->entry()->name(), name) == 0) return child;
492 }
493 return NULL;
494}
495
496
497TEST(RecordStackTraceAtStartProfiling) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100498 // This test does not pass with inlining enabled since inlined functions
499 // don't appear in the stack trace.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000500 i::FLAG_turbo_inlining = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100501 i::FLAG_use_inlining = false;
502
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 v8::HandleScope scope(CcTest::isolate());
504 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
505 v8::Context::Scope context_scope(env);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100506
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
508 CHECK_EQ(0, profiler->GetProfilesCount());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100509 CompileRun(
510 "function c() { startProfiling(); }\n"
511 "function b() { c(); }\n"
512 "function a() { b(); }\n"
513 "a();\n"
514 "stopProfiling();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 CHECK_EQ(1, profiler->GetProfilesCount());
516 CpuProfile* profile = profiler->GetProfile(0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100517 const ProfileTree* topDown = profile->top_down();
518 const ProfileNode* current = topDown->root();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100519 const_cast<ProfileNode*>(current)->Print(0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100520 // The tree should look like this:
521 // (root)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 // ""
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100523 // a
524 // b
525 // c
Ben Murdochb0fe1622011-05-05 13:52:32 +0100526 // There can also be:
527 // startProfiling
528 // if the sampler managed to get a tick.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 current = PickChild(current, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000530 CHECK(const_cast<ProfileNode*>(current));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100531 current = PickChild(current, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000532 CHECK(const_cast<ProfileNode*>(current));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100533 current = PickChild(current, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000534 CHECK(const_cast<ProfileNode*>(current));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100535 current = PickChild(current, "c");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000536 CHECK(const_cast<ProfileNode*>(current));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100537 CHECK(current->children()->length() == 0 ||
538 current->children()->length() == 1);
539 if (current->children()->length() == 1) {
540 current = PickChild(current, "startProfiling");
541 CHECK_EQ(0, current->children()->length());
542 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100543}
544
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100545
546TEST(Issue51919) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 CpuProfilesCollection collection(CcTest::heap());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100548 i::EmbeddedVector<char*,
549 CpuProfilesCollection::kMaxSimultaneousProfiles> titles;
550 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) {
551 i::Vector<char> title = i::Vector<char>::New(16);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552 i::SNPrintF(title, "%d", i);
553 CHECK(collection.StartProfiling(title.start(), false));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100554 titles[i] = title.start();
555 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 CHECK(!collection.StartProfiling("maximum", false));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100557 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i)
558 i::DeleteArray(titles[i]);
559}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560
561
562static const v8::CpuProfileNode* PickChild(const v8::CpuProfileNode* parent,
563 const char* name) {
564 for (int i = 0; i < parent->GetChildrenCount(); ++i) {
565 const v8::CpuProfileNode* child = parent->GetChild(i);
566 v8::String::Utf8Value function_name(child->GetFunctionName());
567 if (strcmp(*function_name, name) == 0) return child;
568 }
569 return NULL;
570}
571
572
573TEST(ProfileNodeScriptId) {
574 // This test does not pass with inlining enabled since inlined functions
575 // don't appear in the stack trace.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000576 i::FLAG_turbo_inlining = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 i::FLAG_use_inlining = false;
578
579 v8::HandleScope scope(CcTest::isolate());
580 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
581 v8::Context::Scope context_scope(env);
582
583 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
584 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
585 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000586 v8::Local<v8::Script> script_a =
587 v8_compile(v8_str("function a() { startProfiling(); }\n"));
588 script_a->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
589 .ToLocalChecked();
590 v8::Local<v8::Script> script_b =
591 v8_compile(v8_str("function b() { a(); }\n"
592 "b();\n"
593 "stopProfiling();\n"));
594 script_b->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
595 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596 CHECK_EQ(1, iprofiler->GetProfilesCount());
597 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
598 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
599 reinterpret_cast<ProfileNode*>(
600 const_cast<v8::CpuProfileNode*>(current))->Print(0);
601 // The tree should look like this:
602 // (root)
603 // ""
604 // b
605 // a
606 // There can also be:
607 // startProfiling
608 // if the sampler managed to get a tick.
609 current = PickChild(current, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000610 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611
612 current = PickChild(current, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000613 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000614 CHECK_EQ(script_b->GetUnboundScript()->GetId(), current->GetScriptId());
615
616 current = PickChild(current, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000617 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 CHECK_EQ(script_a->GetUnboundScript()->GetId(), current->GetScriptId());
619}
620
621
622
623
624static const char* line_number_test_source_existing_functions =
625"function foo_at_the_first_line() {\n"
626"}\n"
627"foo_at_the_first_line();\n"
628"function lazy_func_at_forth_line() {}\n";
629
630
631static const char* line_number_test_source_profile_time_functions =
632"// Empty first line\n"
633"function bar_at_the_second_line() {\n"
634" foo_at_the_first_line();\n"
635"}\n"
636"bar_at_the_second_line();\n"
637"function lazy_func_at_6th_line() {}";
638
639int GetFunctionLineNumber(LocalContext* env, const char* name) {
640 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
641 CodeMap* code_map = profiler->generator()->code_map();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000642 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
643 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
644 (*(*env))
645 ->Global()
646 ->Get(v8::Isolate::GetCurrent()->GetCurrentContext(),
647 v8_str(name))
648 .ToLocalChecked())));
Ben Murdochda12d292016-06-02 14:46:10 +0100649 CodeEntry* func_entry = code_map->FindEntry(func->abstract_code()->address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 if (!func_entry)
651 FATAL(name);
652 return func_entry->line_number();
653}
654
655
656TEST(LineNumber) {
657 i::FLAG_use_inlining = false;
658
659 CcTest::InitializeVM();
660 LocalContext env;
661 i::Isolate* isolate = CcTest::i_isolate();
662 TestSetup test_setup;
663
664 i::HandleScope scope(isolate);
665
666 CompileRun(line_number_test_source_existing_functions);
667
668 CpuProfiler* profiler = isolate->cpu_profiler();
669 profiler->StartProfiling("LineNumber");
670
671 CompileRun(line_number_test_source_profile_time_functions);
672
673 profiler->processor()->StopSynchronously();
674
Ben Murdochda12d292016-06-02 14:46:10 +0100675 bool is_lazy = i::FLAG_lazy && !(i::FLAG_ignition && i::FLAG_ignition_eager);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000676 CHECK_EQ(1, GetFunctionLineNumber(&env, "foo_at_the_first_line"));
Ben Murdochda12d292016-06-02 14:46:10 +0100677 CHECK_EQ(is_lazy ? 0 : 4,
678 GetFunctionLineNumber(&env, "lazy_func_at_forth_line"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000679 CHECK_EQ(2, GetFunctionLineNumber(&env, "bar_at_the_second_line"));
Ben Murdochda12d292016-06-02 14:46:10 +0100680 CHECK_EQ(is_lazy ? 0 : 6,
681 GetFunctionLineNumber(&env, "lazy_func_at_6th_line"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682
683 profiler->StopProfiling("LineNumber");
684}
685
686
687
688TEST(BailoutReason) {
689 v8::HandleScope scope(CcTest::isolate());
690 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
691 v8::Context::Scope context_scope(env);
692
693 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
694 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
695 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000696 v8::Local<v8::Script> script =
697 v8_compile(v8_str("function Debugger() {\n"
698 " debugger;\n"
699 " startProfiling();\n"
700 "}\n"
701 "function TryFinally() {\n"
702 " try {\n"
703 " Debugger();\n"
704 " } finally { };\n"
705 "}\n"
706 "TryFinally();\n"
707 "stopProfiling();"));
708 script->Run(v8::Isolate::GetCurrent()->GetCurrentContext()).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 CHECK_EQ(1, iprofiler->GetProfilesCount());
710 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
711 CHECK(profile);
712 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
713 reinterpret_cast<ProfileNode*>(
714 const_cast<v8::CpuProfileNode*>(current))->Print(0);
715 // The tree should look like this:
716 // (root)
717 // ""
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000718 // kTryFinallyStatement
719 // kDebuggerStatement
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000720 current = PickChild(current, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000721 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000722
723 current = PickChild(current, "TryFinally");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000724 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000725 CHECK(!strcmp("TryFinallyStatement", current->GetBailoutReason()));
726
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000727 current = PickChild(current, "Debugger");
728 CHECK(const_cast<v8::CpuProfileNode*>(current));
729 CHECK(!strcmp("DebuggerStatement", current->GetBailoutReason()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730}