1 /* Copyright (c) 2009, 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.
33 * This module manages the cpu profile timers and the associated interrupt
34 * handler. When enabled, all registered threads in the program are profiled.
35 * (Note: if using linux 2.4 or earlier, you must use the Thread class, in
36 * google3/thread, to ensure all threads are profiled.)
38 * Any component interested in receiving a profile timer interrupt can do so by
39 * registering a callback. All registered callbacks must be async-signal-safe.
41 * Note: This module requires the sole ownership of ITIMER_PROF timer and the
45 #ifndef BASE_PROFILE_HANDLER_H_
46 #define BASE_PROFILE_HANDLER_H_
51 #include "conflict-signal.h"
53 #include "base/basictypes.h"
55 /* All this code should be usable from within C apps. */
60 /* Forward declaration. */
61 struct ProfileHandlerToken
;
64 * Callback function to be used with ProfilefHandlerRegisterCallback. This
65 * function will be called in the context of SIGPROF signal handler and must
66 * be async-signal-safe. The first three arguments are the values provided by
67 * the SIGPROF signal handler. We use void* to avoid using ucontext_t on
71 * - Callback must be async-signal-safe.
72 * - None of the functions in ProfileHandler are async-signal-safe. Therefore,
73 * callback function *must* not call any of the ProfileHandler functions.
74 * - Callback is not required to be re-entrant. At most one instance of
75 * callback can run at a time.
78 * - The SIGPROF signal handler saves and restores errno, so the callback
80 * - Callback code *must* not acquire lock(s) to serialize access to data shared
81 * with the code outside the signal handler (callback must be
82 * async-signal-safe). If such a serialization is needed, follow the model
83 * used by profiler.cc:
85 * When code other than the signal handler modifies the shared data it must:
87 * - Unregister the callback with the ProfileHandler.
88 * - Modify shared data.
89 * - Re-register the callback.
91 * and the callback code gets a lockless, read-write access to the data.
93 typedef void (*ProfileHandlerCallback
)(int sig
, siginfo_t
* sig_info
,
94 void* ucontext
, void* callback_arg
);
97 * Registers a new thread with profile handler and should be called only once
98 * per thread. The main thread is registered at program startup. This routine
99 * is called by the Thread module in google3/thread whenever a new thread is
100 * created. This function is not async-signal-safe.
102 void ProfileHandlerRegisterThread();
105 * Registers a callback routine. This callback function will be called in the
106 * context of SIGPROF handler, so must be async-signal-safe. The returned token
107 * is to be used when unregistering this callback via
108 * ProfileHandlerUnregisterCallback. Registering the first callback enables
109 * the SIGPROF signal handler. Caller must not free the returned token. This
110 * function is not async-signal-safe.
112 ProfileHandlerToken
* ProfileHandlerRegisterCallback(
113 ProfileHandlerCallback callback
, void* callback_arg
);
116 * Unregisters a previously registered callback. Expects the token returned
117 * by the corresponding ProfileHandlerRegisterCallback and asserts that the
118 * passed token is valid. Unregistering the last callback disables the SIGPROF
119 * signal handler. It waits for the currently running callback to
120 * complete before returning. This function is not async-signal-safe.
122 void ProfileHandlerUnregisterCallback(ProfileHandlerToken
* token
);
126 * Unregisters all the callbacks, stops the timers (if shared) and disables the
127 * SIGPROF handler. All the threads, including the main thread, need to be
128 * re-registered after this call. This function is not async-signal-safe.
130 void ProfileHandlerReset();
133 * Stores profile handler's current state. This function is not
136 struct ProfileHandlerState
{
137 int32 frequency
; /* Profiling frequency */
138 int32 callback_count
; /* Number of callbacks registered */
139 int64 interrupts
; /* Number of interrupts received */
140 bool allowed
; /* Profiling is allowed */
142 void ProfileHandlerGetState(struct ProfileHandlerState
* state
);
148 #endif /* BASE_PROFILE_HANDLER_H_ */