| 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 24 matching lines...) Expand all Loading... |
| 35 // application can link against a malloc that does not implement these | 35 // application can link against a malloc that does not implement these |
| 36 // extensions, and it will get default versions that do nothing. | 36 // extensions, and it will get default versions that do nothing. |
| 37 // | 37 // |
| 38 // NOTE FOR C USERS: If you wish to use this functionality from within | 38 // NOTE FOR C USERS: If you wish to use this functionality from within |
| 39 // a C program, see malloc_extension_c.h. | 39 // a C program, see malloc_extension_c.h. |
| 40 | 40 |
| 41 #ifndef BASE_MALLOC_EXTENSION_H_ | 41 #ifndef BASE_MALLOC_EXTENSION_H_ |
| 42 #define BASE_MALLOC_EXTENSION_H_ | 42 #define BASE_MALLOC_EXTENSION_H_ |
| 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 | |
| 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 | |
| 48 // for now I'm assuming it's a problem only with MSVC. | |
| 49 #ifndef _MSC_VER | |
| 50 #include <stdint.h> | |
| 51 #endif | |
| 52 #include <string> | 45 #include <string> |
| 53 | 46 |
| 54 // Annoying stuff for windows -- makes sure clients can import these functions | 47 // Annoying stuff for windows -- makes sure clients can import these functions |
| 55 #ifndef PERFTOOLS_DLL_DECL | 48 #ifndef PERFTOOLS_DLL_DECL |
| 56 # ifdef _WIN32 | 49 # ifdef _WIN32 |
| 57 # define PERFTOOLS_DLL_DECL __declspec(dllimport) | 50 # define PERFTOOLS_DLL_DECL __declspec(dllimport) |
| 58 # else | 51 # else |
| 59 # define PERFTOOLS_DLL_DECL | 52 # define PERFTOOLS_DLL_DECL |
| 60 # endif | 53 # endif |
| 61 #endif | 54 #endif |
| 62 | 55 |
| 63 static const int kMallocHistogramSize = 64; | 56 static const int kMallocHistogramSize = 64; |
| 64 | 57 |
| 65 // One day, we could support other types of writers (perhaps for C?) | 58 // One day, we could support other types of writers (perhaps for C?) |
| 66 typedef std::string MallocExtensionWriter; | 59 typedef std::string MallocExtensionWriter; |
| 67 | 60 |
| 68 namespace base { | |
| 69 struct MallocRange; | |
| 70 } | |
| 71 | |
| 72 // The default implementations of the following routines do nothing. | 61 // The default implementations of the following routines do nothing. |
| 73 // All implementations should be thread-safe; the current one | 62 // All implementations should be thread-safe; the current one |
| 74 // (TCMallocImplementation) is. | 63 // (TCMallocImplementation) is. |
| 75 class PERFTOOLS_DLL_DECL MallocExtension { | 64 class PERFTOOLS_DLL_DECL MallocExtension { |
| 76 public: | 65 public: |
| 77 virtual ~MallocExtension(); | 66 virtual ~MallocExtension(); |
| 78 | 67 |
| 79 // Call this very early in the program execution -- say, in a global | 68 // Call this very early in the program execution -- say, in a global |
| 80 // constructor -- to set up parameters and state needed by all | 69 // constructor -- to set up parameters and state needed by all |
| 81 // instrumented malloc implemenatations. One example: this routine | 70 // instrumented malloc implemenatations. One example: this routine |
| (...skipping 21 matching lines...) Expand all Loading... |
| 103 // is equivalent to the output of the heap profiler and can | 92 // is equivalent to the output of the heap profiler and can |
| 104 // therefore be passed to "pprof". | 93 // therefore be passed to "pprof". |
| 105 virtual void GetHeapSample(MallocExtensionWriter* writer); | 94 virtual void GetHeapSample(MallocExtensionWriter* writer); |
| 106 | 95 |
| 107 // Outputs to "writer" the stack traces that caused growth in the | 96 // Outputs to "writer" the stack traces that caused growth in the |
| 108 // address space size. The format of the returned output is | 97 // address space size. The format of the returned output is |
| 109 // equivalent to the output of the heap profiler and can therefore | 98 // equivalent to the output of the heap profiler and can therefore |
| 110 // be passed to "pprof". | 99 // be passed to "pprof". |
| 111 virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer); | 100 virtual void GetHeapGrowthStacks(MallocExtensionWriter* writer); |
| 112 | 101 |
| 113 // Invokes func(arg, range) for every controlled memory | |
| 114 // range. *range is filled in with information about the range. | |
| 115 // | |
| 116 // This is a best-effort interface useful only for performance | |
| 117 // analysis. The implementation may not call func at all. | |
| 118 typedef void (RangeFunction)(void*, const base::MallocRange*); | |
| 119 virtual void Ranges(void* arg, RangeFunction func); | |
| 120 | |
| 121 // ------------------------------------------------------------------- | 102 // ------------------------------------------------------------------- |
| 122 // Control operations for getting and setting malloc implementation | 103 // Control operations for getting and setting malloc implementation |
| 123 // specific parameters. Some currently useful properties: | 104 // specific parameters. Some currently useful properties: |
| 124 // | 105 // |
| 125 // generic | 106 // generic |
| 126 // ------- | 107 // ------- |
| 127 // "generic.current_allocated_bytes" | 108 // "generic.current_allocated_bytes" |
| 128 // Number of bytes currently allocated by application | 109 // Number of bytes currently allocated by application |
| 129 // This property is not writable. | 110 // This property is not writable. |
| 130 // | 111 // |
| 131 // "generic.heap_size" | 112 // "generic.heap_size" |
| 132 // Number of bytes in the heap == | 113 // Number of bytes in the heap == |
| 133 // current_allocated_bytes + | 114 // current_allocated_bytes + |
| 134 // fragmentation + | 115 // fragmentation + |
| 135 // freed memory regions | 116 // freed memory regions |
| 136 // This property is not writable. | 117 // This property is not writable. |
| 137 // | 118 // |
| 138 // tcmalloc | 119 // tcmalloc |
| 139 // -------- | 120 // -------- |
| 140 // "tcmalloc.max_total_thread_cache_bytes" | 121 // "tcmalloc.max_total_thread_cache_bytes" |
| 141 // Upper limit on total number of bytes stored across all | 122 // Upper limit on total number of bytes stored across all |
| 142 // per-thread caches. Default: 16MB. | 123 // per-thread caches. Default: 16MB. |
| 143 // | 124 // |
| 144 // "tcmalloc.current_total_thread_cache_bytes" | 125 // "tcmalloc.current_total_thread_cache_bytes" |
| 145 // Number of bytes used across all thread caches. | 126 // Number of bytes used across all thread caches. |
| 146 // This property is not writable. | 127 // This property is not writable. |
| 147 // | 128 // |
| 148 // "tcmalloc.slack_bytes" | 129 // "tcmalloc.slack_bytes" |
| 149 // Number of bytes allocated from system, but not currently in | 130 // Number of bytes allocated from system, but not currently |
| 150 // use by malloced objects. I.e., bytes available for | 131 // in use by malloced objects. I.e., bytes available for |
| 151 // allocation without needing more bytes from system. It is | 132 // allocation without needing more bytes from system. |
| 152 // the sum of pageheap_free_bytes and pageheap_unmapped_bytes. | |
| 153 // This property is not writable. | 133 // This property is not writable. |
| 154 // | 134 // |
| 155 // "tcmalloc.pageheap_free_bytes" | 135 // TODO: Add more properties as necessary |
| 156 // Number of bytes in free, mapped pages in pageheap | |
| 157 // This property is not writable. | |
| 158 // | |
| 159 // "tcmalloc.pageheap_unmapped_bytes" | |
| 160 // Number of bytes in free, unmapped pages in pageheap | |
| 161 // This property is not writable. | |
| 162 // | |
| 163 // ------------------------------------------------------------------- | 136 // ------------------------------------------------------------------- |
| 164 | 137 |
| 165 // Get the named "property"'s value. Returns true if the property | 138 // Get the named "property"'s value. Returns true if the property |
| 166 // is known. Returns false if the property is not a valid property | 139 // is known. Returns false if the property is not a valid property |
| 167 // name for the current malloc implementation. | 140 // name for the current malloc implementation. |
| 168 // REQUIRES: property != NULL; value != NULL | 141 // REQUIRES: property != NULL; value != NULL |
| 169 virtual bool GetNumericProperty(const char* property, size_t* value); | 142 virtual bool GetNumericProperty(const char* property, size_t* value); |
| 170 | 143 |
| 171 // Set the named "property"'s value. Returns true if the property | 144 // Set the named "property"'s value. Returns true if the property |
| 172 // is known and writable. Returns false if the property is not a | 145 // 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. | 160 // Most malloc implementations ignore this routine. |
| 188 virtual void MarkThreadIdle(); | 161 virtual void MarkThreadIdle(); |
| 189 | 162 |
| 190 // Mark the current thread as "busy". This routine should be | 163 // Mark the current thread as "busy". This routine should be |
| 191 // called after MarkThreadIdle() if the thread will now do more | 164 // called after MarkThreadIdle() if the thread will now do more |
| 192 // work. If this method is not called, performance may suffer. | 165 // work. If this method is not called, performance may suffer. |
| 193 // | 166 // |
| 194 // Most malloc implementations ignore this routine. | 167 // Most malloc implementations ignore this routine. |
| 195 virtual void MarkThreadBusy(); | 168 virtual void MarkThreadBusy(); |
| 196 | 169 |
| 197 // Try to release num_bytes of free memory back to the operating | 170 // Try to free memory back to the operating system for reuse. Only |
| 198 // system for reuse. Use this extension with caution -- to get this | 171 // use this extension if the application has recently freed a lot of |
| 199 // memory back may require faulting pages back in by the OS, and | 172 // memory, and does not anticipate using it again for a long time -- |
| 200 // that may be slow. (Currently only implemented in tcmalloc.) | 173 // to get this memory back may require faulting pages back in by the |
| 201 virtual void ReleaseToSystem(size_t num_bytes); | 174 // OS, and that may be slow. (Currently only implemented in |
| 202 | 175 // tcmalloc.) |
| 203 // Same as ReleaseToSystem() but release as much memory as possible. | |
| 204 virtual void ReleaseFreeMemory(); | 176 virtual void ReleaseFreeMemory(); |
| 205 | 177 |
| 206 // Sets the rate at which we release unused memory to the system. | 178 // Sets the rate at which we release unused memory to the system. |
| 207 // Zero means we never release memory back to the system. Increase | 179 // Zero means we never release memory back to the system. Increase |
| 208 // this flag to return memory faster; decrease it to return memory | 180 // this flag to return memory faster; decrease it to return memory |
| 209 // slower. Reasonable rates are in the range [0,10]. (Currently | 181 // slower. Reasonable rates are in the range [0,10]. (Currently |
| 210 // only implemented in tcmalloc). | 182 // only implemented in tcmalloc). |
| 211 virtual void SetMemoryReleaseRate(double rate); | 183 virtual void SetMemoryReleaseRate(double rate); |
| 212 | 184 |
| 213 // Gets the release rate. Returns a value < 0 if unknown. | 185 // Gets the release rate. Returns a value < 0 if unknown. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // | 232 // |
| 261 // This is an internal extension. Callers should use the more | 233 // This is an internal extension. Callers should use the more |
| 262 // convenient "GetHeapSample(string*)" method defined above. | 234 // convenient "GetHeapSample(string*)" method defined above. |
| 263 virtual void** ReadStackTraces(int* sample_period); | 235 virtual void** ReadStackTraces(int* sample_period); |
| 264 | 236 |
| 265 // Like ReadStackTraces(), but returns stack traces that caused growth | 237 // Like ReadStackTraces(), but returns stack traces that caused growth |
| 266 // in the address space size. | 238 // in the address space size. |
| 267 virtual void** ReadHeapGrowthStackTraces(); | 239 virtual void** ReadHeapGrowthStackTraces(); |
| 268 }; | 240 }; |
| 269 | 241 |
| 270 namespace base { | |
| 271 | |
| 272 // Information passed per range. More fields may be added later. | |
| 273 struct MallocRange { | |
| 274 enum Type { | |
| 275 INUSE, // Application is using this range | |
| 276 FREE, // Range is currently free | |
| 277 UNMAPPED, // Backing physical memory has been returned to the OS | |
| 278 UNKNOWN, | |
| 279 // More enum values may be added in the future | |
| 280 }; | |
| 281 | |
| 282 uintptr_t address; // Address of range | |
| 283 size_t length; // Byte length of range | |
| 284 Type type; // Type of this range | |
| 285 double fraction; // Fraction of range that is being used (0 if !INUSE) | |
| 286 | |
| 287 // Perhaps add the following: | |
| 288 // - stack trace if this range was sampled | |
| 289 // - heap growth stack trace if applicable to this range | |
| 290 // - age when allocated (for inuse) or freed (if not in use) | |
| 291 }; | |
| 292 | |
| 293 } // namespace base | |
| 294 | |
| 295 #endif // BASE_MALLOC_EXTENSION_H_ | 242 #endif // BASE_MALLOC_EXTENSION_H_ |
| OLD | NEW |