1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 // Tests of profiles generator and utilities.
32 #include "include/v8-profiler.h"
33 #include "src/base/platform/platform.h"
34 #include "src/cpu-profiler-inl.h"
35 #include "src/deoptimizer.h"
36 #include "src/smart-pointers.h"
37 #include "src/utils.h"
38 #include "test/cctest/cctest.h"
39 #include "test/cctest/profiler-extension.h"
43 using i::CpuProfilesCollection;
45 using i::ProfileGenerator;
47 using i::ProfilerEventsProcessor;
48 using i::ScopedVector;
49 using i::SmartPointer;
54 static v8::Local<v8::Function> GetFunction(v8::Context* env, const char* name) {
55 return v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str(name)));
59 static int offset(const char* src, const char* substring) {
60 return static_cast<int>(strstr(src, substring) - src);
64 static const char* reason(const i::Deoptimizer::DeoptReason reason) {
65 return i::Deoptimizer::GetDeoptReason(reason);
70 i::Isolate* isolate = CcTest::i_isolate();
71 CpuProfilesCollection profiles(isolate->heap());
72 ProfileGenerator generator(&profiles);
73 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
74 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
76 processor->StopSynchronously();
80 static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc,
82 i::Address frame2 = NULL,
83 i::Address frame3 = NULL) {
84 i::TickSample* sample = proc->StartTickSample();
87 sample->frames_count = 0;
89 sample->stack[0] = frame2;
90 sample->frames_count = 1;
93 sample->stack[1] = frame3;
94 sample->frames_count = 2;
96 proc->FinishTickSample();
104 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
105 i::FLAG_prof_browser_mode = false;
109 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
113 bool old_flag_prof_browser_mode_;
119 i::Code* CreateCode(LocalContext* env) {
120 static int counter = 0;
121 i::EmbeddedVector<char, 256> script;
122 i::EmbeddedVector<char, 32> name;
124 i::SNPrintF(name, "function_%d", ++counter);
125 const char* name_start = name.start();
129 "for (var i = 0; i < %d; ++i) counter += i;\n"
130 "return '%s_' + counter;\n"
132 "%s();\n", name_start, counter, name_start, name_start);
133 CompileRun(script.start());
135 i::Handle<i::JSFunction> fun =
136 v8::Utils::OpenHandle(*GetFunction(**env, name_start));
142 CcTest::InitializeVM();
144 i::Isolate* isolate = CcTest::i_isolate();
145 i::Factory* factory = isolate->factory();
146 TestSetup test_setup;
148 i::HandleScope scope(isolate);
150 i::Code* aaa_code = CreateCode(&env);
151 i::Code* comment_code = CreateCode(&env);
152 i::Code* args5_code = CreateCode(&env);
153 i::Code* comment2_code = CreateCode(&env);
154 i::Code* moved_code = CreateCode(&env);
155 i::Code* args3_code = CreateCode(&env);
156 i::Code* args4_code = CreateCode(&env);
158 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
159 profiles->StartProfiling("", false);
160 ProfileGenerator generator(profiles);
161 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
162 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
164 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
166 // Enqueue code creation events.
167 const char* aaa_str = "aaa";
168 i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str);
169 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code, *aaa_name);
170 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code, "comment");
171 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5);
172 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code, "comment2");
173 profiler.CodeMoveEvent(comment2_code->address(), moved_code->address());
174 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3);
175 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4);
177 // Enqueue a tick event to enable code events processing.
178 EnqueueTickSampleEvent(processor.get(), aaa_code->address());
180 processor->StopSynchronously();
182 // Check the state of profile generator.
183 CodeEntry* aaa = generator.code_map()->FindEntry(aaa_code->address());
185 CHECK_EQ(0, strcmp(aaa_str, aaa->name()));
187 CodeEntry* comment = generator.code_map()->FindEntry(comment_code->address());
189 CHECK_EQ(0, strcmp("comment", comment->name()));
191 CodeEntry* args5 = generator.code_map()->FindEntry(args5_code->address());
193 CHECK_EQ(0, strcmp("5", args5->name()));
195 CHECK(!generator.code_map()->FindEntry(comment2_code->address()));
197 CodeEntry* comment2 = generator.code_map()->FindEntry(moved_code->address());
199 CHECK_EQ(0, strcmp("comment2", comment2->name()));
204 static int CompareProfileNodes(const T* p1, const T* p2) {
205 return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
210 TestSetup test_setup;
212 i::Isolate* isolate = CcTest::i_isolate();
213 i::HandleScope scope(isolate);
215 i::Code* frame1_code = CreateCode(&env);
216 i::Code* frame2_code = CreateCode(&env);
217 i::Code* frame3_code = CreateCode(&env);
219 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
220 profiles->StartProfiling("", false);
221 ProfileGenerator generator(profiles);
222 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
223 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
225 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
227 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb");
228 profiler.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5);
229 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd");
231 EnqueueTickSampleEvent(processor.get(), frame1_code->instruction_start());
232 EnqueueTickSampleEvent(
234 frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2,
235 frame1_code->instruction_start() + frame2_code->ExecutableSize() / 2);
236 EnqueueTickSampleEvent(
238 frame3_code->instruction_end() - 1,
239 frame2_code->instruction_end() - 1,
240 frame1_code->instruction_end() - 1);
242 processor->StopSynchronously();
243 CpuProfile* profile = profiles->StopProfiling("");
247 const i::List<ProfileNode*>* top_down_root_children =
248 profile->top_down()->root()->children();
249 CHECK_EQ(1, top_down_root_children->length());
250 CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name()));
251 const i::List<ProfileNode*>* top_down_bbb_children =
252 top_down_root_children->last()->children();
253 CHECK_EQ(1, top_down_bbb_children->length());
254 CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name()));
255 const i::List<ProfileNode*>* top_down_stub_children =
256 top_down_bbb_children->last()->children();
257 CHECK_EQ(1, top_down_stub_children->length());
258 CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name()));
259 const i::List<ProfileNode*>* top_down_ddd_children =
260 top_down_stub_children->last()->children();
261 CHECK_EQ(0, top_down_ddd_children->length());
265 // http://crbug/51594
266 // This test must not crash.
267 TEST(CrashIfStoppingLastNonExistentProfile) {
268 CcTest::InitializeVM();
269 TestSetup test_setup;
270 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
271 profiler->StartProfiling("1");
272 profiler->StopProfiling("2");
273 profiler->StartProfiling("1");
274 profiler->StopProfiling("");
278 // http://code.google.com/p/v8/issues/detail?id=1398
279 // Long stacks (exceeding max frames limit) must not be erased.
281 TestSetup test_setup;
283 i::Isolate* isolate = CcTest::i_isolate();
284 i::HandleScope scope(isolate);
286 i::Code* code = CreateCode(&env);
288 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
289 profiles->StartProfiling("", false);
290 ProfileGenerator generator(profiles);
291 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
292 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
294 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
296 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb");
298 i::TickSample* sample = processor->StartTickSample();
299 sample->pc = code->address();
301 sample->frames_count = i::TickSample::kMaxFramesCount;
302 for (unsigned i = 0; i < sample->frames_count; ++i) {
303 sample->stack[i] = code->address();
305 processor->FinishTickSample();
307 processor->StopSynchronously();
308 CpuProfile* profile = profiles->StopProfiling("");
311 unsigned actual_depth = 0;
312 const ProfileNode* node = profile->top_down()->root();
313 while (node->children()->length() > 0) {
314 node = node->children()->last();
318 CHECK_EQ(1 + i::TickSample::kMaxFramesCount, actual_depth); // +1 for PC.
322 TEST(DeleteAllCpuProfiles) {
323 CcTest::InitializeVM();
324 TestSetup test_setup;
325 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
326 CHECK_EQ(0, profiler->GetProfilesCount());
327 profiler->DeleteAllProfiles();
328 CHECK_EQ(0, profiler->GetProfilesCount());
330 profiler->StartProfiling("1");
331 profiler->StopProfiling("1");
332 CHECK_EQ(1, profiler->GetProfilesCount());
333 profiler->DeleteAllProfiles();
334 CHECK_EQ(0, profiler->GetProfilesCount());
335 profiler->StartProfiling("1");
336 profiler->StartProfiling("2");
337 profiler->StopProfiling("2");
338 profiler->StopProfiling("1");
339 CHECK_EQ(2, profiler->GetProfilesCount());
340 profiler->DeleteAllProfiles();
341 CHECK_EQ(0, profiler->GetProfilesCount());
343 // Test profiling cancellation by the 'delete' command.
344 profiler->StartProfiling("1");
345 profiler->StartProfiling("2");
346 CHECK_EQ(0, profiler->GetProfilesCount());
347 profiler->DeleteAllProfiles();
348 CHECK_EQ(0, profiler->GetProfilesCount());
352 static bool FindCpuProfile(v8::CpuProfiler* v8profiler,
353 const v8::CpuProfile* v8profile) {
354 i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler);
355 const i::CpuProfile* profile =
356 reinterpret_cast<const i::CpuProfile*>(v8profile);
357 int length = profiler->GetProfilesCount();
358 for (int i = 0; i < length; i++) {
359 if (profile == profiler->GetProfile(i))
366 TEST(DeleteCpuProfile) {
368 v8::HandleScope scope(env->GetIsolate());
369 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
370 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler);
372 CHECK_EQ(0, iprofiler->GetProfilesCount());
373 v8::Local<v8::String> name1 = v8_str("1");
374 cpu_profiler->StartProfiling(name1);
375 v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1);
377 CHECK_EQ(1, iprofiler->GetProfilesCount());
378 CHECK(FindCpuProfile(cpu_profiler, p1));
380 CHECK_EQ(0, iprofiler->GetProfilesCount());
382 v8::Local<v8::String> name2 = v8_str("2");
383 cpu_profiler->StartProfiling(name2);
384 v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2);
386 CHECK_EQ(1, iprofiler->GetProfilesCount());
387 CHECK(FindCpuProfile(cpu_profiler, p2));
388 v8::Local<v8::String> name3 = v8_str("3");
389 cpu_profiler->StartProfiling(name3);
390 v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3);
392 CHECK_EQ(2, iprofiler->GetProfilesCount());
394 CHECK(FindCpuProfile(cpu_profiler, p3));
395 CHECK(FindCpuProfile(cpu_profiler, p2));
397 CHECK_EQ(1, iprofiler->GetProfilesCount());
398 CHECK(!FindCpuProfile(cpu_profiler, p2));
399 CHECK(FindCpuProfile(cpu_profiler, p3));
401 CHECK_EQ(0, iprofiler->GetProfilesCount());
405 TEST(ProfileStartEndTime) {
407 v8::HandleScope scope(env->GetIsolate());
408 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
410 v8::Local<v8::String> profile_name = v8_str("test");
411 cpu_profiler->StartProfiling(profile_name);
412 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
413 CHECK(profile->GetStartTime() <= profile->GetEndTime());
417 static v8::CpuProfile* RunProfiler(
418 v8::Handle<v8::Context> env, v8::Handle<v8::Function> function,
419 v8::Handle<v8::Value> argv[], int argc,
420 unsigned min_js_samples, bool collect_samples = false) {
421 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
422 v8::Local<v8::String> profile_name = v8_str("my_profile");
424 cpu_profiler->StartProfiling(profile_name, collect_samples);
426 i::Sampler* sampler =
427 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler();
428 sampler->StartCountingSamples();
430 function->Call(env->Global(), argc, argv);
431 } while (sampler->js_and_external_sample_count() < min_js_samples);
433 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
436 // Dump collected profile to have a better diagnostic in case of failure.
437 reinterpret_cast<i::CpuProfile*>(profile)->Print();
443 static bool ContainsString(v8::Handle<v8::String> string,
444 const Vector<v8::Handle<v8::String> >& vector) {
445 for (int i = 0; i < vector.length(); i++) {
446 if (string->Equals(vector[i])) return true;
452 static void CheckChildrenNames(const v8::CpuProfileNode* node,
453 const Vector<v8::Handle<v8::String> >& names) {
454 int count = node->GetChildrenCount();
455 for (int i = 0; i < count; i++) {
456 v8::Handle<v8::String> name = node->GetChild(i)->GetFunctionName();
457 if (!ContainsString(name, names)) {
459 i::SNPrintF(Vector<char>(buffer, arraysize(buffer)),
460 "Unexpected child '%s' found in '%s'",
461 *v8::String::Utf8Value(name),
462 *v8::String::Utf8Value(node->GetFunctionName()));
465 // Check that there are no duplicates.
466 for (int j = 0; j < count; j++) {
467 if (j == i) continue;
468 if (name->Equals(node->GetChild(j)->GetFunctionName())) {
470 i::SNPrintF(Vector<char>(buffer, arraysize(buffer)),
471 "Second child with the same name '%s' found in '%s'",
472 *v8::String::Utf8Value(name),
473 *v8::String::Utf8Value(node->GetFunctionName()));
481 static const v8::CpuProfileNode* FindChild(const v8::CpuProfileNode* node,
483 int count = node->GetChildrenCount();
484 v8::Handle<v8::String> nameHandle = v8_str(name);
485 for (int i = 0; i < count; i++) {
486 const v8::CpuProfileNode* child = node->GetChild(i);
487 if (nameHandle->Equals(child->GetFunctionName())) return child;
493 static const v8::CpuProfileNode* GetChild(const v8::CpuProfileNode* node,
495 const v8::CpuProfileNode* result = FindChild(node, name);
498 i::SNPrintF(Vector<char>(buffer, arraysize(buffer)),
499 "Failed to GetChild: %s", name);
506 static void CheckSimpleBranch(const v8::CpuProfileNode* node,
507 const char* names[], int length) {
508 for (int i = 0; i < length; i++) {
509 const char* name = names[i];
510 node = GetChild(node, name);
511 int expectedChildrenCount = (i == length - 1) ? 0 : 1;
512 CHECK_EQ(expectedChildrenCount, node->GetChildrenCount());
517 static const ProfileNode* GetSimpleBranch(v8::CpuProfile* profile,
518 const char* names[], int length) {
519 const v8::CpuProfileNode* node = profile->GetTopDownRoot();
520 for (int i = 0; i < length; i++) {
521 node = GetChild(node, names[i]);
523 return reinterpret_cast<const ProfileNode*>(node);
527 static const char* cpu_profiler_test_source = "function loop(timeout) {\n"
529 " var start = Date.now();\n"
530 " while (Date.now() - start < timeout) {\n"
531 " var n = 100*1000;\n"
534 " this.mmm += n * n * n;\n"
538 "function delay() { try { loop(10); } catch(e) { } }\n"
539 "function bar() { delay(); }\n"
540 "function baz() { delay(); }\n"
549 "function start(timeout) {\n"
550 " var start = Date.now();\n"
553 " var duration = Date.now() - start;\n"
554 " } while (duration < timeout);\n"
555 " return duration;\n"
559 // Check that the profile tree for the script above will look like the
563 // 1062 0 (root) [-1]
574 // 2 2 (program) [-1]
575 // 6 6 (garbage collector) [-1]
576 TEST(CollectCpuProfile) {
578 v8::HandleScope scope(env->GetIsolate());
580 CompileRun(cpu_profiler_test_source);
581 v8::Local<v8::Function> function = GetFunction(*env, "start");
583 int32_t profiling_interval_ms = 200;
584 v8::Handle<v8::Value> args[] = {
585 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)
587 v8::CpuProfile* profile =
588 RunProfiler(env.local(), function, args, arraysize(args), 200);
589 function->Call(env->Global(), arraysize(args), args);
591 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
593 ScopedVector<v8::Handle<v8::String> > names(3);
594 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
595 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
596 names[2] = v8_str("start");
597 CheckChildrenNames(root, names);
599 const v8::CpuProfileNode* startNode = GetChild(root, "start");
600 CHECK_EQ(1, startNode->GetChildrenCount());
602 const v8::CpuProfileNode* fooNode = GetChild(startNode, "foo");
603 CHECK_EQ(3, fooNode->GetChildrenCount());
605 const char* barBranch[] = { "bar", "delay", "loop" };
606 CheckSimpleBranch(fooNode, barBranch, arraysize(barBranch));
607 const char* bazBranch[] = { "baz", "delay", "loop" };
608 CheckSimpleBranch(fooNode, bazBranch, arraysize(bazBranch));
609 const char* delayBranch[] = { "delay", "loop" };
610 CheckSimpleBranch(fooNode, delayBranch, arraysize(delayBranch));
616 static const char* hot_deopt_no_frame_entry_test_source =
617 "function foo(a, b) {\n"
622 "function start(timeout) {\n"
623 " var start = Date.now();\n"
625 " for (var i = 1; i < 1000; ++i) foo(1, i);\n"
626 " var duration = Date.now() - start;\n"
627 " } while (duration < timeout);\n"
628 " return duration;\n"
631 // Check that the profile tree for the script above will look like the
635 // 1062 0 (root) [-1]
638 // 2 2 (program) [-1]
639 // 6 6 (garbage collector) [-1]
641 // The test checks no FP ranges are present in a deoptimized funcion.
642 // If 'foo' has no ranges the samples falling into the prologue will miss the
643 // 'start' function on the stack, so 'foo' will be attached to the (root).
644 TEST(HotDeoptNoFrameEntry) {
646 v8::HandleScope scope(env->GetIsolate());
648 CompileRun(hot_deopt_no_frame_entry_test_source);
649 v8::Local<v8::Function> function = GetFunction(*env, "start");
651 int32_t profiling_interval_ms = 200;
652 v8::Handle<v8::Value> args[] = {
653 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)
655 v8::CpuProfile* profile =
656 RunProfiler(env.local(), function, args, arraysize(args), 200);
657 function->Call(env->Global(), arraysize(args), args);
659 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
661 ScopedVector<v8::Handle<v8::String> > names(3);
662 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
663 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
664 names[2] = v8_str("start");
665 CheckChildrenNames(root, names);
667 const v8::CpuProfileNode* startNode = GetChild(root, "start");
668 CHECK_EQ(1, startNode->GetChildrenCount());
670 GetChild(startNode, "foo");
676 TEST(CollectCpuProfileSamples) {
678 v8::HandleScope scope(env->GetIsolate());
680 CompileRun(cpu_profiler_test_source);
681 v8::Local<v8::Function> function = GetFunction(*env, "start");
683 int32_t profiling_interval_ms = 200;
684 v8::Handle<v8::Value> args[] = {
685 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)
687 v8::CpuProfile* profile =
688 RunProfiler(env.local(), function, args, arraysize(args), 200, true);
690 CHECK_LE(200, profile->GetSamplesCount());
691 uint64_t end_time = profile->GetEndTime();
692 uint64_t current_time = profile->GetStartTime();
693 CHECK_LE(current_time, end_time);
694 for (int i = 0; i < profile->GetSamplesCount(); i++) {
695 CHECK(profile->GetSample(i));
696 uint64_t timestamp = profile->GetSampleTimestamp(i);
697 CHECK_LE(current_time, timestamp);
698 CHECK_LE(timestamp, end_time);
699 current_time = timestamp;
706 static const char* cpu_profiler_test_source2 = "function loop() {}\n"
707 "function delay() { loop(); }\n"
708 "function start(count) {\n"
712 " } while (++k < count*100*1000);\n"
715 // Check that the profile tree doesn't contain unexpected traces:
716 // - 'loop' can be called only by 'delay'
717 // - 'delay' may be called only by 'start'
718 // The profile will look like the following:
721 // 135 0 (root) [-1] #1
722 // 121 72 start [-1] #3
723 // 49 33 delay [-1] #4
724 // 16 16 loop [-1] #5
725 // 14 14 (program) [-1] #2
726 TEST(SampleWhenFrameIsNotSetup) {
728 v8::HandleScope scope(env->GetIsolate());
730 CompileRun(cpu_profiler_test_source2);
731 v8::Local<v8::Function> function = GetFunction(*env, "start");
733 int32_t repeat_count = 100;
734 #if defined(USE_SIMULATOR)
735 // Simulators are much slower.
738 v8::Handle<v8::Value> args[] = {
739 v8::Integer::New(env->GetIsolate(), repeat_count)
741 v8::CpuProfile* profile =
742 RunProfiler(env.local(), function, args, arraysize(args), 100);
744 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
746 ScopedVector<v8::Handle<v8::String> > names(3);
747 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
748 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
749 names[2] = v8_str("start");
750 CheckChildrenNames(root, names);
752 const v8::CpuProfileNode* startNode = FindChild(root, "start");
753 // On slow machines there may be no meaningfull samples at all, skip the
755 if (startNode && startNode->GetChildrenCount() > 0) {
756 CHECK_EQ(1, startNode->GetChildrenCount());
757 const v8::CpuProfileNode* delayNode = GetChild(startNode, "delay");
758 if (delayNode->GetChildrenCount() > 0) {
759 CHECK_EQ(1, delayNode->GetChildrenCount());
760 GetChild(delayNode, "loop");
768 static const char* native_accessor_test_source = "function start(count) {\n"
769 " for (var i = 0; i < count; i++) {\n"
770 " var o = instance.foo;\n"
771 " instance.foo = o + 1;\n"
776 class TestApiCallbacks {
778 explicit TestApiCallbacks(int min_duration_ms)
779 : min_duration_ms_(min_duration_ms),
780 is_warming_up_(false) {}
782 static void Getter(v8::Local<v8::String> name,
783 const v8::PropertyCallbackInfo<v8::Value>& info) {
784 TestApiCallbacks* data = fromInfo(info);
788 static void Setter(v8::Local<v8::String> name,
789 v8::Local<v8::Value> value,
790 const v8::PropertyCallbackInfo<void>& info) {
791 TestApiCallbacks* data = fromInfo(info);
795 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
796 TestApiCallbacks* data = fromInfo(info);
800 void set_warming_up(bool value) { is_warming_up_ = value; }
804 if (is_warming_up_) return;
805 double start = v8::base::OS::TimeCurrentMillis();
807 while (duration < min_duration_ms_) {
808 v8::base::OS::Sleep(1);
809 duration = v8::base::OS::TimeCurrentMillis() - start;
814 static TestApiCallbacks* fromInfo(const T& info) {
815 void* data = v8::External::Cast(*info.Data())->Value();
816 return reinterpret_cast<TestApiCallbacks*>(data);
819 int min_duration_ms_;
824 // Test that native accessors are properly reported in the CPU profile.
825 // This test checks the case when the long-running accessors are called
826 // only once and the optimizer doesn't have chance to change the invocation
828 TEST(NativeAccessorUninitializedIC) {
830 v8::Isolate* isolate = env->GetIsolate();
831 v8::HandleScope scope(isolate);
833 v8::Local<v8::FunctionTemplate> func_template =
834 v8::FunctionTemplate::New(isolate);
835 v8::Local<v8::ObjectTemplate> instance_template =
836 func_template->InstanceTemplate();
838 TestApiCallbacks accessors(100);
839 v8::Local<v8::External> data =
840 v8::External::New(isolate, &accessors);
841 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
842 &TestApiCallbacks::Setter, data);
843 v8::Local<v8::Function> func = func_template->GetFunction();
844 v8::Local<v8::Object> instance = func->NewInstance();
845 env->Global()->Set(v8_str("instance"), instance);
847 CompileRun(native_accessor_test_source);
848 v8::Local<v8::Function> function = GetFunction(*env, "start");
850 int32_t repeat_count = 1;
851 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
852 v8::CpuProfile* profile =
853 RunProfiler(env.local(), function, args, arraysize(args), 180);
855 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
856 const v8::CpuProfileNode* startNode = GetChild(root, "start");
857 GetChild(startNode, "get foo");
858 GetChild(startNode, "set foo");
864 // Test that native accessors are properly reported in the CPU profile.
865 // This test makes sure that the accessors are called enough times to become
866 // hot and to trigger optimizations.
867 TEST(NativeAccessorMonomorphicIC) {
869 v8::Isolate* isolate = env->GetIsolate();
870 v8::HandleScope scope(isolate);
872 v8::Local<v8::FunctionTemplate> func_template =
873 v8::FunctionTemplate::New(isolate);
874 v8::Local<v8::ObjectTemplate> instance_template =
875 func_template->InstanceTemplate();
877 TestApiCallbacks accessors(1);
878 v8::Local<v8::External> data =
879 v8::External::New(isolate, &accessors);
880 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
881 &TestApiCallbacks::Setter, data);
882 v8::Local<v8::Function> func = func_template->GetFunction();
883 v8::Local<v8::Object> instance = func->NewInstance();
884 env->Global()->Set(v8_str("instance"), instance);
886 CompileRun(native_accessor_test_source);
887 v8::Local<v8::Function> function = GetFunction(*env, "start");
890 // Make sure accessors ICs are in monomorphic state before starting
892 accessors.set_warming_up(true);
893 int32_t warm_up_iterations = 3;
894 v8::Handle<v8::Value> args[] = {
895 v8::Integer::New(isolate, warm_up_iterations)
897 function->Call(env->Global(), arraysize(args), args);
898 accessors.set_warming_up(false);
901 int32_t repeat_count = 100;
902 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
903 v8::CpuProfile* profile =
904 RunProfiler(env.local(), function, args, arraysize(args), 200);
906 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
907 const v8::CpuProfileNode* startNode = GetChild(root, "start");
908 GetChild(startNode, "get foo");
909 GetChild(startNode, "set foo");
915 static const char* native_method_test_source = "function start(count) {\n"
916 " for (var i = 0; i < count; i++) {\n"
917 " instance.fooMethod();\n"
922 TEST(NativeMethodUninitializedIC) {
924 v8::Isolate* isolate = env->GetIsolate();
925 v8::HandleScope scope(isolate);
927 TestApiCallbacks callbacks(100);
928 v8::Local<v8::External> data =
929 v8::External::New(isolate, &callbacks);
931 v8::Local<v8::FunctionTemplate> func_template =
932 v8::FunctionTemplate::New(isolate);
933 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
934 v8::Local<v8::ObjectTemplate> proto_template =
935 func_template->PrototypeTemplate();
936 v8::Local<v8::Signature> signature =
937 v8::Signature::New(isolate, func_template);
940 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
943 v8::Local<v8::Function> func = func_template->GetFunction();
944 v8::Local<v8::Object> instance = func->NewInstance();
945 env->Global()->Set(v8_str("instance"), instance);
947 CompileRun(native_method_test_source);
948 v8::Local<v8::Function> function = GetFunction(*env, "start");
950 int32_t repeat_count = 1;
951 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
952 v8::CpuProfile* profile =
953 RunProfiler(env.local(), function, args, arraysize(args), 100);
955 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
956 const v8::CpuProfileNode* startNode = GetChild(root, "start");
957 GetChild(startNode, "fooMethod");
963 TEST(NativeMethodMonomorphicIC) {
965 v8::Isolate* isolate = env->GetIsolate();
966 v8::HandleScope scope(isolate);
968 TestApiCallbacks callbacks(1);
969 v8::Local<v8::External> data =
970 v8::External::New(isolate, &callbacks);
972 v8::Local<v8::FunctionTemplate> func_template =
973 v8::FunctionTemplate::New(isolate);
974 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
975 v8::Local<v8::ObjectTemplate> proto_template =
976 func_template->PrototypeTemplate();
977 v8::Local<v8::Signature> signature =
978 v8::Signature::New(isolate, func_template);
981 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
984 v8::Local<v8::Function> func = func_template->GetFunction();
985 v8::Local<v8::Object> instance = func->NewInstance();
986 env->Global()->Set(v8_str("instance"), instance);
988 CompileRun(native_method_test_source);
989 v8::Local<v8::Function> function = GetFunction(*env, "start");
991 // Make sure method ICs are in monomorphic state before starting
993 callbacks.set_warming_up(true);
994 int32_t warm_up_iterations = 3;
995 v8::Handle<v8::Value> args[] = {
996 v8::Integer::New(isolate, warm_up_iterations)
998 function->Call(env->Global(), arraysize(args), args);
999 callbacks.set_warming_up(false);
1002 int32_t repeat_count = 100;
1003 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
1004 v8::CpuProfile* profile =
1005 RunProfiler(env.local(), function, args, arraysize(args), 100);
1007 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1008 GetChild(root, "start");
1009 const v8::CpuProfileNode* startNode = GetChild(root, "start");
1010 GetChild(startNode, "fooMethod");
1016 static const char* bound_function_test_source =
1017 "function foo() {\n"
1018 " startProfiling('my_profile');\n"
1020 "function start() {\n"
1021 " var callback = foo.bind(this);\n"
1026 TEST(BoundFunctionCall) {
1027 v8::HandleScope scope(CcTest::isolate());
1028 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1029 v8::Context::Scope context_scope(env);
1031 CompileRun(bound_function_test_source);
1032 v8::Local<v8::Function> function = GetFunction(*env, "start");
1034 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1036 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1037 ScopedVector<v8::Handle<v8::String> > names(3);
1038 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1039 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1040 names[2] = v8_str("start");
1041 // Don't allow |foo| node to be at the top level.
1042 CheckChildrenNames(root, names);
1044 const v8::CpuProfileNode* startNode = GetChild(root, "start");
1045 GetChild(startNode, "foo");
1051 // This tests checks distribution of the samples through the source lines.
1053 CcTest::InitializeVM();
1055 i::FLAG_turbo_source_positions = true;
1056 i::Isolate* isolate = CcTest::i_isolate();
1057 i::Factory* factory = isolate->factory();
1058 i::HandleScope scope(isolate);
1060 i::EmbeddedVector<char, 512> script;
1062 const char* func_name = "func";
1066 " var m = 100*100;\n"
1067 " while (m > 1) {\n"
1069 " n += m * m * m;\n"
1073 func_name, func_name);
1075 CompileRun(script.start());
1077 i::Handle<i::JSFunction> func =
1078 v8::Utils::OpenHandle(*GetFunction(*env, func_name));
1079 CHECK(func->shared());
1080 CHECK(func->shared()->code());
1081 i::Code* code = NULL;
1082 if (func->code()->is_optimized_code()) {
1083 code = func->code();
1085 CHECK(func->shared()->code() == func->code() || !i::FLAG_crankshaft);
1086 code = func->shared()->code();
1089 i::Address code_address = code->instruction_start();
1090 CHECK(code_address);
1092 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
1093 profiles->StartProfiling("", false);
1094 ProfileGenerator generator(profiles);
1095 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
1096 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
1098 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
1100 // Enqueue code creation events.
1101 i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name);
1104 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, code, func->shared(), NULL,
1105 *str, line, column);
1107 // Enqueue a tick event to enable code events processing.
1108 EnqueueTickSampleEvent(processor.get(), code_address);
1110 processor->StopSynchronously();
1112 CpuProfile* profile = profiles->StopProfiling("");
1115 // Check the state of profile generator.
1116 CodeEntry* func_entry = generator.code_map()->FindEntry(code_address);
1118 CHECK_EQ(0, strcmp(func_name, func_entry->name()));
1119 const i::JITLineInfoTable* line_info = func_entry->line_info();
1121 CHECK(!line_info->empty());
1123 // Check the hit source lines using V8 Public APIs.
1124 const i::ProfileTree* tree = profile->top_down();
1125 ProfileNode* root = tree->root();
1127 ProfileNode* func_node = root->FindChild(func_entry);
1130 // Add 10 faked ticks to source line #5.
1133 for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line);
1135 unsigned int line_count = func_node->GetHitLineCount();
1136 CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5.
1137 ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count);
1138 CHECK(func_node->GetLineTicks(&entries[0], line_count));
1140 for (int i = 0; i < entries.length(); i++)
1141 if (entries[i].line == hit_line) {
1142 value = entries[i].hit_count;
1145 CHECK_EQ(hit_count, value);
1149 static const char* call_function_test_source = "function bar(iterations) {\n"
1151 "function start(duration) {\n"
1152 " var start = Date.now();\n"
1153 " while (Date.now() - start < duration) {\n"
1155 " bar.call(this, 10 * 1000);\n"
1161 // Test that if we sampled thread when it was inside FunctionCall buitin then
1162 // its caller frame will be '(unresolved function)' as we have no reliable way
1166 // 96 0 (root) [-1] #1
1167 // 1 1 (garbage collector) [-1] #4
1168 // 5 0 (unresolved function) [-1] #5
1170 // 71 70 start [-1] #3
1172 // 19 19 (program) [-1] #2
1173 TEST(FunctionCallSample) {
1175 v8::HandleScope scope(env->GetIsolate());
1177 // Collect garbage that might have be generated while installing
1179 CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags);
1181 CompileRun(call_function_test_source);
1182 v8::Local<v8::Function> function = GetFunction(*env, "start");
1184 int32_t duration_ms = 100;
1185 v8::Handle<v8::Value> args[] = {
1186 v8::Integer::New(env->GetIsolate(), duration_ms)
1188 v8::CpuProfile* profile =
1189 RunProfiler(env.local(), function, args, arraysize(args), 100);
1191 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1193 ScopedVector<v8::Handle<v8::String> > names(4);
1194 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1195 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1196 names[2] = v8_str("start");
1197 names[3] = v8_str(i::ProfileGenerator::kUnresolvedFunctionName);
1198 // Don't allow |bar| and |call| nodes to be at the top level.
1199 CheckChildrenNames(root, names);
1202 // In case of GC stress tests all samples may be in GC phase and there
1203 // won't be |start| node in the profiles.
1204 bool is_gc_stress_testing =
1205 (i::FLAG_gc_interval != -1) || i::FLAG_stress_compaction;
1206 const v8::CpuProfileNode* startNode = FindChild(root, "start");
1207 CHECK(is_gc_stress_testing || startNode);
1209 ScopedVector<v8::Handle<v8::String> > names(2);
1210 names[0] = v8_str("bar");
1211 names[1] = v8_str("call");
1212 CheckChildrenNames(startNode, names);
1215 const v8::CpuProfileNode* unresolvedNode =
1216 FindChild(root, i::ProfileGenerator::kUnresolvedFunctionName);
1217 if (unresolvedNode) {
1218 ScopedVector<v8::Handle<v8::String> > names(1);
1219 names[0] = v8_str("call");
1220 CheckChildrenNames(unresolvedNode, names);
1227 static const char* function_apply_test_source =
1228 "function bar(iterations) {\n"
1230 "function test() {\n"
1231 " bar.apply(this, [10 * 1000]);\n"
1233 "function start(duration) {\n"
1234 " var start = Date.now();\n"
1235 " while (Date.now() - start < duration) {\n"
1244 // 94 0 (root) [-1] #0 1
1245 // 2 2 (garbage collector) [-1] #0 7
1246 // 82 49 start [-1] #16 3
1247 // 1 0 (unresolved function) [-1] #0 8
1248 // 1 1 apply [-1] #0 9
1249 // 32 21 test [-1] #16 4
1250 // 2 2 bar [-1] #16 6
1251 // 9 9 apply [-1] #0 5
1252 // 10 10 (program) [-1] #0 2
1253 TEST(FunctionApplySample) {
1255 v8::HandleScope scope(env->GetIsolate());
1257 CompileRun(function_apply_test_source);
1258 v8::Local<v8::Function> function = GetFunction(*env, "start");
1260 int32_t duration_ms = 100;
1261 v8::Handle<v8::Value> args[] = {
1262 v8::Integer::New(env->GetIsolate(), duration_ms)
1265 v8::CpuProfile* profile =
1266 RunProfiler(env.local(), function, args, arraysize(args), 100);
1268 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1270 ScopedVector<v8::Handle<v8::String> > names(3);
1271 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1272 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1273 names[2] = v8_str("start");
1274 // Don't allow |test|, |bar| and |apply| nodes to be at the top level.
1275 CheckChildrenNames(root, names);
1278 const v8::CpuProfileNode* startNode = FindChild(root, "start");
1281 ScopedVector<v8::Handle<v8::String> > names(2);
1282 names[0] = v8_str("test");
1283 names[1] = v8_str(ProfileGenerator::kUnresolvedFunctionName);
1284 CheckChildrenNames(startNode, names);
1287 const v8::CpuProfileNode* testNode = FindChild(startNode, "test");
1289 ScopedVector<v8::Handle<v8::String> > names(3);
1290 names[0] = v8_str("bar");
1291 names[1] = v8_str("apply");
1292 // apply calls "get length" before invoking the function itself
1293 // and we may get hit into it.
1294 names[2] = v8_str("get length");
1295 CheckChildrenNames(testNode, names);
1298 if (const v8::CpuProfileNode* unresolvedNode =
1299 FindChild(startNode, ProfileGenerator::kUnresolvedFunctionName)) {
1300 ScopedVector<v8::Handle<v8::String> > names(1);
1301 names[0] = v8_str("apply");
1302 CheckChildrenNames(unresolvedNode, names);
1303 GetChild(unresolvedNode, "apply");
1311 static const char* cpu_profiler_deep_stack_test_source =
1312 "function foo(n) {\n"
1316 " startProfiling('my_profile');\n"
1318 "function start() {\n"
1323 // Check a deep stack
1328 // 0 start 21 #3 no reason
1329 // 0 foo 21 #4 no reason
1330 // 0 foo 21 #5 no reason
1332 // 0 foo 21 #253 no reason
1333 // 1 startProfiling 0 #254
1334 TEST(CpuProfileDeepStack) {
1335 v8::HandleScope scope(CcTest::isolate());
1336 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1337 v8::Context::Scope context_scope(env);
1339 CompileRun(cpu_profiler_deep_stack_test_source);
1340 v8::Local<v8::Function> function = GetFunction(*env, "start");
1342 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1343 v8::Local<v8::String> profile_name = v8_str("my_profile");
1344 function->Call(env->Global(), 0, NULL);
1345 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
1347 // Dump collected profile to have a better diagnostic in case of failure.
1348 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1350 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1352 ScopedVector<v8::Handle<v8::String> > names(3);
1353 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1354 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1355 names[2] = v8_str("start");
1356 CheckChildrenNames(root, names);
1359 const v8::CpuProfileNode* node = GetChild(root, "start");
1360 for (int i = 0; i < 250; ++i) {
1361 node = GetChild(node, "foo");
1364 // In theory there must be one more 'foo' and a 'startProfiling' nodes,
1365 // but due to unstable top frame extraction these might be missing.
1371 static const char* js_native_js_test_source =
1372 "function foo() {\n"
1373 " startProfiling('my_profile');\n"
1375 "function bar() {\n"
1376 " try { foo(); } catch(e) {}\n"
1378 "function start() {\n"
1380 " CallJsFunction(bar);\n"
1384 static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
1385 v8::Handle<v8::Function> function = info[0].As<v8::Function>();
1386 v8::Handle<v8::Value> argv[] = { info[1] };
1387 function->Call(info.This(), arraysize(argv), argv);
1393 // 2 2 (program) #0 2
1395 // 55 0 CallJsFunction #0 4
1398 TEST(JsNativeJsSample) {
1399 v8::HandleScope scope(CcTest::isolate());
1400 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1401 v8::Context::Scope context_scope(env);
1403 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1404 env->GetIsolate(), CallJsFunction);
1405 v8::Local<v8::Function> func = func_template->GetFunction();
1406 func->SetName(v8_str("CallJsFunction"));
1407 env->Global()->Set(v8_str("CallJsFunction"), func);
1409 CompileRun(js_native_js_test_source);
1410 v8::Local<v8::Function> function = GetFunction(*env, "start");
1412 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1414 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1416 ScopedVector<v8::Handle<v8::String> > names(3);
1417 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1418 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1419 names[2] = v8_str("start");
1420 CheckChildrenNames(root, names);
1423 const v8::CpuProfileNode* startNode = GetChild(root, "start");
1424 CHECK_EQ(1, startNode->GetChildrenCount());
1425 const v8::CpuProfileNode* nativeFunctionNode =
1426 GetChild(startNode, "CallJsFunction");
1428 CHECK_EQ(1, nativeFunctionNode->GetChildrenCount());
1429 const v8::CpuProfileNode* barNode = GetChild(nativeFunctionNode, "bar");
1431 CHECK_EQ(1, barNode->GetChildrenCount());
1432 GetChild(barNode, "foo");
1438 static const char* js_native_js_runtime_js_test_source =
1439 "function foo() {\n"
1440 " startProfiling('my_profile');\n"
1442 "var bound = foo.bind(this);\n"
1443 "function bar() {\n"
1444 " try { bound(); } catch(e) {}\n"
1446 "function start() {\n"
1448 " CallJsFunction(bar);\n"
1456 // 54 0 CallJsFunction #0 4
1459 // 2 2 (program) #0 2
1460 TEST(JsNativeJsRuntimeJsSample) {
1461 v8::HandleScope scope(CcTest::isolate());
1462 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1463 v8::Context::Scope context_scope(env);
1465 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1466 env->GetIsolate(), CallJsFunction);
1467 v8::Local<v8::Function> func = func_template->GetFunction();
1468 func->SetName(v8_str("CallJsFunction"));
1469 env->Global()->Set(v8_str("CallJsFunction"), func);
1471 CompileRun(js_native_js_runtime_js_test_source);
1472 v8::Local<v8::Function> function = GetFunction(*env, "start");
1474 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1476 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1477 ScopedVector<v8::Handle<v8::String> > names(3);
1478 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1479 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1480 names[2] = v8_str("start");
1481 CheckChildrenNames(root, names);
1483 const v8::CpuProfileNode* startNode = GetChild(root, "start");
1484 CHECK_EQ(1, startNode->GetChildrenCount());
1485 const v8::CpuProfileNode* nativeFunctionNode =
1486 GetChild(startNode, "CallJsFunction");
1488 CHECK_EQ(1, nativeFunctionNode->GetChildrenCount());
1489 const v8::CpuProfileNode* barNode = GetChild(nativeFunctionNode, "bar");
1491 // The child is in fact a bound foo.
1492 // A bound function has a wrapper that may make calls to
1493 // other functions e.g. "get length".
1494 CHECK_LE(1, barNode->GetChildrenCount());
1495 CHECK_GE(2, barNode->GetChildrenCount());
1496 GetChild(barNode, "foo");
1502 static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) {
1503 v8::base::OS::Print("In CallJsFunction2\n");
1504 CallJsFunction(info);
1508 static const char* js_native1_js_native2_js_test_source =
1509 "function foo() {\n"
1511 " startProfiling('my_profile');\n"
1514 "function bar() {\n"
1515 " CallJsFunction2(foo);\n"
1517 "function start() {\n"
1519 " CallJsFunction1(bar);\n"
1527 // 54 0 CallJsFunction1 #0 4
1529 // 54 0 CallJsFunction2 #0 6
1531 // 2 2 (program) #0 2
1532 TEST(JsNative1JsNative2JsSample) {
1533 v8::HandleScope scope(CcTest::isolate());
1534 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1535 v8::Context::Scope context_scope(env);
1537 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1538 env->GetIsolate(), CallJsFunction);
1539 v8::Local<v8::Function> func1 = func_template->GetFunction();
1540 func1->SetName(v8_str("CallJsFunction1"));
1541 env->Global()->Set(v8_str("CallJsFunction1"), func1);
1543 v8::Local<v8::Function> func2 = v8::FunctionTemplate::New(
1544 env->GetIsolate(), CallJsFunction2)->GetFunction();
1545 func2->SetName(v8_str("CallJsFunction2"));
1546 env->Global()->Set(v8_str("CallJsFunction2"), func2);
1548 CompileRun(js_native1_js_native2_js_test_source);
1549 v8::Local<v8::Function> function = GetFunction(*env, "start");
1551 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1553 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1554 ScopedVector<v8::Handle<v8::String> > names(3);
1555 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1556 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1557 names[2] = v8_str("start");
1558 CheckChildrenNames(root, names);
1560 const v8::CpuProfileNode* startNode = GetChild(root, "start");
1561 CHECK_EQ(1, startNode->GetChildrenCount());
1562 const v8::CpuProfileNode* nativeNode1 =
1563 GetChild(startNode, "CallJsFunction1");
1565 CHECK_EQ(1, nativeNode1->GetChildrenCount());
1566 const v8::CpuProfileNode* barNode = GetChild(nativeNode1, "bar");
1568 CHECK_EQ(1, barNode->GetChildrenCount());
1569 const v8::CpuProfileNode* nativeNode2 = GetChild(barNode, "CallJsFunction2");
1571 CHECK_EQ(1, nativeNode2->GetChildrenCount());
1572 GetChild(nativeNode2, "foo");
1580 // 3 3 (program) #0 2
1584 v8::HandleScope scope(env->GetIsolate());
1585 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1587 v8::Local<v8::String> profile_name = v8_str("my_profile");
1588 cpu_profiler->StartProfiling(profile_name);
1590 i::Isolate* isolate = CcTest::i_isolate();
1591 i::ProfilerEventsProcessor* processor = isolate->cpu_profiler()->processor();
1592 processor->AddCurrentStack(isolate);
1594 cpu_profiler->SetIdle(true);
1596 for (int i = 0; i < 3; i++) {
1597 processor->AddCurrentStack(isolate);
1600 cpu_profiler->SetIdle(false);
1601 processor->AddCurrentStack(isolate);
1604 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
1606 // Dump collected profile to have a better diagnostic in case of failure.
1607 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1609 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1610 ScopedVector<v8::Handle<v8::String> > names(3);
1611 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1612 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1613 names[2] = v8_str(ProfileGenerator::kIdleEntryName);
1614 CheckChildrenNames(root, names);
1616 const v8::CpuProfileNode* programNode =
1617 GetChild(root, ProfileGenerator::kProgramEntryName);
1618 CHECK_EQ(0, programNode->GetChildrenCount());
1619 CHECK_GE(programNode->GetHitCount(), 3u);
1621 const v8::CpuProfileNode* idleNode =
1622 GetChild(root, ProfileGenerator::kIdleEntryName);
1623 CHECK_EQ(0, idleNode->GetChildrenCount());
1624 CHECK_GE(idleNode->GetHitCount(), 3u);
1630 static 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) {
1634 CHECK(v8_str(name)->Equals(node->GetFunctionName()));
1635 CHECK(v8_str(script_name)->Equals(node->GetScriptResourceName()));
1636 CHECK_EQ(script_id, node->GetScriptId());
1637 CHECK_EQ(line, node->GetLineNumber());
1638 CHECK_EQ(column, node->GetColumnNumber());
1642 TEST(FunctionDetails) {
1643 v8::HandleScope scope(CcTest::isolate());
1644 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1645 v8::Context::Scope context_scope(env);
1647 v8::Handle<v8::Script> script_a = CompileWithOrigin(
1648 " function foo\n() { try { bar(); } catch(e) {} }\n"
1649 " function bar() { startProfiling(); }\n",
1652 v8::Handle<v8::Script> script_b = CompileWithOrigin(
1653 "\n\n function baz() { try { foo(); } catch(e) {} }\n"
1655 "stopProfiling();\n",
1658 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
1659 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
1660 reinterpret_cast<ProfileNode*>(
1661 const_cast<v8::CpuProfileNode*>(current))->Print(0);
1662 // The tree should look like this:
1664 // 0 "" 19 #2 no reason script_b:1
1665 // 0 baz 19 #3 TryCatchStatement script_b:3
1666 // 0 foo 18 #4 TryCatchStatement script_a:2
1667 // 1 bar 18 #5 no reason script_a:3
1668 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1669 const v8::CpuProfileNode* script = GetChild(root, "");
1670 CheckFunctionDetails(env->GetIsolate(), script, "", "script_b",
1671 script_b->GetUnboundScript()->GetId(), 1, 1);
1672 const v8::CpuProfileNode* baz = GetChild(script, "baz");
1673 CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b",
1674 script_b->GetUnboundScript()->GetId(), 3, 16);
1675 const v8::CpuProfileNode* foo = GetChild(baz, "foo");
1676 CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a",
1677 script_a->GetUnboundScript()->GetId(), 2, 1);
1678 const v8::CpuProfileNode* bar = GetChild(foo, "bar");
1679 CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a",
1680 script_a->GetUnboundScript()->GetId(), 3, 14);
1684 TEST(DontStopOnFinishedProfileDelete) {
1685 v8::HandleScope scope(CcTest::isolate());
1686 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1687 v8::Context::Scope context_scope(env);
1689 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
1690 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1692 CHECK_EQ(0, iprofiler->GetProfilesCount());
1693 v8::Handle<v8::String> outer = v8_str("outer");
1694 profiler->StartProfiling(outer);
1695 CHECK_EQ(0, iprofiler->GetProfilesCount());
1697 v8::Handle<v8::String> inner = v8_str("inner");
1698 profiler->StartProfiling(inner);
1699 CHECK_EQ(0, iprofiler->GetProfilesCount());
1701 v8::CpuProfile* inner_profile = profiler->StopProfiling(inner);
1702 CHECK(inner_profile);
1703 CHECK_EQ(1, iprofiler->GetProfilesCount());
1704 inner_profile->Delete();
1705 inner_profile = NULL;
1706 CHECK_EQ(0, iprofiler->GetProfilesCount());
1708 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer);
1709 CHECK(outer_profile);
1710 CHECK_EQ(1, iprofiler->GetProfilesCount());
1711 outer_profile->Delete();
1712 outer_profile = NULL;
1713 CHECK_EQ(0, iprofiler->GetProfilesCount());
1717 const char* GetBranchDeoptReason(i::CpuProfile* iprofile, const char* branch[],
1719 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1720 const ProfileNode* iopt_function = NULL;
1721 iopt_function = GetSimpleBranch(profile, branch, length);
1722 CHECK_EQ(1, iopt_function->deopt_infos().size());
1723 return iopt_function->deopt_infos()[0].deopt_reason;
1727 // deopt at top function
1728 TEST(CollectDeoptEvents) {
1729 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1730 i::FLAG_allow_natives_syntax = true;
1731 v8::HandleScope scope(CcTest::isolate());
1732 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1733 v8::Context::Scope context_scope(env);
1734 v8::Isolate* isolate = env->GetIsolate();
1735 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1736 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1738 const char opt_source[] =
1739 "function opt_function%d(value, depth) {\n"
1740 " if (depth) return opt_function%d(value, depth - 1);\n"
1742 " return 10 / value;\n"
1746 for (int i = 0; i < 3; ++i) {
1747 i::EmbeddedVector<char, sizeof(opt_source) + 100> buffer;
1748 i::SNPrintF(buffer, opt_source, i, i);
1749 v8::Script::Compile(v8_str(buffer.start()))->Run();
1752 const char* source =
1753 "startProfiling();\n"
1755 "opt_function0(1, 1);\n"
1757 "%OptimizeFunctionOnNextCall(opt_function0)\n"
1759 "opt_function0(1, 1);\n"
1761 "opt_function0(undefined, 1);\n"
1763 "opt_function1(1, 1);\n"
1765 "%OptimizeFunctionOnNextCall(opt_function1)\n"
1767 "opt_function1(1, 1);\n"
1769 "opt_function1(NaN, 1);\n"
1771 "opt_function2(1, 1);\n"
1773 "%OptimizeFunctionOnNextCall(opt_function2)\n"
1775 "opt_function2(1, 1);\n"
1777 "opt_function2(0, 1);\n"
1779 "stopProfiling();\n"
1782 v8::Script::Compile(v8_str(source))->Run();
1783 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1785 /* The expected profile
1789 1 opt_function2 31 #7
1790 1 opt_function2 31 #8
1791 ;;; deopted at script_id: 31 position: 106 with reason
1793 2 opt_function0 29 #3
1794 4 opt_function0 29 #4
1795 ;;; deopted at script_id: 29 position: 108 with reason 'not a
1797 0 opt_function1 30 #5
1798 1 opt_function1 30 #6
1799 ;;; deopted at script_id: 30 position: 108 with reason 'lost
1804 const char* branch[] = {"", "opt_function0", "opt_function0"};
1805 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber),
1806 GetBranchDeoptReason(iprofile, branch, arraysize(branch)));
1809 const char* branch[] = {"", "opt_function1", "opt_function1"};
1810 const char* deopt_reason =
1811 GetBranchDeoptReason(iprofile, branch, arraysize(branch));
1812 if (deopt_reason != reason(i::Deoptimizer::kNaN) &&
1813 deopt_reason != reason(i::Deoptimizer::kLostPrecisionOrNaN)) {
1814 FATAL(deopt_reason);
1818 const char* branch[] = {"", "opt_function2", "opt_function2"};
1819 CHECK_EQ(reason(i::Deoptimizer::kDivisionByZero),
1820 GetBranchDeoptReason(iprofile, branch, arraysize(branch)));
1822 iprofiler->DeleteProfile(iprofile);
1826 TEST(SourceLocation) {
1827 i::FLAG_always_opt = true;
1828 i::FLAG_hydrogen_track_positions = true;
1830 v8::HandleScope scope(CcTest::isolate());
1832 const char* source =
1833 "function CompareStatementWithThis() {\n"
1834 " if (this === 1) {}\n"
1836 "CompareStatementWithThis();\n";
1838 v8::Script::Compile(v8_str(source))->Run();
1842 static const char* inlined_source =
1843 "function opt_function(left, right) { var k = left / 10; var r = 10 / "
1844 "right; return k + r; }\n";
1845 // 0.........1.........2.........3.........4....*....5.........6......*..7
1848 // deopt at the first level inlined function
1849 TEST(DeoptAtFirstLevelInlinedSource) {
1850 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1851 i::FLAG_allow_natives_syntax = true;
1852 v8::HandleScope scope(CcTest::isolate());
1853 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1854 v8::Context::Scope context_scope(env);
1855 v8::Isolate* isolate = env->GetIsolate();
1856 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1857 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1859 // 0.........1.........2.........3.........4.........5.........6.........7
1860 const char* source =
1861 "function test(left, right) { return opt_function(left, right); }\n"
1863 "startProfiling();\n"
1867 "%OptimizeFunctionOnNextCall(test)\n"
1871 "test(undefined, 10);\n"
1873 "stopProfiling();\n"
1876 v8::Handle<v8::Script> inlined_script = v8_compile(inlined_source);
1877 inlined_script->Run();
1878 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1880 v8::Handle<v8::Script> script = v8_compile(source);
1882 int script_id = script->GetUnboundScript()->GetId();
1884 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1886 /* The expected profile output
1891 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1893 ;;; Inline point: script_id 30 position: 36.
1894 4 opt_function 29 #4
1896 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1898 const char* branch[] = {"", "test"};
1899 const ProfileNode* itest_node =
1900 GetSimpleBranch(profile, branch, arraysize(branch));
1901 const std::vector<i::DeoptInfo>& deopt_infos = itest_node->deopt_infos();
1902 CHECK_EQ(1, deopt_infos.size());
1904 const i::DeoptInfo& info = deopt_infos[0];
1905 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1906 CHECK_EQ(2, info.stack.size());
1907 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1908 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1909 CHECK_EQ(script_id, info.stack[1].script_id);
1910 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1912 iprofiler->DeleteProfile(iprofile);
1916 // deopt at the second level inlined function
1917 TEST(DeoptAtSecondLevelInlinedSource) {
1918 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1919 i::FLAG_allow_natives_syntax = true;
1920 v8::HandleScope scope(CcTest::isolate());
1921 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1922 v8::Context::Scope context_scope(env);
1923 v8::Isolate* isolate = env->GetIsolate();
1924 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1925 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1927 // 0.........1.........2.........3.........4.........5.........6.........7
1928 const char* source =
1929 "function test2(left, right) { return opt_function(left, right); }\n"
1930 "function test1(left, right) { return test2(left, right); }\n"
1932 "startProfiling();\n"
1936 "%OptimizeFunctionOnNextCall(test1)\n"
1940 "test1(undefined, 10);\n"
1942 "stopProfiling();\n"
1945 v8::Handle<v8::Script> inlined_script = v8_compile(inlined_source);
1946 inlined_script->Run();
1947 int inlined_script_id = inlined_script->GetUnboundScript()->GetId();
1949 v8::Handle<v8::Script> script = v8_compile(source);
1951 int script_id = script->GetUnboundScript()->GetId();
1953 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1955 /* The expected profile output
1960 ;;; deopted at script_id: 29 position: 45 with reason 'not a
1962 ;;; Inline point: script_id 30 position: 37.
1963 ;;; Inline point: script_id 30 position: 103.
1965 3 opt_function 29 #5
1968 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1970 const char* branch[] = {"", "test1"};
1971 const ProfileNode* itest_node =
1972 GetSimpleBranch(profile, branch, arraysize(branch));
1973 const std::vector<i::DeoptInfo>& deopt_infos = itest_node->deopt_infos();
1974 CHECK_EQ(1, deopt_infos.size());
1976 const i::DeoptInfo info = deopt_infos[0];
1977 CHECK_EQ(reason(i::Deoptimizer::kNotAHeapNumber), info.deopt_reason);
1978 CHECK_EQ(3, info.stack.size());
1979 CHECK_EQ(inlined_script_id, info.stack[0].script_id);
1980 CHECK_EQ(offset(inlined_source, "left /"), info.stack[0].position);
1981 CHECK_EQ(script_id, info.stack[1].script_id);
1982 CHECK_EQ(offset(source, "opt_function(left,"), info.stack[1].position);
1983 CHECK_EQ(offset(source, "test2(left, right);"), info.stack[2].position);
1985 iprofiler->DeleteProfile(iprofile);
1989 // deopt in untracked function
1990 TEST(DeoptUntrackedFunction) {
1991 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1992 i::FLAG_allow_natives_syntax = true;
1993 v8::HandleScope scope(CcTest::isolate());
1994 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1995 v8::Context::Scope context_scope(env);
1996 v8::Isolate* isolate = env->GetIsolate();
1997 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1998 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
2000 // 0.........1.........2.........3.........4.........5.........6.........7
2001 const char* source =
2002 "function test(left, right) { return opt_function(left, right); }\n"
2006 "%OptimizeFunctionOnNextCall(test)\n"
2010 "startProfiling();\n" // profiler started after compilation.
2012 "test(undefined, 10);\n"
2014 "stopProfiling();\n"
2017 v8::Handle<v8::Script> inlined_script = v8_compile(inlined_source);
2018 inlined_script->Run();
2020 v8::Handle<v8::Script> script = v8_compile(source);
2023 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
2025 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
2027 const char* branch[] = {"", "test"};
2028 const ProfileNode* itest_node =
2029 GetSimpleBranch(profile, branch, arraysize(branch));
2030 CHECK_EQ(0, itest_node->deopt_infos().size());
2032 iprofiler->DeleteProfile(iprofile);