blob: 70b34e3d7c276688c27a49755d1081899f5ec499 [file] [log] [blame]
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001// Copyright 2010 the V8 project authors. All rights reserved.
ulan@chromium.org750145a2013-03-07 15:14:13 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fschneider@chromium.org086aac62010-03-17 13:18:24 +000027//
28// Tests of profiles generator and utilities.
29
ulan@chromium.org57ff8812013-05-10 08:16:55 +000030// TODO(dcarney): remove
31#define V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
32
fschneider@chromium.org086aac62010-03-17 13:18:24 +000033#include "v8.h"
34#include "profile-generator-inl.h"
35#include "cctest.h"
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000036#include "../include/v8-profiler.h"
fschneider@chromium.org086aac62010-03-17 13:18:24 +000037
fschneider@chromium.org086aac62010-03-17 13:18:24 +000038using i::CodeEntry;
39using i::CodeMap;
lrn@chromium.org25156de2010-04-06 13:10:27 +000040using i::CpuProfile;
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +000041using i::CpuProfiler;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000042using i::CpuProfilesCollection;
fschneider@chromium.org086aac62010-03-17 13:18:24 +000043using i::ProfileNode;
44using i::ProfileTree;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000045using i::ProfileGenerator;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000046using i::SampleRateCalculator;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +000047using i::TickSample;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000048using i::TokenEnumerator;
fschneider@chromium.org086aac62010-03-17 13:18:24 +000049using i::Vector;
50
51
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000052namespace v8 {
53namespace internal {
54
55class TokenEnumeratorTester {
56 public:
57 static i::List<bool>* token_removed(TokenEnumerator* te) {
58 return &te->token_removed_;
59 }
60};
61
62} } // namespace v8::internal
63
64TEST(TokenEnumerator) {
65 TokenEnumerator te;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000066 CHECK_EQ(TokenEnumerator::kNoSecurityToken, te.GetTokenId(NULL));
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000067 v8::HandleScope hs(v8::Isolate::GetCurrent());
ricow@chromium.org55ee8072011-09-08 16:33:10 +000068 v8::Local<v8::String> token1(v8::String::New("1x"));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000069 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
70 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
ricow@chromium.org55ee8072011-09-08 16:33:10 +000071 v8::Local<v8::String> token2(v8::String::New("2x"));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000072 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
73 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
74 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
75 {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +000076 v8::HandleScope hs(v8::Isolate::GetCurrent());
ricow@chromium.org55ee8072011-09-08 16:33:10 +000077 v8::Local<v8::String> token3(v8::String::New("3x"));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000078 CHECK_EQ(2, te.GetTokenId(*v8::Utils::OpenHandle(*token3)));
79 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
80 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
81 }
82 CHECK(!i::TokenEnumeratorTester::token_removed(&te)->at(2));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000083 HEAP->CollectAllGarbage(i::Heap::kNoGCFlags);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000084 CHECK(i::TokenEnumeratorTester::token_removed(&te)->at(2));
85 CHECK_EQ(1, te.GetTokenId(*v8::Utils::OpenHandle(*token2)));
86 CHECK_EQ(0, te.GetTokenId(*v8::Utils::OpenHandle(*token1)));
87}
88
89
fschneider@chromium.org086aac62010-03-17 13:18:24 +000090TEST(ProfileNodeFindOrAddChild) {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +000091 ProfileTree tree;
92 ProfileNode node(&tree, NULL);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000093 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
94 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +000095 ProfileNode* childNode1 = node.FindOrAddChild(&entry1);
96 CHECK_NE(NULL, childNode1);
97 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000098 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
99 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000100 ProfileNode* childNode2 = node.FindOrAddChild(&entry2);
101 CHECK_NE(NULL, childNode2);
102 CHECK_NE(childNode1, childNode2);
103 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
104 CHECK_EQ(childNode2, node.FindOrAddChild(&entry2));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000105 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
106 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000107 ProfileNode* childNode3 = node.FindOrAddChild(&entry3);
108 CHECK_NE(NULL, childNode3);
109 CHECK_NE(childNode1, childNode3);
110 CHECK_NE(childNode2, childNode3);
111 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
112 CHECK_EQ(childNode2, node.FindOrAddChild(&entry2));
113 CHECK_EQ(childNode3, node.FindOrAddChild(&entry3));
114}
115
116
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000117TEST(ProfileNodeFindOrAddChildForSameFunction) {
118 const char* empty = "";
119 const char* aaa = "aaa";
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000120 ProfileTree tree;
121 ProfileNode node(&tree, NULL);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000122 CodeEntry entry1(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0,
123 TokenEnumerator::kNoSecurityToken);
124 ProfileNode* childNode1 = node.FindOrAddChild(&entry1);
125 CHECK_NE(NULL, childNode1);
126 CHECK_EQ(childNode1, node.FindOrAddChild(&entry1));
127 // The same function again.
128 CodeEntry entry2(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0,
129 TokenEnumerator::kNoSecurityToken);
130 CHECK_EQ(childNode1, node.FindOrAddChild(&entry2));
131 // Now with a different security token.
132 CodeEntry entry3(i::Logger::FUNCTION_TAG, empty, aaa, empty, 0,
133 TokenEnumerator::kNoSecurityToken + 1);
134 CHECK_EQ(childNode1, node.FindOrAddChild(&entry3));
135}
136
137
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000138namespace {
139
140class ProfileTreeTestHelper {
141 public:
lrn@chromium.org25156de2010-04-06 13:10:27 +0000142 explicit ProfileTreeTestHelper(const ProfileTree* tree)
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000143 : tree_(tree) { }
144
145 ProfileNode* Walk(CodeEntry* entry1,
146 CodeEntry* entry2 = NULL,
147 CodeEntry* entry3 = NULL) {
148 ProfileNode* node = tree_->root();
149 node = node->FindChild(entry1);
150 if (node == NULL) return NULL;
151 if (entry2 != NULL) {
152 node = node->FindChild(entry2);
153 if (node == NULL) return NULL;
154 }
155 if (entry3 != NULL) {
156 node = node->FindChild(entry3);
157 }
158 return node;
159 }
160
161 private:
lrn@chromium.org25156de2010-04-06 13:10:27 +0000162 const ProfileTree* tree_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000163};
164
165} // namespace
166
167TEST(ProfileTreeAddPathFromStart) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000168 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
169 TokenEnumerator::kNoSecurityToken);
170 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
171 TokenEnumerator::kNoSecurityToken);
172 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
173 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000174 ProfileTree tree;
175 ProfileTreeTestHelper helper(&tree);
176 CHECK_EQ(NULL, helper.Walk(&entry1));
177 CHECK_EQ(NULL, helper.Walk(&entry2));
178 CHECK_EQ(NULL, helper.Walk(&entry3));
179
180 CodeEntry* path[] = {NULL, &entry1, NULL, &entry2, NULL, NULL, &entry3, NULL};
181 Vector<CodeEntry*> path_vec(path, sizeof(path) / sizeof(path[0]));
182 tree.AddPathFromStart(path_vec);
183 CHECK_EQ(NULL, helper.Walk(&entry2));
184 CHECK_EQ(NULL, helper.Walk(&entry3));
185 ProfileNode* node1 = helper.Walk(&entry1);
186 CHECK_NE(NULL, node1);
187 CHECK_EQ(0, node1->total_ticks());
188 CHECK_EQ(0, node1->self_ticks());
189 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
190 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
191 ProfileNode* node2 = helper.Walk(&entry1, &entry2);
192 CHECK_NE(NULL, node2);
193 CHECK_NE(node1, node2);
194 CHECK_EQ(0, node2->total_ticks());
195 CHECK_EQ(0, node2->self_ticks());
196 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
197 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry2));
198 ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3);
199 CHECK_NE(NULL, node3);
200 CHECK_NE(node1, node3);
201 CHECK_NE(node2, node3);
202 CHECK_EQ(0, node3->total_ticks());
203 CHECK_EQ(1, node3->self_ticks());
204
205 tree.AddPathFromStart(path_vec);
206 CHECK_EQ(node1, helper.Walk(&entry1));
207 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
208 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
209 CHECK_EQ(0, node1->total_ticks());
210 CHECK_EQ(0, node1->self_ticks());
211 CHECK_EQ(0, node2->total_ticks());
212 CHECK_EQ(0, node2->self_ticks());
213 CHECK_EQ(0, node3->total_ticks());
214 CHECK_EQ(2, node3->self_ticks());
215
216 CodeEntry* path2[] = {&entry1, &entry2, &entry2};
217 Vector<CodeEntry*> path2_vec(path2, sizeof(path2) / sizeof(path2[0]));
218 tree.AddPathFromStart(path2_vec);
219 CHECK_EQ(NULL, helper.Walk(&entry2));
220 CHECK_EQ(NULL, helper.Walk(&entry3));
221 CHECK_EQ(node1, helper.Walk(&entry1));
222 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
223 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
224 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
225 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
226 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
227 CHECK_EQ(0, node3->total_ticks());
228 CHECK_EQ(2, node3->self_ticks());
229 ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2);
230 CHECK_NE(NULL, node4);
231 CHECK_NE(node3, node4);
232 CHECK_EQ(0, node4->total_ticks());
233 CHECK_EQ(1, node4->self_ticks());
234}
235
236
237TEST(ProfileTreeAddPathFromEnd) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000238 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
239 TokenEnumerator::kNoSecurityToken);
240 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
241 TokenEnumerator::kNoSecurityToken);
242 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
243 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000244 ProfileTree tree;
245 ProfileTreeTestHelper helper(&tree);
246 CHECK_EQ(NULL, helper.Walk(&entry1));
247 CHECK_EQ(NULL, helper.Walk(&entry2));
248 CHECK_EQ(NULL, helper.Walk(&entry3));
249
250 CodeEntry* path[] = {NULL, &entry3, NULL, &entry2, NULL, NULL, &entry1, NULL};
251 Vector<CodeEntry*> path_vec(path, sizeof(path) / sizeof(path[0]));
252 tree.AddPathFromEnd(path_vec);
253 CHECK_EQ(NULL, helper.Walk(&entry2));
254 CHECK_EQ(NULL, helper.Walk(&entry3));
255 ProfileNode* node1 = helper.Walk(&entry1);
256 CHECK_NE(NULL, node1);
257 CHECK_EQ(0, node1->total_ticks());
258 CHECK_EQ(0, node1->self_ticks());
259 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
260 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
261 ProfileNode* node2 = helper.Walk(&entry1, &entry2);
262 CHECK_NE(NULL, node2);
263 CHECK_NE(node1, node2);
264 CHECK_EQ(0, node2->total_ticks());
265 CHECK_EQ(0, node2->self_ticks());
266 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
267 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry2));
268 ProfileNode* node3 = helper.Walk(&entry1, &entry2, &entry3);
269 CHECK_NE(NULL, node3);
270 CHECK_NE(node1, node3);
271 CHECK_NE(node2, node3);
272 CHECK_EQ(0, node3->total_ticks());
273 CHECK_EQ(1, node3->self_ticks());
274
275 tree.AddPathFromEnd(path_vec);
276 CHECK_EQ(node1, helper.Walk(&entry1));
277 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
278 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
279 CHECK_EQ(0, node1->total_ticks());
280 CHECK_EQ(0, node1->self_ticks());
281 CHECK_EQ(0, node2->total_ticks());
282 CHECK_EQ(0, node2->self_ticks());
283 CHECK_EQ(0, node3->total_ticks());
284 CHECK_EQ(2, node3->self_ticks());
285
286 CodeEntry* path2[] = {&entry2, &entry2, &entry1};
287 Vector<CodeEntry*> path2_vec(path2, sizeof(path2) / sizeof(path2[0]));
288 tree.AddPathFromEnd(path2_vec);
289 CHECK_EQ(NULL, helper.Walk(&entry2));
290 CHECK_EQ(NULL, helper.Walk(&entry3));
291 CHECK_EQ(node1, helper.Walk(&entry1));
292 CHECK_EQ(NULL, helper.Walk(&entry1, &entry1));
293 CHECK_EQ(NULL, helper.Walk(&entry1, &entry3));
294 CHECK_EQ(node2, helper.Walk(&entry1, &entry2));
295 CHECK_EQ(NULL, helper.Walk(&entry1, &entry2, &entry1));
296 CHECK_EQ(node3, helper.Walk(&entry1, &entry2, &entry3));
297 CHECK_EQ(0, node3->total_ticks());
298 CHECK_EQ(2, node3->self_ticks());
299 ProfileNode* node4 = helper.Walk(&entry1, &entry2, &entry2);
300 CHECK_NE(NULL, node4);
301 CHECK_NE(node3, node4);
302 CHECK_EQ(0, node4->total_ticks());
303 CHECK_EQ(1, node4->self_ticks());
304}
305
306
307TEST(ProfileTreeCalculateTotalTicks) {
308 ProfileTree empty_tree;
309 CHECK_EQ(0, empty_tree.root()->total_ticks());
310 CHECK_EQ(0, empty_tree.root()->self_ticks());
311 empty_tree.CalculateTotalTicks();
312 CHECK_EQ(0, empty_tree.root()->total_ticks());
313 CHECK_EQ(0, empty_tree.root()->self_ticks());
314 empty_tree.root()->IncrementSelfTicks();
315 CHECK_EQ(0, empty_tree.root()->total_ticks());
316 CHECK_EQ(1, empty_tree.root()->self_ticks());
317 empty_tree.CalculateTotalTicks();
318 CHECK_EQ(1, empty_tree.root()->total_ticks());
319 CHECK_EQ(1, empty_tree.root()->self_ticks());
320
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000321 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
322 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000323 CodeEntry* e1_path[] = {&entry1};
324 Vector<CodeEntry*> e1_path_vec(
325 e1_path, sizeof(e1_path) / sizeof(e1_path[0]));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000326
327 ProfileTree single_child_tree;
328 single_child_tree.AddPathFromStart(e1_path_vec);
329 single_child_tree.root()->IncrementSelfTicks();
330 CHECK_EQ(0, single_child_tree.root()->total_ticks());
331 CHECK_EQ(1, single_child_tree.root()->self_ticks());
332 ProfileTreeTestHelper single_child_helper(&single_child_tree);
333 ProfileNode* node1 = single_child_helper.Walk(&entry1);
334 CHECK_NE(NULL, node1);
335 CHECK_EQ(0, node1->total_ticks());
336 CHECK_EQ(1, node1->self_ticks());
337 single_child_tree.CalculateTotalTicks();
338 CHECK_EQ(2, single_child_tree.root()->total_ticks());
339 CHECK_EQ(1, single_child_tree.root()->self_ticks());
340 CHECK_EQ(1, node1->total_ticks());
341 CHECK_EQ(1, node1->self_ticks());
342
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000343 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
344 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000345 CodeEntry* e1_e2_path[] = {&entry1, &entry2};
346 Vector<CodeEntry*> e1_e2_path_vec(
347 e1_e2_path, sizeof(e1_e2_path) / sizeof(e1_e2_path[0]));
348
349 ProfileTree flat_tree;
350 ProfileTreeTestHelper flat_helper(&flat_tree);
351 flat_tree.AddPathFromStart(e1_path_vec);
352 flat_tree.AddPathFromStart(e1_path_vec);
353 flat_tree.AddPathFromStart(e1_e2_path_vec);
354 flat_tree.AddPathFromStart(e1_e2_path_vec);
355 flat_tree.AddPathFromStart(e1_e2_path_vec);
356 // Results in {root,0,0} -> {entry1,0,2} -> {entry2,0,3}
357 CHECK_EQ(0, flat_tree.root()->total_ticks());
358 CHECK_EQ(0, flat_tree.root()->self_ticks());
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000359 node1 = flat_helper.Walk(&entry1);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000360 CHECK_NE(NULL, node1);
361 CHECK_EQ(0, node1->total_ticks());
362 CHECK_EQ(2, node1->self_ticks());
363 ProfileNode* node2 = flat_helper.Walk(&entry1, &entry2);
364 CHECK_NE(NULL, node2);
365 CHECK_EQ(0, node2->total_ticks());
366 CHECK_EQ(3, node2->self_ticks());
367 flat_tree.CalculateTotalTicks();
368 // Must calculate {root,5,0} -> {entry1,5,2} -> {entry2,3,3}
369 CHECK_EQ(5, flat_tree.root()->total_ticks());
370 CHECK_EQ(0, flat_tree.root()->self_ticks());
371 CHECK_EQ(5, node1->total_ticks());
372 CHECK_EQ(2, node1->self_ticks());
373 CHECK_EQ(3, node2->total_ticks());
374 CHECK_EQ(3, node2->self_ticks());
375
376 CodeEntry* e2_path[] = {&entry2};
377 Vector<CodeEntry*> e2_path_vec(
378 e2_path, sizeof(e2_path) / sizeof(e2_path[0]));
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000379 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
380 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000381 CodeEntry* e3_path[] = {&entry3};
382 Vector<CodeEntry*> e3_path_vec(
383 e3_path, sizeof(e3_path) / sizeof(e3_path[0]));
384
385 ProfileTree wide_tree;
386 ProfileTreeTestHelper wide_helper(&wide_tree);
387 wide_tree.AddPathFromStart(e1_path_vec);
388 wide_tree.AddPathFromStart(e1_path_vec);
389 wide_tree.AddPathFromStart(e1_e2_path_vec);
390 wide_tree.AddPathFromStart(e2_path_vec);
391 wide_tree.AddPathFromStart(e2_path_vec);
392 wide_tree.AddPathFromStart(e2_path_vec);
393 wide_tree.AddPathFromStart(e3_path_vec);
394 wide_tree.AddPathFromStart(e3_path_vec);
395 wide_tree.AddPathFromStart(e3_path_vec);
396 wide_tree.AddPathFromStart(e3_path_vec);
397 // Results in -> {entry1,0,2} -> {entry2,0,1}
398 // {root,0,0} -> {entry2,0,3}
399 // -> {entry3,0,4}
400 CHECK_EQ(0, wide_tree.root()->total_ticks());
401 CHECK_EQ(0, wide_tree.root()->self_ticks());
402 node1 = wide_helper.Walk(&entry1);
403 CHECK_NE(NULL, node1);
404 CHECK_EQ(0, node1->total_ticks());
405 CHECK_EQ(2, node1->self_ticks());
406 ProfileNode* node1_2 = wide_helper.Walk(&entry1, &entry2);
407 CHECK_NE(NULL, node1_2);
408 CHECK_EQ(0, node1_2->total_ticks());
409 CHECK_EQ(1, node1_2->self_ticks());
410 node2 = wide_helper.Walk(&entry2);
411 CHECK_NE(NULL, node2);
412 CHECK_EQ(0, node2->total_ticks());
413 CHECK_EQ(3, node2->self_ticks());
414 ProfileNode* node3 = wide_helper.Walk(&entry3);
415 CHECK_NE(NULL, node3);
416 CHECK_EQ(0, node3->total_ticks());
417 CHECK_EQ(4, node3->self_ticks());
418 wide_tree.CalculateTotalTicks();
419 // Calculates -> {entry1,3,2} -> {entry2,1,1}
420 // {root,10,0} -> {entry2,3,3}
421 // -> {entry3,4,4}
422 CHECK_EQ(10, wide_tree.root()->total_ticks());
423 CHECK_EQ(0, wide_tree.root()->self_ticks());
424 CHECK_EQ(3, node1->total_ticks());
425 CHECK_EQ(2, node1->self_ticks());
426 CHECK_EQ(1, node1_2->total_ticks());
427 CHECK_EQ(1, node1_2->self_ticks());
428 CHECK_EQ(3, node2->total_ticks());
429 CHECK_EQ(3, node2->self_ticks());
430 CHECK_EQ(4, node3->total_ticks());
431 CHECK_EQ(4, node3->self_ticks());
432}
433
434
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000435TEST(ProfileTreeFilteredClone) {
436 ProfileTree source_tree;
437 const int token0 = 0, token1 = 1, token2 = 2;
438 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0, token0);
439 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0, token1);
440 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0, token0);
441 CodeEntry entry4(
442 i::Logger::FUNCTION_TAG, "", "ddd", "", 0,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000443 TokenEnumerator::kInheritsSecurityToken);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000444
445 {
446 CodeEntry* e1_e2_path[] = {&entry1, &entry2};
447 Vector<CodeEntry*> e1_e2_path_vec(
448 e1_e2_path, sizeof(e1_e2_path) / sizeof(e1_e2_path[0]));
449 source_tree.AddPathFromStart(e1_e2_path_vec);
450 CodeEntry* e2_e4_path[] = {&entry2, &entry4};
451 Vector<CodeEntry*> e2_e4_path_vec(
452 e2_e4_path, sizeof(e2_e4_path) / sizeof(e2_e4_path[0]));
453 source_tree.AddPathFromStart(e2_e4_path_vec);
454 CodeEntry* e3_e1_path[] = {&entry3, &entry1};
455 Vector<CodeEntry*> e3_e1_path_vec(
456 e3_e1_path, sizeof(e3_e1_path) / sizeof(e3_e1_path[0]));
457 source_tree.AddPathFromStart(e3_e1_path_vec);
458 CodeEntry* e3_e2_path[] = {&entry3, &entry2};
459 Vector<CodeEntry*> e3_e2_path_vec(
460 e3_e2_path, sizeof(e3_e2_path) / sizeof(e3_e2_path[0]));
461 source_tree.AddPathFromStart(e3_e2_path_vec);
462 source_tree.CalculateTotalTicks();
463 // Results in -> {entry1,0,1,0} -> {entry2,1,1,1}
464 // {root,0,4,-1} -> {entry2,0,1,1} -> {entry4,1,1,inherits}
465 // -> {entry3,0,2,0} -> {entry1,1,1,0}
466 // -> {entry2,1,1,1}
467 CHECK_EQ(4, source_tree.root()->total_ticks());
468 CHECK_EQ(0, source_tree.root()->self_ticks());
469 }
470
471 {
472 ProfileTree token0_tree;
473 token0_tree.FilteredClone(&source_tree, token0);
474 // Should be -> {entry1,1,1,0}
475 // {root,1,4,-1} -> {entry3,1,2,0} -> {entry1,1,1,0}
476 // [self ticks from filtered nodes are attributed to their parents]
477 CHECK_EQ(4, token0_tree.root()->total_ticks());
478 CHECK_EQ(1, token0_tree.root()->self_ticks());
479 ProfileTreeTestHelper token0_helper(&token0_tree);
480 ProfileNode* node1 = token0_helper.Walk(&entry1);
481 CHECK_NE(NULL, node1);
482 CHECK_EQ(1, node1->total_ticks());
483 CHECK_EQ(1, node1->self_ticks());
484 CHECK_EQ(NULL, token0_helper.Walk(&entry2));
485 ProfileNode* node3 = token0_helper.Walk(&entry3);
486 CHECK_NE(NULL, node3);
487 CHECK_EQ(2, node3->total_ticks());
488 CHECK_EQ(1, node3->self_ticks());
489 ProfileNode* node3_1 = token0_helper.Walk(&entry3, &entry1);
490 CHECK_NE(NULL, node3_1);
491 CHECK_EQ(1, node3_1->total_ticks());
492 CHECK_EQ(1, node3_1->self_ticks());
493 CHECK_EQ(NULL, token0_helper.Walk(&entry3, &entry2));
494 }
495
496 {
497 ProfileTree token1_tree;
498 token1_tree.FilteredClone(&source_tree, token1);
499 // Should be
500 // {root,1,4,-1} -> {entry2,2,3,1} -> {entry4,1,1,inherits}
501 // [child nodes referring to the same entry get merged and
502 // their self times summed up]
503 CHECK_EQ(4, token1_tree.root()->total_ticks());
504 CHECK_EQ(1, token1_tree.root()->self_ticks());
505 ProfileTreeTestHelper token1_helper(&token1_tree);
506 CHECK_EQ(NULL, token1_helper.Walk(&entry1));
507 CHECK_EQ(NULL, token1_helper.Walk(&entry3));
508 ProfileNode* node2 = token1_helper.Walk(&entry2);
509 CHECK_NE(NULL, node2);
510 CHECK_EQ(3, node2->total_ticks());
511 CHECK_EQ(2, node2->self_ticks());
512 ProfileNode* node2_4 = token1_helper.Walk(&entry2, &entry4);
513 CHECK_NE(NULL, node2_4);
514 CHECK_EQ(1, node2_4->total_ticks());
515 CHECK_EQ(1, node2_4->self_ticks());
516 }
517
518 {
519 ProfileTree token2_tree;
520 token2_tree.FilteredClone(&source_tree, token2);
521 // Should be
522 // {root,4,4,-1}
523 // [no nodes, all ticks get migrated into root node]
524 CHECK_EQ(4, token2_tree.root()->total_ticks());
525 CHECK_EQ(4, token2_tree.root()->self_ticks());
526 ProfileTreeTestHelper token2_helper(&token2_tree);
527 CHECK_EQ(NULL, token2_helper.Walk(&entry1));
528 CHECK_EQ(NULL, token2_helper.Walk(&entry2));
529 CHECK_EQ(NULL, token2_helper.Walk(&entry3));
530 }
531}
532
533
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000534static inline i::Address ToAddress(int n) {
535 return reinterpret_cast<i::Address>(n);
536}
537
538TEST(CodeMapAddCode) {
539 CodeMap code_map;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000540 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
541 TokenEnumerator::kNoSecurityToken);
542 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
543 TokenEnumerator::kNoSecurityToken);
544 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
545 TokenEnumerator::kNoSecurityToken);
546 CodeEntry entry4(i::Logger::FUNCTION_TAG, "", "ddd", "", 0,
547 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000548 code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
549 code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
550 code_map.AddCode(ToAddress(0x1900), &entry3, 0x50);
551 code_map.AddCode(ToAddress(0x1950), &entry4, 0x10);
552 CHECK_EQ(NULL, code_map.FindEntry(0));
553 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1500 - 1)));
554 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
555 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x100)));
556 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500 + 0x200 - 1)));
557 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
558 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x50)));
559 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700 + 0x100 - 1)));
560 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1700 + 0x100)));
561 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1900 - 1)));
562 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900)));
563 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1900 + 0x28)));
564 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950)));
565 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x7)));
566 CHECK_EQ(&entry4, code_map.FindEntry(ToAddress(0x1950 + 0x10 - 1)));
567 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1950 + 0x10)));
568 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0xFFFFFFFF)));
569}
570
571
572TEST(CodeMapMoveAndDeleteCode) {
573 CodeMap code_map;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000574 CodeEntry entry1(i::Logger::FUNCTION_TAG, "", "aaa", "", 0,
575 TokenEnumerator::kNoSecurityToken);
576 CodeEntry entry2(i::Logger::FUNCTION_TAG, "", "bbb", "", 0,
577 TokenEnumerator::kNoSecurityToken);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000578 code_map.AddCode(ToAddress(0x1500), &entry1, 0x200);
579 code_map.AddCode(ToAddress(0x1700), &entry2, 0x100);
580 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1500)));
581 CHECK_EQ(&entry2, code_map.FindEntry(ToAddress(0x1700)));
lrn@chromium.org34e60782011-09-15 07:25:40 +0000582 code_map.MoveCode(ToAddress(0x1500), ToAddress(0x1700)); // Deprecate bbb.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000583 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1500)));
lrn@chromium.org34e60782011-09-15 07:25:40 +0000584 CHECK_EQ(&entry1, code_map.FindEntry(ToAddress(0x1700)));
585 CodeEntry entry3(i::Logger::FUNCTION_TAG, "", "ccc", "", 0,
586 TokenEnumerator::kNoSecurityToken);
587 code_map.AddCode(ToAddress(0x1750), &entry3, 0x100);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000588 CHECK_EQ(NULL, code_map.FindEntry(ToAddress(0x1700)));
lrn@chromium.org34e60782011-09-15 07:25:40 +0000589 CHECK_EQ(&entry3, code_map.FindEntry(ToAddress(0x1750)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000590}
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000591
592
ager@chromium.org357bf652010-04-12 11:30:10 +0000593namespace {
594
595class TestSetup {
596 public:
597 TestSetup()
598 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
599 i::FLAG_prof_browser_mode = false;
600 }
601
602 ~TestSetup() {
603 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
604 }
605
606 private:
607 bool old_flag_prof_browser_mode_;
608};
609
610} // namespace
611
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000612TEST(RecordTickSample) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000613 TestSetup test_setup;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000614 CpuProfilesCollection profiles;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000615 profiles.StartProfiling("", 1, false);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000616 ProfileGenerator generator(&profiles);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000617 CodeEntry* entry1 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
618 CodeEntry* entry2 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "bbb");
619 CodeEntry* entry3 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "ccc");
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000620 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
621 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
622 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
623
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000624 // We are building the following calls tree:
625 // -> aaa - sample1
626 // aaa -> bbb -> ccc - sample2
627 // -> ccc -> aaa - sample3
628 TickSample sample1;
629 sample1.pc = ToAddress(0x1600);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000630 sample1.tos = ToAddress(0x1500);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000631 sample1.stack[0] = ToAddress(0x1510);
632 sample1.frames_count = 1;
633 generator.RecordTickSample(sample1);
634 TickSample sample2;
635 sample2.pc = ToAddress(0x1925);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000636 sample2.tos = ToAddress(0x1900);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000637 sample2.stack[0] = ToAddress(0x1780);
638 sample2.stack[1] = ToAddress(0x10000); // non-existent.
639 sample2.stack[2] = ToAddress(0x1620);
640 sample2.frames_count = 3;
641 generator.RecordTickSample(sample2);
642 TickSample sample3;
643 sample3.pc = ToAddress(0x1510);
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000644 sample3.tos = ToAddress(0x1500);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000645 sample3.stack[0] = ToAddress(0x1910);
646 sample3.stack[1] = ToAddress(0x1610);
647 sample3.frames_count = 2;
648 generator.RecordTickSample(sample3);
649
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000650 CpuProfile* profile =
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000651 profiles.StopProfiling(TokenEnumerator::kNoSecurityToken, "", 1);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000652 CHECK_NE(NULL, profile);
653 ProfileTreeTestHelper top_down_test_helper(profile->top_down());
654 CHECK_EQ(NULL, top_down_test_helper.Walk(entry2));
655 CHECK_EQ(NULL, top_down_test_helper.Walk(entry3));
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000656 ProfileNode* node1 = top_down_test_helper.Walk(entry1);
657 CHECK_NE(NULL, node1);
658 CHECK_EQ(entry1, node1->entry());
659 ProfileNode* node2 = top_down_test_helper.Walk(entry1, entry1);
660 CHECK_NE(NULL, node2);
661 CHECK_EQ(entry1, node2->entry());
662 ProfileNode* node3 = top_down_test_helper.Walk(entry1, entry2, entry3);
663 CHECK_NE(NULL, node3);
664 CHECK_EQ(entry3, node3->entry());
665 ProfileNode* node4 = top_down_test_helper.Walk(entry1, entry3, entry1);
666 CHECK_NE(NULL, node4);
667 CHECK_EQ(entry1, node4->entry());
668}
lrn@chromium.org25156de2010-04-06 13:10:27 +0000669
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000670
671TEST(SampleRateCalculator) {
672 const double kSamplingIntervalMs = i::Logger::kSamplingIntervalMs;
673
674 // Verify that ticking exactly in query intervals results in the
675 // initial sampling interval.
676 double time = 0.0;
677 SampleRateCalculator calc1;
678 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
679 calc1.UpdateMeasurements(time);
680 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
681 time += SampleRateCalculator::kWallTimeQueryIntervalMs;
682 calc1.UpdateMeasurements(time);
683 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
684 time += SampleRateCalculator::kWallTimeQueryIntervalMs;
685 calc1.UpdateMeasurements(time);
686 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
687 time += SampleRateCalculator::kWallTimeQueryIntervalMs;
688 calc1.UpdateMeasurements(time);
689 CHECK_EQ(kSamplingIntervalMs, calc1.ticks_per_ms());
690
691 SampleRateCalculator calc2;
692 time = 0.0;
693 CHECK_EQ(kSamplingIntervalMs, calc2.ticks_per_ms());
694 calc2.UpdateMeasurements(time);
695 CHECK_EQ(kSamplingIntervalMs, calc2.ticks_per_ms());
696 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 0.5;
697 calc2.UpdateMeasurements(time);
698 // (1.0 + 2.0) / 2
699 CHECK_EQ(kSamplingIntervalMs * 1.5, calc2.ticks_per_ms());
700 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 0.75;
701 calc2.UpdateMeasurements(time);
702 // (1.0 + 2.0 + 2.0) / 3
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000703 CHECK_EQ(kSamplingIntervalMs * 5.0, floor(calc2.ticks_per_ms() * 3.0 + 0.5));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000704
705 SampleRateCalculator calc3;
706 time = 0.0;
707 CHECK_EQ(kSamplingIntervalMs, calc3.ticks_per_ms());
708 calc3.UpdateMeasurements(time);
709 CHECK_EQ(kSamplingIntervalMs, calc3.ticks_per_ms());
710 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 2;
711 calc3.UpdateMeasurements(time);
712 // (1.0 + 0.5) / 2
713 CHECK_EQ(kSamplingIntervalMs * 0.75, calc3.ticks_per_ms());
714 time += SampleRateCalculator::kWallTimeQueryIntervalMs * 1.5;
715 calc3.UpdateMeasurements(time);
716 // (1.0 + 0.5 + 0.5) / 3
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000717 CHECK_EQ(kSamplingIntervalMs * 2.0, floor(calc3.ticks_per_ms() * 3.0 + 0.5));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000718}
719
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000720
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000721static void CheckNodeIds(ProfileNode* node, int* expectedId) {
722 CHECK_EQ((*expectedId)++, node->id());
723 for (int i = 0; i < node->children()->length(); i++) {
724 CheckNodeIds(node->children()->at(i), expectedId);
725 }
726}
727
728TEST(SampleIds) {
729 TestSetup test_setup;
730 CpuProfilesCollection profiles;
731 profiles.StartProfiling("", 1, true);
732 ProfileGenerator generator(&profiles);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000733 CodeEntry* entry1 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
734 CodeEntry* entry2 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "bbb");
735 CodeEntry* entry3 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "ccc");
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000736 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
737 generator.code_map()->AddCode(ToAddress(0x1700), entry2, 0x100);
738 generator.code_map()->AddCode(ToAddress(0x1900), entry3, 0x50);
739
740 // We are building the following calls tree:
741 // -> aaa #3 - sample1
742 // (root)#1 -> aaa #2 -> bbb #4 -> ccc #5 - sample2
743 // -> ccc #6 -> aaa #7 - sample3
744 TickSample sample1;
745 sample1.pc = ToAddress(0x1600);
746 sample1.stack[0] = ToAddress(0x1510);
747 sample1.frames_count = 1;
748 generator.RecordTickSample(sample1);
749 TickSample sample2;
750 sample2.pc = ToAddress(0x1925);
751 sample2.stack[0] = ToAddress(0x1780);
752 sample2.stack[1] = ToAddress(0x10000); // non-existent.
753 sample2.stack[2] = ToAddress(0x1620);
754 sample2.frames_count = 3;
755 generator.RecordTickSample(sample2);
756 TickSample sample3;
757 sample3.pc = ToAddress(0x1510);
758 sample3.stack[0] = ToAddress(0x1910);
759 sample3.stack[1] = ToAddress(0x1610);
760 sample3.frames_count = 2;
761 generator.RecordTickSample(sample3);
762
763 CpuProfile* profile =
764 profiles.StopProfiling(TokenEnumerator::kNoSecurityToken, "", 1);
765 int nodeId = 1;
766 CheckNodeIds(profile->top_down()->root(), &nodeId);
767 CHECK_EQ(7, nodeId - 1);
768
769 CHECK_EQ(3, profile->samples_count());
770 int expected_id[] = {3, 5, 7};
771 for (int i = 0; i < 3; i++) {
772 CHECK_EQ(expected_id[i], profile->sample(i)->id());
773 }
774}
775
776
777TEST(NoSamples) {
778 TestSetup test_setup;
779 CpuProfilesCollection profiles;
780 profiles.StartProfiling("", 1, false);
781 ProfileGenerator generator(&profiles);
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000782 CodeEntry* entry1 = profiles.NewCodeEntry(i::Logger::FUNCTION_TAG, "aaa");
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000783 generator.code_map()->AddCode(ToAddress(0x1500), entry1, 0x200);
784
785 // We are building the following calls tree:
786 // (root)#1 -> aaa #2 -> aaa #3 - sample1
787 TickSample sample1;
788 sample1.pc = ToAddress(0x1600);
789 sample1.stack[0] = ToAddress(0x1510);
790 sample1.frames_count = 1;
791 generator.RecordTickSample(sample1);
792
793 CpuProfile* profile =
794 profiles.StopProfiling(TokenEnumerator::kNoSecurityToken, "", 1);
795 int nodeId = 1;
796 CheckNodeIds(profile->top_down()->root(), &nodeId);
797 CHECK_EQ(3, nodeId - 1);
798
799 CHECK_EQ(0, profile->samples_count());
800}
801
802
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000803// --- P r o f i l e r E x t e n s i o n ---
804
805class ProfilerExtension : public v8::Extension {
806 public:
807 ProfilerExtension() : v8::Extension("v8/profiler", kSource) { }
808 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
809 v8::Handle<v8::String> name);
810 static v8::Handle<v8::Value> StartProfiling(const v8::Arguments& args);
811 static v8::Handle<v8::Value> StopProfiling(const v8::Arguments& args);
812 private:
813 static const char* kSource;
814};
815
816
817const char* ProfilerExtension::kSource =
818 "native function startProfiling();"
819 "native function stopProfiling();";
820
821v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction(
822 v8::Handle<v8::String> name) {
823 if (name->Equals(v8::String::New("startProfiling"))) {
824 return v8::FunctionTemplate::New(ProfilerExtension::StartProfiling);
825 } else if (name->Equals(v8::String::New("stopProfiling"))) {
826 return v8::FunctionTemplate::New(ProfilerExtension::StopProfiling);
827 } else {
828 CHECK(false);
829 return v8::Handle<v8::FunctionTemplate>();
830 }
831}
832
833
834v8::Handle<v8::Value> ProfilerExtension::StartProfiling(
835 const v8::Arguments& args) {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000836 v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000837 if (args.Length() > 0)
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000838 cpu_profiler->StartCpuProfiling(args[0].As<v8::String>());
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000839 else
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000840 cpu_profiler->StartCpuProfiling(v8::String::New(""));
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000841 return v8::Undefined();
842}
843
844
845v8::Handle<v8::Value> ProfilerExtension::StopProfiling(
846 const v8::Arguments& args) {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000847 v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000848 if (args.Length() > 0)
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000849 cpu_profiler->StopCpuProfiling(args[0].As<v8::String>());
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000850 else
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000851 cpu_profiler->StopCpuProfiling(v8::String::New(""));
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000852 return v8::Undefined();
853}
854
855
856static ProfilerExtension kProfilerExtension;
857v8::DeclareExtension kProfilerExtensionDeclaration(&kProfilerExtension);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000858
859static const ProfileNode* PickChild(const ProfileNode* parent,
860 const char* name) {
861 for (int i = 0; i < parent->children()->length(); ++i) {
862 const ProfileNode* child = parent->children()->at(i);
863 if (strcmp(child->entry()->name(), name) == 0) return child;
864 }
865 return NULL;
866}
867
868
869TEST(RecordStackTraceAtStartProfiling) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000870 // This test does not pass with inlining enabled since inlined functions
871 // don't appear in the stack trace.
872 i::FLAG_use_inlining = false;
873
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000874 v8::Isolate* isolate = v8::Isolate::GetCurrent();
875 v8::HandleScope scope(isolate);
876 const char* extensions[] = { "v8/profiler" };
877 v8::ExtensionConfiguration config(1, extensions);
878 v8::Local<v8::Context> context = v8::Context::New(isolate);
879 context->Enter();
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000880
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000881 CpuProfiler* profiler = i::Isolate::Current()->cpu_profiler();
882 CHECK_EQ(0, profiler->GetProfilesCount());
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000883 CompileRun(
884 "function c() { startProfiling(); }\n"
885 "function b() { c(); }\n"
886 "function a() { b(); }\n"
887 "a();\n"
888 "stopProfiling();");
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000889 CHECK_EQ(1, profiler->GetProfilesCount());
890 CpuProfile* profile = profiler->GetProfile(NULL, 0);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000891 const ProfileTree* topDown = profile->top_down();
892 const ProfileNode* current = topDown->root();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000893 const_cast<ProfileNode*>(current)->Print(0);
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000894 // The tree should look like this:
895 // (root)
896 // (anonymous function)
897 // a
898 // b
899 // c
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000900 // There can also be:
901 // startProfiling
902 // if the sampler managed to get a tick.
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000903 current = PickChild(current, "(anonymous function)");
904 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
905 current = PickChild(current, "a");
906 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
907 current = PickChild(current, "b");
908 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
909 current = PickChild(current, "c");
910 CHECK_NE(NULL, const_cast<ProfileNode*>(current));
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000911 CHECK(current->children()->length() == 0 ||
912 current->children()->length() == 1);
913 if (current->children()->length() == 1) {
914 current = PickChild(current, "startProfiling");
915 CHECK_EQ(0, current->children()->length());
916 }
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000917}
918
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000919
920TEST(Issue51919) {
921 CpuProfilesCollection collection;
922 i::EmbeddedVector<char*,
923 CpuProfilesCollection::kMaxSimultaneousProfiles> titles;
924 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) {
925 i::Vector<char> title = i::Vector<char>::New(16);
926 i::OS::SNPrintF(title, "%d", i);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000927 // UID must be > 0.
928 CHECK(collection.StartProfiling(title.start(), i + 1, false));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000929 titles[i] = title.start();
930 }
931 CHECK(!collection.StartProfiling(
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000932 "maximum", CpuProfilesCollection::kMaxSimultaneousProfiles + 1, false));
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000933 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i)
934 i::DeleteArray(titles[i]);
935}