blob: 3bdf8add2b61e6256c9be558b54be25294fc2f42 [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/deoptimizer.h"
35#include "src/profiler/cpu-profiler-inl.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010036#include "src/profiler/profiler-listener.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"
Ben Murdoch61f157c2016-09-16 13:49:30 +010040
Steve Block6ded16b2010-05-10 14:33:55 +010041using i::CodeEntry;
42using i::CpuProfile;
Iain Merrick75681382010-08-19 15:07:18 +010043using i::CpuProfiler;
Steve Block6ded16b2010-05-10 14:33:55 +010044using i::CpuProfilesCollection;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045using i::Heap;
Steve Block6ded16b2010-05-10 14:33:55 +010046using i::ProfileGenerator;
47using i::ProfileNode;
48using i::ProfilerEventsProcessor;
Ben Murdoch61f157c2016-09-16 13:49:30 +010049using i::ProfilerListener;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050using i::ScopedVector;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051using i::Vector;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052
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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060static size_t offset(const char* src, const char* substring) {
61 const char* it = strstr(src, substring);
62 CHECK(it);
63 return static_cast<size_t>(it - src);
64}
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066static const char* reason(const i::Deoptimizer::DeoptReason reason) {
67 return i::Deoptimizer::GetDeoptReason(reason);
68}
Steve Block6ded16b2010-05-10 14:33:55 +010069
Steve Block6ded16b2010-05-10 14:33:55 +010070TEST(StartStop) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010071 CpuProfilesCollection profiles(CcTest::i_isolate());
Steve Block6ded16b2010-05-10 14:33:55 +010072 ProfileGenerator generator(&profiles);
Ben Murdoch61f157c2016-09-16 13:49:30 +010073 std::unique_ptr<ProfilerEventsProcessor> processor(
74 new ProfilerEventsProcessor(&generator, nullptr,
75 v8::base::TimeDelta::FromMicroseconds(100)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 processor->Start();
77 processor->StopSynchronously();
Steve Block6ded16b2010-05-10 14:33:55 +010078}
79
Steve Block6ded16b2010-05-10 14:33:55 +010080static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc,
81 i::Address frame1,
82 i::Address frame2 = NULL,
83 i::Address frame3 = NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 i::TickSample* sample = proc->StartTickSample();
Steve Block6ded16b2010-05-10 14:33:55 +010085 sample->pc = frame1;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010086 sample->tos = frame1;
Steve Block6ded16b2010-05-10 14:33:55 +010087 sample->frames_count = 0;
88 if (frame2 != NULL) {
89 sample->stack[0] = frame2;
90 sample->frames_count = 1;
91 }
92 if (frame3 != NULL) {
93 sample->stack[1] = frame3;
94 sample->frames_count = 2;
95 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 proc->FinishTickSample();
Steve Block6ded16b2010-05-10 14:33:55 +010097}
98
99namespace {
100
101class TestSetup {
102 public:
103 TestSetup()
104 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
105 i::FLAG_prof_browser_mode = false;
106 }
107
108 ~TestSetup() {
109 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
110 }
111
112 private:
113 bool old_flag_prof_browser_mode_;
114};
115
116} // namespace
117
Ben Murdochda12d292016-06-02 14:46:10 +0100118i::AbstractCode* CreateCode(LocalContext* env) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 static int counter = 0;
120 i::EmbeddedVector<char, 256> script;
121 i::EmbeddedVector<char, 32> name;
122
123 i::SNPrintF(name, "function_%d", ++counter);
124 const char* name_start = name.start();
125 i::SNPrintF(script,
126 "function %s() {\n"
127 "var counter = 0;\n"
128 "for (var i = 0; i < %d; ++i) counter += i;\n"
129 "return '%s_' + counter;\n"
130 "}\n"
131 "%s();\n", name_start, counter, name_start, name_start);
132 CompileRun(script.start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133
134 i::Handle<i::JSFunction> fun = i::Handle<i::JSFunction>::cast(
135 v8::Utils::OpenHandle(*GetFunction(env->local(), name_start)));
Ben Murdochda12d292016-06-02 14:46:10 +0100136 return fun->abstract_code();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137}
138
Steve Block6ded16b2010-05-10 14:33:55 +0100139TEST(CodeEvents) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 CcTest::InitializeVM();
141 LocalContext env;
142 i::Isolate* isolate = CcTest::i_isolate();
143 i::Factory* factory = isolate->factory();
Steve Block6ded16b2010-05-10 14:33:55 +0100144 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145
146 i::HandleScope scope(isolate);
147
Ben Murdochda12d292016-06-02 14:46:10 +0100148 i::AbstractCode* aaa_code = CreateCode(&env);
149 i::AbstractCode* comment_code = CreateCode(&env);
150 i::AbstractCode* args5_code = CreateCode(&env);
151 i::AbstractCode* comment2_code = CreateCode(&env);
152 i::AbstractCode* moved_code = CreateCode(&env);
153 i::AbstractCode* args3_code = CreateCode(&env);
154 i::AbstractCode* args4_code = CreateCode(&env);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155
Ben Murdoch61f157c2016-09-16 13:49:30 +0100156 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
157 ProfileGenerator* generator = new ProfileGenerator(profiles);
158 ProfilerEventsProcessor* processor = new ProfilerEventsProcessor(
159 generator, nullptr, v8::base::TimeDelta::FromMicroseconds(100));
160 CpuProfiler profiler(isolate, profiles, generator, processor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 profiles->StartProfiling("", false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 processor->Start();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100163 ProfilerListener profiler_listener(isolate);
164 isolate->code_event_dispatcher()->AddListener(&profiler_listener);
165 profiler_listener.AddObserver(&profiler);
Steve Block6ded16b2010-05-10 14:33:55 +0100166
167 // Enqueue code creation events.
Steve Block6ded16b2010-05-10 14:33:55 +0100168 const char* aaa_str = "aaa";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100170 profiler_listener.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code,
171 *aaa_name);
172 profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code,
173 "comment");
174 profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5);
175 profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code,
176 "comment2");
177 profiler_listener.CodeMoveEvent(comment2_code, moved_code->address());
178 profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3);
179 profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4);
Steve Block6ded16b2010-05-10 14:33:55 +0100180
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 // Enqueue a tick event to enable code events processing.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100182 EnqueueTickSampleEvent(processor, aaa_code->address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183
Ben Murdoch61f157c2016-09-16 13:49:30 +0100184 profiler_listener.RemoveObserver(&profiler);
185 isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 processor->StopSynchronously();
Steve Block6ded16b2010-05-10 14:33:55 +0100187
188 // Check the state of profile generator.
Ben Murdoch61f157c2016-09-16 13:49:30 +0100189 CodeEntry* aaa = generator->code_map()->FindEntry(aaa_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000190 CHECK(aaa);
191 CHECK_EQ(0, strcmp(aaa_str, aaa->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192
Ben Murdoch61f157c2016-09-16 13:49:30 +0100193 CodeEntry* comment =
194 generator->code_map()->FindEntry(comment_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 CHECK(comment);
196 CHECK_EQ(0, strcmp("comment", comment->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197
Ben Murdoch61f157c2016-09-16 13:49:30 +0100198 CodeEntry* args5 = generator->code_map()->FindEntry(args5_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 CHECK(args5);
200 CHECK_EQ(0, strcmp("5", args5->name()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201
Ben Murdoch61f157c2016-09-16 13:49:30 +0100202 CHECK(!generator->code_map()->FindEntry(comment2_code->address()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203
Ben Murdoch61f157c2016-09-16 13:49:30 +0100204 CodeEntry* comment2 = generator->code_map()->FindEntry(moved_code->address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000205 CHECK(comment2);
206 CHECK_EQ(0, strcmp("comment2", comment2->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100207}
208
Steve Block6ded16b2010-05-10 14:33:55 +0100209template<typename T>
210static int CompareProfileNodes(const T* p1, const T* p2) {
211 return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
212}
213
214TEST(TickEvents) {
215 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 LocalContext env;
217 i::Isolate* isolate = CcTest::i_isolate();
218 i::HandleScope scope(isolate);
Steve Block6ded16b2010-05-10 14:33:55 +0100219
Ben Murdochda12d292016-06-02 14:46:10 +0100220 i::AbstractCode* frame1_code = CreateCode(&env);
221 i::AbstractCode* frame2_code = CreateCode(&env);
222 i::AbstractCode* frame3_code = CreateCode(&env);
Steve Block6ded16b2010-05-10 14:33:55 +0100223
Ben Murdoch61f157c2016-09-16 13:49:30 +0100224 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
225 ProfileGenerator* generator = new ProfileGenerator(profiles);
226 ProfilerEventsProcessor* processor = new ProfilerEventsProcessor(
227 generator, nullptr, v8::base::TimeDelta::FromMicroseconds(100));
228 CpuProfiler profiler(isolate, profiles, generator, processor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000229 profiles->StartProfiling("", false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 processor->Start();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100231 ProfilerListener profiler_listener(isolate);
232 isolate->code_event_dispatcher()->AddListener(&profiler_listener);
233 profiler_listener.AddObserver(&profiler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234
Ben Murdoch61f157c2016-09-16 13:49:30 +0100235 profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb");
236 profiler_listener.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5);
237 profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238
Ben Murdoch61f157c2016-09-16 13:49:30 +0100239 EnqueueTickSampleEvent(processor, frame1_code->instruction_start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 EnqueueTickSampleEvent(
Ben Murdoch61f157c2016-09-16 13:49:30 +0100241 processor,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2,
243 frame1_code->instruction_start() + frame2_code->ExecutableSize() / 2);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100244 EnqueueTickSampleEvent(processor, frame3_code->instruction_end() - 1,
245 frame2_code->instruction_end() - 1,
246 frame1_code->instruction_end() - 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247
Ben Murdoch61f157c2016-09-16 13:49:30 +0100248 profiler_listener.RemoveObserver(&profiler);
249 isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 processor->StopSynchronously();
251 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 CHECK(profile);
Steve Block6ded16b2010-05-10 14:33:55 +0100253
254 // Check call trees.
255 const i::List<ProfileNode*>* top_down_root_children =
256 profile->top_down()->root()->children();
257 CHECK_EQ(1, top_down_root_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000258 CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100259 const i::List<ProfileNode*>* top_down_bbb_children =
260 top_down_root_children->last()->children();
261 CHECK_EQ(1, top_down_bbb_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000262 CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100263 const i::List<ProfileNode*>* top_down_stub_children =
264 top_down_bbb_children->last()->children();
265 CHECK_EQ(1, top_down_stub_children->length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name()));
Steve Block6ded16b2010-05-10 14:33:55 +0100267 const i::List<ProfileNode*>* top_down_ddd_children =
268 top_down_stub_children->last()->children();
269 CHECK_EQ(0, top_down_ddd_children->length());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100270
271 isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
Steve Block6ded16b2010-05-10 14:33:55 +0100272}
273
Iain Merrick75681382010-08-19 15:07:18 +0100274// http://crbug/51594
275// This test must not crash.
276TEST(CrashIfStoppingLastNonExistentProfile) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 CcTest::InitializeVM();
Iain Merrick75681382010-08-19 15:07:18 +0100278 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
280 profiler->StartProfiling("1");
281 profiler->StopProfiling("2");
282 profiler->StartProfiling("1");
283 profiler->StopProfiling("");
Iain Merrick75681382010-08-19 15:07:18 +0100284}
285
Steve Block053d10c2011-06-13 19:13:29 +0100286// http://code.google.com/p/v8/issues/detail?id=1398
287// Long stacks (exceeding max frames limit) must not be erased.
288TEST(Issue1398) {
289 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 LocalContext env;
291 i::Isolate* isolate = CcTest::i_isolate();
292 i::HandleScope scope(isolate);
Steve Block053d10c2011-06-13 19:13:29 +0100293
Ben Murdochda12d292016-06-02 14:46:10 +0100294 i::AbstractCode* code = CreateCode(&env);
Steve Block053d10c2011-06-13 19:13:29 +0100295
Ben Murdoch61f157c2016-09-16 13:49:30 +0100296 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
297 ProfileGenerator* generator = new ProfileGenerator(profiles);
298 ProfilerEventsProcessor* processor = new ProfilerEventsProcessor(
299 generator, nullptr, v8::base::TimeDelta::FromMicroseconds(100));
300 CpuProfiler profiler(isolate, profiles, generator, processor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 profiles->StartProfiling("", false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 processor->Start();
Ben Murdoch61f157c2016-09-16 13:49:30 +0100303 ProfilerListener profiler_listener(isolate);
304 isolate->code_event_dispatcher()->AddListener(&profiler_listener);
305 profiler_listener.AddObserver(&profiler);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306
Ben Murdoch61f157c2016-09-16 13:49:30 +0100307 profiler_listener.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308
309 i::TickSample* sample = processor->StartTickSample();
310 sample->pc = code->address();
Steve Block053d10c2011-06-13 19:13:29 +0100311 sample->tos = 0;
312 sample->frames_count = i::TickSample::kMaxFramesCount;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 for (unsigned i = 0; i < sample->frames_count; ++i) {
314 sample->stack[i] = code->address();
Steve Block053d10c2011-06-13 19:13:29 +0100315 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316 processor->FinishTickSample();
Steve Block053d10c2011-06-13 19:13:29 +0100317
Ben Murdoch61f157c2016-09-16 13:49:30 +0100318 profiler_listener.RemoveObserver(&profiler);
319 isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 processor->StopSynchronously();
321 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 CHECK(profile);
Steve Block053d10c2011-06-13 19:13:29 +0100323
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 unsigned actual_depth = 0;
Steve Block053d10c2011-06-13 19:13:29 +0100325 const ProfileNode* node = profile->top_down()->root();
326 while (node->children()->length() > 0) {
327 node = node->children()->last();
328 ++actual_depth;
329 }
330
331 CHECK_EQ(1 + i::TickSample::kMaxFramesCount, actual_depth); // +1 for PC.
332}
333
Steve Block44f0eee2011-05-26 01:26:41 +0100334TEST(DeleteAllCpuProfiles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 CcTest::InitializeVM();
Steve Block44f0eee2011-05-26 01:26:41 +0100336 TestSetup test_setup;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
338 CHECK_EQ(0, profiler->GetProfilesCount());
339 profiler->DeleteAllProfiles();
340 CHECK_EQ(0, profiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100341
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342 profiler->StartProfiling("1");
343 profiler->StopProfiling("1");
344 CHECK_EQ(1, profiler->GetProfilesCount());
345 profiler->DeleteAllProfiles();
346 CHECK_EQ(0, profiler->GetProfilesCount());
347 profiler->StartProfiling("1");
348 profiler->StartProfiling("2");
349 profiler->StopProfiling("2");
350 profiler->StopProfiling("1");
351 CHECK_EQ(2, profiler->GetProfilesCount());
352 profiler->DeleteAllProfiles();
353 CHECK_EQ(0, profiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100354
355 // Test profiling cancellation by the 'delete' command.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356 profiler->StartProfiling("1");
357 profiler->StartProfiling("2");
358 CHECK_EQ(0, profiler->GetProfilesCount());
359 profiler->DeleteAllProfiles();
360 CHECK_EQ(0, profiler->GetProfilesCount());
361}
Steve Block44f0eee2011-05-26 01:26:41 +0100362
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363
364static bool FindCpuProfile(v8::CpuProfiler* v8profiler,
365 const v8::CpuProfile* v8profile) {
366 i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler);
367 const i::CpuProfile* profile =
368 reinterpret_cast<const i::CpuProfile*>(v8profile);
369 int length = profiler->GetProfilesCount();
370 for (int i = 0; i < length; i++) {
371 if (profile == profiler->GetProfile(i))
372 return true;
373 }
374 return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100375}
376
377
378TEST(DeleteCpuProfile) {
Steve Block44f0eee2011-05-26 01:26:41 +0100379 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 v8::HandleScope scope(env->GetIsolate());
381 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
382 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler);
Steve Block44f0eee2011-05-26 01:26:41 +0100383
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 v8::Local<v8::String> name1 = v8_str("1");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 cpu_profiler->StartProfiling(name1);
387 v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 CHECK(p1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 CHECK_EQ(1, iprofiler->GetProfilesCount());
390 CHECK(FindCpuProfile(cpu_profiler, p1));
391 p1->Delete();
392 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100393
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000394 v8::Local<v8::String> name2 = v8_str("2");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 cpu_profiler->StartProfiling(name2);
396 v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397 CHECK(p2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 CHECK_EQ(1, iprofiler->GetProfilesCount());
399 CHECK(FindCpuProfile(cpu_profiler, p2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 v8::Local<v8::String> name3 = v8_str("3");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 cpu_profiler->StartProfiling(name3);
402 v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403 CHECK(p3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 CHECK_EQ(2, iprofiler->GetProfilesCount());
405 CHECK_NE(p2, p3);
406 CHECK(FindCpuProfile(cpu_profiler, p3));
407 CHECK(FindCpuProfile(cpu_profiler, p2));
408 p2->Delete();
409 CHECK_EQ(1, iprofiler->GetProfilesCount());
410 CHECK(!FindCpuProfile(cpu_profiler, p2));
411 CHECK(FindCpuProfile(cpu_profiler, p3));
412 p3->Delete();
413 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +0100414}
415
416
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417TEST(ProfileStartEndTime) {
Steve Block44f0eee2011-05-26 01:26:41 +0100418 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000419 v8::HandleScope scope(env->GetIsolate());
420 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +0100421
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000422 v8::Local<v8::String> profile_name = v8_str("test");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 cpu_profiler->StartProfiling(profile_name);
424 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
425 CHECK(profile->GetStartTime() <= profile->GetEndTime());
426}
Steve Block44f0eee2011-05-26 01:26:41 +0100427
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000428static v8::CpuProfile* RunProfiler(v8::Local<v8::Context> env,
429 v8::Local<v8::Function> function,
430 v8::Local<v8::Value> argv[], int argc,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100431 unsigned min_js_samples = 0,
432 unsigned min_external_samples = 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433 bool collect_samples = false) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 v8::Local<v8::String> profile_name = v8_str("my_profile");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000436
Ben Murdoch097c5b22016-05-18 11:27:45 +0100437 cpu_profiler->SetSamplingInterval(100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 cpu_profiler->StartProfiling(profile_name, collect_samples);
439
Ben Murdoch61f157c2016-09-16 13:49:30 +0100440 v8::sampler::Sampler* sampler =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler();
442 sampler->StartCountingSamples();
443 do {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444 function->Call(env, env->Global(), argc, argv).ToLocalChecked();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100445 } while (sampler->js_sample_count() < min_js_samples ||
446 sampler->external_sample_count() < min_external_samples);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447
448 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
449
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 // Dump collected profile to have a better diagnostic in case of failure.
452 reinterpret_cast<i::CpuProfile*>(profile)->Print();
453
454 return profile;
455}
456
457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458static const v8::CpuProfileNode* FindChild(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 const v8::CpuProfileNode* node,
460 const char* name) {
461 int count = node->GetChildrenCount();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100462 v8::Local<v8::String> name_handle = v8_str(name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000463 for (int i = 0; i < count; i++) {
464 const v8::CpuProfileNode* child = node->GetChild(i);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100465 if (name_handle->Equals(context, child->GetFunctionName()).FromJust()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466 return child;
467 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 }
469 return NULL;
470}
471
472
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000473static const v8::CpuProfileNode* GetChild(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 const v8::CpuProfileNode* node,
475 const char* name) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000476 const v8::CpuProfileNode* result = FindChild(context, node, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 if (!result) {
478 char buffer[100];
Ben Murdochc5610432016-08-08 18:44:38 +0100479 i::SNPrintF(i::ArrayVector(buffer), "Failed to GetChild: %s", name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 FATAL(buffer);
481 }
482 return result;
483}
484
485
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000486static void CheckSimpleBranch(v8::Local<v8::Context> context,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 const v8::CpuProfileNode* node,
488 const char* names[], int length) {
489 for (int i = 0; i < length; i++) {
490 const char* name = names[i];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 node = GetChild(context, node, name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000492 }
493}
494
495
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496static const ProfileNode* GetSimpleBranch(v8::Local<v8::Context> context,
497 v8::CpuProfile* profile,
498 const char* names[], int length) {
499 const v8::CpuProfileNode* node = profile->GetTopDownRoot();
500 for (int i = 0; i < length; i++) {
501 node = GetChild(context, node, names[i]);
502 }
503 return reinterpret_cast<const ProfileNode*>(node);
504}
505
Ben Murdoch097c5b22016-05-18 11:27:45 +0100506static void CallCollectSample(const v8::FunctionCallbackInfo<v8::Value>& info) {
507 info.GetIsolate()->GetCpuProfiler()->CollectSample();
508}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000509
Ben Murdoch097c5b22016-05-18 11:27:45 +0100510static const char* cpu_profiler_test_source =
511 "%NeverOptimizeFunction(loop);\n"
512 "%NeverOptimizeFunction(delay);\n"
513 "%NeverOptimizeFunction(bar);\n"
514 "%NeverOptimizeFunction(baz);\n"
515 "%NeverOptimizeFunction(foo);\n"
516 "%NeverOptimizeFunction(start);\n"
517 "function loop(timeout) {\n"
518 " this.mmm = 0;\n"
519 " var start = Date.now();\n"
520 " do {\n"
521 " var n = 1000;\n"
522 " while(n > 1) {\n"
523 " n--;\n"
524 " this.mmm += n * n * n;\n"
525 " }\n"
526 " } while (Date.now() - start < timeout);\n"
527 "}\n"
528 "function delay() { loop(10); }\n"
529 "function bar() { delay(); }\n"
530 "function baz() { delay(); }\n"
531 "function foo() {\n"
532 " delay();\n"
533 " bar();\n"
534 " delay();\n"
535 " baz();\n"
536 "}\n"
537 "function start(duration) {\n"
538 " var start = Date.now();\n"
539 " do {\n"
540 " foo();\n"
541 " } while (Date.now() - start < duration);\n"
542 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543
544// Check that the profile tree for the script above will look like the
545// following:
546//
547// [Top down]:
548// 1062 0 (root) [-1]
549// 1054 0 start [-1]
550// 1054 1 foo [-1]
551// 265 0 baz [-1]
552// 265 1 delay [-1]
553// 264 264 loop [-1]
554// 525 3 delay [-1]
555// 522 522 loop [-1]
556// 263 0 bar [-1]
557// 263 1 delay [-1]
558// 262 262 loop [-1]
559// 2 2 (program) [-1]
560// 6 6 (garbage collector) [-1]
561TEST(CollectCpuProfile) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100562 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000563 LocalContext env;
564 v8::HandleScope scope(env->GetIsolate());
565
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000566 CompileRun(cpu_profiler_test_source);
567 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568
569 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000570 v8::Local<v8::Value> args[] = {
571 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100573 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574
575 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100576 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
577 const v8::CpuProfileNode* foo_node = GetChild(env.local(), start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578
Ben Murdoch097c5b22016-05-18 11:27:45 +0100579 const char* bar_branch[] = {"bar", "delay", "loop"};
580 CheckSimpleBranch(env.local(), foo_node, bar_branch, arraysize(bar_branch));
581 const char* baz_branch[] = {"baz", "delay", "loop"};
582 CheckSimpleBranch(env.local(), foo_node, baz_branch, arraysize(baz_branch));
583 const char* delay_branch[] = {"delay", "loop"};
584 CheckSimpleBranch(env.local(), foo_node, delay_branch,
585 arraysize(delay_branch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586
587 profile->Delete();
588}
589
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590static const char* hot_deopt_no_frame_entry_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100591 "%NeverOptimizeFunction(foo);\n"
592 "%NeverOptimizeFunction(start);\n"
593 "function foo(a, b) {\n"
594 " return a + b;\n"
595 "}\n"
596 "function start(timeout) {\n"
597 " var start = Date.now();\n"
598 " do {\n"
599 " for (var i = 1; i < 1000; ++i) foo(1, i);\n"
600 " var duration = Date.now() - start;\n"
601 " } while (duration < timeout);\n"
602 " return duration;\n"
603 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604
605// Check that the profile tree for the script above will look like the
606// following:
607//
608// [Top down]:
609// 1062 0 (root) [-1]
610// 1054 0 start [-1]
611// 1054 1 foo [-1]
612// 2 2 (program) [-1]
613// 6 6 (garbage collector) [-1]
614//
Ben Murdoch097c5b22016-05-18 11:27:45 +0100615// The test checks no FP ranges are present in a deoptimized function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616// If 'foo' has no ranges the samples falling into the prologue will miss the
617// 'start' function on the stack, so 'foo' will be attached to the (root).
618TEST(HotDeoptNoFrameEntry) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100619 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620 LocalContext env;
621 v8::HandleScope scope(env->GetIsolate());
622
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000623 CompileRun(hot_deopt_no_frame_entry_test_source);
624 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000625
626 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000627 v8::Local<v8::Value> args[] = {
628 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100630 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000631 function->Call(env.local(), env->Global(), arraysize(args), args)
632 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000633
634 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100635 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
636 GetChild(env.local(), start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637
638 profile->Delete();
639}
640
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641TEST(CollectCpuProfileSamples) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100642 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000643 LocalContext env;
644 v8::HandleScope scope(env->GetIsolate());
645
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000646 CompileRun(cpu_profiler_test_source);
647 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648
649 int32_t profiling_interval_ms = 200;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000650 v8::Local<v8::Value> args[] = {
651 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100653 RunProfiler(env.local(), function, args, arraysize(args), 1000, 0, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654
655 CHECK_LE(200, profile->GetSamplesCount());
656 uint64_t end_time = profile->GetEndTime();
657 uint64_t current_time = profile->GetStartTime();
658 CHECK_LE(current_time, end_time);
659 for (int i = 0; i < profile->GetSamplesCount(); i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000660 CHECK(profile->GetSample(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661 uint64_t timestamp = profile->GetSampleTimestamp(i);
662 CHECK_LE(current_time, timestamp);
663 CHECK_LE(timestamp, end_time);
664 current_time = timestamp;
665 }
666
667 profile->Delete();
668}
669
Ben Murdoch097c5b22016-05-18 11:27:45 +0100670static const char* cpu_profiler_test_source2 =
671 "%NeverOptimizeFunction(loop);\n"
672 "%NeverOptimizeFunction(delay);\n"
673 "%NeverOptimizeFunction(start);\n"
674 "function loop() {}\n"
675 "function delay() { loop(); }\n"
676 "function start(duration) {\n"
677 " var start = Date.now();\n"
678 " do {\n"
679 " for (var i = 0; i < 10000; ++i) delay();\n"
680 " } while (Date.now() - start < duration);\n"
681 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000682
683// Check that the profile tree doesn't contain unexpected traces:
684// - 'loop' can be called only by 'delay'
685// - 'delay' may be called only by 'start'
686// The profile will look like the following:
687//
688// [Top down]:
689// 135 0 (root) [-1] #1
690// 121 72 start [-1] #3
691// 49 33 delay [-1] #4
692// 16 16 loop [-1] #5
693// 14 14 (program) [-1] #2
694TEST(SampleWhenFrameIsNotSetup) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100695 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000696 LocalContext env;
697 v8::HandleScope scope(env->GetIsolate());
698
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000699 CompileRun(cpu_profiler_test_source2);
700 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000701
Ben Murdoch097c5b22016-05-18 11:27:45 +0100702 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000703 v8::Local<v8::Value> args[] = {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100704 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000705 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100706 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000707
708 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100709 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
710 const v8::CpuProfileNode* delay_node =
711 GetChild(env.local(), start_node, "delay");
712 GetChild(env.local(), delay_node, "loop");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000713
714 profile->Delete();
715}
716
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717static const char* native_accessor_test_source = "function start(count) {\n"
718" for (var i = 0; i < count; i++) {\n"
719" var o = instance.foo;\n"
720" instance.foo = o + 1;\n"
721" }\n"
722"}\n";
723
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000724class TestApiCallbacks {
725 public:
726 explicit TestApiCallbacks(int min_duration_ms)
727 : min_duration_ms_(min_duration_ms),
728 is_warming_up_(false) {}
729
730 static void Getter(v8::Local<v8::String> name,
731 const v8::PropertyCallbackInfo<v8::Value>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100732 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 data->Wait();
734 }
735
736 static void Setter(v8::Local<v8::String> name,
737 v8::Local<v8::Value> value,
738 const v8::PropertyCallbackInfo<void>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100739 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000740 data->Wait();
741 }
742
743 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100744 TestApiCallbacks* data = FromInfo(info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000745 data->Wait();
746 }
747
748 void set_warming_up(bool value) { is_warming_up_ = value; }
749
750 private:
751 void Wait() {
752 if (is_warming_up_) return;
753 double start = v8::base::OS::TimeCurrentMillis();
754 double duration = 0;
755 while (duration < min_duration_ms_) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000756 v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 duration = v8::base::OS::TimeCurrentMillis() - start;
758 }
759 }
760
Ben Murdoch097c5b22016-05-18 11:27:45 +0100761 template <typename T>
762 static TestApiCallbacks* FromInfo(const T& info) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000763 void* data = v8::External::Cast(*info.Data())->Value();
764 return reinterpret_cast<TestApiCallbacks*>(data);
765 }
766
767 int min_duration_ms_;
768 bool is_warming_up_;
769};
770
771
772// Test that native accessors are properly reported in the CPU profile.
773// This test checks the case when the long-running accessors are called
774// only once and the optimizer doesn't have chance to change the invocation
775// code.
776TEST(NativeAccessorUninitializedIC) {
777 LocalContext env;
778 v8::Isolate* isolate = env->GetIsolate();
779 v8::HandleScope scope(isolate);
780
781 v8::Local<v8::FunctionTemplate> func_template =
782 v8::FunctionTemplate::New(isolate);
783 v8::Local<v8::ObjectTemplate> instance_template =
784 func_template->InstanceTemplate();
785
786 TestApiCallbacks accessors(100);
787 v8::Local<v8::External> data =
788 v8::External::New(isolate, &accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000789 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
790 &TestApiCallbacks::Setter, data);
791 v8::Local<v8::Function> func =
792 func_template->GetFunction(env.local()).ToLocalChecked();
793 v8::Local<v8::Object> instance =
794 func->NewInstance(env.local()).ToLocalChecked();
795 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000797 CompileRun(native_accessor_test_source);
798 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000799
800 int32_t repeat_count = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000801 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000802 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100803 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804
805 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100806 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
807 GetChild(env.local(), start_node, "get foo");
808 GetChild(env.local(), start_node, "set foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809
810 profile->Delete();
811}
812
813
814// Test that native accessors are properly reported in the CPU profile.
815// This test makes sure that the accessors are called enough times to become
816// hot and to trigger optimizations.
817TEST(NativeAccessorMonomorphicIC) {
818 LocalContext env;
819 v8::Isolate* isolate = env->GetIsolate();
820 v8::HandleScope scope(isolate);
821
822 v8::Local<v8::FunctionTemplate> func_template =
823 v8::FunctionTemplate::New(isolate);
824 v8::Local<v8::ObjectTemplate> instance_template =
825 func_template->InstanceTemplate();
826
827 TestApiCallbacks accessors(1);
828 v8::Local<v8::External> data =
829 v8::External::New(isolate, &accessors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000830 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
831 &TestApiCallbacks::Setter, data);
832 v8::Local<v8::Function> func =
833 func_template->GetFunction(env.local()).ToLocalChecked();
834 v8::Local<v8::Object> instance =
835 func->NewInstance(env.local()).ToLocalChecked();
836 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000837
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000838 CompileRun(native_accessor_test_source);
839 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000840
841 {
842 // Make sure accessors ICs are in monomorphic state before starting
843 // profiling.
844 accessors.set_warming_up(true);
845 int32_t warm_up_iterations = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000846 v8::Local<v8::Value> args[] = {
847 v8::Integer::New(isolate, warm_up_iterations)};
848 function->Call(env.local(), env->Global(), arraysize(args), args)
849 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850 accessors.set_warming_up(false);
851 }
852
853 int32_t repeat_count = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000854 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100856 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000857
858 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100859 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
860 GetChild(env.local(), start_node, "get foo");
861 GetChild(env.local(), start_node, "set foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000862
863 profile->Delete();
864}
865
866
867static const char* native_method_test_source = "function start(count) {\n"
868" for (var i = 0; i < count; i++) {\n"
869" instance.fooMethod();\n"
870" }\n"
871"}\n";
872
873
874TEST(NativeMethodUninitializedIC) {
875 LocalContext env;
876 v8::Isolate* isolate = env->GetIsolate();
877 v8::HandleScope scope(isolate);
878
879 TestApiCallbacks callbacks(100);
880 v8::Local<v8::External> data =
881 v8::External::New(isolate, &callbacks);
882
883 v8::Local<v8::FunctionTemplate> func_template =
884 v8::FunctionTemplate::New(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000885 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000886 v8::Local<v8::ObjectTemplate> proto_template =
887 func_template->PrototypeTemplate();
888 v8::Local<v8::Signature> signature =
889 v8::Signature::New(isolate, func_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000890 proto_template->Set(
891 v8_str("fooMethod"),
892 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
893 signature, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000894
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000895 v8::Local<v8::Function> func =
896 func_template->GetFunction(env.local()).ToLocalChecked();
897 v8::Local<v8::Object> instance =
898 func->NewInstance(env.local()).ToLocalChecked();
899 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000900
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000901 CompileRun(native_method_test_source);
902 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903
904 int32_t repeat_count = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000905 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100907 RunProfiler(env.local(), function, args, arraysize(args), 0, 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000908
909 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100910 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
911 GetChild(env.local(), start_node, "fooMethod");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912
913 profile->Delete();
914}
915
916
917TEST(NativeMethodMonomorphicIC) {
918 LocalContext env;
919 v8::Isolate* isolate = env->GetIsolate();
920 v8::HandleScope scope(isolate);
921
922 TestApiCallbacks callbacks(1);
923 v8::Local<v8::External> data =
924 v8::External::New(isolate, &callbacks);
925
926 v8::Local<v8::FunctionTemplate> func_template =
927 v8::FunctionTemplate::New(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000928 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929 v8::Local<v8::ObjectTemplate> proto_template =
930 func_template->PrototypeTemplate();
931 v8::Local<v8::Signature> signature =
932 v8::Signature::New(isolate, func_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000933 proto_template->Set(
934 v8_str("fooMethod"),
935 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
936 signature, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000938 v8::Local<v8::Function> func =
939 func_template->GetFunction(env.local()).ToLocalChecked();
940 v8::Local<v8::Object> instance =
941 func->NewInstance(env.local()).ToLocalChecked();
942 env->Global()->Set(env.local(), v8_str("instance"), instance).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000944 CompileRun(native_method_test_source);
945 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000946 {
947 // Make sure method ICs are in monomorphic state before starting
948 // profiling.
949 callbacks.set_warming_up(true);
950 int32_t warm_up_iterations = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 v8::Local<v8::Value> args[] = {
952 v8::Integer::New(isolate, warm_up_iterations)};
953 function->Call(env.local(), env->Global(), arraysize(args), args)
954 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 callbacks.set_warming_up(false);
956 }
957
958 int32_t repeat_count = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000959 v8::Local<v8::Value> args[] = {v8::Integer::New(isolate, repeat_count)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000960 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +0100961 RunProfiler(env.local(), function, args, arraysize(args), 0, 200);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000962
963 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000964 GetChild(env.local(), root, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100965 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
966 GetChild(env.local(), start_node, "fooMethod");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000967
968 profile->Delete();
969}
970
971
972static const char* bound_function_test_source =
973 "function foo() {\n"
974 " startProfiling('my_profile');\n"
975 "}\n"
976 "function start() {\n"
977 " var callback = foo.bind(this);\n"
978 " callback();\n"
979 "}";
980
981
982TEST(BoundFunctionCall) {
983 v8::HandleScope scope(CcTest::isolate());
984 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
985 v8::Context::Scope context_scope(env);
986
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000987 CompileRun(bound_function_test_source);
988 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989
Ben Murdoch097c5b22016-05-18 11:27:45 +0100990 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000991
992 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000993
Ben Murdoch097c5b22016-05-18 11:27:45 +0100994 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
995 GetChild(env, start_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996
997 profile->Delete();
998}
999
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001000// This tests checks distribution of the samples through the source lines.
Ben Murdochda12d292016-06-02 14:46:10 +01001001static void TickLines(bool optimize) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001002 CcTest::InitializeVM();
1003 LocalContext env;
Ben Murdochda12d292016-06-02 14:46:10 +01001004 i::FLAG_allow_natives_syntax = true;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001005 i::FLAG_turbo_source_positions = true;
1006 i::Isolate* isolate = CcTest::i_isolate();
1007 i::Factory* factory = isolate->factory();
1008 i::HandleScope scope(isolate);
1009
1010 i::EmbeddedVector<char, 512> script;
1011
1012 const char* func_name = "func";
Ben Murdochda12d292016-06-02 14:46:10 +01001013 const char* opt_func =
1014 optimize ? "%OptimizeFunctionOnNextCall" : "%NeverOptimizeFunction";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001015 i::SNPrintF(script,
1016 "function %s() {\n"
1017 " var n = 0;\n"
1018 " var m = 100*100;\n"
1019 " while (m > 1) {\n"
1020 " m--;\n"
1021 " n += m * m * m;\n"
1022 " }\n"
1023 "}\n"
Ben Murdochda12d292016-06-02 14:46:10 +01001024 "%s(%s);\n"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001025 "%s();\n",
Ben Murdochda12d292016-06-02 14:46:10 +01001026 func_name, opt_func, func_name, func_name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001027
1028 CompileRun(script.start());
1029
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001030 i::Handle<i::JSFunction> func = i::Handle<i::JSFunction>::cast(
1031 v8::Utils::OpenHandle(*GetFunction(env.local(), func_name)));
1032 CHECK(func->shared());
Ben Murdochda12d292016-06-02 14:46:10 +01001033 CHECK(func->shared()->abstract_code());
1034 i::AbstractCode* code = func->abstract_code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001035 CHECK(code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001036 i::Address code_address = code->instruction_start();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001037 CHECK(code_address);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001038
Ben Murdoch61f157c2016-09-16 13:49:30 +01001039 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate);
1040 ProfileGenerator* generator = new ProfileGenerator(profiles);
1041 ProfilerEventsProcessor* processor = new ProfilerEventsProcessor(
1042 generator, nullptr, v8::base::TimeDelta::FromMicroseconds(100));
1043 CpuProfiler profiler(isolate, profiles, generator, processor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001044 profiles->StartProfiling("", false);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001045 processor->Start();
Ben Murdoch61f157c2016-09-16 13:49:30 +01001046 ProfilerListener profiler_listener(isolate);
1047 isolate->code_event_dispatcher()->AddListener(&profiler_listener);
1048 profiler_listener.AddObserver(&profiler);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001049
1050 // Enqueue code creation events.
1051 i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name);
1052 int line = 1;
1053 int column = 1;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001054 profiler_listener.CodeCreateEvent(i::Logger::FUNCTION_TAG, code,
1055 func->shared(), *str, line, column);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001056
1057 // Enqueue a tick event to enable code events processing.
Ben Murdoch61f157c2016-09-16 13:49:30 +01001058 EnqueueTickSampleEvent(processor, code_address);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001059
Ben Murdoch61f157c2016-09-16 13:49:30 +01001060 profiler_listener.RemoveObserver(&profiler);
1061 isolate->code_event_dispatcher()->RemoveListener(&profiler_listener);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001062 processor->StopSynchronously();
1063
1064 CpuProfile* profile = profiles->StopProfiling("");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001065 CHECK(profile);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001066
1067 // Check the state of profile generator.
Ben Murdoch61f157c2016-09-16 13:49:30 +01001068 CodeEntry* func_entry = generator->code_map()->FindEntry(code_address);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069 CHECK(func_entry);
1070 CHECK_EQ(0, strcmp(func_name, func_entry->name()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001071 const i::JITLineInfoTable* line_info = func_entry->line_info();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001072 CHECK(line_info);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001073 CHECK(!line_info->empty());
1074
1075 // Check the hit source lines using V8 Public APIs.
1076 const i::ProfileTree* tree = profile->top_down();
1077 ProfileNode* root = tree->root();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001078 CHECK(root);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001079 ProfileNode* func_node = root->FindChild(func_entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001080 CHECK(func_node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001081
1082 // Add 10 faked ticks to source line #5.
1083 int hit_line = 5;
1084 int hit_count = 10;
1085 for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line);
1086
1087 unsigned int line_count = func_node->GetHitLineCount();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001088 CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001089 ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count);
1090 CHECK(func_node->GetLineTicks(&entries[0], line_count));
1091 int value = 0;
1092 for (int i = 0; i < entries.length(); i++)
1093 if (entries[i].line == hit_line) {
1094 value = entries[i].hit_count;
1095 break;
1096 }
1097 CHECK_EQ(hit_count, value);
1098}
1099
Ben Murdochda12d292016-06-02 14:46:10 +01001100TEST(TickLinesBaseline) { TickLines(false); }
1101
1102TEST(TickLinesOptimized) { TickLines(true); }
1103
Ben Murdoch097c5b22016-05-18 11:27:45 +01001104static const char* call_function_test_source =
1105 "%NeverOptimizeFunction(bar);\n"
1106 "%NeverOptimizeFunction(start);\n"
1107 "function bar(n) {\n"
1108 " var s = 0;\n"
1109 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1110 " return s;\n"
1111 "}\n"
1112 "function start(duration) {\n"
1113 " var start = Date.now();\n"
1114 " do {\n"
1115 " for (var i = 0; i < 100; ++i)\n"
1116 " bar.call(this, 1000);\n"
1117 " } while (Date.now() - start < duration);\n"
1118 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001119
1120// Test that if we sampled thread when it was inside FunctionCall buitin then
1121// its caller frame will be '(unresolved function)' as we have no reliable way
1122// to resolve it.
1123//
1124// [Top down]:
1125// 96 0 (root) [-1] #1
1126// 1 1 (garbage collector) [-1] #4
1127// 5 0 (unresolved function) [-1] #5
1128// 5 5 call [-1] #6
1129// 71 70 start [-1] #3
1130// 1 1 bar [-1] #7
1131// 19 19 (program) [-1] #2
1132TEST(FunctionCallSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001133 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134 LocalContext env;
1135 v8::HandleScope scope(env->GetIsolate());
1136
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001137 // Collect garbage that might have be generated while installing
1138 // extensions.
1139 CcTest::heap()->CollectAllGarbage();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001140
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001141 CompileRun(call_function_test_source);
1142 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143
1144 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001145 v8::Local<v8::Value> args[] = {
1146 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001148 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149
1150 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001151 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
1152 GetChild(env.local(), start_node, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153
Ben Murdoch61f157c2016-09-16 13:49:30 +01001154 const v8::CpuProfileNode* unresolved_node =
1155 FindChild(env.local(), root, i::CodeEntry::kUnresolvedFunctionName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001156 CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "call"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001157
1158 profile->Delete();
1159}
1160
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001161static const char* function_apply_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001162 "%NeverOptimizeFunction(bar);\n"
1163 "%NeverOptimizeFunction(test);\n"
1164 "%NeverOptimizeFunction(start);\n"
1165 "function bar(n) {\n"
1166 " var s = 0;\n"
1167 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1168 " return s;\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001169 "}\n"
1170 "function test() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001171 " bar.apply(this, [1000]);\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001172 "}\n"
1173 "function start(duration) {\n"
1174 " var start = Date.now();\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001175 " do {\n"
1176 " for (var i = 0; i < 100; ++i) test();\n"
1177 " } while (Date.now() - start < duration);\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001178 "}";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001179
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001180// [Top down]:
1181// 94 0 (root) [-1] #0 1
1182// 2 2 (garbage collector) [-1] #0 7
1183// 82 49 start [-1] #16 3
1184// 1 0 (unresolved function) [-1] #0 8
1185// 1 1 apply [-1] #0 9
1186// 32 21 test [-1] #16 4
1187// 2 2 bar [-1] #16 6
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001188// 10 10 (program) [-1] #0 2
1189TEST(FunctionApplySample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001190 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001191 LocalContext env;
1192 v8::HandleScope scope(env->GetIsolate());
1193
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001194 CompileRun(function_apply_test_source);
1195 v8::Local<v8::Function> function = GetFunction(env.local(), "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001196
1197 int32_t duration_ms = 100;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001198 v8::Local<v8::Value> args[] = {
1199 v8::Integer::New(env->GetIsolate(), duration_ms)};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200
1201 v8::CpuProfile* profile =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001202 RunProfiler(env.local(), function, args, arraysize(args), 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001203
1204 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001205 const v8::CpuProfileNode* start_node = GetChild(env.local(), root, "start");
1206 const v8::CpuProfileNode* test_node =
1207 GetChild(env.local(), start_node, "test");
1208 GetChild(env.local(), test_node, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209
Ben Murdoch61f157c2016-09-16 13:49:30 +01001210 const v8::CpuProfileNode* unresolved_node =
1211 FindChild(env.local(), start_node, CodeEntry::kUnresolvedFunctionName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001212 CHECK(!unresolved_node || GetChild(env.local(), unresolved_node, "apply"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213
1214 profile->Delete();
1215}
1216
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001217static const char* cpu_profiler_deep_stack_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001218 "function foo(n) {\n"
1219 " if (n)\n"
1220 " foo(n - 1);\n"
1221 " else\n"
1222 " collectSample();\n"
1223 "}\n"
1224 "function start() {\n"
1225 " startProfiling('my_profile');\n"
1226 " foo(250);\n"
1227 "}\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001228
1229// Check a deep stack
1230//
1231// [Top down]:
1232// 0 (root) 0 #1
1233// 2 (program) 0 #2
1234// 0 start 21 #3 no reason
1235// 0 foo 21 #4 no reason
1236// 0 foo 21 #5 no reason
1237// ....
Ben Murdoch097c5b22016-05-18 11:27:45 +01001238// 0 foo 21 #254 no reason
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239TEST(CpuProfileDeepStack) {
1240 v8::HandleScope scope(CcTest::isolate());
1241 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1242 v8::Context::Scope context_scope(env);
1243
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001244 CompileRun(cpu_profiler_deep_stack_test_source);
1245 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246
1247 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001248 v8::Local<v8::String> profile_name = v8_str("my_profile");
1249 function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001250 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001251 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252 // Dump collected profile to have a better diagnostic in case of failure.
1253 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1254
1255 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001256 const v8::CpuProfileNode* node = GetChild(env, root, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001257 for (int i = 0; i <= 250; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001258 node = GetChild(env, node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001259 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001260 CHECK(!FindChild(env, node, "foo"));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261
1262 profile->Delete();
1263}
1264
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001265static const char* js_native_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001266 "%NeverOptimizeFunction(foo);\n"
1267 "%NeverOptimizeFunction(bar);\n"
1268 "%NeverOptimizeFunction(start);\n"
1269 "function foo(n) {\n"
1270 " var s = 0;\n"
1271 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1272 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001273 "}\n"
1274 "function bar() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001275 " foo(1000);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001276 "}\n"
1277 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001278 " CallJsFunction(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 "}";
1280
1281static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001282 v8::Local<v8::Function> function = info[0].As<v8::Function>();
1283 v8::Local<v8::Value> argv[] = {info[1]};
1284 function->Call(info.GetIsolate()->GetCurrentContext(), info.This(),
1285 arraysize(argv), argv)
1286 .ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001287}
1288
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001289// [Top down]:
1290// 58 0 (root) #0 1
1291// 2 2 (program) #0 2
1292// 56 1 start #16 3
1293// 55 0 CallJsFunction #0 4
1294// 55 1 bar #16 5
1295// 54 54 foo #16 6
1296TEST(JsNativeJsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001297 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001298 v8::HandleScope scope(CcTest::isolate());
1299 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1300 v8::Context::Scope context_scope(env);
1301
1302 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1303 env->GetIsolate(), CallJsFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001304 v8::Local<v8::Function> func =
1305 func_template->GetFunction(env).ToLocalChecked();
1306 func->SetName(v8_str("CallJsFunction"));
1307 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 CompileRun(js_native_js_test_source);
1310 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001311
Ben Murdoch097c5b22016-05-18 11:27:45 +01001312 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001313
1314 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001315 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1316 const v8::CpuProfileNode* native_node =
1317 GetChild(env, start_node, "CallJsFunction");
1318 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1319 GetChild(env, bar_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320
1321 profile->Delete();
1322}
1323
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324static const char* js_native_js_runtime_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001325 "%NeverOptimizeFunction(foo);\n"
1326 "%NeverOptimizeFunction(bar);\n"
1327 "%NeverOptimizeFunction(start);\n"
1328 "function foo(n) {\n"
1329 " var s = 0;\n"
1330 " for (var i = 0; i < n; i++) s += i * i * i;\n"
1331 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332 "}\n"
1333 "var bound = foo.bind(this);\n"
1334 "function bar() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001335 " bound(1000);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 "}\n"
1337 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001338 " CallJsFunction(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001339 "}";
1340
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341// [Top down]:
1342// 57 0 (root) #0 1
1343// 55 1 start #16 3
1344// 54 0 CallJsFunction #0 4
1345// 54 3 bar #16 5
1346// 51 51 foo #16 6
1347// 2 2 (program) #0 2
1348TEST(JsNativeJsRuntimeJsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001349 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001350 v8::HandleScope scope(CcTest::isolate());
1351 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1352 v8::Context::Scope context_scope(env);
1353
1354 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1355 env->GetIsolate(), CallJsFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001356 v8::Local<v8::Function> func =
1357 func_template->GetFunction(env).ToLocalChecked();
1358 func->SetName(v8_str("CallJsFunction"));
1359 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001360
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001361 CompileRun(js_native_js_runtime_js_test_source);
1362 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdoch097c5b22016-05-18 11:27:45 +01001363 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001364
1365 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001366 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1367 const v8::CpuProfileNode* native_node =
1368 GetChild(env, start_node, "CallJsFunction");
1369 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1370 GetChild(env, bar_node, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001371
1372 profile->Delete();
1373}
1374
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001375static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) {
1376 v8::base::OS::Print("In CallJsFunction2\n");
1377 CallJsFunction(info);
1378}
1379
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001380static const char* js_native1_js_native2_js_test_source =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001381 "%NeverOptimizeFunction(foo);\n"
1382 "%NeverOptimizeFunction(bar);\n"
1383 "%NeverOptimizeFunction(start);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384 "function foo() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001385 " var s = 0;\n"
1386 " for (var i = 0; i < 1000; i++) s += i * i * i;\n"
1387 " return s;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001388 "}\n"
1389 "function bar() {\n"
1390 " CallJsFunction2(foo);\n"
1391 "}\n"
1392 "function start() {\n"
Ben Murdoch097c5b22016-05-18 11:27:45 +01001393 " CallJsFunction1(bar);\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 "}";
1395
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001396// [Top down]:
1397// 57 0 (root) #0 1
1398// 55 1 start #16 3
1399// 54 0 CallJsFunction1 #0 4
1400// 54 0 bar #16 5
1401// 54 0 CallJsFunction2 #0 6
1402// 54 54 foo #16 7
1403// 2 2 (program) #0 2
1404TEST(JsNative1JsNative2JsSample) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001405 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001406 v8::HandleScope scope(CcTest::isolate());
1407 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1408 v8::Context::Scope context_scope(env);
1409
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001410 v8::Local<v8::Function> func1 =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001411 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction)
1412 ->GetFunction(env)
1413 .ToLocalChecked();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001414 func1->SetName(v8_str("CallJsFunction1"));
1415 env->Global()->Set(env, v8_str("CallJsFunction1"), func1).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001417 v8::Local<v8::Function> func2 =
1418 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction2)
1419 ->GetFunction(env)
1420 .ToLocalChecked();
1421 func2->SetName(v8_str("CallJsFunction2"));
1422 env->Global()->Set(env, v8_str("CallJsFunction2"), func2).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001423
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001424 CompileRun(js_native1_js_native2_js_test_source);
1425 v8::Local<v8::Function> function = GetFunction(env, "start");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001426
Ben Murdoch097c5b22016-05-18 11:27:45 +01001427 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 1000);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001428
1429 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001430 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1431 const v8::CpuProfileNode* native_node1 =
1432 GetChild(env, start_node, "CallJsFunction1");
1433 const v8::CpuProfileNode* bar_node = GetChild(env, native_node1, "bar");
1434 const v8::CpuProfileNode* native_node2 =
1435 GetChild(env, bar_node, "CallJsFunction2");
1436 GetChild(env, native_node2, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437
1438 profile->Delete();
1439}
1440
Ben Murdoch097c5b22016-05-18 11:27:45 +01001441static const char* js_force_collect_sample_source =
1442 "function start() {\n"
1443 " CallCollectSample();\n"
1444 "}";
1445
1446TEST(CollectSampleAPI) {
1447 v8::HandleScope scope(CcTest::isolate());
1448 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1449 v8::Context::Scope context_scope(env);
1450
1451 v8::Local<v8::FunctionTemplate> func_template =
1452 v8::FunctionTemplate::New(env->GetIsolate(), CallCollectSample);
1453 v8::Local<v8::Function> func =
1454 func_template->GetFunction(env).ToLocalChecked();
1455 func->SetName(v8_str("CallCollectSample"));
1456 env->Global()->Set(env, v8_str("CallCollectSample"), func).FromJust();
1457
1458 CompileRun(js_force_collect_sample_source);
1459 v8::Local<v8::Function> function = GetFunction(env, "start");
1460 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1461
1462 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1463 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1464 CHECK_LE(1, start_node->GetChildrenCount());
1465 GetChild(env, start_node, "CallCollectSample");
1466
1467 profile->Delete();
1468}
1469
1470static const char* js_native_js_runtime_multiple_test_source =
1471 "%NeverOptimizeFunction(foo);\n"
1472 "%NeverOptimizeFunction(bar);\n"
1473 "%NeverOptimizeFunction(start);\n"
1474 "function foo() {\n"
1475 " return Math.sin(Math.random());\n"
1476 "}\n"
1477 "var bound = foo.bind(this);\n"
1478 "function bar() {\n"
1479 " return bound();\n"
1480 "}\n"
1481 "function start() {\n"
1482 " startProfiling('my_profile');\n"
1483 " var startTime = Date.now();\n"
1484 " do {\n"
1485 " CallJsFunction(bar);\n"
1486 " } while (Date.now() - startTime < 200);\n"
1487 "}";
1488
1489// The test check multiple entrances/exits between JS and native code.
1490//
1491// [Top down]:
1492// (root) #0 1
1493// start #16 3
1494// CallJsFunction #0 4
1495// bar #16 5
1496// foo #16 6
1497// (program) #0 2
1498TEST(JsNativeJsRuntimeJsSampleMultiple) {
1499 i::FLAG_allow_natives_syntax = true;
1500 v8::HandleScope scope(CcTest::isolate());
1501 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1502 v8::Context::Scope context_scope(env);
1503
1504 v8::Local<v8::FunctionTemplate> func_template =
1505 v8::FunctionTemplate::New(env->GetIsolate(), CallJsFunction);
1506 v8::Local<v8::Function> func =
1507 func_template->GetFunction(env).ToLocalChecked();
1508 func->SetName(v8_str("CallJsFunction"));
1509 env->Global()->Set(env, v8_str("CallJsFunction"), func).FromJust();
1510
1511 CompileRun(js_native_js_runtime_multiple_test_source);
1512 v8::Local<v8::Function> function = GetFunction(env, "start");
1513
1514 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 500, 500);
1515
1516 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1517 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1518 const v8::CpuProfileNode* native_node =
1519 GetChild(env, start_node, "CallJsFunction");
1520 const v8::CpuProfileNode* bar_node = GetChild(env, native_node, "bar");
1521 GetChild(env, bar_node, "foo");
1522
1523 profile->Delete();
1524}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001525
Ben Murdochda12d292016-06-02 14:46:10 +01001526static const char* inlining_test_source =
1527 "%NeverOptimizeFunction(action);\n"
1528 "%NeverOptimizeFunction(start);\n"
1529 "%OptimizeFunctionOnNextCall(level1);\n"
1530 "%OptimizeFunctionOnNextCall(level2);\n"
1531 "%OptimizeFunctionOnNextCall(level3);\n"
1532 "var finish = false;\n"
1533 "function action(n) {\n"
1534 " var s = 0;\n"
1535 " for (var i = 0; i < n; ++i) s += i*i*i;\n"
1536 " if (finish)\n"
1537 " startProfiling('my_profile');\n"
1538 " return s;\n"
1539 "}\n"
1540 "function level3() { return action(100); }\n"
1541 "function level2() { return level3() * 2; }\n"
1542 "function level1() { return level2(); }\n"
1543 "function start() {\n"
1544 " var n = 100;\n"
1545 " while (--n)\n"
1546 " level1();\n"
1547 " finish = true;\n"
1548 " level1();\n"
1549 "}";
1550
1551// The test check multiple entrances/exits between JS and native code.
1552//
1553// [Top down]:
1554// (root) #0 1
1555// start #16 3
1556// level1 #0 4
1557// level2 #16 5
1558// level3 #16 6
1559// action #16 7
1560// (program) #0 2
1561TEST(Inlining) {
1562 i::FLAG_allow_natives_syntax = true;
1563 v8::HandleScope scope(CcTest::isolate());
1564 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1565 v8::Context::Scope context_scope(env);
1566
1567 CompileRun(inlining_test_source);
1568 v8::Local<v8::Function> function = GetFunction(env, "start");
1569
1570 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1571 v8::Local<v8::String> profile_name = v8_str("my_profile");
1572 function->Call(env, env->Global(), 0, NULL).ToLocalChecked();
1573 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
1574 CHECK(profile);
1575 // Dump collected profile to have a better diagnostic in case of failure.
1576 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1577
1578 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1579 const v8::CpuProfileNode* start_node = GetChild(env, root, "start");
1580 const v8::CpuProfileNode* level1_node = GetChild(env, start_node, "level1");
1581 const v8::CpuProfileNode* level2_node = GetChild(env, level1_node, "level2");
1582 const v8::CpuProfileNode* level3_node = GetChild(env, level2_node, "level3");
1583 GetChild(env, level3_node, "action");
1584
1585 profile->Delete();
1586}
1587
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001588// [Top down]:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001589// 0 (root) #0 1
1590// 2 (program) #0 2
1591// 3 (idle) #0 3
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001592TEST(IdleTime) {
1593 LocalContext env;
1594 v8::HandleScope scope(env->GetIsolate());
1595 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1596
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001597 v8::Local<v8::String> profile_name = v8_str("my_profile");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001598 cpu_profiler->StartProfiling(profile_name);
1599
1600 i::Isolate* isolate = CcTest::i_isolate();
1601 i::ProfilerEventsProcessor* processor = isolate->cpu_profiler()->processor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602
Ben Murdoch097c5b22016-05-18 11:27:45 +01001603 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001604 cpu_profiler->SetIdle(true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001605 for (int i = 0; i < 3; i++) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001606 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001607 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001608 cpu_profiler->SetIdle(false);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001609 processor->AddCurrentStack(isolate, true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001610
1611 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001612 CHECK(profile);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613 // Dump collected profile to have a better diagnostic in case of failure.
1614 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1615
1616 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch097c5b22016-05-18 11:27:45 +01001617 const v8::CpuProfileNode* program_node =
Ben Murdoch61f157c2016-09-16 13:49:30 +01001618 GetChild(env.local(), root, CodeEntry::kProgramEntryName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001619 CHECK_EQ(0, program_node->GetChildrenCount());
1620 CHECK_GE(program_node->GetHitCount(), 2u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001621
Ben Murdoch097c5b22016-05-18 11:27:45 +01001622 const v8::CpuProfileNode* idle_node =
Ben Murdoch61f157c2016-09-16 13:49:30 +01001623 GetChild(env.local(), root, CodeEntry::kIdleEntryName);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001624 CHECK_EQ(0, idle_node->GetChildrenCount());
1625 CHECK_GE(idle_node->GetHitCount(), 3u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626
1627 profile->Delete();
1628}
1629
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630static void CheckFunctionDetails(v8::Isolate* isolate,
1631 const v8::CpuProfileNode* node,
1632 const char* name, const char* script_name,
1633 int script_id, int line, int column) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001634 v8::Local<v8::Context> context = isolate->GetCurrentContext();
1635 CHECK(v8_str(name)->Equals(context, node->GetFunctionName()).FromJust());
1636 CHECK(v8_str(script_name)
1637 ->Equals(context, node->GetScriptResourceName())
1638 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001639 CHECK_EQ(script_id, node->GetScriptId());
1640 CHECK_EQ(line, node->GetLineNumber());
1641 CHECK_EQ(column, node->GetColumnNumber());
1642}
1643
1644
1645TEST(FunctionDetails) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001646 i::FLAG_allow_natives_syntax = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001647 v8::HandleScope scope(CcTest::isolate());
1648 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1649 v8::Context::Scope context_scope(env);
1650
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001651 v8::Local<v8::Script> script_a = CompileWithOrigin(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001652 "%NeverOptimizeFunction(foo);\n"
1653 "%NeverOptimizeFunction(bar);\n"
1654 " function foo\n() { bar(); }\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001655 " function bar() { startProfiling(); }\n",
1656 "script_a");
1657 script_a->Run(env).ToLocalChecked();
1658 v8::Local<v8::Script> script_b = CompileWithOrigin(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001659 "%NeverOptimizeFunction(baz);"
1660 "\n\n function baz() { foo(); }\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001661 "\n\nbaz();\n"
1662 "stopProfiling();\n",
1663 "script_b");
1664 script_b->Run(env).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001665 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
1666 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
1667 reinterpret_cast<ProfileNode*>(
1668 const_cast<v8::CpuProfileNode*>(current))->Print(0);
1669 // The tree should look like this:
1670 // 0 (root) 0 #1
1671 // 0 "" 19 #2 no reason script_b:1
1672 // 0 baz 19 #3 TryCatchStatement script_b:3
1673 // 0 foo 18 #4 TryCatchStatement script_a:2
1674 // 1 bar 18 #5 no reason script_a:3
1675 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001676 const v8::CpuProfileNode* script = GetChild(env, root, "");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001677 CheckFunctionDetails(env->GetIsolate(), script, "", "script_b",
1678 script_b->GetUnboundScript()->GetId(), 1, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001679 const v8::CpuProfileNode* baz = GetChild(env, script, "baz");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001680 CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b",
1681 script_b->GetUnboundScript()->GetId(), 3, 16);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001682 const v8::CpuProfileNode* foo = GetChild(env, baz, "foo");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001683 CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a",
Ben Murdoch097c5b22016-05-18 11:27:45 +01001684 script_a->GetUnboundScript()->GetId(), 4, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001685 const v8::CpuProfileNode* bar = GetChild(env, foo, "bar");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686 CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a",
Ben Murdoch097c5b22016-05-18 11:27:45 +01001687 script_a->GetUnboundScript()->GetId(), 5, 14);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001688}
1689
1690
1691TEST(DontStopOnFinishedProfileDelete) {
1692 v8::HandleScope scope(CcTest::isolate());
1693 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1694 v8::Context::Scope context_scope(env);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001695
1696 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
1697 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1698
1699 CHECK_EQ(0, iprofiler->GetProfilesCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001700 v8::Local<v8::String> outer = v8_str("outer");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001701 profiler->StartProfiling(outer);
1702 CHECK_EQ(0, iprofiler->GetProfilesCount());
1703
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001704 v8::Local<v8::String> inner = v8_str("inner");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001705 profiler->StartProfiling(inner);
1706 CHECK_EQ(0, iprofiler->GetProfilesCount());
1707
1708 v8::CpuProfile* inner_profile = profiler->StopProfiling(inner);
1709 CHECK(inner_profile);
1710 CHECK_EQ(1, iprofiler->GetProfilesCount());
1711 inner_profile->Delete();
1712 inner_profile = NULL;
1713 CHECK_EQ(0, iprofiler->GetProfilesCount());
1714
1715 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer);
1716 CHECK(outer_profile);
1717 CHECK_EQ(1, iprofiler->GetProfilesCount());
1718 outer_profile->Delete();
1719 outer_profile = NULL;
1720 CHECK_EQ(0, iprofiler->GetProfilesCount());
Steve Block44f0eee2011-05-26 01:26:41 +01001721}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001722
1723
1724const char* GetBranchDeoptReason(v8::Local<v8::Context> context,
1725 i::CpuProfile* iprofile, const char* branch[],
1726 int length) {
1727 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1728 const ProfileNode* iopt_function = NULL;
1729 iopt_function = GetSimpleBranch(context, profile, branch, length);
1730 CHECK_EQ(1U, iopt_function->deopt_infos().size());
1731 return iopt_function->deopt_infos()[0].deopt_reason;
1732}
1733
1734
1735// deopt at top function
1736TEST(CollectDeoptEvents) {
1737 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1738 i::FLAG_allow_natives_syntax = true;
1739 v8::HandleScope scope(CcTest::isolate());
1740 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1741 v8::Context::Scope context_scope(env);
1742 v8::Isolate* isolate = env->GetIsolate();
1743 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1744 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1745
1746 const char opt_source[] =
1747 "function opt_function%d(value, depth) {\n"
1748 " if (depth) return opt_function%d(value, depth - 1);\n"
1749 "\n"
1750 " return 10 / value;\n"
1751 "}\n"
1752 "\n";
1753
1754 for (int i = 0; i < 3; ++i) {
1755 i::EmbeddedVector<char, sizeof(opt_source) + 100> buffer;
1756 i::SNPrintF(buffer, opt_source, i, i);
1757 v8::Script::Compile(env, v8_str(buffer.start()))
1758 .ToLocalChecked()
1759 ->Run(env)
1760 .ToLocalChecked();
1761 }
1762
1763 const char* source =
1764 "startProfiling();\n"
1765 "\n"
1766 "opt_function0(1, 1);\n"
1767 "\n"
1768 "%OptimizeFunctionOnNextCall(opt_function0)\n"
1769 "\n"
1770 "opt_function0(1, 1);\n"
1771 "\n"
1772 "opt_function0(undefined, 1);\n"
1773 "\n"
1774 "opt_function1(1, 1);\n"
1775 "\n"
1776 "%OptimizeFunctionOnNextCall(opt_function1)\n"
1777 "\n"
1778 "opt_function1(1, 1);\n"
1779 "\n"
1780 "opt_function1(NaN, 1);\n"
1781 "\n"
1782 "opt_function2(1, 1);\n"
1783 "\n"
1784 "%OptimizeFunctionOnNextCall(opt_function2)\n"
1785 "\n"
1786 "opt_function2(1, 1);\n"
1787 "\n"
1788 "opt_function2(0, 1);\n"
1789 "\n"
1790 "stopProfiling();\n"
1791 "\n";
1792
1793 v8::Script::Compile(env, v8_str(source))
1794 .ToLocalChecked()
1795 ->Run(env)
1796 .ToLocalChecked();
1797 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1798 iprofile->Print();
1799 /* The expected profile
1800 [Top down]:
1801 0 (root) 0 #1
1802 23 32 #2
1803 1 opt_function2 31 #7
1804 1 opt_function2 31 #8
1805 ;;; deopted at script_id: 31 position: 106 with reason
1806 'division by zero'.
1807 2 opt_function0 29 #3
1808 4 opt_function0 29 #4
1809 ;;; deopted at script_id: 29 position: 108 with reason 'not a
1810 heap number'.
1811 0 opt_function1 30 #5
1812 1 opt_function1 30 #6
1813 ;;; deopted at script_id: 30 position: 108 with reason 'lost
1814 precision or NaN'.
1815 */
1816
1817 {
1818 const char* branch[] = {"", "opt_function0", "opt_function0"};
1819 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber),
1820 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
1821 }
1822 {
1823 const char* branch[] = {"", "opt_function1", "opt_function1"};
1824 const char* deopt_reason =
1825 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch));
1826 if (deopt_reason != reason(i::Deoptimizer::kNaN) &&
1827 deopt_reason != reason(i::Deoptimizer::kLostPrecisionOrNaN)) {
1828 FATAL(deopt_reason);
1829 }
1830 }
1831 {
1832 const char* branch[] = {"", "opt_function2", "opt_function2"};
1833 CHECK_EQ(reason(i::Deoptimizer::kDivisionByZero),
1834 GetBranchDeoptReason(env, iprofile, branch, arraysize(branch)));
1835 }
1836 iprofiler->DeleteProfile(iprofile);
1837}
1838
1839
1840TEST(SourceLocation) {
1841 i::FLAG_always_opt = true;
1842 i::FLAG_hydrogen_track_positions = true;
1843 LocalContext env;
1844 v8::HandleScope scope(CcTest::isolate());
1845
1846 const char* source =
1847 "function CompareStatementWithThis() {\n"
1848 " if (this === 1) {}\n"
1849 "}\n"
1850 "CompareStatementWithThis();\n";
1851
1852 v8::Script::Compile(env.local(), v8_str(source))
1853 .ToLocalChecked()
1854 ->Run(env.local())
1855 .ToLocalChecked();
1856}
1857
1858
1859static const char* inlined_source =
1860 "function opt_function(left, right) { var k = left / 10; var r = 10 / "
1861 "right; return k + r; }\n";
1862// 0.........1.........2.........3.........4....*....5.........6......*..7
1863
1864
1865// deopt at the first level inlined function
1866TEST(DeoptAtFirstLevelInlinedSource) {
1867 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1868 i::FLAG_allow_natives_syntax = true;
1869 v8::HandleScope scope(CcTest::isolate());
1870 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1871 v8::Context::Scope context_scope(env);
1872 v8::Isolate* isolate = env->GetIsolate();
1873 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1874 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1875
1876 // 0.........1.........2.........3.........4.........5.........6.........7
1877 const char* source =
1878 "function test(left, right) { return opt_function(left, right); }\n"
1879 "\n"
1880 "startProfiling();\n"
1881 "\n"
1882 "test(10, 10);\n"
1883 "\n"
1884 "%OptimizeFunctionOnNextCall(test)\n"
1885 "\n"
1886 "test(10, 10);\n"
1887 "\n"
1888 "test(undefined, 10);\n"
1889 "\n"
1890 "stopProfiling();\n"
1891 "\n";
1892
1893 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1894 inlined_script->Run(env).ToLocalChecked();
1895 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1896
1897 v8::Local<v8::Script> script = v8_compile(source);
1898 script->Run(env).ToLocalChecked();
1899 int script_id = script->GetUnboundScript()->GetId();
1900
1901 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1902 iprofile->Print();
1903 /* The expected profile output
1904 [Top down]:
1905 0 (root) 0 #1
1906 10 30 #2
1907 1 test 30 #3
1908 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1909 heap number'.
1910 ;;; Inline point: script_id 30 position: 36.
1911 4 opt_function 29 #4
1912 */
1913 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1914
1915 const char* branch[] = {"", "test"};
1916 const ProfileNode* itest_node =
1917 GetSimpleBranch(env, profile, branch, arraysize(branch));
1918 const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
1919 itest_node->deopt_infos();
1920 CHECK_EQ(1U, deopt_infos.size());
1921
1922 const v8::CpuProfileDeoptInfo& info = deopt_infos[0];
1923 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1924 CHECK_EQ(2U, info.stack.size());
1925 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1926 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1927 CHECK_EQ(script_id, info.stack[1].script_id);
1928 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1929
1930 iprofiler->DeleteProfile(iprofile);
1931}
1932
1933
1934// deopt at the second level inlined function
1935TEST(DeoptAtSecondLevelInlinedSource) {
1936 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1937 i::FLAG_allow_natives_syntax = true;
1938 v8::HandleScope scope(CcTest::isolate());
1939 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1940 v8::Context::Scope context_scope(env);
1941 v8::Isolate* isolate = env->GetIsolate();
1942 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1943 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1944
1945 // 0.........1.........2.........3.........4.........5.........6.........7
1946 const char* source =
1947 "function test2(left, right) { return opt_function(left, right); }\n"
1948 "function test1(left, right) { return test2(left, right); }\n"
1949 "\n"
1950 "startProfiling();\n"
1951 "\n"
1952 "test1(10, 10);\n"
1953 "\n"
1954 "%OptimizeFunctionOnNextCall(test1)\n"
1955 "\n"
1956 "test1(10, 10);\n"
1957 "\n"
1958 "test1(undefined, 10);\n"
1959 "\n"
1960 "stopProfiling();\n"
1961 "\n";
1962
1963 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
1964 inlined_script->Run(env).ToLocalChecked();
1965 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1966
1967 v8::Local<v8::Script> script = v8_compile(source);
1968 script->Run(env).ToLocalChecked();
1969 int script_id = script->GetUnboundScript()->GetId();
1970
1971 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1972 iprofile->Print();
1973 /* The expected profile output
1974 [Top down]:
1975 0 (root) 0 #1
1976 11 30 #2
1977 1 test1 30 #3
1978 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1979 heap number'.
1980 ;;; Inline point: script_id 30 position: 37.
1981 ;;; Inline point: script_id 30 position: 103.
1982 1 test2 30 #4
1983 3 opt_function 29 #5
1984 */
1985
1986 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1987
1988 const char* branch[] = {"", "test1"};
1989 const ProfileNode* itest_node =
1990 GetSimpleBranch(env, profile, branch, arraysize(branch));
1991 const std::vector<v8::CpuProfileDeoptInfo>& deopt_infos =
1992 itest_node->deopt_infos();
1993 CHECK_EQ(1U, deopt_infos.size());
1994
1995 const v8::CpuProfileDeoptInfo info = deopt_infos[0];
1996 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1997 CHECK_EQ(3U, info.stack.size());
1998 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1999 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
2000 CHECK_EQ(script_id, info.stack[1].script_id);
2001 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
2002 CHECK_EQ(offset(source, "test2(left, right);"), info.stack[2].position);
2003
2004 iprofiler->DeleteProfile(iprofile);
2005}
2006
2007
2008// deopt in untracked function
2009TEST(DeoptUntrackedFunction) {
2010 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
2011 i::FLAG_allow_natives_syntax = true;
2012 v8::HandleScope scope(CcTest::isolate());
2013 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
2014 v8::Context::Scope context_scope(env);
2015 v8::Isolate* isolate = env->GetIsolate();
2016 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
2017 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
2018
2019 // 0.........1.........2.........3.........4.........5.........6.........7
2020 const char* source =
2021 "function test(left, right) { return opt_function(left, right); }\n"
2022 "\n"
2023 "test(10, 10);\n"
2024 "\n"
2025 "%OptimizeFunctionOnNextCall(test)\n"
2026 "\n"
2027 "test(10, 10);\n"
2028 "\n"
2029 "startProfiling();\n" // profiler started after compilation.
2030 "\n"
2031 "test(undefined, 10);\n"
2032 "\n"
2033 "stopProfiling();\n"
2034 "\n";
2035
2036 v8::Local<v8::Script> inlined_script = v8_compile(inlined_source);
2037 inlined_script->Run(env).ToLocalChecked();
2038
2039 v8::Local<v8::Script> script = v8_compile(source);
2040 script->Run(env).ToLocalChecked();
2041
2042 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
2043 iprofile->Print();
2044 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
2045
2046 const char* branch[] = {"", "test"};
2047 const ProfileNode* itest_node =
2048 GetSimpleBranch(env, profile, branch, arraysize(branch));
2049 CHECK_EQ(0U, itest_node->deopt_infos().size());
2050
2051 iprofiler->DeleteProfile(iprofile);
2052}