blob: 24c84c3df82c6340ab9fceadddacf7cc6b1fd7bb [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 Murdochda12d292016-06-02 14:46:10 +0100122i::AbstractCode* CreateCode(LocalContext* env) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 static int counter = 0;
124 i::EmbeddedVector<char, 256> script;
125 i::EmbeddedVector<char, 32> name;
126
127 i::SNPrintF(name, "function_%d", ++counter);
128 const char* name_start = name.start();
129 i::SNPrintF(script,
130 "function %s() {\n"
131 "var counter = 0;\n"
132 "for (var i = 0; i < %d; ++i) counter += i;\n"
133 "return '%s_' + counter;\n"
134 "}\n"
135 "%s();\n", name_start, counter, name_start, name_start);
136 CompileRun(script.start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137
138 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(
139 v8::Utils::OpenHandle(*GetFunction(env->local(), name_start)));
Ben Murdochda12d292016-06-02 14:46:10 +0100140 return fun->abstract_code();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141}
142
Steve Block6ded16b2010-05-10 14:33:55 +0100143TEST(CodeEvents) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 CcTest::InitializeVM();
145 LocalContext env;
146 i::Isolate* isolate = CcTest::i_isolate();
147 i::Factory* factory = isolate->factory();
Steve Block6ded16b2010-05-10 14:33:55 +0100148 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149
150 i::HandleScope scope(isolate);
151
Ben Murdochda12d292016-06-02 14:46:10 +0100152 i::AbstractCode* aaa_code = CreateCode(&env);
153 i::AbstractCode* comment_code = CreateCode(&env);
154 i::AbstractCode* args5_code = CreateCode(&env);
155 i::AbstractCode* comment2_code = CreateCode(&env);
156 i::AbstractCode* moved_code = CreateCode(&env);
157 i::AbstractCode* args3_code = CreateCode(&env);
158 i::AbstractCode* args4_code = CreateCode(&env);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159
160 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
161 profiles->StartProfiling("", false);
162 ProfileGenerator generator(profiles);
163 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
164 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
165 processor->Start();
166 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
Steve Block6ded16b2010-05-10 14:33:55 +0100167
168 // Enqueue code creation events.
Steve Block6ded16b2010-05-10 14:33:55 +0100169 const char* aaa_str = "aaa";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str);
171 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code, *aaa_name);
172 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code, "comment");
173 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5);
174 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code, "comment2");
Ben Murdochda12d292016-06-02 14:46:10 +0100175 profiler.CodeMoveEvent(comment2_code, moved_code->address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3);
177 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4);
Steve Block6ded16b2010-05-10 14:33:55 +0100178
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 // Enqueue a tick event to enable code events processing.
180 EnqueueTickSampleEvent(processor.get(), aaa_code->address());
181
182 processor->StopSynchronously();
Steve Block6ded16b2010-05-10 14:33:55 +0100183
184 // Check the state of profile generator.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 CodeEntry* aaa = generator.code_map()->FindEntry(aaa_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 CHECK(aaa);
187 CHECK_EQ(0, strcmp(aaa_str, aaa->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188
189 CodeEntry* comment = generator.code_map()->FindEntry(comment_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000190 CHECK(comment);
191 CHECK_EQ(0, strcmp("comment", comment->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192
193 CodeEntry* args5 = generator.code_map()->FindEntry(args5_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194 CHECK(args5);
195 CHECK_EQ(0, strcmp("5", args5->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197 CHECK(!generator.code_map()->FindEntry(comment2_code->address()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198
199 CodeEntry* comment2 = generator.code_map()->FindEntry(moved_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200 CHECK(comment2);
201 CHECK_EQ(0, strcmp("comment2", comment2->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100202}
203
Steve Block6ded16b2010-05-10 14:33:55 +0100204template<typename T>
205static int CompareProfileNodes(const T* p1, const T* p2) {
206 return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
207}
208
209TEST(TickEvents) {
210 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 LocalContext env;
212 i::Isolate* isolate = CcTest::i_isolate();
213 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100214
Ben Murdochda12d292016-06-02 14:46:10 +0100215 i::AbstractCode* frame1_code = CreateCode(&env);
216 i::AbstractCode* frame2_code = CreateCode(&env);
217 i::AbstractCode* frame3_code = CreateCode(&env);
Steve Block6ded16b2010-05-10 14:33:55 +0100218
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
220 profiles->StartProfiling("", false);
221 ProfileGenerator generator(profiles);
222 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
223 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
224 processor->Start();
225 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
226
227 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb");
228 profiler.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5);
229 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd");
230
231 EnqueueTickSampleEvent(processor.get(), frame1_code->instruction_start());
232 EnqueueTickSampleEvent(
233 processor.get(),
234 frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2,
235 frame1_code->instruction_start() + frame2_code->ExecutableSize() / 2);
236 EnqueueTickSampleEvent(
237 processor.get(),
238 frame3_code->instruction_end() - 1,
239 frame2_code->instruction_end() - 1,
240 frame1_code->instruction_end() - 1);
241
242 processor->StopSynchronously();
243 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 CHECK(profile);
Steve Block6ded16b2010-05-10 14:33:55 +0100245
246 // Check call trees.
247 const i::List<ProfileNode*>* top_down_root_children =
248 profile->top_down()->root()->children();
249 CHECK_EQ(1, top_down_root_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100251 const i::List<ProfileNode*>* top_down_bbb_children =
252 top_down_root_children->last()->children();
253 CHECK_EQ(1, top_down_bbb_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000254 CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100255 const i::List<ProfileNode*>* top_down_stub_children =
256 top_down_bbb_children->last()->children();
257 CHECK_EQ(1, top_down_stub_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258 CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100259 const i::List<ProfileNode*>* top_down_ddd_children =
260 top_down_stub_children->last()->children();
261 CHECK_EQ(0, top_down_ddd_children->length());
Steve Block6ded16b2010-05-10 14:33:55 +0100262}
263
Iain Merrick75681382010-08-19 15:07:18 +0100264// http://crbug/51594
265// This test must not crash.
266TEST(CrashIfStoppingLastNonExistentProfile) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 CcTest::InitializeVM();
Iain Merrick75681382010-08-19 15:07:18 +0100268 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
270 profiler->StartProfiling("1");
271 profiler->StopProfiling("2");
272 profiler->StartProfiling("1");
273 profiler->StopProfiling("");
Iain Merrick75681382010-08-19 15:07:18 +0100274}
275
Steve Block053d10c2011-06-13 19:13:29 +0100276// http://code.google.com/p/v8/issues/detail?id=1398
277// Long stacks (exceeding max frames limit) must not be erased.
278TEST(Issue1398) {
279 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 LocalContext env;
281 i::Isolate* isolate = CcTest::i_isolate();
282 i::HandleScope scope(isolate);
Steve Block053d10c2011-06-13 19:13:29 +0100283
Ben Murdochda12d292016-06-02 14:46:10 +0100284 i::AbstractCode* code = CreateCode(&env);
Steve Block053d10c2011-06-13 19:13:29 +0100285
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
287 profiles->StartProfiling("", false);
288 ProfileGenerator generator(profiles);
289 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
290 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
291 processor->Start();
292 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
293
294 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb");
295
296 i::TickSample* sample = processor->StartTickSample();
297 sample->pc = code->address();
Steve Block053d10c2011-06-13 19:13:29 +0100298 sample->tos = 0;
299 sample->frames_count = i::TickSample::kMaxFramesCount;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 for (unsigned i = 0; i < sample->frames_count; ++i) {
301 sample->stack[i] = code->address();
Steve Block053d10c2011-06-13 19:13:29 +0100302 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000303 processor->FinishTickSample();
Steve Block053d10c2011-06-13 19:13:29 +0100304
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305 processor->StopSynchronously();
306 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000307 CHECK(profile);
Steve Block053d10c2011-06-13 19:13:29 +0100308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 unsigned actual_depth = 0;
Steve Block053d10c2011-06-13 19:13:29 +0100310 const ProfileNode* node = profile->top_down()->root();
311 while (node->children()->length() > 0) {
312 node = node->children()->last();
313 ++actual_depth;
314 }
315
316 CHECK_EQ(1 + i::TickSample::kMaxFramesCount, actual_depth); // +1 for PC.
317}
318
Steve Block44f0eee2011-05-26 01:26:41 +0100319TEST(DeleteAllCpuProfiles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 CcTest::InitializeVM();
Steve Block44f0eee2011-05-26 01:26:41 +0100321 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
323 CHECK_EQ(0, profiler->GetProfilesCount());
324 profiler->DeleteAllProfiles();
325 CHECK_EQ(0, profiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100326
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327 profiler->StartProfiling("1");
328 profiler->StopProfiling("1");
329 CHECK_EQ(1, profiler->GetProfilesCount());
330 profiler->DeleteAllProfiles();
331 CHECK_EQ(0, profiler->GetProfilesCount());
332 profiler->StartProfiling("1");
333 profiler->StartProfiling("2");
334 profiler->StopProfiling("2");
335 profiler->StopProfiling("1");
336 CHECK_EQ(2, profiler->GetProfilesCount());
337 profiler->DeleteAllProfiles();
338 CHECK_EQ(0, profiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100339
340 // Test profiling cancellation by the 'delete' command.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 profiler->StartProfiling("1");
342 profiler->StartProfiling("2");
343 CHECK_EQ(0, profiler->GetProfilesCount());
344 profiler->DeleteAllProfiles();
345 CHECK_EQ(0, profiler->GetProfilesCount());
346}
Steve Block44f0eee2011-05-26 01:26:41 +0100347
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348
349static bool FindCpuProfile(v8::CpuProfiler* v8profiler,
350 const v8::CpuProfile* v8profile) {
351 i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler);
352 const i::CpuProfile* profile =
353 reinterpret_cast<const i::CpuProfile*>(v8profile);
354 int length = profiler->GetProfilesCount();
355 for (int i = 0; i < length; i++) {
356 if (profile == profiler->GetProfile(i))
357 return true;
358 }
359 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100360}
361
362
363TEST(DeleteCpuProfile) {
Steve Block44f0eee2011-05-26 01:26:41 +0100364 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 v8::HandleScope scope(env->GetIsolate());
366 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
367 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler);
Steve Block44f0eee2011-05-26 01:26:41 +0100368
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000370 v8::Local<v8::String> name1 = v8_str("1");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 cpu_profiler->StartProfiling(name1);
372 v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 CHECK(p1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 CHECK_EQ(1, iprofiler->GetProfilesCount());
375 CHECK(FindCpuProfile(cpu_profiler, p1));
376 p1->Delete();
377 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100378
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 v8::Local<v8::String> name2 = v8_str("2");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 cpu_profiler->StartProfiling(name2);
381 v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000382 CHECK(p2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 CHECK_EQ(1, iprofiler->GetProfilesCount());
384 CHECK(FindCpuProfile(cpu_profiler, p2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 v8::Local<v8::String> name3 = v8_str("3");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 cpu_profiler->StartProfiling(name3);
387 v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 CHECK(p3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 CHECK_EQ(2, iprofiler->GetProfilesCount());
390 CHECK_NE(p2, p3);
391 CHECK(FindCpuProfile(cpu_profiler, p3));
392 CHECK(FindCpuProfile(cpu_profiler, p2));
393 p2->Delete();
394 CHECK_EQ(1, iprofiler->GetProfilesCount());
395 CHECK(!FindCpuProfile(cpu_profiler, p2));
396 CHECK(FindCpuProfile(cpu_profiler, p3));
397 p3->Delete();
398 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100399}
400
401
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402TEST(ProfileStartEndTime) {
Steve Block44f0eee2011-05-26 01:26:41 +0100403 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 v8::HandleScope scope(env->GetIsolate());
405 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 v8::Local<v8::String> profile_name = v8_str("test");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 cpu_profiler->StartProfiling(profile_name);
409 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
410 CHECK(profile->GetStartTime() <= profile->GetEndTime());
411}
Steve Block44f0eee2011-05-26 01:26:41 +0100412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413static v8::CpuProfile* RunProfiler(v8::Local<v8::Context> env,
414 v8::Local<v8::Function> function,
415 v8::Local<v8::Value> argv[], int argc,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100416 unsigned min_js_samples = 0,
417 unsigned min_external_samples = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418 bool collect_samples = false) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000419 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420 v8::Local<v8::String> profile_name = v8_str("my_profile");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421
Ben Murdoch097c5b22016-05-18 11:27:45 +0100422 cpu_profiler->SetSamplingInterval(100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 cpu_profiler->StartProfiling(profile_name, collect_samples);
424
425 i::Sampler* sampler =
426 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler();
427 sampler->StartCountingSamples();
428 do {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000429 function->Call(env, env->Global(), argc, argv).ToLocalChecked();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100430 } while (sampler->js_sample_count() < min_js_samples ||
431 sampler->external_sample_count() < min_external_samples);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432
433 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
434
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000436 // Dump collected profile to have a better diagnostic in case of failure.
437 reinterpret_cast<i::CpuProfile*>(profile)->Print();
438
439 return profile;
440}
441
442
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000443static const v8::CpuProfileNode* FindChild(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000444 const v8::CpuProfileNode* node,
445 const char* name) {
446 int count = node->GetChildrenCount();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100447 v8::Local<v8::String> name_handle = v8_str(name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448 for (int i = 0; i < count; i++) {
449 const v8::CpuProfileNode* child = node->GetChild(i);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100450 if (name_handle->Equals(context, child->GetFunctionName()).FromJust()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000451 return child;
452 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000453 }
454 return NULL;
455}
456
457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458static const v8::CpuProfileNode* GetChild(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 const v8::CpuProfileNode* node,
460 const char* name) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000461 const v8::CpuProfileNode* result = FindChild(context, node, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 if (!result) {
463 char buffer[100];
464 i::SNPrintF(Vector<char>(buffer, arraysize(buffer)),
465 "Failed to GetChild: %s", name);
466 FATAL(buffer);
467 }
468 return result;
469}
470
471
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472static void CheckSimpleBranch(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 const v8::CpuProfileNode* node,
474 const char* names[], int length) {
475 for (int i = 0; i < length; i++) {
476 const char* name = names[i];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 node = GetChild(context, node, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 }
479}
480
481
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000482static const ProfileNode* GetSimpleBranch(v8::Local<v8::Context> context,
483 v8::CpuProfile* profile,
484 const char* names[], int length) {
485 const v8::CpuProfileNode* node = profile->GetTopDownRoot();
486 for (int i = 0; i < length; i++) {
487 node = GetChild(context, node, names[i]);
488 }
489 return reinterpret_cast<const ProfileNode*>(node);
490}
491
Ben Murdoch097c5b22016-05-18 11:27:45 +0100492static void CallCollectSample(const v8::FunctionCallbackInfo<v8::Value>& info) {
493 info.GetIsolate()->GetCpuProfiler()->CollectSample();
494}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495
Ben Murdoch097c5b22016-05-18 11:27:45 +0100496static const char* cpu_profiler_test_source =
497 "%NeverOptimizeFunction(loop);\n"
498 "%NeverOptimizeFunction(delay);\n"
499 "%NeverOptimizeFunction(bar);\n"
500 "%NeverOptimizeFunction(baz);\n"
501 "%NeverOptimizeFunction(foo);\n"
502 "%NeverOptimizeFunction(start);\n"
503 "function loop(timeout) {\n"
504 " this.mmm = 0;\n"
505 " var start = Date.now();\n"
506 " do {\n"
507 " var n = 1000;\n"
508 " while(n > 1) {\n"
509 " n--;\n"
510 " this.mmm += n * n * n;\n"
511 " }\n"
512 " } while (Date.now() - start < timeout);\n"
513 "}\n"
514 "function delay() { loop(10); }\n"
515 "function bar() { delay(); }\n"
516 "function baz() { delay(); }\n"
517 "function foo() {\n"
518 " delay();\n"
519 " bar();\n"
520 " delay();\n"
521 " baz();\n"
522 "}\n"
523 "function start(duration) {\n"
524 " var start = Date.now();\n"
525 " do {\n"
526 " foo();\n"
527 " } while (Date.now() - start < duration);\n"
528 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529
530// Check that the profile tree for the script above will look like the
531// following:
532//
533// [Top down]:
534// 1062 0 (root) [-1]
535// 1054 0 start [-1]
536// 1054 1 foo [-1]
537// 265 0 baz [-1]
538// 265 1 delay [-1]
539// 264 264 loop [-1]
540// 525 3 delay [-1]
541// 522 522 loop [-1]
542// 263 0 bar [-1]
543// 263 1 delay [-1]
544// 262 262 loop [-1]
545// 2 2 (program) [-1]
546// 6 6 (garbage collector) [-1]
547TEST(CollectCpuProfile) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100548 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 LocalContext env;
550 v8::HandleScope scope(env->GetIsolate());
551
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000552 CompileRun(cpu_profiler_test_source);
553 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554
555 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000556 v8::Local<v8::Value> args[] = {
557 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100559 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560
561 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100562 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
563 const v8::CpuProfileNode* foo_node = GetChild(env.local(), start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564
Ben Murdoch097c5b22016-05-18 11:27:45 +0100565 const char* bar_branch[] = {"bar", "delay", "loop"};
566 CheckSimpleBranch(env.local(), foo_node, bar_branch, arraysize(bar_branch));
567 const char* baz_branch[] = {"baz", "delay", "loop"};
568 CheckSimpleBranch(env.local(), foo_node, baz_branch, arraysize(baz_branch));
569 const char* delay_branch[] = {"delay", "loop"};
570 CheckSimpleBranch(env.local(), foo_node, delay_branch,
571 arraysize(delay_branch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572
573 profile->Delete();
574}
575
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576static const char* hot_deopt_no_frame_entry_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100577 "%NeverOptimizeFunction(foo);\n"
578 "%NeverOptimizeFunction(start);\n"
579 "function foo(a, b) {\n"
580 " return a + b;\n"
581 "}\n"
582 "function start(timeout) {\n"
583 " var start = Date.now();\n"
584 " do {\n"
585 " for (var i = 1; i < 1000; ++i) foo(1, i);\n"
586 " var duration = Date.now() - start;\n"
587 " } while (duration < timeout);\n"
588 " return duration;\n"
589 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590
591// Check that the profile tree for the script above will look like the
592// following:
593//
594// [Top down]:
595// 1062 0 (root) [-1]
596// 1054 0 start [-1]
597// 1054 1 foo [-1]
598// 2 2 (program) [-1]
599// 6 6 (garbage collector) [-1]
600//
Ben Murdoch097c5b22016-05-18 11:27:45 +0100601// The test checks no FP ranges are present in a deoptimized function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602// If 'foo' has no ranges the samples falling into the prologue will miss the
603// 'start' function on the stack, so 'foo' will be attached to the (root).
604TEST(HotDeoptNoFrameEntry) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100605 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606 LocalContext env;
607 v8::HandleScope scope(env->GetIsolate());
608
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000609 CompileRun(hot_deopt_no_frame_entry_test_source);
610 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611
612 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000613 v8::Local<v8::Value> args[] = {
614 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000615 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100616 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000617 function->Call(env.local(), env->Global(), arraysize(args), args)
618 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619
620 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100621 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
622 GetChild(env.local(), start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623
624 profile->Delete();
625}
626
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627TEST(CollectCpuProfileSamples) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100628 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 LocalContext env;
630 v8::HandleScope scope(env->GetIsolate());
631
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000632 CompileRun(cpu_profiler_test_source);
633 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634
635 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636 v8::Local<v8::Value> args[] = {
637 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100639 RunProfiler(env.local(), function, args, arraysize(args), 1000, 0, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000640
641 CHECK_LE(200, profile->GetSamplesCount());
642 uint64_t end_time = profile->GetEndTime();
643 uint64_t current_time = profile->GetStartTime();
644 CHECK_LE(current_time, end_time);
645 for (int i = 0; i < profile->GetSamplesCount(); i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000646 CHECK(profile->GetSample(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 uint64_t timestamp = profile->GetSampleTimestamp(i);
648 CHECK_LE(current_time, timestamp);
649 CHECK_LE(timestamp, end_time);
650 current_time = timestamp;
651 }
652
653 profile->Delete();
654}
655
Ben Murdoch097c5b22016-05-18 11:27:45 +0100656static const char* cpu_profiler_test_source2 =
657 "%NeverOptimizeFunction(loop);\n"
658 "%NeverOptimizeFunction(delay);\n"
659 "%NeverOptimizeFunction(start);\n"
660 "function loop() {}\n"
661 "function delay() { loop(); }\n"
662 "function start(duration) {\n"
663 " var start = Date.now();\n"
664 " do {\n"
665 " for (var i = 0; i < 10000; ++i) delay();\n"
666 " } while (Date.now() - start < duration);\n"
667 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668
669// Check that the profile tree doesn't contain unexpected traces:
670// - 'loop' can be called only by 'delay'
671// - 'delay' may be called only by 'start'
672// The profile will look like the following:
673//
674// [Top down]:
675// 135 0 (root) [-1] #1
676// 121 72 start [-1] #3
677// 49 33 delay [-1] #4
678// 16 16 loop [-1] #5
679// 14 14 (program) [-1] #2
680TEST(SampleWhenFrameIsNotSetup) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100681 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682 LocalContext env;
683 v8::HandleScope scope(env->GetIsolate());
684
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000685 CompileRun(cpu_profiler_test_source2);
686 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000687
Ben Murdoch097c5b22016-05-18 11:27:45 +0100688 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000689 v8::Local<v8::Value> args[] = {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100690 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000691 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100692 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693
694 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100695 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
696 const v8::CpuProfileNode* delay_node =
697 GetChild(env.local(), start_node, "delay");
698 GetChild(env.local(), delay_node, "loop");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699
700 profile->Delete();
701}
702
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000703static const char* native_accessor_test_source = "function start(count) {\n"
704" for (var i = 0; i < count; i++) {\n"
705" var o = instance.foo;\n"
706" instance.foo = o + 1;\n"
707" }\n"
708"}\n";
709
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000710class TestApiCallbacks {
711 public:
712 explicit TestApiCallbacks(int min_duration_ms)
713 : min_duration_ms_(min_duration_ms),
714 is_warming_up_(false) {}
715
716 static void Getter(v8::Local<v8::String> name,
717 const v8::PropertyCallbackInfo<v8::Value>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100718 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 data->Wait();
720 }
721
722 static void Setter(v8::Local<v8::String> name,
723 v8::Local<v8::Value> value,
724 const v8::PropertyCallbackInfo<void>& 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 Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100730 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 data->Wait();
732 }
733
734 void set_warming_up(bool value) { is_warming_up_ = value; }
735
736 private:
737 void Wait() {
738 if (is_warming_up_) return;
739 double start = v8::base::OS::TimeCurrentMillis();
740 double duration = 0;
741 while (duration < min_duration_ms_) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000742 v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743 duration = v8::base::OS::TimeCurrentMillis() - start;
744 }
745 }
746
Ben Murdoch097c5b22016-05-18 11:27:45 +0100747 template <typename T>
748 static TestApiCallbacks* FromInfo(const T& info) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000749 void* data = v8::External::Cast(*info.Data())->Value();
750 return reinterpret_cast<TestApiCallbacks*>(data);
751 }
752
753 int min_duration_ms_;
754 bool is_warming_up_;
755};
756
757
758// Test that native accessors are properly reported in the CPU profile.
759// This test checks the case when the long-running accessors are called
760// only once and the optimizer doesn't have chance to change the invocation
761// code.
762TEST(NativeAccessorUninitializedIC) {
763 LocalContext env;
764 v8::Isolate* isolate = env->GetIsolate();
765 v8::HandleScope scope(isolate);
766
767 v8::Local<v8::FunctionTemplate> func_template =
768 v8::FunctionTemplate::New(isolate);
769 v8::Local<v8::ObjectTemplate> instance_template =
770 func_template->InstanceTemplate();
771
772 TestApiCallbacks accessors(100);
773 v8::Local<v8::External> data =
774 v8::External::New(isolate, &accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000775 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
776 &TestApiCallbacks::Setter, data);
777 v8::Local<v8::Function> func =
778 func_template->GetFunction(env.local()).ToLocalChecked();
779 v8::Local<v8::Object> instance =
780 func->NewInstance(env.local()).ToLocalChecked();
781 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000783 CompileRun(native_accessor_test_source);
784 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000785
786 int32_t repeat_count = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000787 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790
791 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100792 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
793 GetChild(env.local(), start_node, "get foo");
794 GetChild(env.local(), start_node, "set foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000795
796 profile->Delete();
797}
798
799
800// Test that native accessors are properly reported in the CPU profile.
801// This test makes sure that the accessors are called enough times to become
802// hot and to trigger optimizations.
803TEST(NativeAccessorMonomorphicIC) {
804 LocalContext env;
805 v8::Isolate* isolate = env->GetIsolate();
806 v8::HandleScope scope(isolate);
807
808 v8::Local<v8::FunctionTemplate> func_template =
809 v8::FunctionTemplate::New(isolate);
810 v8::Local<v8::ObjectTemplate> instance_template =
811 func_template->InstanceTemplate();
812
813 TestApiCallbacks accessors(1);
814 v8::Local<v8::External> data =
815 v8::External::New(isolate, &accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
817 &TestApiCallbacks::Setter, data);
818 v8::Local<v8::Function> func =
819 func_template->GetFunction(env.local()).ToLocalChecked();
820 v8::Local<v8::Object> instance =
821 func->NewInstance(env.local()).ToLocalChecked();
822 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000823
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000824 CompileRun(native_accessor_test_source);
825 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826
827 {
828 // Make sure accessors ICs are in monomorphic state before starting
829 // profiling.
830 accessors.set_warming_up(true);
831 int32_t warm_up_iterations = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000832 v8::Local<v8::Value> args[] = {
833 v8::Integer::New(isolate, warm_up_iterations)};
834 function->Call(env.local(), env->Global(), arraysize(args), args)
835 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000836 accessors.set_warming_up(false);
837 }
838
839 int32_t repeat_count = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000840 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000841 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100842 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000843
844 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100845 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
846 GetChild(env.local(), start_node, "get foo");
847 GetChild(env.local(), start_node, "set foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848
849 profile->Delete();
850}
851
852
853static const char* native_method_test_source = "function start(count) {\n"
854" for (var i = 0; i < count; i++) {\n"
855" instance.fooMethod();\n"
856" }\n"
857"}\n";
858
859
860TEST(NativeMethodUninitializedIC) {
861 LocalContext env;
862 v8::Isolate* isolate = env->GetIsolate();
863 v8::HandleScope scope(isolate);
864
865 TestApiCallbacks callbacks(100);
866 v8::Local<v8::External> data =
867 v8::External::New(isolate, &callbacks);
868
869 v8::Local<v8::FunctionTemplate> func_template =
870 v8::FunctionTemplate::New(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000871 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000872 v8::Local<v8::ObjectTemplate> proto_template =
873 func_template->PrototypeTemplate();
874 v8::Local<v8::Signature> signature =
875 v8::Signature::New(isolate, func_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000876 proto_template->Set(
877 v8_str("fooMethod"),
878 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
879 signature, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000881 v8::Local<v8::Function> func =
882 func_template->GetFunction(env.local()).ToLocalChecked();
883 v8::Local<v8::Object> instance =
884 func->NewInstance(env.local()).ToLocalChecked();
885 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000886
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000887 CompileRun(native_method_test_source);
888 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000889
890 int32_t repeat_count = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000891 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000892 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100893 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000894
895 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100896 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
897 GetChild(env.local(), start_node, "fooMethod");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898
899 profile->Delete();
900}
901
902
903TEST(NativeMethodMonomorphicIC) {
904 LocalContext env;
905 v8::Isolate* isolate = env->GetIsolate();
906 v8::HandleScope scope(isolate);
907
908 TestApiCallbacks callbacks(1);
909 v8::Local<v8::External> data =
910 v8::External::New(isolate, &callbacks);
911
912 v8::Local<v8::FunctionTemplate> func_template =
913 v8::FunctionTemplate::New(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000914 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 v8::Local<v8::ObjectTemplate> proto_template =
916 func_template->PrototypeTemplate();
917 v8::Local<v8::Signature> signature =
918 v8::Signature::New(isolate, func_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000919 proto_template->Set(
920 v8_str("fooMethod"),
921 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
922 signature, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000924 v8::Local<v8::Function> func =
925 func_template->GetFunction(env.local()).ToLocalChecked();
926 v8::Local<v8::Object> instance =
927 func->NewInstance(env.local()).ToLocalChecked();
928 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000930 CompileRun(native_method_test_source);
931 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 {
933 // Make sure method ICs are in monomorphic state before starting
934 // profiling.
935 callbacks.set_warming_up(true);
936 int32_t warm_up_iterations = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000937 v8::Local<v8::Value> args[] = {
938 v8::Integer::New(isolate, warm_up_iterations)};
939 function->Call(env.local(), env->Global(), arraysize(args), args)
940 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941 callbacks.set_warming_up(false);
942 }
943
944 int32_t repeat_count = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000945 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000946 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100947 RunProfiler(env.local(), function, args, arraysize(args), 0, 200);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948
949 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000950 GetChild(env.local(), root, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100951 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
952 GetChild(env.local(), start_node, "fooMethod");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953
954 profile->Delete();
955}
956
957
958static const char* bound_function_test_source =
959 "function foo() {\n"
960 " startProfiling('my_profile');\n"
961 "}\n"
962 "function start() {\n"
963 " var callback = foo.bind(this);\n"
964 " callback();\n"
965 "}";
966
967
968TEST(BoundFunctionCall) {
969 v8::HandleScope scope(CcTest::isolate());
970 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
971 v8::Context::Scope context_scope(env);
972
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000973 CompileRun(bound_function_test_source);
974 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000975
Ben Murdoch097c5b22016-05-18 11:27:45 +0100976 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000977
978 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979
Ben Murdoch097c5b22016-05-18 11:27:45 +0100980 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
981 GetChild(env, start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982
983 profile->Delete();
984}
985
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400986// This tests checks distribution of the samples through the source lines.
Ben Murdochda12d292016-06-02 14:46:10 +0100987static void TickLines(bool optimize) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400988 CcTest::InitializeVM();
989 LocalContext env;
Ben Murdochda12d292016-06-02 14:46:10 +0100990 i::FLAG_allow_natives_syntax = true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400991 i::FLAG_turbo_source_positions = true;
992 i::Isolate* isolate = CcTest::i_isolate();
993 i::Factory* factory = isolate->factory();
994 i::HandleScope scope(isolate);
995
996 i::EmbeddedVector<char, 512> script;
997
998 const char* func_name = "func";
Ben Murdochda12d292016-06-02 14:46:10 +0100999 const char* opt_func =
1000 optimize ? "%OptimizeFunctionOnNextCall" : "%NeverOptimizeFunction";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001001 i::SNPrintF(script,
1002 "function %s() {\n"
1003 " var n = 0;\n"
1004 " var m = 100*100;\n"
1005 " while (m > 1) {\n"
1006 " m--;\n"
1007 " n += m * m * m;\n"
1008 " }\n"
1009 "}\n"
Ben Murdochda12d292016-06-02 14:46:10 +01001010 "%s(%s);\n"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001011 "%s();\n",
Ben Murdochda12d292016-06-02 14:46:10 +01001012 func_name, opt_func, func_name, func_name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001013
1014 CompileRun(script.start());
1015
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001016 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
1017 v8::Utils::OpenHandle(*GetFunction(env.local(), func_name)));
1018 CHECK(func->shared());
Ben Murdochda12d292016-06-02 14:46:10 +01001019 CHECK(func->shared()->abstract_code());
1020 i::AbstractCode* code = func->abstract_code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001021 CHECK(code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001022 i::Address code_address = code->instruction_start();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001023 CHECK(code_address);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001024
1025 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
1026 profiles->StartProfiling("", false);
1027 ProfileGenerator generator(profiles);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001028 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
1029 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001030 processor->Start();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001031 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001032
1033 // Enqueue code creation events.
1034 i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name);
1035 int line = 1;
1036 int column = 1;
1037 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, code, func->shared(), NULL,
1038 *str, line, column);
1039
1040 // Enqueue a tick event to enable code events processing.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001041 EnqueueTickSampleEvent(processor.get(), code_address);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001042
1043 processor->StopSynchronously();
1044
1045 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001046 CHECK(profile);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001047
1048 // Check the state of profile generator.
1049 CodeEntry* func_entry = generator.code_map()->FindEntry(code_address);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001050 CHECK(func_entry);
1051 CHECK_EQ(0, strcmp(func_name, func_entry->name()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001052 const i::JITLineInfoTable* line_info = func_entry->line_info();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001053 CHECK(line_info);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001054 CHECK(!line_info->empty());
1055
1056 // Check the hit source lines using V8 Public APIs.
1057 const i::ProfileTree* tree = profile->top_down();
1058 ProfileNode* root = tree->root();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001059 CHECK(root);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001060 ProfileNode* func_node = root->FindChild(func_entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001061 CHECK(func_node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001062
1063 // Add 10 faked ticks to source line #5.
1064 int hit_line = 5;
1065 int hit_count = 10;
1066 for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line);
1067
1068 unsigned int line_count = func_node->GetHitLineCount();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069 CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001070 ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count);
1071 CHECK(func_node->GetLineTicks(&entries[0], line_count));
1072 int value = 0;
1073 for (int i = 0; i < entries.length(); i++)
1074 if (entries[i].line == hit_line) {
1075 value = entries[i].hit_count;
1076 break;
1077 }
1078 CHECK_EQ(hit_count, value);
1079}
1080
Ben Murdochda12d292016-06-02 14:46:10 +01001081TEST(TickLinesBaseline) { TickLines(false); }
1082
1083TEST(TickLinesOptimized) { TickLines(true); }
1084
Ben Murdoch097c5b22016-05-18 11:27:45 +01001085static const char* call_function_test_source =
1086 "%NeverOptimizeFunction(bar);\n"
1087 "%NeverOptimizeFunction(start);\n"
1088 "function bar(n) {\n"
1089 " var s = 0;\n"
1090 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1091 " return s;\n"
1092 "}\n"
1093 "function start(duration) {\n"
1094 " var start = Date.now();\n"
1095 " do {\n"
1096 " for (var i = 0; i < 100; ++i)\n"
1097 " bar.call(this, 1000);\n"
1098 " } while (Date.now() - start < duration);\n"
1099 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001100
1101// Test that if we sampled thread when it was inside FunctionCall buitin then
1102// its caller frame will be '(unresolved function)' as we have no reliable way
1103// to resolve it.
1104//
1105// [Top down]:
1106// 96 0 (root) [-1] #1
1107// 1 1 (garbage collector) [-1] #4
1108// 5 0 (unresolved function) [-1] #5
1109// 5 5 call [-1] #6
1110// 71 70 start [-1] #3
1111// 1 1 bar [-1] #7
1112// 19 19 (program) [-1] #2
1113TEST(FunctionCallSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001114 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001115 LocalContext env;
1116 v8::HandleScope scope(env->GetIsolate());
1117
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001118 // Collect garbage that might have be generated while installing
1119 // extensions.
1120 CcTest::heap()->CollectAllGarbage();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001122 CompileRun(call_function_test_source);
1123 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124
1125 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001126 v8::Local<v8::Value> args[] = {
1127 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001129 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130
1131 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001132 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
1133 GetChild(env.local(), start_node, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134
Ben Murdoch097c5b22016-05-18 11:27:45 +01001135 const v8::CpuProfileNode* unresolved_node = FindChild(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001136 env.local(), root, i::ProfileGenerator::kUnresolvedFunctionName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001137 CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "call"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138
1139 profile->Delete();
1140}
1141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001142static const char* function_apply_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001143 "%NeverOptimizeFunction(bar);\n"
1144 "%NeverOptimizeFunction(test);\n"
1145 "%NeverOptimizeFunction(start);\n"
1146 "function bar(n) {\n"
1147 " var s = 0;\n"
1148 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1149 " return s;\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001150 "}\n"
1151 "function test() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001152 " bar.apply(this, [1000]);\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001153 "}\n"
1154 "function start(duration) {\n"
1155 " var start = Date.now();\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001156 " do {\n"
1157 " for (var i = 0; i < 100; ++i) test();\n"
1158 " } while (Date.now() - start < duration);\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001159 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001161// [Top down]:
1162// 94 0 (root) [-1] #0 1
1163// 2 2 (garbage collector) [-1] #0 7
1164// 82 49 start [-1] #16 3
1165// 1 0 (unresolved function) [-1] #0 8
1166// 1 1 apply [-1] #0 9
1167// 32 21 test [-1] #16 4
1168// 2 2 bar [-1] #16 6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001169// 10 10 (program) [-1] #0 2
1170TEST(FunctionApplySample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001171 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001172 LocalContext env;
1173 v8::HandleScope scope(env->GetIsolate());
1174
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001175 CompileRun(function_apply_test_source);
1176 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001177
1178 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001179 v8::Local<v8::Value> args[] = {
1180 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001181
1182 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001183 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001184
1185 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001186 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
1187 const v8::CpuProfileNode* test_node =
1188 GetChild(env.local(), start_node, "test");
1189 GetChild(env.local(), test_node, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001190
Ben Murdoch097c5b22016-05-18 11:27:45 +01001191 const v8::CpuProfileNode* unresolved_node = FindChild(
1192 env.local(), start_node, ProfileGenerator::kUnresolvedFunctionName);
1193 CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "apply"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001194
1195 profile->Delete();
1196}
1197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198static const char* cpu_profiler_deep_stack_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001199 "function foo(n) {\n"
1200 " if (n)\n"
1201 " foo(n - 1);\n"
1202 " else\n"
1203 " collectSample();\n"
1204 "}\n"
1205 "function start() {\n"
1206 " startProfiling('my_profile');\n"
1207 " foo(250);\n"
1208 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209
1210// Check a deep stack
1211//
1212// [Top down]:
1213// 0 (root) 0 #1
1214// 2 (program) 0 #2
1215// 0 start 21 #3 no reason
1216// 0 foo 21 #4 no reason
1217// 0 foo 21 #5 no reason
1218// ....
Ben Murdoch097c5b22016-05-18 11:27:45 +01001219// 0 foo 21 #254 no reason
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001220TEST(CpuProfileDeepStack) {
1221 v8::HandleScope scope(CcTest::isolate());
1222 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1223 v8::Context::Scope context_scope(env);
1224
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001225 CompileRun(cpu_profiler_deep_stack_test_source);
1226 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227
1228 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001229 v8::Local<v8::String> profile_name = v8_str("my_profile");
1230 function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001231 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001232 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233 // Dump collected profile to have a better diagnostic in case of failure.
1234 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1235
1236 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001237 const v8::CpuProfileNode* node = GetChild(env, root, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001238 for (int i = 0; i <= 250; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001239 node = GetChild(env, node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001240 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001241 CHECK(!FindChild(env, node, "foo"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001242
1243 profile->Delete();
1244}
1245
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246static const char* js_native_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001247 "%NeverOptimizeFunction(foo);\n"
1248 "%NeverOptimizeFunction(bar);\n"
1249 "%NeverOptimizeFunction(start);\n"
1250 "function foo(n) {\n"
1251 " var s = 0;\n"
1252 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1253 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001254 "}\n"
1255 "function bar() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001256 " foo(1000);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001257 "}\n"
1258 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001259 " CallJsFunction(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001260 "}";
1261
1262static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001263 v8::Local<v8::Function> function = info[0].As<v8::Function>();
1264 v8::Local<v8::Value> argv[] = {info[1]};
1265 function->Call(info.GetIsolate()->GetCurrentContext(), info.This(),
1266 arraysize(argv), argv)
1267 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001268}
1269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001270// [Top down]:
1271// 58 0 (root) #0 1
1272// 2 2 (program) #0 2
1273// 56 1 start #16 3
1274// 55 0 CallJsFunction #0 4
1275// 55 1 bar #16 5
1276// 54 54 foo #16 6
1277TEST(JsNativeJsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001278 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 v8::HandleScope scope(CcTest::isolate());
1280 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1281 v8::Context::Scope context_scope(env);
1282
1283 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1284 env->GetIsolate(), CallJsFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001285 v8::Local<v8::Function> func =
1286 func_template->GetFunction(env).ToLocalChecked();
1287 func->SetName(v8_str("CallJsFunction"));
1288 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001289
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001290 CompileRun(js_native_js_test_source);
1291 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001292
Ben Murdoch097c5b22016-05-18 11:27:45 +01001293 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001294
1295 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001296 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1297 const v8::CpuProfileNode* native_node =
1298 GetChild(env, start_node, "CallJsFunction");
1299 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1300 GetChild(env, bar_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001301
1302 profile->Delete();
1303}
1304
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001305static const char* js_native_js_runtime_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001306 "%NeverOptimizeFunction(foo);\n"
1307 "%NeverOptimizeFunction(bar);\n"
1308 "%NeverOptimizeFunction(start);\n"
1309 "function foo(n) {\n"
1310 " var s = 0;\n"
1311 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1312 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001313 "}\n"
1314 "var bound = foo.bind(this);\n"
1315 "function bar() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001316 " bound(1000);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001317 "}\n"
1318 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001319 " CallJsFunction(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 "}";
1321
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322// [Top down]:
1323// 57 0 (root) #0 1
1324// 55 1 start #16 3
1325// 54 0 CallJsFunction #0 4
1326// 54 3 bar #16 5
1327// 51 51 foo #16 6
1328// 2 2 (program) #0 2
1329TEST(JsNativeJsRuntimeJsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001330 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 v8::HandleScope scope(CcTest::isolate());
1332 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1333 v8::Context::Scope context_scope(env);
1334
1335 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1336 env->GetIsolate(), CallJsFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001337 v8::Local<v8::Function> func =
1338 func_template->GetFunction(env).ToLocalChecked();
1339 func->SetName(v8_str("CallJsFunction"));
1340 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001342 CompileRun(js_native_js_runtime_js_test_source);
1343 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001344 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001345
1346 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001347 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1348 const v8::CpuProfileNode* native_node =
1349 GetChild(env, start_node, "CallJsFunction");
1350 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1351 GetChild(env, bar_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352
1353 profile->Delete();
1354}
1355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001356static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) {
1357 v8::base::OS::Print("In CallJsFunction2\n");
1358 CallJsFunction(info);
1359}
1360
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001361static const char* js_native1_js_native2_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001362 "%NeverOptimizeFunction(foo);\n"
1363 "%NeverOptimizeFunction(bar);\n"
1364 "%NeverOptimizeFunction(start);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001365 "function foo() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001366 " var s = 0;\n"
1367 " for (var i = 0; i < 1000; i++) s += i * i * i;\n"
1368 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001369 "}\n"
1370 "function bar() {\n"
1371 " CallJsFunction2(foo);\n"
1372 "}\n"
1373 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001374 " CallJsFunction1(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001375 "}";
1376
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001377// [Top down]:
1378// 57 0 (root) #0 1
1379// 55 1 start #16 3
1380// 54 0 CallJsFunction1 #0 4
1381// 54 0 bar #16 5
1382// 54 0 CallJsFunction2 #0 6
1383// 54 54 foo #16 7
1384// 2 2 (program) #0 2
1385TEST(JsNative1JsNative2JsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001386 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001387 v8::HandleScope scope(CcTest::isolate());
1388 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1389 v8::Context::Scope context_scope(env);
1390
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001391 v8::Local<v8::Function> func1 =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001392 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction)
1393 ->GetFunction(env)
1394 .ToLocalChecked();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001395 func1->SetName(v8_str("CallJsFunction1"));
1396 env->Global()->Set(env, v8_str("CallJsFunction1"), func1).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001398 v8::Local<v8::Function> func2 =
1399 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction2)
1400 ->GetFunction(env)
1401 .ToLocalChecked();
1402 func2->SetName(v8_str("CallJsFunction2"));
1403 env->Global()->Set(env, v8_str("CallJsFunction2"), func2).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001405 CompileRun(js_native1_js_native2_js_test_source);
1406 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407
Ben Murdoch097c5b22016-05-18 11:27:45 +01001408 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001409
1410 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001411 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1412 const v8::CpuProfileNode* native_node1 =
1413 GetChild(env, start_node, "CallJsFunction1");
1414 const v8::CpuProfileNode* bar_node = GetChild(env, native_node1, "bar");
1415 const v8::CpuProfileNode* native_node2 =
1416 GetChild(env, bar_node, "CallJsFunction2");
1417 GetChild(env, native_node2, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418
1419 profile->Delete();
1420}
1421
Ben Murdoch097c5b22016-05-18 11:27:45 +01001422static const char* js_force_collect_sample_source =
1423 "function start() {\n"
1424 " CallCollectSample();\n"
1425 "}";
1426
1427TEST(CollectSampleAPI) {
1428 v8::HandleScope scope(CcTest::isolate());
1429 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1430 v8::Context::Scope context_scope(env);
1431
1432 v8::Local<v8::FunctionTemplate> func_template =
1433 v8::FunctionTemplate::New(env->GetIsolate(), CallCollectSample);
1434 v8::Local<v8::Function> func =
1435 func_template->GetFunction(env).ToLocalChecked();
1436 func->SetName(v8_str("CallCollectSample"));
1437 env->Global()->Set(env, v8_str("CallCollectSample"), func).FromJust();
1438
1439 CompileRun(js_force_collect_sample_source);
1440 v8::Local<v8::Function> function = GetFunction(env, "start");
1441 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1442
1443 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1444 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1445 CHECK_LE(1, start_node->GetChildrenCount());
1446 GetChild(env, start_node, "CallCollectSample");
1447
1448 profile->Delete();
1449}
1450
1451static const char* js_native_js_runtime_multiple_test_source =
1452 "%NeverOptimizeFunction(foo);\n"
1453 "%NeverOptimizeFunction(bar);\n"
1454 "%NeverOptimizeFunction(start);\n"
1455 "function foo() {\n"
1456 " return Math.sin(Math.random());\n"
1457 "}\n"
1458 "var bound = foo.bind(this);\n"
1459 "function bar() {\n"
1460 " return bound();\n"
1461 "}\n"
1462 "function start() {\n"
1463 " startProfiling('my_profile');\n"
1464 " var startTime = Date.now();\n"
1465 " do {\n"
1466 " CallJsFunction(bar);\n"
1467 " } while (Date.now() - startTime < 200);\n"
1468 "}";
1469
1470// The test check multiple entrances/exits between JS and native code.
1471//
1472// [Top down]:
1473// (root) #0 1
1474// start #16 3
1475// CallJsFunction #0 4
1476// bar #16 5
1477// foo #16 6
1478// (program) #0 2
1479TEST(JsNativeJsRuntimeJsSampleMultiple) {
1480 i::FLAG_allow_natives_syntax = true;
1481 v8::HandleScope scope(CcTest::isolate());
1482 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1483 v8::Context::Scope context_scope(env);
1484
1485 v8::Local<v8::FunctionTemplate> func_template =
1486 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction);
1487 v8::Local<v8::Function> func =
1488 func_template->GetFunction(env).ToLocalChecked();
1489 func->SetName(v8_str("CallJsFunction"));
1490 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
1491
1492 CompileRun(js_native_js_runtime_multiple_test_source);
1493 v8::Local<v8::Function> function = GetFunction(env, "start");
1494
1495 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 500, 500);
1496
1497 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1498 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1499 const v8::CpuProfileNode* native_node =
1500 GetChild(env, start_node, "CallJsFunction");
1501 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1502 GetChild(env, bar_node, "foo");
1503
1504 profile->Delete();
1505}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001506
Ben Murdochda12d292016-06-02 14:46:10 +01001507static const char* inlining_test_source =
1508 "%NeverOptimizeFunction(action);\n"
1509 "%NeverOptimizeFunction(start);\n"
1510 "%OptimizeFunctionOnNextCall(level1);\n"
1511 "%OptimizeFunctionOnNextCall(level2);\n"
1512 "%OptimizeFunctionOnNextCall(level3);\n"
1513 "var finish = false;\n"
1514 "function action(n) {\n"
1515 " var s = 0;\n"
1516 " for (var i = 0; i < n; ++i) s += i*i*i;\n"
1517 " if (finish)\n"
1518 " startProfiling('my_profile');\n"
1519 " return s;\n"
1520 "}\n"
1521 "function level3() { return action(100); }\n"
1522 "function level2() { return level3() * 2; }\n"
1523 "function level1() { return level2(); }\n"
1524 "function start() {\n"
1525 " var n = 100;\n"
1526 " while (--n)\n"
1527 " level1();\n"
1528 " finish = true;\n"
1529 " level1();\n"
1530 "}";
1531
1532// The test check multiple entrances/exits between JS and native code.
1533//
1534// [Top down]:
1535// (root) #0 1
1536// start #16 3
1537// level1 #0 4
1538// level2 #16 5
1539// level3 #16 6
1540// action #16 7
1541// (program) #0 2
1542TEST(Inlining) {
1543 i::FLAG_allow_natives_syntax = true;
1544 v8::HandleScope scope(CcTest::isolate());
1545 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1546 v8::Context::Scope context_scope(env);
1547
1548 CompileRun(inlining_test_source);
1549 v8::Local<v8::Function> function = GetFunction(env, "start");
1550
1551 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1552 v8::Local<v8::String> profile_name = v8_str("my_profile");
1553 function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
1554 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
1555 CHECK(profile);
1556 // Dump collected profile to have a better diagnostic in case of failure.
1557 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1558
1559 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1560 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1561 const v8::CpuProfileNode* level1_node = GetChild(env, start_node, "level1");
1562 const v8::CpuProfileNode* level2_node = GetChild(env, level1_node, "level2");
1563 const v8::CpuProfileNode* level3_node = GetChild(env, level2_node, "level3");
1564 GetChild(env, level3_node, "action");
1565
1566 profile->Delete();
1567}
1568
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001569// [Top down]:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001570// 0 (root) #0 1
1571// 2 (program) #0 2
1572// 3 (idle) #0 3
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001573TEST(IdleTime) {
1574 LocalContext env;
1575 v8::HandleScope scope(env->GetIsolate());
1576 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1577
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001578 v8::Local<v8::String> profile_name = v8_str("my_profile");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001579 cpu_profiler->StartProfiling(profile_name);
1580
1581 i::Isolate* isolate = CcTest::i_isolate();
1582 i::ProfilerEventsProcessor* processor = isolate->cpu_profiler()->processor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001583
Ben Murdoch097c5b22016-05-18 11:27:45 +01001584 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001585 cpu_profiler->SetIdle(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001586 for (int i = 0; i < 3; i++) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001587 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001588 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 cpu_profiler->SetIdle(false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001590 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001591
1592 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001593 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001594 // Dump collected profile to have a better diagnostic in case of failure.
1595 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1596
1597 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001598 const v8::CpuProfileNode* program_node =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599 GetChild(env.local(), root, ProfileGenerator::kProgramEntryName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001600 CHECK_EQ(0, program_node->GetChildrenCount());
1601 CHECK_GE(program_node->GetHitCount(), 2u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602
Ben Murdoch097c5b22016-05-18 11:27:45 +01001603 const v8::CpuProfileNode* idle_node =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001604 GetChild(env.local(), root, ProfileGenerator::kIdleEntryName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001605 CHECK_EQ(0, idle_node->GetChildrenCount());
1606 CHECK_GE(idle_node->GetHitCount(), 3u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001607
1608 profile->Delete();
1609}
1610
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611static void CheckFunctionDetails(v8::Isolate* isolate,
1612 const v8::CpuProfileNode* node,
1613 const char* name, const char* script_name,
1614 int script_id, int line, int column) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001615 v8::Local<v8::Context> context = isolate->GetCurrentContext();
1616 CHECK(v8_str(name)->Equals(context, node->GetFunctionName()).FromJust());
1617 CHECK(v8_str(script_name)
1618 ->Equals(context, node->GetScriptResourceName())
1619 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001620 CHECK_EQ(script_id, node->GetScriptId());
1621 CHECK_EQ(line, node->GetLineNumber());
1622 CHECK_EQ(column, node->GetColumnNumber());
1623}
1624
1625
1626TEST(FunctionDetails) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001627 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001628 v8::HandleScope scope(CcTest::isolate());
1629 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1630 v8::Context::Scope context_scope(env);
1631
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001632 v8::Local<v8::Script> script_a = CompileWithOrigin(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001633 "%NeverOptimizeFunction(foo);\n"
1634 "%NeverOptimizeFunction(bar);\n"
1635 " function foo\n() { bar(); }\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001636 " function bar() { startProfiling(); }\n",
1637 "script_a");
1638 script_a->Run(env).ToLocalChecked();
1639 v8::Local<v8::Script> script_b = CompileWithOrigin(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001640 "%NeverOptimizeFunction(baz);"
1641 "\n\n function baz() { foo(); }\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001642 "\n\nbaz();\n"
1643 "stopProfiling();\n",
1644 "script_b");
1645 script_b->Run(env).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001646 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
1647 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
1648 reinterpret_cast<ProfileNode*>(
1649 const_cast<v8::CpuProfileNode*>(current))->Print(0);
1650 // The tree should look like this:
1651 // 0 (root) 0 #1
1652 // 0 "" 19 #2 no reason script_b:1
1653 // 0 baz 19 #3 TryCatchStatement script_b:3
1654 // 0 foo 18 #4 TryCatchStatement script_a:2
1655 // 1 bar 18 #5 no reason script_a:3
1656 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001657 const v8::CpuProfileNode* script = GetChild(env, root, "");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001658 CheckFunctionDetails(env->GetIsolate(), script, "", "script_b",
1659 script_b->GetUnboundScript()->GetId(), 1, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001660 const v8::CpuProfileNode* baz = GetChild(env, script, "baz");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001661 CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b",
1662 script_b->GetUnboundScript()->GetId(), 3, 16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001663 const v8::CpuProfileNode* foo = GetChild(env, baz, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664 CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a",
Ben Murdoch097c5b22016-05-18 11:27:45 +01001665 script_a->GetUnboundScript()->GetId(), 4, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001666 const v8::CpuProfileNode* bar = GetChild(env, foo, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001667 CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a",
Ben Murdoch097c5b22016-05-18 11:27:45 +01001668 script_a->GetUnboundScript()->GetId(), 5, 14);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669}
1670
1671
1672TEST(DontStopOnFinishedProfileDelete) {
1673 v8::HandleScope scope(CcTest::isolate());
1674 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1675 v8::Context::Scope context_scope(env);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001676
1677 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
1678 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1679
1680 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001681 v8::Local<v8::String> outer = v8_str("outer");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 profiler->StartProfiling(outer);
1683 CHECK_EQ(0, iprofiler->GetProfilesCount());
1684
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001685 v8::Local<v8::String> inner = v8_str("inner");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686 profiler->StartProfiling(inner);
1687 CHECK_EQ(0, iprofiler->GetProfilesCount());
1688
1689 v8::CpuProfile* inner_profile = profiler->StopProfiling(inner);
1690 CHECK(inner_profile);
1691 CHECK_EQ(1, iprofiler->GetProfilesCount());
1692 inner_profile->Delete();
1693 inner_profile = NULL;
1694 CHECK_EQ(0, iprofiler->GetProfilesCount());
1695
1696 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer);
1697 CHECK(outer_profile);
1698 CHECK_EQ(1, iprofiler->GetProfilesCount());
1699 outer_profile->Delete();
1700 outer_profile = NULL;
1701 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +01001702}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001703
1704
1705const char* GetBranchDeoptReason(v8::Local<v8::Context> context,
1706 i::CpuProfile* iprofile, const char* branch[],
1707 int length) {
1708 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1709 const ProfileNode* iopt_function = NULL;
1710 iopt_function = GetSimpleBranch(context, profile, branch, length);
1711 CHECK_EQ(1U, iopt_function->deopt_infos().size());
1712 return iopt_function->deopt_infos()[0].deopt_reason;
1713}
1714
1715
1716// deopt at top function
1717TEST(CollectDeoptEvents) {
1718 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1719 i::FLAG_allow_natives_syntax = true;
1720 v8::HandleScope scope(CcTest::isolate());
1721 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1722 v8::Context::Scope context_scope(env);
1723 v8::Isolate* isolate = env->GetIsolate();
1724 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1725 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1726
1727 const char opt_source[] =
1728 "function opt_function%d(value, depth) {\n"
1729 " if (depth) return opt_function%d(value, depth - 1);\n"
1730 "\n"
1731 " return 10 / value;\n"
1732 "}\n"
1733 "\n";
1734
1735 for (int i = 0; i < 3; ++i) {
1736 i::EmbeddedVector<char, sizeof(opt_source) + 100> buffer;
1737 i::SNPrintF(buffer, opt_source, i, i);
1738 v8::Script::Compile(env, v8_str(buffer.start()))
1739 .ToLocalChecked()
1740 ->Run(env)
1741 .ToLocalChecked();
1742 }
1743
1744 const char* source =
1745 "startProfiling();\n"
1746 "\n"
1747 "opt_function0(1, 1);\n"
1748 "\n"
1749 "%OptimizeFunctionOnNextCall(opt_function0)\n"
1750 "\n"
1751 "opt_function0(1, 1);\n"
1752 "\n"
1753 "opt_function0(undefined, 1);\n"
1754 "\n"
1755 "opt_function1(1, 1);\n"
1756 "\n"
1757 "%OptimizeFunctionOnNextCall(opt_function1)\n"
1758 "\n"
1759 "opt_function1(1, 1);\n"
1760 "\n"
1761 "opt_function1(NaN, 1);\n"
1762 "\n"
1763 "opt_function2(1, 1);\n"
1764 "\n"
1765 "%OptimizeFunctionOnNextCall(opt_function2)\n"
1766 "\n"
1767 "opt_function2(1, 1);\n"
1768 "\n"
1769 "opt_function2(0, 1);\n"
1770 "\n"
1771 "stopProfiling();\n"
1772 "\n";
1773
1774 v8::Script::Compile(env, v8_str(source))
1775 .ToLocalChecked()
1776 ->Run(env)
1777 .ToLocalChecked();
1778 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1779 iprofile->Print();
1780 /* The expected profile
1781 [Top down]:
1782 0 (root) 0 #1
1783 23 32 #2
1784 1 opt_function2 31 #7
1785 1 opt_function2 31 #8
1786 ;;; deopted at script_id: 31 position: 106 with reason
1787 'division by zero'.
1788 2 opt_function0 29 #3
1789 4 opt_function0 29 #4
1790 ;;; deopted at script_id: 29 position: 108 with reason 'not a
1791 heap number'.
1792 0 opt_function1 30 #5
1793 1 opt_function1 30 #6
1794 ;;; deopted at script_id: 30 position: 108 with reason 'lost
1795 precision or NaN'.
1796 */
1797
1798 {
1799 const char* branch[] = {"", "opt_function0", "opt_function0"};
1800 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber),
1801 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
1802 }
1803 {
1804 const char* branch[] = {"", "opt_function1", "opt_function1"};
1805 const char* deopt_reason =
1806 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch));
1807 if (deopt_reason != reason(i::Deoptimizer::kNaN) &&
1808 deopt_reason != reason(i::Deoptimizer::kLostPrecisionOrNaN)) {
1809 FATAL(deopt_reason);
1810 }
1811 }
1812 {
1813 const char* branch[] = {"", "opt_function2", "opt_function2"};
1814 CHECK_EQ(reason(i::Deoptimizer::kDivisionByZero),
1815 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
1816 }
1817 iprofiler->DeleteProfile(iprofile);
1818}
1819
1820
1821TEST(SourceLocation) {
1822 i::FLAG_always_opt = true;
1823 i::FLAG_hydrogen_track_positions = true;
1824 LocalContext env;
1825 v8::HandleScope scope(CcTest::isolate());
1826
1827 const char* source =
1828 "function CompareStatementWithThis() {\n"
1829 " if (this === 1) {}\n"
1830 "}\n"
1831 "CompareStatementWithThis();\n";
1832
1833 v8::Script::Compile(env.local(), v8_str(source))
1834 .ToLocalChecked()
1835 ->Run(env.local())
1836 .ToLocalChecked();
1837}
1838
1839
1840static const char* inlined_source =
1841 "function opt_function(left, right) { var k = left / 10; var r = 10 / "
1842 "right; return k + r; }\n";
1843// 0.........1.........2.........3.........4....*....5.........6......*..7
1844
1845
1846// deopt at the first level inlined function
1847TEST(DeoptAtFirstLevelInlinedSource) {
1848 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1849 i::FLAG_allow_natives_syntax = true;
1850 v8::HandleScope scope(CcTest::isolate());
1851 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1852 v8::Context::Scope context_scope(env);
1853 v8::Isolate* isolate = env->GetIsolate();
1854 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1855 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1856
1857 // 0.........1.........2.........3.........4.........5.........6.........7
1858 const char* source =
1859 "function test(left, right) { return opt_function(left, right); }\n"
1860 "\n"
1861 "startProfiling();\n"
1862 "\n"
1863 "test(10, 10);\n"
1864 "\n"
1865 "%OptimizeFunctionOnNextCall(test)\n"
1866 "\n"
1867 "test(10, 10);\n"
1868 "\n"
1869 "test(undefined, 10);\n"
1870 "\n"
1871 "stopProfiling();\n"
1872 "\n";
1873
1874 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1875 inlined_script->Run(env).ToLocalChecked();
1876 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1877
1878 v8::Local<v8::Script> script = v8_compile(source);
1879 script->Run(env).ToLocalChecked();
1880 int script_id = script->GetUnboundScript()->GetId();
1881
1882 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1883 iprofile->Print();
1884 /* The expected profile output
1885 [Top down]:
1886 0 (root) 0 #1
1887 10 30 #2
1888 1 test 30 #3
1889 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1890 heap number'.
1891 ;;; Inline point: script_id 30 position: 36.
1892 4 opt_function 29 #4
1893 */
1894 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1895
1896 const char* branch[] = {"", "test"};
1897 const ProfileNode* itest_node =
1898 GetSimpleBranch(env, profile, branch, arraysize(branch));
1899 const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
1900 itest_node->deopt_infos();
1901 CHECK_EQ(1U, deopt_infos.size());
1902
1903 const v8::CpuProfileDeoptInfo& info = deopt_infos[0];
1904 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1905 CHECK_EQ(2U, info.stack.size());
1906 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1907 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1908 CHECK_EQ(script_id, info.stack[1].script_id);
1909 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1910
1911 iprofiler->DeleteProfile(iprofile);
1912}
1913
1914
1915// deopt at the second level inlined function
1916TEST(DeoptAtSecondLevelInlinedSource) {
1917 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1918 i::FLAG_allow_natives_syntax = true;
1919 v8::HandleScope scope(CcTest::isolate());
1920 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1921 v8::Context::Scope context_scope(env);
1922 v8::Isolate* isolate = env->GetIsolate();
1923 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1924 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1925
1926 // 0.........1.........2.........3.........4.........5.........6.........7
1927 const char* source =
1928 "function test2(left, right) { return opt_function(left, right); }\n"
1929 "function test1(left, right) { return test2(left, right); }\n"
1930 "\n"
1931 "startProfiling();\n"
1932 "\n"
1933 "test1(10, 10);\n"
1934 "\n"
1935 "%OptimizeFunctionOnNextCall(test1)\n"
1936 "\n"
1937 "test1(10, 10);\n"
1938 "\n"
1939 "test1(undefined, 10);\n"
1940 "\n"
1941 "stopProfiling();\n"
1942 "\n";
1943
1944 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1945 inlined_script->Run(env).ToLocalChecked();
1946 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1947
1948 v8::Local<v8::Script> script = v8_compile(source);
1949 script->Run(env).ToLocalChecked();
1950 int script_id = script->GetUnboundScript()->GetId();
1951
1952 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1953 iprofile->Print();
1954 /* The expected profile output
1955 [Top down]:
1956 0 (root) 0 #1
1957 11 30 #2
1958 1 test1 30 #3
1959 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1960 heap number'.
1961 ;;; Inline point: script_id 30 position: 37.
1962 ;;; Inline point: script_id 30 position: 103.
1963 1 test2 30 #4
1964 3 opt_function 29 #5
1965 */
1966
1967 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1968
1969 const char* branch[] = {"", "test1"};
1970 const ProfileNode* itest_node =
1971 GetSimpleBranch(env, profile, branch, arraysize(branch));
1972 const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
1973 itest_node->deopt_infos();
1974 CHECK_EQ(1U, deopt_infos.size());
1975
1976 const v8::CpuProfileDeoptInfo info = deopt_infos[0];
1977 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1978 CHECK_EQ(3U, info.stack.size());
1979 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1980 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1981 CHECK_EQ(script_id, info.stack[1].script_id);
1982 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1983 CHECK_EQ(offset(source, "test2(left, right);"), info.stack[2].position);
1984
1985 iprofiler->DeleteProfile(iprofile);
1986}
1987
1988
1989// deopt in untracked function
1990TEST(DeoptUntrackedFunction) {
1991 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1992 i::FLAG_allow_natives_syntax = true;
1993 v8::HandleScope scope(CcTest::isolate());
1994 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1995 v8::Context::Scope context_scope(env);
1996 v8::Isolate* isolate = env->GetIsolate();
1997 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1998 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1999
2000 // 0.........1.........2.........3.........4.........5.........6.........7
2001 const char* source =
2002 "function test(left, right) { return opt_function(left, right); }\n"
2003 "\n"
2004 "test(10, 10);\n"
2005 "\n"
2006 "%OptimizeFunctionOnNextCall(test)\n"
2007 "\n"
2008 "test(10, 10);\n"
2009 "\n"
2010 "startProfiling();\n" // profiler started after compilation.
2011 "\n"
2012 "test(undefined, 10);\n"
2013 "\n"
2014 "stopProfiling();\n"
2015 "\n";
2016
2017 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
2018 inlined_script->Run(env).ToLocalChecked();
2019
2020 v8::Local<v8::Script> script = v8_compile(source);
2021 script->Run(env).ToLocalChecked();
2022
2023 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
2024 iprofile->Print();
2025 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
2026
2027 const char* branch[] = {"", "test"};
2028 const ProfileNode* itest_node =
2029 GetSimpleBranch(env, profile, branch, arraysize(branch));
2030 CHECK_EQ(0U, itest_node->deopt_infos().size());
2031
2032 iprofiler->DeleteProfile(iprofile);
2033}