blob: 7e59839414dc711ffffeb0290e47124773f78f5a [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();
Ben Murdoch61f157c2016-09-16 13:49:30 +010054 CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 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));
Ben Murdoch61f157c2016-09-16 13:49:30 +010058 CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 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));
Ben Murdoch61f157c2016-09-16 13:49:30 +010064 CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065 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();
Ben Murdoch61f157c2016-09-16 13:49:30 +010080 CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, aaa);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 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 Murdoch61f157c2016-09-16 13:49:30 +010085 CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, aaa);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 CHECK_EQ(childNode1, node->FindOrAddChild(&entry2));
Kristian Monsen0d5e1162010-09-30 15:31:59 +010087 // Now with a different security token.
Ben Murdoch61f157c2016-09-16 13:49:30 +010088 CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, aaa);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 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 Murdoch61f157c2016-09-16 13:49:30 +0100125 CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
126 CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
127 CodeEntry entry3(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100190 CodeEntry entry1(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100204 CodeEntry entry2(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100230 CodeEntry entry3(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100280 CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
281 CodeEntry entry2(i::CodeEventListener::FUNCTION_TAG, "bbb");
282 CodeEntry entry3(i::CodeEventListener::FUNCTION_TAG, "ccc");
283 CodeEntry entry4(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100310 CodeEntry entry1(i::CodeEventListener::FUNCTION_TAG, "aaa");
311 CodeEntry entry2(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100319 CodeEntry entry3(i::CodeEventListener::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 Murdoch61f157c2016-09-16 13:49:30 +0100347 CpuProfilesCollection profiles(CcTest::i_isolate());
348 profiles.set_cpu_profiler(CcTest::i_isolate()->cpu_profiler());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 profiles.StartProfiling("", false);
Steve Block6ded16b2010-05-10 14:33:55 +0100350 ProfileGenerator generator(&profiles);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100351 CodeEntry* entry1 = new CodeEntry(i::Logger::FUNCTION_TAG, "aaa");
352 CodeEntry* entry2 = new CodeEntry(i::Logger::FUNCTION_TAG, "bbb");
353 CodeEntry* entry3 = new CodeEntry(i::Logger::FUNCTION_TAG, "ccc");
Steve Block6ded16b2010-05-10 14:33:55 +0100354 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
355 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
356 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
357
358 // We are building the following calls tree:
359 // -> aaa - sample1
360 // aaa -> bbb -> ccc - sample2
361 // -> ccc -> aaa - sample3
362 TickSample sample1;
363 sample1.pc = ToAddress(0x1600);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100364 sample1.tos = ToAddress(0x1500);
Steve Block6ded16b2010-05-10 14:33:55 +0100365 sample1.stack[0] = ToAddress(0x1510);
366 sample1.frames_count = 1;
367 generator.RecordTickSample(sample1);
368 TickSample sample2;
369 sample2.pc = ToAddress(0x1925);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100370 sample2.tos = ToAddress(0x1900);
Steve Block6ded16b2010-05-10 14:33:55 +0100371 sample2.stack[0] = ToAddress(0x1780);
372 sample2.stack[1] = ToAddress(0x10000); // non-existent.
373 sample2.stack[2] = ToAddress(0x1620);
374 sample2.frames_count = 3;
375 generator.RecordTickSample(sample2);
376 TickSample sample3;
377 sample3.pc = ToAddress(0x1510);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100378 sample3.tos = ToAddress(0x1500);
Steve Block6ded16b2010-05-10 14:33:55 +0100379 sample3.stack[0] = ToAddress(0x1910);
380 sample3.stack[1] = ToAddress(0x1610);
381 sample3.frames_count = 2;
382 generator.RecordTickSample(sample3);
383
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 CpuProfile* profile = profiles.StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 CHECK(profile);
Steve Block6ded16b2010-05-10 14:33:55 +0100386 ProfileTreeTestHelper top_down_test_helper(profile->top_down());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000387 CHECK(!top_down_test_helper.Walk(entry2));
388 CHECK(!top_down_test_helper.Walk(entry3));
Steve Block6ded16b2010-05-10 14:33:55 +0100389 ProfileNode* node1 = top_down_test_helper.Walk(entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 CHECK(node1);
Steve Block6ded16b2010-05-10 14:33:55 +0100391 CHECK_EQ(entry1, node1->entry());
392 ProfileNode* node2 = top_down_test_helper.Walk(entry1, entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393 CHECK(node2);
Steve Block6ded16b2010-05-10 14:33:55 +0100394 CHECK_EQ(entry1, node2->entry());
395 ProfileNode* node3 = top_down_test_helper.Walk(entry1, entry2, entry3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396 CHECK(node3);
Steve Block6ded16b2010-05-10 14:33:55 +0100397 CHECK_EQ(entry3, node3->entry());
398 ProfileNode* node4 = top_down_test_helper.Walk(entry1, entry3, entry1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000399 CHECK(node4);
Steve Block6ded16b2010-05-10 14:33:55 +0100400 CHECK_EQ(entry1, node4->entry());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100401
402 delete entry1;
403 delete entry2;
404 delete entry3;
Steve Block6ded16b2010-05-10 14:33:55 +0100405}
406
407
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000408static void CheckNodeIds(ProfileNode* node, unsigned* expectedId) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409 CHECK_EQ((*expectedId)++, node->id());
410 for (int i = 0; i < node->children()->length(); i++) {
411 CheckNodeIds(node->children()->at(i), expectedId);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100412 }
413}
414
415
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416TEST(SampleIds) {
417 TestSetup test_setup;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100418 CpuProfilesCollection profiles(CcTest::i_isolate());
419 profiles.set_cpu_profiler(CcTest::i_isolate()->cpu_profiler());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 profiles.StartProfiling("", true);
421 ProfileGenerator generator(&profiles);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100422 CodeEntry* entry1 = new CodeEntry(i::Logger::FUNCTION_TAG, "aaa");
423 CodeEntry* entry2 = new CodeEntry(i::Logger::FUNCTION_TAG, "bbb");
424 CodeEntry* entry3 = new CodeEntry(i::Logger::FUNCTION_TAG, "ccc");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
426 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
427 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
428
429 // We are building the following calls tree:
430 // -> aaa #3 - sample1
431 // (root)#1 -> aaa #2 -> bbb #4 -> ccc #5 - sample2
432 // -> ccc #6 -> aaa #7 - sample3
433 TickSample sample1;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100434 sample1.timestamp = v8::base::TimeTicks::HighResolutionNow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435 sample1.pc = ToAddress(0x1600);
436 sample1.stack[0] = ToAddress(0x1510);
437 sample1.frames_count = 1;
438 generator.RecordTickSample(sample1);
439 TickSample sample2;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100440 sample2.timestamp = v8::base::TimeTicks::HighResolutionNow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 sample2.pc = ToAddress(0x1925);
442 sample2.stack[0] = ToAddress(0x1780);
443 sample2.stack[1] = ToAddress(0x10000); // non-existent.
444 sample2.stack[2] = ToAddress(0x1620);
445 sample2.frames_count = 3;
446 generator.RecordTickSample(sample2);
447 TickSample sample3;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100448 sample3.timestamp = v8::base::TimeTicks::HighResolutionNow();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 sample3.pc = ToAddress(0x1510);
450 sample3.stack[0] = ToAddress(0x1910);
451 sample3.stack[1] = ToAddress(0x1610);
452 sample3.frames_count = 2;
453 generator.RecordTickSample(sample3);
454
455 CpuProfile* profile = profiles.StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000456 unsigned nodeId = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000457 CheckNodeIds(profile->top_down()->root(), &nodeId);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458 CHECK_EQ(7u, nodeId - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459
460 CHECK_EQ(3, profile->samples_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 unsigned expected_id[] = {3, 5, 7};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 for (int i = 0; i < 3; i++) {
463 CHECK_EQ(expected_id[i], profile->sample(i)->id());
464 }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100465
466 delete entry1;
467 delete entry2;
468 delete entry3;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100469}
470
471
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472TEST(NoSamples) {
473 TestSetup test_setup;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100474 CpuProfilesCollection profiles(CcTest::i_isolate());
475 profiles.set_cpu_profiler(CcTest::i_isolate()->cpu_profiler());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 profiles.StartProfiling("", false);
477 ProfileGenerator generator(&profiles);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100478 CodeEntry* entry1 = new CodeEntry(i::Logger::FUNCTION_TAG, "aaa");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
480
481 // We are building the following calls tree:
482 // (root)#1 -> aaa #2 -> aaa #3 - sample1
483 TickSample sample1;
484 sample1.pc = ToAddress(0x1600);
485 sample1.stack[0] = ToAddress(0x1510);
486 sample1.frames_count = 1;
487 generator.RecordTickSample(sample1);
488
489 CpuProfile* profile = profiles.StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000490 unsigned nodeId = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 CheckNodeIds(profile->top_down()->root(), &nodeId);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 CHECK_EQ(3u, nodeId - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493
494 CHECK_EQ(0, profile->samples_count());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100495
496 delete entry1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100497}
498
499
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100500static const ProfileNode* PickChild(const ProfileNode* parent,
501 const char* name) {
502 for (int i = 0; i < parent->children()->length(); ++i) {
503 const ProfileNode* child = parent->children()->at(i);
504 if (strcmp(child->entry()->name(), name) == 0) return child;
505 }
506 return NULL;
507}
508
509
510TEST(RecordStackTraceAtStartProfiling) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100511 // This test does not pass with inlining enabled since inlined functions
512 // don't appear in the stack trace.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000513 i::FLAG_turbo_inlining = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100514 i::FLAG_use_inlining = false;
515
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 v8::HandleScope scope(CcTest::isolate());
517 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
518 v8::Context::Scope context_scope(env);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100519
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000520 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
521 CHECK_EQ(0, profiler->GetProfilesCount());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100522 CompileRun(
523 "function c() { startProfiling(); }\n"
524 "function b() { c(); }\n"
525 "function a() { b(); }\n"
526 "a();\n"
527 "stopProfiling();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 CHECK_EQ(1, profiler->GetProfilesCount());
529 CpuProfile* profile = profiler->GetProfile(0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100530 const ProfileTree* topDown = profile->top_down();
531 const ProfileNode* current = topDown->root();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100532 const_cast<ProfileNode*>(current)->Print(0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100533 // The tree should look like this:
534 // (root)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535 // ""
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100536 // a
537 // b
538 // c
Ben Murdochb0fe1622011-05-05 13:52:32 +0100539 // There can also be:
540 // startProfiling
541 // if the sampler managed to get a tick.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 current = PickChild(current, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 CHECK(const_cast<ProfileNode*>(current));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100544 current = PickChild(current, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000545 CHECK(const_cast<ProfileNode*>(current));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100546 current = PickChild(current, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 CHECK(const_cast<ProfileNode*>(current));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100548 current = PickChild(current, "c");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 CHECK(const_cast<ProfileNode*>(current));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100550 CHECK(current->children()->length() == 0 ||
551 current->children()->length() == 1);
552 if (current->children()->length() == 1) {
553 current = PickChild(current, "startProfiling");
554 CHECK_EQ(0, current->children()->length());
555 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100556}
557
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100558
559TEST(Issue51919) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100560 CpuProfilesCollection collection(CcTest::i_isolate());
561 collection.set_cpu_profiler(CcTest::i_isolate()->cpu_profiler());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100562 i::EmbeddedVector<char*,
563 CpuProfilesCollection::kMaxSimultaneousProfiles> titles;
564 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) {
565 i::Vector<char> title = i::Vector<char>::New(16);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 i::SNPrintF(title, "%d", i);
567 CHECK(collection.StartProfiling(title.start(), false));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100568 titles[i] = title.start();
569 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 CHECK(!collection.StartProfiling("maximum", false));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100571 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i)
572 i::DeleteArray(titles[i]);
573}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574
575
576static const v8::CpuProfileNode* PickChild(const v8::CpuProfileNode* parent,
577 const char* name) {
578 for (int i = 0; i < parent->GetChildrenCount(); ++i) {
579 const v8::CpuProfileNode* child = parent->GetChild(i);
580 v8::String::Utf8Value function_name(child->GetFunctionName());
581 if (strcmp(*function_name, name) == 0) return child;
582 }
583 return NULL;
584}
585
586
587TEST(ProfileNodeScriptId) {
588 // This test does not pass with inlining enabled since inlined functions
589 // don't appear in the stack trace.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000590 i::FLAG_turbo_inlining = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 i::FLAG_use_inlining = false;
592
593 v8::HandleScope scope(CcTest::isolate());
594 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
595 v8::Context::Scope context_scope(env);
596
597 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
598 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
599 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000600 v8::Local<v8::Script> script_a =
601 v8_compile(v8_str("function a() { startProfiling(); }\n"));
602 script_a->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
603 .ToLocalChecked();
604 v8::Local<v8::Script> script_b =
605 v8_compile(v8_str("function b() { a(); }\n"
606 "b();\n"
607 "stopProfiling();\n"));
608 script_b->Run(v8::Isolate::GetCurrent()->GetCurrentContext())
609 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 CHECK_EQ(1, iprofiler->GetProfilesCount());
611 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
612 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
613 reinterpret_cast<ProfileNode*>(
614 const_cast<v8::CpuProfileNode*>(current))->Print(0);
615 // The tree should look like this:
616 // (root)
617 // ""
618 // b
619 // a
620 // There can also be:
621 // startProfiling
622 // if the sampler managed to get a tick.
623 current = PickChild(current, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000624 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000625
626 current = PickChild(current, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000627 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 CHECK_EQ(script_b->GetUnboundScript()->GetId(), current->GetScriptId());
629
630 current = PickChild(current, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000631 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632 CHECK_EQ(script_a->GetUnboundScript()->GetId(), current->GetScriptId());
633}
634
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635static const char* line_number_test_source_existing_functions =
636"function foo_at_the_first_line() {\n"
637"}\n"
638"foo_at_the_first_line();\n"
639"function lazy_func_at_forth_line() {}\n";
640
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641static const char* line_number_test_source_profile_time_functions =
642"// Empty first line\n"
643"function bar_at_the_second_line() {\n"
644" foo_at_the_first_line();\n"
645"}\n"
646"bar_at_the_second_line();\n"
647"function lazy_func_at_6th_line() {}";
648
649int GetFunctionLineNumber(LocalContext* env, const char* name) {
650 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
651 CodeMap* code_map = profiler->generator()->code_map();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000652 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
653 v8::Utils::OpenHandle(*v8::Local<v8::Function>::Cast(
654 (*(*env))
655 ->Global()
656 ->Get(v8::Isolate::GetCurrent()->GetCurrentContext(),
657 v8_str(name))
658 .ToLocalChecked())));
Ben Murdochda12d292016-06-02 14:46:10 +0100659 CodeEntry* func_entry = code_map->FindEntry(func->abstract_code()->address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000660 if (!func_entry)
661 FATAL(name);
662 return func_entry->line_number();
663}
664
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665TEST(LineNumber) {
666 i::FLAG_use_inlining = false;
667
668 CcTest::InitializeVM();
669 LocalContext env;
670 i::Isolate* isolate = CcTest::i_isolate();
671 TestSetup test_setup;
672
673 i::HandleScope scope(isolate);
674
675 CompileRun(line_number_test_source_existing_functions);
676
677 CpuProfiler* profiler = isolate->cpu_profiler();
678 profiler->StartProfiling("LineNumber");
679
680 CompileRun(line_number_test_source_profile_time_functions);
681
682 profiler->processor()->StopSynchronously();
683
Ben Murdochda12d292016-06-02 14:46:10 +0100684 bool is_lazy = i::FLAG_lazy && !(i::FLAG_ignition && i::FLAG_ignition_eager);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685 CHECK_EQ(1, GetFunctionLineNumber(&env, "foo_at_the_first_line"));
Ben Murdochda12d292016-06-02 14:46:10 +0100686 CHECK_EQ(is_lazy ? 0 : 4,
687 GetFunctionLineNumber(&env, "lazy_func_at_forth_line"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688 CHECK_EQ(2, GetFunctionLineNumber(&env, "bar_at_the_second_line"));
Ben Murdochda12d292016-06-02 14:46:10 +0100689 CHECK_EQ(is_lazy ? 0 : 6,
690 GetFunctionLineNumber(&env, "lazy_func_at_6th_line"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000691
692 profiler->StopProfiling("LineNumber");
693}
694
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000695TEST(BailoutReason) {
696 v8::HandleScope scope(CcTest::isolate());
697 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
698 v8::Context::Scope context_scope(env);
699
700 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
701 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
702 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000703 v8::Local<v8::Script> script =
704 v8_compile(v8_str("function Debugger() {\n"
705 " debugger;\n"
706 " startProfiling();\n"
707 "}\n"
Ben Murdochc5610432016-08-08 18:44:38 +0100708 "Debugger();\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000709 "stopProfiling();"));
710 script->Run(v8::Isolate::GetCurrent()->GetCurrentContext()).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000711 CHECK_EQ(1, iprofiler->GetProfilesCount());
712 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
713 CHECK(profile);
714 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
715 reinterpret_cast<ProfileNode*>(
716 const_cast<v8::CpuProfileNode*>(current))->Print(0);
717 // The tree should look like this:
718 // (root)
719 // ""
Ben Murdochc5610432016-08-08 18:44:38 +0100720 // kDebuggerStatement
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 current = PickChild(current, "");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000722 CHECK(const_cast<v8::CpuProfileNode*>(current));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000724 current = PickChild(current, "Debugger");
725 CHECK(const_cast<v8::CpuProfileNode*>(current));
726 CHECK(!strcmp("DebuggerStatement", current->GetBailoutReason()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000727}