Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "swap_space.h" |
| 18 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <numeric> |
| 23 | |
Andreas Gampe | 3b7dc35 | 2017-06-06 20:02:03 -0700 | [diff] [blame] | 24 | #include "base/bit_utils.h" |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
| 26 | #include "base/mutex.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 27 | #include "thread-current-inl.h" |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | // The chunk size by which the swap file is increased and mapped. |
| 32 | static constexpr size_t kMininumMapSize = 16 * MB; |
| 33 | |
| 34 | static constexpr bool kCheckFreeMaps = false; |
| 35 | |
| 36 | template <typename FreeBySizeSet> |
| 37 | static void DumpFreeMap(const FreeBySizeSet& free_by_size) { |
| 38 | size_t last_size = static_cast<size_t>(-1); |
| 39 | for (const auto& entry : free_by_size) { |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 40 | if (last_size != entry.size) { |
| 41 | last_size = entry.size; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 42 | LOG(INFO) << "Size " << last_size; |
| 43 | } |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 44 | LOG(INFO) << " 0x" << std::hex << entry.free_by_start_entry->Start() |
| 45 | << " size=" << std::dec << entry.free_by_start_entry->size; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 49 | void SwapSpace::RemoveChunk(FreeBySizeSet::const_iterator free_by_size_pos) { |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 50 | auto free_by_start_pos = free_by_size_pos->free_by_start_entry; |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 51 | free_by_size_.erase(free_by_size_pos); |
| 52 | free_by_start_.erase(free_by_start_pos); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 55 | inline void SwapSpace::InsertChunk(const SpaceChunk& chunk) { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 56 | DCHECK_NE(chunk.size, 0u); |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 57 | auto insert_result = free_by_start_.insert(chunk); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 58 | DCHECK(insert_result.second); |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 59 | free_by_size_.emplace(chunk.size, insert_result.first); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SwapSpace::SwapSpace(int fd, size_t initial_size) |
| 63 | : fd_(fd), |
| 64 | size_(0), |
| 65 | lock_("SwapSpace lock", static_cast<LockLevel>(LockLevel::kDefaultMutexLevel - 1)) { |
| 66 | // Assume that the file is unlinked. |
| 67 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 68 | InsertChunk(NewFileChunk(initial_size)); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | SwapSpace::~SwapSpace() { |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 72 | // Unmap all mmapped chunks. Nothing should be allocated anymore at |
| 73 | // this point, so there should be only full size chunks in free_by_start_. |
| 74 | for (const SpaceChunk& chunk : free_by_start_) { |
| 75 | if (munmap(chunk.ptr, chunk.size) != 0) { |
| 76 | PLOG(ERROR) << "Failed to unmap swap space chunk at " |
| 77 | << static_cast<const void*>(chunk.ptr) << " size=" << chunk.size; |
| 78 | } |
| 79 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 80 | // All arenas are backed by the same file. Just close the descriptor. |
| 81 | close(fd_); |
| 82 | } |
| 83 | |
| 84 | template <typename FreeByStartSet, typename FreeBySizeSet> |
| 85 | static size_t CollectFree(const FreeByStartSet& free_by_start, const FreeBySizeSet& free_by_size) { |
| 86 | if (free_by_start.size() != free_by_size.size()) { |
| 87 | LOG(FATAL) << "Size: " << free_by_start.size() << " vs " << free_by_size.size(); |
| 88 | } |
| 89 | |
| 90 | // Calculate over free_by_size. |
| 91 | size_t sum1 = 0; |
| 92 | for (const auto& entry : free_by_size) { |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 93 | sum1 += entry.free_by_start_entry->size; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Calculate over free_by_start. |
| 97 | size_t sum2 = 0; |
| 98 | for (const auto& entry : free_by_start) { |
| 99 | sum2 += entry.size; |
| 100 | } |
| 101 | |
| 102 | if (sum1 != sum2) { |
| 103 | LOG(FATAL) << "Sum: " << sum1 << " vs " << sum2; |
| 104 | } |
| 105 | return sum1; |
| 106 | } |
| 107 | |
| 108 | void* SwapSpace::Alloc(size_t size) { |
| 109 | MutexLock lock(Thread::Current(), lock_); |
| 110 | size = RoundUp(size, 8U); |
| 111 | |
| 112 | // Check the free list for something that fits. |
| 113 | // TODO: Smarter implementation. Global biggest chunk, ... |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 114 | auto it = free_by_start_.empty() |
| 115 | ? free_by_size_.end() |
| 116 | : free_by_size_.lower_bound(FreeBySizeEntry { size, free_by_start_.begin() }); |
| 117 | if (it != free_by_size_.end()) { |
Vladimir Marko | 22e2223 | 2019-02-05 16:35:01 +0000 | [diff] [blame] | 118 | SpaceChunk old_chunk = *it->free_by_start_entry; |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 119 | if (old_chunk.size == size) { |
| 120 | RemoveChunk(it); |
| 121 | } else { |
Vladimir Marko | 22e2223 | 2019-02-05 16:35:01 +0000 | [diff] [blame] | 122 | // Avoid deallocating and allocating the std::set<> nodes. |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 123 | // This would be much simpler if we could use replace() from Boost.Bimap. |
| 124 | |
| 125 | // The free_by_start_ map contains disjoint intervals ordered by the `ptr`. |
| 126 | // Shrinking the interval does not affect the ordering. |
| 127 | it->free_by_start_entry->ptr += size; |
| 128 | it->free_by_start_entry->size -= size; |
| 129 | |
Vladimir Marko | 22e2223 | 2019-02-05 16:35:01 +0000 | [diff] [blame] | 130 | auto node = free_by_size_.extract(it); |
| 131 | node.value().size -= size; |
| 132 | free_by_size_.insert(std::move(node)); |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 133 | } |
| 134 | return old_chunk.ptr; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 135 | } else { |
| 136 | // Not a big enough free chunk, need to increase file size. |
Vladimir Marko | 3e10702 | 2017-02-22 10:57:03 +0000 | [diff] [blame] | 137 | SpaceChunk new_chunk = NewFileChunk(size); |
| 138 | if (new_chunk.size != size) { |
| 139 | // Insert the remainder. |
| 140 | SpaceChunk remainder = { new_chunk.ptr + size, new_chunk.size - size }; |
| 141 | InsertChunk(remainder); |
| 142 | } |
| 143 | return new_chunk.ptr; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 144 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 147 | SwapSpace::SpaceChunk SwapSpace::NewFileChunk(size_t min_size) { |
Andreas Gampe | bed520b | 2014-12-19 12:04:06 -0800 | [diff] [blame] | 148 | #if !defined(__APPLE__) |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 149 | size_t next_part = std::max(RoundUp(min_size, kPageSize), RoundUp(kMininumMapSize, kPageSize)); |
| 150 | int result = TEMP_FAILURE_RETRY(ftruncate64(fd_, size_ + next_part)); |
| 151 | if (result != 0) { |
| 152 | PLOG(FATAL) << "Unable to increase swap file."; |
| 153 | } |
| 154 | uint8_t* ptr = reinterpret_cast<uint8_t*>( |
| 155 | mmap(nullptr, next_part, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, size_)); |
| 156 | if (ptr == MAP_FAILED) { |
| 157 | LOG(ERROR) << "Unable to mmap new swap file chunk."; |
| 158 | LOG(ERROR) << "Current size: " << size_ << " requested: " << next_part << "/" << min_size; |
| 159 | LOG(ERROR) << "Free list:"; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 160 | DumpFreeMap(free_by_size_); |
| 161 | LOG(ERROR) << "In free list: " << CollectFree(free_by_start_, free_by_size_); |
| 162 | LOG(FATAL) << "Aborting..."; |
| 163 | } |
| 164 | size_ += next_part; |
| 165 | SpaceChunk new_chunk = {ptr, next_part}; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 166 | return new_chunk; |
Andreas Gampe | bed520b | 2014-12-19 12:04:06 -0800 | [diff] [blame] | 167 | #else |
Andreas Gampe | edb157f | 2014-12-22 13:06:44 -0800 | [diff] [blame] | 168 | UNUSED(min_size, kMininumMapSize); |
Andreas Gampe | bed520b | 2014-12-19 12:04:06 -0800 | [diff] [blame] | 169 | LOG(FATAL) << "No swap file support on the Mac."; |
Andreas Gampe | edb157f | 2014-12-22 13:06:44 -0800 | [diff] [blame] | 170 | UNREACHABLE(); |
Andreas Gampe | bed520b | 2014-12-19 12:04:06 -0800 | [diff] [blame] | 171 | #endif |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // TODO: Full coalescing. |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 175 | void SwapSpace::Free(void* ptr, size_t size) { |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 176 | MutexLock lock(Thread::Current(), lock_); |
| 177 | size = RoundUp(size, 8U); |
| 178 | |
| 179 | size_t free_before = 0; |
| 180 | if (kCheckFreeMaps) { |
| 181 | free_before = CollectFree(free_by_start_, free_by_size_); |
| 182 | } |
| 183 | |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 184 | SpaceChunk chunk = { reinterpret_cast<uint8_t*>(ptr), size }; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 185 | auto it = free_by_start_.lower_bound(chunk); |
| 186 | if (it != free_by_start_.begin()) { |
| 187 | auto prev = it; |
| 188 | --prev; |
| 189 | CHECK_LE(prev->End(), chunk.Start()); |
| 190 | if (prev->End() == chunk.Start()) { |
| 191 | // Merge *prev with this chunk. |
| 192 | chunk.size += prev->size; |
| 193 | chunk.ptr -= prev->size; |
| 194 | auto erase_pos = free_by_size_.find(FreeBySizeEntry { prev->size, prev }); |
| 195 | DCHECK(erase_pos != free_by_size_.end()); |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 196 | RemoveChunk(erase_pos); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 197 | // "prev" is invalidated but "it" remains valid. |
| 198 | } |
| 199 | } |
| 200 | if (it != free_by_start_.end()) { |
| 201 | CHECK_LE(chunk.End(), it->Start()); |
| 202 | if (chunk.End() == it->Start()) { |
| 203 | // Merge *it with this chunk. |
| 204 | chunk.size += it->size; |
| 205 | auto erase_pos = free_by_size_.find(FreeBySizeEntry { it->size, it }); |
| 206 | DCHECK(erase_pos != free_by_size_.end()); |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 207 | RemoveChunk(erase_pos); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 208 | // "it" is invalidated but we don't need it anymore. |
| 209 | } |
| 210 | } |
Vladimir Marko | ef9230b | 2015-10-06 13:51:55 +0100 | [diff] [blame] | 211 | InsertChunk(chunk); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 212 | |
| 213 | if (kCheckFreeMaps) { |
| 214 | size_t free_after = CollectFree(free_by_start_, free_by_size_); |
| 215 | |
| 216 | if (free_after != free_before + size) { |
| 217 | DumpFreeMap(free_by_size_); |
| 218 | CHECK_EQ(free_after, free_before + size) << "Should be " << size << " difference from " << free_before; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | } // namespace art |