ClickHouse/src/IO/MMappedFileDescriptor.cpp

108 lines
2.4 KiB
C++
Raw Normal View History

2021-03-26 23:22:51 +00:00
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fmt/format.h>
#include <Common/formatReadable.h>
#include <Common/Exception.h>
2021-10-02 07:13:14 +00:00
#include <base/getPageSize.h>
2021-03-28 19:24:28 +00:00
#include <IO/MMappedFileDescriptor.h>
2021-03-26 23:22:51 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_ALLOCATE_MEMORY;
extern const int CANNOT_MUNMAP;
extern const int CANNOT_STAT;
extern const int BAD_ARGUMENTS;
extern const int LOGICAL_ERROR;
}
static size_t getFileSize(int fd)
{
struct stat stat_res {};
if (0 != fstat(fd, &stat_res))
2021-03-28 19:24:28 +00:00
throwFromErrno("MMappedFileDescriptor: Cannot fstat.", ErrorCodes::CANNOT_STAT);
2021-03-26 23:22:51 +00:00
off_t file_size = stat_res.st_size;
if (file_size < 0)
2021-03-28 19:24:28 +00:00
throw Exception("MMappedFileDescriptor: fstat returned negative file size", ErrorCodes::LOGICAL_ERROR);
2021-03-26 23:22:51 +00:00
return file_size;
}
2021-03-28 19:24:28 +00:00
MMappedFileDescriptor::MMappedFileDescriptor(int fd_, size_t offset_, size_t length_)
2021-03-26 23:22:51 +00:00
{
set(fd_, offset_, length_);
}
2021-03-28 19:24:28 +00:00
MMappedFileDescriptor::MMappedFileDescriptor(int fd_, size_t offset_)
2021-03-26 23:22:51 +00:00
: fd(fd_), offset(offset_)
{
set(fd_, offset_);
}
2021-03-28 19:24:28 +00:00
void MMappedFileDescriptor::set(int fd_, size_t offset_, size_t length_)
2021-03-26 23:22:51 +00:00
{
finish();
fd = fd_;
offset = offset_;
length = length_;
if (!length)
return;
void * buf = mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, offset);
if (MAP_FAILED == buf)
2021-03-28 19:24:28 +00:00
throwFromErrno(fmt::format("MMappedFileDescriptor: Cannot mmap {}.", ReadableSize(length)),
ErrorCodes::CANNOT_ALLOCATE_MEMORY);
data = static_cast<char *>(buf);
files_metric_increment.changeTo(1);
bytes_metric_increment.changeTo(length);
2021-03-26 23:22:51 +00:00
}
2021-03-28 19:24:28 +00:00
void MMappedFileDescriptor::set(int fd_, size_t offset_)
2021-03-26 23:22:51 +00:00
{
size_t file_size = getFileSize(fd_);
if (offset > static_cast<size_t>(file_size))
2021-03-28 19:24:28 +00:00
throw Exception("MMappedFileDescriptor: requested offset is greater than file size", ErrorCodes::BAD_ARGUMENTS);
2021-03-26 23:22:51 +00:00
set(fd_, offset_, file_size - offset);
}
2021-03-28 19:24:28 +00:00
void MMappedFileDescriptor::finish()
2021-03-26 23:22:51 +00:00
{
if (!length)
return;
if (0 != munmap(data, length))
2021-03-28 19:24:28 +00:00
throwFromErrno(fmt::format("MMappedFileDescriptor: Cannot munmap {}.", ReadableSize(length)),
2021-03-26 23:22:51 +00:00
ErrorCodes::CANNOT_MUNMAP);
length = 0;
files_metric_increment.changeTo(0);
bytes_metric_increment.changeTo(0);
2021-03-26 23:22:51 +00:00
}
2021-03-28 19:24:28 +00:00
MMappedFileDescriptor::~MMappedFileDescriptor()
2021-03-26 23:22:51 +00:00
{
finish(); /// Exceptions will lead to std::terminate and that's Ok.
}
}