| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
// Copyright (c) 2006-2011 The Chromium Authors. 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 |
| 7 |
// are met: |
| 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 copyright |
| 11 |
// notice, this list of conditions and the following disclaimer in |
| 12 |
// the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google, Inc. nor the names of its contributors |
| 15 |
// may be used to endorse or promote products derived from this |
| 16 |
// software without specific prior written permission. |
| 17 |
// |
| 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 |
| 21 |
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 |
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 |
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 |
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 25 |
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 26 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 |
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 28 |
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 |
// SUCH DAMAGE. |
| 30 |
|
| 31 |
// This file is used for both Linux and Android. |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <math.h> |
| 35 |
|
| 36 |
#include <pthread.h> |
| 37 |
#if defined(GP_OS_freebsd) |
| 38 |
# include <sys/thr.h> |
| 39 |
#endif |
| 40 |
#include <semaphore.h> |
| 41 |
#include <signal.h> |
| 42 |
#include <sys/time.h> |
| 43 |
#include <sys/resource.h> |
| 44 |
#include <sys/syscall.h> |
| 45 |
#include <sys/types.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include <sched.h> |
| 48 |
#include <ucontext.h> |
| 49 |
// Ubuntu Dapper requires memory pages to be marked as |
| 50 |
// executable. Otherwise, OS raises an exception when executing code |
| 51 |
// in that page. |
| 52 |
#include <sys/types.h> // mmap & munmap |
| 53 |
#include <sys/mman.h> // mmap & munmap |
| 54 |
#include <sys/stat.h> // open |
| 55 |
#include <fcntl.h> // open |
| 56 |
#include <unistd.h> // sysconf |
| 57 |
#include <semaphore.h> |
| 58 |
#ifdef __GLIBC__ |
| 59 |
# include <execinfo.h> // backtrace, backtrace_symbols |
| 60 |
#endif // def __GLIBC__ |
| 61 |
#include <strings.h> // index |
| 62 |
#include <errno.h> |
| 63 |
#include <stdarg.h> |
| 64 |
|
| 65 |
#include "prenv.h" |
| 66 |
#include "mozilla/PodOperations.h" |
| 67 |
#include "mozilla/DebugOnly.h" |
| 68 |
|
| 69 |
#include <string.h> |
| 70 |
#include <list> |
| 71 |
|
| 72 |
using namespace mozilla; |
| 73 |
|
| 74 |
namespace mozilla { |
| 75 |
namespace baseprofiler { |
| 76 |
|
| 77 |
static int64_t MicrosecondsSince1970() { |
| 78 |
struct timeval tv; |
| 79 |
gettimeofday(&tv, NULL); |
| 80 |
return int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec); |
| 81 |
} |
| 82 |
|
| 83 |
void* GetStackTop(void* aGuess) { return aGuess; } |
| 84 |
|
| 85 |
static void PopulateRegsFromContext(Registers& aRegs, ucontext_t* aContext) { |
| 86 |
aRegs.mContext = aContext; |
| 87 |
mcontext_t& mcontext = aContext->uc_mcontext; |
| 88 |
|
| 89 |
// Extracting the sample from the context is extremely machine dependent. |
| 90 |
#if defined(GP_PLAT_x86_linux) || defined(GP_PLAT_x86_android) |
| 91 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); |
| 92 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); |
| 93 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); |
| 94 |
aRegs.mEcx = reinterpret_cast<Address>(mcontext.gregs[REG_ECX]); |
| 95 |
aRegs.mEdx = reinterpret_cast<Address>(mcontext.gregs[REG_EDX]); |
| 96 |
#elif defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android) |
| 97 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); |
| 98 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); |
| 99 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); |
| 100 |
aRegs.mR10 = reinterpret_cast<Address>(mcontext.gregs[REG_R10]); |
| 101 |
aRegs.mR12 = reinterpret_cast<Address>(mcontext.gregs[REG_R12]); |
| 102 |
#elif defined(GP_PLAT_amd64_freebsd) |
| 103 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.mc_rip); |
| 104 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.mc_rsp); |
| 105 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.mc_rbp); |
| 106 |
aRegs.mR10 = reinterpret_cast<Address>(mcontext.mc_r10); |
| 107 |
aRegs.mR12 = reinterpret_cast<Address>(mcontext.mc_r12); |
| 108 |
#elif defined(GP_PLAT_arm_linux) || defined(GP_PLAT_arm_android) |
| 109 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.arm_pc); |
| 110 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.arm_sp); |
| 111 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.arm_fp); |
| 112 |
aRegs.mLR = reinterpret_cast<Address>(mcontext.arm_lr); |
| 113 |
aRegs.mR7 = reinterpret_cast<Address>(mcontext.arm_r7); |
| 114 |
#elif defined(GP_PLAT_arm64_linux) || defined(GP_PLAT_arm64_android) |
| 115 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.pc); |
| 116 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.sp); |
| 117 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.regs[29]); |
| 118 |
aRegs.mLR = reinterpret_cast<Address>(mcontext.regs[30]); |
| 119 |
aRegs.mR11 = reinterpret_cast<Address>(mcontext.regs[11]); |
| 120 |
#elif defined(GP_PLAT_arm64_freebsd) |
| 121 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_elr); |
| 122 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_sp); |
| 123 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_x[29]); |
| 124 |
aRegs.mLR = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_lr); |
| 125 |
aRegs.mR11 = reinterpret_cast<Address>(mcontext.mc_gpregs.gp_x[11]); |
| 126 |
#elif defined(GP_PLAT_mips64_linux) || defined(GP_PLAT_mips64_android) |
| 127 |
aRegs.mPC = reinterpret_cast<Address>(mcontext.pc); |
| 128 |
aRegs.mSP = reinterpret_cast<Address>(mcontext.gregs[29]); |
| 129 |
aRegs.mFP = reinterpret_cast<Address>(mcontext.gregs[30]); |
| 130 |
|
| 131 |
#else |
| 132 |
# error "bad platform" |
| 133 |
#endif |
| 134 |
} |
| 135 |
|
| 136 |
#if defined(GP_OS_android) |
| 137 |
# define SYS_tgkill __NR_tgkill |
| 138 |
#endif |
| 139 |
|
| 140 |
#if defined(GP_OS_linux) || defined(GP_OS_android) |
| 141 |
int tgkill(pid_t tgid, pid_t tid, int signalno) { |
| 142 |
return syscall(SYS_tgkill, tgid, tid, signalno); |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
#if defined(GP_OS_freebsd) |
| 147 |
# define tgkill thr_kill2 |
| 148 |
#endif |
| 149 |
|
| 150 |
class PlatformData { |
| 151 |
public: |
| 152 |
explicit PlatformData(BaseProfilerThreadId aThreadId) {} |
| 153 |
|
| 154 |
~PlatformData() {} |
| 155 |
}; |
| 156 |
|
| 157 |
//////////////////////////////////////////////////////////////////////// |
| 158 |
// BEGIN Sampler target specifics |
| 159 |
|
| 160 |
// The only way to reliably interrupt a Linux thread and inspect its register |
| 161 |
// and stack state is by sending a signal to it, and doing the work inside the |
| 162 |
// signal handler. But we don't want to run much code inside the signal |
| 163 |
// handler, since POSIX severely restricts what we can do in signal handlers. |
| 164 |
// So we use a system of semaphores to suspend the thread and allow the |
| 165 |
// sampler thread to do all the work of unwinding and copying out whatever |
| 166 |
// data it wants. |
| 167 |
// |
| 168 |
// A four-message protocol is used to reliably suspend and later resume the |
| 169 |
// thread to be sampled (the samplee): |
| 170 |
// |
| 171 |
// Sampler (signal sender) thread Samplee (thread to be sampled) |
| 172 |
// |
| 173 |
// Prepare the SigHandlerCoordinator |
| 174 |
// and point sSigHandlerCoordinator at it |
| 175 |
// |
| 176 |
// send SIGPROF to samplee ------- MSG 1 ----> (enter signal handler) |
| 177 |
// wait(mMessage2) Copy register state |
| 178 |
// into sSigHandlerCoordinator |
| 179 |
// <------ MSG 2 ----- post(mMessage2) |
| 180 |
// Samplee is now suspended. wait(mMessage3) |
| 181 |
// Examine its stack/register |
| 182 |
// state at leisure |
| 183 |
// |
| 184 |
// Release samplee: |
| 185 |
// post(mMessage3) ------- MSG 3 -----> |
| 186 |
// wait(mMessage4) Samplee now resumes. Tell |
| 187 |
// the sampler that we are done. |
| 188 |
// <------ MSG 4 ------ post(mMessage4) |
| 189 |
// Now we know the samplee's signal (leave signal handler) |
| 190 |
// handler has finished using |
| 191 |
// sSigHandlerCoordinator. We can |
| 192 |
// safely reuse it for some other thread. |
| 193 |
// |
| 194 |
|
| 195 |
// A type used to coordinate between the sampler (signal sending) thread and |
| 196 |
// the thread currently being sampled (the samplee, which receives the |
| 197 |
// signals). |
| 198 |
// |
| 199 |
// The first message is sent using a SIGPROF signal delivery. The subsequent |
| 200 |
// three are sent using sem_wait/sem_post pairs. They are named accordingly |
| 201 |
// in the following struct. |
| 202 |
struct SigHandlerCoordinator { |
| 203 |
SigHandlerCoordinator() { |
| 204 |
PodZero(&mUContext); |
| 205 |
int r = sem_init(&mMessage2, /* pshared */ 0, 0); |
| 206 |
r |= sem_init(&mMessage3, /* pshared */ 0, 0); |
| 207 |
r |= sem_init(&mMessage4, /* pshared */ 0, 0); |
| 208 |
MOZ_ASSERT(r == 0); |
| 209 |
(void)r; |
| 210 |
} |
| 211 |
|
| 212 |
~SigHandlerCoordinator() { |
| 213 |
int r = sem_destroy(&mMessage2); |
| 214 |
r |= sem_destroy(&mMessage3); |
| 215 |
r |= sem_destroy(&mMessage4); |
| 216 |
MOZ_ASSERT(r == 0); |
| 217 |
(void)r; |
| 218 |
} |
| 219 |
|
| 220 |
sem_t mMessage2; // To sampler: "context is in sSigHandlerCoordinator" |
| 221 |
sem_t mMessage3; // To samplee: "resume" |
| 222 |
sem_t mMessage4; // To sampler: "finished with sSigHandlerCoordinator" |
| 223 |
ucontext_t mUContext; // Context at signal |
| 224 |
}; |
| 225 |
|
| 226 |
struct SigHandlerCoordinator* Sampler::sSigHandlerCoordinator = nullptr; |
| 227 |
|
| 228 |
static void SigprofHandler(int aSignal, siginfo_t* aInfo, void* aContext) { |
| 229 |
// Avoid TSan warning about clobbering errno. |
| 230 |
int savedErrno = errno; |
| 231 |
|
| 232 |
MOZ_ASSERT(aSignal == SIGPROF); |
| 233 |
MOZ_ASSERT(Sampler::sSigHandlerCoordinator); |
| 234 |
|
| 235 |
// By sending us this signal, the sampler thread has sent us message 1 in |
| 236 |
// the comment above, with the meaning "|sSigHandlerCoordinator| is ready |
| 237 |
// for use, please copy your register context into it." |
| 238 |
Sampler::sSigHandlerCoordinator->mUContext = |
| 239 |
*static_cast<ucontext_t*>(aContext); |
| 240 |
|
| 241 |
// Send message 2: tell the sampler thread that the context has been copied |
| 242 |
// into |sSigHandlerCoordinator->mUContext|. sem_post can never fail by |
| 243 |
// being interrupted by a signal, so there's no loop around this call. |
| 244 |
int r = sem_post(&Sampler::sSigHandlerCoordinator->mMessage2); |
| 245 |
MOZ_ASSERT(r == 0); |
| 246 |
|
| 247 |
// At this point, the sampler thread assumes we are suspended, so we must |
| 248 |
// not touch any global state here. |
| 249 |
|
| 250 |
// Wait for message 3: the sampler thread tells us to resume. |
| 251 |
while (true) { |
| 252 |
r = sem_wait(&Sampler::sSigHandlerCoordinator->mMessage3); |
| 253 |
if (r == -1 && errno == EINTR) { |
| 254 |
// Interrupted by a signal. Try again. |
| 255 |
continue; |
| 256 |
} |
| 257 |
// We don't expect any other kind of failure |
| 258 |
MOZ_ASSERT(r == 0); |
| 259 |
break; |
| 260 |
} |
| 261 |
|
| 262 |
// Send message 4: tell the sampler thread that we are finished accessing |
| 263 |
// |sSigHandlerCoordinator|. After this point it is not safe to touch |
| 264 |
// |sSigHandlerCoordinator|. |
| 265 |
r = sem_post(&Sampler::sSigHandlerCoordinator->mMessage4); |
| 266 |
MOZ_ASSERT(r == 0); |
| 267 |
|
| 268 |
errno = savedErrno; |
| 269 |
} |
| 270 |
|
| 271 |
Sampler::Sampler(PSLockRef aLock) : mMyPid(profiler_current_process_id()) { |
| 272 |
#if defined(USE_EHABI_STACKWALK) |
| 273 |
EHABIStackWalkInit(); |
| 274 |
#endif |
| 275 |
|
| 276 |
// NOTE: We don't initialize LUL here, instead initializing it in |
| 277 |
// SamplerThread's constructor. This is because with the |
| 278 |
// profiler_suspend_and_sample_thread entry point, we want to be able to |
| 279 |
// sample without waiting for LUL to be initialized. |
| 280 |
|
| 281 |
// Request profiling signals. |
| 282 |
struct sigaction sa; |
| 283 |
sa.sa_sigaction = SigprofHandler; |
| 284 |
sigemptyset(&sa.sa_mask); |
| 285 |
sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 286 |
if (sigaction(SIGPROF, &sa, &mOldSigprofHandler) != 0) { |
| 287 |
MOZ_CRASH("Error installing SIGPROF handler in the profiler"); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
void Sampler::Disable(PSLockRef aLock) { |
| 292 |
// Restore old signal handler. This is global state so it's important that |
| 293 |
// we do it now, while gPSMutex is locked. |
| 294 |
sigaction(SIGPROF, &mOldSigprofHandler, 0); |
| 295 |
} |
| 296 |
|
| 297 |
template <typename Func> |
| 298 |
void Sampler::SuspendAndSampleAndResumeThread( |
| 299 |
PSLockRef aLock, const RegisteredThread& aRegisteredThread, |
| 300 |
const TimeStamp& aNow, const Func& aProcessRegs) { |
| 301 |
// Only one sampler thread can be sampling at once. So we expect to have |
| 302 |
// complete control over |sSigHandlerCoordinator|. |
| 303 |
MOZ_ASSERT(!sSigHandlerCoordinator); |
| 304 |
|
| 305 |
if (!mSamplerTid.IsSpecified()) { |
| 306 |
mSamplerTid = profiler_current_thread_id(); |
| 307 |
} |
| 308 |
BaseProfilerThreadId sampleeTid = aRegisteredThread.Info()->ThreadId(); |
| 309 |
MOZ_RELEASE_ASSERT(sampleeTid != mSamplerTid); |
| 310 |
|
| 311 |
//----------------------------------------------------------------// |
| 312 |
// Suspend the samplee thread and get its context. |
| 313 |
|
| 314 |
SigHandlerCoordinator coord; // on sampler thread's stack |
| 315 |
sSigHandlerCoordinator = &coord; |
| 316 |
|
| 317 |
// Send message 1 to the samplee (the thread to be sampled), by |
| 318 |
// signalling at it. |
| 319 |
// This could fail if the thread doesn't exist anymore. |
| 320 |
int r = tgkill(mMyPid.ToNumber(), sampleeTid.ToNumber(), SIGPROF); |
| 321 |
if (r == 0) { |
| 322 |
// Wait for message 2 from the samplee, indicating that the context |
| 323 |
// is available and that the thread is suspended. |
| 324 |
while (true) { |
| 325 |
r = sem_wait(&sSigHandlerCoordinator->mMessage2); |
| 326 |
if (r == -1 && errno == EINTR) { |
| 327 |
// Interrupted by a signal. Try again. |
| 328 |
continue; |
| 329 |
} |
| 330 |
// We don't expect any other kind of failure. |
| 331 |
MOZ_ASSERT(r == 0); |
| 332 |
break; |
| 333 |
} |
| 334 |
|
| 335 |
//----------------------------------------------------------------// |
| 336 |
// Sample the target thread. |
| 337 |
|
| 338 |
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 339 |
// |
| 340 |
// The profiler's "critical section" begins here. In the critical section, |
| 341 |
// we must not do any dynamic memory allocation, nor try to acquire any lock |
| 342 |
// or any other unshareable resource. This is because the thread to be |
| 343 |
// sampled has been suspended at some entirely arbitrary point, and we have |
| 344 |
// no idea which unsharable resources (locks, essentially) it holds. So any |
| 345 |
// attempt to acquire any lock, including the implied locks used by the |
| 346 |
// malloc implementation, risks deadlock. This includes TimeStamp::Now(), |
| 347 |
// which gets a lock on Windows. |
| 348 |
|
| 349 |
// The samplee thread is now frozen and sSigHandlerCoordinator->mUContext is |
| 350 |
// valid. We can poke around in it and unwind its stack as we like. |
| 351 |
|
| 352 |
// Extract the current register values. |
| 353 |
Registers regs; |
| 354 |
PopulateRegsFromContext(regs, &sSigHandlerCoordinator->mUContext); |
| 355 |
aProcessRegs(regs, aNow); |
| 356 |
|
| 357 |
//----------------------------------------------------------------// |
| 358 |
// Resume the target thread. |
| 359 |
|
| 360 |
// Send message 3 to the samplee, which tells it to resume. |
| 361 |
r = sem_post(&sSigHandlerCoordinator->mMessage3); |
| 362 |
MOZ_ASSERT(r == 0); |
| 363 |
|
| 364 |
// Wait for message 4 from the samplee, which tells us that it has |
| 365 |
// finished with |sSigHandlerCoordinator|. |
| 366 |
while (true) { |
| 367 |
r = sem_wait(&sSigHandlerCoordinator->mMessage4); |
| 368 |
if (r == -1 && errno == EINTR) { |
| 369 |
continue; |
| 370 |
} |
| 371 |
MOZ_ASSERT(r == 0); |
| 372 |
break; |
| 373 |
} |
| 374 |
|
| 375 |
// The profiler's critical section ends here. After this point, none of the |
| 376 |
// critical section limitations documented above apply. |
| 377 |
// |
| 378 |
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 379 |
} |
| 380 |
|
| 381 |
// This isn't strictly necessary, but doing so does help pick up anomalies |
| 382 |
// in which the signal handler is running when it shouldn't be. |
| 383 |
sSigHandlerCoordinator = nullptr; |
| 384 |
} |
| 385 |
|
| 386 |
// END Sampler target specifics |
| 387 |
//////////////////////////////////////////////////////////////////////// |
| 388 |
|
| 389 |
//////////////////////////////////////////////////////////////////////// |
| 390 |
// BEGIN SamplerThread target specifics |
| 391 |
|
| 392 |
static void* ThreadEntry(void* aArg) { |
| 393 |
auto thread = static_cast<SamplerThread*>(aArg); |
| 394 |
thread->Run(); |
| 395 |
return nullptr; |
| 396 |
} |
| 397 |
|
| 398 |
SamplerThread::SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, |
| 399 |
double aIntervalMilliseconds, uint32_t aFeatures) |
| 400 |
: mSampler(aLock), |
| 401 |
mActivityGeneration(aActivityGeneration), |
| 402 |
mIntervalMicroseconds( |
| 403 |
std::max(1, int(floor(aIntervalMilliseconds * 1000 + 0.5)))) { |
| 404 |
// Start the sampling thread. It repeatedly sends a SIGPROF signal. Sending |
| 405 |
// the signal ourselves instead of relying on itimer provides much better |
| 406 |
// accuracy. |
| 407 |
if (pthread_create(&mThread, nullptr, ThreadEntry, this) != 0) { |
| 408 |
MOZ_CRASH("pthread_create failed"); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
SamplerThread::~SamplerThread() { pthread_join(mThread, nullptr); } |
| 413 |
|
| 414 |
void SamplerThread::SleepMicro(uint32_t aMicroseconds) { |
| 415 |
if (aMicroseconds >= 1000000) { |
| 416 |
// Use usleep for larger intervals, because the nanosleep |
| 417 |
// code below only supports intervals < 1 second. |
| 418 |
MOZ_ALWAYS_TRUE(!::usleep(aMicroseconds)); |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
struct timespec ts; |
| 423 |
ts.tv_sec = 0; |
| 424 |
ts.tv_nsec = aMicroseconds * 1000UL; |
| 425 |
|
| 426 |
int rv = ::nanosleep(&ts, &ts); |
| 427 |
|
| 428 |
while (rv != 0 && errno == EINTR) { |
| 429 |
// Keep waiting in case of interrupt. |
| 430 |
// nanosleep puts the remaining time back into ts. |
| 431 |
rv = ::nanosleep(&ts, &ts); |
| 432 |
} |
| 433 |
|
| 434 |
MOZ_ASSERT(!rv, "nanosleep call failed"); |
| 435 |
} |
| 436 |
|
| 437 |
void SamplerThread::Stop(PSLockRef aLock) { |
| 438 |
// Restore old signal handler. This is global state so it's important that |
| 439 |
// we do it now, while gPSMutex is locked. It's safe to do this now even |
| 440 |
// though this SamplerThread is still alive, because the next time the main |
| 441 |
// loop of Run() iterates it won't get past the mActivityGeneration check, |
| 442 |
// and so won't send any signals. |
| 443 |
mSampler.Disable(aLock); |
| 444 |
} |
| 445 |
|
| 446 |
// END SamplerThread target specifics |
| 447 |
//////////////////////////////////////////////////////////////////////// |
| 448 |
|
| 449 |
#if defined(GP_OS_linux) || defined(GP_OS_freebsd) |
| 450 |
|
| 451 |
// We use pthread_atfork() to temporarily disable signal delivery during any |
| 452 |
// fork() call. Without that, fork() can be repeatedly interrupted by signal |
| 453 |
// delivery, requiring it to be repeatedly restarted, which can lead to *long* |
| 454 |
// delays. See bug 837390. |
| 455 |
// |
| 456 |
// We provide no paf_child() function to run in the child after forking. This |
| 457 |
// is fine because we always immediately exec() after fork(), and exec() |
| 458 |
// clobbers all process state. Also, we don't want the sampler to resume in the |
| 459 |
// child process between fork() and exec(), it would be wasteful. |
| 460 |
// |
| 461 |
// Unfortunately all this is only doable on non-Android because Bionic doesn't |
| 462 |
// have pthread_atfork. |
| 463 |
|
| 464 |
// In the parent, before the fork, increase gSkipSampling to ensure that |
| 465 |
// profiler sampling loops will be skipped. There could be one in progress now, |
| 466 |
// causing a small delay, but further sampling will be skipped, allowing `fork` |
| 467 |
// to complete. |
| 468 |
static void paf_prepare() { ++gSkipSampling; } |
| 469 |
|
| 470 |
// In the parent, after the fork, decrease gSkipSampling to let the sampler |
| 471 |
// resume sampling (unless other places have made it non-zero as well). |
| 472 |
static void paf_parent() { --gSkipSampling; } |
| 473 |
|
| 474 |
static void PlatformInit(PSLockRef aLock) { |
| 475 |
// Set up the fork handlers. |
| 476 |
pthread_atfork(paf_prepare, paf_parent, nullptr); |
| 477 |
} |
| 478 |
|
| 479 |
#else |
| 480 |
|
| 481 |
static void PlatformInit(PSLockRef aLock) {} |
| 482 |
|
| 483 |
#endif |
| 484 |
|
| 485 |
#if defined(HAVE_NATIVE_UNWIND) |
| 486 |
// TODO port getcontext from breakpad, if profiler_get_backtrace is needed. |
| 487 |
# define REGISTERS_SYNC_POPULATE(regs) \ |
| 488 |
MOZ_CRASH("profiler_get_backtrace() unsupported"); |
| 489 |
#endif |
| 490 |
|
| 491 |
} // namespace baseprofiler |
| 492 |
} // namespace mozilla |