memfs_malloc.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2007, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * 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.
  18. //
  19. // 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. // Author: Arun Sharma
  32. //
  33. // A tcmalloc system allocator that uses a memory based filesystem such as
  34. // tmpfs or hugetlbfs
  35. //
  36. // Since these only exist on linux, we only register this allocator there.
  37. #ifdef __linux
  38. #include <config.h>
  39. #include <errno.h> // for errno, EINVAL
  40. #include <inttypes.h> // for PRId64
  41. #include <limits.h> // for PATH_MAX
  42. #include <stddef.h> // for size_t, NULL
  43. #ifdef HAVE_STDINT_H
  44. #include <stdint.h> // for int64_t, uintptr_t
  45. #endif
  46. #include <stdio.h> // for snprintf
  47. #include <stdlib.h> // for mkstemp
  48. #include <string.h> // for strerror
  49. #include <sys/mman.h> // for mmap, MAP_FAILED, etc
  50. #include <sys/statfs.h> // for fstatfs, statfs
  51. #include <unistd.h> // for ftruncate, off_t, unlink
  52. #include <new> // for operator new
  53. #include <string>
  54. #include <gperftools/malloc_extension.h>
  55. #include "base/basictypes.h"
  56. #include "base/googleinit.h"
  57. #include "base/sysinfo.h"
  58. #include "internal_logging.h"
  59. // TODO(sanjay): Move the code below into the tcmalloc namespace
  60. using tcmalloc::kLog;
  61. using tcmalloc::kCrash;
  62. using tcmalloc::Log;
  63. using std::string;
  64. DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""),
  65. "Path where hugetlbfs or tmpfs is mounted. The caller is "
  66. "responsible for ensuring that the path is unique and does "
  67. "not conflict with another process");
  68. DEFINE_int64(memfs_malloc_limit_mb,
  69. EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0),
  70. "Limit total allocation size to the "
  71. "specified number of MiB. 0 == no limit.");
  72. DEFINE_bool(memfs_malloc_abort_on_fail,
  73. EnvToBool("TCMALLOC_MEMFS_ABORT_ON_FAIL", false),
  74. "abort() whenever memfs_malloc fails to satisfy an allocation "
  75. "for any reason.");
  76. DEFINE_bool(memfs_malloc_ignore_mmap_fail,
  77. EnvToBool("TCMALLOC_MEMFS_IGNORE_MMAP_FAIL", false),
  78. "Ignore failures from mmap");
  79. DEFINE_bool(memfs_malloc_map_private,
  80. EnvToBool("TCMALLOC_MEMFS_MAP_PRIVATE", false),
  81. "Use MAP_PRIVATE with mmap");
  82. // Hugetlbfs based allocator for tcmalloc
  83. class HugetlbSysAllocator: public SysAllocator {
  84. public:
  85. explicit HugetlbSysAllocator(SysAllocator* fallback)
  86. : failed_(true), // To disable allocator until Initialize() is called.
  87. big_page_size_(0),
  88. hugetlb_fd_(-1),
  89. hugetlb_base_(0),
  90. fallback_(fallback) {
  91. }
  92. void* Alloc(size_t size, size_t *actual_size, size_t alignment);
  93. bool Initialize();
  94. bool failed_; // Whether failed to allocate memory.
  95. private:
  96. void* AllocInternal(size_t size, size_t *actual_size, size_t alignment);
  97. int64 big_page_size_;
  98. int hugetlb_fd_; // file descriptor for hugetlb
  99. off_t hugetlb_base_;
  100. SysAllocator* fallback_; // Default system allocator to fall back to.
  101. };
  102. static union {
  103. char buf[sizeof(HugetlbSysAllocator)];
  104. void *ptr;
  105. } hugetlb_space;
  106. // No locking needed here since we assume that tcmalloc calls
  107. // us with an internal lock held (see tcmalloc/system-alloc.cc).
  108. void* HugetlbSysAllocator::Alloc(size_t size, size_t *actual_size,
  109. size_t alignment) {
  110. if (failed_) {
  111. return fallback_->Alloc(size, actual_size, alignment);
  112. }
  113. // We don't respond to allocation requests smaller than big_page_size_ unless
  114. // the caller is ok to take more than they asked for. Used by MetaDataAlloc.
  115. if (actual_size == NULL && size < big_page_size_) {
  116. return fallback_->Alloc(size, actual_size, alignment);
  117. }
  118. // Enforce huge page alignment. Be careful to deal with overflow.
  119. size_t new_alignment = alignment;
  120. if (new_alignment < big_page_size_) new_alignment = big_page_size_;
  121. size_t aligned_size = ((size + new_alignment - 1) /
  122. new_alignment) * new_alignment;
  123. if (aligned_size < size) {
  124. return fallback_->Alloc(size, actual_size, alignment);
  125. }
  126. void* result = AllocInternal(aligned_size, actual_size, new_alignment);
  127. if (result != NULL) {
  128. return result;
  129. }
  130. Log(kLog, __FILE__, __LINE__,
  131. "HugetlbSysAllocator: (failed, allocated)", failed_, hugetlb_base_);
  132. if (FLAGS_memfs_malloc_abort_on_fail) {
  133. Log(kCrash, __FILE__, __LINE__,
  134. "memfs_malloc_abort_on_fail is set");
  135. }
  136. return fallback_->Alloc(size, actual_size, alignment);
  137. }
  138. void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size,
  139. size_t alignment) {
  140. // Ask for extra memory if alignment > pagesize
  141. size_t extra = 0;
  142. if (alignment > big_page_size_) {
  143. extra = alignment - big_page_size_;
  144. }
  145. // Test if this allocation would put us over the limit.
  146. off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024;
  147. if (limit > 0 && hugetlb_base_ + size + extra > limit) {
  148. // Disable the allocator when there's less than one page left.
  149. if (limit - hugetlb_base_ < big_page_size_) {
  150. Log(kLog, __FILE__, __LINE__, "reached memfs_malloc_limit_mb");
  151. failed_ = true;
  152. }
  153. else {
  154. Log(kLog, __FILE__, __LINE__,
  155. "alloc too large (size, bytes left)", size, limit-hugetlb_base_);
  156. }
  157. return NULL;
  158. }
  159. // This is not needed for hugetlbfs, but needed for tmpfs. Annoyingly
  160. // hugetlbfs returns EINVAL for ftruncate.
  161. int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra);
  162. if (ret != 0 && errno != EINVAL) {
  163. Log(kLog, __FILE__, __LINE__,
  164. "ftruncate failed", strerror(errno));
  165. failed_ = true;
  166. return NULL;
  167. }
  168. // Note: size + extra does not overflow since:
  169. // size + alignment < (1<<NBITS).
  170. // and extra <= alignment
  171. // therefore size + extra < (1<<NBITS)
  172. void *result;
  173. result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
  174. FLAGS_memfs_malloc_map_private ? MAP_PRIVATE : MAP_SHARED,
  175. hugetlb_fd_, hugetlb_base_);
  176. if (result == reinterpret_cast<void*>(MAP_FAILED)) {
  177. if (!FLAGS_memfs_malloc_ignore_mmap_fail) {
  178. Log(kLog, __FILE__, __LINE__,
  179. "mmap failed (size, error)", size + extra, strerror(errno));
  180. failed_ = true;
  181. }
  182. return NULL;
  183. }
  184. uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
  185. // Adjust the return memory so it is aligned
  186. size_t adjust = 0;
  187. if ((ptr & (alignment - 1)) != 0) {
  188. adjust = alignment - (ptr & (alignment - 1));
  189. }
  190. ptr += adjust;
  191. hugetlb_base_ += (size + extra);
  192. if (actual_size) {
  193. *actual_size = size + extra - adjust;
  194. }
  195. return reinterpret_cast<void*>(ptr);
  196. }
  197. bool HugetlbSysAllocator::Initialize() {
  198. char path[PATH_MAX];
  199. const int pathlen = FLAGS_memfs_malloc_path.size();
  200. if (pathlen + 8 > sizeof(path)) {
  201. Log(kCrash, __FILE__, __LINE__, "XX fatal: memfs_malloc_path too long");
  202. return false;
  203. }
  204. memcpy(path, FLAGS_memfs_malloc_path.data(), pathlen);
  205. memcpy(path + pathlen, ".XXXXXX", 8); // Also copies terminating \0
  206. int hugetlb_fd = mkstemp(path);
  207. if (hugetlb_fd == -1) {
  208. Log(kLog, __FILE__, __LINE__,
  209. "warning: unable to create memfs_malloc_path",
  210. path, strerror(errno));
  211. return false;
  212. }
  213. // Cleanup memory on process exit
  214. if (unlink(path) == -1) {
  215. Log(kCrash, __FILE__, __LINE__,
  216. "fatal: error unlinking memfs_malloc_path", path, strerror(errno));
  217. return false;
  218. }
  219. // Use fstatfs to figure out the default page size for memfs
  220. struct statfs sfs;
  221. if (fstatfs(hugetlb_fd, &sfs) == -1) {
  222. Log(kCrash, __FILE__, __LINE__,
  223. "fatal: error fstatfs of memfs_malloc_path", strerror(errno));
  224. return false;
  225. }
  226. int64 page_size = sfs.f_bsize;
  227. hugetlb_fd_ = hugetlb_fd;
  228. big_page_size_ = page_size;
  229. failed_ = false;
  230. return true;
  231. }
  232. REGISTER_MODULE_INITIALIZER(memfs_malloc, {
  233. if (FLAGS_memfs_malloc_path.length()) {
  234. SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator();
  235. HugetlbSysAllocator* hp =
  236. new (hugetlb_space.buf) HugetlbSysAllocator(alloc);
  237. if (hp->Initialize()) {
  238. MallocExtension::instance()->SetSystemAllocator(hp);
  239. }
  240. }
  241. });
  242. #endif /* ifdef __linux */