blob: 361c879af39935f40b845e57d6cc3d3ab0948f18 [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"
Steve Block6ded16b2010-05-10 14:33:55 +010031
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "include/v8-profiler.h"
33#include "src/base/platform/platform.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034#include "src/base/smart-pointers.h"
35#include "src/deoptimizer.h"
36#include "src/profiler/cpu-profiler-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037#include "src/utils.h"
38#include "test/cctest/cctest.h"
39#include "test/cctest/profiler-extension.h"
Steve Block6ded16b2010-05-10 14:33:55 +010040using i::CodeEntry;
41using i::CpuProfile;
Iain Merrick75681382010-08-19 15:07:18 +010042using i::CpuProfiler;
Steve Block6ded16b2010-05-10 14:33:55 +010043using i::CpuProfilesCollection;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044using i::Heap;
Steve Block6ded16b2010-05-10 14:33:55 +010045using i::ProfileGenerator;
46using i::ProfileNode;
47using i::ProfilerEventsProcessor;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048using i::ScopedVector;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049using i::Vector;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050using v8::base::SmartPointer;
51
52
53// Helper methods
54static v8::Local<v8::Function> GetFunction(v8::Local<v8::Context> env,
55 const char* name) {
56 return v8::Local<v8::Function>::Cast(
57 env->Global()->Get(env, v8_str(name)).ToLocalChecked());
58}
59
60
61static size_t offset(const char* src, const char* substring) {
62 const char* it = strstr(src, substring);
63 CHECK(it);
64 return static_cast<size_t>(it - src);
65}
66
67
68static const char* reason(const i::Deoptimizer::DeoptReason reason) {
69 return i::Deoptimizer::GetDeoptReason(reason);
70}
Steve Block6ded16b2010-05-10 14:33:55 +010071
72
73TEST(StartStop) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 i::Isolate* isolate = CcTest::i_isolate();
75 CpuProfilesCollection profiles(isolate->heap());
Steve Block6ded16b2010-05-10 14:33:55 +010076 ProfileGenerator generator(&profiles);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
Ben Murdoch097c5b22016-05-18 11:27:45 +010078 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 processor->Start();
80 processor->StopSynchronously();
Steve Block6ded16b2010-05-10 14:33:55 +010081}
82
Steve Block6ded16b2010-05-10 14:33:55 +010083
84static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc,
85 i::Address frame1,
86 i::Address frame2 = NULL,
87 i::Address frame3 = NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088 i::TickSample* sample = proc->StartTickSample();
Steve Block6ded16b2010-05-10 14:33:55 +010089 sample->pc = frame1;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010090 sample->tos = frame1;
Steve Block6ded16b2010-05-10 14:33:55 +010091 sample->frames_count = 0;
92 if (frame2 != NULL) {
93 sample->stack[0] = frame2;
94 sample->frames_count = 1;
95 }
96 if (frame3 != NULL) {
97 sample->stack[1] = frame3;
98 sample->frames_count = 2;
99 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 proc->FinishTickSample();
Steve Block6ded16b2010-05-10 14:33:55 +0100101}
102
103namespace {
104
105class TestSetup {
106 public:
107 TestSetup()
108 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
109 i::FLAG_prof_browser_mode = false;
110 }
111
112 ~TestSetup() {
113 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
114 }
115
116 private:
117 bool old_flag_prof_browser_mode_;
118};
119
120} // namespace
121
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122
123i::Code* CreateCode(LocalContext* env) {
124 static int counter = 0;
125 i::EmbeddedVector<char, 256> script;
126 i::EmbeddedVector<char, 32> name;
127
128 i::SNPrintF(name, "function_%d", ++counter);
129 const char* name_start = name.start();
130 i::SNPrintF(script,
131 "function %s() {\n"
132 "var counter = 0;\n"
133 "for (var i = 0; i < %d; ++i) counter += i;\n"
134 "return '%s_' + counter;\n"
135 "}\n"
136 "%s();\n", name_start, counter, name_start, name_start);
137 CompileRun(script.start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138
139 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(
140 v8::Utils::OpenHandle(*GetFunction(env->local(), name_start)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 return fun->code();
142}
143
144
Steve Block6ded16b2010-05-10 14:33:55 +0100145TEST(CodeEvents) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 CcTest::InitializeVM();
147 LocalContext env;
148 i::Isolate* isolate = CcTest::i_isolate();
149 i::Factory* factory = isolate->factory();
Steve Block6ded16b2010-05-10 14:33:55 +0100150 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151
152 i::HandleScope scope(isolate);
153
154 i::Code* aaa_code = CreateCode(&env);
155 i::Code* comment_code = CreateCode(&env);
156 i::Code* args5_code = CreateCode(&env);
157 i::Code* comment2_code = CreateCode(&env);
158 i::Code* moved_code = CreateCode(&env);
159 i::Code* args3_code = CreateCode(&env);
160 i::Code* args4_code = CreateCode(&env);
161
162 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
163 profiles->StartProfiling("", false);
164 ProfileGenerator generator(profiles);
165 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
166 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
167 processor->Start();
168 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
Steve Block6ded16b2010-05-10 14:33:55 +0100169
170 // Enqueue code creation events.
Steve Block6ded16b2010-05-10 14:33:55 +0100171 const char* aaa_str = "aaa";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str);
173 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code, *aaa_name);
174 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code, "comment");
175 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5);
176 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code, "comment2");
177 profiler.CodeMoveEvent(comment2_code->address(), moved_code->address());
178 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3);
179 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4);
Steve Block6ded16b2010-05-10 14:33:55 +0100180
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 // Enqueue a tick event to enable code events processing.
182 EnqueueTickSampleEvent(processor.get(), aaa_code->address());
183
184 processor->StopSynchronously();
Steve Block6ded16b2010-05-10 14:33:55 +0100185
186 // Check the state of profile generator.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 CodeEntry* aaa = generator.code_map()->FindEntry(aaa_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 CHECK(aaa);
189 CHECK_EQ(0, strcmp(aaa_str, aaa->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190
191 CodeEntry* comment = generator.code_map()->FindEntry(comment_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192 CHECK(comment);
193 CHECK_EQ(0, strcmp("comment", comment->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194
195 CodeEntry* args5 = generator.code_map()->FindEntry(args5_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 CHECK(args5);
197 CHECK_EQ(0, strcmp("5", args5->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 CHECK(!generator.code_map()->FindEntry(comment2_code->address()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200
201 CodeEntry* comment2 = generator.code_map()->FindEntry(moved_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000202 CHECK(comment2);
203 CHECK_EQ(0, strcmp("comment2", comment2->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100204}
205
206
207template<typename T>
208static int CompareProfileNodes(const T* p1, const T* p2) {
209 return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
210}
211
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212
Steve Block6ded16b2010-05-10 14:33:55 +0100213TEST(TickEvents) {
214 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 LocalContext env;
216 i::Isolate* isolate = CcTest::i_isolate();
217 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100218
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 i::Code* frame1_code = CreateCode(&env);
220 i::Code* frame2_code = CreateCode(&env);
221 i::Code* frame3_code = CreateCode(&env);
Steve Block6ded16b2010-05-10 14:33:55 +0100222
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
224 profiles->StartProfiling("", false);
225 ProfileGenerator generator(profiles);
226 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
227 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
228 processor->Start();
229 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
230
231 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb");
232 profiler.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5);
233 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd");
234
235 EnqueueTickSampleEvent(processor.get(), frame1_code->instruction_start());
236 EnqueueTickSampleEvent(
237 processor.get(),
238 frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2,
239 frame1_code->instruction_start() + frame2_code->ExecutableSize() / 2);
240 EnqueueTickSampleEvent(
241 processor.get(),
242 frame3_code->instruction_end() - 1,
243 frame2_code->instruction_end() - 1,
244 frame1_code->instruction_end() - 1);
245
246 processor->StopSynchronously();
247 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000248 CHECK(profile);
Steve Block6ded16b2010-05-10 14:33:55 +0100249
250 // Check call trees.
251 const i::List<ProfileNode*>* top_down_root_children =
252 profile->top_down()->root()->children();
253 CHECK_EQ(1, top_down_root_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100255 const i::List<ProfileNode*>* top_down_bbb_children =
256 top_down_root_children->last()->children();
257 CHECK_EQ(1, top_down_bbb_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258 CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100259 const i::List<ProfileNode*>* top_down_stub_children =
260 top_down_bbb_children->last()->children();
261 CHECK_EQ(1, top_down_stub_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000262 CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100263 const i::List<ProfileNode*>* top_down_ddd_children =
264 top_down_stub_children->last()->children();
265 CHECK_EQ(0, top_down_ddd_children->length());
Steve Block6ded16b2010-05-10 14:33:55 +0100266}
267
Iain Merrick75681382010-08-19 15:07:18 +0100268
269// http://crbug/51594
270// This test must not crash.
271TEST(CrashIfStoppingLastNonExistentProfile) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 CcTest::InitializeVM();
Iain Merrick75681382010-08-19 15:07:18 +0100273 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
275 profiler->StartProfiling("1");
276 profiler->StopProfiling("2");
277 profiler->StartProfiling("1");
278 profiler->StopProfiling("");
Iain Merrick75681382010-08-19 15:07:18 +0100279}
280
Steve Block44f0eee2011-05-26 01:26:41 +0100281
Steve Block053d10c2011-06-13 19:13:29 +0100282// http://code.google.com/p/v8/issues/detail?id=1398
283// Long stacks (exceeding max frames limit) must not be erased.
284TEST(Issue1398) {
285 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 LocalContext env;
287 i::Isolate* isolate = CcTest::i_isolate();
288 i::HandleScope scope(isolate);
Steve Block053d10c2011-06-13 19:13:29 +0100289
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 i::Code* code = CreateCode(&env);
Steve Block053d10c2011-06-13 19:13:29 +0100291
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
293 profiles->StartProfiling("", false);
294 ProfileGenerator generator(profiles);
295 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
296 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
297 processor->Start();
298 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
299
300 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb");
301
302 i::TickSample* sample = processor->StartTickSample();
303 sample->pc = code->address();
Steve Block053d10c2011-06-13 19:13:29 +0100304 sample->tos = 0;
305 sample->frames_count = i::TickSample::kMaxFramesCount;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 for (unsigned i = 0; i < sample->frames_count; ++i) {
307 sample->stack[i] = code->address();
Steve Block053d10c2011-06-13 19:13:29 +0100308 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 processor->FinishTickSample();
Steve Block053d10c2011-06-13 19:13:29 +0100310
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 processor->StopSynchronously();
312 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313 CHECK(profile);
Steve Block053d10c2011-06-13 19:13:29 +0100314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 unsigned actual_depth = 0;
Steve Block053d10c2011-06-13 19:13:29 +0100316 const ProfileNode* node = profile->top_down()->root();
317 while (node->children()->length() > 0) {
318 node = node->children()->last();
319 ++actual_depth;
320 }
321
322 CHECK_EQ(1 + i::TickSample::kMaxFramesCount, actual_depth); // +1 for PC.
323}
324
325
Steve Block44f0eee2011-05-26 01:26:41 +0100326TEST(DeleteAllCpuProfiles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327 CcTest::InitializeVM();
Steve Block44f0eee2011-05-26 01:26:41 +0100328 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
330 CHECK_EQ(0, profiler->GetProfilesCount());
331 profiler->DeleteAllProfiles();
332 CHECK_EQ(0, profiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100333
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 profiler->StartProfiling("1");
335 profiler->StopProfiling("1");
336 CHECK_EQ(1, profiler->GetProfilesCount());
337 profiler->DeleteAllProfiles();
338 CHECK_EQ(0, profiler->GetProfilesCount());
339 profiler->StartProfiling("1");
340 profiler->StartProfiling("2");
341 profiler->StopProfiling("2");
342 profiler->StopProfiling("1");
343 CHECK_EQ(2, profiler->GetProfilesCount());
344 profiler->DeleteAllProfiles();
345 CHECK_EQ(0, profiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100346
347 // Test profiling cancellation by the 'delete' command.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348 profiler->StartProfiling("1");
349 profiler->StartProfiling("2");
350 CHECK_EQ(0, profiler->GetProfilesCount());
351 profiler->DeleteAllProfiles();
352 CHECK_EQ(0, profiler->GetProfilesCount());
353}
Steve Block44f0eee2011-05-26 01:26:41 +0100354
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355
356static bool FindCpuProfile(v8::CpuProfiler* v8profiler,
357 const v8::CpuProfile* v8profile) {
358 i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler);
359 const i::CpuProfile* profile =
360 reinterpret_cast<const i::CpuProfile*>(v8profile);
361 int length = profiler->GetProfilesCount();
362 for (int i = 0; i < length; i++) {
363 if (profile == profiler->GetProfile(i))
364 return true;
365 }
366 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100367}
368
369
370TEST(DeleteCpuProfile) {
Steve Block44f0eee2011-05-26 01:26:41 +0100371 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 v8::HandleScope scope(env->GetIsolate());
373 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
374 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler);
Steve Block44f0eee2011-05-26 01:26:41 +0100375
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000377 v8::Local<v8::String> name1 = v8_str("1");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378 cpu_profiler->StartProfiling(name1);
379 v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000380 CHECK(p1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 CHECK_EQ(1, iprofiler->GetProfilesCount());
382 CHECK(FindCpuProfile(cpu_profiler, p1));
383 p1->Delete();
384 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100385
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 v8::Local<v8::String> name2 = v8_str("2");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 cpu_profiler->StartProfiling(name2);
388 v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 CHECK(p2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 CHECK_EQ(1, iprofiler->GetProfilesCount());
391 CHECK(FindCpuProfile(cpu_profiler, p2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000392 v8::Local<v8::String> name3 = v8_str("3");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 cpu_profiler->StartProfiling(name3);
394 v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000395 CHECK(p3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396 CHECK_EQ(2, iprofiler->GetProfilesCount());
397 CHECK_NE(p2, p3);
398 CHECK(FindCpuProfile(cpu_profiler, p3));
399 CHECK(FindCpuProfile(cpu_profiler, p2));
400 p2->Delete();
401 CHECK_EQ(1, iprofiler->GetProfilesCount());
402 CHECK(!FindCpuProfile(cpu_profiler, p2));
403 CHECK(FindCpuProfile(cpu_profiler, p3));
404 p3->Delete();
405 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100406}
407
408
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409TEST(ProfileStartEndTime) {
Steve Block44f0eee2011-05-26 01:26:41 +0100410 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411 v8::HandleScope scope(env->GetIsolate());
412 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100413
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000414 v8::Local<v8::String> profile_name = v8_str("test");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 cpu_profiler->StartProfiling(profile_name);
416 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
417 CHECK(profile->GetStartTime() <= profile->GetEndTime());
418}
Steve Block44f0eee2011-05-26 01:26:41 +0100419
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420static v8::CpuProfile* RunProfiler(v8::Local<v8::Context> env,
421 v8::Local<v8::Function> function,
422 v8::Local<v8::Value> argv[], int argc,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100423 unsigned min_js_samples = 0,
424 unsigned min_external_samples = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000425 bool collect_samples = false) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000426 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000427 v8::Local<v8::String> profile_name = v8_str("my_profile");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428
Ben Murdoch097c5b22016-05-18 11:27:45 +0100429 cpu_profiler->SetSamplingInterval(100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000430 cpu_profiler->StartProfiling(profile_name, collect_samples);
431
432 i::Sampler* sampler =
433 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler();
434 sampler->StartCountingSamples();
435 do {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000436 function->Call(env, env->Global(), argc, argv).ToLocalChecked();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100437 } while (sampler->js_sample_count() < min_js_samples ||
438 sampler->external_sample_count() < min_external_samples);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439
440 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
441
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000442 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443 // Dump collected profile to have a better diagnostic in case of failure.
444 reinterpret_cast<i::CpuProfile*>(profile)->Print();
445
446 return profile;
447}
448
449
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450static const v8::CpuProfileNode* FindChild(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 const v8::CpuProfileNode* node,
452 const char* name) {
453 int count = node->GetChildrenCount();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100454 v8::Local<v8::String> name_handle = v8_str(name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 for (int i = 0; i < count; i++) {
456 const v8::CpuProfileNode* child = node->GetChild(i);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100457 if (name_handle->Equals(context, child->GetFunctionName()).FromJust()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458 return child;
459 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 }
461 return NULL;
462}
463
464
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465static const v8::CpuProfileNode* GetChild(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466 const v8::CpuProfileNode* node,
467 const char* name) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000468 const v8::CpuProfileNode* result = FindChild(context, node, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 if (!result) {
470 char buffer[100];
471 i::SNPrintF(Vector<char>(buffer, arraysize(buffer)),
472 "Failed to GetChild: %s", name);
473 FATAL(buffer);
474 }
475 return result;
476}
477
478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479static void CheckSimpleBranch(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 const v8::CpuProfileNode* node,
481 const char* names[], int length) {
482 for (int i = 0; i < length; i++) {
483 const char* name = names[i];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000484 node = GetChild(context, node, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000485 }
486}
487
488
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000489static const ProfileNode* GetSimpleBranch(v8::Local<v8::Context> context,
490 v8::CpuProfile* profile,
491 const char* names[], int length) {
492 const v8::CpuProfileNode* node = profile->GetTopDownRoot();
493 for (int i = 0; i < length; i++) {
494 node = GetChild(context, node, names[i]);
495 }
496 return reinterpret_cast<const ProfileNode*>(node);
497}
498
Ben Murdoch097c5b22016-05-18 11:27:45 +0100499static void CallCollectSample(const v8::FunctionCallbackInfo<v8::Value>& info) {
500 info.GetIsolate()->GetCpuProfiler()->CollectSample();
501}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502
Ben Murdoch097c5b22016-05-18 11:27:45 +0100503static const char* cpu_profiler_test_source =
504 "%NeverOptimizeFunction(loop);\n"
505 "%NeverOptimizeFunction(delay);\n"
506 "%NeverOptimizeFunction(bar);\n"
507 "%NeverOptimizeFunction(baz);\n"
508 "%NeverOptimizeFunction(foo);\n"
509 "%NeverOptimizeFunction(start);\n"
510 "function loop(timeout) {\n"
511 " this.mmm = 0;\n"
512 " var start = Date.now();\n"
513 " do {\n"
514 " var n = 1000;\n"
515 " while(n > 1) {\n"
516 " n--;\n"
517 " this.mmm += n * n * n;\n"
518 " }\n"
519 " } while (Date.now() - start < timeout);\n"
520 "}\n"
521 "function delay() { loop(10); }\n"
522 "function bar() { delay(); }\n"
523 "function baz() { delay(); }\n"
524 "function foo() {\n"
525 " delay();\n"
526 " bar();\n"
527 " delay();\n"
528 " baz();\n"
529 "}\n"
530 "function start(duration) {\n"
531 " var start = Date.now();\n"
532 " do {\n"
533 " foo();\n"
534 " } while (Date.now() - start < duration);\n"
535 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536
537// Check that the profile tree for the script above will look like the
538// following:
539//
540// [Top down]:
541// 1062 0 (root) [-1]
542// 1054 0 start [-1]
543// 1054 1 foo [-1]
544// 265 0 baz [-1]
545// 265 1 delay [-1]
546// 264 264 loop [-1]
547// 525 3 delay [-1]
548// 522 522 loop [-1]
549// 263 0 bar [-1]
550// 263 1 delay [-1]
551// 262 262 loop [-1]
552// 2 2 (program) [-1]
553// 6 6 (garbage collector) [-1]
554TEST(CollectCpuProfile) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100555 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 LocalContext env;
557 v8::HandleScope scope(env->GetIsolate());
558
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000559 CompileRun(cpu_profiler_test_source);
560 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000561
562 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000563 v8::Local<v8::Value> args[] = {
564 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100566 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000567
568 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100569 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
570 const v8::CpuProfileNode* foo_node = GetChild(env.local(), start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571
Ben Murdoch097c5b22016-05-18 11:27:45 +0100572 const char* bar_branch[] = {"bar", "delay", "loop"};
573 CheckSimpleBranch(env.local(), foo_node, bar_branch, arraysize(bar_branch));
574 const char* baz_branch[] = {"baz", "delay", "loop"};
575 CheckSimpleBranch(env.local(), foo_node, baz_branch, arraysize(baz_branch));
576 const char* delay_branch[] = {"delay", "loop"};
577 CheckSimpleBranch(env.local(), foo_node, delay_branch,
578 arraysize(delay_branch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000579
580 profile->Delete();
581}
582
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583static const char* hot_deopt_no_frame_entry_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100584 "%NeverOptimizeFunction(foo);\n"
585 "%NeverOptimizeFunction(start);\n"
586 "function foo(a, b) {\n"
587 " return a + b;\n"
588 "}\n"
589 "function start(timeout) {\n"
590 " var start = Date.now();\n"
591 " do {\n"
592 " for (var i = 1; i < 1000; ++i) foo(1, i);\n"
593 " var duration = Date.now() - start;\n"
594 " } while (duration < timeout);\n"
595 " return duration;\n"
596 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597
598// Check that the profile tree for the script above will look like the
599// following:
600//
601// [Top down]:
602// 1062 0 (root) [-1]
603// 1054 0 start [-1]
604// 1054 1 foo [-1]
605// 2 2 (program) [-1]
606// 6 6 (garbage collector) [-1]
607//
Ben Murdoch097c5b22016-05-18 11:27:45 +0100608// The test checks no FP ranges are present in a deoptimized function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609// If 'foo' has no ranges the samples falling into the prologue will miss the
610// 'start' function on the stack, so 'foo' will be attached to the (root).
611TEST(HotDeoptNoFrameEntry) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100612 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613 LocalContext env;
614 v8::HandleScope scope(env->GetIsolate());
615
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000616 CompileRun(hot_deopt_no_frame_entry_test_source);
617 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618
619 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000620 v8::Local<v8::Value> args[] = {
621 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100623 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000624 function->Call(env.local(), env->Global(), arraysize(args), args)
625 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626
627 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100628 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
629 GetChild(env.local(), start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630
631 profile->Delete();
632}
633
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634TEST(CollectCpuProfileSamples) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100635 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 LocalContext env;
637 v8::HandleScope scope(env->GetIsolate());
638
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000639 CompileRun(cpu_profiler_test_source);
640 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641
642 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000643 v8::Local<v8::Value> args[] = {
644 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100646 RunProfiler(env.local(), function, args, arraysize(args), 1000, 0, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647
648 CHECK_LE(200, profile->GetSamplesCount());
649 uint64_t end_time = profile->GetEndTime();
650 uint64_t current_time = profile->GetStartTime();
651 CHECK_LE(current_time, end_time);
652 for (int i = 0; i < profile->GetSamplesCount(); i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000653 CHECK(profile->GetSample(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 uint64_t timestamp = profile->GetSampleTimestamp(i);
655 CHECK_LE(current_time, timestamp);
656 CHECK_LE(timestamp, end_time);
657 current_time = timestamp;
658 }
659
660 profile->Delete();
661}
662
Ben Murdoch097c5b22016-05-18 11:27:45 +0100663static const char* cpu_profiler_test_source2 =
664 "%NeverOptimizeFunction(loop);\n"
665 "%NeverOptimizeFunction(delay);\n"
666 "%NeverOptimizeFunction(start);\n"
667 "function loop() {}\n"
668 "function delay() { loop(); }\n"
669 "function start(duration) {\n"
670 " var start = Date.now();\n"
671 " do {\n"
672 " for (var i = 0; i < 10000; ++i) delay();\n"
673 " } while (Date.now() - start < duration);\n"
674 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675
676// Check that the profile tree doesn't contain unexpected traces:
677// - 'loop' can be called only by 'delay'
678// - 'delay' may be called only by 'start'
679// The profile will look like the following:
680//
681// [Top down]:
682// 135 0 (root) [-1] #1
683// 121 72 start [-1] #3
684// 49 33 delay [-1] #4
685// 16 16 loop [-1] #5
686// 14 14 (program) [-1] #2
687TEST(SampleWhenFrameIsNotSetup) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100688 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 LocalContext env;
690 v8::HandleScope scope(env->GetIsolate());
691
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000692 CompileRun(cpu_profiler_test_source2);
693 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000694
Ben Murdoch097c5b22016-05-18 11:27:45 +0100695 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000696 v8::Local<v8::Value> args[] = {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100697 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100699 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700
701 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100702 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
703 const v8::CpuProfileNode* delay_node =
704 GetChild(env.local(), start_node, "delay");
705 GetChild(env.local(), delay_node, "loop");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000706
707 profile->Delete();
708}
709
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000710static const char* native_accessor_test_source = "function start(count) {\n"
711" for (var i = 0; i < count; i++) {\n"
712" var o = instance.foo;\n"
713" instance.foo = o + 1;\n"
714" }\n"
715"}\n";
716
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717class TestApiCallbacks {
718 public:
719 explicit TestApiCallbacks(int min_duration_ms)
720 : min_duration_ms_(min_duration_ms),
721 is_warming_up_(false) {}
722
723 static void Getter(v8::Local<v8::String> name,
724 const v8::PropertyCallbackInfo<v8::Value>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100725 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726 data->Wait();
727 }
728
729 static void Setter(v8::Local<v8::String> name,
730 v8::Local<v8::Value> value,
731 const v8::PropertyCallbackInfo<void>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100732 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 data->Wait();
734 }
735
736 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100737 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 data->Wait();
739 }
740
741 void set_warming_up(bool value) { is_warming_up_ = value; }
742
743 private:
744 void Wait() {
745 if (is_warming_up_) return;
746 double start = v8::base::OS::TimeCurrentMillis();
747 double duration = 0;
748 while (duration < min_duration_ms_) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000749 v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750 duration = v8::base::OS::TimeCurrentMillis() - start;
751 }
752 }
753
Ben Murdoch097c5b22016-05-18 11:27:45 +0100754 template <typename T>
755 static TestApiCallbacks* FromInfo(const T& info) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 void* data = v8::External::Cast(*info.Data())->Value();
757 return reinterpret_cast<TestApiCallbacks*>(data);
758 }
759
760 int min_duration_ms_;
761 bool is_warming_up_;
762};
763
764
765// Test that native accessors are properly reported in the CPU profile.
766// This test checks the case when the long-running accessors are called
767// only once and the optimizer doesn't have chance to change the invocation
768// code.
769TEST(NativeAccessorUninitializedIC) {
770 LocalContext env;
771 v8::Isolate* isolate = env->GetIsolate();
772 v8::HandleScope scope(isolate);
773
774 v8::Local<v8::FunctionTemplate> func_template =
775 v8::FunctionTemplate::New(isolate);
776 v8::Local<v8::ObjectTemplate> instance_template =
777 func_template->InstanceTemplate();
778
779 TestApiCallbacks accessors(100);
780 v8::Local<v8::External> data =
781 v8::External::New(isolate, &accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000782 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
783 &TestApiCallbacks::Setter, data);
784 v8::Local<v8::Function> func =
785 func_template->GetFunction(env.local()).ToLocalChecked();
786 v8::Local<v8::Object> instance =
787 func->NewInstance(env.local()).ToLocalChecked();
788 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000790 CompileRun(native_accessor_test_source);
791 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792
793 int32_t repeat_count = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000795 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100796 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000797
798 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100799 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
800 GetChild(env.local(), start_node, "get foo");
801 GetChild(env.local(), start_node, "set foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000802
803 profile->Delete();
804}
805
806
807// Test that native accessors are properly reported in the CPU profile.
808// This test makes sure that the accessors are called enough times to become
809// hot and to trigger optimizations.
810TEST(NativeAccessorMonomorphicIC) {
811 LocalContext env;
812 v8::Isolate* isolate = env->GetIsolate();
813 v8::HandleScope scope(isolate);
814
815 v8::Local<v8::FunctionTemplate> func_template =
816 v8::FunctionTemplate::New(isolate);
817 v8::Local<v8::ObjectTemplate> instance_template =
818 func_template->InstanceTemplate();
819
820 TestApiCallbacks accessors(1);
821 v8::Local<v8::External> data =
822 v8::External::New(isolate, &accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000823 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
824 &TestApiCallbacks::Setter, data);
825 v8::Local<v8::Function> func =
826 func_template->GetFunction(env.local()).ToLocalChecked();
827 v8::Local<v8::Object> instance =
828 func->NewInstance(env.local()).ToLocalChecked();
829 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000830
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000831 CompileRun(native_accessor_test_source);
832 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833
834 {
835 // Make sure accessors ICs are in monomorphic state before starting
836 // profiling.
837 accessors.set_warming_up(true);
838 int32_t warm_up_iterations = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000839 v8::Local<v8::Value> args[] = {
840 v8::Integer::New(isolate, warm_up_iterations)};
841 function->Call(env.local(), env->Global(), arraysize(args), args)
842 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000843 accessors.set_warming_up(false);
844 }
845
846 int32_t repeat_count = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000847 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100849 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850
851 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100852 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
853 GetChild(env.local(), start_node, "get foo");
854 GetChild(env.local(), start_node, "set foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855
856 profile->Delete();
857}
858
859
860static const char* native_method_test_source = "function start(count) {\n"
861" for (var i = 0; i < count; i++) {\n"
862" instance.fooMethod();\n"
863" }\n"
864"}\n";
865
866
867TEST(NativeMethodUninitializedIC) {
868 LocalContext env;
869 v8::Isolate* isolate = env->GetIsolate();
870 v8::HandleScope scope(isolate);
871
872 TestApiCallbacks callbacks(100);
873 v8::Local<v8::External> data =
874 v8::External::New(isolate, &callbacks);
875
876 v8::Local<v8::FunctionTemplate> func_template =
877 v8::FunctionTemplate::New(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000878 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000879 v8::Local<v8::ObjectTemplate> proto_template =
880 func_template->PrototypeTemplate();
881 v8::Local<v8::Signature> signature =
882 v8::Signature::New(isolate, func_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000883 proto_template->Set(
884 v8_str("fooMethod"),
885 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
886 signature, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000887
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000888 v8::Local<v8::Function> func =
889 func_template->GetFunction(env.local()).ToLocalChecked();
890 v8::Local<v8::Object> instance =
891 func->NewInstance(env.local()).ToLocalChecked();
892 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000893
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000894 CompileRun(native_method_test_source);
895 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896
897 int32_t repeat_count = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000898 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100900 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901
902 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100903 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
904 GetChild(env.local(), start_node, "fooMethod");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000905
906 profile->Delete();
907}
908
909
910TEST(NativeMethodMonomorphicIC) {
911 LocalContext env;
912 v8::Isolate* isolate = env->GetIsolate();
913 v8::HandleScope scope(isolate);
914
915 TestApiCallbacks callbacks(1);
916 v8::Local<v8::External> data =
917 v8::External::New(isolate, &callbacks);
918
919 v8::Local<v8::FunctionTemplate> func_template =
920 v8::FunctionTemplate::New(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000921 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000922 v8::Local<v8::ObjectTemplate> proto_template =
923 func_template->PrototypeTemplate();
924 v8::Local<v8::Signature> signature =
925 v8::Signature::New(isolate, func_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 proto_template->Set(
927 v8_str("fooMethod"),
928 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
929 signature, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000931 v8::Local<v8::Function> func =
932 func_template->GetFunction(env.local()).ToLocalChecked();
933 v8::Local<v8::Object> instance =
934 func->NewInstance(env.local()).ToLocalChecked();
935 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000937 CompileRun(native_method_test_source);
938 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000939 {
940 // Make sure method ICs are in monomorphic state before starting
941 // profiling.
942 callbacks.set_warming_up(true);
943 int32_t warm_up_iterations = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000944 v8::Local<v8::Value> args[] = {
945 v8::Integer::New(isolate, warm_up_iterations)};
946 function->Call(env.local(), env->Global(), arraysize(args), args)
947 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948 callbacks.set_warming_up(false);
949 }
950
951 int32_t repeat_count = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000952 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100954 RunProfiler(env.local(), function, args, arraysize(args), 0, 200);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955
956 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000957 GetChild(env.local(), root, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100958 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
959 GetChild(env.local(), start_node, "fooMethod");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960
961 profile->Delete();
962}
963
964
965static const char* bound_function_test_source =
966 "function foo() {\n"
967 " startProfiling('my_profile');\n"
968 "}\n"
969 "function start() {\n"
970 " var callback = foo.bind(this);\n"
971 " callback();\n"
972 "}";
973
974
975TEST(BoundFunctionCall) {
976 v8::HandleScope scope(CcTest::isolate());
977 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
978 v8::Context::Scope context_scope(env);
979
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000980 CompileRun(bound_function_test_source);
981 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982
Ben Murdoch097c5b22016-05-18 11:27:45 +0100983 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000984
985 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000986
Ben Murdoch097c5b22016-05-18 11:27:45 +0100987 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
988 GetChild(env, start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989
990 profile->Delete();
991}
992
993
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400994// This tests checks distribution of the samples through the source lines.
995TEST(TickLines) {
996 CcTest::InitializeVM();
997 LocalContext env;
998 i::FLAG_turbo_source_positions = true;
999 i::Isolate* isolate = CcTest::i_isolate();
1000 i::Factory* factory = isolate->factory();
1001 i::HandleScope scope(isolate);
1002
1003 i::EmbeddedVector<char, 512> script;
1004
1005 const char* func_name = "func";
1006 i::SNPrintF(script,
1007 "function %s() {\n"
1008 " var n = 0;\n"
1009 " var m = 100*100;\n"
1010 " while (m > 1) {\n"
1011 " m--;\n"
1012 " n += m * m * m;\n"
1013 " }\n"
1014 "}\n"
1015 "%s();\n",
1016 func_name, func_name);
1017
1018 CompileRun(script.start());
1019
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001020 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
1021 v8::Utils::OpenHandle(*GetFunction(env.local(), func_name)));
1022 CHECK(func->shared());
1023 CHECK(func->shared()->code());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001024 i::Code* code = NULL;
1025 if (func->code()->is_optimized_code()) {
1026 code = func->code();
1027 } else {
1028 CHECK(func->shared()->code() == func->code() || !i::FLAG_crankshaft);
1029 code = func->shared()->code();
1030 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001031 CHECK(code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001032 i::Address code_address = code->instruction_start();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001033 CHECK(code_address);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001034
1035 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
1036 profiles->StartProfiling("", false);
1037 ProfileGenerator generator(profiles);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001038 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
1039 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001040 processor->Start();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001041 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001042
1043 // Enqueue code creation events.
1044 i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name);
1045 int line = 1;
1046 int column = 1;
1047 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, code, func->shared(), NULL,
1048 *str, line, column);
1049
1050 // Enqueue a tick event to enable code events processing.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001051 EnqueueTickSampleEvent(processor.get(), code_address);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001052
1053 processor->StopSynchronously();
1054
1055 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001056 CHECK(profile);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001057
1058 // Check the state of profile generator.
1059 CodeEntry* func_entry = generator.code_map()->FindEntry(code_address);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001060 CHECK(func_entry);
1061 CHECK_EQ(0, strcmp(func_name, func_entry->name()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001062 const i::JITLineInfoTable* line_info = func_entry->line_info();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001063 CHECK(line_info);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001064 CHECK(!line_info->empty());
1065
1066 // Check the hit source lines using V8 Public APIs.
1067 const i::ProfileTree* tree = profile->top_down();
1068 ProfileNode* root = tree->root();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069 CHECK(root);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001070 ProfileNode* func_node = root->FindChild(func_entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001071 CHECK(func_node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001072
1073 // Add 10 faked ticks to source line #5.
1074 int hit_line = 5;
1075 int hit_count = 10;
1076 for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line);
1077
1078 unsigned int line_count = func_node->GetHitLineCount();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001079 CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001080 ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count);
1081 CHECK(func_node->GetLineTicks(&entries[0], line_count));
1082 int value = 0;
1083 for (int i = 0; i < entries.length(); i++)
1084 if (entries[i].line == hit_line) {
1085 value = entries[i].hit_count;
1086 break;
1087 }
1088 CHECK_EQ(hit_count, value);
1089}
1090
Ben Murdoch097c5b22016-05-18 11:27:45 +01001091static const char* call_function_test_source =
1092 "%NeverOptimizeFunction(bar);\n"
1093 "%NeverOptimizeFunction(start);\n"
1094 "function bar(n) {\n"
1095 " var s = 0;\n"
1096 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1097 " return s;\n"
1098 "}\n"
1099 "function start(duration) {\n"
1100 " var start = Date.now();\n"
1101 " do {\n"
1102 " for (var i = 0; i < 100; ++i)\n"
1103 " bar.call(this, 1000);\n"
1104 " } while (Date.now() - start < duration);\n"
1105 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001106
1107// Test that if we sampled thread when it was inside FunctionCall buitin then
1108// its caller frame will be '(unresolved function)' as we have no reliable way
1109// to resolve it.
1110//
1111// [Top down]:
1112// 96 0 (root) [-1] #1
1113// 1 1 (garbage collector) [-1] #4
1114// 5 0 (unresolved function) [-1] #5
1115// 5 5 call [-1] #6
1116// 71 70 start [-1] #3
1117// 1 1 bar [-1] #7
1118// 19 19 (program) [-1] #2
1119TEST(FunctionCallSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001120 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121 LocalContext env;
1122 v8::HandleScope scope(env->GetIsolate());
1123
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001124 // Collect garbage that might have be generated while installing
1125 // extensions.
1126 CcTest::heap()->CollectAllGarbage();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001128 CompileRun(call_function_test_source);
1129 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130
1131 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001132 v8::Local<v8::Value> args[] = {
1133 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001135 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001136
1137 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001138 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
1139 GetChild(env.local(), start_node, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001140
Ben Murdoch097c5b22016-05-18 11:27:45 +01001141 const v8::CpuProfileNode* unresolved_node = FindChild(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001142 env.local(), root, i::ProfileGenerator::kUnresolvedFunctionName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001143 CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "call"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001144
1145 profile->Delete();
1146}
1147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001148static const char* function_apply_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001149 "%NeverOptimizeFunction(bar);\n"
1150 "%NeverOptimizeFunction(test);\n"
1151 "%NeverOptimizeFunction(start);\n"
1152 "function bar(n) {\n"
1153 " var s = 0;\n"
1154 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1155 " return s;\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001156 "}\n"
1157 "function test() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001158 " bar.apply(this, [1000]);\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001159 "}\n"
1160 "function start(duration) {\n"
1161 " var start = Date.now();\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001162 " do {\n"
1163 " for (var i = 0; i < 100; ++i) test();\n"
1164 " } while (Date.now() - start < duration);\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001165 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001166
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001167// [Top down]:
1168// 94 0 (root) [-1] #0 1
1169// 2 2 (garbage collector) [-1] #0 7
1170// 82 49 start [-1] #16 3
1171// 1 0 (unresolved function) [-1] #0 8
1172// 1 1 apply [-1] #0 9
1173// 32 21 test [-1] #16 4
1174// 2 2 bar [-1] #16 6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001175// 10 10 (program) [-1] #0 2
1176TEST(FunctionApplySample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001177 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001178 LocalContext env;
1179 v8::HandleScope scope(env->GetIsolate());
1180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001181 CompileRun(function_apply_test_source);
1182 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001183
1184 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001185 v8::Local<v8::Value> args[] = {
1186 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001187
1188 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001189 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001190
1191 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001192 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
1193 const v8::CpuProfileNode* test_node =
1194 GetChild(env.local(), start_node, "test");
1195 GetChild(env.local(), test_node, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001196
Ben Murdoch097c5b22016-05-18 11:27:45 +01001197 const v8::CpuProfileNode* unresolved_node = FindChild(
1198 env.local(), start_node, ProfileGenerator::kUnresolvedFunctionName);
1199 CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "apply"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200
1201 profile->Delete();
1202}
1203
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001204static const char* cpu_profiler_deep_stack_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001205 "function foo(n) {\n"
1206 " if (n)\n"
1207 " foo(n - 1);\n"
1208 " else\n"
1209 " collectSample();\n"
1210 "}\n"
1211 "function start() {\n"
1212 " startProfiling('my_profile');\n"
1213 " foo(250);\n"
1214 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001215
1216// Check a deep stack
1217//
1218// [Top down]:
1219// 0 (root) 0 #1
1220// 2 (program) 0 #2
1221// 0 start 21 #3 no reason
1222// 0 foo 21 #4 no reason
1223// 0 foo 21 #5 no reason
1224// ....
Ben Murdoch097c5b22016-05-18 11:27:45 +01001225// 0 foo 21 #254 no reason
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001226TEST(CpuProfileDeepStack) {
1227 v8::HandleScope scope(CcTest::isolate());
1228 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1229 v8::Context::Scope context_scope(env);
1230
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001231 CompileRun(cpu_profiler_deep_stack_test_source);
1232 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233
1234 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001235 v8::Local<v8::String> profile_name = v8_str("my_profile");
1236 function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001237 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001238 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239 // Dump collected profile to have a better diagnostic in case of failure.
1240 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1241
1242 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001243 const v8::CpuProfileNode* node = GetChild(env, root, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001244 for (int i = 0; i <= 250; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001245 node = GetChild(env, node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001247 CHECK(!FindChild(env, node, "foo"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001248
1249 profile->Delete();
1250}
1251
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252static const char* js_native_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001253 "%NeverOptimizeFunction(foo);\n"
1254 "%NeverOptimizeFunction(bar);\n"
1255 "%NeverOptimizeFunction(start);\n"
1256 "function foo(n) {\n"
1257 " var s = 0;\n"
1258 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1259 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001260 "}\n"
1261 "function bar() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001262 " foo(1000);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001263 "}\n"
1264 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001265 " CallJsFunction(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001266 "}";
1267
1268static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001269 v8::Local<v8::Function> function = info[0].As<v8::Function>();
1270 v8::Local<v8::Value> argv[] = {info[1]};
1271 function->Call(info.GetIsolate()->GetCurrentContext(), info.This(),
1272 arraysize(argv), argv)
1273 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274}
1275
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001276// [Top down]:
1277// 58 0 (root) #0 1
1278// 2 2 (program) #0 2
1279// 56 1 start #16 3
1280// 55 0 CallJsFunction #0 4
1281// 55 1 bar #16 5
1282// 54 54 foo #16 6
1283TEST(JsNativeJsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001284 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001285 v8::HandleScope scope(CcTest::isolate());
1286 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1287 v8::Context::Scope context_scope(env);
1288
1289 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1290 env->GetIsolate(), CallJsFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001291 v8::Local<v8::Function> func =
1292 func_template->GetFunction(env).ToLocalChecked();
1293 func->SetName(v8_str("CallJsFunction"));
1294 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001296 CompileRun(js_native_js_test_source);
1297 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001298
Ben Murdoch097c5b22016-05-18 11:27:45 +01001299 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001300
1301 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001302 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1303 const v8::CpuProfileNode* native_node =
1304 GetChild(env, start_node, "CallJsFunction");
1305 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1306 GetChild(env, bar_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001307
1308 profile->Delete();
1309}
1310
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001311static const char* js_native_js_runtime_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001312 "%NeverOptimizeFunction(foo);\n"
1313 "%NeverOptimizeFunction(bar);\n"
1314 "%NeverOptimizeFunction(start);\n"
1315 "function foo(n) {\n"
1316 " var s = 0;\n"
1317 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1318 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001319 "}\n"
1320 "var bound = foo.bind(this);\n"
1321 "function bar() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001322 " bound(1000);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 "}\n"
1324 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001325 " CallJsFunction(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001326 "}";
1327
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001328// [Top down]:
1329// 57 0 (root) #0 1
1330// 55 1 start #16 3
1331// 54 0 CallJsFunction #0 4
1332// 54 3 bar #16 5
1333// 51 51 foo #16 6
1334// 2 2 (program) #0 2
1335TEST(JsNativeJsRuntimeJsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001336 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337 v8::HandleScope scope(CcTest::isolate());
1338 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1339 v8::Context::Scope context_scope(env);
1340
1341 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1342 env->GetIsolate(), CallJsFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001343 v8::Local<v8::Function> func =
1344 func_template->GetFunction(env).ToLocalChecked();
1345 func->SetName(v8_str("CallJsFunction"));
1346 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001348 CompileRun(js_native_js_runtime_js_test_source);
1349 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001350 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351
1352 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001353 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1354 const v8::CpuProfileNode* native_node =
1355 GetChild(env, start_node, "CallJsFunction");
1356 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1357 GetChild(env, bar_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358
1359 profile->Delete();
1360}
1361
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001362static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) {
1363 v8::base::OS::Print("In CallJsFunction2\n");
1364 CallJsFunction(info);
1365}
1366
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001367static const char* js_native1_js_native2_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001368 "%NeverOptimizeFunction(foo);\n"
1369 "%NeverOptimizeFunction(bar);\n"
1370 "%NeverOptimizeFunction(start);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001371 "function foo() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001372 " var s = 0;\n"
1373 " for (var i = 0; i < 1000; i++) s += i * i * i;\n"
1374 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001375 "}\n"
1376 "function bar() {\n"
1377 " CallJsFunction2(foo);\n"
1378 "}\n"
1379 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001380 " CallJsFunction1(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001381 "}";
1382
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001383// [Top down]:
1384// 57 0 (root) #0 1
1385// 55 1 start #16 3
1386// 54 0 CallJsFunction1 #0 4
1387// 54 0 bar #16 5
1388// 54 0 CallJsFunction2 #0 6
1389// 54 54 foo #16 7
1390// 2 2 (program) #0 2
1391TEST(JsNative1JsNative2JsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001392 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001393 v8::HandleScope scope(CcTest::isolate());
1394 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1395 v8::Context::Scope context_scope(env);
1396
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001397 v8::Local<v8::Function> func1 =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001398 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction)
1399 ->GetFunction(env)
1400 .ToLocalChecked();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001401 func1->SetName(v8_str("CallJsFunction1"));
1402 env->Global()->Set(env, v8_str("CallJsFunction1"), func1).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001404 v8::Local<v8::Function> func2 =
1405 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction2)
1406 ->GetFunction(env)
1407 .ToLocalChecked();
1408 func2->SetName(v8_str("CallJsFunction2"));
1409 env->Global()->Set(env, v8_str("CallJsFunction2"), func2).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001410
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001411 CompileRun(js_native1_js_native2_js_test_source);
1412 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413
Ben Murdoch097c5b22016-05-18 11:27:45 +01001414 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415
1416 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001417 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1418 const v8::CpuProfileNode* native_node1 =
1419 GetChild(env, start_node, "CallJsFunction1");
1420 const v8::CpuProfileNode* bar_node = GetChild(env, native_node1, "bar");
1421 const v8::CpuProfileNode* native_node2 =
1422 GetChild(env, bar_node, "CallJsFunction2");
1423 GetChild(env, native_node2, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001424
1425 profile->Delete();
1426}
1427
Ben Murdoch097c5b22016-05-18 11:27:45 +01001428static const char* js_force_collect_sample_source =
1429 "function start() {\n"
1430 " CallCollectSample();\n"
1431 "}";
1432
1433TEST(CollectSampleAPI) {
1434 v8::HandleScope scope(CcTest::isolate());
1435 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1436 v8::Context::Scope context_scope(env);
1437
1438 v8::Local<v8::FunctionTemplate> func_template =
1439 v8::FunctionTemplate::New(env->GetIsolate(), CallCollectSample);
1440 v8::Local<v8::Function> func =
1441 func_template->GetFunction(env).ToLocalChecked();
1442 func->SetName(v8_str("CallCollectSample"));
1443 env->Global()->Set(env, v8_str("CallCollectSample"), func).FromJust();
1444
1445 CompileRun(js_force_collect_sample_source);
1446 v8::Local<v8::Function> function = GetFunction(env, "start");
1447 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1448
1449 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1450 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1451 CHECK_LE(1, start_node->GetChildrenCount());
1452 GetChild(env, start_node, "CallCollectSample");
1453
1454 profile->Delete();
1455}
1456
1457static const char* js_native_js_runtime_multiple_test_source =
1458 "%NeverOptimizeFunction(foo);\n"
1459 "%NeverOptimizeFunction(bar);\n"
1460 "%NeverOptimizeFunction(start);\n"
1461 "function foo() {\n"
1462 " return Math.sin(Math.random());\n"
1463 "}\n"
1464 "var bound = foo.bind(this);\n"
1465 "function bar() {\n"
1466 " return bound();\n"
1467 "}\n"
1468 "function start() {\n"
1469 " startProfiling('my_profile');\n"
1470 " var startTime = Date.now();\n"
1471 " do {\n"
1472 " CallJsFunction(bar);\n"
1473 " } while (Date.now() - startTime < 200);\n"
1474 "}";
1475
1476// The test check multiple entrances/exits between JS and native code.
1477//
1478// [Top down]:
1479// (root) #0 1
1480// start #16 3
1481// CallJsFunction #0 4
1482// bar #16 5
1483// foo #16 6
1484// (program) #0 2
1485TEST(JsNativeJsRuntimeJsSampleMultiple) {
1486 i::FLAG_allow_natives_syntax = true;
1487 v8::HandleScope scope(CcTest::isolate());
1488 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1489 v8::Context::Scope context_scope(env);
1490
1491 v8::Local<v8::FunctionTemplate> func_template =
1492 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction);
1493 v8::Local<v8::Function> func =
1494 func_template->GetFunction(env).ToLocalChecked();
1495 func->SetName(v8_str("CallJsFunction"));
1496 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
1497
1498 CompileRun(js_native_js_runtime_multiple_test_source);
1499 v8::Local<v8::Function> function = GetFunction(env, "start");
1500
1501 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 500, 500);
1502
1503 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1504 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1505 const v8::CpuProfileNode* native_node =
1506 GetChild(env, start_node, "CallJsFunction");
1507 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1508 GetChild(env, bar_node, "foo");
1509
1510 profile->Delete();
1511}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001512
1513// [Top down]:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001514// 0 (root) #0 1
1515// 2 (program) #0 2
1516// 3 (idle) #0 3
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001517TEST(IdleTime) {
1518 LocalContext env;
1519 v8::HandleScope scope(env->GetIsolate());
1520 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1521
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001522 v8::Local<v8::String> profile_name = v8_str("my_profile");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 cpu_profiler->StartProfiling(profile_name);
1524
1525 i::Isolate* isolate = CcTest::i_isolate();
1526 i::ProfilerEventsProcessor* processor = isolate->cpu_profiler()->processor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001527
Ben Murdoch097c5b22016-05-18 11:27:45 +01001528 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001529 cpu_profiler->SetIdle(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001530 for (int i = 0; i < 3; i++) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001531 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001532 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533 cpu_profiler->SetIdle(false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001534 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001535
1536 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001537 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001538 // Dump collected profile to have a better diagnostic in case of failure.
1539 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1540
1541 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001542 const v8::CpuProfileNode* program_node =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543 GetChild(env.local(), root, ProfileGenerator::kProgramEntryName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001544 CHECK_EQ(0, program_node->GetChildrenCount());
1545 CHECK_GE(program_node->GetHitCount(), 2u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001546
Ben Murdoch097c5b22016-05-18 11:27:45 +01001547 const v8::CpuProfileNode* idle_node =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001548 GetChild(env.local(), root, ProfileGenerator::kIdleEntryName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001549 CHECK_EQ(0, idle_node->GetChildrenCount());
1550 CHECK_GE(idle_node->GetHitCount(), 3u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001551
1552 profile->Delete();
1553}
1554
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001555static void CheckFunctionDetails(v8::Isolate* isolate,
1556 const v8::CpuProfileNode* node,
1557 const char* name, const char* script_name,
1558 int script_id, int line, int column) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001559 v8::Local<v8::Context> context = isolate->GetCurrentContext();
1560 CHECK(v8_str(name)->Equals(context, node->GetFunctionName()).FromJust());
1561 CHECK(v8_str(script_name)
1562 ->Equals(context, node->GetScriptResourceName())
1563 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001564 CHECK_EQ(script_id, node->GetScriptId());
1565 CHECK_EQ(line, node->GetLineNumber());
1566 CHECK_EQ(column, node->GetColumnNumber());
1567}
1568
1569
1570TEST(FunctionDetails) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001571 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001572 v8::HandleScope scope(CcTest::isolate());
1573 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1574 v8::Context::Scope context_scope(env);
1575
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001576 v8::Local<v8::Script> script_a = CompileWithOrigin(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001577 "%NeverOptimizeFunction(foo);\n"
1578 "%NeverOptimizeFunction(bar);\n"
1579 " function foo\n() { bar(); }\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001580 " function bar() { startProfiling(); }\n",
1581 "script_a");
1582 script_a->Run(env).ToLocalChecked();
1583 v8::Local<v8::Script> script_b = CompileWithOrigin(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001584 "%NeverOptimizeFunction(baz);"
1585 "\n\n function baz() { foo(); }\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001586 "\n\nbaz();\n"
1587 "stopProfiling();\n",
1588 "script_b");
1589 script_b->Run(env).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001590 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
1591 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
1592 reinterpret_cast<ProfileNode*>(
1593 const_cast<v8::CpuProfileNode*>(current))->Print(0);
1594 // The tree should look like this:
1595 // 0 (root) 0 #1
1596 // 0 "" 19 #2 no reason script_b:1
1597 // 0 baz 19 #3 TryCatchStatement script_b:3
1598 // 0 foo 18 #4 TryCatchStatement script_a:2
1599 // 1 bar 18 #5 no reason script_a:3
1600 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001601 const v8::CpuProfileNode* script = GetChild(env, root, "");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602 CheckFunctionDetails(env->GetIsolate(), script, "", "script_b",
1603 script_b->GetUnboundScript()->GetId(), 1, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001604 const v8::CpuProfileNode* baz = GetChild(env, script, "baz");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001605 CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b",
1606 script_b->GetUnboundScript()->GetId(), 3, 16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001607 const v8::CpuProfileNode* foo = GetChild(env, baz, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001608 CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a",
Ben Murdoch097c5b22016-05-18 11:27:45 +01001609 script_a->GetUnboundScript()->GetId(), 4, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001610 const v8::CpuProfileNode* bar = GetChild(env, foo, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611 CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a",
Ben Murdoch097c5b22016-05-18 11:27:45 +01001612 script_a->GetUnboundScript()->GetId(), 5, 14);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613}
1614
1615
1616TEST(DontStopOnFinishedProfileDelete) {
1617 v8::HandleScope scope(CcTest::isolate());
1618 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1619 v8::Context::Scope context_scope(env);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001620
1621 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
1622 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1623
1624 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001625 v8::Local<v8::String> outer = v8_str("outer");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626 profiler->StartProfiling(outer);
1627 CHECK_EQ(0, iprofiler->GetProfilesCount());
1628
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001629 v8::Local<v8::String> inner = v8_str("inner");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630 profiler->StartProfiling(inner);
1631 CHECK_EQ(0, iprofiler->GetProfilesCount());
1632
1633 v8::CpuProfile* inner_profile = profiler->StopProfiling(inner);
1634 CHECK(inner_profile);
1635 CHECK_EQ(1, iprofiler->GetProfilesCount());
1636 inner_profile->Delete();
1637 inner_profile = NULL;
1638 CHECK_EQ(0, iprofiler->GetProfilesCount());
1639
1640 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer);
1641 CHECK(outer_profile);
1642 CHECK_EQ(1, iprofiler->GetProfilesCount());
1643 outer_profile->Delete();
1644 outer_profile = NULL;
1645 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +01001646}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001647
1648
1649const char* GetBranchDeoptReason(v8::Local<v8::Context> context,
1650 i::CpuProfile* iprofile, const char* branch[],
1651 int length) {
1652 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1653 const ProfileNode* iopt_function = NULL;
1654 iopt_function = GetSimpleBranch(context, profile, branch, length);
1655 CHECK_EQ(1U, iopt_function->deopt_infos().size());
1656 return iopt_function->deopt_infos()[0].deopt_reason;
1657}
1658
1659
1660// deopt at top function
1661TEST(CollectDeoptEvents) {
1662 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1663 i::FLAG_allow_natives_syntax = true;
1664 v8::HandleScope scope(CcTest::isolate());
1665 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1666 v8::Context::Scope context_scope(env);
1667 v8::Isolate* isolate = env->GetIsolate();
1668 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1669 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1670
1671 const char opt_source[] =
1672 "function opt_function%d(value, depth) {\n"
1673 " if (depth) return opt_function%d(value, depth - 1);\n"
1674 "\n"
1675 " return 10 / value;\n"
1676 "}\n"
1677 "\n";
1678
1679 for (int i = 0; i < 3; ++i) {
1680 i::EmbeddedVector<char, sizeof(opt_source) + 100> buffer;
1681 i::SNPrintF(buffer, opt_source, i, i);
1682 v8::Script::Compile(env, v8_str(buffer.start()))
1683 .ToLocalChecked()
1684 ->Run(env)
1685 .ToLocalChecked();
1686 }
1687
1688 const char* source =
1689 "startProfiling();\n"
1690 "\n"
1691 "opt_function0(1, 1);\n"
1692 "\n"
1693 "%OptimizeFunctionOnNextCall(opt_function0)\n"
1694 "\n"
1695 "opt_function0(1, 1);\n"
1696 "\n"
1697 "opt_function0(undefined, 1);\n"
1698 "\n"
1699 "opt_function1(1, 1);\n"
1700 "\n"
1701 "%OptimizeFunctionOnNextCall(opt_function1)\n"
1702 "\n"
1703 "opt_function1(1, 1);\n"
1704 "\n"
1705 "opt_function1(NaN, 1);\n"
1706 "\n"
1707 "opt_function2(1, 1);\n"
1708 "\n"
1709 "%OptimizeFunctionOnNextCall(opt_function2)\n"
1710 "\n"
1711 "opt_function2(1, 1);\n"
1712 "\n"
1713 "opt_function2(0, 1);\n"
1714 "\n"
1715 "stopProfiling();\n"
1716 "\n";
1717
1718 v8::Script::Compile(env, v8_str(source))
1719 .ToLocalChecked()
1720 ->Run(env)
1721 .ToLocalChecked();
1722 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1723 iprofile->Print();
1724 /* The expected profile
1725 [Top down]:
1726 0 (root) 0 #1
1727 23 32 #2
1728 1 opt_function2 31 #7
1729 1 opt_function2 31 #8
1730 ;;; deopted at script_id: 31 position: 106 with reason
1731 'division by zero'.
1732 2 opt_function0 29 #3
1733 4 opt_function0 29 #4
1734 ;;; deopted at script_id: 29 position: 108 with reason 'not a
1735 heap number'.
1736 0 opt_function1 30 #5
1737 1 opt_function1 30 #6
1738 ;;; deopted at script_id: 30 position: 108 with reason 'lost
1739 precision or NaN'.
1740 */
1741
1742 {
1743 const char* branch[] = {"", "opt_function0", "opt_function0"};
1744 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber),
1745 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
1746 }
1747 {
1748 const char* branch[] = {"", "opt_function1", "opt_function1"};
1749 const char* deopt_reason =
1750 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch));
1751 if (deopt_reason != reason(i::Deoptimizer::kNaN) &&
1752 deopt_reason != reason(i::Deoptimizer::kLostPrecisionOrNaN)) {
1753 FATAL(deopt_reason);
1754 }
1755 }
1756 {
1757 const char* branch[] = {"", "opt_function2", "opt_function2"};
1758 CHECK_EQ(reason(i::Deoptimizer::kDivisionByZero),
1759 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
1760 }
1761 iprofiler->DeleteProfile(iprofile);
1762}
1763
1764
1765TEST(SourceLocation) {
1766 i::FLAG_always_opt = true;
1767 i::FLAG_hydrogen_track_positions = true;
1768 LocalContext env;
1769 v8::HandleScope scope(CcTest::isolate());
1770
1771 const char* source =
1772 "function CompareStatementWithThis() {\n"
1773 " if (this === 1) {}\n"
1774 "}\n"
1775 "CompareStatementWithThis();\n";
1776
1777 v8::Script::Compile(env.local(), v8_str(source))
1778 .ToLocalChecked()
1779 ->Run(env.local())
1780 .ToLocalChecked();
1781}
1782
1783
1784static const char* inlined_source =
1785 "function opt_function(left, right) { var k = left / 10; var r = 10 / "
1786 "right; return k + r; }\n";
1787// 0.........1.........2.........3.........4....*....5.........6......*..7
1788
1789
1790// deopt at the first level inlined function
1791TEST(DeoptAtFirstLevelInlinedSource) {
1792 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1793 i::FLAG_allow_natives_syntax = true;
1794 v8::HandleScope scope(CcTest::isolate());
1795 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1796 v8::Context::Scope context_scope(env);
1797 v8::Isolate* isolate = env->GetIsolate();
1798 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1799 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1800
1801 // 0.........1.........2.........3.........4.........5.........6.........7
1802 const char* source =
1803 "function test(left, right) { return opt_function(left, right); }\n"
1804 "\n"
1805 "startProfiling();\n"
1806 "\n"
1807 "test(10, 10);\n"
1808 "\n"
1809 "%OptimizeFunctionOnNextCall(test)\n"
1810 "\n"
1811 "test(10, 10);\n"
1812 "\n"
1813 "test(undefined, 10);\n"
1814 "\n"
1815 "stopProfiling();\n"
1816 "\n";
1817
1818 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1819 inlined_script->Run(env).ToLocalChecked();
1820 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1821
1822 v8::Local<v8::Script> script = v8_compile(source);
1823 script->Run(env).ToLocalChecked();
1824 int script_id = script->GetUnboundScript()->GetId();
1825
1826 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1827 iprofile->Print();
1828 /* The expected profile output
1829 [Top down]:
1830 0 (root) 0 #1
1831 10 30 #2
1832 1 test 30 #3
1833 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1834 heap number'.
1835 ;;; Inline point: script_id 30 position: 36.
1836 4 opt_function 29 #4
1837 */
1838 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1839
1840 const char* branch[] = {"", "test"};
1841 const ProfileNode* itest_node =
1842 GetSimpleBranch(env, profile, branch, arraysize(branch));
1843 const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
1844 itest_node->deopt_infos();
1845 CHECK_EQ(1U, deopt_infos.size());
1846
1847 const v8::CpuProfileDeoptInfo& info = deopt_infos[0];
1848 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1849 CHECK_EQ(2U, info.stack.size());
1850 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1851 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1852 CHECK_EQ(script_id, info.stack[1].script_id);
1853 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1854
1855 iprofiler->DeleteProfile(iprofile);
1856}
1857
1858
1859// deopt at the second level inlined function
1860TEST(DeoptAtSecondLevelInlinedSource) {
1861 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1862 i::FLAG_allow_natives_syntax = true;
1863 v8::HandleScope scope(CcTest::isolate());
1864 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1865 v8::Context::Scope context_scope(env);
1866 v8::Isolate* isolate = env->GetIsolate();
1867 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1868 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1869
1870 // 0.........1.........2.........3.........4.........5.........6.........7
1871 const char* source =
1872 "function test2(left, right) { return opt_function(left, right); }\n"
1873 "function test1(left, right) { return test2(left, right); }\n"
1874 "\n"
1875 "startProfiling();\n"
1876 "\n"
1877 "test1(10, 10);\n"
1878 "\n"
1879 "%OptimizeFunctionOnNextCall(test1)\n"
1880 "\n"
1881 "test1(10, 10);\n"
1882 "\n"
1883 "test1(undefined, 10);\n"
1884 "\n"
1885 "stopProfiling();\n"
1886 "\n";
1887
1888 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1889 inlined_script->Run(env).ToLocalChecked();
1890 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1891
1892 v8::Local<v8::Script> script = v8_compile(source);
1893 script->Run(env).ToLocalChecked();
1894 int script_id = script->GetUnboundScript()->GetId();
1895
1896 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1897 iprofile->Print();
1898 /* The expected profile output
1899 [Top down]:
1900 0 (root) 0 #1
1901 11 30 #2
1902 1 test1 30 #3
1903 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1904 heap number'.
1905 ;;; Inline point: script_id 30 position: 37.
1906 ;;; Inline point: script_id 30 position: 103.
1907 1 test2 30 #4
1908 3 opt_function 29 #5
1909 */
1910
1911 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1912
1913 const char* branch[] = {"", "test1"};
1914 const ProfileNode* itest_node =
1915 GetSimpleBranch(env, profile, branch, arraysize(branch));
1916 const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
1917 itest_node->deopt_infos();
1918 CHECK_EQ(1U, deopt_infos.size());
1919
1920 const v8::CpuProfileDeoptInfo info = deopt_infos[0];
1921 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1922 CHECK_EQ(3U, info.stack.size());
1923 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1924 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1925 CHECK_EQ(script_id, info.stack[1].script_id);
1926 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1927 CHECK_EQ(offset(source, "test2(left, right);"), info.stack[2].position);
1928
1929 iprofiler->DeleteProfile(iprofile);
1930}
1931
1932
1933// deopt in untracked function
1934TEST(DeoptUntrackedFunction) {
1935 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1936 i::FLAG_allow_natives_syntax = true;
1937 v8::HandleScope scope(CcTest::isolate());
1938 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1939 v8::Context::Scope context_scope(env);
1940 v8::Isolate* isolate = env->GetIsolate();
1941 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1942 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1943
1944 // 0.........1.........2.........3.........4.........5.........6.........7
1945 const char* source =
1946 "function test(left, right) { return opt_function(left, right); }\n"
1947 "\n"
1948 "test(10, 10);\n"
1949 "\n"
1950 "%OptimizeFunctionOnNextCall(test)\n"
1951 "\n"
1952 "test(10, 10);\n"
1953 "\n"
1954 "startProfiling();\n" // profiler started after compilation.
1955 "\n"
1956 "test(undefined, 10);\n"
1957 "\n"
1958 "stopProfiling();\n"
1959 "\n";
1960
1961 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1962 inlined_script->Run(env).ToLocalChecked();
1963
1964 v8::Local<v8::Script> script = v8_compile(source);
1965 script->Run(env).ToLocalChecked();
1966
1967 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1968 iprofile->Print();
1969 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1970
1971 const char* branch[] = {"", "test"};
1972 const ProfileNode* itest_node =
1973 GetSimpleBranch(env, profile, branch, arraysize(branch));
1974 CHECK_EQ(0U, itest_node->deopt_infos().size());
1975
1976 iprofiler->DeleteProfile(iprofile);
1977}