OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 static const char* kSource; | 549 static const char* kSource; |
550 }; | 550 }; |
551 | 551 |
552 | 552 |
553 const char* ProfilerExtension::kSource = | 553 const char* ProfilerExtension::kSource = |
554 "native function startProfiling();" | 554 "native function startProfiling();" |
555 "native function stopProfiling();"; | 555 "native function stopProfiling();"; |
556 | 556 |
557 v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction( | 557 v8::Handle<v8::FunctionTemplate> ProfilerExtension::GetNativeFunction( |
558 v8::Handle<v8::String> name) { | 558 v8::Handle<v8::String> name) { |
559 if (name->Equals(v8::String::New("startProfiling"))) { | 559 if (name->Equals(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), |
| 560 "startProfiling"))) { |
560 return v8::FunctionTemplate::New(ProfilerExtension::StartProfiling); | 561 return v8::FunctionTemplate::New(ProfilerExtension::StartProfiling); |
561 } else if (name->Equals(v8::String::New("stopProfiling"))) { | 562 } else if (name->Equals(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), |
| 563 "stopProfiling"))) { |
562 return v8::FunctionTemplate::New(ProfilerExtension::StopProfiling); | 564 return v8::FunctionTemplate::New(ProfilerExtension::StopProfiling); |
563 } else { | 565 } else { |
564 CHECK(false); | 566 CHECK(false); |
565 return v8::Handle<v8::FunctionTemplate>(); | 567 return v8::Handle<v8::FunctionTemplate>(); |
566 } | 568 } |
567 } | 569 } |
568 | 570 |
569 | 571 |
570 void ProfilerExtension::StartProfiling( | 572 void ProfilerExtension::StartProfiling( |
571 const v8::FunctionCallbackInfo<v8::Value>& args) { | 573 const v8::FunctionCallbackInfo<v8::Value>& args) { |
572 v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler(); | 574 v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler(); |
573 if (args.Length() > 0) | 575 if (args.Length() > 0) |
574 cpu_profiler->StartCpuProfiling(args[0].As<v8::String>()); | 576 cpu_profiler->StartCpuProfiling(args[0].As<v8::String>()); |
575 else | 577 else |
576 cpu_profiler->StartCpuProfiling(v8::String::New("")); | 578 cpu_profiler->StartCpuProfiling( |
| 579 v8::String::NewFromUtf8(args.GetIsolate(), "")); |
577 } | 580 } |
578 | 581 |
579 | 582 |
580 void ProfilerExtension::StopProfiling( | 583 void ProfilerExtension::StopProfiling( |
581 const v8::FunctionCallbackInfo<v8::Value>& args) { | 584 const v8::FunctionCallbackInfo<v8::Value>& args) { |
582 v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler(); | 585 v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler(); |
583 if (args.Length() > 0) | 586 if (args.Length() > 0) |
584 cpu_profiler->StopCpuProfiling(args[0].As<v8::String>()); | 587 cpu_profiler->StopCpuProfiling(args[0].As<v8::String>()); |
585 else | 588 else |
586 cpu_profiler->StopCpuProfiling(v8::String::New("")); | 589 cpu_profiler->StopCpuProfiling( |
| 590 v8::String::NewFromUtf8(args.GetIsolate(), "")); |
587 } | 591 } |
588 | 592 |
589 | 593 |
590 static ProfilerExtension kProfilerExtension; | 594 static ProfilerExtension kProfilerExtension; |
591 v8::DeclareExtension kProfilerExtensionDeclaration(&kProfilerExtension); | 595 v8::DeclareExtension kProfilerExtensionDeclaration(&kProfilerExtension); |
592 | 596 |
593 static const ProfileNode* PickChild(const ProfileNode* parent, | 597 static const ProfileNode* PickChild(const ProfileNode* parent, |
594 const char* name) { | 598 const char* name) { |
595 for (int i = 0; i < parent->children()->length(); ++i) { | 599 for (int i = 0; i < parent->children()->length(); ++i) { |
596 const ProfileNode* child = parent->children()->at(i); | 600 const ProfileNode* child = parent->children()->at(i); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 "maximum", CpuProfilesCollection::kMaxSimultaneousProfiles + 1, false)); | 670 "maximum", CpuProfilesCollection::kMaxSimultaneousProfiles + 1, false)); |
667 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) | 671 for (int i = 0; i < CpuProfilesCollection::kMaxSimultaneousProfiles; ++i) |
668 i::DeleteArray(titles[i]); | 672 i::DeleteArray(titles[i]); |
669 } | 673 } |
670 | 674 |
671 | 675 |
672 static const v8::CpuProfileNode* PickChild(const v8::CpuProfileNode* parent, | 676 static const v8::CpuProfileNode* PickChild(const v8::CpuProfileNode* parent, |
673 const char* name) { | 677 const char* name) { |
674 for (int i = 0; i < parent->GetChildrenCount(); ++i) { | 678 for (int i = 0; i < parent->GetChildrenCount(); ++i) { |
675 const v8::CpuProfileNode* child = parent->GetChild(i); | 679 const v8::CpuProfileNode* child = parent->GetChild(i); |
676 v8::String::AsciiValue function_name(child->GetFunctionName()); | 680 v8::String::Utf8Value function_name(child->GetFunctionName()); |
677 if (strcmp(*function_name, name) == 0) return child; | 681 if (strcmp(*function_name, name) == 0) return child; |
678 } | 682 } |
679 return NULL; | 683 return NULL; |
680 } | 684 } |
681 | 685 |
682 | 686 |
683 TEST(ProfileNodeScriptId) { | 687 TEST(ProfileNodeScriptId) { |
684 // This test does not pass with inlining enabled since inlined functions | 688 // This test does not pass with inlining enabled since inlined functions |
685 // don't appear in the stack trace. | 689 // don't appear in the stack trace. |
686 i::FLAG_use_inlining = false; | 690 i::FLAG_use_inlining = false; |
687 | 691 |
688 const char* extensions[] = { "v8/profiler" }; | 692 const char* extensions[] = { "v8/profiler" }; |
689 v8::ExtensionConfiguration config(1, extensions); | 693 v8::ExtensionConfiguration config(1, extensions); |
690 LocalContext env(&config); | 694 LocalContext env(&config); |
691 v8::HandleScope hs(env->GetIsolate()); | 695 v8::HandleScope hs(env->GetIsolate()); |
692 | 696 |
693 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler(); | 697 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler(); |
694 CHECK_EQ(0, profiler->GetProfileCount()); | 698 CHECK_EQ(0, profiler->GetProfileCount()); |
695 v8::Handle<v8::Script> script_a = v8::Script::Compile(v8::String::New( | 699 v8::Handle<v8::Script> script_a = v8::Script::Compile(v8::String::NewFromUtf8( |
696 "function a() { startProfiling(); }\n")); | 700 env->GetIsolate(), "function a() { startProfiling(); }\n")); |
697 script_a->Run(); | 701 script_a->Run(); |
698 v8::Handle<v8::Script> script_b = v8::Script::Compile(v8::String::New( | 702 v8::Handle<v8::Script> script_b = |
699 "function b() { a(); }\n" | 703 v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
700 "b();\n" | 704 "function b() { a(); }\n" |
701 "stopProfiling();\n")); | 705 "b();\n" |
| 706 "stopProfiling();\n")); |
702 script_b->Run(); | 707 script_b->Run(); |
703 CHECK_EQ(1, profiler->GetProfileCount()); | 708 CHECK_EQ(1, profiler->GetProfileCount()); |
704 const v8::CpuProfile* profile = profiler->GetCpuProfile(0); | 709 const v8::CpuProfile* profile = profiler->GetCpuProfile(0); |
705 const v8::CpuProfileNode* current = profile->GetTopDownRoot(); | 710 const v8::CpuProfileNode* current = profile->GetTopDownRoot(); |
706 reinterpret_cast<ProfileNode*>( | 711 reinterpret_cast<ProfileNode*>( |
707 const_cast<v8::CpuProfileNode*>(current))->Print(0); | 712 const_cast<v8::CpuProfileNode*>(current))->Print(0); |
708 // The tree should look like this: | 713 // The tree should look like this: |
709 // (root) | 714 // (root) |
710 // (anonymous function) | 715 // (anonymous function) |
711 // b | 716 // b |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 | 791 |
787 | 792 |
788 TEST(BailoutReason) { | 793 TEST(BailoutReason) { |
789 const char* extensions[] = { "v8/profiler" }; | 794 const char* extensions[] = { "v8/profiler" }; |
790 v8::ExtensionConfiguration config(1, extensions); | 795 v8::ExtensionConfiguration config(1, extensions); |
791 LocalContext env(&config); | 796 LocalContext env(&config); |
792 v8::HandleScope hs(env->GetIsolate()); | 797 v8::HandleScope hs(env->GetIsolate()); |
793 | 798 |
794 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler(); | 799 v8::CpuProfiler* profiler = env->GetIsolate()->GetCpuProfiler(); |
795 CHECK_EQ(0, profiler->GetProfileCount()); | 800 CHECK_EQ(0, profiler->GetProfileCount()); |
796 v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New( | 801 v8::Handle<v8::Script> script = |
797 "function TryCatch() {\n" | 802 v8::Script::Compile(v8::String::NewFromUtf8(env->GetIsolate(), |
798 " try {\n" | 803 "function TryCatch() {\n" |
799 " startProfiling();\n" | 804 " try {\n" |
800 " } catch (e) { };\n" | 805 " startProfiling();\n" |
801 "}\n" | 806 " } catch (e) { };\n" |
802 "function TryFinally() {\n" | 807 "}\n" |
803 " try {\n" | 808 "function TryFinally() {\n" |
804 " TryCatch();\n" | 809 " try {\n" |
805 " } finally { };\n" | 810 " TryCatch();\n" |
806 "}\n" | 811 " } finally { };\n" |
807 "TryFinally();\n" | 812 "}\n" |
808 "stopProfiling();")); | 813 "TryFinally();\n" |
| 814 "stopProfiling();")); |
809 script->Run(); | 815 script->Run(); |
810 CHECK_EQ(1, profiler->GetProfileCount()); | 816 CHECK_EQ(1, profiler->GetProfileCount()); |
811 const v8::CpuProfile* profile = profiler->GetCpuProfile(0); | 817 const v8::CpuProfile* profile = profiler->GetCpuProfile(0); |
812 const v8::CpuProfileNode* current = profile->GetTopDownRoot(); | 818 const v8::CpuProfileNode* current = profile->GetTopDownRoot(); |
813 reinterpret_cast<ProfileNode*>( | 819 reinterpret_cast<ProfileNode*>( |
814 const_cast<v8::CpuProfileNode*>(current))->Print(0); | 820 const_cast<v8::CpuProfileNode*>(current))->Print(0); |
815 // The tree should look like this: | 821 // The tree should look like this: |
816 // (root) | 822 // (root) |
817 // (anonymous function) | 823 // (anonymous function) |
818 // kTryFinally | 824 // kTryFinally |
819 // kTryCatch | 825 // kTryCatch |
820 current = PickChild(current, i::ProfileGenerator::kAnonymousFunctionName); | 826 current = PickChild(current, i::ProfileGenerator::kAnonymousFunctionName); |
821 CHECK_NE(NULL, const_cast<v8::CpuProfileNode*>(current)); | 827 CHECK_NE(NULL, const_cast<v8::CpuProfileNode*>(current)); |
822 | 828 |
823 current = PickChild(current, "TryFinally"); | 829 current = PickChild(current, "TryFinally"); |
824 CHECK_NE(NULL, const_cast<v8::CpuProfileNode*>(current)); | 830 CHECK_NE(NULL, const_cast<v8::CpuProfileNode*>(current)); |
825 CHECK(!strcmp("TryFinallyStatement", current->GetBailoutReason())); | 831 CHECK(!strcmp("TryFinallyStatement", current->GetBailoutReason())); |
826 | 832 |
827 current = PickChild(current, "TryCatch"); | 833 current = PickChild(current, "TryCatch"); |
828 CHECK_NE(NULL, const_cast<v8::CpuProfileNode*>(current)); | 834 CHECK_NE(NULL, const_cast<v8::CpuProfileNode*>(current)); |
829 CHECK(!strcmp("TryCatchStatement", current->GetBailoutReason())); | 835 CHECK(!strcmp("TryCatchStatement", current->GetBailoutReason())); |
830 } | 836 } |
OLD | NEW |