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