1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
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.
31 // Author: Sanjay Ghemawat
32 // Chris Demetriou (refactoring)
34 // Profile current program by sampling stack-trace every so often
37 #include "getpc.h" // should be first to get the _GNU_SOURCE dfn
44 #include <unistd.h> // for getpid()
46 #if defined(HAVE_SYS_UCONTEXT_H)
47 #include <sys/ucontext.h>
48 #elif defined(HAVE_UCONTEXT_H)
50 #elif defined(HAVE_CYGWIN_SIGNAL_H)
51 #include <cygwin/signal.h>
52 typedef ucontext ucontext_t
;
54 typedef int ucontext_t
; // just to quiet the compiler, mostly
58 #include <gperftools/profiler.h>
59 #include <gperftools/stacktrace.h>
60 #include "base/commandlineflags.h"
61 #include "base/logging.h"
62 #include "base/googleinit.h"
63 #include "base/spinlock.h"
64 #include "base/sysinfo.h" /* for GetUniquePathFromEnv, etc */
65 #include "profiledata.h"
66 #include "profile-handler.h"
67 #ifdef HAVE_CONFLICT_SIGNAL_H
68 #include "conflict-signal.h" /* used on msvc machines */
73 // Collects up all profile data. This is a singleton, which is
74 // initialized by a constructor at startup.
80 // Start profiler to write profile info into fname
81 bool Start(const char* fname
, const ProfilerOptions
* options
);
83 // Stop profiling and write the data to disk.
86 // Write the data to disk (and continue profiling).
91 void GetCurrentState(ProfilerState
* state
);
93 static CpuProfiler instance_
;
96 // This lock implements the locking requirements described in the ProfileData
97 // documentation, specifically:
99 // lock_ is held all over all collector_ method calls except for the 'Add'
100 // call made from the signal handler, to protect against concurrent use of
101 // collector_'s control routines. Code other than signal handler must
102 // unregister the signal handler before calling any collector_ method.
103 // 'Add' method in the collector is protected by a guarantee from
104 // ProfileHandle that only one instance of prof_handler can run at a time.
106 ProfileData collector_
;
108 // Filter function and its argument, if any. (NULL means include all
109 // samples). Set at start, read-only while running. Written while holding
110 // lock_, read and executed in the context of SIGPROF interrupt.
111 int (*filter_
)(void*);
114 // Opaque token returned by the profile handler. To be used when calling
115 // ProfileHandlerUnregisterCallback.
116 ProfileHandlerToken
* prof_handler_token_
;
118 // Sets up a callback to receive SIGPROF interrupt.
119 void EnableHandler();
121 // Disables receiving SIGPROF interrupt.
122 void DisableHandler();
124 // Signal handler that records the interrupted pc in the profile data.
125 static void prof_handler(int sig
, siginfo_t
*, void* signal_ucontext
,
129 // Profile data structure singleton: Constructor will check to see if
130 // profiling should be enabled. Destructor will write profile data
132 CpuProfiler
CpuProfiler::instance_
;
134 // Initialize profiling: activated if getenv("CPUPROFILE") exists.
135 CpuProfiler::CpuProfiler()
136 : prof_handler_token_(NULL
) {
137 // TODO(cgd) Move this code *out* of the CpuProfile constructor into a
138 // separate object responsible for initialization. With ProfileHandler there
139 // is no need to limit the number of profilers.
140 char fname
[PATH_MAX
];
141 if (!GetUniquePathFromEnv("CPUPROFILE", fname
)) {
144 // We don't enable profiling if setuid -- it's a security risk
146 if (getuid() != geteuid())
150 if (!Start(fname
, NULL
)) {
151 RAW_LOG(FATAL
, "Can't turn on cpu profiling for '%s': %s\n",
152 fname
, strerror(errno
));
156 bool CpuProfiler::Start(const char* fname
, const ProfilerOptions
* options
) {
157 SpinLockHolder
cl(&lock_
);
159 if (collector_
.enabled()) {
163 ProfileHandlerState prof_handler_state
;
164 ProfileHandlerGetState(&prof_handler_state
);
166 ProfileData::Options collector_options
;
167 collector_options
.set_frequency(prof_handler_state
.frequency
);
168 if (!collector_
.Start(fname
, collector_options
)) {
173 if (options
!= NULL
&& options
->filter_in_thread
!= NULL
) {
174 filter_
= options
->filter_in_thread
;
175 filter_arg_
= options
->filter_in_thread_arg
;
178 // Setup handler for SIGPROF interrupts
184 CpuProfiler::~CpuProfiler() {
188 // Stop profiling and write out any collected profile data
189 void CpuProfiler::Stop() {
190 SpinLockHolder
cl(&lock_
);
192 if (!collector_
.enabled()) {
196 // Unregister prof_handler to stop receiving SIGPROF interrupts before
197 // stopping the collector.
200 // DisableHandler waits for the currently running callback to complete and
201 // guarantees no future invocations. It is safe to stop the collector.
205 void CpuProfiler::FlushTable() {
206 SpinLockHolder
cl(&lock_
);
208 if (!collector_
.enabled()) {
212 // Unregister prof_handler to stop receiving SIGPROF interrupts before
213 // flushing the profile data.
216 // DisableHandler waits for the currently running callback to complete and
217 // guarantees no future invocations. It is safe to flush the profile data.
218 collector_
.FlushTable();
223 bool CpuProfiler::Enabled() {
224 SpinLockHolder
cl(&lock_
);
225 return collector_
.enabled();
228 void CpuProfiler::GetCurrentState(ProfilerState
* state
) {
229 ProfileData::State collector_state
;
231 SpinLockHolder
cl(&lock_
);
232 collector_
.GetCurrentState(&collector_state
);
235 state
->enabled
= collector_state
.enabled
;
236 state
->start_time
= static_cast<time_t>(collector_state
.start_time
);
237 state
->samples_gathered
= collector_state
.samples_gathered
;
238 int buf_size
= sizeof(state
->profile_name
);
239 strncpy(state
->profile_name
, collector_state
.profile_name
, buf_size
);
240 state
->profile_name
[buf_size
-1] = '\0';
243 void CpuProfiler::EnableHandler() {
244 RAW_CHECK(prof_handler_token_
== NULL
, "SIGPROF handler already registered");
245 prof_handler_token_
= ProfileHandlerRegisterCallback(prof_handler
, this);
246 RAW_CHECK(prof_handler_token_
!= NULL
, "Failed to set up SIGPROF handler");
249 void CpuProfiler::DisableHandler() {
250 RAW_CHECK(prof_handler_token_
!= NULL
, "SIGPROF handler is not registered");
251 ProfileHandlerUnregisterCallback(prof_handler_token_
);
252 prof_handler_token_
= NULL
;
255 // Signal handler that records the pc in the profile-data structure. We do no
256 // synchronization here. profile-handler.cc guarantees that at most one
257 // instance of prof_handler() will run at a time. All other routines that
258 // access the data touched by prof_handler() disable this signal handler before
259 // accessing the data and therefore cannot execute concurrently with
261 void CpuProfiler::prof_handler(int sig
, siginfo_t
*, void* signal_ucontext
,
262 void* cpu_profiler
) {
263 CpuProfiler
* instance
= static_cast<CpuProfiler
*>(cpu_profiler
);
265 if (instance
->filter_
== NULL
||
266 (*instance
->filter_
)(instance
->filter_arg_
)) {
267 void* stack
[ProfileData::kMaxStackDepth
];
269 // The top-most active routine doesn't show up as a normal
270 // frame, but as the "pc" value in the signal handler context.
271 stack
[0] = GetPC(*reinterpret_cast<ucontext_t
*>(signal_ucontext
));
273 // We skip the top two stack trace entries (this function and one
274 // signal handler frame) since they are artifacts of profiling and
275 // should not be measured. Other profiling related frames may be
276 // removed by "pprof" at analysis time. Instead of skipping the top
277 // frames, we could skip nothing, but that would increase the
278 // profile size unnecessarily.
279 int depth
= GetStackTraceWithContext(stack
+ 1, arraysize(stack
) - 1,
281 depth
++; // To account for pc value in stack[0];
283 instance
->collector_
.Add(depth
, stack
);
287 #if !(defined(__CYGWIN__) || defined(__CYGWIN32__))
289 extern "C" PERFTOOLS_DLL_DECL
void ProfilerRegisterThread() {
290 ProfileHandlerRegisterThread();
293 extern "C" PERFTOOLS_DLL_DECL
void ProfilerFlush() {
294 CpuProfiler::instance_
.FlushTable();
297 extern "C" PERFTOOLS_DLL_DECL
int ProfilingIsEnabledForAllThreads() {
298 return CpuProfiler::instance_
.Enabled();
301 extern "C" PERFTOOLS_DLL_DECL
int ProfilerStart(const char* fname
) {
302 return CpuProfiler::instance_
.Start(fname
, NULL
);
305 extern "C" PERFTOOLS_DLL_DECL
int ProfilerStartWithOptions(
306 const char *fname
, const ProfilerOptions
*options
) {
307 return CpuProfiler::instance_
.Start(fname
, options
);
310 extern "C" PERFTOOLS_DLL_DECL
void ProfilerStop() {
311 CpuProfiler::instance_
.Stop();
314 extern "C" PERFTOOLS_DLL_DECL
void ProfilerGetCurrentState(
315 ProfilerState
* state
) {
316 CpuProfiler::instance_
.GetCurrentState(state
);
321 // ITIMER_PROF doesn't work under cygwin. ITIMER_REAL is available, but doesn't
322 // work as well for profiling, and also interferes with alarm(). Because of
323 // these issues, unless a specific need is identified, profiler support is
324 // disabled under Cygwin.
325 extern "C" void ProfilerRegisterThread() { }
326 extern "C" void ProfilerFlush() { }
327 extern "C" int ProfilingIsEnabledForAllThreads() { return 0; }
328 extern "C" int ProfilerStart(const char* fname
) { return 0; }
329 extern "C" int ProfilerStartWithOptions(const char *fname
,
330 const ProfilerOptions
*options
) {
333 extern "C" void ProfilerStop() { }
334 extern "C" void ProfilerGetCurrentState(ProfilerState
* state
) {
335 memset(state
, 0, sizeof(*state
));
340 // DEPRECATED routines
341 extern "C" PERFTOOLS_DLL_DECL
void ProfilerEnable() { }
342 extern "C" PERFTOOLS_DLL_DECL
void ProfilerDisable() { }