source: webkit/trunk/Source/JavaScriptCore/jit/ExecutableMemoryHandle.h

Last change on this file was 289590, checked in by [email protected], 3 years ago

[libpas] jit_heap should support the segregated heap
https://bugs.webkit.org/show_bug.cgi?id=235497

Reviewed by Yusuke Suzuki.

Source/bmalloc:

One of the things that libpas provides is a fast implementation of executable memory allocation, which
libpas calls "jit_heap". The initial implementation of this only used libpas's bitfit and large
algorithms. Bitfit was used for smaller objects and large was used for larger objects. The libpas
segregated heap was disabled in jit_heap. This made sense because:

  • Bitfit and large support the shrink() call, where we shrink an allocation in-place. Executable allocation supports this to aid branch compaction (we compact code after we copy it into its final resting place, which is after we allocate the memory - so after we finish compaction, we can only shrink the allocation in-place or not at all).
  • Segregated heaps have some virtual address space overheads, and memory overheads, that the bitfit and large heaps don't have. This happens because segreated heaps will dedicate each page to objects of exactly one size forever.

However, segregated heaps are substantially faster than bitfit heaps. They are faster under serial
allocation thanks to the fact that no locks need to be acquired for a typical allocation or
deallocation. They are even more faster under parallel allocation thanks to thread-local caching.

This patch enables segregated heaps for the smallest jit_heap allocations (<=2000 bytes). The cutoff
threshold is runtime configurable, so it's possible to disable segregated heaps at runtime by setting
the cutoff to 0.

With the 2000 threshold, this appears to be a 0.3% Speedometer2 speed-up.

While there is a theoretical possibility of memory overhead, I haven't seen it. If we are using jump
islands then the virtual memory overhead of segregated heaps will just mean that we use jump islands
more frequently, which would manifest as a performance regression -- but I've not seen any such
regression. So, this disables the segregated heap if the JIT pool size is too small (see changes to
ExecutableAllocator.cpp in JSC).

  • libpas/libpas.xcodeproj/project.pbxproj:
  • libpas/src/libpas/jit_heap.c:

(jit_heap_shrink):

  • libpas/src/libpas/jit_heap_config.c:

(jit_small_segregated_allocate_page):
(jit_small_segregated_create_page_header):
(jit_small_destroy_page_header):
(jit_small_segregated_shared_page_directory_selector):
(jit_small_bitfit_allocate_page):
(jit_small_bitfit_create_page_header):
(jit_medium_bitfit_create_page_header):
(jit_medium_destroy_page_header):
(jit_prepare_to_enumerate):
(jit_small_bitfit_destroy_page_header): Deleted.
(jit_medium_bitfit_destroy_page_header): Deleted.

  • libpas/src/libpas/jit_heap_config.h:

(jit_small_page_header_for_boundary):
(jit_small_boundary_for_page_header):
(jit_medium_page_header_for_boundary):
(jit_medium_boundary_for_page_header):
(jit_heap_config_page_header):
(jit_small_bitfit_page_header_for_boundary): Deleted.
(jit_small_bitfit_boundary_for_page_header): Deleted.
(jit_medium_bitfit_page_header_for_boundary): Deleted.
(jit_medium_bitfit_boundary_for_page_header): Deleted.

  • libpas/src/libpas/jit_heap_config_root_data.h:
  • libpas/src/libpas/pas_bitfit_heap.c:

(pas_bitfit_heap_select_variant):
(pas_bitfit_heap_construct_and_insert_size_class):

  • libpas/src/libpas/pas_bitfit_heap.h:
  • libpas/src/libpas/pas_bitfit_page_config.h:

(pas_bitfit_page_config_is_enabled):

  • libpas/src/libpas/pas_heap_config.h:

(pas_heap_config_segregated_heap_min_align_shift):
(pas_heap_config_segregated_heap_min_align):

  • libpas/src/libpas/pas_segregated_heap.c:

(min_align_for_heap):
(min_object_size_for_heap):
(max_segregated_object_size_for_heap):
(max_bitfit_object_size_for_heap):
(max_object_size_for_heap):
(pas_segregated_heap_ensure_allocator_index):
(pas_segregated_heap_ensure_size_directory_for_size):
(min_object_size_for_heap_config): Deleted.
(max_segregated_object_size_for_heap_config): Deleted.
(max_bitfit_object_size_for_heap_config): Deleted.
(max_object_size_for_heap_config): Deleted.

  • libpas/src/libpas/pas_segregated_heap.h:

(pas_segregated_heap_index_for_size):
(pas_segregated_heap_size_for_index):

  • libpas/src/libpas/pas_segregated_page.c:

(pas_segregated_page_construct):

  • libpas/src/libpas/pas_segregated_page_config.h:

(pas_segregated_page_config_is_enabled):

  • libpas/src/libpas/pas_segregated_page_config_kind.def:
  • libpas/src/libpas/pas_segregated_page_config_kind_and_role.h:
  • libpas/src/libpas/pas_segregated_size_directory.c:

(pas_segregated_size_directory_create):

  • libpas/src/test/IsoHeapChaosTests.cpp:

(std::addAllTests):

  • libpas/src/test/JITHeapTests.cpp:

(std::testAllocationSize):
(addJITHeapTests):

  • libpas/src/test/TestHarness.cpp:

(main):

  • libpas/src/test/ViewCacheTests.cpp: Added.

(std::setupConfig):
(std::testDisableViewCacheUsingBoundForNoViewCache):
(std::testEnableViewCacheAtSomeBoundForNoViewCache):
(addViewCacheTests):

Source/JavaScriptCore:

Disable the jit_heap segregated heap if the executable pool size is too small.

  • jit/ExecutableAllocator.cpp:

(JSC::initializeJITPageReservation):

File size: 3.5 KB
Line 
1/*
2 * Copyright (C) 2021-2022 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if USE(LIBPAS_JIT_HEAP) && ENABLE(JIT)
29#include <wtf/MetaAllocatorPtr.h>
30#include <wtf/ThreadSafeRefCounted.h>
31#else
32#include <wtf/MetaAllocatorHandle.h>
33#endif
34
35namespace JSC {
36
37#if USE(LIBPAS_JIT_HEAP) && ENABLE(JIT)
38class ExecutableMemoryHandle : public ThreadSafeRefCounted<ExecutableMemoryHandle> {
39 WTF_MAKE_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(ExecutableMemoryHandle);
40
41public:
42 using MemoryPtr = MetaAllocatorPtr<WTF::HandleMemoryPtrTag>;
43
44 // Don't call this directly - for proper accounting it's necessary to call
45 // ExecutableAllocator::allocate().
46 JS_EXPORT_PRIVATE static RefPtr<ExecutableMemoryHandle> createImpl(size_t sizeInBytes);
47
48 JS_EXPORT_PRIVATE ~ExecutableMemoryHandle();
49
50 MemoryPtr start() const
51 {
52 return m_start;
53 }
54
55 MemoryPtr end() const
56 {
57 return MemoryPtr::makeFromRawPointer(reinterpret_cast<void*>(endAsInteger()));
58 }
59
60 uintptr_t startAsInteger() const
61 {
62 return m_start.untaggedPtr<uintptr_t>();
63 }
64
65 uintptr_t endAsInteger() const
66 {
67 return startAsInteger() + sizeInBytes();
68 }
69
70 size_t sizeInBytes() const { return m_sizeInBytes; }
71
72 bool containsIntegerAddress(uintptr_t address) const
73 {
74 uintptr_t startAddress = startAsInteger();
75 uintptr_t endAddress = startAddress + sizeInBytes();
76 return address >= startAddress && address < endAddress;
77 }
78
79 bool contains(void* address) const
80 {
81 return containsIntegerAddress(reinterpret_cast<uintptr_t>(address));
82 }
83
84 JS_EXPORT_PRIVATE void shrink(size_t newSizeInBytes);
85
86 void* key() const
87 {
88 return m_start.untaggedPtr();
89 }
90
91 void dump(PrintStream& out) const
92 {
93 out.print(RawPointer(key()));
94 }
95
96private:
97 ExecutableMemoryHandle(MemoryPtr start, size_t sizeInBytes)
98 : m_sizeInBytes(sizeInBytes)
99 , m_start(start)
100 {
101 ASSERT(sizeInBytes == m_sizeInBytes); // executable memory region does not exceed 4GB.
102 }
103
104 unsigned m_sizeInBytes;
105 MemoryPtr m_start;
106};
107#else // not (USE(LIBPAS_JIT_HEAP) && ENABLE(JIT))
108typedef WTF::MetaAllocatorHandle ExecutableMemoryHandle;
109#endif // USE(LIBPAS_JIT_HEAP) && ENABLE(JIT)
110
111} // namespace JSC
112
Note: See TracBrowser for help on using the repository browser.