]>
Commit | Line | Data |
---|---|---|
ba379fdc A |
1 | /* |
2 | * Copyright (C) 2009 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 | #include "config.h" | |
ba379fdc A |
27 | #include "ExecutableAllocator.h" |
28 | ||
81345200 A |
29 | #include "JSCInlines.h" |
30 | ||
4e4e5a6f | 31 | #if ENABLE(EXECUTABLE_ALLOCATOR_FIXED) |
ba379fdc | 32 | |
6fe7ccc8 | 33 | #include "CodeProfiling.h" |
4e4e5a6f | 34 | #include <errno.h> |
ba379fdc | 35 | #include <unistd.h> |
6fe7ccc8 | 36 | #include <wtf/MetaAllocator.h> |
14957cd0 | 37 | #include <wtf/PageReservation.h> |
ba379fdc A |
38 | #include <wtf/VMTags.h> |
39 | ||
93a37866 A |
40 | #if OS(DARWIN) |
41 | #include <sys/mman.h> | |
42 | #endif | |
43 | ||
14957cd0 A |
44 | #if OS(LINUX) |
45 | #include <stdio.h> | |
46 | #endif | |
47 | ||
81345200 | 48 | #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1090 |
93a37866 A |
49 | // MADV_FREE_REUSABLE does not work for JIT memory on older OSes so use MADV_FREE in that case. |
50 | #define WTF_USE_MADV_FREE_FOR_JIT_MEMORY 1 | |
51 | #endif | |
4e4e5a6f | 52 | |
ba379fdc A |
53 | using namespace WTF; |
54 | ||
55 | namespace JSC { | |
14957cd0 | 56 | |
93a37866 | 57 | uintptr_t startOfFixedExecutableMemoryPool; |
ba379fdc | 58 | |
6fe7ccc8 | 59 | class FixedVMPoolExecutableAllocator : public MetaAllocator { |
93a37866 | 60 | WTF_MAKE_FAST_ALLOCATED; |
b80e6193 | 61 | public: |
6fe7ccc8 | 62 | FixedVMPoolExecutableAllocator() |
93a37866 | 63 | : MetaAllocator(jitAllocationGranule) // round up all allocations to 32 bytes |
b80e6193 | 64 | { |
93a37866 | 65 | m_reservation = PageReservation::reserveWithGuardPages(fixedExecutableMemoryPoolSize, OSAllocator::JSJITCodePages, EXECUTABLE_POOL_WRITABLE, true); |
6fe7ccc8 | 66 | if (m_reservation) { |
93a37866 | 67 | ASSERT(m_reservation.size() == fixedExecutableMemoryPoolSize); |
6fe7ccc8 | 68 | addFreshFreeSpace(m_reservation.base(), m_reservation.size()); |
93a37866 A |
69 | |
70 | startOfFixedExecutableMemoryPool = reinterpret_cast<uintptr_t>(m_reservation.base()); | |
ba379fdc | 71 | } |
b80e6193 | 72 | } |
93a37866 A |
73 | |
74 | virtual ~FixedVMPoolExecutableAllocator(); | |
6fe7ccc8 A |
75 | |
76 | protected: | |
81345200 | 77 | virtual void* allocateNewSpace(size_t&) override |
b80e6193 | 78 | { |
6fe7ccc8 A |
79 | // We're operating in a fixed pool, so new allocation is always prohibited. |
80 | return 0; | |
b80e6193 | 81 | } |
6fe7ccc8 | 82 | |
81345200 | 83 | virtual void notifyNeedPage(void* page) override |
b80e6193 | 84 | { |
93a37866 | 85 | #if USE(MADV_FREE_FOR_JIT_MEMORY) |
6fe7ccc8 A |
86 | UNUSED_PARAM(page); |
87 | #else | |
88 | m_reservation.commit(page, pageSize()); | |
b80e6193 | 89 | #endif |
b80e6193 | 90 | } |
6fe7ccc8 | 91 | |
81345200 | 92 | virtual void notifyPageIsFree(void* page) override |
6fe7ccc8 | 93 | { |
93a37866 | 94 | #if USE(MADV_FREE_FOR_JIT_MEMORY) |
6fe7ccc8 A |
95 | for (;;) { |
96 | int result = madvise(page, pageSize(), MADV_FREE); | |
97 | if (!result) | |
98 | return; | |
99 | ASSERT(result == -1); | |
100 | if (errno != EAGAIN) { | |
93a37866 | 101 | RELEASE_ASSERT_NOT_REACHED(); // In debug mode, this should be a hard failure. |
6fe7ccc8 | 102 | break; // In release mode, we should just ignore the error - not returning memory to the OS is better than crashing, especially since we _will_ be able to reuse the memory internally anyway. |
ba379fdc | 103 | } |
b80e6193 | 104 | } |
b80e6193 | 105 | #else |
6fe7ccc8 | 106 | m_reservation.decommit(page, pageSize()); |
4e4e5a6f | 107 | #endif |
ba379fdc | 108 | } |
b80e6193 | 109 | |
14957cd0 | 110 | private: |
14957cd0 | 111 | PageReservation m_reservation; |
ba379fdc A |
112 | }; |
113 | ||
6fe7ccc8 | 114 | static FixedVMPoolExecutableAllocator* allocator; |
14957cd0 | 115 | |
6fe7ccc8 A |
116 | void ExecutableAllocator::initializeAllocator() |
117 | { | |
118 | ASSERT(!allocator); | |
119 | allocator = new FixedVMPoolExecutableAllocator(); | |
120 | CodeProfiling::notifyAllocator(allocator); | |
121 | } | |
14957cd0 | 122 | |
93a37866 | 123 | ExecutableAllocator::ExecutableAllocator(VM&) |
14957cd0 | 124 | { |
6fe7ccc8 A |
125 | ASSERT(allocator); |
126 | } | |
14957cd0 | 127 | |
6fe7ccc8 | 128 | ExecutableAllocator::~ExecutableAllocator() |
ba379fdc | 129 | { |
ba379fdc A |
130 | } |
131 | ||
93a37866 A |
132 | FixedVMPoolExecutableAllocator::~FixedVMPoolExecutableAllocator() |
133 | { | |
134 | m_reservation.deallocate(); | |
135 | } | |
136 | ||
4e4e5a6f A |
137 | bool ExecutableAllocator::isValid() const |
138 | { | |
6fe7ccc8 | 139 | return !!allocator->bytesReserved(); |
4e4e5a6f A |
140 | } |
141 | ||
14957cd0 | 142 | bool ExecutableAllocator::underMemoryPressure() |
ba379fdc | 143 | { |
6fe7ccc8 A |
144 | MetaAllocator::Statistics statistics = allocator->currentStatistics(); |
145 | return statistics.bytesAllocated > statistics.bytesReserved / 2; | |
ba379fdc A |
146 | } |
147 | ||
6fe7ccc8 | 148 | double ExecutableAllocator::memoryPressureMultiplier(size_t addedMemoryUsage) |
ba379fdc | 149 | { |
6fe7ccc8 A |
150 | MetaAllocator::Statistics statistics = allocator->currentStatistics(); |
151 | ASSERT(statistics.bytesAllocated <= statistics.bytesReserved); | |
152 | size_t bytesAllocated = statistics.bytesAllocated + addedMemoryUsage; | |
153 | if (bytesAllocated >= statistics.bytesReserved) | |
154 | bytesAllocated = statistics.bytesReserved; | |
155 | double result = 1.0; | |
156 | size_t divisor = statistics.bytesReserved - bytesAllocated; | |
157 | if (divisor) | |
158 | result = static_cast<double>(statistics.bytesReserved) / divisor; | |
159 | if (result < 1.0) | |
160 | result = 1.0; | |
161 | return result; | |
b80e6193 A |
162 | } |
163 | ||
93a37866 | 164 | PassRefPtr<ExecutableMemoryHandle> ExecutableAllocator::allocate(VM& vm, size_t sizeInBytes, void* ownerUID, JITCompilationEffort effort) |
b80e6193 | 165 | { |
6fe7ccc8 A |
166 | RefPtr<ExecutableMemoryHandle> result = allocator->allocate(sizeInBytes, ownerUID); |
167 | if (!result) { | |
168 | if (effort == JITCompilationCanFail) | |
169 | return result; | |
93a37866 | 170 | releaseExecutableMemory(vm); |
6fe7ccc8 | 171 | result = allocator->allocate(sizeInBytes, ownerUID); |
93a37866 | 172 | RELEASE_ASSERT(result); |
6fe7ccc8 A |
173 | } |
174 | return result.release(); | |
ba379fdc A |
175 | } |
176 | ||
6fe7ccc8 A |
177 | size_t ExecutableAllocator::committedByteCount() |
178 | { | |
179 | return allocator->bytesCommitted(); | |
180 | } | |
181 | ||
182 | #if ENABLE(META_ALLOCATOR_PROFILE) | |
183 | void ExecutableAllocator::dumpProfile() | |
184 | { | |
185 | allocator->dumpProfile(); | |
186 | } | |
187 | #endif | |
188 | ||
ba379fdc A |
189 | } |
190 | ||
14957cd0 | 191 | |
6fe7ccc8 | 192 | #endif // ENABLE(EXECUTABLE_ALLOCATOR_FIXED) |