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 32 * 33 * Module for heap-profiling. 34 * 35 * For full(er) information, see doc/heapprofile.html 36 * 37 * This module can be linked into your program with 38 * no slowdown caused by this unless you activate the profiler 39 * using one of the following methods: 40 * 41 * 1. Before starting the program, set the environment variable 42 * "HEAPPROFILE" to be the name of the file to which the profile 43 * data should be written. 44 * 45 * 2. Programmatically, start and stop the profiler using the 46 * routines "HeapProfilerStart(filename)" and "HeapProfilerStop()". 47 * 48 */ 49 50 #ifndef BASE_HEAP_PROFILER_H_ 51 #define BASE_HEAP_PROFILER_H_ 52 53 #include <stddef.h> 54 55 /* Annoying stuff for windows; makes sure clients can import these functions */ 56 #ifndef PERFTOOLS_DLL_DECL 57 # ifdef _WIN32 58 # define PERFTOOLS_DLL_DECL __declspec(dllimport) 59 # else 60 # define PERFTOOLS_DLL_DECL 61 # endif 62 #endif 63 64 // Make the linker NOT to strip functions in this file. 65 #if defined(_WIN64) 66 #pragma comment(linker, "/INCLUDE:HeapProfilerStart") 67 #elif defined(_WIN32) 68 #pragma comment(linker, "/INCLUDE:_HeapProfilerStart") 69 #endif 70 71 /* All this code should be usable from within C apps. */ 72 #ifdef __cplusplus 73 extern "C" { 74 #endif 75 76 /* Start profiling and arrange to write profile data to file names 77 * of the form: "prefix.0000", "prefix.0001", ... 78 * 79 * If |prefix| is NULL then dumps will not be written to disk. Applications 80 * can use GetHeapProfile() to get profile data, but HeapProfilerDump() will do 81 * nothing. 82 */ 83 PERFTOOLS_DLL_DECL void HeapProfilerStart(const char* prefix); 84 85 /* Start profiling with a callback function that returns application-generated 86 * stacks. Profiles are not written to disk, but may be obtained via 87 * GetHeapProfile(). The callback: 88 * 1. May optionally skip the first |skip_count| items on the stack. 89 * 2. Must provide a |stack| buffer of at least size 32 * sizeof(void*). 90 * 3. Must return the number of items copied or zero. 91 */ 92 typedef int (*StackGeneratorFunction)(int skip_count, void** stack); 93 PERFTOOLS_DLL_DECL void HeapProfilerWithPseudoStackStart( 94 StackGeneratorFunction callback); 95 96 /* Returns non-zero if we are currently profiling the heap. (Returns 97 * an int rather than a bool so it's usable from C.) This is true 98 * between calls to HeapProfilerStart() and HeapProfilerStop(), and 99 * also if the program has been run with HEAPPROFILER, or some other 100 * way to turn on whole-program profiling. 101 */ 102 int IsHeapProfilerRunning(); 103 104 /* Stop heap profiling. Can be restarted again with HeapProfilerStart(), 105 * but the currently accumulated profiling information will be cleared. 106 */ 107 PERFTOOLS_DLL_DECL void HeapProfilerStop(); 108 109 /* Dump a profile now - can be used for dumping at a hopefully 110 * quiescent state in your program, in order to more easily track down 111 * memory leaks. Will include the reason in the logged message 112 */ 113 PERFTOOLS_DLL_DECL void HeapProfilerDump(const char *reason); 114 115 /* Generate current heap profiling information. 116 * Returns an empty string when heap profiling is not active. 117 * The returned pointer is a '\0'-terminated string allocated using malloc() 118 * and should be free()-ed as soon as the caller does not need it anymore. 119 */ 120 PERFTOOLS_DLL_DECL char* GetHeapProfile(); 121 122 /* Callback function for iterating through all allocated objects. Accepts 123 * pointer to user data passed into IterateAllocatedObjects and pointer 124 * to the object being visited. 125 */ 126 typedef void (*AddressVisitor)(void* data, const void* ptr); 127 128 /* Iterate over all live allocated objects. For each allocation the 129 * callback will be invoked with the data argument and allocation pointer. 130 */ 131 PERFTOOLS_DLL_DECL void IterateAllocatedObjects(AddressVisitor callback, 132 void* data); 133 134 #ifdef __cplusplus 135 } // extern "C" 136 #endif 137 138 #endif /* BASE_HEAP_PROFILER_H_ */ 139