malloc_extension.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 <opensource@google.com>
  32. //
  33. // Extra extensions exported by some malloc implementations. These
  34. // extensions are accessed through a virtual base class so an
  35. // application can link against a malloc that does not implement these
  36. // extensions, and it will get default versions that do nothing.
  37. //
  38. // NOTE FOR C USERS: If you wish to use this functionality from within
  39. // a C program, see malloc_extension_c.h.
  40. #ifndef BASE_MALLOC_EXTENSION_H_
  41. #define BASE_MALLOC_EXTENSION_H_
  42. #include <stddef.h>
  43. // I can't #include config.h in this public API file, but I should
  44. // really use configure (and make malloc_extension.h a .in file) to
  45. // figure out if the system has stdint.h or not. But I'm lazy, so
  46. // for now I'm assuming it's a problem only with MSVC.
  47. #ifndef _MSC_VER
  48. #include <stdint.h>
  49. #endif
  50. #include <string>
  51. #include <vector>
  52. // Annoying stuff for windows -- makes sure clients can import these functions
  53. #ifndef PERFTOOLS_DLL_DECL
  54. # ifdef _WIN32
  55. # define PERFTOOLS_DLL_DECL __declspec(dllimport)
  56. # else
  57. # define PERFTOOLS_DLL_DECL
  58. # endif
  59. #endif
  60. static const int kMallocHistogramSize = 64;
  61. // One day, we could support other types of writers (perhaps for C?)
  62. typedef std::string MallocExtensionWriter;
  63. namespace base {
  64. struct MallocRange;
  65. }
  66. // Interface to a pluggable system allocator.
  67. class PERFTOOLS_DLL_DECL SysAllocator {
  68. public:
  69. SysAllocator() {
  70. }
  71. virtual ~SysAllocator();
  72. // Allocates "size"-byte of memory from system aligned with "alignment".
  73. // Returns NULL if failed. Otherwise, the returned pointer p up to and
  74. // including (p + actual_size -1) have been allocated.
  75. virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0;
  76. };
  77. // The default implementations of the following routines do nothing.
  78. // All implementations should be thread-safe; the current one
  79. // (TCMallocImplementation) is.
  80. class PERFTOOLS_DLL_DECL MallocExtension {
  81. public:
  82. virtual ~MallocExtension();
  83. // Call this very early in the program execution -- say, in a global
  84. // constructor -- to set up parameters and state needed by all
  85. // instrumented malloc implemenatations. One example: this routine
  86. // sets environemnt variables to tell STL to use libc's malloc()
  87. // instead of doing its own memory management. This is safe to call
  88. // multiple times, as long as each time is before threads start up.
  89. static void Initialize();
  90. // See "verify_memory.h" to see what these routines do
  91. virtual bool VerifyAllMemory();
  92. virtual bool VerifyNewMemory(const void* p);
  93. virtual bool VerifyArrayNewMemory(const void* p);
  94. virtual bool VerifyMallocMemory(const void* p);
  95. virtual bool MallocMemoryStats(int* blocks, size_t* total,
  96. int histogram[kMallocHistogramSize]);
  97. // Get a human readable description of the following malloc data structures.
  98. // - Total inuse memory by application.
  99. // - Free memory(thread, central and page heap),
  100. // - Freelist of central cache, each class.
  101. // - Page heap freelist.
  102. // The state is stored as a null-terminated string
  103. // in a prefix of "buffer[0,buffer_length-1]".
  104. // REQUIRES: buffer_length > 0.
  105. virtual void GetStats(char* buffer, int buffer_length);
  106. // Outputs to "writer" a sample of live objects and the stack traces
  107. // that allocated these objects. The format of the returned output
  108. // is equivalent to the output of the heap profiler and can
  109. // therefore be passed to "pprof". This function is equivalent to
  110. // ReadStackTraces. The main difference is that this function returns
  111. // serialized data appropriately formatted for use by the pprof tool.
  112. // NOTE: by default, tcmalloc does not do any heap sampling, and this
  113. // function will always return an empty sample. To get useful
  114. // data from GetHeapSample, you must also set the environment
  115. // variable TCMALLOC_SAMPLE_PARAMETER to a value such as 524288.
  116. virtual void GetHeapSample(MallocExtensionWriter* writer);
  117. // Outputs to "writer" the stack traces that caused growth in the
  118. // address space size. The format of the returned output is
  119. // equivalent to the output of the heap profiler and can therefore
  120. // be passed to "pprof". This function is equivalent to
  121. // ReadHeapGrowthStackTraces. The main difference is that this function
  122. // returns serialized data appropriately formatted for use by the
  123. // pprof tool. (This does not depend on, or require,
  124. // TCMALLOC_SAMPLE_PARAMETER.)
  125. virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer);
  126. // Invokes func(arg, range) for every controlled memory
  127. // range. *range is filled in with information about the range.
  128. //
  129. // This is a best-effort interface useful only for performance
  130. // analysis. The implementation may not call func at all.
  131. typedef void (RangeFunction)(void*, const base::MallocRange*);
  132. virtual void Ranges(void* arg, RangeFunction func);
  133. // -------------------------------------------------------------------
  134. // Control operations for getting and setting malloc implementation
  135. // specific parameters. Some currently useful properties:
  136. //
  137. // generic
  138. // -------
  139. // "generic.current_allocated_bytes"
  140. // Number of bytes currently allocated by application
  141. // This property is not writable.
  142. //
  143. // "generic.heap_size"
  144. // Number of bytes in the heap ==
  145. // current_allocated_bytes +
  146. // fragmentation +
  147. // freed memory regions
  148. // This property is not writable.
  149. //
  150. // tcmalloc
  151. // --------
  152. // "tcmalloc.max_total_thread_cache_bytes"
  153. // Upper limit on total number of bytes stored across all
  154. // per-thread caches. Default: 16MB.
  155. //
  156. // "tcmalloc.current_total_thread_cache_bytes"
  157. // Number of bytes used across all thread caches.
  158. // This property is not writable.
  159. //
  160. // "tcmalloc.central_cache_free_bytes"
  161. // Number of free bytes in the central cache that have been
  162. // assigned to size classes. They always count towards virtual
  163. // memory usage, and unless the underlying memory is swapped out
  164. // by the OS, they also count towards physical memory usage.
  165. // This property is not writable.
  166. //
  167. // "tcmalloc.transfer_cache_free_bytes"
  168. // Number of free bytes that are waiting to be transfered between
  169. // the central cache and a thread cache. They always count
  170. // towards virtual memory usage, and unless the underlying memory
  171. // is swapped out by the OS, they also count towards physical
  172. // memory usage. This property is not writable.
  173. //
  174. // "tcmalloc.thread_cache_free_bytes"
  175. // Number of free bytes in thread caches. They always count
  176. // towards virtual memory usage, and unless the underlying memory
  177. // is swapped out by the OS, they also count towards physical
  178. // memory usage. This property is not writable.
  179. //
  180. // "tcmalloc.pageheap_free_bytes"
  181. // Number of bytes in free, mapped pages in page heap. These
  182. // bytes can be used to fulfill allocation requests. They
  183. // always count towards virtual memory usage, and unless the
  184. // underlying memory is swapped out by the OS, they also count
  185. // towards physical memory usage. This property is not writable.
  186. //
  187. // "tcmalloc.pageheap_unmapped_bytes"
  188. // Number of bytes in free, unmapped pages in page heap.
  189. // These are bytes that have been released back to the OS,
  190. // possibly by one of the MallocExtension "Release" calls.
  191. // They can be used to fulfill allocation requests, but
  192. // typically incur a page fault. They always count towards
  193. // virtual memory usage, and depending on the OS, typically
  194. // do not count towards physical memory usage. This property
  195. // is not writable.
  196. // -------------------------------------------------------------------
  197. // Get the named "property"'s value. Returns true if the property
  198. // is known. Returns false if the property is not a valid property
  199. // name for the current malloc implementation.
  200. // REQUIRES: property != NULL; value != NULL
  201. virtual bool GetNumericProperty(const char* property, size_t* value);
  202. // Set the named "property"'s value. Returns true if the property
  203. // is known and writable. Returns false if the property is not a
  204. // valid property name for the current malloc implementation, or
  205. // is not writable.
  206. // REQUIRES: property != NULL
  207. virtual bool SetNumericProperty(const char* property, size_t value);
  208. // Mark the current thread as "idle". This routine may optionally
  209. // be called by threads as a hint to the malloc implementation that
  210. // any thread-specific resources should be released. Note: this may
  211. // be an expensive routine, so it should not be called too often.
  212. //
  213. // Also, if the code that calls this routine will go to sleep for
  214. // a while, it should take care to not allocate anything between
  215. // the call to this routine and the beginning of the sleep.
  216. //
  217. // Most malloc implementations ignore this routine.
  218. virtual void MarkThreadIdle();
  219. // Mark the current thread as "busy". This routine should be
  220. // called after MarkThreadIdle() if the thread will now do more
  221. // work. If this method is not called, performance may suffer.
  222. //
  223. // Most malloc implementations ignore this routine.
  224. virtual void MarkThreadBusy();
  225. // Gets the system allocator used by the malloc extension instance. Returns
  226. // NULL for malloc implementations that do not support pluggable system
  227. // allocators.
  228. virtual SysAllocator* GetSystemAllocator();
  229. // Sets the system allocator to the specified.
  230. //
  231. // Users could register their own system allocators for malloc implementation
  232. // that supports pluggable system allocators, such as TCMalloc, by doing:
  233. // alloc = new MyOwnSysAllocator();
  234. // MallocExtension::instance()->SetSystemAllocator(alloc);
  235. // It's up to users whether to fall back (recommended) to the default
  236. // system allocator (use GetSystemAllocator() above) or not. The caller is
  237. // responsible to any necessary locking.
  238. // See tcmalloc/system-alloc.h for the interface and
  239. // tcmalloc/memfs_malloc.cc for the examples.
  240. //
  241. // It's a no-op for malloc implementations that do not support pluggable
  242. // system allocators.
  243. virtual void SetSystemAllocator(SysAllocator *a);
  244. // Try to release num_bytes of free memory back to the operating
  245. // system for reuse. Use this extension with caution -- to get this
  246. // memory back may require faulting pages back in by the OS, and
  247. // that may be slow. (Currently only implemented in tcmalloc.)
  248. virtual void ReleaseToSystem(size_t num_bytes);
  249. // Same as ReleaseToSystem() but release as much memory as possible.
  250. virtual void ReleaseFreeMemory();
  251. // Sets the rate at which we release unused memory to the system.
  252. // Zero means we never release memory back to the system. Increase
  253. // this flag to return memory faster; decrease it to return memory
  254. // slower. Reasonable rates are in the range [0,10]. (Currently
  255. // only implemented in tcmalloc).
  256. virtual void SetMemoryReleaseRate(double rate);
  257. // Gets the release rate. Returns a value < 0 if unknown.
  258. virtual double GetMemoryReleaseRate();
  259. // Returns the estimated number of bytes that will be allocated for
  260. // a request of "size" bytes. This is an estimate: an allocation of
  261. // SIZE bytes may reserve more bytes, but will never reserve less.
  262. // (Currently only implemented in tcmalloc, other implementations
  263. // always return SIZE.)
  264. // This is equivalent to malloc_good_size() in OS X.
  265. virtual size_t GetEstimatedAllocatedSize(size_t size);
  266. // Returns the actual number N of bytes reserved by tcmalloc for the
  267. // pointer p. The client is allowed to use the range of bytes
  268. // [p, p+N) in any way it wishes (i.e. N is the "usable size" of this
  269. // allocation). This number may be equal to or greater than the number
  270. // of bytes requested when p was allocated.
  271. // p must have been allocated by this malloc implementation,
  272. // must not be an interior pointer -- that is, must be exactly
  273. // the pointer returned to by malloc() et al., not some offset
  274. // from that -- and should not have been freed yet. p may be NULL.
  275. // (Currently only implemented in tcmalloc; other implementations
  276. // will return 0.)
  277. // This is equivalent to malloc_size() in OS X, malloc_usable_size()
  278. // in glibc, and _msize() for windows.
  279. virtual size_t GetAllocatedSize(const void* p);
  280. // Returns kOwned if this malloc implementation allocated the memory
  281. // pointed to by p, or kNotOwned if some other malloc implementation
  282. // allocated it or p is NULL. May also return kUnknownOwnership if
  283. // the malloc implementation does not keep track of ownership.
  284. // REQUIRES: p must be a value returned from a previous call to
  285. // malloc(), calloc(), realloc(), memalign(), posix_memalign(),
  286. // valloc(), pvalloc(), new, or new[], and must refer to memory that
  287. // is currently allocated (so, for instance, you should not pass in
  288. // a pointer after having called free() on it).
  289. enum Ownership {
  290. // NOTE: Enum values MUST be kept in sync with the version in
  291. // malloc_extension_c.h
  292. kUnknownOwnership = 0,
  293. kOwned,
  294. kNotOwned
  295. };
  296. virtual Ownership GetOwnership(const void* p);
  297. // The current malloc implementation. Always non-NULL.
  298. static MallocExtension* instance();
  299. // Change the malloc implementation. Typically called by the
  300. // malloc implementation during initialization.
  301. static void Register(MallocExtension* implementation);
  302. // Returns detailed information about malloc's freelists. For each list,
  303. // return a FreeListInfo:
  304. struct FreeListInfo {
  305. size_t min_object_size;
  306. size_t max_object_size;
  307. size_t total_bytes_free;
  308. const char* type;
  309. };
  310. // Each item in the vector refers to a different freelist. The lists
  311. // are identified by the range of allocations that objects in the
  312. // list can satisfy ([min_object_size, max_object_size]) and the
  313. // type of freelist (see below). The current size of the list is
  314. // returned in total_bytes_free (which count against a processes
  315. // resident and virtual size).
  316. //
  317. // Currently supported types are:
  318. //
  319. // "tcmalloc.page{_unmapped}" - tcmalloc's page heap. An entry for each size
  320. // class in the page heap is returned. Bytes in "page_unmapped"
  321. // are no longer backed by physical memory and do not count against
  322. // the resident size of a process.
  323. //
  324. // "tcmalloc.large{_unmapped}" - tcmalloc's list of objects larger
  325. // than the largest page heap size class. Only one "large"
  326. // entry is returned. There is no upper-bound on the size
  327. // of objects in the large free list; this call returns
  328. // kint64max for max_object_size. Bytes in
  329. // "large_unmapped" are no longer backed by physical memory
  330. // and do not count against the resident size of a process.
  331. //
  332. // "tcmalloc.central" - tcmalloc's central free-list. One entry per
  333. // size-class is returned. Never unmapped.
  334. //
  335. // "debug.free_queue" - free objects queued by the debug allocator
  336. // and not returned to tcmalloc.
  337. //
  338. // "tcmalloc.thread" - tcmalloc's per-thread caches. Never unmapped.
  339. virtual void GetFreeListSizes(std::vector<FreeListInfo>* v);
  340. // Get a list of stack traces of sampled allocation points. Returns
  341. // a pointer to a "new[]-ed" result array, and stores the sample
  342. // period in "sample_period".
  343. //
  344. // The state is stored as a sequence of adjacent entries
  345. // in the returned array. Each entry has the following form:
  346. // uintptr_t count; // Number of objects with following trace
  347. // uintptr_t size; // Total size of objects with following trace
  348. // uintptr_t depth; // Number of PC values in stack trace
  349. // void* stack[depth]; // PC values that form the stack trace
  350. //
  351. // The list of entries is terminated by a "count" of 0.
  352. //
  353. // It is the responsibility of the caller to "delete[]" the returned array.
  354. //
  355. // May return NULL to indicate no results.
  356. //
  357. // This is an internal extension. Callers should use the more
  358. // convenient "GetHeapSample(string*)" method defined above.
  359. virtual void** ReadStackTraces(int* sample_period);
  360. // Like ReadStackTraces(), but returns stack traces that caused growth
  361. // in the address space size.
  362. virtual void** ReadHeapGrowthStackTraces();
  363. // Returns the size in bytes of the calling threads cache.
  364. virtual size_t GetThreadCacheSize();
  365. // Like MarkThreadIdle, but does not destroy the internal data
  366. // structures of the thread cache. When the thread resumes, it wil
  367. // have an empty cache but will not need to pay to reconstruct the
  368. // cache data structures.
  369. virtual void MarkThreadTemporarilyIdle();
  370. };
  371. namespace base {
  372. // Information passed per range. More fields may be added later.
  373. struct MallocRange {
  374. enum Type {
  375. INUSE, // Application is using this range
  376. FREE, // Range is currently free
  377. UNMAPPED, // Backing physical memory has been returned to the OS
  378. UNKNOWN
  379. // More enum values may be added in the future
  380. };
  381. uintptr_t address; // Address of range
  382. size_t length; // Byte length of range
  383. Type type; // Type of this range
  384. double fraction; // Fraction of range that is being used (0 if !INUSE)
  385. // Perhaps add the following:
  386. // - stack trace if this range was sampled
  387. // - heap growth stack trace if applicable to this range
  388. // - age when allocated (for inuse) or freed (if not in use)
  389. };
  390. } // namespace base
  391. #endif // BASE_MALLOC_EXTENSION_H_