blob: ef0ba5cca89f50660ee87165c0c0abcd947cee62 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2007, Google Inc.
3// All rights reserved.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Arun Sharma
33//
34// A tcmalloc system allocator that uses a memory based filesystem such as
35// tmpfs or hugetlbfs
36//
37// Since these only exist on linux, we only register this allocator there.
38
39#ifdef __linux
40
41#include <config.h>
42#include <errno.h> // for errno, EINVAL
43#include <inttypes.h> // for PRId64
44#include <limits.h> // for PATH_MAX
45#include <stddef.h> // for size_t, NULL
46#ifdef HAVE_STDINT_H
47#include <stdint.h> // for int64_t, uintptr_t
48#endif
49#include <stdio.h> // for snprintf
50#include <stdlib.h> // for mkstemp
51#include <string.h> // for strerror
52#include <sys/mman.h> // for mmap, MAP_FAILED, etc
53#include <sys/statfs.h> // for fstatfs, statfs
54#include <unistd.h> // for ftruncate, off_t, unlink
55#include <new> // for operator new
56#include <string>
57
58#include <gperftools/malloc_extension.h>
59#include "base/basictypes.h"
60#include "base/googleinit.h"
61#include "base/sysinfo.h"
62#include "internal_logging.h"
63
64// TODO(sanjay): Move the code below into the tcmalloc namespace
65using tcmalloc::kLog;
66using tcmalloc::kCrash;
67using tcmalloc::Log;
68using std::string;
69
70DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""),
71 "Path where hugetlbfs or tmpfs is mounted. The caller is "
72 "responsible for ensuring that the path is unique and does "
73 "not conflict with another process");
74DEFINE_int64(memfs_malloc_limit_mb,
75 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0),
76 "Limit total allocation size to the "
77 "specified number of MiB. 0 == no limit.");
78DEFINE_bool(memfs_malloc_abort_on_fail,
79 EnvToBool("TCMALLOC_MEMFS_ABORT_ON_FAIL", false),
80 "abort() whenever memfs_malloc fails to satisfy an allocation "
81 "for any reason.");
82DEFINE_bool(memfs_malloc_ignore_mmap_fail,
83 EnvToBool("TCMALLOC_MEMFS_IGNORE_MMAP_FAIL", false),
84 "Ignore failures from mmap");
85DEFINE_bool(memfs_malloc_map_private,
86 EnvToBool("TCMALLOC_MEMFS_MAP_PRIVATE", false),
87 "Use MAP_PRIVATE with mmap");
Brian Silverman20350ac2021-11-17 18:19:55 -080088DEFINE_bool(memfs_malloc_disable_fallback,
89 EnvToBool("TCMALLOC_MEMFS_DISABLE_FALLBACK", false),
90 "If we run out of hugepage memory don't fallback to default "
91 "allocator.");
Austin Schuh745610d2015-09-06 18:19:50 -070092
93// Hugetlbfs based allocator for tcmalloc
94class HugetlbSysAllocator: public SysAllocator {
95public:
96 explicit HugetlbSysAllocator(SysAllocator* fallback)
97 : failed_(true), // To disable allocator until Initialize() is called.
98 big_page_size_(0),
99 hugetlb_fd_(-1),
100 hugetlb_base_(0),
101 fallback_(fallback) {
102 }
103
104 void* Alloc(size_t size, size_t *actual_size, size_t alignment);
105 bool Initialize();
106
107 bool failed_; // Whether failed to allocate memory.
108
109private:
110 void* AllocInternal(size_t size, size_t *actual_size, size_t alignment);
111
112 int64 big_page_size_;
113 int hugetlb_fd_; // file descriptor for hugetlb
114 off_t hugetlb_base_;
115
116 SysAllocator* fallback_; // Default system allocator to fall back to.
117};
Brian Silverman20350ac2021-11-17 18:19:55 -0800118static union {
119 char buf[sizeof(HugetlbSysAllocator)];
120 void *ptr;
121} hugetlb_space;
Austin Schuh745610d2015-09-06 18:19:50 -0700122
123// No locking needed here since we assume that tcmalloc calls
124// us with an internal lock held (see tcmalloc/system-alloc.cc).
125void* HugetlbSysAllocator::Alloc(size_t size, size_t *actual_size,
126 size_t alignment) {
Brian Silverman20350ac2021-11-17 18:19:55 -0800127 if (!FLAGS_memfs_malloc_disable_fallback && failed_) {
Austin Schuh745610d2015-09-06 18:19:50 -0700128 return fallback_->Alloc(size, actual_size, alignment);
129 }
130
131 // We don't respond to allocation requests smaller than big_page_size_ unless
132 // the caller is ok to take more than they asked for. Used by MetaDataAlloc.
Brian Silverman20350ac2021-11-17 18:19:55 -0800133 if (!FLAGS_memfs_malloc_disable_fallback &&
134 actual_size == NULL && size < big_page_size_) {
Austin Schuh745610d2015-09-06 18:19:50 -0700135 return fallback_->Alloc(size, actual_size, alignment);
136 }
137
138 // Enforce huge page alignment. Be careful to deal with overflow.
139 size_t new_alignment = alignment;
140 if (new_alignment < big_page_size_) new_alignment = big_page_size_;
141 size_t aligned_size = ((size + new_alignment - 1) /
142 new_alignment) * new_alignment;
Brian Silverman20350ac2021-11-17 18:19:55 -0800143 if (!FLAGS_memfs_malloc_disable_fallback && aligned_size < size) {
Austin Schuh745610d2015-09-06 18:19:50 -0700144 return fallback_->Alloc(size, actual_size, alignment);
145 }
146
147 void* result = AllocInternal(aligned_size, actual_size, new_alignment);
148 if (result != NULL) {
149 return result;
Brian Silverman20350ac2021-11-17 18:19:55 -0800150 } else if (FLAGS_memfs_malloc_disable_fallback) {
151 return NULL;
Austin Schuh745610d2015-09-06 18:19:50 -0700152 }
153 Log(kLog, __FILE__, __LINE__,
154 "HugetlbSysAllocator: (failed, allocated)", failed_, hugetlb_base_);
155 if (FLAGS_memfs_malloc_abort_on_fail) {
156 Log(kCrash, __FILE__, __LINE__,
157 "memfs_malloc_abort_on_fail is set");
158 }
159 return fallback_->Alloc(size, actual_size, alignment);
160}
161
162void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size,
163 size_t alignment) {
164 // Ask for extra memory if alignment > pagesize
165 size_t extra = 0;
166 if (alignment > big_page_size_) {
167 extra = alignment - big_page_size_;
168 }
169
170 // Test if this allocation would put us over the limit.
171 off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024;
172 if (limit > 0 && hugetlb_base_ + size + extra > limit) {
173 // Disable the allocator when there's less than one page left.
174 if (limit - hugetlb_base_ < big_page_size_) {
175 Log(kLog, __FILE__, __LINE__, "reached memfs_malloc_limit_mb");
176 failed_ = true;
177 }
178 else {
179 Log(kLog, __FILE__, __LINE__,
180 "alloc too large (size, bytes left)", size, limit-hugetlb_base_);
181 }
182 return NULL;
183 }
184
185 // This is not needed for hugetlbfs, but needed for tmpfs. Annoyingly
186 // hugetlbfs returns EINVAL for ftruncate.
187 int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra);
188 if (ret != 0 && errno != EINVAL) {
189 Log(kLog, __FILE__, __LINE__,
190 "ftruncate failed", strerror(errno));
191 failed_ = true;
192 return NULL;
193 }
194
195 // Note: size + extra does not overflow since:
196 // size + alignment < (1<<NBITS).
197 // and extra <= alignment
198 // therefore size + extra < (1<<NBITS)
199 void *result;
200 result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
201 FLAGS_memfs_malloc_map_private ? MAP_PRIVATE : MAP_SHARED,
202 hugetlb_fd_, hugetlb_base_);
203 if (result == reinterpret_cast<void*>(MAP_FAILED)) {
204 if (!FLAGS_memfs_malloc_ignore_mmap_fail) {
205 Log(kLog, __FILE__, __LINE__,
206 "mmap failed (size, error)", size + extra, strerror(errno));
207 failed_ = true;
208 }
209 return NULL;
210 }
211 uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
212
213 // Adjust the return memory so it is aligned
214 size_t adjust = 0;
215 if ((ptr & (alignment - 1)) != 0) {
216 adjust = alignment - (ptr & (alignment - 1));
217 }
218 ptr += adjust;
219 hugetlb_base_ += (size + extra);
220
221 if (actual_size) {
222 *actual_size = size + extra - adjust;
223 }
224
225 return reinterpret_cast<void*>(ptr);
226}
227
228bool HugetlbSysAllocator::Initialize() {
229 char path[PATH_MAX];
230 const int pathlen = FLAGS_memfs_malloc_path.size();
231 if (pathlen + 8 > sizeof(path)) {
232 Log(kCrash, __FILE__, __LINE__, "XX fatal: memfs_malloc_path too long");
233 return false;
234 }
235 memcpy(path, FLAGS_memfs_malloc_path.data(), pathlen);
236 memcpy(path + pathlen, ".XXXXXX", 8); // Also copies terminating \0
237
238 int hugetlb_fd = mkstemp(path);
239 if (hugetlb_fd == -1) {
240 Log(kLog, __FILE__, __LINE__,
241 "warning: unable to create memfs_malloc_path",
242 path, strerror(errno));
243 return false;
244 }
245
246 // Cleanup memory on process exit
247 if (unlink(path) == -1) {
248 Log(kCrash, __FILE__, __LINE__,
249 "fatal: error unlinking memfs_malloc_path", path, strerror(errno));
250 return false;
251 }
252
253 // Use fstatfs to figure out the default page size for memfs
254 struct statfs sfs;
255 if (fstatfs(hugetlb_fd, &sfs) == -1) {
256 Log(kCrash, __FILE__, __LINE__,
257 "fatal: error fstatfs of memfs_malloc_path", strerror(errno));
258 return false;
259 }
260 int64 page_size = sfs.f_bsize;
261
262 hugetlb_fd_ = hugetlb_fd;
263 big_page_size_ = page_size;
264 failed_ = false;
265 return true;
266}
267
268REGISTER_MODULE_INITIALIZER(memfs_malloc, {
269 if (FLAGS_memfs_malloc_path.length()) {
270 SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator();
Brian Silverman20350ac2021-11-17 18:19:55 -0800271 HugetlbSysAllocator* hp =
272 new (hugetlb_space.buf) HugetlbSysAllocator(alloc);
Austin Schuh745610d2015-09-06 18:19:50 -0700273 if (hp->Initialize()) {
274 MallocExtension::instance()->SetSystemAllocator(hp);
275 }
276 }
277});
278
279#endif /* ifdef __linux */