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 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 | 641 |
642 | 642 |
643 static const char* native_accessor_test_source = "function start(count) {\n" | 643 static const char* native_accessor_test_source = "function start(count) {\n" |
644 " for (var i = 0; i < count; i++) {\n" | 644 " for (var i = 0; i < count; i++) {\n" |
645 " var o = instance.foo;\n" | 645 " var o = instance.foo;\n" |
646 " instance.foo = o + 1;\n" | 646 " instance.foo = o + 1;\n" |
647 " }\n" | 647 " }\n" |
648 "}\n"; | 648 "}\n"; |
649 | 649 |
650 | 650 |
651 class FooAccessorsData { | 651 class TestApiCallbacks { |
652 public: | 652 public: |
653 explicit FooAccessorsData(int min_duration_ms) | 653 explicit TestApiCallbacks(int min_duration_ms) |
654 : min_duration_ms_(min_duration_ms), | 654 : min_duration_ms_(min_duration_ms), |
655 is_warming_up_(false) {} | 655 is_warming_up_(false) {} |
656 | 656 |
657 static v8::Handle<v8::Value> Getter(v8::Local<v8::String> name, | 657 static v8::Handle<v8::Value> Getter(v8::Local<v8::String> name, |
658 const v8::AccessorInfo& info) { | 658 const v8::AccessorInfo& info) { |
659 FooAccessorsData* data = fromInfo(info); | 659 TestApiCallbacks* data = fromInfo(info); |
660 data->Wait(); | 660 data->Wait(); |
661 return v8::Int32::New(2013); | 661 return v8::Int32::New(2013); |
662 } | 662 } |
663 | 663 |
664 static void Setter(v8::Local<v8::String> name, | 664 static void Setter(v8::Local<v8::String> name, |
665 v8::Local<v8::Value> value, | 665 v8::Local<v8::Value> value, |
666 const v8::AccessorInfo& info) { | 666 const v8::AccessorInfo& info) { |
667 FooAccessorsData* data = fromInfo(info); | 667 TestApiCallbacks* data = fromInfo(info); |
668 data->Wait(); | 668 data->Wait(); |
669 } | 669 } |
670 | 670 |
| 671 static void Callback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 672 TestApiCallbacks* data = fromInfo(info); |
| 673 data->Wait(); |
| 674 } |
| 675 |
671 void set_warming_up(bool value) { is_warming_up_ = value; } | 676 void set_warming_up(bool value) { is_warming_up_ = value; } |
672 | 677 |
673 private: | 678 private: |
674 void Wait() { | 679 void Wait() { |
675 if (is_warming_up_) return; | 680 if (is_warming_up_) return; |
676 double start = i::OS::TimeCurrentMillis(); | 681 double start = i::OS::TimeCurrentMillis(); |
677 double duration = 0; | 682 double duration = 0; |
678 while (duration < min_duration_ms_) { | 683 while (duration < min_duration_ms_) { |
679 i::OS::Sleep(1); | 684 i::OS::Sleep(1); |
680 duration = i::OS::TimeCurrentMillis() - start; | 685 duration = i::OS::TimeCurrentMillis() - start; |
681 } | 686 } |
682 } | 687 } |
683 | 688 |
684 static FooAccessorsData* fromInfo(const v8::AccessorInfo& info) { | 689 static TestApiCallbacks* fromInfo(const v8::AccessorInfo& info) { |
685 void* data = v8::External::Cast(*info.Data())->Value(); | 690 void* data = v8::External::Cast(*info.Data())->Value(); |
686 return reinterpret_cast<FooAccessorsData*>(data); | 691 return reinterpret_cast<TestApiCallbacks*>(data); |
| 692 } |
| 693 |
| 694 static TestApiCallbacks* fromInfo( |
| 695 const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 696 void* data = v8::External::Cast(*info.Data())->Value(); |
| 697 return reinterpret_cast<TestApiCallbacks*>(data); |
687 } | 698 } |
688 | 699 |
689 int min_duration_ms_; | 700 int min_duration_ms_; |
690 bool is_warming_up_; | 701 bool is_warming_up_; |
691 }; | 702 }; |
692 | 703 |
693 | 704 |
694 // Test that native accessors are properly reported in the CPU profile. | 705 // Test that native accessors are properly reported in the CPU profile. |
695 // This test checks the case when the long-running accessors are called | 706 // This test checks the case when the long-running accessors are called |
696 // only once and the optimizer doesn't have chance to change the invocation | 707 // only once and the optimizer doesn't have chance to change the invocation |
697 // code. | 708 // code. |
698 TEST(NativeAccessorUninitializedIC) { | 709 TEST(NativeAccessorUninitializedIC) { |
699 LocalContext env; | 710 LocalContext env; |
700 v8::HandleScope scope(env->GetIsolate()); | 711 v8::HandleScope scope(env->GetIsolate()); |
701 | 712 |
702 | 713 |
703 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); | 714 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); |
704 v8::Local<v8::ObjectTemplate> instance_template = | 715 v8::Local<v8::ObjectTemplate> instance_template = |
705 func_template->InstanceTemplate(); | 716 func_template->InstanceTemplate(); |
706 | 717 |
707 FooAccessorsData accessors(100); | 718 TestApiCallbacks accessors(100); |
708 v8::Local<v8::External> data = v8::External::New(&accessors); | 719 v8::Local<v8::External> data = v8::External::New(&accessors); |
709 instance_template->SetAccessor( | 720 instance_template->SetAccessor( |
710 v8::String::New("foo"), &FooAccessorsData::Getter, | 721 v8::String::New("foo"), &TestApiCallbacks::Getter, |
711 &FooAccessorsData::Setter, data); | 722 &TestApiCallbacks::Setter, data); |
712 v8::Local<v8::Function> func = func_template->GetFunction(); | 723 v8::Local<v8::Function> func = func_template->GetFunction(); |
713 v8::Local<v8::Object> instance = func->NewInstance(); | 724 v8::Local<v8::Object> instance = func->NewInstance(); |
714 env->Global()->Set(v8::String::New("instance"), instance); | 725 env->Global()->Set(v8::String::New("instance"), instance); |
715 | 726 |
716 v8::Script::Compile(v8::String::New(native_accessor_test_source))->Run(); | 727 v8::Script::Compile(v8::String::New(native_accessor_test_source))->Run(); |
717 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | 728 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
718 env->Global()->Get(v8::String::New("start"))); | 729 env->Global()->Get(v8::String::New("start"))); |
719 | 730 |
720 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); | 731 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
721 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); | 732 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); |
(...skipping 23 matching lines...) Expand all Loading... |
745 // hot and to trigger optimizations. | 756 // hot and to trigger optimizations. |
746 TEST(NativeAccessorMonomorphicIC) { | 757 TEST(NativeAccessorMonomorphicIC) { |
747 LocalContext env; | 758 LocalContext env; |
748 v8::HandleScope scope(env->GetIsolate()); | 759 v8::HandleScope scope(env->GetIsolate()); |
749 | 760 |
750 | 761 |
751 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); | 762 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); |
752 v8::Local<v8::ObjectTemplate> instance_template = | 763 v8::Local<v8::ObjectTemplate> instance_template = |
753 func_template->InstanceTemplate(); | 764 func_template->InstanceTemplate(); |
754 | 765 |
755 FooAccessorsData accessors(1); | 766 TestApiCallbacks accessors(1); |
756 v8::Local<v8::External> data = v8::External::New(&accessors); | 767 v8::Local<v8::External> data = v8::External::New(&accessors); |
757 instance_template->SetAccessor( | 768 instance_template->SetAccessor( |
758 v8::String::New("foo"), &FooAccessorsData::Getter, | 769 v8::String::New("foo"), &TestApiCallbacks::Getter, |
759 &FooAccessorsData::Setter, data); | 770 &TestApiCallbacks::Setter, data); |
760 v8::Local<v8::Function> func = func_template->GetFunction(); | 771 v8::Local<v8::Function> func = func_template->GetFunction(); |
761 v8::Local<v8::Object> instance = func->NewInstance(); | 772 v8::Local<v8::Object> instance = func->NewInstance(); |
762 env->Global()->Set(v8::String::New("instance"), instance); | 773 env->Global()->Set(v8::String::New("instance"), instance); |
763 | 774 |
764 v8::Script::Compile(v8::String::New(native_accessor_test_source))->Run(); | 775 v8::Script::Compile(v8::String::New(native_accessor_test_source))->Run(); |
765 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | 776 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
766 env->Global()->Get(v8::String::New("start"))); | 777 env->Global()->Get(v8::String::New("start"))); |
767 | 778 |
768 { | 779 { |
769 // Make sure accessors ICs are in monomorphic state before starting | 780 // Make sure accessors ICs are in monomorphic state before starting |
(...skipping 21 matching lines...) Expand all Loading... |
791 | 802 |
792 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); | 803 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
793 const v8::CpuProfileNode* startNode = GetChild(root, "start"); | 804 const v8::CpuProfileNode* startNode = GetChild(root, "start"); |
794 // TODO(yurys): in LoadIC should be changed to report external callback | 805 // TODO(yurys): in LoadIC should be changed to report external callback |
795 // invocation. See r13768 where it was LoadCallbackProperty was removed. | 806 // invocation. See r13768 where it was LoadCallbackProperty was removed. |
796 // GetChild(startNode, "get foo"); | 807 // GetChild(startNode, "get foo"); |
797 GetChild(startNode, "set foo"); | 808 GetChild(startNode, "set foo"); |
798 | 809 |
799 cpu_profiler->DeleteAllCpuProfiles(); | 810 cpu_profiler->DeleteAllCpuProfiles(); |
800 } | 811 } |
| 812 |
| 813 |
| 814 static const char* native_method_test_source = "function start(count) {\n" |
| 815 " for (var i = 0; i < count; i++) {\n" |
| 816 " instance.fooMethod();\n" |
| 817 " }\n" |
| 818 "}\n"; |
| 819 |
| 820 |
| 821 TEST(NativeMethodUninitializedIC) { |
| 822 LocalContext env; |
| 823 v8::HandleScope scope(env->GetIsolate()); |
| 824 |
| 825 TestApiCallbacks callbacks(100); |
| 826 v8::Local<v8::External> data = v8::External::New(&callbacks); |
| 827 |
| 828 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); |
| 829 func_template->SetClassName(v8::String::New("Test_InstanceCostructor")); |
| 830 v8::Local<v8::ObjectTemplate> proto_template = |
| 831 func_template->PrototypeTemplate(); |
| 832 v8::Local<v8::Signature> signature = v8::Signature::New(func_template); |
| 833 proto_template->Set(v8::String::New("fooMethod"), v8::FunctionTemplate::New( |
| 834 &TestApiCallbacks::Callback, data, signature, 0)); |
| 835 |
| 836 v8::Local<v8::Function> func = func_template->GetFunction(); |
| 837 v8::Local<v8::Object> instance = func->NewInstance(); |
| 838 env->Global()->Set(v8::String::New("instance"), instance); |
| 839 |
| 840 v8::Script::Compile(v8::String::New(native_method_test_source))->Run(); |
| 841 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
| 842 env->Global()->Get(v8::String::New("start"))); |
| 843 |
| 844 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
| 845 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); |
| 846 |
| 847 cpu_profiler->StartCpuProfiling(profile_name); |
| 848 int32_t repeat_count = 1; |
| 849 v8::Handle<v8::Value> args[] = { v8::Integer::New(repeat_count) }; |
| 850 function->Call(env->Global(), ARRAY_SIZE(args), args); |
| 851 const v8::CpuProfile* profile = cpu_profiler->StopCpuProfiling(profile_name); |
| 852 |
| 853 CHECK_NE(NULL, profile); |
| 854 // Dump collected profile to have a better diagnostic in case of failure. |
| 855 reinterpret_cast<i::CpuProfile*>( |
| 856 const_cast<v8::CpuProfile*>(profile))->Print(); |
| 857 |
| 858 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
| 859 const v8::CpuProfileNode* startNode = GetChild(root, "start"); |
| 860 GetChild(startNode, "fooMethod"); |
| 861 |
| 862 cpu_profiler->DeleteAllCpuProfiles(); |
| 863 } |
| 864 |
| 865 |
| 866 TEST(NativeMethodMonomorphicIC) { |
| 867 LocalContext env; |
| 868 v8::HandleScope scope(env->GetIsolate()); |
| 869 |
| 870 TestApiCallbacks callbacks(1); |
| 871 v8::Local<v8::External> data = v8::External::New(&callbacks); |
| 872 |
| 873 v8::Local<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); |
| 874 func_template->SetClassName(v8::String::New("Test_InstanceCostructor")); |
| 875 v8::Local<v8::ObjectTemplate> proto_template = |
| 876 func_template->PrototypeTemplate(); |
| 877 v8::Local<v8::Signature> signature = v8::Signature::New(func_template); |
| 878 proto_template->Set(v8::String::New("fooMethod"), v8::FunctionTemplate::New( |
| 879 &TestApiCallbacks::Callback, data, signature, 0)); |
| 880 |
| 881 v8::Local<v8::Function> func = func_template->GetFunction(); |
| 882 v8::Local<v8::Object> instance = func->NewInstance(); |
| 883 env->Global()->Set(v8::String::New("instance"), instance); |
| 884 |
| 885 v8::Script::Compile(v8::String::New(native_method_test_source))->Run(); |
| 886 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( |
| 887 env->Global()->Get(v8::String::New("start"))); |
| 888 { |
| 889 // Make sure method ICs are in monomorphic state before starting |
| 890 // profiling. |
| 891 callbacks.set_warming_up(true); |
| 892 int32_t warm_up_iterations = 3; |
| 893 v8::Handle<v8::Value> args[] = { v8::Integer::New(warm_up_iterations) }; |
| 894 function->Call(env->Global(), ARRAY_SIZE(args), args); |
| 895 callbacks.set_warming_up(false); |
| 896 } |
| 897 |
| 898 v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
| 899 v8::Local<v8::String> profile_name = v8::String::New("my_profile"); |
| 900 |
| 901 cpu_profiler->StartCpuProfiling(profile_name); |
| 902 int32_t repeat_count = 100; |
| 903 v8::Handle<v8::Value> args[] = { v8::Integer::New(repeat_count) }; |
| 904 function->Call(env->Global(), ARRAY_SIZE(args), args); |
| 905 const v8::CpuProfile* profile = cpu_profiler->StopCpuProfiling(profile_name); |
| 906 |
| 907 CHECK_NE(NULL, profile); |
| 908 // Dump collected profile to have a better diagnostic in case of failure. |
| 909 reinterpret_cast<i::CpuProfile*>( |
| 910 const_cast<v8::CpuProfile*>(profile))->Print(); |
| 911 |
| 912 const v8::CpuProfileNode* root = profile->GetTopDownRoot(); |
| 913 GetChild(root, "start"); |
| 914 // TODO(yurys): in CallIC should be changed to report external callback |
| 915 // invocation. |
| 916 // GetChild(startNode, "fooMethod"); |
| 917 |
| 918 cpu_profiler->DeleteAllCpuProfiles(); |
| 919 } |
OLD | NEW |