OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2005, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 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 |
| 41 #ifndef BASE_MALLOC_EXTENSION_H_ |
| 42 #define BASE_MALLOC_EXTENSION_H_ |
| 43 |
| 44 #include <stddef.h> |
| 45 #include <string> |
| 46 |
| 47 // Annoying stuff for windows -- makes sure clients can import these functions |
| 48 #ifndef PERFTOOLS_DLL_DECL |
| 49 # ifdef _WIN32 |
| 50 # define PERFTOOLS_DLL_DECL __declspec(dllimport) |
| 51 # else |
| 52 # define PERFTOOLS_DLL_DECL |
| 53 # endif |
| 54 #endif |
| 55 |
| 56 static const int kMallocHistogramSize = 64; |
| 57 |
| 58 // One day, we could support other types of writers (perhaps for C?) |
| 59 typedef std::string MallocExtensionWriter; |
| 60 |
| 61 // The default implementations of the following routines do nothing. |
| 62 // All implementations should be thread-safe; the current one |
| 63 // (TCMallocImplementation) is. |
| 64 class PERFTOOLS_DLL_DECL MallocExtension { |
| 65 public: |
| 66 virtual ~MallocExtension(); |
| 67 |
| 68 // Call this very early in the program execution -- say, in a global |
| 69 // constructor -- to set up parameters and state needed by all |
| 70 // instrumented malloc implemenatations. One example: this routine |
| 71 // sets environemnt variables to tell STL to use libc's malloc() |
| 72 // instead of doing its own memory management. This is safe to call |
| 73 // multiple times, as long as each time is before threads start up. |
| 74 static void Initialize(); |
| 75 |
| 76 // See "verify_memory.h" to see what these routines do |
| 77 virtual bool VerifyAllMemory(); |
| 78 virtual bool VerifyNewMemory(void* p); |
| 79 virtual bool VerifyArrayNewMemory(void* p); |
| 80 virtual bool VerifyMallocMemory(void* p); |
| 81 virtual bool MallocMemoryStats(int* blocks, size_t* total, |
| 82 int histogram[kMallocHistogramSize]); |
| 83 |
| 84 // Get a human readable description of the current state of the malloc |
| 85 // data structures. The state is stored as a null-terminated string |
| 86 // in a prefix of "buffer[0,buffer_length-1]". |
| 87 // REQUIRES: buffer_length > 0. |
| 88 virtual void GetStats(char* buffer, int buffer_length); |
| 89 |
| 90 // Outputs to "writer" a sample of live objects and the stack traces |
| 91 // that allocated these objects. The format of the returned output |
| 92 // is equivalent to the output of the heap profiler and can |
| 93 // therefore be passed to "pprof". |
| 94 virtual void GetHeapSample(MallocExtensionWriter* writer); |
| 95 |
| 96 // Outputs to "writer" the stack traces that caused growth in the |
| 97 // address space size. The format of the returned output is |
| 98 // equivalent to the output of the heap profiler and can therefore |
| 99 // be passed to "pprof". |
| 100 virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer); |
| 101 |
| 102 // ------------------------------------------------------------------- |
| 103 // Control operations for getting and setting malloc implementation |
| 104 // specific parameters. Some currently useful properties: |
| 105 // |
| 106 // generic |
| 107 // ------- |
| 108 // "generic.current_allocated_bytes" |
| 109 // Number of bytes currently allocated by application |
| 110 // This property is not writable. |
| 111 // |
| 112 // "generic.heap_size" |
| 113 // Number of bytes in the heap == |
| 114 // current_allocated_bytes + |
| 115 // fragmentation + |
| 116 // freed memory regions |
| 117 // This property is not writable. |
| 118 // |
| 119 // tcmalloc |
| 120 // -------- |
| 121 // "tcmalloc.max_total_thread_cache_bytes" |
| 122 // Upper limit on total number of bytes stored across all |
| 123 // per-thread caches. Default: 16MB. |
| 124 // |
| 125 // "tcmalloc.current_total_thread_cache_bytes" |
| 126 // Number of bytes used across all thread caches. |
| 127 // This property is not writable. |
| 128 // |
| 129 // "tcmalloc.slack_bytes" |
| 130 // Number of bytes allocated from system, but not currently |
| 131 // in use by malloced objects. I.e., bytes available for |
| 132 // allocation without needing more bytes from system. |
| 133 // This property is not writable. |
| 134 // |
| 135 // TODO: Add more properties as necessary |
| 136 // ------------------------------------------------------------------- |
| 137 |
| 138 // Get the named "property"'s value. Returns true if the property |
| 139 // is known. Returns false if the property is not a valid property |
| 140 // name for the current malloc implementation. |
| 141 // REQUIRES: property != NULL; value != NULL |
| 142 virtual bool GetNumericProperty(const char* property, size_t* value); |
| 143 |
| 144 // Set the named "property"'s value. Returns true if the property |
| 145 // is known and writable. Returns false if the property is not a |
| 146 // valid property name for the current malloc implementation, or |
| 147 // is not writable. |
| 148 // REQUIRES: property != NULL |
| 149 virtual bool SetNumericProperty(const char* property, size_t value); |
| 150 |
| 151 // Mark the current thread as "idle". This routine may optionally |
| 152 // be called by threads as a hint to the malloc implementation that |
| 153 // any thread-specific resources should be released. Note: this may |
| 154 // be an expensive routine, so it should not be called too often. |
| 155 // |
| 156 // Also, if the code that calls this routine will go to sleep for |
| 157 // a while, it should take care to not allocate anything between |
| 158 // the call to this routine and the beginning of the sleep. |
| 159 // |
| 160 // Most malloc implementations ignore this routine. |
| 161 virtual void MarkThreadIdle(); |
| 162 |
| 163 // Scavenge at least some resources and free them back to OS. |
| 164 // This method doesn't promise to do anything useful (it might be |
| 165 // implemented as noop), but it's a good idea to invoke it when |
| 166 // application is idle. |
| 167 virtual void Scavenge(); |
| 168 |
| 169 // Try to free memory back to the operating system for reuse. Only |
| 170 // use this extension if the application has recently freed a lot of |
| 171 // memory, and does not anticipate using it again for a long time -- |
| 172 // to get this memory back may require faulting pages back in by the |
| 173 // OS, and that may be slow. (Currently only implemented in |
| 174 // tcmalloc.) |
| 175 virtual void ReleaseFreeMemory(); |
| 176 |
| 177 // Sets the rate at which we release unused memory to the system. |
| 178 // Zero means we never release memory back to the system. Increase |
| 179 // this flag to return memory faster; decrease it to return memory |
| 180 // slower. Reasonable rates are in the range [0,10]. (Currently |
| 181 // only implemented in tcmalloc). |
| 182 virtual void SetMemoryReleaseRate(double rate); |
| 183 |
| 184 // Gets the release rate. Returns a value < 0 if unknown. |
| 185 virtual double GetMemoryReleaseRate(); |
| 186 |
| 187 // Returns the estimated number of bytes that will be allocated for |
| 188 // a request of "size" bytes. This is an estimate: an allocation of |
| 189 // SIZE bytes may reserve more bytes, but will never reserve less. |
| 190 // (Currently only implemented in tcmalloc, other implementations |
| 191 // always return SIZE.) |
| 192 virtual size_t GetEstimatedAllocatedSize(size_t size); |
| 193 |
| 194 // Returns the actual number of bytes reserved by tcmalloc for the |
| 195 // pointer p. This number may be equal to or greater than |
| 196 // the number of bytes requested when p was allocated. |
| 197 // p must have been allocated by this malloc implementation, |
| 198 // must not be an interior pointer -- that is, must be exactly |
| 199 // the pointer returned to by malloc() et al., not some offset |
| 200 // from that -- and should not have been freed yet. p may be NULL. |
| 201 // (Currently only implemented in tcmalloc; other implementations |
| 202 // will return 0.) |
| 203 virtual size_t GetAllocatedSize(void* p); |
| 204 |
| 205 // The current malloc implementation. Always non-NULL. |
| 206 static MallocExtension* instance(); |
| 207 |
| 208 // Change the malloc implementation. Typically called by the |
| 209 // malloc implementation during initialization. |
| 210 static void Register(MallocExtension* implementation); |
| 211 |
| 212 protected: |
| 213 // Get a list of stack traces of sampled allocation points. Returns |
| 214 // a pointer to a "new[]-ed" result array, and stores the sample |
| 215 // period in "sample_period". |
| 216 // |
| 217 // The state is stored as a sequence of adjacent entries |
| 218 // in the returned array. Each entry has the following form: |
| 219 // uintptr_t count; // Number of objects with following trace |
| 220 // uintptr_t size; // Total size of objects with following trace |
| 221 // uintptr_t depth; // Number of PC values in stack trace |
| 222 // void* stack[depth]; // PC values that form the stack trace |
| 223 // |
| 224 // The list of entries is terminated by a "count" of 0. |
| 225 // |
| 226 // It is the responsibility of the caller to "delete[]" the returned array. |
| 227 // |
| 228 // May return NULL to indicate no results. |
| 229 // |
| 230 // This is an internal extension. Callers should use the more |
| 231 // convenient "GetHeapSample(string*)" method defined above. |
| 232 virtual void** ReadStackTraces(int* sample_period); |
| 233 |
| 234 // Like ReadStackTraces(), but returns stack traces that caused growth |
| 235 // in the address space size. |
| 236 virtual void** ReadHeapGrowthStackTraces(); |
| 237 }; |
| 238 |
| 239 #endif // BASE_MALLOC_EXTENSION_H_ |
OLD | NEW |