blob: 4f96a18bc113ad61e23fd177efccf9640093bc33 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- 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 registered threads in the program are profiled.
36 * (Note: if using linux 2.4 or earlier, you must use the Thread class, in
37 * google3/thread, to ensure all threads are profiled.)
38 *
39 * Any component interested in receiving a profile timer interrupt can do so by
40 * registering a callback. All registered callbacks must be async-signal-safe.
41 *
42 * Note: This module requires the sole ownership of ITIMER_PROF timer and the
43 * SIGPROF signal.
44 */
45
46#ifndef BASE_PROFILE_HANDLER_H_
47#define BASE_PROFILE_HANDLER_H_
48
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
56/* All this code should be usable from within C apps. */
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/* Forward declaration. */
62struct ProfileHandlerToken;
63
64/*
65 * Callback function to be used with ProfilefHandlerRegisterCallback. This
66 * function will be called in the context of SIGPROF signal handler and must
67 * be async-signal-safe. The first three arguments are the values provided by
68 * the SIGPROF signal handler. We use void* to avoid using ucontext_t on
69 * non-POSIX systems.
70 *
71 * Requirements:
72 * - Callback must be async-signal-safe.
73 * - None of the functions in ProfileHandler are async-signal-safe. Therefore,
74 * callback function *must* not call any of the ProfileHandler functions.
75 * - Callback is not required to be re-entrant. At most one instance of
76 * callback can run at a time.
77 *
78 * Notes:
79 * - The SIGPROF signal handler saves and restores errno, so the callback
80 * doesn't need to.
81 * - Callback code *must* not acquire lock(s) to serialize access to data shared
82 * with the code outside the signal handler (callback must be
83 * async-signal-safe). If such a serialization is needed, follow the model
84 * used by profiler.cc:
85 *
86 * When code other than the signal handler modifies the shared data it must:
87 * - Acquire lock.
88 * - Unregister the callback with the ProfileHandler.
89 * - Modify shared data.
90 * - Re-register the callback.
91 * - Release lock.
92 * and the callback code gets a lockless, read-write access to the data.
93 */
94typedef void (*ProfileHandlerCallback)(int sig, siginfo_t* sig_info,
95 void* ucontext, void* callback_arg);
96
97/*
98 * Registers a new thread with profile handler and should be called only once
99 * per thread. The main thread is registered at program startup. This routine
100 * is called by the Thread module in google3/thread whenever a new thread is
101 * created. This function is not async-signal-safe.
102 */
103void ProfileHandlerRegisterThread();
104
105/*
106 * Registers a callback routine. This callback function will be called in the
107 * context of SIGPROF handler, so must be async-signal-safe. The returned token
108 * is to be used when unregistering this callback via
109 * ProfileHandlerUnregisterCallback. Registering the first callback enables
110 * the SIGPROF signal handler. Caller must not free the returned token. This
111 * function is not async-signal-safe.
112 */
113ProfileHandlerToken* ProfileHandlerRegisterCallback(
114 ProfileHandlerCallback callback, void* callback_arg);
115
116/*
117 * Unregisters a previously registered callback. Expects the token returned
118 * by the corresponding ProfileHandlerRegisterCallback and asserts that the
119 * passed token is valid. Unregistering the last callback disables the SIGPROF
120 * signal handler. It waits for the currently running callback to
121 * complete before returning. This function is not async-signal-safe.
122 */
123void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token);
124
125/*
126 * FOR TESTING ONLY
127 * Unregisters all the callbacks, stops the timers (if shared) and disables the
128 * SIGPROF handler. All the threads, including the main thread, need to be
129 * re-registered after this call. This function is not async-signal-safe.
130 */
131void ProfileHandlerReset();
132
133/*
134 * Stores profile handler's current state. This function is not
135 * async-signal-safe.
136 */
137struct ProfileHandlerState {
138 int32 frequency; /* Profiling frequency */
139 int32 callback_count; /* Number of callbacks registered */
140 int64 interrupts; /* Number of interrupts received */
141 bool allowed; /* Profiling is allowed */
142};
143void ProfileHandlerGetState(struct ProfileHandlerState* state);
144
145#ifdef __cplusplus
146} /* extern "C" */
147#endif
148
149#endif /* BASE_PROFILE_HANDLER_H_ */