OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 1766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1777 CHECK_EQ(initial_size, static_cast<int>(CcTest::heap()->SizeOfObjects())); | 1777 CHECK_EQ(initial_size, static_cast<int>(CcTest::heap()->SizeOfObjects())); |
1778 | 1778 |
1779 // Waiting for sweeper threads should not change heap size. | 1779 // Waiting for sweeper threads should not change heap size. |
1780 if (collector->sweeping_in_progress()) { | 1780 if (collector->sweeping_in_progress()) { |
1781 collector->EnsureSweepingCompleted(); | 1781 collector->EnsureSweepingCompleted(); |
1782 } | 1782 } |
1783 CHECK_EQ(initial_size, static_cast<int>(CcTest::heap()->SizeOfObjects())); | 1783 CHECK_EQ(initial_size, static_cast<int>(CcTest::heap()->SizeOfObjects())); |
1784 } | 1784 } |
1785 | 1785 |
1786 | 1786 |
1787 TEST(TestAlignmentCalculations) { | |
1788 // Maximum fill amounts should be consistent. | |
1789 int maximum_double_misalignment = kDoubleSize - kPointerSize; | |
1790 int max_word_fill = Heap::GetMaximumFillToAlign(kWordAligned); | |
1791 CHECK_EQ(0, max_word_fill); | |
1792 int max_double_fill = Heap::GetMaximumFillToAlign(kDoubleAligned); | |
1793 CHECK_EQ(maximum_double_misalignment, max_double_fill); | |
1794 int max_double_unaligned_fill = Heap::GetMaximumFillToAlign(kDoubleUnaligned); | |
1795 CHECK_EQ(maximum_double_misalignment, max_double_unaligned_fill); | |
1796 | |
1797 Address base = reinterpret_cast<Address>(NULL); | |
1798 int fill = 0; | |
1799 | |
1800 // Word alignment never requires fill. | |
1801 fill = Heap::GetFillToAlign(base, kWordAligned); | |
1802 CHECK_EQ(0, fill); | |
1803 fill = Heap::GetFillToAlign(base + kPointerSize, kWordAligned); | |
1804 CHECK_EQ(0, fill); | |
1805 | |
1806 // No fill is required when address is double aligned. | |
1807 fill = Heap::GetFillToAlign(base, kDoubleAligned); | |
1808 CHECK_EQ(0, fill); | |
1809 // Fill is required if address is not double aligned. | |
1810 fill = Heap::GetFillToAlign(base + kPointerSize, kDoubleAligned); | |
1811 CHECK_EQ(maximum_double_misalignment, fill); | |
1812 // kDoubleUnaligned has the opposite fill amounts. | |
1813 fill = Heap::GetFillToAlign(base, kDoubleUnaligned); | |
1814 CHECK_EQ(maximum_double_misalignment, fill); | |
1815 fill = Heap::GetFillToAlign(base + kPointerSize, kDoubleUnaligned); | |
1816 CHECK_EQ(0, fill); | |
1817 } | |
1818 | |
1819 | |
1820 static HeapObject* NewSpaceAllocateAligned(int size, | |
1821 AllocationAlignment alignment) { | |
1822 Heap* heap = CcTest::heap(); | |
1823 AllocationResult allocation = | |
1824 heap->new_space()->AllocateRawAligned(size, alignment); | |
1825 HeapObject* obj = NULL; | |
1826 allocation.To(&obj); | |
1827 heap->CreateFillerObjectAt(obj->address(), size); | |
1828 return obj; | |
1829 } | |
1830 | |
1831 | |
1832 TEST(TestAlignedAllocation) { | |
1833 // Double misalignment is 4 on 32-bit platforms, 0 on 64-bit ones. | |
1834 const intptr_t double_misalignment = kDoubleSize - kPointerSize; | |
1835 if (double_misalignment) { | |
1836 // Allocate a pointer sized object that must be double aligned. | |
1837 Address* top_addr = CcTest::heap()->new_space()->allocation_top_address(); | |
1838 Address start = *top_addr; | |
1839 HeapObject* obj1 = NewSpaceAllocateAligned(kPointerSize, kDoubleAligned); | |
1840 CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment)); | |
1841 // Allocate a second pointer sized object. These two allocations should | |
1842 // cause exactly one filler object to be created. | |
1843 HeapObject* obj2 = NewSpaceAllocateAligned(kPointerSize, kDoubleAligned); | |
1844 CHECK(IsAddressAligned(obj2->address(), kDoubleAlignment)); | |
1845 CHECK_EQ(2 * kPointerSize + double_misalignment, *top_addr - start); | |
1846 // There should be 3 filler objects now (the two HeapObjects we created and | |
1847 // the filler.) | |
1848 CHECK(HeapObject::FromAddress(start)->IsFiller() && | |
1849 HeapObject::FromAddress(start + kPointerSize)->IsFiller() && | |
1850 HeapObject::FromAddress(start + 2 * kPointerSize)->IsFiller()); | |
1851 | |
1852 // Similarly for kDoubleUnaligned. | |
1853 start = *top_addr; | |
1854 obj1 = NewSpaceAllocateAligned(kPointerSize, kDoubleUnaligned); | |
1855 CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment, kPointerSize)); | |
1856 obj2 = NewSpaceAllocateAligned(kPointerSize, kDoubleUnaligned); | |
1857 CHECK(IsAddressAligned(obj2->address(), kDoubleAlignment, kPointerSize)); | |
1858 CHECK_EQ(2 * kPointerSize + double_misalignment, *top_addr - start); | |
1859 CHECK(HeapObject::FromAddress(start)->IsFiller() && | |
1860 HeapObject::FromAddress(start + kPointerSize)->IsFiller() && | |
1861 HeapObject::FromAddress(start + 2 * kPointerSize)->IsFiller()); | |
1862 } | |
1863 } | |
1864 | |
1865 | |
1866 // Force allocation to happen from the free list, at a desired misalignment. | |
1867 static Address SetUpFreeListAllocation(int misalignment) { | |
1868 Heap* heap = CcTest::heap(); | |
1869 OldSpace* old_space = heap->old_space(); | |
1870 Address top = old_space->top(); | |
1871 // First, allocate enough filler to get the linear area into the desired | |
1872 // misalignment. | |
1873 const intptr_t maximum_misalignment = 2 * kPointerSize; | |
1874 const intptr_t maximum_misalignment_mask = maximum_misalignment - 1; | |
1875 intptr_t top_alignment = OffsetFrom(top) & maximum_misalignment_mask; | |
1876 int filler_size = misalignment - static_cast<int>(top_alignment); | |
1877 if (filler_size < 0) filler_size += maximum_misalignment; | |
1878 if (filler_size) { | |
1879 // Create the filler object. | |
1880 AllocationResult allocation = old_space->AllocateRawUnaligned(filler_size); | |
1881 HeapObject* obj = NULL; | |
1882 allocation.To(&obj); | |
1883 heap->CreateFillerObjectAt(obj->address(), filler_size); | |
1884 } | |
1885 top = old_space->top(); | |
1886 old_space->EmptyAllocationInfo(); | |
1887 return top; | |
1888 } | |
1889 | |
1890 | |
1891 static HeapObject* OldSpaceAllocateAligned(int size, | |
1892 AllocationAlignment alignment) { | |
1893 Heap* heap = CcTest::heap(); | |
1894 AllocationResult allocation = | |
1895 heap->old_space()->AllocateRawAligned(size, alignment); | |
1896 HeapObject* obj = NULL; | |
1897 allocation.To(&obj); | |
1898 heap->CreateFillerObjectAt(obj->address(), size); | |
1899 return obj; | |
1900 } | |
1901 | |
1902 | |
1903 // Test the case where allocation must be done from the free list, so filler | |
1904 // may precede or follow the object. | |
1905 TEST(TestAlignedOverAllocation) { | |
1906 // Double misalignment is 4 on 32-bit platforms, 0 on 64-bit ones. | |
1907 const intptr_t double_misalignment = kDoubleSize - kPointerSize; | |
1908 if (double_misalignment) { | |
1909 Address start = SetUpFreeListAllocation(0); | |
1910 HeapObject* obj1 = OldSpaceAllocateAligned(kPointerSize, kDoubleAligned); | |
1911 // The object should be aligned, and a filler object should be created. | |
1912 CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment)); | |
1913 CHECK(HeapObject::FromAddress(start)->IsFiller() && | |
1914 HeapObject::FromAddress(start + kPointerSize)->IsFiller()); | |
1915 // Try the opposite alignment case. | |
1916 start = SetUpFreeListAllocation(kPointerSize); | |
1917 HeapObject* obj2 = OldSpaceAllocateAligned(kPointerSize, kDoubleAligned); | |
1918 CHECK(IsAddressAligned(obj2->address(), kDoubleAlignment)); | |
1919 CHECK(HeapObject::FromAddress(start)->IsFiller() && | |
1920 HeapObject::FromAddress(start + kPointerSize)->IsFiller()); | |
1921 | |
1922 // Similarly for kDoubleUnaligned. | |
1923 start = SetUpFreeListAllocation(0); | |
1924 obj1 = OldSpaceAllocateAligned(kPointerSize, kDoubleUnaligned); | |
1925 // The object should be aligned, and a filler object should be created. | |
1926 CHECK(IsAddressAligned(obj1->address(), kDoubleAlignment, kPointerSize)); | |
1927 CHECK(HeapObject::FromAddress(start)->IsFiller() && | |
1928 HeapObject::FromAddress(start + kPointerSize)->IsFiller()); | |
1929 // Try the opposite alignment case. | |
1930 start = SetUpFreeListAllocation(kPointerSize); | |
1931 obj2 = OldSpaceAllocateAligned(kPointerSize, kDoubleUnaligned); | |
1932 CHECK(IsAddressAligned(obj2->address(), kDoubleAlignment, kPointerSize)); | |
1933 CHECK(HeapObject::FromAddress(start)->IsFiller() && | |
1934 HeapObject::FromAddress(start + kPointerSize)->IsFiller()); | |
1935 } | |
1936 } | |
1937 | |
1938 | |
1939 TEST(TestSizeOfObjectsVsHeapIteratorPrecision) { | 1787 TEST(TestSizeOfObjectsVsHeapIteratorPrecision) { |
1940 CcTest::InitializeVM(); | 1788 CcTest::InitializeVM(); |
1941 HeapIterator iterator(CcTest::heap()); | 1789 HeapIterator iterator(CcTest::heap()); |
1942 intptr_t size_of_objects_1 = CcTest::heap()->SizeOfObjects(); | 1790 intptr_t size_of_objects_1 = CcTest::heap()->SizeOfObjects(); |
1943 intptr_t size_of_objects_2 = 0; | 1791 intptr_t size_of_objects_2 = 0; |
1944 for (HeapObject* obj = iterator.next(); | 1792 for (HeapObject* obj = iterator.next(); |
1945 obj != NULL; | 1793 obj != NULL; |
1946 obj = iterator.next()) { | 1794 obj = iterator.next()) { |
1947 if (!obj->IsFreeSpace()) { | 1795 if (!obj->IsFreeSpace()) { |
1948 size_of_objects_2 += obj->Size(); | 1796 size_of_objects_2 += obj->Size(); |
(...skipping 3754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5703 size_t counter2 = 2000; | 5551 size_t counter2 = 2000; |
5704 tracer->SampleNewSpaceAllocation(time2, counter2); | 5552 tracer->SampleNewSpaceAllocation(time2, counter2); |
5705 size_t bytes = tracer->NewSpaceAllocatedBytesInLast(1000); | 5553 size_t bytes = tracer->NewSpaceAllocatedBytesInLast(1000); |
5706 CHECK_EQ(10000, bytes); | 5554 CHECK_EQ(10000, bytes); |
5707 int time3 = 1000; | 5555 int time3 = 1000; |
5708 size_t counter3 = 30000; | 5556 size_t counter3 = 30000; |
5709 tracer->SampleNewSpaceAllocation(time3, counter3); | 5557 tracer->SampleNewSpaceAllocation(time3, counter3); |
5710 bytes = tracer->NewSpaceAllocatedBytesInLast(100); | 5558 bytes = tracer->NewSpaceAllocatedBytesInLast(100); |
5711 CHECK_EQ((counter3 - counter1) * 100 / (time3 - time1), bytes); | 5559 CHECK_EQ((counter3 - counter1) * 100 / (time3 - time1), bytes); |
5712 } | 5560 } |
OLD | NEW |