15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Copyright (c) 2009, Google Inc.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions are
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * met:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in the documentation and/or other materials provided with the
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distribution.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * this software without specific prior written permission.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ---
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Author: Nabeel Mian
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This module manages the cpu profile timers and the associated interrupt
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * handler. When enabled, all registered threads in the program are profiled.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (Note: if using linux 2.4 or earlier, you must use the Thread class, in
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * google3/thread, to ensure all threads are profiled.)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Any component interested in receiving a profile timer interrupt can do so by
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * registering a callback. All registered callbacks must be async-signal-safe.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Note: This module requires the sole ownership of ITIMER_PROF timer and the
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SIGPROF signal.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_PROFILE_HANDLER_H_
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_PROFILE_HANDLER_H_
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "config.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <signal.h>
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef COMPILER_MSVC
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "conflict-signal.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* All this code should be usable from within C apps. */
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef __cplusplus
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)extern "C" {
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Forward declaration. */
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct ProfileHandlerToken;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Callback function to be used with ProfilefHandlerRegisterCallback. This
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * function will be called in the context of SIGPROF signal handler and must
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * be async-signal-safe. The first three arguments are the values provided by
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the SIGPROF signal handler. We use void* to avoid using ucontext_t on
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * non-POSIX systems.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Requirements:
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Callback must be async-signal-safe.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - None of the functions in ProfileHandler are async-signal-safe. Therefore,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   callback function *must* not call any of the ProfileHandler functions.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Callback is not required to be re-entrant. At most one instance of
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   callback can run at a time.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Notes:
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - The SIGPROF signal handler saves and restores errno, so the callback
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   doesn't need to.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - Callback code *must* not acquire lock(s) to serialize access to data shared
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   with the code outside the signal handler (callback must be
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   async-signal-safe). If such a serialization is needed, follow the model
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   used by profiler.cc:
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   When code other than the signal handler modifies the shared data it must:
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   - Acquire lock.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   - Unregister the callback with the ProfileHandler.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   - Modify shared data.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   - Re-register the callback.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   - Release lock.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *   and the callback code gets a lockless, read-write access to the data.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef void (*ProfileHandlerCallback)(int sig, siginfo_t* sig_info,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       void* ucontext, void* callback_arg);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Registers a new thread with profile handler and should be called only once
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * per thread. The main thread is registered at program startup. This routine
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * is called by the Thread module in google3/thread whenever a new thread is
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * created. This function is not async-signal-safe.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileHandlerRegisterThread();
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Registers a callback routine. This callback function will be called in the
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * context of SIGPROF handler, so must be async-signal-safe. The returned token
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * is to be used when unregistering this callback via
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ProfileHandlerUnregisterCallback. Registering the first callback enables
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the SIGPROF signal handler. Caller must not free the returned token. This
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * function is not async-signal-safe.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileHandlerToken* ProfileHandlerRegisterCallback(
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProfileHandlerCallback callback, void* callback_arg);
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unregisters a previously registered callback. Expects the token returned
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * by the corresponding ProfileHandlerRegisterCallback and asserts that the
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * passed token is valid. Unregistering the last callback disables the SIGPROF
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * signal handler. It waits for the currently running callback to
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * complete before returning. This function is not async-signal-safe.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileHandlerUnregisterCallback(ProfileHandlerToken* token);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * FOR TESTING ONLY
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unregisters all the callbacks, stops the timers (if shared) and disables the
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SIGPROF handler. All the threads, including the main thread, need to be
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * re-registered after this call. This function is not async-signal-safe.
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileHandlerReset();
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Stores profile handler's current state. This function is not
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * async-signal-safe.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct ProfileHandlerState {
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int32 frequency;  /* Profiling frequency */
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int32 callback_count;  /* Number of callbacks registered */
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 interrupts;  /* Number of interrupts received */
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool allowed; /* Profiling is allowed */
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileHandlerGetState(struct ProfileHandlerState* state);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef __cplusplus
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  /* extern "C" */
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  /* BASE_PROFILE_HANDLER_H_ */
149