OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 | 790 |
791 static HeapObject* AllocateUnaligned(NewSpace* space, int size) { | 791 static HeapObject* AllocateUnaligned(NewSpace* space, int size) { |
792 AllocationResult allocation = space->AllocateRawUnaligned(size); | 792 AllocationResult allocation = space->AllocateRawUnaligned(size); |
793 CHECK(!allocation.IsRetry()); | 793 CHECK(!allocation.IsRetry()); |
794 HeapObject* filler = NULL; | 794 HeapObject* filler = NULL; |
795 CHECK(allocation.To(&filler)); | 795 CHECK(allocation.To(&filler)); |
796 space->heap()->CreateFillerObjectAt(filler->address(), size); | 796 space->heap()->CreateFillerObjectAt(filler->address(), size); |
797 return filler; | 797 return filler; |
798 } | 798 } |
799 | 799 |
800 class Observer : public InlineAllocationObserver { | 800 class Observer : public AllocationObserver { |
801 public: | 801 public: |
802 explicit Observer(intptr_t step_size) | 802 explicit Observer(intptr_t step_size) |
803 : InlineAllocationObserver(step_size), count_(0) {} | 803 : AllocationObserver(step_size), count_(0) {} |
804 | 804 |
805 void Step(int bytes_allocated, Address, size_t) override { count_++; } | 805 void Step(int bytes_allocated, Address, size_t) override { count_++; } |
806 | 806 |
807 int count() const { return count_; } | 807 int count() const { return count_; } |
808 | 808 |
809 private: | 809 private: |
810 int count_; | 810 int count_; |
811 }; | 811 }; |
812 | 812 |
813 | 813 |
814 UNINITIALIZED_TEST(InlineAllocationObserver) { | 814 UNINITIALIZED_TEST(AllocationObserver) { |
815 v8::Isolate::CreateParams create_params; | 815 v8::Isolate::CreateParams create_params; |
816 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 816 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
817 v8::Isolate* isolate = v8::Isolate::New(create_params); | 817 v8::Isolate* isolate = v8::Isolate::New(create_params); |
818 { | 818 { |
819 v8::Isolate::Scope isolate_scope(isolate); | 819 v8::Isolate::Scope isolate_scope(isolate); |
820 v8::HandleScope handle_scope(isolate); | 820 v8::HandleScope handle_scope(isolate); |
821 v8::Context::New(isolate)->Enter(); | 821 v8::Context::New(isolate)->Enter(); |
822 | 822 |
823 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate); | 823 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate); |
824 | 824 |
825 NewSpace* new_space = i_isolate->heap()->new_space(); | 825 NewSpace* new_space = i_isolate->heap()->new_space(); |
826 | 826 |
827 Observer observer1(128); | 827 Observer observer1(128); |
828 new_space->AddInlineAllocationObserver(&observer1); | 828 new_space->AddAllocationObserver(&observer1); |
829 | 829 |
830 // The observer should not get notified if we have only allocated less than | 830 // The observer should not get notified if we have only allocated less than |
831 // 128 bytes. | 831 // 128 bytes. |
832 AllocateUnaligned(new_space, 64); | 832 AllocateUnaligned(new_space, 64); |
833 CHECK_EQ(observer1.count(), 0); | 833 CHECK_EQ(observer1.count(), 0); |
834 | 834 |
835 // The observer should get called when we have allocated exactly 128 bytes. | 835 // The observer should get called when we have allocated exactly 128 bytes. |
836 AllocateUnaligned(new_space, 64); | 836 AllocateUnaligned(new_space, 64); |
837 CHECK_EQ(observer1.count(), 1); | 837 CHECK_EQ(observer1.count(), 1); |
838 | 838 |
839 // Another >128 bytes should get another notification. | 839 // Another >128 bytes should get another notification. |
840 AllocateUnaligned(new_space, 136); | 840 AllocateUnaligned(new_space, 136); |
841 CHECK_EQ(observer1.count(), 2); | 841 CHECK_EQ(observer1.count(), 2); |
842 | 842 |
843 // Allocating a large object should get only one notification. | 843 // Allocating a large object should get only one notification. |
844 AllocateUnaligned(new_space, 1024); | 844 AllocateUnaligned(new_space, 1024); |
845 CHECK_EQ(observer1.count(), 3); | 845 CHECK_EQ(observer1.count(), 3); |
846 | 846 |
847 // Allocating another 2048 bytes in small objects should get 16 | 847 // Allocating another 2048 bytes in small objects should get 16 |
848 // notifications. | 848 // notifications. |
849 for (int i = 0; i < 64; ++i) { | 849 for (int i = 0; i < 64; ++i) { |
850 AllocateUnaligned(new_space, 32); | 850 AllocateUnaligned(new_space, 32); |
851 } | 851 } |
852 CHECK_EQ(observer1.count(), 19); | 852 CHECK_EQ(observer1.count(), 19); |
853 | 853 |
854 // Multiple observers should work. | 854 // Multiple observers should work. |
855 Observer observer2(96); | 855 Observer observer2(96); |
856 new_space->AddInlineAllocationObserver(&observer2); | 856 new_space->AddAllocationObserver(&observer2); |
857 | 857 |
858 AllocateUnaligned(new_space, 2048); | 858 AllocateUnaligned(new_space, 2048); |
859 CHECK_EQ(observer1.count(), 20); | 859 CHECK_EQ(observer1.count(), 20); |
860 CHECK_EQ(observer2.count(), 1); | 860 CHECK_EQ(observer2.count(), 1); |
861 | 861 |
862 AllocateUnaligned(new_space, 104); | 862 AllocateUnaligned(new_space, 104); |
863 CHECK_EQ(observer1.count(), 20); | 863 CHECK_EQ(observer1.count(), 20); |
864 CHECK_EQ(observer2.count(), 2); | 864 CHECK_EQ(observer2.count(), 2); |
865 | 865 |
866 // Callback should stop getting called after an observer is removed. | 866 // Callback should stop getting called after an observer is removed. |
867 new_space->RemoveInlineAllocationObserver(&observer1); | 867 new_space->RemoveAllocationObserver(&observer1); |
868 | 868 |
869 AllocateUnaligned(new_space, 384); | 869 AllocateUnaligned(new_space, 384); |
870 CHECK_EQ(observer1.count(), 20); // no more notifications. | 870 CHECK_EQ(observer1.count(), 20); // no more notifications. |
871 CHECK_EQ(observer2.count(), 3); // this one is still active. | 871 CHECK_EQ(observer2.count(), 3); // this one is still active. |
872 | 872 |
873 // Ensure that PauseInlineAllocationObserversScope work correctly. | 873 // Ensure that PauseInlineAllocationObserversScope work correctly. |
874 AllocateUnaligned(new_space, 48); | 874 AllocateUnaligned(new_space, 48); |
875 CHECK_EQ(observer2.count(), 3); | 875 CHECK_EQ(observer2.count(), 3); |
876 { | 876 { |
877 PauseInlineAllocationObserversScope pause_observers(new_space); | 877 PauseAllocationObserversScope pause_observers(new_space); |
878 CHECK_EQ(observer2.count(), 3); | 878 CHECK_EQ(observer2.count(), 3); |
879 AllocateUnaligned(new_space, 384); | 879 AllocateUnaligned(new_space, 384); |
880 CHECK_EQ(observer2.count(), 3); | 880 CHECK_EQ(observer2.count(), 3); |
881 } | 881 } |
882 CHECK_EQ(observer2.count(), 3); | 882 CHECK_EQ(observer2.count(), 3); |
883 // Coupled with the 48 bytes allocated before the pause, another 48 bytes | 883 // Coupled with the 48 bytes allocated before the pause, another 48 bytes |
884 // allocated here should trigger a notification. | 884 // allocated here should trigger a notification. |
885 AllocateUnaligned(new_space, 48); | 885 AllocateUnaligned(new_space, 48); |
886 CHECK_EQ(observer2.count(), 4); | 886 CHECK_EQ(observer2.count(), 4); |
887 | 887 |
888 new_space->RemoveInlineAllocationObserver(&observer2); | 888 new_space->RemoveAllocationObserver(&observer2); |
889 AllocateUnaligned(new_space, 384); | 889 AllocateUnaligned(new_space, 384); |
890 CHECK_EQ(observer1.count(), 20); | 890 CHECK_EQ(observer1.count(), 20); |
891 CHECK_EQ(observer2.count(), 4); | 891 CHECK_EQ(observer2.count(), 4); |
892 } | 892 } |
893 isolate->Dispose(); | 893 isolate->Dispose(); |
894 } | 894 } |
895 | 895 |
896 | 896 |
897 UNINITIALIZED_TEST(InlineAllocationObserverCadence) { | 897 UNINITIALIZED_TEST(InlineAllocationObserverCadence) { |
898 v8::Isolate::CreateParams create_params; | 898 v8::Isolate::CreateParams create_params; |
899 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 899 create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
900 v8::Isolate* isolate = v8::Isolate::New(create_params); | 900 v8::Isolate* isolate = v8::Isolate::New(create_params); |
901 { | 901 { |
902 v8::Isolate::Scope isolate_scope(isolate); | 902 v8::Isolate::Scope isolate_scope(isolate); |
903 v8::HandleScope handle_scope(isolate); | 903 v8::HandleScope handle_scope(isolate); |
904 v8::Context::New(isolate)->Enter(); | 904 v8::Context::New(isolate)->Enter(); |
905 | 905 |
906 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate); | 906 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate); |
907 | 907 |
908 NewSpace* new_space = i_isolate->heap()->new_space(); | 908 NewSpace* new_space = i_isolate->heap()->new_space(); |
909 | 909 |
910 Observer observer1(512); | 910 Observer observer1(512); |
911 new_space->AddInlineAllocationObserver(&observer1); | 911 new_space->AddAllocationObserver(&observer1); |
912 Observer observer2(576); | 912 Observer observer2(576); |
913 new_space->AddInlineAllocationObserver(&observer2); | 913 new_space->AddAllocationObserver(&observer2); |
914 | 914 |
915 for (int i = 0; i < 512; ++i) { | 915 for (int i = 0; i < 512; ++i) { |
916 AllocateUnaligned(new_space, 32); | 916 AllocateUnaligned(new_space, 32); |
917 } | 917 } |
918 | 918 |
919 new_space->RemoveInlineAllocationObserver(&observer1); | 919 new_space->RemoveAllocationObserver(&observer1); |
920 new_space->RemoveInlineAllocationObserver(&observer2); | 920 new_space->RemoveAllocationObserver(&observer2); |
921 | 921 |
922 CHECK_EQ(observer1.count(), 32); | 922 CHECK_EQ(observer1.count(), 32); |
923 CHECK_EQ(observer2.count(), 28); | 923 CHECK_EQ(observer2.count(), 28); |
924 } | 924 } |
925 isolate->Dispose(); | 925 isolate->Dispose(); |
926 } | 926 } |
927 | 927 |
928 } // namespace internal | 928 } // namespace internal |
929 } // namespace v8 | 929 } // namespace v8 |
OLD | NEW |