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
108
109
110
111
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "mmappool.h"
#include "common.h"
#include <sys/mman.h>
namespace vespamalloc {
MMapPool::MMapPool()
: _page_size(getpagesize()),
_huge_flags((getenv("VESPA_USE_HUGEPAGES") != nullptr) ? MAP_HUGETLB : 0),
_count(0),
_mutex(),
_mappings()
{
}
MMapPool::~MMapPool() {
ASSERT_STACKTRACE(_mappings.empty());
}
size_t
MMapPool::getNumMappings() const {
std::lock_guard guard(_mutex);
return _mappings.size();
}
size_t
MMapPool::getMmappedBytes() const {
std::lock_guard guard(_mutex);
size_t sum(0);
std::for_each(_mappings.begin(), _mappings.end(), [&sum](const auto & e){ sum += e.second._sz; });
return sum;
}
void *
MMapPool::mmap(size_t sz) {
void * buf(nullptr);
ASSERT_STACKTRACE((sz & (_page_size - 1)) == 0);
if (sz > 0) {
const int flags(MAP_ANON | MAP_PRIVATE);
const int prot(PROT_READ | PROT_WRITE);
size_t mmapId = _count.fetch_add(1);
if (sz >= _G_bigBlockLimit) {
fprintf(_G_logFile, "mmap %ld of size %ld from : ", mmapId, sz);
logStackTrace();
}
buf = ::mmap(nullptr, sz, prot, flags | _huge_flags, -1, 0);
if (buf == MAP_FAILED) {
if (!_has_hugepage_failure_just_happened) {
_has_hugepage_failure_just_happened = true;
}
buf = ::mmap(nullptr, sz, prot, flags, -1, 0);
if (buf == MAP_FAILED) {
fprintf(_G_logFile, "Failed mmaping anonymous of size %ld errno(%d) from : ", sz, errno);
logStackTrace();
abort();
}
} else {
if (_has_hugepage_failure_just_happened) {
_has_hugepage_failure_just_happened = false;
}
}
#ifdef __linux__
if (sz >= _G_bigBlockLimit) {
if (madvise(buf, sz, MADV_DONTDUMP) != 0) {
std::error_code ec(errno, std::system_category());
fprintf(_G_logFile, "Failed madvise(%p, %ld, MADV_DONTDUMP) = '%s'\n", buf, sz,
ec.message().c_str());
}
}
#endif
std::lock_guard guard(_mutex);
auto [it, inserted] = _mappings.insert(std::make_pair(buf, MMapInfo(mmapId, sz)));
ASSERT_STACKTRACE(inserted);
if (sz >= _G_bigBlockLimit) {
size_t sum(0);
std::for_each(_mappings.begin(), _mappings.end(), [&sum](const auto & e){ sum += e.second._sz; });
fprintf(_G_logFile, "%ld mappings of accumulated size %ld\n", _mappings.size(), sum);
}
}
return buf;
}
void
MMapPool::unmap(void * ptr) {
size_t sz;
{
std::lock_guard guard(_mutex);
auto found = _mappings.find(ptr);
if (found == _mappings.end()) {
fprintf(_G_logFile, "Not able to unmap %p as it is not registered: ", ptr);
logStackTrace();
abort();
}
sz = found->second._sz;
_mappings.erase(found);
}
int munmap_ok = ::munmap(ptr, sz);
ASSERT_STACKTRACE(munmap_ok == 0);
}
size_t
MMapPool::get_size(void * ptr) const {
std::lock_guard guard(_mutex);
auto found = _mappings.find(ptr);
ASSERT_STACKTRACE(found != _mappings.end());
return found->second._sz;
}
}
|