1 /* Copyright (c) 2005, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 * Author: Sanjay Ghemawat
33 * Module for heap-profiling.
35 * For full(er) information, see doc/heapprofile.html
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:
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.
45 * 2. Programmatically, start and stop the profiler using the
46 * routines "HeapProfilerStart(filename)" and "HeapProfilerStop()".
50 #ifndef BASE_HEAP_PROFILER_H_
51 #define BASE_HEAP_PROFILER_H_
55 /* Annoying stuff for windows; makes sure clients can import these functions */
56 #ifndef PERFTOOLS_DLL_DECL
58 # define PERFTOOLS_DLL_DECL __declspec(dllimport)
60 # define PERFTOOLS_DLL_DECL
64 // Make the linker NOT to strip functions in this file.
66 #pragma comment(linker, "/INCLUDE:HeapProfilerStart")
68 #pragma comment(linker, "/INCLUDE:_HeapProfilerStart")
71 /* All this code should be usable from within C apps. */
76 /* Start profiling and arrange to write profile data to file names
77 * of the form: "prefix.0000", "prefix.0001", ...
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
83 PERFTOOLS_DLL_DECL
void HeapProfilerStart(const char* prefix
);
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.
92 typedef int (*StackGeneratorFunction
)(int skip_count
, void** stack
);
93 PERFTOOLS_DLL_DECL
void HeapProfilerWithPseudoStackStart(
94 StackGeneratorFunction callback
);
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.
102 int IsHeapProfilerRunning();
104 /* Stop heap profiling. Can be restarted again with HeapProfilerStart(),
105 * but the currently accumulated profiling information will be cleared.
107 PERFTOOLS_DLL_DECL
void HeapProfilerStop();
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
113 PERFTOOLS_DLL_DECL
void HeapProfilerDump(const char *reason
);
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.
120 PERFTOOLS_DLL_DECL
char* GetHeapProfile();
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.
126 typedef void (*AddressVisitor
)(void* data
, const void* ptr
);
128 /* Iterate over all live allocated objects. For each allocation the
129 * callback will be invoked with the data argument and allocation pointer.
131 PERFTOOLS_DLL_DECL
void IterateAllocatedObjects(AddressVisitor callback
,
138 #endif /* BASE_HEAP_PROFILER_H_ */