1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#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>
#include <base/getPageSize.h>
#include <IO/MMappedFileDescriptor.h>
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))
throwFromErrno("MMappedFileDescriptor: Cannot fstat.", ErrorCodes::CANNOT_STAT);
off_t file_size = stat_res.st_size;
if (file_size < 0)
throw Exception(ErrorCodes::LOGICAL_ERROR, "MMappedFileDescriptor: fstat returned negative file size");
return file_size;
}
MMappedFileDescriptor::MMappedFileDescriptor(int fd_, size_t offset_, size_t length_)
{
set(fd_, offset_, length_);
}
MMappedFileDescriptor::MMappedFileDescriptor(int fd_, size_t offset_)
: fd(fd_), offset(offset_)
{
set(fd_, offset_);
}
void MMappedFileDescriptor::set(int fd_, size_t offset_, size_t length_)
{
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)
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);
}
void MMappedFileDescriptor::set(int fd_, size_t offset_)
{
size_t file_size = getFileSize(fd_);
if (offset > static_cast<size_t>(file_size))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "MMappedFileDescriptor: requested offset is greater than file size");
set(fd_, offset_, file_size - offset);
}
void MMappedFileDescriptor::finish()
{
if (!length)
return;
if (0 != munmap(data, length))
throwFromErrno(fmt::format("MMappedFileDescriptor: Cannot munmap {}.", ReadableSize(length)),
ErrorCodes::CANNOT_MUNMAP);
length = 0;
files_metric_increment.changeTo(0);
bytes_metric_increment.changeTo(0);
}
MMappedFileDescriptor::~MMappedFileDescriptor()
{
finish(); /// Exceptions will lead to std::terminate and that's Ok.
}
}
|