profiler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 CPU profiling based on periodic pc-sampling.
  35. *
  36. * For full(er) information, see doc/cpuprofile.html
  37. *
  38. * This module is 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. * "CPUPROFILE" 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 "ProfilerStart(filename)" and "ProfilerStop()".
  48. *
  49. *
  50. * (Note: if using linux 2.4 or earlier, only the main thread may be
  51. * profiled.)
  52. *
  53. * Use pprof to view the resulting profile output.
  54. * % pprof <path_to_executable> <profile_file_name>
  55. * % pprof --gv <path_to_executable> <profile_file_name>
  56. *
  57. * These functions are thread-safe.
  58. */
  59. #ifndef BASE_PROFILER_H_
  60. #define BASE_PROFILER_H_
  61. #include <time.h> /* For time_t */
  62. /* Annoying stuff for windows; makes sure clients can import these functions */
  63. #ifndef PERFTOOLS_DLL_DECL
  64. # ifdef _WIN32
  65. # define PERFTOOLS_DLL_DECL __declspec(dllimport)
  66. # else
  67. # define PERFTOOLS_DLL_DECL
  68. # endif
  69. #endif
  70. /* All this code should be usable from within C apps. */
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. /* Profiler options, for use with ProfilerStartWithOptions. To use:
  75. *
  76. * struct ProfilerOptions options;
  77. * memset(&options, 0, sizeof options);
  78. *
  79. * then fill in fields as needed.
  80. *
  81. * This structure is intended to be usable from C code, so no constructor
  82. * is provided to initialize it. (Use memset as described above).
  83. */
  84. struct ProfilerOptions {
  85. /* Filter function and argument.
  86. *
  87. * If filter_in_thread is not NULL, when a profiling tick is delivered
  88. * the profiler will call:
  89. *
  90. * (*filter_in_thread)(filter_in_thread_arg)
  91. *
  92. * If it returns nonzero, the sample will be included in the profile.
  93. * Note that filter_in_thread runs in a signal handler, so must be
  94. * async-signal-safe.
  95. *
  96. * A typical use would be to set up filter results for each thread
  97. * in the system before starting the profiler, then to make
  98. * filter_in_thread be a very simple function which retrieves those
  99. * results in an async-signal-safe way. Retrieval could be done
  100. * using thread-specific data, or using a shared data structure that
  101. * supports async-signal-safe lookups.
  102. */
  103. int (*filter_in_thread)(void *arg);
  104. void *filter_in_thread_arg;
  105. };
  106. /* Start profiling and write profile info into fname, discarding any
  107. * existing profiling data in that file.
  108. *
  109. * This is equivalent to calling ProfilerStartWithOptions(fname, NULL).
  110. */
  111. PERFTOOLS_DLL_DECL int ProfilerStart(const char* fname);
  112. /* Start profiling and write profile into fname, discarding any
  113. * existing profiling data in that file.
  114. *
  115. * The profiler is configured using the options given by 'options'.
  116. * Options which are not specified are given default values.
  117. *
  118. * 'options' may be NULL, in which case all are given default values.
  119. *
  120. * Returns nonzero if profiling was started successfully, or zero else.
  121. */
  122. PERFTOOLS_DLL_DECL int ProfilerStartWithOptions(
  123. const char *fname, const struct ProfilerOptions *options);
  124. /* Stop profiling. Can be started again with ProfilerStart(), but
  125. * the currently accumulated profiling data will be cleared.
  126. */
  127. PERFTOOLS_DLL_DECL void ProfilerStop(void);
  128. /* Flush any currently buffered profiling state to the profile file.
  129. * Has no effect if the profiler has not been started.
  130. */
  131. PERFTOOLS_DLL_DECL void ProfilerFlush(void);
  132. /* DEPRECATED: these functions were used to enable/disable profiling
  133. * in the current thread, but no longer do anything.
  134. */
  135. PERFTOOLS_DLL_DECL void ProfilerEnable(void);
  136. PERFTOOLS_DLL_DECL void ProfilerDisable(void);
  137. /* Returns nonzero if profile is currently enabled, zero if it's not. */
  138. PERFTOOLS_DLL_DECL int ProfilingIsEnabledForAllThreads(void);
  139. /* Routine for registering new threads with the profiler.
  140. */
  141. PERFTOOLS_DLL_DECL void ProfilerRegisterThread(void);
  142. /* Stores state about profiler's current status into "*state". */
  143. struct ProfilerState {
  144. int enabled; /* Is profiling currently enabled? */
  145. time_t start_time; /* If enabled, when was profiling started? */
  146. char profile_name[1024]; /* Name of profile file being written, or '\0' */
  147. int samples_gathered; /* Number of samples gathered so far (or 0) */
  148. };
  149. PERFTOOLS_DLL_DECL void ProfilerGetCurrentState(struct ProfilerState* state);
  150. #ifdef __cplusplus
  151. } // extern "C"
  152. #endif
  153. #endif /* BASE_PROFILER_H_ */