system-alloc.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright (c) 2005, 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: Sanjay Ghemawat
  32. #include <config.h>
  33. #include <errno.h> // for EAGAIN, errno
  34. #ifndef TCMALLOC_SGX
  35. #include <fcntl.h> // for open, O_RDWR
  36. #endif
  37. #include <stddef.h> // for size_t, NULL, ptrdiff_t
  38. #if defined HAVE_STDINT_H
  39. #include <stdint.h> // for uintptr_t, intptr_t
  40. #elif defined HAVE_INTTYPES_H
  41. #include <inttypes.h>
  42. #else
  43. #include <sys/types.h>
  44. #endif
  45. #ifdef HAVE_MMAP
  46. #include <sys/mman.h> // for munmap, mmap, MADV_DONTNEED, etc
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h> // for sbrk, getpagesize, off_t
  50. #endif
  51. #include <new> // for operator new
  52. #include <gperftools/malloc_extension.h>
  53. #include "base/basictypes.h"
  54. #include "base/commandlineflags.h"
  55. #include "base/spinlock.h" // for SpinLockHolder, SpinLock, etc
  56. #include "common.h"
  57. #include "internal_logging.h"
  58. // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old
  59. // form of the name instead.
  60. #ifndef MAP_ANONYMOUS
  61. # define MAP_ANONYMOUS MAP_ANON
  62. #endif
  63. // MADV_FREE is specifically designed for use by malloc(), but only
  64. // FreeBSD supports it; in linux we fall back to the somewhat inferior
  65. // MADV_DONTNEED.
  66. #if !defined(MADV_FREE) && defined(MADV_DONTNEED)
  67. # define MADV_FREE MADV_DONTNEED
  68. #endif
  69. // Solaris has a bug where it doesn't declare madvise() for C++.
  70. // http://www.opensolaris.org/jive/thread.jspa?threadID=21035&tstart=0
  71. #if defined(__sun) && defined(__SVR4)
  72. # include <sys/types.h> // for caddr_t
  73. extern "C" { extern int madvise(caddr_t, size_t, int); }
  74. #endif
  75. // Set kDebugMode mode so that we can have use C++ conditionals
  76. // instead of preprocessor conditionals.
  77. #ifdef NDEBUG
  78. static const bool kDebugMode = false;
  79. #else
  80. static const bool kDebugMode = true;
  81. #endif
  82. // TODO(sanjay): Move the code below into the tcmalloc namespace
  83. using tcmalloc::kLog;
  84. using tcmalloc::Log;
  85. // Anonymous namespace to avoid name conflicts on "CheckAddressBits".
  86. namespace {
  87. // Check that no bit is set at position ADDRESS_BITS or higher.
  88. template <int ADDRESS_BITS> bool CheckAddressBits(uintptr_t ptr) {
  89. return (ptr >> ADDRESS_BITS) == 0;
  90. }
  91. // Specialize for the bit width of a pointer to avoid undefined shift.
  92. template <> bool CheckAddressBits<8 * sizeof(void*)>(uintptr_t ptr) {
  93. return true;
  94. }
  95. } // Anonymous namespace to avoid name conflicts on "CheckAddressBits".
  96. COMPILE_ASSERT(kAddressBits <= 8 * sizeof(void*),
  97. address_bits_larger_than_pointer_size);
  98. static SpinLock spinlock(SpinLock::LINKER_INITIALIZED);
  99. #if defined(HAVE_MMAP) || defined(MADV_FREE)
  100. // Page size is initialized on demand (only needed for mmap-based allocators)
  101. static size_t pagesize = 0;
  102. #endif
  103. // The current system allocator
  104. SysAllocator* sys_alloc = NULL;
  105. // Number of bytes taken from system.
  106. size_t TCMalloc_SystemTaken = 0;
  107. // Configuration parameters.
  108. DEFINE_int32(malloc_devmem_start,
  109. EnvToInt("TCMALLOC_DEVMEM_START", 0),
  110. "Physical memory starting location in MB for /dev/mem allocation."
  111. " Setting this to 0 disables /dev/mem allocation");
  112. DEFINE_int32(malloc_devmem_limit,
  113. EnvToInt("TCMALLOC_DEVMEM_LIMIT", 0),
  114. "Physical memory limit location in MB for /dev/mem allocation."
  115. " Setting this to 0 means no limit.");
  116. DEFINE_bool(malloc_skip_sbrk,
  117. EnvToBool("TCMALLOC_SKIP_SBRK", false),
  118. "Whether sbrk can be used to obtain memory.");
  119. DEFINE_bool(malloc_skip_mmap,
  120. EnvToBool("TCMALLOC_SKIP_MMAP", false),
  121. "Whether mmap can be used to obtain memory.");
  122. DEFINE_bool(malloc_disable_memory_release,
  123. EnvToBool("TCMALLOC_DISABLE_MEMORY_RELEASE", false),
  124. "Whether MADV_FREE/MADV_DONTNEED should be used"
  125. " to return unused memory to the system.");
  126. // static allocators
  127. class SbrkSysAllocator : public SysAllocator {
  128. public:
  129. SbrkSysAllocator() : SysAllocator() {
  130. }
  131. void* Alloc(size_t size, size_t *actual_size, size_t alignment);
  132. };
  133. static union {
  134. char buf[sizeof(SbrkSysAllocator)];
  135. void *ptr;
  136. } sbrk_space;
  137. class MmapSysAllocator : public SysAllocator {
  138. public:
  139. MmapSysAllocator() : SysAllocator() {
  140. }
  141. void* Alloc(size_t size, size_t *actual_size, size_t alignment);
  142. };
  143. #ifndef TCMALLOC_SGX
  144. static union {
  145. char buf[sizeof(MmapSysAllocator)];
  146. void *ptr;
  147. } mmap_space;
  148. #endif
  149. class DevMemSysAllocator : public SysAllocator {
  150. public:
  151. DevMemSysAllocator() : SysAllocator() {
  152. }
  153. void* Alloc(size_t size, size_t *actual_size, size_t alignment);
  154. };
  155. class DefaultSysAllocator : public SysAllocator {
  156. public:
  157. DefaultSysAllocator() : SysAllocator() {
  158. for (int i = 0; i < kMaxAllocators; i++) {
  159. failed_[i] = true;
  160. allocs_[i] = NULL;
  161. names_[i] = NULL;
  162. }
  163. }
  164. void SetChildAllocator(SysAllocator* alloc, unsigned int index,
  165. const char* name) {
  166. if (index < kMaxAllocators && alloc != NULL) {
  167. allocs_[index] = alloc;
  168. failed_[index] = false;
  169. names_[index] = name;
  170. }
  171. }
  172. void* Alloc(size_t size, size_t *actual_size, size_t alignment);
  173. private:
  174. static const int kMaxAllocators = 2;
  175. bool failed_[kMaxAllocators];
  176. SysAllocator* allocs_[kMaxAllocators];
  177. const char* names_[kMaxAllocators];
  178. };
  179. static union {
  180. char buf[sizeof(DefaultSysAllocator)];
  181. void *ptr;
  182. } default_space;
  183. static const char sbrk_name[] = "SbrkSysAllocator";
  184. static const char mmap_name[] = "MmapSysAllocator";
  185. void* SbrkSysAllocator::Alloc(size_t size, size_t *actual_size,
  186. size_t alignment) {
  187. #if !defined(HAVE_SBRK) || defined(__UCLIBC__)
  188. return NULL;
  189. #else
  190. // Check if we should use sbrk allocation.
  191. // FLAGS_malloc_skip_sbrk starts out as false (its uninitialized
  192. // state) and eventually gets initialized to the specified value. Note
  193. // that this code runs for a while before the flags are initialized.
  194. // That means that even if this flag is set to true, some (initial)
  195. // memory will be allocated with sbrk before the flag takes effect.
  196. if (FLAGS_malloc_skip_sbrk) {
  197. return NULL;
  198. }
  199. // sbrk will release memory if passed a negative number, so we do
  200. // a strict check here
  201. if (static_cast<ptrdiff_t>(size + alignment) < 0) return NULL;
  202. // This doesn't overflow because TCMalloc_SystemAlloc has already
  203. // tested for overflow at the alignment boundary.
  204. size = ((size + alignment - 1) / alignment) * alignment;
  205. // "actual_size" indicates that the bytes from the returned pointer
  206. // p up to and including (p + actual_size - 1) have been allocated.
  207. if (actual_size) {
  208. *actual_size = size;
  209. }
  210. // Check that we we're not asking for so much more memory that we'd
  211. // wrap around the end of the virtual address space. (This seems
  212. // like something sbrk() should check for us, and indeed opensolaris
  213. // does, but glibc does not:
  214. // http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/sys/sbrk.c?a=true
  215. // http://sourceware.org/cgi-bin/cvsweb.cgi/~checkout~/libc/misc/sbrk.c?rev=1.1.2.1&content-type=text/plain&cvsroot=glibc
  216. // Without this check, sbrk may succeed when it ought to fail.)
  217. if (reinterpret_cast<intptr_t>(sbrk(0)) + size < size) {
  218. return NULL;
  219. }
  220. void* result = sbrk(size);
  221. if (result == reinterpret_cast<void*>(-1)) {
  222. return NULL;
  223. }
  224. // Is it aligned?
  225. uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
  226. if ((ptr & (alignment-1)) == 0) return result;
  227. // Try to get more memory for alignment
  228. size_t extra = alignment - (ptr & (alignment-1));
  229. void* r2 = sbrk(extra);
  230. if (reinterpret_cast<uintptr_t>(r2) == (ptr + size)) {
  231. // Contiguous with previous result
  232. return reinterpret_cast<void*>(ptr + extra);
  233. }
  234. // Give up and ask for "size + alignment - 1" bytes so
  235. // that we can find an aligned region within it.
  236. result = sbrk(size + alignment - 1);
  237. if (result == reinterpret_cast<void*>(-1)) {
  238. return NULL;
  239. }
  240. ptr = reinterpret_cast<uintptr_t>(result);
  241. if ((ptr & (alignment-1)) != 0) {
  242. ptr += alignment - (ptr & (alignment-1));
  243. }
  244. return reinterpret_cast<void*>(ptr);
  245. #endif // HAVE_SBRK
  246. }
  247. void* MmapSysAllocator::Alloc(size_t size, size_t *actual_size,
  248. size_t alignment) {
  249. #ifndef HAVE_MMAP
  250. return NULL;
  251. #else
  252. // Check if we should use mmap allocation.
  253. // FLAGS_malloc_skip_mmap starts out as false (its uninitialized
  254. // state) and eventually gets initialized to the specified value. Note
  255. // that this code runs for a while before the flags are initialized.
  256. // Chances are we never get here before the flags are initialized since
  257. // sbrk is used until the heap is exhausted (before mmap is used).
  258. if (FLAGS_malloc_skip_mmap) {
  259. return NULL;
  260. }
  261. // Enforce page alignment
  262. if (pagesize == 0) pagesize = getpagesize();
  263. if (alignment < pagesize) alignment = pagesize;
  264. size_t aligned_size = ((size + alignment - 1) / alignment) * alignment;
  265. if (aligned_size < size) {
  266. return NULL;
  267. }
  268. size = aligned_size;
  269. // "actual_size" indicates that the bytes from the returned pointer
  270. // p up to and including (p + actual_size - 1) have been allocated.
  271. if (actual_size) {
  272. *actual_size = size;
  273. }
  274. // Ask for extra memory if alignment > pagesize
  275. size_t extra = 0;
  276. if (alignment > pagesize) {
  277. extra = alignment - pagesize;
  278. }
  279. // Note: size + extra does not overflow since:
  280. // size + alignment < (1<<NBITS).
  281. // and extra <= alignment
  282. // therefore size + extra < (1<<NBITS)
  283. void* result = mmap(NULL, size + extra,
  284. PROT_READ|PROT_WRITE,
  285. MAP_PRIVATE|MAP_ANONYMOUS,
  286. -1, 0);
  287. if (result == reinterpret_cast<void*>(MAP_FAILED)) {
  288. return NULL;
  289. }
  290. // Adjust the return memory so it is aligned
  291. uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
  292. size_t adjust = 0;
  293. if ((ptr & (alignment - 1)) != 0) {
  294. adjust = alignment - (ptr & (alignment - 1));
  295. }
  296. // Return the unused memory to the system
  297. if (adjust > 0) {
  298. munmap(reinterpret_cast<void*>(ptr), adjust);
  299. }
  300. if (adjust < extra) {
  301. munmap(reinterpret_cast<void*>(ptr + adjust + size), extra - adjust);
  302. }
  303. ptr += adjust;
  304. return reinterpret_cast<void*>(ptr);
  305. #endif // HAVE_MMAP
  306. }
  307. void* DevMemSysAllocator::Alloc(size_t size, size_t *actual_size,
  308. size_t alignment) {
  309. #ifndef HAVE_MMAP
  310. return NULL;
  311. #else
  312. static bool initialized = false;
  313. static off_t physmem_base; // next physical memory address to allocate
  314. static off_t physmem_limit; // maximum physical address allowed
  315. static int physmem_fd; // file descriptor for /dev/mem
  316. // Check if we should use /dev/mem allocation. Note that it may take
  317. // a while to get this flag initialized, so meanwhile we fall back to
  318. // the next allocator. (It looks like 7MB gets allocated before
  319. // this flag gets initialized -khr.)
  320. if (FLAGS_malloc_devmem_start == 0) {
  321. // NOTE: not a devmem_failure - we'd like TCMalloc_SystemAlloc to
  322. // try us again next time.
  323. return NULL;
  324. }
  325. if (!initialized) {
  326. physmem_fd = open("/dev/mem", O_RDWR);
  327. if (physmem_fd < 0) {
  328. return NULL;
  329. }
  330. physmem_base = FLAGS_malloc_devmem_start*1024LL*1024LL;
  331. physmem_limit = FLAGS_malloc_devmem_limit*1024LL*1024LL;
  332. initialized = true;
  333. }
  334. // Enforce page alignment
  335. if (pagesize == 0) pagesize = getpagesize();
  336. if (alignment < pagesize) alignment = pagesize;
  337. size_t aligned_size = ((size + alignment - 1) / alignment) * alignment;
  338. if (aligned_size < size) {
  339. return NULL;
  340. }
  341. size = aligned_size;
  342. // "actual_size" indicates that the bytes from the returned pointer
  343. // p up to and including (p + actual_size - 1) have been allocated.
  344. if (actual_size) {
  345. *actual_size = size;
  346. }
  347. // Ask for extra memory if alignment > pagesize
  348. size_t extra = 0;
  349. if (alignment > pagesize) {
  350. extra = alignment - pagesize;
  351. }
  352. // check to see if we have any memory left
  353. if (physmem_limit != 0 &&
  354. ((size + extra) > (physmem_limit - physmem_base))) {
  355. return NULL;
  356. }
  357. // Note: size + extra does not overflow since:
  358. // size + alignment < (1<<NBITS).
  359. // and extra <= alignment
  360. // therefore size + extra < (1<<NBITS)
  361. void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
  362. MAP_SHARED, physmem_fd, physmem_base);
  363. if (result == reinterpret_cast<void*>(MAP_FAILED)) {
  364. return NULL;
  365. }
  366. uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
  367. // Adjust the return memory so it is aligned
  368. size_t adjust = 0;
  369. if ((ptr & (alignment - 1)) != 0) {
  370. adjust = alignment - (ptr & (alignment - 1));
  371. }
  372. // Return the unused virtual memory to the system
  373. if (adjust > 0) {
  374. munmap(reinterpret_cast<void*>(ptr), adjust);
  375. }
  376. if (adjust < extra) {
  377. munmap(reinterpret_cast<void*>(ptr + adjust + size), extra - adjust);
  378. }
  379. ptr += adjust;
  380. physmem_base += adjust + size;
  381. return reinterpret_cast<void*>(ptr);
  382. #endif // HAVE_MMAP
  383. }
  384. void* DefaultSysAllocator::Alloc(size_t size, size_t *actual_size,
  385. size_t alignment) {
  386. for (int i = 0; i < kMaxAllocators; i++) {
  387. if (!failed_[i] && allocs_[i] != NULL) {
  388. void* result = allocs_[i]->Alloc(size, actual_size, alignment);
  389. if (result != NULL) {
  390. return result;
  391. }
  392. failed_[i] = true;
  393. }
  394. }
  395. // After both failed, reset "failed_" to false so that a single failed
  396. // allocation won't make the allocator never work again.
  397. for (int i = 0; i < kMaxAllocators; i++) {
  398. failed_[i] = false;
  399. }
  400. return NULL;
  401. }
  402. ATTRIBUTE_WEAK ATTRIBUTE_NOINLINE
  403. SysAllocator *tc_get_sysalloc_override(SysAllocator *def)
  404. {
  405. return def;
  406. }
  407. static bool system_alloc_inited = false;
  408. void InitSystemAllocators(void) {
  409. #ifndef TCMALLOC_SGX
  410. MmapSysAllocator *mmap = new (mmap_space.buf) MmapSysAllocator();
  411. #endif
  412. SbrkSysAllocator *sbrk = new (sbrk_space.buf) SbrkSysAllocator();
  413. // In 64-bit debug mode, place the mmap allocator first since it
  414. // allocates pointers that do not fit in 32 bits and therefore gives
  415. // us better testing of code's 64-bit correctness. It also leads to
  416. // less false negatives in heap-checking code. (Numbers are less
  417. // likely to look like pointers and therefore the conservative gc in
  418. // the heap-checker is less likely to misinterpret a number as a
  419. // pointer).
  420. DefaultSysAllocator *sdef = new (default_space.buf) DefaultSysAllocator();
  421. if (kDebugMode && sizeof(void*) > 4) {
  422. #ifndef TCMALLOC_SGX
  423. sdef->SetChildAllocator(mmap, 0, mmap_name);
  424. #endif
  425. sdef->SetChildAllocator(sbrk, 1, sbrk_name);
  426. } else {
  427. sdef->SetChildAllocator(sbrk, 0, sbrk_name);
  428. #ifndef TCMALLOC_SGX
  429. sdef->SetChildAllocator(mmap, 1, mmap_name);
  430. #endif
  431. }
  432. sys_alloc = tc_get_sysalloc_override(sdef);
  433. }
  434. void* TCMalloc_SystemAlloc(size_t size, size_t *actual_size,
  435. size_t alignment) {
  436. // Discard requests that overflow
  437. if (size + alignment < size) return NULL;
  438. SpinLockHolder lock_holder(&spinlock);
  439. if (!system_alloc_inited) {
  440. InitSystemAllocators();
  441. system_alloc_inited = true;
  442. }
  443. // Enforce minimum alignment
  444. if (alignment < sizeof(MemoryAligner)) alignment = sizeof(MemoryAligner);
  445. size_t actual_size_storage;
  446. if (actual_size == NULL) {
  447. actual_size = &actual_size_storage;
  448. }
  449. void* result = sys_alloc->Alloc(size, actual_size, alignment);
  450. if (result != NULL) {
  451. CHECK_CONDITION(
  452. CheckAddressBits<kAddressBits>(
  453. reinterpret_cast<uintptr_t>(result) + *actual_size - 1));
  454. TCMalloc_SystemTaken += *actual_size;
  455. }
  456. return result;
  457. }
  458. bool TCMalloc_SystemRelease(void* start, size_t length) {
  459. #ifdef MADV_FREE
  460. if (FLAGS_malloc_devmem_start) {
  461. // It's not safe to use MADV_FREE/MADV_DONTNEED if we've been
  462. // mapping /dev/mem for heap memory.
  463. return false;
  464. }
  465. if (FLAGS_malloc_disable_memory_release) return false;
  466. if (pagesize == 0) pagesize = getpagesize();
  467. const size_t pagemask = pagesize - 1;
  468. size_t new_start = reinterpret_cast<size_t>(start);
  469. size_t end = new_start + length;
  470. size_t new_end = end;
  471. // Round up the starting address and round down the ending address
  472. // to be page aligned:
  473. new_start = (new_start + pagesize - 1) & ~pagemask;
  474. new_end = new_end & ~pagemask;
  475. ASSERT((new_start & pagemask) == 0);
  476. ASSERT((new_end & pagemask) == 0);
  477. ASSERT(new_start >= reinterpret_cast<size_t>(start));
  478. ASSERT(new_end <= end);
  479. if (new_end > new_start) {
  480. int result;
  481. do {
  482. result = madvise(reinterpret_cast<char*>(new_start),
  483. new_end - new_start, MADV_FREE);
  484. } while (result == -1 && errno == EAGAIN);
  485. return result != -1;
  486. }
  487. #endif
  488. return false;
  489. }
  490. void TCMalloc_SystemCommit(void* start, size_t length) {
  491. // Nothing to do here. TCMalloc_SystemRelease does not alter pages
  492. // such that they need to be re-committed before they can be used by the
  493. // application.
  494. }