| OLD | NEW |
| 1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 | 43 |
| 44 #include <stddef.h> | 44 #include <stddef.h> |
| 45 // I can't #include config.h in this public API file, but I should | 45 // I can't #include config.h in this public API file, but I should |
| 46 // really use configure (and make malloc_extension.h a .in file) to | 46 // really use configure (and make malloc_extension.h a .in file) to |
| 47 // figure out if the system has stdint.h or not. But I'm lazy, so | 47 // figure out if the system has stdint.h or not. But I'm lazy, so |
| 48 // for now I'm assuming it's a problem only with MSVC. | 48 // for now I'm assuming it's a problem only with MSVC. |
| 49 #ifndef _MSC_VER | 49 #ifndef _MSC_VER |
| 50 #include <stdint.h> | 50 #include <stdint.h> |
| 51 #endif | 51 #endif |
| 52 #include <string> | 52 #include <string> |
| 53 #include <vector> |
| 53 | 54 |
| 54 // Annoying stuff for windows -- makes sure clients can import these functions | 55 // Annoying stuff for windows -- makes sure clients can import these functions |
| 55 #ifndef PERFTOOLS_DLL_DECL | 56 #ifndef PERFTOOLS_DLL_DECL |
| 56 # ifdef _WIN32 | 57 # ifdef _WIN32 |
| 57 # define PERFTOOLS_DLL_DECL __declspec(dllimport) | 58 # define PERFTOOLS_DLL_DECL __declspec(dllimport) |
| 58 # else | 59 # else |
| 59 # define PERFTOOLS_DLL_DECL | 60 # define PERFTOOLS_DLL_DECL |
| 60 # endif | 61 # endif |
| 61 #endif | 62 #endif |
| 62 | 63 |
| 63 static const int kMallocHistogramSize = 64; | 64 static const int kMallocHistogramSize = 64; |
| 64 | 65 |
| 65 // One day, we could support other types of writers (perhaps for C?) | 66 // One day, we could support other types of writers (perhaps for C?) |
| 66 typedef std::string MallocExtensionWriter; | 67 typedef std::string MallocExtensionWriter; |
| 67 | 68 |
| 68 namespace base { | 69 namespace base { |
| 69 struct MallocRange; | 70 struct MallocRange; |
| 70 } | 71 } |
| 71 | 72 |
| 73 // Interface to a pluggable system allocator. |
| 74 class SysAllocator { |
| 75 public: |
| 76 SysAllocator() { |
| 77 } |
| 78 virtual ~SysAllocator(); |
| 79 |
| 80 // Allocates "size"-byte of memory from system aligned with "alignment". |
| 81 // Returns NULL if failed. Otherwise, the returned pointer p up to and |
| 82 // including (p + actual_size -1) have been allocated. |
| 83 virtual void* Alloc(size_t size, size_t *actual_size, size_t alignment) = 0; |
| 84 |
| 85 // Notification that command-line flags have been initialized. |
| 86 virtual void FlagsInitialized() = 0; |
| 87 }; |
| 88 |
| 72 // The default implementations of the following routines do nothing. | 89 // The default implementations of the following routines do nothing. |
| 73 // All implementations should be thread-safe; the current one | 90 // All implementations should be thread-safe; the current one |
| 74 // (TCMallocImplementation) is. | 91 // (TCMallocImplementation) is. |
| 75 class PERFTOOLS_DLL_DECL MallocExtension { | 92 class PERFTOOLS_DLL_DECL MallocExtension { |
| 76 public: | 93 public: |
| 77 virtual ~MallocExtension(); | 94 virtual ~MallocExtension(); |
| 78 | 95 |
| 79 // Call this very early in the program execution -- say, in a global | 96 // Call this very early in the program execution -- say, in a global |
| 80 // constructor -- to set up parameters and state needed by all | 97 // constructor -- to set up parameters and state needed by all |
| 81 // instrumented malloc implemenatations. One example: this routine | 98 // instrumented malloc implemenatations. One example: this routine |
| (...skipping 12 matching lines...) Expand all Loading... |
| 94 | 111 |
| 95 // Get a human readable description of the current state of the malloc | 112 // Get a human readable description of the current state of the malloc |
| 96 // data structures. The state is stored as a null-terminated string | 113 // data structures. The state is stored as a null-terminated string |
| 97 // in a prefix of "buffer[0,buffer_length-1]". | 114 // in a prefix of "buffer[0,buffer_length-1]". |
| 98 // REQUIRES: buffer_length > 0. | 115 // REQUIRES: buffer_length > 0. |
| 99 virtual void GetStats(char* buffer, int buffer_length); | 116 virtual void GetStats(char* buffer, int buffer_length); |
| 100 | 117 |
| 101 // Outputs to "writer" a sample of live objects and the stack traces | 118 // Outputs to "writer" a sample of live objects and the stack traces |
| 102 // that allocated these objects. The format of the returned output | 119 // that allocated these objects. The format of the returned output |
| 103 // is equivalent to the output of the heap profiler and can | 120 // is equivalent to the output of the heap profiler and can |
| 104 // therefore be passed to "pprof". | 121 // therefore be passed to "pprof". This function is equivalent to |
| 122 // ReadStackTraces. The main difference is that this function returns |
| 123 // serialized data appropriately formatted for use by the pprof tool. |
| 124 // NOTE: by default, tcmalloc does not do any heap sampling, and this |
| 125 // function will always return an empty sample. To get useful |
| 126 // data from GetHeapSample, you must also set the environment |
| 127 // variable TCMALLOC_SAMPLE_PARAMETER to a value such as 524288. |
| 105 virtual void GetHeapSample(MallocExtensionWriter* writer); | 128 virtual void GetHeapSample(MallocExtensionWriter* writer); |
| 106 | 129 |
| 107 // Outputs to "writer" the stack traces that caused growth in the | 130 // Outputs to "writer" the stack traces that caused growth in the |
| 108 // address space size. The format of the returned output is | 131 // address space size. The format of the returned output is |
| 109 // equivalent to the output of the heap profiler and can therefore | 132 // equivalent to the output of the heap profiler and can therefore |
| 110 // be passed to "pprof". | 133 // be passed to "pprof". This function is equivalent to |
| 134 // ReadHeapGrowthStackTraces. The main difference is that this function |
| 135 // returns serialized data appropriately formatted for use by the |
| 136 // pprof tool. (This does not depend on, or require, |
| 137 // TCMALLOC_SAMPLE_PARAMETER.) |
| 111 virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer); | 138 virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer); |
| 112 | 139 |
| 113 // Invokes func(arg, range) for every controlled memory | 140 // Invokes func(arg, range) for every controlled memory |
| 114 // range. *range is filled in with information about the range. | 141 // range. *range is filled in with information about the range. |
| 115 // | 142 // |
| 116 // This is a best-effort interface useful only for performance | 143 // This is a best-effort interface useful only for performance |
| 117 // analysis. The implementation may not call func at all. | 144 // analysis. The implementation may not call func at all. |
| 118 typedef void (RangeFunction)(void*, const base::MallocRange*); | 145 typedef void (RangeFunction)(void*, const base::MallocRange*); |
| 119 virtual void Ranges(void* arg, RangeFunction func); | 146 virtual void Ranges(void* arg, RangeFunction func); |
| 120 | 147 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 138 // tcmalloc | 165 // tcmalloc |
| 139 // -------- | 166 // -------- |
| 140 // "tcmalloc.max_total_thread_cache_bytes" | 167 // "tcmalloc.max_total_thread_cache_bytes" |
| 141 // Upper limit on total number of bytes stored across all | 168 // Upper limit on total number of bytes stored across all |
| 142 // per-thread caches. Default: 16MB. | 169 // per-thread caches. Default: 16MB. |
| 143 // | 170 // |
| 144 // "tcmalloc.current_total_thread_cache_bytes" | 171 // "tcmalloc.current_total_thread_cache_bytes" |
| 145 // Number of bytes used across all thread caches. | 172 // Number of bytes used across all thread caches. |
| 146 // This property is not writable. | 173 // This property is not writable. |
| 147 // | 174 // |
| 148 // "tcmalloc.slack_bytes" | |
| 149 // Number of bytes allocated from system, but not currently in | |
| 150 // use by malloced objects. I.e., bytes available for | |
| 151 // allocation without needing more bytes from system. It is | |
| 152 // the sum of pageheap_free_bytes and pageheap_unmapped_bytes. | |
| 153 // This property is not writable. | |
| 154 // | |
| 155 // "tcmalloc.pageheap_free_bytes" | 175 // "tcmalloc.pageheap_free_bytes" |
| 156 // Number of bytes in free, mapped pages in pageheap | 176 // Number of bytes in free, mapped pages in page heap. These |
| 157 // This property is not writable. | 177 // bytes can be used to fulfill allocation requests. They |
| 178 // always count towards virtual memory usage, and unless the |
| 179 // underlying memory is swapped out by the OS, they also count |
| 180 // towards physical memory usage. This property is not writable. |
| 158 // | 181 // |
| 159 // "tcmalloc.pageheap_unmapped_bytes" | 182 // "tcmalloc.pageheap_unmapped_bytes" |
| 160 // Number of bytes in free, unmapped pages in pageheap | 183 // Number of bytes in free, unmapped pages in page heap. |
| 161 // This property is not writable. | 184 // These are bytes that have been released back to the OS, |
| 162 // | 185 // possibly by one of the MallocExtension "Release" calls. |
| 186 // They can be used to fulfill allocation requests, but |
| 187 // typically incur a page fault. They always count towards |
| 188 // virtual memory usage, and depending on the OS, typically |
| 189 // do not count towards physical memory usage. This property |
| 190 // is not writable. |
| 163 // ------------------------------------------------------------------- | 191 // ------------------------------------------------------------------- |
| 164 | 192 |
| 165 // Get the named "property"'s value. Returns true if the property | 193 // Get the named "property"'s value. Returns true if the property |
| 166 // is known. Returns false if the property is not a valid property | 194 // is known. Returns false if the property is not a valid property |
| 167 // name for the current malloc implementation. | 195 // name for the current malloc implementation. |
| 168 // REQUIRES: property != NULL; value != NULL | 196 // REQUIRES: property != NULL; value != NULL |
| 169 virtual bool GetNumericProperty(const char* property, size_t* value); | 197 virtual bool GetNumericProperty(const char* property, size_t* value); |
| 170 | 198 |
| 171 // Set the named "property"'s value. Returns true if the property | 199 // Set the named "property"'s value. Returns true if the property |
| 172 // is known and writable. Returns false if the property is not a | 200 // is known and writable. Returns false if the property is not a |
| (...skipping 14 matching lines...) Expand all Loading... |
| 187 // Most malloc implementations ignore this routine. | 215 // Most malloc implementations ignore this routine. |
| 188 virtual void MarkThreadIdle(); | 216 virtual void MarkThreadIdle(); |
| 189 | 217 |
| 190 // Mark the current thread as "busy". This routine should be | 218 // Mark the current thread as "busy". This routine should be |
| 191 // called after MarkThreadIdle() if the thread will now do more | 219 // called after MarkThreadIdle() if the thread will now do more |
| 192 // work. If this method is not called, performance may suffer. | 220 // work. If this method is not called, performance may suffer. |
| 193 // | 221 // |
| 194 // Most malloc implementations ignore this routine. | 222 // Most malloc implementations ignore this routine. |
| 195 virtual void MarkThreadBusy(); | 223 virtual void MarkThreadBusy(); |
| 196 | 224 |
| 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 |
| 230 // Sets the system allocator to the specified. |
| 231 // |
| 232 // Users could register their own system allocators for malloc implementation |
| 233 // that supports pluggable system allocators, such as TCMalloc, by doing: |
| 234 // alloc = new MyOwnSysAllocator(); |
| 235 // MallocExtension::instance()->SetSystemAllocator(alloc); |
| 236 // It's up to users whether to fall back (recommended) to the default |
| 237 // system allocator (use GetSystemAllocator() above) or not. The caller is |
| 238 // responsible to any necessary locking. |
| 239 // See tcmalloc/system-alloc.h for the interface and |
| 240 // tcmalloc/memfs_malloc.cc for the examples. |
| 241 // |
| 242 // It's a no-op for malloc implementations that do not support pluggable |
| 243 // system allocators. |
| 244 virtual void SetSystemAllocator(SysAllocator *a); |
| 245 |
| 197 // Try to release num_bytes of free memory back to the operating | 246 // Try to release num_bytes of free memory back to the operating |
| 198 // system for reuse. Use this extension with caution -- to get this | 247 // system for reuse. Use this extension with caution -- to get this |
| 199 // memory back may require faulting pages back in by the OS, and | 248 // memory back may require faulting pages back in by the OS, and |
| 200 // that may be slow. (Currently only implemented in tcmalloc.) | 249 // that may be slow. (Currently only implemented in tcmalloc.) |
| 201 virtual void ReleaseToSystem(size_t num_bytes); | 250 virtual void ReleaseToSystem(size_t num_bytes); |
| 202 | 251 |
| 203 // Same as ReleaseToSystem() but release as much memory as possible. | 252 // Same as ReleaseToSystem() but release as much memory as possible. |
| 204 virtual void ReleaseFreeMemory(); | 253 virtual void ReleaseFreeMemory(); |
| 205 | 254 |
| 206 // Sets the rate at which we release unused memory to the system. | 255 // Sets the rate at which we release unused memory to the system. |
| 207 // Zero means we never release memory back to the system. Increase | 256 // Zero means we never release memory back to the system. Increase |
| 208 // this flag to return memory faster; decrease it to return memory | 257 // this flag to return memory faster; decrease it to return memory |
| 209 // slower. Reasonable rates are in the range [0,10]. (Currently | 258 // slower. Reasonable rates are in the range [0,10]. (Currently |
| 210 // only implemented in tcmalloc). | 259 // only implemented in tcmalloc). |
| 211 virtual void SetMemoryReleaseRate(double rate); | 260 virtual void SetMemoryReleaseRate(double rate); |
| 212 | 261 |
| 213 // Gets the release rate. Returns a value < 0 if unknown. | 262 // Gets the release rate. Returns a value < 0 if unknown. |
| 214 virtual double GetMemoryReleaseRate(); | 263 virtual double GetMemoryReleaseRate(); |
| 215 | 264 |
| 216 // Returns the estimated number of bytes that will be allocated for | 265 // Returns the estimated number of bytes that will be allocated for |
| 217 // a request of "size" bytes. This is an estimate: an allocation of | 266 // a request of "size" bytes. This is an estimate: an allocation of |
| 218 // SIZE bytes may reserve more bytes, but will never reserve less. | 267 // SIZE bytes may reserve more bytes, but will never reserve less. |
| 219 // (Currently only implemented in tcmalloc, other implementations | 268 // (Currently only implemented in tcmalloc, other implementations |
| 220 // always return SIZE.) | 269 // always return SIZE.) |
| 270 // This is equivalent to malloc_good_size() in OS X. |
| 221 virtual size_t GetEstimatedAllocatedSize(size_t size); | 271 virtual size_t GetEstimatedAllocatedSize(size_t size); |
| 222 | 272 |
| 223 // Returns the actual number N of bytes reserved by tcmalloc for the | 273 // Returns the actual number N of bytes reserved by tcmalloc for the |
| 224 // pointer p. The client is allowed to use the range of bytes | 274 // pointer p. The client is allowed to use the range of bytes |
| 225 // [p, p+N) in any way it wishes (i.e. N is the "usable size" of this | 275 // [p, p+N) in any way it wishes (i.e. N is the "usable size" of this |
| 226 // allocation). This number may be equal to or greater than the number | 276 // allocation). This number may be equal to or greater than the number |
| 227 // of bytes requested when p was allocated. | 277 // of bytes requested when p was allocated. |
| 228 // p must have been allocated by this malloc implementation, | 278 // p must have been allocated by this malloc implementation, |
| 229 // must not be an interior pointer -- that is, must be exactly | 279 // must not be an interior pointer -- that is, must be exactly |
| 230 // the pointer returned to by malloc() et al., not some offset | 280 // the pointer returned to by malloc() et al., not some offset |
| 231 // from that -- and should not have been freed yet. p may be NULL. | 281 // from that -- and should not have been freed yet. p may be NULL. |
| 232 // (Currently only implemented in tcmalloc; other implementations | 282 // (Currently only implemented in tcmalloc; other implementations |
| 233 // will return 0.) | 283 // will return 0.) |
| 284 // This is equivalent to malloc_size() in OS X, malloc_usable_size() |
| 285 // in glibc, and _msize() for windows. |
| 234 virtual size_t GetAllocatedSize(void* p); | 286 virtual size_t GetAllocatedSize(void* p); |
| 235 | 287 |
| 236 // The current malloc implementation. Always non-NULL. | 288 // The current malloc implementation. Always non-NULL. |
| 237 static MallocExtension* instance(); | 289 static MallocExtension* instance(); |
| 238 | 290 |
| 239 // Change the malloc implementation. Typically called by the | 291 // Change the malloc implementation. Typically called by the |
| 240 // malloc implementation during initialization. | 292 // malloc implementation during initialization. |
| 241 static void Register(MallocExtension* implementation); | 293 static void Register(MallocExtension* implementation); |
| 242 | 294 |
| 243 protected: | 295 // Returns detailed information about malloc's freelists. For each list, |
| 296 // return a FreeListInfo: |
| 297 struct FreeListInfo { |
| 298 size_t min_object_size; |
| 299 size_t max_object_size; |
| 300 size_t total_bytes_free; |
| 301 const char* type; |
| 302 }; |
| 303 // Each item in the vector refers to a different freelist. The lists |
| 304 // are identified by the range of allocations that objects in the |
| 305 // list can satisfy ([min_object_size, max_object_size]) and the |
| 306 // type of freelist (see below). The current size of the list is |
| 307 // returned in total_bytes_free (which count against a processes |
| 308 // resident and virtual size). |
| 309 // |
| 310 // Currently supported types are: |
| 311 // |
| 312 // "tcmalloc.page{_unmapped}" - tcmalloc's page heap. An entry for each size |
| 313 // class in the page heap is returned. Bytes in "page_unmapped" |
| 314 // are no longer backed by physical memory and do not count against |
| 315 // the resident size of a process. |
| 316 // |
| 317 // "tcmalloc.large{_unmapped}" - tcmalloc's list of objects larger |
| 318 // than the largest page heap size class. Only one "large" |
| 319 // entry is returned. There is no upper-bound on the size |
| 320 // of objects in the large free list; this call returns |
| 321 // kint64max for max_object_size. Bytes in |
| 322 // "large_unmapped" are no longer backed by physical memory |
| 323 // and do not count against the resident size of a process. |
| 324 // |
| 325 // "tcmalloc.central" - tcmalloc's central free-list. One entry per |
| 326 // size-class is returned. Never unmapped. |
| 327 // |
| 328 // "debug.free_queue" - free objects queued by the debug allocator |
| 329 // and not returned to tcmalloc. |
| 330 // |
| 331 // "tcmalloc.thread" - tcmalloc's per-thread caches. Never unmapped. |
| 332 virtual void GetFreeListSizes(std::vector<FreeListInfo>* v); |
| 333 |
| 244 // Get a list of stack traces of sampled allocation points. Returns | 334 // Get a list of stack traces of sampled allocation points. Returns |
| 245 // a pointer to a "new[]-ed" result array, and stores the sample | 335 // a pointer to a "new[]-ed" result array, and stores the sample |
| 246 // period in "sample_period". | 336 // period in "sample_period". |
| 247 // | 337 // |
| 248 // The state is stored as a sequence of adjacent entries | 338 // The state is stored as a sequence of adjacent entries |
| 249 // in the returned array. Each entry has the following form: | 339 // in the returned array. Each entry has the following form: |
| 250 // uintptr_t count; // Number of objects with following trace | 340 // uintptr_t count; // Number of objects with following trace |
| 251 // uintptr_t size; // Total size of objects with following trace | 341 // uintptr_t size; // Total size of objects with following trace |
| 252 // uintptr_t depth; // Number of PC values in stack trace | 342 // uintptr_t depth; // Number of PC values in stack trace |
| 253 // void* stack[depth]; // PC values that form the stack trace | 343 // void* stack[depth]; // PC values that form the stack trace |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 | 376 |
| 287 // Perhaps add the following: | 377 // Perhaps add the following: |
| 288 // - stack trace if this range was sampled | 378 // - stack trace if this range was sampled |
| 289 // - heap growth stack trace if applicable to this range | 379 // - heap growth stack trace if applicable to this range |
| 290 // - age when allocated (for inuse) or freed (if not in use) | 380 // - age when allocated (for inuse) or freed (if not in use) |
| 291 }; | 381 }; |
| 292 | 382 |
| 293 } // namespace base | 383 } // namespace base |
| 294 | 384 |
| 295 #endif // BASE_MALLOC_EXTENSION_H_ | 385 #endif // BASE_MALLOC_EXTENSION_H_ |
| OLD | NEW |