SwapSpace::SwapSpace(int fd, size_t initial_size)
: fd_(fd),
size_(0),
lock_("SwapSpace lock", static_cast<LockLevel>(LockLevel::kDefaultMutexLevel - 1)) { // Assume that the file is unlinked.
InsertChunk(NewFileChunk(initial_size));
}
SwapSpace::~SwapSpace() { // Unmap all mmapped chunks. Nothing should be allocated anymore at // this point, so there should be only full size chunks in free_by_start_. for (const SpaceChunk& chunk : free_by_start_) { if (munmap(chunk.ptr, chunk.size) != 0) {
PLOG(ERROR) << "Failed to unmap swap space chunk at "
<< static_cast<constvoid*>(chunk.ptr) << " size=" << chunk.size;
}
} // All arenas are backed by the same file. Just close the descriptor.
close(fd_);
}
// Check the free list for something that fits. // TODO: Smarter implementation. Global biggest chunk, ... auto it = free_by_start_.empty()
? free_by_size_.end()
: free_by_size_.lower_bound(FreeBySizeEntry { size, free_by_start_.begin() }); if (it != free_by_size_.end()) {
SpaceChunk old_chunk = *it->free_by_start_entry; if (old_chunk.size == size) {
RemoveChunk(it);
} else { // Avoid deallocating and allocating the std::set<> nodes. // This would be much simpler if we could use replace() from Boost.Bimap.
// The free_by_start_ map contains disjoint intervals ordered by the `ptr`. // Shrinking the interval does not affect the ordering.
it->free_by_start_entry->ptr += size;
it->free_by_start_entry->size -= size;
auto node = free_by_size_.extract(it);
node.value().size -= size;
free_by_size_.insert(std::move(node));
} return old_chunk.ptr;
} else { // Not a big enough free chunk, need to increase file size.
SpaceChunk new_chunk = NewFileChunk(size); if (new_chunk.size != size) { // Insert the remainder.
SpaceChunk remainder = { new_chunk.ptr + size, new_chunk.size - size };
InsertChunk(remainder);
} return new_chunk.ptr;
}
}
SwapSpace::SpaceChunk SwapSpace::NewFileChunk(size_t min_size) { #if !defined(__APPLE__) const size_t page_size = MemMap::GetPageSize();
size_t next_part = std::max(RoundUp(min_size, page_size), RoundUp(kMinimumMapSize, page_size)); int result = TEMP_FAILURE_RETRY(ftruncate64(fd_, size_ + next_part)); if (result != 0) {
PLOG(FATAL) << "Unable to increase swap file.";
}
uint8_t* ptr = reinterpret_cast<uint8_t*>(
mmap(nullptr, next_part, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, size_)); if (ptr == MAP_FAILED) {
LOG(ERROR) << "Unable to mmap new swap file chunk.";
LOG(ERROR) << "Current size: " << size_ << " requested: " << next_part << "/" << min_size;
LOG(ERROR) << "Free list:";
DumpFreeMap(free_by_size_);
LOG(ERROR) << "In free list: " << CollectFree(free_by_start_, free_by_size_);
PLOG(FATAL) << "Unable to mmap new swap file chunk.";
}
size_ += next_part;
SpaceChunk new_chunk = {ptr, next_part}; return new_chunk; #else
UNUSED(min_size, kMinimumMapSize);
LOG(FATAL) << "No swap file support on the Mac.";
UNREACHABLE(); #endif
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.