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)));
60 i::Isolate* isolate = CcTest::i_isolate();
61 CpuProfilesCollection profiles(isolate->heap());
62 ProfileGenerator generator(&profiles);
63 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
64 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
66 processor->StopSynchronously();
70 static void EnqueueTickSampleEvent(ProfilerEventsProcessor* proc,
72 i::Address frame2 = NULL,
73 i::Address frame3 = NULL) {
74 i::TickSample* sample = proc->StartTickSample();
77 sample->frames_count = 0;
79 sample->stack[0] = frame2;
80 sample->frames_count = 1;
83 sample->stack[1] = frame3;
84 sample->frames_count = 2;
86 proc->FinishTickSample();
94 : old_flag_prof_browser_mode_(i::FLAG_prof_browser_mode) {
95 i::FLAG_prof_browser_mode = false;
99 i::FLAG_prof_browser_mode = old_flag_prof_browser_mode_;
103 bool old_flag_prof_browser_mode_;
109 i::Code* CreateCode(LocalContext* env) {
110 static int counter = 0;
111 i::EmbeddedVector<char, 256> script;
112 i::EmbeddedVector<char, 32> name;
114 i::SNPrintF(name, "function_%d", ++counter);
115 const char* name_start = name.start();
119 "for (var i = 0; i < %d; ++i) counter += i;\n"
120 "return '%s_' + counter;\n"
122 "%s();\n", name_start, counter, name_start, name_start);
123 CompileRun(script.start());
125 i::Handle<i::JSFunction> fun =
126 v8::Utils::OpenHandle(*GetFunction(**env, name_start));
132 CcTest::InitializeVM();
134 i::Isolate* isolate = CcTest::i_isolate();
135 i::Factory* factory = isolate->factory();
136 TestSetup test_setup;
138 i::HandleScope scope(isolate);
140 i::Code* aaa_code = CreateCode(&env);
141 i::Code* comment_code = CreateCode(&env);
142 i::Code* args5_code = CreateCode(&env);
143 i::Code* comment2_code = CreateCode(&env);
144 i::Code* moved_code = CreateCode(&env);
145 i::Code* args3_code = CreateCode(&env);
146 i::Code* args4_code = CreateCode(&env);
148 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
149 profiles->StartProfiling("", false);
150 ProfileGenerator generator(profiles);
151 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
152 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
154 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
156 // Enqueue code creation events.
157 const char* aaa_str = "aaa";
158 i::Handle<i::String> aaa_name = factory->NewStringFromAsciiChecked(aaa_str);
159 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, aaa_code, *aaa_name);
160 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment_code, "comment");
161 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args5_code, 5);
162 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, comment2_code, "comment2");
163 profiler.CodeMoveEvent(comment2_code->address(), moved_code->address());
164 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args3_code, 3);
165 profiler.CodeCreateEvent(i::Logger::STUB_TAG, args4_code, 4);
167 // Enqueue a tick event to enable code events processing.
168 EnqueueTickSampleEvent(processor.get(), aaa_code->address());
170 processor->StopSynchronously();
172 // Check the state of profile generator.
173 CodeEntry* aaa = generator.code_map()->FindEntry(aaa_code->address());
175 CHECK_EQ(0, strcmp(aaa_str, aaa->name()));
177 CodeEntry* comment = generator.code_map()->FindEntry(comment_code->address());
179 CHECK_EQ(0, strcmp("comment", comment->name()));
181 CodeEntry* args5 = generator.code_map()->FindEntry(args5_code->address());
183 CHECK_EQ(0, strcmp("5", args5->name()));
185 CHECK(!generator.code_map()->FindEntry(comment2_code->address()));
187 CodeEntry* comment2 = generator.code_map()->FindEntry(moved_code->address());
189 CHECK_EQ(0, strcmp("comment2", comment2->name()));
194 static int CompareProfileNodes(const T* p1, const T* p2) {
195 return strcmp((*p1)->entry()->name(), (*p2)->entry()->name());
200 TestSetup test_setup;
202 i::Isolate* isolate = CcTest::i_isolate();
203 i::HandleScope scope(isolate);
205 i::Code* frame1_code = CreateCode(&env);
206 i::Code* frame2_code = CreateCode(&env);
207 i::Code* frame3_code = CreateCode(&env);
209 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
210 profiles->StartProfiling("", false);
211 ProfileGenerator generator(profiles);
212 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
213 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
215 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
217 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame1_code, "bbb");
218 profiler.CodeCreateEvent(i::Logger::STUB_TAG, frame2_code, 5);
219 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, frame3_code, "ddd");
221 EnqueueTickSampleEvent(processor.get(), frame1_code->instruction_start());
222 EnqueueTickSampleEvent(
224 frame2_code->instruction_start() + frame2_code->ExecutableSize() / 2,
225 frame1_code->instruction_start() + frame2_code->ExecutableSize() / 2);
226 EnqueueTickSampleEvent(
228 frame3_code->instruction_end() - 1,
229 frame2_code->instruction_end() - 1,
230 frame1_code->instruction_end() - 1);
232 processor->StopSynchronously();
233 CpuProfile* profile = profiles->StopProfiling("");
237 const i::List<ProfileNode*>* top_down_root_children =
238 profile->top_down()->root()->children();
239 CHECK_EQ(1, top_down_root_children->length());
240 CHECK_EQ(0, strcmp("bbb", top_down_root_children->last()->entry()->name()));
241 const i::List<ProfileNode*>* top_down_bbb_children =
242 top_down_root_children->last()->children();
243 CHECK_EQ(1, top_down_bbb_children->length());
244 CHECK_EQ(0, strcmp("5", top_down_bbb_children->last()->entry()->name()));
245 const i::List<ProfileNode*>* top_down_stub_children =
246 top_down_bbb_children->last()->children();
247 CHECK_EQ(1, top_down_stub_children->length());
248 CHECK_EQ(0, strcmp("ddd", top_down_stub_children->last()->entry()->name()));
249 const i::List<ProfileNode*>* top_down_ddd_children =
250 top_down_stub_children->last()->children();
251 CHECK_EQ(0, top_down_ddd_children->length());
255 // http://crbug/51594
256 // This test must not crash.
257 TEST(CrashIfStoppingLastNonExistentProfile) {
258 CcTest::InitializeVM();
259 TestSetup test_setup;
260 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
261 profiler->StartProfiling("1");
262 profiler->StopProfiling("2");
263 profiler->StartProfiling("1");
264 profiler->StopProfiling("");
268 // http://code.google.com/p/v8/issues/detail?id=1398
269 // Long stacks (exceeding max frames limit) must not be erased.
271 TestSetup test_setup;
273 i::Isolate* isolate = CcTest::i_isolate();
274 i::HandleScope scope(isolate);
276 i::Code* code = CreateCode(&env);
278 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
279 profiles->StartProfiling("", false);
280 ProfileGenerator generator(profiles);
281 SmartPointer<ProfilerEventsProcessor> processor(new ProfilerEventsProcessor(
282 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100)));
284 CpuProfiler profiler(isolate, profiles, &generator, processor.get());
286 profiler.CodeCreateEvent(i::Logger::BUILTIN_TAG, code, "bbb");
288 i::TickSample* sample = processor->StartTickSample();
289 sample->pc = code->address();
291 sample->frames_count = i::TickSample::kMaxFramesCount;
292 for (unsigned i = 0; i < sample->frames_count; ++i) {
293 sample->stack[i] = code->address();
295 processor->FinishTickSample();
297 processor->StopSynchronously();
298 CpuProfile* profile = profiles->StopProfiling("");
301 unsigned actual_depth = 0;
302 const ProfileNode* node = profile->top_down()->root();
303 while (node->children()->length() > 0) {
304 node = node->children()->last();
308 CHECK_EQ(1 + i::TickSample::kMaxFramesCount, actual_depth); // +1 for PC.
312 TEST(DeleteAllCpuProfiles) {
313 CcTest::InitializeVM();
314 TestSetup test_setup;
315 CpuProfiler* profiler = CcTest::i_isolate()->cpu_profiler();
316 CHECK_EQ(0, profiler->GetProfilesCount());
317 profiler->DeleteAllProfiles();
318 CHECK_EQ(0, profiler->GetProfilesCount());
320 profiler->StartProfiling("1");
321 profiler->StopProfiling("1");
322 CHECK_EQ(1, profiler->GetProfilesCount());
323 profiler->DeleteAllProfiles();
324 CHECK_EQ(0, profiler->GetProfilesCount());
325 profiler->StartProfiling("1");
326 profiler->StartProfiling("2");
327 profiler->StopProfiling("2");
328 profiler->StopProfiling("1");
329 CHECK_EQ(2, profiler->GetProfilesCount());
330 profiler->DeleteAllProfiles();
331 CHECK_EQ(0, profiler->GetProfilesCount());
333 // Test profiling cancellation by the 'delete' command.
334 profiler->StartProfiling("1");
335 profiler->StartProfiling("2");
336 CHECK_EQ(0, profiler->GetProfilesCount());
337 profiler->DeleteAllProfiles();
338 CHECK_EQ(0, profiler->GetProfilesCount());
342 static bool FindCpuProfile(v8::CpuProfiler* v8profiler,
343 const v8::CpuProfile* v8profile) {
344 i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(v8profiler);
345 const i::CpuProfile* profile =
346 reinterpret_cast<const i::CpuProfile*>(v8profile);
347 int length = profiler->GetProfilesCount();
348 for (int i = 0; i < length; i++) {
349 if (profile == profiler->GetProfile(i))
356 TEST(DeleteCpuProfile) {
358 v8::HandleScope scope(env->GetIsolate());
359 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
360 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(cpu_profiler);
362 CHECK_EQ(0, iprofiler->GetProfilesCount());
363 v8::Local<v8::String> name1 = v8_str("1");
364 cpu_profiler->StartProfiling(name1);
365 v8::CpuProfile* p1 = cpu_profiler->StopProfiling(name1);
367 CHECK_EQ(1, iprofiler->GetProfilesCount());
368 CHECK(FindCpuProfile(cpu_profiler, p1));
370 CHECK_EQ(0, iprofiler->GetProfilesCount());
372 v8::Local<v8::String> name2 = v8_str("2");
373 cpu_profiler->StartProfiling(name2);
374 v8::CpuProfile* p2 = cpu_profiler->StopProfiling(name2);
376 CHECK_EQ(1, iprofiler->GetProfilesCount());
377 CHECK(FindCpuProfile(cpu_profiler, p2));
378 v8::Local<v8::String> name3 = v8_str("3");
379 cpu_profiler->StartProfiling(name3);
380 v8::CpuProfile* p3 = cpu_profiler->StopProfiling(name3);
382 CHECK_EQ(2, iprofiler->GetProfilesCount());
384 CHECK(FindCpuProfile(cpu_profiler, p3));
385 CHECK(FindCpuProfile(cpu_profiler, p2));
387 CHECK_EQ(1, iprofiler->GetProfilesCount());
388 CHECK(!FindCpuProfile(cpu_profiler, p2));
389 CHECK(FindCpuProfile(cpu_profiler, p3));
391 CHECK_EQ(0, iprofiler->GetProfilesCount());
395 TEST(ProfileStartEndTime) {
397 v8::HandleScope scope(env->GetIsolate());
398 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
400 v8::Local<v8::String> profile_name = v8_str("test");
401 cpu_profiler->StartProfiling(profile_name);
402 const v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
403 CHECK(profile->GetStartTime() <= profile->GetEndTime());
407 static v8::CpuProfile* RunProfiler(
408 v8::Handle<v8::Context> env, v8::Handle<v8::Function> function,
409 v8::Handle<v8::Value> argv[], int argc,
410 unsigned min_js_samples, bool collect_samples = false) {
411 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
412 v8::Local<v8::String> profile_name = v8_str("my_profile");
414 cpu_profiler->StartProfiling(profile_name, collect_samples);
416 i::Sampler* sampler =
417 reinterpret_cast<i::Isolate*>(env->GetIsolate())->logger()->sampler();
418 sampler->StartCountingSamples();
420 function->Call(env->Global(), argc, argv);
421 } while (sampler->js_and_external_sample_count() < min_js_samples);
423 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
426 // Dump collected profile to have a better diagnostic in case of failure.
427 reinterpret_cast<i::CpuProfile*>(profile)->Print();
433 static bool ContainsString(v8::Handle<v8::String> string,
434 const Vector<v8::Handle<v8::String> >& vector) {
435 for (int i = 0; i < vector.length(); i++) {
436 if (string->Equals(vector[i]))
443 static void CheckChildrenNames(const v8::CpuProfileNode* node,
444 const Vector<v8::Handle<v8::String> >& names) {
445 int count = node->GetChildrenCount();
446 for (int i = 0; i < count; i++) {
447 v8::Handle<v8::String> name = node->GetChild(i)->GetFunctionName();
448 CHECK(ContainsString(name, names));
449 // Check that there are no duplicates.
450 for (int j = 0; j < count; j++) {
451 if (j == i) continue;
452 CHECK(!name->Equals(node->GetChild(j)->GetFunctionName()));
458 static const v8::CpuProfileNode* FindChild(v8::Isolate* isolate,
459 const v8::CpuProfileNode* node,
461 int count = node->GetChildrenCount();
462 v8::Handle<v8::String> nameHandle = v8_str(name);
463 for (int i = 0; i < count; i++) {
464 const v8::CpuProfileNode* child = node->GetChild(i);
465 if (nameHandle->Equals(child->GetFunctionName())) return child;
471 static const v8::CpuProfileNode* GetChild(v8::Isolate* isolate,
472 const v8::CpuProfileNode* node,
474 const v8::CpuProfileNode* result = FindChild(isolate, node, name);
477 i::SNPrintF(Vector<char>(buffer, arraysize(buffer)),
478 "Failed to GetChild: %s", name);
485 static void CheckSimpleBranch(v8::Isolate* isolate,
486 const v8::CpuProfileNode* node,
487 const char* names[], int length) {
488 for (int i = 0; i < length; i++) {
489 const char* name = names[i];
490 node = GetChild(isolate, node, name);
491 int expectedChildrenCount = (i == length - 1) ? 0 : 1;
492 CHECK_EQ(expectedChildrenCount, node->GetChildrenCount());
497 static const v8::CpuProfileNode* GetSimpleBranch(v8::Isolate* isolate,
498 const v8::CpuProfileNode* node,
501 for (int i = 0; i < length; i++) {
502 node = GetChild(isolate, node, names[i]);
508 static const char* cpu_profiler_test_source = "function loop(timeout) {\n"
510 " var start = Date.now();\n"
511 " while (Date.now() - start < timeout) {\n"
512 " var n = 100*1000;\n"
515 " this.mmm += n * n * n;\n"
519 "function delay() { try { loop(10); } catch(e) { } }\n"
520 "function bar() { delay(); }\n"
521 "function baz() { delay(); }\n"
530 "function start(timeout) {\n"
531 " var start = Date.now();\n"
534 " var duration = Date.now() - start;\n"
535 " } while (duration < timeout);\n"
536 " return duration;\n"
540 // Check that the profile tree for the script above will look like the
544 // 1062 0 (root) [-1]
555 // 2 2 (program) [-1]
556 // 6 6 (garbage collector) [-1]
557 TEST(CollectCpuProfile) {
559 v8::HandleScope scope(env->GetIsolate());
561 CompileRun(cpu_profiler_test_source);
562 v8::Local<v8::Function> function = GetFunction(*env, "start");
564 int32_t profiling_interval_ms = 200;
565 v8::Handle<v8::Value> args[] = {
566 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)
568 v8::CpuProfile* profile =
569 RunProfiler(env.local(), function, args, arraysize(args), 200);
570 function->Call(env->Global(), arraysize(args), args);
572 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
574 ScopedVector<v8::Handle<v8::String> > names(3);
575 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
576 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
577 names[2] = v8_str("start");
578 CheckChildrenNames(root, names);
580 const v8::CpuProfileNode* startNode =
581 GetChild(env->GetIsolate(), root, "start");
582 CHECK_EQ(1, startNode->GetChildrenCount());
584 const v8::CpuProfileNode* fooNode =
585 GetChild(env->GetIsolate(), startNode, "foo");
586 CHECK_EQ(3, fooNode->GetChildrenCount());
588 const char* barBranch[] = { "bar", "delay", "loop" };
589 CheckSimpleBranch(env->GetIsolate(), fooNode, barBranch,
590 arraysize(barBranch));
591 const char* bazBranch[] = { "baz", "delay", "loop" };
592 CheckSimpleBranch(env->GetIsolate(), fooNode, bazBranch,
593 arraysize(bazBranch));
594 const char* delayBranch[] = { "delay", "loop" };
595 CheckSimpleBranch(env->GetIsolate(), fooNode, delayBranch,
596 arraysize(delayBranch));
602 static const char* hot_deopt_no_frame_entry_test_source =
603 "function foo(a, b) {\n"
608 "function start(timeout) {\n"
609 " var start = Date.now();\n"
611 " for (var i = 1; i < 1000; ++i) foo(1, i);\n"
612 " var duration = Date.now() - start;\n"
613 " } while (duration < timeout);\n"
614 " return duration;\n"
617 // Check that the profile tree for the script above will look like the
621 // 1062 0 (root) [-1]
624 // 2 2 (program) [-1]
625 // 6 6 (garbage collector) [-1]
627 // The test checks no FP ranges are present in a deoptimized funcion.
628 // If 'foo' has no ranges the samples falling into the prologue will miss the
629 // 'start' function on the stack, so 'foo' will be attached to the (root).
630 TEST(HotDeoptNoFrameEntry) {
632 v8::HandleScope scope(env->GetIsolate());
634 CompileRun(hot_deopt_no_frame_entry_test_source);
635 v8::Local<v8::Function> function = GetFunction(*env, "start");
637 int32_t profiling_interval_ms = 200;
638 v8::Handle<v8::Value> args[] = {
639 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)
641 v8::CpuProfile* profile =
642 RunProfiler(env.local(), function, args, arraysize(args), 200);
643 function->Call(env->Global(), arraysize(args), args);
645 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
647 ScopedVector<v8::Handle<v8::String> > names(3);
648 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
649 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
650 names[2] = v8_str("start");
651 CheckChildrenNames(root, names);
653 const v8::CpuProfileNode* startNode =
654 GetChild(env->GetIsolate(), root, "start");
655 CHECK_EQ(1, startNode->GetChildrenCount());
657 GetChild(env->GetIsolate(), startNode, "foo");
663 TEST(CollectCpuProfileSamples) {
665 v8::HandleScope scope(env->GetIsolate());
667 CompileRun(cpu_profiler_test_source);
668 v8::Local<v8::Function> function = GetFunction(*env, "start");
670 int32_t profiling_interval_ms = 200;
671 v8::Handle<v8::Value> args[] = {
672 v8::Integer::New(env->GetIsolate(), profiling_interval_ms)
674 v8::CpuProfile* profile =
675 RunProfiler(env.local(), function, args, arraysize(args), 200, true);
677 CHECK_LE(200, profile->GetSamplesCount());
678 uint64_t end_time = profile->GetEndTime();
679 uint64_t current_time = profile->GetStartTime();
680 CHECK_LE(current_time, end_time);
681 for (int i = 0; i < profile->GetSamplesCount(); i++) {
682 CHECK(profile->GetSample(i));
683 uint64_t timestamp = profile->GetSampleTimestamp(i);
684 CHECK_LE(current_time, timestamp);
685 CHECK_LE(timestamp, end_time);
686 current_time = timestamp;
693 static const char* cpu_profiler_test_source2 = "function loop() {}\n"
694 "function delay() { loop(); }\n"
695 "function start(count) {\n"
699 " } while (++k < count*100*1000);\n"
702 // Check that the profile tree doesn't contain unexpected traces:
703 // - 'loop' can be called only by 'delay'
704 // - 'delay' may be called only by 'start'
705 // The profile will look like the following:
708 // 135 0 (root) [-1] #1
709 // 121 72 start [-1] #3
710 // 49 33 delay [-1] #4
711 // 16 16 loop [-1] #5
712 // 14 14 (program) [-1] #2
713 TEST(SampleWhenFrameIsNotSetup) {
715 v8::HandleScope scope(env->GetIsolate());
717 CompileRun(cpu_profiler_test_source2);
718 v8::Local<v8::Function> function = GetFunction(*env, "start");
720 int32_t repeat_count = 100;
721 #if defined(USE_SIMULATOR)
722 // Simulators are much slower.
725 v8::Handle<v8::Value> args[] = {
726 v8::Integer::New(env->GetIsolate(), repeat_count)
728 v8::CpuProfile* profile =
729 RunProfiler(env.local(), function, args, arraysize(args), 100);
731 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
733 ScopedVector<v8::Handle<v8::String> > names(3);
734 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
735 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
736 names[2] = v8_str("start");
737 CheckChildrenNames(root, names);
739 const v8::CpuProfileNode* startNode =
740 FindChild(env->GetIsolate(), root, "start");
741 // On slow machines there may be no meaningfull samples at all, skip the
743 if (startNode && startNode->GetChildrenCount() > 0) {
744 CHECK_EQ(1, startNode->GetChildrenCount());
745 const v8::CpuProfileNode* delayNode =
746 GetChild(env->GetIsolate(), startNode, "delay");
747 if (delayNode->GetChildrenCount() > 0) {
748 CHECK_EQ(1, delayNode->GetChildrenCount());
749 GetChild(env->GetIsolate(), delayNode, "loop");
757 static const char* native_accessor_test_source = "function start(count) {\n"
758 " for (var i = 0; i < count; i++) {\n"
759 " var o = instance.foo;\n"
760 " instance.foo = o + 1;\n"
765 class TestApiCallbacks {
767 explicit TestApiCallbacks(int min_duration_ms)
768 : min_duration_ms_(min_duration_ms),
769 is_warming_up_(false) {}
771 static void Getter(v8::Local<v8::String> name,
772 const v8::PropertyCallbackInfo<v8::Value>& info) {
773 TestApiCallbacks* data = fromInfo(info);
777 static void Setter(v8::Local<v8::String> name,
778 v8::Local<v8::Value> value,
779 const v8::PropertyCallbackInfo<void>& info) {
780 TestApiCallbacks* data = fromInfo(info);
784 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) {
785 TestApiCallbacks* data = fromInfo(info);
789 void set_warming_up(bool value) { is_warming_up_ = value; }
793 if (is_warming_up_) return;
794 double start = v8::base::OS::TimeCurrentMillis();
796 while (duration < min_duration_ms_) {
797 v8::base::OS::Sleep(1);
798 duration = v8::base::OS::TimeCurrentMillis() - start;
803 static TestApiCallbacks* fromInfo(const T& info) {
804 void* data = v8::External::Cast(*info.Data())->Value();
805 return reinterpret_cast<TestApiCallbacks*>(data);
808 int min_duration_ms_;
813 // Test that native accessors are properly reported in the CPU profile.
814 // This test checks the case when the long-running accessors are called
815 // only once and the optimizer doesn't have chance to change the invocation
817 TEST(NativeAccessorUninitializedIC) {
819 v8::Isolate* isolate = env->GetIsolate();
820 v8::HandleScope scope(isolate);
822 v8::Local<v8::FunctionTemplate> func_template =
823 v8::FunctionTemplate::New(isolate);
824 v8::Local<v8::ObjectTemplate> instance_template =
825 func_template->InstanceTemplate();
827 TestApiCallbacks accessors(100);
828 v8::Local<v8::External> data =
829 v8::External::New(isolate, &accessors);
830 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
831 &TestApiCallbacks::Setter, data);
832 v8::Local<v8::Function> func = func_template->GetFunction();
833 v8::Local<v8::Object> instance = func->NewInstance();
834 env->Global()->Set(v8_str("instance"), instance);
836 CompileRun(native_accessor_test_source);
837 v8::Local<v8::Function> function = GetFunction(*env, "start");
839 int32_t repeat_count = 1;
840 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
841 v8::CpuProfile* profile =
842 RunProfiler(env.local(), function, args, arraysize(args), 180);
844 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
845 const v8::CpuProfileNode* startNode =
846 GetChild(isolate, root, "start");
847 GetChild(isolate, startNode, "get foo");
848 GetChild(isolate, startNode, "set foo");
854 // Test that native accessors are properly reported in the CPU profile.
855 // This test makes sure that the accessors are called enough times to become
856 // hot and to trigger optimizations.
857 TEST(NativeAccessorMonomorphicIC) {
859 v8::Isolate* isolate = env->GetIsolate();
860 v8::HandleScope scope(isolate);
862 v8::Local<v8::FunctionTemplate> func_template =
863 v8::FunctionTemplate::New(isolate);
864 v8::Local<v8::ObjectTemplate> instance_template =
865 func_template->InstanceTemplate();
867 TestApiCallbacks accessors(1);
868 v8::Local<v8::External> data =
869 v8::External::New(isolate, &accessors);
870 instance_template->SetAccessor(v8_str("foo"), &TestApiCallbacks::Getter,
871 &TestApiCallbacks::Setter, data);
872 v8::Local<v8::Function> func = func_template->GetFunction();
873 v8::Local<v8::Object> instance = func->NewInstance();
874 env->Global()->Set(v8_str("instance"), instance);
876 CompileRun(native_accessor_test_source);
877 v8::Local<v8::Function> function = GetFunction(*env, "start");
880 // Make sure accessors ICs are in monomorphic state before starting
882 accessors.set_warming_up(true);
883 int32_t warm_up_iterations = 3;
884 v8::Handle<v8::Value> args[] = {
885 v8::Integer::New(isolate, warm_up_iterations)
887 function->Call(env->Global(), arraysize(args), args);
888 accessors.set_warming_up(false);
891 int32_t repeat_count = 100;
892 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
893 v8::CpuProfile* profile =
894 RunProfiler(env.local(), function, args, arraysize(args), 200);
896 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
897 const v8::CpuProfileNode* startNode =
898 GetChild(isolate, root, "start");
899 GetChild(isolate, startNode, "get foo");
900 GetChild(isolate, startNode, "set foo");
906 static const char* native_method_test_source = "function start(count) {\n"
907 " for (var i = 0; i < count; i++) {\n"
908 " instance.fooMethod();\n"
913 TEST(NativeMethodUninitializedIC) {
915 v8::Isolate* isolate = env->GetIsolate();
916 v8::HandleScope scope(isolate);
918 TestApiCallbacks callbacks(100);
919 v8::Local<v8::External> data =
920 v8::External::New(isolate, &callbacks);
922 v8::Local<v8::FunctionTemplate> func_template =
923 v8::FunctionTemplate::New(isolate);
924 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
925 v8::Local<v8::ObjectTemplate> proto_template =
926 func_template->PrototypeTemplate();
927 v8::Local<v8::Signature> signature =
928 v8::Signature::New(isolate, func_template);
931 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
934 v8::Local<v8::Function> func = func_template->GetFunction();
935 v8::Local<v8::Object> instance = func->NewInstance();
936 env->Global()->Set(v8_str("instance"), instance);
938 CompileRun(native_method_test_source);
939 v8::Local<v8::Function> function = GetFunction(*env, "start");
941 int32_t repeat_count = 1;
942 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
943 v8::CpuProfile* profile =
944 RunProfiler(env.local(), function, args, arraysize(args), 100);
946 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
947 const v8::CpuProfileNode* startNode =
948 GetChild(isolate, root, "start");
949 GetChild(isolate, startNode, "fooMethod");
955 TEST(NativeMethodMonomorphicIC) {
957 v8::Isolate* isolate = env->GetIsolate();
958 v8::HandleScope scope(isolate);
960 TestApiCallbacks callbacks(1);
961 v8::Local<v8::External> data =
962 v8::External::New(isolate, &callbacks);
964 v8::Local<v8::FunctionTemplate> func_template =
965 v8::FunctionTemplate::New(isolate);
966 func_template->SetClassName(v8_str("Test_InstanceCostructor"));
967 v8::Local<v8::ObjectTemplate> proto_template =
968 func_template->PrototypeTemplate();
969 v8::Local<v8::Signature> signature =
970 v8::Signature::New(isolate, func_template);
973 v8::FunctionTemplate::New(isolate, &TestApiCallbacks::Callback, data,
976 v8::Local<v8::Function> func = func_template->GetFunction();
977 v8::Local<v8::Object> instance = func->NewInstance();
978 env->Global()->Set(v8_str("instance"), instance);
980 CompileRun(native_method_test_source);
981 v8::Local<v8::Function> function = GetFunction(*env, "start");
983 // Make sure method ICs are in monomorphic state before starting
985 callbacks.set_warming_up(true);
986 int32_t warm_up_iterations = 3;
987 v8::Handle<v8::Value> args[] = {
988 v8::Integer::New(isolate, warm_up_iterations)
990 function->Call(env->Global(), arraysize(args), args);
991 callbacks.set_warming_up(false);
994 int32_t repeat_count = 100;
995 v8::Handle<v8::Value> args[] = { v8::Integer::New(isolate, repeat_count) };
996 v8::CpuProfile* profile =
997 RunProfiler(env.local(), function, args, arraysize(args), 100);
999 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1000 GetChild(isolate, root, "start");
1001 const v8::CpuProfileNode* startNode =
1002 GetChild(isolate, root, "start");
1003 GetChild(isolate, startNode, "fooMethod");
1009 static const char* bound_function_test_source =
1010 "function foo() {\n"
1011 " startProfiling('my_profile');\n"
1013 "function start() {\n"
1014 " var callback = foo.bind(this);\n"
1019 TEST(BoundFunctionCall) {
1020 v8::HandleScope scope(CcTest::isolate());
1021 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1022 v8::Context::Scope context_scope(env);
1024 CompileRun(bound_function_test_source);
1025 v8::Local<v8::Function> function = GetFunction(*env, "start");
1027 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1029 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1030 ScopedVector<v8::Handle<v8::String> > names(3);
1031 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1032 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1033 names[2] = v8_str("start");
1034 // Don't allow |foo| node to be at the top level.
1035 CheckChildrenNames(root, names);
1037 const v8::CpuProfileNode* startNode =
1038 GetChild(env->GetIsolate(), root, "start");
1039 GetChild(env->GetIsolate(), startNode, "foo");
1045 // This tests checks distribution of the samples through the source lines.
1047 CcTest::InitializeVM();
1049 i::FLAG_turbo_source_positions = true;
1050 i::Isolate* isolate = CcTest::i_isolate();
1051 i::Factory* factory = isolate->factory();
1052 i::HandleScope scope(isolate);
1054 i::EmbeddedVector<char, 512> script;
1056 const char* func_name = "func";
1060 " var m = 100*100;\n"
1061 " while (m > 1) {\n"
1063 " n += m * m * m;\n"
1067 func_name, func_name);
1069 CompileRun(script.start());
1071 i::Handle<i::JSFunction> func =
1072 v8::Utils::OpenHandle(*GetFunction(*env, func_name));
1073 CHECK(func->shared());
1074 CHECK(func->shared()->code());
1075 i::Code* code = NULL;
1076 if (func->code()->is_optimized_code()) {
1077 code = func->code();
1079 CHECK(func->shared()->code() == func->code() || !i::FLAG_crankshaft);
1080 code = func->shared()->code();
1083 i::Address code_address = code->instruction_start();
1084 CHECK(code_address);
1086 CpuProfilesCollection* profiles = new CpuProfilesCollection(isolate->heap());
1087 profiles->StartProfiling("", false);
1088 ProfileGenerator generator(profiles);
1089 ProfilerEventsProcessor* processor = new ProfilerEventsProcessor(
1090 &generator, NULL, v8::base::TimeDelta::FromMicroseconds(100));
1092 CpuProfiler profiler(isolate, profiles, &generator, processor);
1094 // Enqueue code creation events.
1095 i::Handle<i::String> str = factory->NewStringFromAsciiChecked(func_name);
1098 profiler.CodeCreateEvent(i::Logger::FUNCTION_TAG, code, func->shared(), NULL,
1099 *str, line, column);
1101 // Enqueue a tick event to enable code events processing.
1102 EnqueueTickSampleEvent(processor, code_address);
1104 processor->StopSynchronously();
1106 CpuProfile* profile = profiles->StopProfiling("");
1109 // Check the state of profile generator.
1110 CodeEntry* func_entry = generator.code_map()->FindEntry(code_address);
1112 CHECK_EQ(0, strcmp(func_name, func_entry->name()));
1113 const i::JITLineInfoTable* line_info = func_entry->line_info();
1115 CHECK(!line_info->empty());
1117 // Check the hit source lines using V8 Public APIs.
1118 const i::ProfileTree* tree = profile->top_down();
1119 ProfileNode* root = tree->root();
1121 ProfileNode* func_node = root->FindChild(func_entry);
1124 // Add 10 faked ticks to source line #5.
1127 for (int i = 0; i < hit_count; i++) func_node->IncrementLineTicks(hit_line);
1129 unsigned int line_count = func_node->GetHitLineCount();
1130 CHECK_EQ(2u, line_count); // Expect two hit source lines - #1 and #5.
1131 ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count);
1132 CHECK(func_node->GetLineTicks(&entries[0], line_count));
1134 for (int i = 0; i < entries.length(); i++)
1135 if (entries[i].line == hit_line) {
1136 value = entries[i].hit_count;
1139 CHECK_EQ(hit_count, value);
1143 static const char* call_function_test_source = "function bar(iterations) {\n"
1145 "function start(duration) {\n"
1146 " var start = Date.now();\n"
1147 " while (Date.now() - start < duration) {\n"
1149 " bar.call(this, 10 * 1000);\n"
1155 // Test that if we sampled thread when it was inside FunctionCall buitin then
1156 // its caller frame will be '(unresolved function)' as we have no reliable way
1160 // 96 0 (root) [-1] #1
1161 // 1 1 (garbage collector) [-1] #4
1162 // 5 0 (unresolved function) [-1] #5
1164 // 71 70 start [-1] #3
1166 // 19 19 (program) [-1] #2
1167 TEST(FunctionCallSample) {
1169 v8::HandleScope scope(env->GetIsolate());
1171 // Collect garbage that might have be generated while installing
1173 CcTest::heap()->CollectAllGarbage(Heap::kNoGCFlags);
1175 CompileRun(call_function_test_source);
1176 v8::Local<v8::Function> function = GetFunction(*env, "start");
1178 int32_t duration_ms = 100;
1179 v8::Handle<v8::Value> args[] = {
1180 v8::Integer::New(env->GetIsolate(), duration_ms)
1182 v8::CpuProfile* profile =
1183 RunProfiler(env.local(), function, args, arraysize(args), 100);
1185 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1187 ScopedVector<v8::Handle<v8::String> > names(4);
1188 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1189 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1190 names[2] = v8_str("start");
1191 names[3] = v8_str(i::ProfileGenerator::kUnresolvedFunctionName);
1192 // Don't allow |bar| and |call| nodes to be at the top level.
1193 CheckChildrenNames(root, names);
1196 // In case of GC stress tests all samples may be in GC phase and there
1197 // won't be |start| node in the profiles.
1198 bool is_gc_stress_testing =
1199 (i::FLAG_gc_interval != -1) || i::FLAG_stress_compaction;
1200 const v8::CpuProfileNode* startNode =
1201 FindChild(env->GetIsolate(), root, "start");
1202 CHECK(is_gc_stress_testing || startNode);
1204 ScopedVector<v8::Handle<v8::String> > names(2);
1205 names[0] = v8_str("bar");
1206 names[1] = v8_str("call");
1207 CheckChildrenNames(startNode, names);
1210 const v8::CpuProfileNode* unresolvedNode = FindChild(
1211 env->GetIsolate(), root, i::ProfileGenerator::kUnresolvedFunctionName);
1212 if (unresolvedNode) {
1213 ScopedVector<v8::Handle<v8::String> > names(1);
1214 names[0] = v8_str("call");
1215 CheckChildrenNames(unresolvedNode, names);
1222 static const char* function_apply_test_source =
1223 "function bar(iterations) {\n"
1225 "function test() {\n"
1226 " bar.apply(this, [10 * 1000]);\n"
1228 "function start(duration) {\n"
1229 " var start = Date.now();\n"
1230 " while (Date.now() - start < duration) {\n"
1239 // 94 0 (root) [-1] #0 1
1240 // 2 2 (garbage collector) [-1] #0 7
1241 // 82 49 start [-1] #16 3
1242 // 1 0 (unresolved function) [-1] #0 8
1243 // 1 1 apply [-1] #0 9
1244 // 32 21 test [-1] #16 4
1245 // 2 2 bar [-1] #16 6
1246 // 9 9 apply [-1] #0 5
1247 // 10 10 (program) [-1] #0 2
1248 TEST(FunctionApplySample) {
1250 v8::HandleScope scope(env->GetIsolate());
1252 CompileRun(function_apply_test_source);
1253 v8::Local<v8::Function> function = GetFunction(*env, "start");
1255 int32_t duration_ms = 100;
1256 v8::Handle<v8::Value> args[] = {
1257 v8::Integer::New(env->GetIsolate(), duration_ms)
1260 v8::CpuProfile* profile =
1261 RunProfiler(env.local(), function, args, arraysize(args), 100);
1263 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1265 ScopedVector<v8::Handle<v8::String> > names(3);
1266 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1267 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1268 names[2] = v8_str("start");
1269 // Don't allow |test|, |bar| and |apply| nodes to be at the top level.
1270 CheckChildrenNames(root, names);
1273 const v8::CpuProfileNode* startNode =
1274 FindChild(env->GetIsolate(), root, "start");
1277 ScopedVector<v8::Handle<v8::String> > names(2);
1278 names[0] = v8_str("test");
1279 names[1] = v8_str(ProfileGenerator::kUnresolvedFunctionName);
1280 CheckChildrenNames(startNode, names);
1283 const v8::CpuProfileNode* testNode =
1284 FindChild(env->GetIsolate(), startNode, "test");
1286 ScopedVector<v8::Handle<v8::String> > names(3);
1287 names[0] = v8_str("bar");
1288 names[1] = v8_str("apply");
1289 // apply calls "get length" before invoking the function itself
1290 // and we may get hit into it.
1291 names[2] = v8_str("get length");
1292 CheckChildrenNames(testNode, names);
1295 if (const v8::CpuProfileNode* unresolvedNode =
1296 FindChild(env->GetIsolate(), startNode,
1297 ProfileGenerator::kUnresolvedFunctionName)) {
1298 ScopedVector<v8::Handle<v8::String> > names(1);
1299 names[0] = v8_str("apply");
1300 CheckChildrenNames(unresolvedNode, names);
1301 GetChild(env->GetIsolate(), unresolvedNode, "apply");
1309 static const char* cpu_profiler_deep_stack_test_source =
1310 "function foo(n) {\n"
1314 " startProfiling('my_profile');\n"
1316 "function start() {\n"
1321 // Check a deep stack
1326 // 0 start 21 #3 no reason
1327 // 0 foo 21 #4 no reason
1328 // 0 foo 21 #5 no reason
1330 // 0 foo 21 #253 no reason
1331 // 1 startProfiling 0 #254
1332 TEST(CpuProfileDeepStack) {
1333 v8::HandleScope scope(CcTest::isolate());
1334 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1335 v8::Context::Scope context_scope(env);
1337 CompileRun(cpu_profiler_deep_stack_test_source);
1338 v8::Local<v8::Function> function = GetFunction(*env, "start");
1340 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1341 v8::Local<v8::String> profile_name = v8_str("my_profile");
1342 function->Call(env->Global(), 0, NULL);
1343 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
1345 // Dump collected profile to have a better diagnostic in case of failure.
1346 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1348 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1350 ScopedVector<v8::Handle<v8::String> > names(3);
1351 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1352 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1353 names[2] = v8_str("start");
1354 CheckChildrenNames(root, names);
1357 const v8::CpuProfileNode* node =
1358 GetChild(env->GetIsolate(), root, "start");
1359 for (int i = 0; i < 250; ++i) {
1360 node = GetChild(env->GetIsolate(), node, "foo");
1363 // In theory there must be one more 'foo' and a 'startProfiling' nodes,
1364 // but due to unstable top frame extraction these might be missing.
1370 static const char* js_native_js_test_source =
1371 "function foo() {\n"
1372 " startProfiling('my_profile');\n"
1374 "function bar() {\n"
1375 " try { foo(); } catch(e) {}\n"
1377 "function start() {\n"
1379 " CallJsFunction(bar);\n"
1383 static void CallJsFunction(const v8::FunctionCallbackInfo<v8::Value>& info) {
1384 v8::Handle<v8::Function> function = info[0].As<v8::Function>();
1385 v8::Handle<v8::Value> argv[] = { info[1] };
1386 function->Call(info.This(), arraysize(argv), argv);
1392 // 2 2 (program) #0 2
1394 // 55 0 CallJsFunction #0 4
1397 TEST(JsNativeJsSample) {
1398 v8::HandleScope scope(CcTest::isolate());
1399 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1400 v8::Context::Scope context_scope(env);
1402 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1403 env->GetIsolate(), CallJsFunction);
1404 v8::Local<v8::Function> func = func_template->GetFunction();
1405 func->SetName(v8_str("CallJsFunction"));
1406 env->Global()->Set(v8_str("CallJsFunction"), func);
1408 CompileRun(js_native_js_test_source);
1409 v8::Local<v8::Function> function = GetFunction(*env, "start");
1411 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1413 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1415 ScopedVector<v8::Handle<v8::String> > names(3);
1416 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1417 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1418 names[2] = v8_str("start");
1419 CheckChildrenNames(root, names);
1422 const v8::CpuProfileNode* startNode =
1423 GetChild(env->GetIsolate(), root, "start");
1424 CHECK_EQ(1, startNode->GetChildrenCount());
1425 const v8::CpuProfileNode* nativeFunctionNode =
1426 GetChild(env->GetIsolate(), startNode, "CallJsFunction");
1428 CHECK_EQ(1, nativeFunctionNode->GetChildrenCount());
1429 const v8::CpuProfileNode* barNode =
1430 GetChild(env->GetIsolate(), nativeFunctionNode, "bar");
1432 CHECK_EQ(1, barNode->GetChildrenCount());
1433 GetChild(env->GetIsolate(), barNode, "foo");
1439 static const char* js_native_js_runtime_js_test_source =
1440 "function foo() {\n"
1441 " startProfiling('my_profile');\n"
1443 "var bound = foo.bind(this);\n"
1444 "function bar() {\n"
1445 " try { bound(); } catch(e) {}\n"
1447 "function start() {\n"
1449 " CallJsFunction(bar);\n"
1457 // 54 0 CallJsFunction #0 4
1460 // 2 2 (program) #0 2
1461 TEST(JsNativeJsRuntimeJsSample) {
1462 v8::HandleScope scope(CcTest::isolate());
1463 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1464 v8::Context::Scope context_scope(env);
1466 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1467 env->GetIsolate(), CallJsFunction);
1468 v8::Local<v8::Function> func = func_template->GetFunction();
1469 func->SetName(v8_str("CallJsFunction"));
1470 env->Global()->Set(v8_str("CallJsFunction"), func);
1472 CompileRun(js_native_js_runtime_js_test_source);
1473 v8::Local<v8::Function> function = GetFunction(*env, "start");
1475 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1477 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1478 ScopedVector<v8::Handle<v8::String> > names(3);
1479 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1480 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1481 names[2] = v8_str("start");
1482 CheckChildrenNames(root, names);
1484 const v8::CpuProfileNode* startNode =
1485 GetChild(env->GetIsolate(), root, "start");
1486 CHECK_EQ(1, startNode->GetChildrenCount());
1487 const v8::CpuProfileNode* nativeFunctionNode =
1488 GetChild(env->GetIsolate(), startNode, "CallJsFunction");
1490 CHECK_EQ(1, nativeFunctionNode->GetChildrenCount());
1491 const v8::CpuProfileNode* barNode =
1492 GetChild(env->GetIsolate(), nativeFunctionNode, "bar");
1494 // The child is in fact a bound foo.
1495 // A bound function has a wrapper that may make calls to
1496 // other functions e.g. "get length".
1497 CHECK_LE(1, barNode->GetChildrenCount());
1498 CHECK_GE(2, barNode->GetChildrenCount());
1499 GetChild(env->GetIsolate(), barNode, "foo");
1505 static void CallJsFunction2(const v8::FunctionCallbackInfo<v8::Value>& info) {
1506 v8::base::OS::Print("In CallJsFunction2\n");
1507 CallJsFunction(info);
1511 static const char* js_native1_js_native2_js_test_source =
1512 "function foo() {\n"
1514 " startProfiling('my_profile');\n"
1517 "function bar() {\n"
1518 " CallJsFunction2(foo);\n"
1520 "function start() {\n"
1522 " CallJsFunction1(bar);\n"
1530 // 54 0 CallJsFunction1 #0 4
1532 // 54 0 CallJsFunction2 #0 6
1534 // 2 2 (program) #0 2
1535 TEST(JsNative1JsNative2JsSample) {
1536 v8::HandleScope scope(CcTest::isolate());
1537 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1538 v8::Context::Scope context_scope(env);
1540 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(
1541 env->GetIsolate(), CallJsFunction);
1542 v8::Local<v8::Function> func1 = func_template->GetFunction();
1543 func1->SetName(v8_str("CallJsFunction1"));
1544 env->Global()->Set(v8_str("CallJsFunction1"), func1);
1546 v8::Local<v8::Function> func2 = v8::FunctionTemplate::New(
1547 env->GetIsolate(), CallJsFunction2)->GetFunction();
1548 func2->SetName(v8_str("CallJsFunction2"));
1549 env->Global()->Set(v8_str("CallJsFunction2"), func2);
1551 CompileRun(js_native1_js_native2_js_test_source);
1552 v8::Local<v8::Function> function = GetFunction(*env, "start");
1554 v8::CpuProfile* profile = RunProfiler(env, function, NULL, 0, 0);
1556 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1557 ScopedVector<v8::Handle<v8::String> > names(3);
1558 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1559 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1560 names[2] = v8_str("start");
1561 CheckChildrenNames(root, names);
1563 const v8::CpuProfileNode* startNode =
1564 GetChild(env->GetIsolate(), root, "start");
1565 CHECK_EQ(1, startNode->GetChildrenCount());
1566 const v8::CpuProfileNode* nativeNode1 =
1567 GetChild(env->GetIsolate(), startNode, "CallJsFunction1");
1569 CHECK_EQ(1, nativeNode1->GetChildrenCount());
1570 const v8::CpuProfileNode* barNode =
1571 GetChild(env->GetIsolate(), nativeNode1, "bar");
1573 CHECK_EQ(1, barNode->GetChildrenCount());
1574 const v8::CpuProfileNode* nativeNode2 =
1575 GetChild(env->GetIsolate(), barNode, "CallJsFunction2");
1577 CHECK_EQ(1, nativeNode2->GetChildrenCount());
1578 GetChild(env->GetIsolate(), nativeNode2, "foo");
1586 // 3 3 (program) #0 2
1590 v8::HandleScope scope(env->GetIsolate());
1591 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
1593 v8::Local<v8::String> profile_name = v8_str("my_profile");
1594 cpu_profiler->StartProfiling(profile_name);
1596 i::Isolate* isolate = CcTest::i_isolate();
1597 i::ProfilerEventsProcessor* processor = isolate->cpu_profiler()->processor();
1598 processor->AddCurrentStack(isolate);
1600 cpu_profiler->SetIdle(true);
1602 for (int i = 0; i < 3; i++) {
1603 processor->AddCurrentStack(isolate);
1606 cpu_profiler->SetIdle(false);
1607 processor->AddCurrentStack(isolate);
1610 v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);
1612 // Dump collected profile to have a better diagnostic in case of failure.
1613 reinterpret_cast<i::CpuProfile*>(profile)->Print();
1615 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1616 ScopedVector<v8::Handle<v8::String> > names(3);
1617 names[0] = v8_str(ProfileGenerator::kGarbageCollectorEntryName);
1618 names[1] = v8_str(ProfileGenerator::kProgramEntryName);
1619 names[2] = v8_str(ProfileGenerator::kIdleEntryName);
1620 CheckChildrenNames(root, names);
1622 const v8::CpuProfileNode* programNode =
1623 GetChild(env->GetIsolate(), root, ProfileGenerator::kProgramEntryName);
1624 CHECK_EQ(0, programNode->GetChildrenCount());
1625 CHECK_GE(programNode->GetHitCount(), 3u);
1627 const v8::CpuProfileNode* idleNode =
1628 GetChild(env->GetIsolate(), root, ProfileGenerator::kIdleEntryName);
1629 CHECK_EQ(0, idleNode->GetChildrenCount());
1630 CHECK_GE(idleNode->GetHitCount(), 3u);
1636 static void CheckFunctionDetails(v8::Isolate* isolate,
1637 const v8::CpuProfileNode* node,
1638 const char* name, const char* script_name,
1639 int script_id, int line, int column) {
1640 CHECK(v8_str(name)->Equals(node->GetFunctionName()));
1641 CHECK(v8_str(script_name)->Equals(node->GetScriptResourceName()));
1642 CHECK_EQ(script_id, node->GetScriptId());
1643 CHECK_EQ(line, node->GetLineNumber());
1644 CHECK_EQ(column, node->GetColumnNumber());
1648 TEST(FunctionDetails) {
1649 v8::HandleScope scope(CcTest::isolate());
1650 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1651 v8::Context::Scope context_scope(env);
1653 v8::Handle<v8::Script> script_a = CompileWithOrigin(
1654 " function foo\n() { try { bar(); } catch(e) {} }\n"
1655 " function bar() { startProfiling(); }\n",
1658 v8::Handle<v8::Script> script_b = CompileWithOrigin(
1659 "\n\n function baz() { try { foo(); } catch(e) {} }\n"
1661 "stopProfiling();\n",
1664 const v8::CpuProfile* profile = i::ProfilerExtension::last_profile;
1665 const v8::CpuProfileNode* current = profile->GetTopDownRoot();
1666 reinterpret_cast<ProfileNode*>(
1667 const_cast<v8::CpuProfileNode*>(current))->Print(0);
1668 // The tree should look like this:
1670 // 0 "" 19 #2 no reason script_b:1
1671 // 0 baz 19 #3 TryCatchStatement script_b:3
1672 // 0 foo 18 #4 TryCatchStatement script_a:2
1673 // 1 bar 18 #5 no reason script_a:3
1674 const v8::CpuProfileNode* root = profile->GetTopDownRoot();
1675 const v8::CpuProfileNode* script = GetChild(env->GetIsolate(), root, "");
1676 CheckFunctionDetails(env->GetIsolate(), script, "", "script_b",
1677 script_b->GetUnboundScript()->GetId(), 1, 1);
1678 const v8::CpuProfileNode* baz = GetChild(env->GetIsolate(), script, "baz");
1679 CheckFunctionDetails(env->GetIsolate(), baz, "baz", "script_b",
1680 script_b->GetUnboundScript()->GetId(), 3, 16);
1681 const v8::CpuProfileNode* foo = GetChild(env->GetIsolate(), baz, "foo");
1682 CheckFunctionDetails(env->GetIsolate(), foo, "foo", "script_a",
1683 script_a->GetUnboundScript()->GetId(), 2, 1);
1684 const v8::CpuProfileNode* bar = GetChild(env->GetIsolate(), foo, "bar");
1685 CheckFunctionDetails(env->GetIsolate(), bar, "bar", "script_a",
1686 script_a->GetUnboundScript()->GetId(), 3, 14);
1690 TEST(DontStopOnFinishedProfileDelete) {
1691 v8::HandleScope scope(CcTest::isolate());
1692 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1693 v8::Context::Scope context_scope(env);
1695 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler();
1696 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1698 CHECK_EQ(0, iprofiler->GetProfilesCount());
1699 v8::Handle<v8::String> outer = v8_str("outer");
1700 profiler->StartProfiling(outer);
1701 CHECK_EQ(0, iprofiler->GetProfilesCount());
1703 v8::Handle<v8::String> inner = v8_str("inner");
1704 profiler->StartProfiling(inner);
1705 CHECK_EQ(0, iprofiler->GetProfilesCount());
1707 v8::CpuProfile* inner_profile = profiler->StopProfiling(inner);
1708 CHECK(inner_profile);
1709 CHECK_EQ(1, iprofiler->GetProfilesCount());
1710 inner_profile->Delete();
1711 inner_profile = NULL;
1712 CHECK_EQ(0, iprofiler->GetProfilesCount());
1714 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer);
1715 CHECK(outer_profile);
1716 CHECK_EQ(1, iprofiler->GetProfilesCount());
1717 outer_profile->Delete();
1718 outer_profile = NULL;
1719 CHECK_EQ(0, iprofiler->GetProfilesCount());
1723 static const char* collect_deopt_events_test_source =
1724 "function opt_function(left, right, depth) {\n"
1725 " if (depth) return opt_function(left, right, depth - 1);\n"
1727 " var k = left / 10;\n"
1728 " var r = 10 / right;\n"
1732 "function test(left, right) {\n"
1733 " return opt_function(left, right, 1);\n"
1736 "startProfiling();\n"
1740 "%OptimizeFunctionOnNextCall(opt_function)\n"
1744 "test(undefined, 10);\n"
1746 "%OptimizeFunctionOnNextCall(opt_function)\n"
1752 "stopProfiling();\n"
1756 TEST(CollectDeoptEvents) {
1757 if (!CcTest::i_isolate()->use_crankshaft() || i::FLAG_always_opt) return;
1758 i::FLAG_allow_natives_syntax = true;
1759 v8::HandleScope scope(CcTest::isolate());
1760 v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
1761 v8::Context::Scope context_scope(env);
1762 v8::Isolate* isolate = env->GetIsolate();
1763 v8::CpuProfiler* profiler = isolate->GetCpuProfiler();
1764 i::CpuProfiler* iprofiler = reinterpret_cast<i::CpuProfiler*>(profiler);
1766 v8::Script::Compile(v8_str(collect_deopt_events_test_source))->Run();
1767 i::CpuProfile* iprofile = iprofiler->GetProfile(0);
1769 v8::CpuProfile* profile = reinterpret_cast<v8::CpuProfile*>(iprofile);
1770 const char* branch[] = {"", "test", "opt_function", "opt_function"};
1771 const v8::CpuProfileNode* opt_function = GetSimpleBranch(
1772 env->GetIsolate(), profile->GetTopDownRoot(), branch, arraysize(branch));
1773 CHECK(opt_function);
1774 const i::ProfileNode* iopt_function =
1775 reinterpret_cast<const i::ProfileNode*>(opt_function);
1776 CHECK_EQ(2, iopt_function->deopt_infos().length());
1777 CHECK_EQ(i::Deoptimizer::GetDeoptReason(i::Deoptimizer::kNotAHeapNumber),
1778 iopt_function->deopt_infos()[0].deopt_reason);
1779 CHECK_EQ(i::Deoptimizer::GetDeoptReason(i::Deoptimizer::kDivisionByZero),
1780 iopt_function->deopt_infos()[1].deopt_reason);
1781 iprofiler->DeleteProfile(iprofile);
1785 TEST(SourceLocation) {
1786 i::FLAG_always_opt = true;
1787 i::FLAG_hydrogen_track_positions = true;
1789 v8::HandleScope scope(CcTest::isolate());
1791 const char* source =
1792 "function CompareStatementWithThis() {\n"
1793 " if (this === 1) {}\n"
1795 "CompareStatementWithThis();\n";
1797 v8::Script::Compile(v8_str(source))->Run();