Imported existing code

This commit is contained in:
Hazim Gazov
2010-04-02 02:48:44 -03:00
parent 48fbc5ae91
commit 7a86d01598
13996 changed files with 2468699 additions and 0 deletions

View File

@@ -0,0 +1,433 @@
// Copyright (c) 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Author: Maxim Lifantsev (with design ideas by Sanjay Ghemawat)
//
//
// Module for detecing heap (memory) leaks.
//
// For full(er) information, see doc/heap-checker.html
//
// This module can be linked into programs with
// no slowdown caused by this unless you activate the leak-checker:
//
// 1. Set the environment variable HEAPCHEK to _type_ before
// running the program.
//
// _type_ is usually "normal" but can also be "minimal", "strict", or
// "draconian". (See the html file for other options, like 'local'.)
//
// After that, just run your binary. If the heap-checker detects
// a memory leak at program-exit, it will print instructions on how
// to track down the leak.
#ifndef BASE_HEAP_CHECKER_H__
#define BASE_HEAP_CHECKER_H__
#include <sys/types.h> // for size_t
#include <stdint.h> // for uintptr_t
#include <vector>
// Annoying stuff for windows -- makes sure clients can import these functions
#ifndef PERFTOOLS_DLL_DECL
# ifdef WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else
# define PERFTOOLS_DLL_DECL
# endif
#endif
class PERFTOOLS_DLL_DECL HeapLeakChecker {
public: // Static functions for working with (whole-program) leak checking.
// If heap leak checking is currently active in some mode
// e.g. if leak checking was started (and is still active now)
// due to any valid non-empty --heap_check flag value
// (including "local") on the command-line
// or via a dependency on //base:heapcheck.
// The return value reflects iff HeapLeakChecker objects manually
// constructed right now will be doing leak checking or nothing.
// Note that we can go from active to inactive state during InitGoogle()
// if FLAGS_heap_check gets set to "" by some code before/during InitGoogle().
static bool IsActive();
// Return pointer to the whole-program checker if (still) active
// and NULL otherwise.
// This is mainly to access BytesLeaked() and ObjectsLeaked() (see below)
// for the whole-program checker after one calls NoGlobalLeaks()
// or similar and gets false.
static HeapLeakChecker* GlobalChecker();
// Do whole-program leak check now (if it was activated for this binary);
// return false only if it was activated and has failed.
// The mode of the check is controlled by the command-line flags.
// This method can be called repeatedly.
// Things like GlobalChecker()->SameHeap() can also be called explicitly
// to do the desired flavor of the check.
static bool NoGlobalLeaks();
// If whole-program checker if active,
// cancel its automatic execution after main() exits.
// This requires that some leak check (e.g. NoGlobalLeaks())
// has been called at least once on the whole-program checker.
static void CancelGlobalCheck();
public: // Non-static functions for starting and doing leak checking.
// Start checking and name the leak check performed.
// The name is used in naming dumped profiles
// and needs to be unique only within your binary.
// It must also be a string that can be a part of a file name,
// in particular not contain path expressions.
explicit HeapLeakChecker(const char *name);
// Return true iff the heap does not have more objects allocated
// w.r.t. its state at the time of our construction.
// This does full pprof heap change checking and reporting.
// To detect tricky leaks it depends on correct working pprof implementation
// referred by FLAGS_heap_profile_pprof.
// (By 'tricky leaks' we mean a change of heap state that e.g. for SameHeap
// preserves the number of allocated objects and bytes
// -- see TestHeapLeakCheckerTrick in heap-checker_unittest.cc --
// and thus is not detected by BriefNoLeaks.)
// CAVEAT: pprof will do no checking over stripped binaries
// (our automatic test binaries are stripped)
// NOTE: All *NoLeaks() and *SameHeap() methods can be called many times
// to check for leaks at different end-points in program's execution.
bool NoLeaks() { return DoNoLeaks(NO_LEAKS, USE_PPROF, PPROF_REPORT); }
// Return true iff the heap does not seem to have more objects allocated
// w.r.t. its state at the time of our construction
// by looking at the number of objects & bytes allocated.
// This also tries to do pprof reporting of detected leaks.
bool QuickNoLeaks() { return DoNoLeaks(NO_LEAKS, USE_COUNTS, PPROF_REPORT); }
// Return true iff the heap does not seem to have more objects allocated
// w.r.t. its state at the time of our construction
// by looking at the number of objects & bytes allocated.
// This does not try to use pprof at all.
bool BriefNoLeaks() { return DoNoLeaks(NO_LEAKS, USE_COUNTS, NO_REPORT); }
// These are similar to their *NoLeaks counterparts,
// but they in addition require no negative leaks,
// i.e. the state of the heap must be exactly the same
// as at the time of our construction.
bool SameHeap() { return DoNoLeaks(SAME_HEAP, USE_PPROF, PPROF_REPORT); }
bool QuickSameHeap()
{ return DoNoLeaks(SAME_HEAP, USE_COUNTS, PPROF_REPORT); }
bool BriefSameHeap() { return DoNoLeaks(SAME_HEAP, USE_COUNTS, NO_REPORT); }
// Detailed information about the number of leaked bytes and objects
// (both of these can be negative as well).
// These are available only after a *SameHeap or *NoLeaks
// method has been called.
// Note that it's possible for both of these to be zero
// while SameHeap() or NoLeaks() returned false in case
// of a heap state change that is significant
// but preserves the byte and object counts.
ssize_t BytesLeaked() const;
ssize_t ObjectsLeaked() const;
// Destructor (verifies that some *NoLeaks or *SameHeap method
// has been called at least once).
~HeapLeakChecker();
private: // data
char* name_; // our remembered name
size_t start_inuse_bytes_; // bytes in use at our construction
size_t start_inuse_allocs_; // allocations in use at our construction
bool has_checked_; // if we have done the leak check, so these are ready:
ssize_t inuse_bytes_increase_; // bytes-in-use increase for this checker
ssize_t inuse_allocs_increase_; // allocations-in-use increase for this checker
public: // Static helpers to make us ignore certain leaks.
// NOTE: All calls to DisableChecks* affect all later heap profile generation
// that happens in our construction and inside of *NoLeaks().
// They do nothing when heap leak checking is turned off.
// CAVEAT: Disabling via all the DisableChecks* functions happens only
// up to kMaxStackTrace stack frames (see heap-profile-table.h)
// down from the stack frame identified by the function.
// Hence, this disabling will stop working for very deep call stacks
// and you might see quite wierd leak profile dumps in such cases.
// CAVEAT: Disabling via DisableChecksIn works only with non-strip'ped
// binaries. It's better not to use this function if at all possible.
//
// Register 'pattern' as another variant of a regular expression to match
// function_name, file_name:line_number, or function_address
// of function call/return points for which allocations below them should be
// ignored during heap leak checking.
// (This becomes a part of pprof's '--ignore' argument.)
// Usually this should be caled from a REGISTER_HEAPCHECK_CLEANUP
// in the source file that is causing the leaks being ignored.
static void DisableChecksIn(const char* pattern);
// A pair of functions to disable heap checking between them.
// For example
// ...
// void* start_address = HeapLeakChecker::GetDisableChecksStart();
// <do things>
// HeapLeakChecker::DisableChecksToHereFrom(start_address);
// ...
// will disable heap leak checking for everything that happens
// during any execution of <do things> (including any calls from it),
// i.e. all objects allocated from there
// and everything reachable from them will not be considered a leak.
// Each such pair of function calls must be from the same function,
// because this disabling works by remembering the range of
// return program counter addresses for the two calls.
static void* GetDisableChecksStart();
static void DisableChecksToHereFrom(const void* start_address);
// ADVICE: Use GetDisableChecksStart, DisableChecksToHereFrom
// instead of DisableChecksUp|At whenever possible
// to make the code less fragile under different degrees of inlining.
// Register the function call point (return program counter address)
// 'stack_frames' above us for which allocations
// (everything reachable from them) below it should be
// ignored during heap leak checking.
// 'stack_frames' must be >= 1 (in most cases one would use the value of 1).
// For example
// void Foo() { // Foo() should not get inlined
// HeapLeakChecker::DisableChecksUp(1);
// <do things>
// }
// will disable heap leak checking for everything that happens
// during any execution of <do things> (including any calls from it),
// i.e. all objects allocated from there
// and everything reachable from them will not be considered a leak.
// CAVEAT: If Foo() is inlined this will disable heap leak checking
// under all processing of all functions Foo() is inlined into.
// Hence, for potentially inlined functions, use the GetDisableChecksStart,
// DisableChecksToHereFrom calls instead.
// (In the above example we store and use the return program counter
// addresses from Foo to do the disabling.)
static void DisableChecksUp(int stack_frames);
// Same as DisableChecksUp,
// but the function return program counter address is given explicitly.
static void DisableChecksAt(const void* address);
// Tests for checking that DisableChecksUp and DisableChecksAt
// behaved as expected, for example
// void Foo() {
// HeapLeakChecker::DisableChecksUp(1);
// <do things>
// }
// void Bar() {
// Foo();
// CHECK(!HeapLeakChecker::HaveDisabledChecksUp(1));
// // This will fail if Foo() got inlined into Bar()
// // (due to more aggressive optimization in the (new) compiler)
// // which breaks the intended behavior of DisableChecksUp(1) in it.
// <do things>
// }
// These return false when heap leak checking is turned off.
static bool HaveDisabledChecksUp(int stack_frames);
static bool HaveDisabledChecksAt(const void* address);
// Ignore an object located at 'ptr'
// (as well as all heap objects (transitively) referenced from it)
// for the purposes of heap leak checking.
// If 'ptr' does not point to an active allocated object
// at the time of this call, it is ignored;
// but if it does, the object must not get deleted from the heap later on;
// it must also be not already ignored at the time of this call.
static void IgnoreObject(const void* ptr);
// Undo what an earlier IgnoreObject() call promised and asked to do.
// At the time of this call 'ptr' must point to an active allocated object
// which was previously registered with IgnoreObject().
static void UnIgnoreObject(const void* ptr);
public: // Initializations; to be called from main() only.
// Full starting of recommended whole-program checking.
static void InternalInitStart();
public: // Internal types defined in .cc
struct Allocator;
struct RangeValue;
private: // Various helpers
// Type for DumpProfileLocked
enum ProfileType { START_PROFILE, END_PROFILE };
// Helper for dumping start/end heap leak checking profiles
// and getting the byte/object counts.
void DumpProfileLocked(ProfileType profile_type, const void* self_stack_top,
size_t* alloc_bytes, size_t* alloc_objects);
// Helper for constructors
void Create(const char *name);
// Types for DoNoLeaks and its helpers.
enum CheckType { SAME_HEAP, NO_LEAKS };
enum CheckFullness { USE_PPROF, USE_COUNTS };
enum ReportMode { PPROF_REPORT, NO_REPORT };
// Helpers for *NoLeaks and *SameHeap
bool DoNoLeaks(CheckType check_type,
CheckFullness fullness,
ReportMode report_mode);
bool DoNoLeaksOnce(CheckType check_type,
CheckFullness fullness,
ReportMode report_mode);
// Helper for IgnoreObject
static void IgnoreObjectLocked(const void* ptr);
// Helper for DisableChecksAt
static void DisableChecksAtLocked(const void* address);
// Helper for DisableChecksIn
static void DisableChecksInLocked(const char* pattern);
// Helper for DisableChecksToHereFrom
static void DisableChecksFromToLocked(const void* start_address,
const void* end_address,
int max_depth);
// Helper for DoNoLeaks to ignore all objects reachable from all live data
static void IgnoreAllLiveObjectsLocked(const void* self_stack_top);
// Callback we pass to ListAllProcessThreads (see thread_lister.h)
// that is invoked when all threads of our process are found and stopped.
// The call back does the things needed to ignore live data reachable from
// thread stacks and registers for all our threads
// as well as do other global-live-data ignoring
// (via IgnoreNonThreadLiveObjectsLocked)
// during the quiet state of all threads being stopped.
// For the argument meaning see the comment by ListAllProcessThreads.
// Here we only use num_threads and thread_pids, that ListAllProcessThreads
// fills for us with the number and pids of all the threads of our process
// it found and attached to.
static int IgnoreLiveThreads(void* parameter,
int num_threads,
pid_t* thread_pids,
va_list ap);
// Helper for IgnoreAllLiveObjectsLocked and IgnoreLiveThreads
// that we prefer to execute from IgnoreLiveThreads
// while all threads are stopped.
// This helper does live object discovery and ignoring
// for all objects that are reachable from everything
// not related to thread stacks and registers.
static void IgnoreNonThreadLiveObjectsLocked();
// Helper for IgnoreNonThreadLiveObjectsLocked and IgnoreLiveThreads
// to discover and ignore all heap objects
// reachable from currently considered live objects
// (live_objects static global variable in out .cc file).
// "name", "name2" are two strings that we print one after another
// in a debug message to describe what kind of live object sources
// are being used.
static void IgnoreLiveObjectsLocked(const char* name, const char* name2);
// Heap profile object filtering callback to filter out live objects.
static bool HeapProfileFilter(const void* ptr, size_t size);
// Runs REGISTER_HEAPCHECK_CLEANUP cleanups and potentially
// calls DoMainHeapCheck
static void RunHeapCleanups();
// Do the overall whole-program heap leak check
static void DoMainHeapCheck();
// Type of task for UseProcMapsLocked
enum ProcMapsTask {
RECORD_GLOBAL_DATA,
DISABLE_LIBRARY_ALLOCS
};
// Success/Error Return codes for UseProcMapsLocked.
enum ProcMapsResult {
PROC_MAPS_USED,
CANT_OPEN_PROC_MAPS,
NO_SHARED_LIBS_IN_PROC_MAPS
};
// Read /proc/self/maps, parse it, and do the 'proc_maps_task' for each line.
static ProcMapsResult UseProcMapsLocked(ProcMapsTask proc_maps_task);
// A ProcMapsTask to disable allocations from 'library'
// that is mapped to [start_address..end_address)
// (only if library is a certain system library).
static void DisableLibraryAllocsLocked(const char* library,
uintptr_t start_address,
uintptr_t end_address);
// Return true iff "*ptr" points to a heap object;
// we also fill *object_size for this object then.
// If yes, we might move "*ptr" to point to the very start of the object
// (this needs to happen for C++ class array allocations
// and for basic_string-s of C++ library that comes with gcc 3.4).
static bool HaveOnHeapLocked(const void** ptr, size_t* object_size);
private:
// Helper for VerifyHeapProfileTableStackGet in the unittest
// to get the recorded allocation caller for ptr,
// which must be a heap object.
static const void* GetAllocCaller(void* ptr);
friend void VerifyHeapProfileTableStackGet();
// This gets to execute before constructors for all global objects
static void BeforeConstructors();
friend void HeapLeakChecker_BeforeConstructors();
// This gets to execute after destructors for all global objects
friend void HeapLeakChecker_AfterDestructors();
// Helper to shutdown heap leak checker when it's not needed
// or can't function properly.
static void TurnItselfOff();
private:
// Start whole-executable checking.
HeapLeakChecker();
private:
// Disallow "evil" constructors.
HeapLeakChecker(const HeapLeakChecker&);
void operator=(const HeapLeakChecker&);
};
// A class that exists solely to run its destructor. This class should not be
// used directly, but instead by the REGISTER_HEAPCHECK_CLEANUP macro below.
class PERFTOOLS_DLL_DECL HeapCleaner {
public:
typedef void (*void_function)(void);
HeapCleaner(void_function f);
static void RunHeapCleanups();
private:
static std::vector<void_function>* heap_cleanups_;
};
// A macro to declare module heap check cleanup tasks
// (they run only if we are doing heap leak checking.)
// 'body' should be the cleanup code to run. 'name' doesn't matter,
// but must be unique amongst all REGISTER_HEAPCHECK_CLEANUP calls.
#define REGISTER_HEAPCHECK_CLEANUP(name, body) \
namespace { \
void heapcheck_cleanup_##name() { body; } \
static HeapCleaner heapcheck_cleaner_##name(&heapcheck_cleanup_##name); \
}
#endif // BASE_HEAP_CHECKER_H__

View File

@@ -0,0 +1,84 @@
// Copyright (c) 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Author: Sanjay Ghemawat
//
// Module for heap-profiling.
//
// For full(er) information, see doc/heapprofile.html
//
// This module can be linked into your program with
// no slowdown caused by this unless you activate the profiler
// using one of the following methods:
//
// 1. Before starting the program, set the environment variable
// "HEAPPROFILE" to be the name of the file to which the profile
// data should be written.
//
// 2. Programmatically, start and stop the profiler using the
// routines "HeapProfilerStart(filename)" and "HeapProfilerStop()".
//
// Use pprof to view the resulting profile output.
// % google3/perftools/pprof <path_to_executable> <profile_file_name>
// % google3/perftools/pprof --gv <path_to_executable> <profile_file_name>
#ifndef BASE_HEAP_PROFILER_H__
#define BASE_HEAP_PROFILER_H__
#include <stddef.h>
// Annoying stuff for windows -- makes sure clients can import these functions
#ifndef PERFTOOLS_DLL_DECL
# ifdef WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else
# define PERFTOOLS_DLL_DECL
# endif
#endif
// Start profiling and arrange to write profile data to file names
// of the form: "prefix.0000", "prefix.0001", ...
extern "C" PERFTOOLS_DLL_DECL void HeapProfilerStart(const char* prefix);
// Stop heap profiling. Can be restarted again with HeapProfilerStart(),
// but the currently accumulated profiling information will be cleared.
extern "C" PERFTOOLS_DLL_DECL void HeapProfilerStop();
// Dump a profile now - can be used for dumping at a hopefully
// quiescent state in your program, in order to more easily track down
// memory leaks. Will include the reason in the logged message
extern "C" PERFTOOLS_DLL_DECL void HeapProfilerDump(const char *reason);
// Generate current heap profiling information. The returned pointer
// is a null-terminated string allocated using malloc() and should be
// free()-ed as soon as the caller does not need it anymore.
extern "C" PERFTOOLS_DLL_DECL char* GetHeapProfile();
#endif /* BASE_HEAP_PROFILER_H__ */

View File

@@ -0,0 +1,202 @@
// Copyright (c) 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Author: Sanjay Ghemawat <opensource@google.com>
//
// Extra extensions exported by some malloc implementations. These
// extensions are accessed through a virtual base class so an
// application can link against a malloc that does not implement these
// extensions, and it will get default versions that do nothing.
#ifndef BASE_MALLOC_EXTENSION_H__
#define BASE_MALLOC_EXTENSION_H__
#include <stddef.h>
#include <string>
// Annoying stuff for windows -- makes sure clients can import these functions
#ifndef PERFTOOLS_DLL_DECL
# ifdef WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else
# define PERFTOOLS_DLL_DECL
# endif
#endif
static const int kMallocHistogramSize = 64;
// The default implementations of the following routines do nothing.
class PERFTOOLS_DLL_DECL MallocExtension {
public:
virtual ~MallocExtension();
// Call this very early in the program execution -- say, in a global
// constructor -- to set up parameters and state needed by all
// instrumented malloc implemenatations. One example: this routine
// sets environemnt variables to tell STL to use libc's malloc()
// instead of doing its own memory management. This is safe to call
// multiple times, as long as each time is before threads start up.
static void Initialize();
// See "verify_memory.h" to see what these routines do
virtual bool VerifyAllMemory();
virtual bool VerifyNewMemory(void* p);
virtual bool VerifyArrayNewMemory(void* p);
virtual bool VerifyMallocMemory(void* p);
virtual bool MallocMemoryStats(int* blocks, size_t* total,
int histogram[kMallocHistogramSize]);
// Get a human readable description of the current state of the malloc
// data structures. The state is stored as a null-terminated string
// in a prefix of "buffer[0,buffer_length-1]".
// REQUIRES: buffer_length > 0.
virtual void GetStats(char* buffer, int buffer_length);
// Get a string that contains a sample of live objects and the stack
// traces that allocated these objects. The format of the returned
// string is equivalent to the output of the heap profiler and can
// therefore be passed to "pprof".
//
// The generated data is *appended* to "*result". I.e., the old
// contents of "*result" are preserved.
virtual void GetHeapSample(std::string* result);
// Get a string that contains the stack traces that caused growth in
// the addres sspace size. The format of the returned string is
// equivalent to the output of the heap profiler and can therefore
// be passed to "pprof".
//
// The generated data is *appended* to "*result". I.e., the old
// contents of "*result" are preserved.
virtual void GetHeapGrowthStacks(std::string* result);
// -------------------------------------------------------------------
// Control operations for getting and setting malloc implementation
// specific parameters. Some currently useful properties:
//
// generic
// -------
// "generic.current_allocated_bytes"
// Number of bytes currently allocated by application
// This property is not writable.
//
// "generic.heap_size"
// Number of bytes in the heap ==
// current_allocated_bytes +
// fragmentation +
// freed memory regions
// This property is not writable.
//
// tcmalloc
// --------
// "tcmalloc.max_total_thread_cache_bytes"
// Upper limit on total number of bytes stored across all
// per-thread caches. Default: 16MB.
//
// "tcmalloc.current_total_thread_cache_bytes"
// Number of bytes used across all thread caches.
// This property is not writable.
//
// "tcmalloc.slack_bytes"
// Number of bytes allocated from system, but not currently
// in use by malloced objects. I.e., bytes available for
// allocation without needing more bytes from system.
// This property is not writable.
//
// TODO: Add more properties as necessary
// -------------------------------------------------------------------
// Get the named "property"'s value. Returns true if the property
// is known. Returns false if the property is not a valid property
// name for the current malloc implementation.
// REQUIRES: property != NULL; value != NULL
virtual bool GetNumericProperty(const char* property, size_t* value);
// Set the named "property"'s value. Returns true if the property
// is known and writable. Returns false if the property is not a
// valid property name for the current malloc implementation, or
// is not writable.
// REQUIRES: property != NULL
virtual bool SetNumericProperty(const char* property, size_t value);
// Mark the current thread as "idle". This routine may optionally
// be called by threads as a hint to the malloc implementation that
// any thread-specific resources should be released. Note: this may
// be an expensive routine, so it should not be called too often.
//
// Also, if the code that calls this routine will go to sleep for
// a while, it should take care to not allocate anything between
// the call to this routine and the beginning of the sleep.
//
// Most malloc implementations ignore this routine.
virtual void MarkThreadIdle();
// Try to free memory back to the operating system for reuse. Only
// use this extension if the application has recently freed a lot of
// memory, and does not anticipate using it again for a long time --
// to get this memory back may require faulting pages back in by the
// OS, and that may be slow. (Currently only implemented in
// tcmalloc.)
virtual void ReleaseFreeMemory();
// The current malloc implementation. Always non-NULL.
static MallocExtension* instance();
// Change the malloc implementation. Typically called by the
// malloc implementation during initialization.
static void Register(MallocExtension* implementation);
protected:
// Get a list of stack traces of sampled allocation points.
// Returns a pointer to a "new[]-ed" result array.
//
// The state is stored as a sequence of adjacent entries
// in the returned array. Each entry has the following form:
// uintptr_t count; // Number of objects with following trace
// uintptr_t size; // Size of object
// uintptr_t depth; // Number of PC values in stack trace
// void* stack[depth]; // PC values that form the stack trace
//
// The list of entries is terminated by a "count" of 0.
//
// It is the responsibility of the caller to "delete[]" the returned array.
//
// May return NULL to indicate no results.
//
// This is an internal extension. Callers should use the more
// convenient "GetHeapSample(string*)" method defined above.
virtual void** ReadStackTraces();
// Like ReadStackTraces(), but returns stack traces that caused growth
// in the address space size.
virtual void** ReadHeapGrowthStackTraces();
};
#endif // BASE_MALLOC_EXTENSION_H__

View File

@@ -0,0 +1,184 @@
// Copyright (c) 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Author: Sanjay Ghemawat
//
// Some of our malloc implementations can invoke the following hooks
// whenever memory is allocated or deallocated. If the hooks are
// NULL, they are not invoked.
//
// One important user of these hooks is the heap profiler.
//
// CAVEAT: If you add new MallocHook::Invoke* calls (not for chaining hooks),
// then those calls must be directly in the code of the (de)allocation
// function that is provided to the user and that function must have
// an ATTRIBUTE_SECTION(malloc_hook) attribute.
#ifndef _MALLOC_HOOK_H
#define _MALLOC_HOOK_H
#include <stddef.h>
#include <sys/types.h>
// Annoying stuff for windows -- makes sure clients can import these functions
#ifndef PERFTOOLS_DLL_DECL
# ifdef WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else
# define PERFTOOLS_DLL_DECL
# endif
#endif
class PERFTOOLS_DLL_DECL MallocHook {
public:
// The NewHook is invoked whenever an object is allocated.
// It may be passed NULL if the allocator returned NULL.
typedef void (*NewHook)(const void* ptr, size_t size);
inline static NewHook GetNewHook() { return new_hook_; }
inline static NewHook SetNewHook(NewHook hook) {
NewHook result = new_hook_;
new_hook_ = hook;
return result;
}
inline static void InvokeNewHook(const void* p, size_t s) {
if (new_hook_ != NULL) (*new_hook_)(p, s);
}
// The DeleteHook is invoked whenever an object is deallocated.
// It may be passed NULL if the caller is trying to delete NULL.
typedef void (*DeleteHook)(const void* ptr);
inline static DeleteHook GetDeleteHook() { return delete_hook_; }
inline static DeleteHook SetDeleteHook(DeleteHook hook) {
DeleteHook result = delete_hook_;
delete_hook_ = hook;
return result;
}
inline static void InvokeDeleteHook(const void* p) {
if (delete_hook_ != NULL) (*delete_hook_)(p);
}
// The MmapHook is invoked whenever a region of memory is mapped.
// It may be passed MAP_FAILED if the mmap failed.
typedef void (*MmapHook)(const void* result,
const void* start,
size_t size,
int protection,
int flags,
int fd,
off_t offset);
inline static MmapHook GetMmapHook() { return mmap_hook_; }
inline static MmapHook SetMmapHook(MmapHook hook) {
MmapHook result = mmap_hook_;
mmap_hook_ = hook;
return result;
}
inline static void InvokeMmapHook(const void* result,
const void* start,
size_t size,
int protection,
int flags,
int fd,
off_t offset) {
if (mmap_hook_ != NULL) (*mmap_hook_)(result,
start, size,
protection, flags,
fd, offset);
}
// The MunmapHook is invoked whenever a region of memory is unmapped.
typedef void (*MunmapHook)(const void* ptr, size_t size);
inline static MunmapHook GetMunmapHook() { return munmap_hook_; }
inline static MunmapHook SetMunmapHook(MunmapHook hook) {
MunmapHook result = munmap_hook_;
munmap_hook_ = hook;
return result;
}
inline static void InvokeMunmapHook(const void* p, size_t size) {
if (munmap_hook_ != NULL) (*munmap_hook_)(p, size);
}
// The MremapHook is invoked whenever a region of memory is remapped.
typedef void (*MremapHook)(const void* result,
const void* old_addr,
size_t old_size,
size_t new_size,
int flags,
const void* new_addr);
inline static MremapHook GetMremapHook() { return mremap_hook_; }
inline static MremapHook SetMremapHook(MremapHook hook) {
MremapHook result = mremap_hook_;
mremap_hook_ = hook;
return result;
}
inline static void InvokeMremapHook(const void* result,
const void* old_addr,
size_t old_size,
size_t new_size,
int flags,
const void* new_addr) {
if (mremap_hook_ != NULL) (*mremap_hook_)(result,
old_addr, old_size,
new_size, flags, new_addr);
}
// The SbrkHook is invoked whenever sbrk is called -- except when
// the increment is 0. This is because sbrk(0) is often called
// to get the top of the memory stack, and is not actually a
// memory-allocation call.
typedef void (*SbrkHook)(const void* result, ptrdiff_t increment);
inline static SbrkHook GetSbrkHook() { return sbrk_hook_; }
inline static SbrkHook SetSbrkHook(SbrkHook hook) {
SbrkHook result = sbrk_hook_;
sbrk_hook_ = hook;
return result;
}
inline static void InvokeSbrkHook(const void* result, ptrdiff_t increment) {
if (sbrk_hook_ != NULL && increment != 0) (*sbrk_hook_)(result, increment);
}
// Get the current stack trace. Try to skip all routines up to and
// and including the caller of MallocHook::Invoke*.
// Use "skip_count" (similarly to GetStackTrace from stacktrace.h)
// as a hint about how many routines to skip if better information
// is not available.
static int GetCallerStackTrace(void** result, int max_depth, int skip_count);
private:
static NewHook new_hook_;
static DeleteHook delete_hook_;
static MmapHook mmap_hook_;
static MunmapHook munmap_hook_;
static MremapHook mremap_hook_;
static SbrkHook sbrk_hook_;
};
#endif /* _MALLOC_HOOK_H */

View File

@@ -0,0 +1,126 @@
// Copyright (c) 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Author: Sanjay Ghemawat
//
// Module for CPU profiling based on periodic pc-sampling.
//
// For full(er) information, see doc/cpuprofile.html
//
// This module is linked into your program with
// no slowdown caused by this unless you activate the profiler
// using one of the following methods:
//
// 1. Before starting the program, set the environment variable
// "PROFILE" to be the name of the file to which the profile
// data should be written.
//
// 2. Programmatically, start and stop the profiler using the
// routines "ProfilerStart(filename)" and "ProfilerStop()".
//
// All threads in the program are profiled whenever profiling is on.
// (Note: if using linux 2.4 or earlier, only the main thread may be
// profiled.)
//
// Use pprof to view the resulting profile output.
// % pprof <path_to_executable> <profile_file_name>
// % pprof --gv <path_to_executable> <profile_file_name>
#ifndef BASE_PROFILER_H__
#define BASE_PROFILER_H__
#include <time.h> // For time_t
// Annoying stuff for windows -- makes sure clients can import these functions
#ifndef PERFTOOLS_DLL_DECL
# ifdef WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else
# define PERFTOOLS_DLL_DECL
# endif
#endif
// Start profiling and write profile info into fname.
extern "C" PERFTOOLS_DLL_DECL bool ProfilerStart(const char* fname);
// Stop profiling. Can be started again with ProfilerStart(), but
// the currently accumulated profiling data will be cleared.
extern "C" PERFTOOLS_DLL_DECL void ProfilerStop();
// Flush any currently buffered profiling state to the profile file.
// Has no effect if the profiler has not been started.
extern "C" PERFTOOLS_DLL_DECL void ProfilerFlush();
// DEPRECATED: these functions were used to enable/disable profiling
// in the current thread, but no longer do anything.
extern "C" PERFTOOLS_DLL_DECL void ProfilerEnable();
extern "C" PERFTOOLS_DLL_DECL void ProfilerDisable();
// Returns true if profile is currently enabled
extern "C" PERFTOOLS_DLL_DECL bool ProfilingIsEnabledForAllThreads();
// Routine for registering new threads with the profiler. This is
// most usefully called when a new thread is first entered.
extern "C" PERFTOOLS_DLL_DECL void ProfilerRegisterThread();
// Stores state about profiler's current status into "*state".
struct ProfilerState {
bool enabled; // Is profiling currently enabled?
time_t start_time; // If enabled, when was profiling started?
char profile_name[1024]; // Name of profile file being written, or '\0'
int samples_gathered; // Number of samples gatheered to far (or 0)
};
extern "C" PERFTOOLS_DLL_DECL void ProfilerGetCurrentState(ProfilerState* state);
// ------------------------- ProfilerThreadState -----------------------
// DEPRECATED: this class is no longer needed.
//
// A small helper class that allows a thread to periodically check if
// profiling has been enabled or disabled, and to react appropriately
// to ensure that activity in the current thread is included in the
// profile. Usage:
//
// ProfileThreadState profile_state;
// while (true) {
// ... do some thread work ...
// profile_state.ThreadCheck();
// }
class ProfilerThreadState {
public:
ProfilerThreadState() { }
// Called in a thread to enable or disable profiling on the thread
// based on whether profiling is currently on or off.
// DEPRECATED: No longer needed
void ThreadCheck() { }
};
#endif /* BASE_PROFILER_H__ */

View File

@@ -0,0 +1,74 @@
// Copyright (c) 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ---
// Author: Sanjay Ghemawat
//
// Routine to extract the current stack trace.
#ifndef _GOOGLE_STACKTRACE_H
#define _GOOGLE_STACKTRACE_H
// Annoying stuff for windows -- makes sure clients can import these functions
#ifndef PERFTOOLS_DLL_DECL
# ifdef WIN32
# define PERFTOOLS_DLL_DECL __declspec(dllimport)
# else
# define PERFTOOLS_DLL_DECL
# endif
#endif
// Skips the most recent "skip_count" stack frames (also skips the
// frame generated for the "GetStackTrace" routine itself), and then
// records the pc values for up to the next "max_depth" frames in
// "result". Returns the number of values recorded in "result".
//
// Example:
// main() { foo(); }
// foo() { bar(); }
// bar() {
// void* result[10];
// int depth = GetStackTrace(result, 10, 1);
// }
//
// The GetStackTrace call will skip the frame for "bar". It will
// return 2 and will produce pc values that map to the following
// procedures:
// result[0] foo
// result[1] main
// (Actually, there may be a few more entries after "main" to account for
// startup procedures.)
//
// This routine may return fewer stack trace entries than are
// available.
extern PERFTOOLS_DLL_DECL int GetStackTrace(void** result, int max_depth,
int skip_count);
#endif /* _GOOGLE_STACKTRACE_H */