heap-profiler.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. /* Copyright (c) 2005, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * ---
  32. * Author: Sanjay Ghemawat
  33. *
  34. * Module for heap-profiling.
  35. *
  36. * For full(er) information, see doc/heapprofile.html
  37. *
  38. * This module can be linked into your program with
  39. * no slowdown caused by this unless you activate the profiler
  40. * using one of the following methods:
  41. *
  42. * 1. Before starting the program, set the environment variable
  43. * "HEAPPROFILE" to be the name of the file to which the profile
  44. * data should be written.
  45. *
  46. * 2. Programmatically, start and stop the profiler using the
  47. * routines "HeapProfilerStart(filename)" and "HeapProfilerStop()".
  48. *
  49. */
  50. #ifndef BASE_HEAP_PROFILER_H_
  51. #define BASE_HEAP_PROFILER_H_
  52. #include <stddef.h>
  53. /* Annoying stuff for windows; makes sure clients can import these functions */
  54. #ifndef PERFTOOLS_DLL_DECL
  55. # ifdef _WIN32
  56. # define PERFTOOLS_DLL_DECL __declspec(dllimport)
  57. # else
  58. # define PERFTOOLS_DLL_DECL
  59. # endif
  60. #endif
  61. /* All this code should be usable from within C apps. */
  62. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /* Start profiling and arrange to write profile data to file names
  66. * of the form: "prefix.0000", "prefix.0001", ...
  67. */
  68. PERFTOOLS_DLL_DECL void HeapProfilerStart(const char* prefix);
  69. /* Returns non-zero if we are currently profiling the heap. (Returns
  70. * an int rather than a bool so it's usable from C.) This is true
  71. * between calls to HeapProfilerStart() and HeapProfilerStop(), and
  72. * also if the program has been run with HEAPPROFILER, or some other
  73. * way to turn on whole-program profiling.
  74. */
  75. int IsHeapProfilerRunning();
  76. /* Stop heap profiling. Can be restarted again with HeapProfilerStart(),
  77. * but the currently accumulated profiling information will be cleared.
  78. */
  79. PERFTOOLS_DLL_DECL void HeapProfilerStop();
  80. /* Dump a profile now - can be used for dumping at a hopefully
  81. * quiescent state in your program, in order to more easily track down
  82. * memory leaks. Will include the reason in the logged message
  83. */
  84. PERFTOOLS_DLL_DECL void HeapProfilerDump(const char *reason);
  85. /* Generate current heap profiling information.
  86. * Returns an empty string when heap profiling is not active.
  87. * The returned pointer is a '\0'-terminated string allocated using malloc()
  88. * and should be free()-ed as soon as the caller does not need it anymore.
  89. */
  90. PERFTOOLS_DLL_DECL char* GetHeapProfile();
  91. #ifdef __cplusplus
  92. } // extern "C"
  93. #endif
  94. #endif /* BASE_HEAP_PROFILER_H_ */