profile-handler.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. /* Copyright (c) 2009, 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: Nabeel Mian
  33. *
  34. * This module manages the cpu profile timers and the associated interrupt
  35. * handler. When enabled, all threads in the program are profiled.
  36. *
  37. * Any component interested in receiving a profile timer interrupt can do so by
  38. * registering a callback. All registered callbacks must be async-signal-safe.
  39. *
  40. * Note: This module requires the sole ownership of the configured timer and
  41. * signal. The timer defaults to ITIMER_PROF, can be changed to ITIMER_REAL by
  42. * the environment variable CPUPROFILE_REALTIME, or is changed to a POSIX timer
  43. * with CPUPROFILE_PER_THREAD_TIMERS. The signal defaults to SIGPROF/SIGALRM to
  44. * match the choice of timer and can be set to an arbitrary value using
  45. * CPUPROFILE_TIMER_SIGNAL with CPUPROFILE_PER_THREAD_TIMERS.
  46. */
  47. #ifndef BASE_PROFILE_HANDLER_H_
  48. #define BASE_PROFILE_HANDLER_H_
  49. #include "config.h"
  50. #include <signal.h>
  51. #ifdef COMPILER_MSVC
  52. #include "conflict-signal.h"
  53. #endif
  54. #include "base/basictypes.h"
  55. /* Forward declaration. */
  56. struct ProfileHandlerToken;
  57. /*
  58. * Callback function to be used with ProfilefHandlerRegisterCallback. This
  59. * function will be called in the context of SIGPROF signal handler and must
  60. * be async-signal-safe. The first three arguments are the values provided by
  61. * the SIGPROF signal handler. We use void* to avoid using ucontext_t on
  62. * non-POSIX systems.
  63. *
  64. * Requirements:
  65. * - Callback must be async-signal-safe.
  66. * - None of the functions in ProfileHandler are async-signal-safe. Therefore,
  67. * callback function *must* not call any of the ProfileHandler functions.
  68. * - Callback is not required to be re-entrant. At most one instance of
  69. * callback can run at a time.
  70. *
  71. * Notes:
  72. * - The SIGPROF signal handler saves and restores errno, so the callback
  73. * doesn't need to.
  74. * - Callback code *must* not acquire lock(s) to serialize access to data shared
  75. * with the code outside the signal handler (callback must be
  76. * async-signal-safe). If such a serialization is needed, follow the model
  77. * used by profiler.cc:
  78. *
  79. * When code other than the signal handler modifies the shared data it must:
  80. * - Acquire lock.
  81. * - Unregister the callback with the ProfileHandler.
  82. * - Modify shared data.
  83. * - Re-register the callback.
  84. * - Release lock.
  85. * and the callback code gets a lockless, read-write access to the data.
  86. */
  87. typedef void (*ProfileHandlerCallback)(int sig, siginfo_t* sig_info,
  88. void* ucontext, void* callback_arg);
  89. /*
  90. * Registers a new thread with profile handler and should be called only once
  91. * per thread. The main thread is registered at program startup. This routine
  92. * is called by the Thread module in google3/thread whenever a new thread is
  93. * created. This function is not async-signal-safe.
  94. */
  95. void ProfileHandlerRegisterThread();
  96. /*
  97. * Registers a callback routine. This callback function will be called in the
  98. * context of SIGPROF handler, so must be async-signal-safe. The returned token
  99. * is to be used when unregistering this callback via
  100. * ProfileHandlerUnregisterCallback. Registering the first callback enables
  101. * the SIGPROF signal handler. Caller must not free the returned token. This
  102. * function is not async-signal-safe.
  103. */
  104. ProfileHandlerToken* ProfileHandlerRegisterCallback(
  105. ProfileHandlerCallback callback, void* callback_arg);
  106. /*
  107. * Unregisters a previously registered callback. Expects the token returned
  108. * by the corresponding ProfileHandlerRegisterCallback and asserts that the
  109. * passed token is valid. Unregistering the last callback disables the SIGPROF
  110. * signal handler. It waits for the currently running callback to
  111. * complete before returning. This function is not async-signal-safe.
  112. */
  113. void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token);
  114. /*
  115. * FOR TESTING ONLY
  116. * Unregisters all the callbacks, stops the timers (if shared) and disables the
  117. * SIGPROF handler. All the threads, including the main thread, need to be
  118. * re-registered after this call. This function is not async-signal-safe.
  119. */
  120. void ProfileHandlerReset();
  121. /*
  122. * Stores profile handler's current state. This function is not
  123. * async-signal-safe.
  124. */
  125. struct ProfileHandlerState {
  126. int32 frequency; /* Profiling frequency */
  127. int32 callback_count; /* Number of callbacks registered */
  128. int64 interrupts; /* Number of interrupts received */
  129. bool allowed; /* Profiling is allowed */
  130. };
  131. void ProfileHandlerGetState(struct ProfileHandlerState* state);
  132. #endif /* BASE_PROFILE_HANDLER_H_ */