2011-08-26 17:05:37 -07:00
|
|
|
// Copyright (c) 2006-2011 The Chromium Authors. All rights reserved.
|
2012-11-19 11:02:32 -08:00
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in
|
|
|
|
// the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google, Inc. nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this
|
|
|
|
// software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
// SUCH DAMAGE.
|
2011-08-26 17:05:37 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
# vim: sw=2
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2013-03-26 09:34:27 -07:00
|
|
|
#include <math.h>
|
2011-08-26 17:05:37 -07:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
2013-09-11 10:57:43 -07:00
|
|
|
#include <sys/prctl.h> // set name
|
2011-08-26 17:05:37 -07:00
|
|
|
#include <stdlib.h>
|
2013-03-29 12:34:49 -07:00
|
|
|
#include <sched.h>
|
2011-08-26 17:05:37 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#else
|
|
|
|
#define __android_log_print(a, ...)
|
|
|
|
#endif
|
2013-09-25 08:28:34 -07:00
|
|
|
#include <ucontext.h>
|
2011-08-26 17:05:37 -07:00
|
|
|
// Ubuntu Dapper requires memory pages to be marked as
|
|
|
|
// executable. Otherwise, OS raises an exception when executing code
|
|
|
|
// in that page.
|
|
|
|
#include <sys/types.h> // mmap & munmap
|
|
|
|
#include <sys/mman.h> // mmap & munmap
|
|
|
|
#include <sys/stat.h> // open
|
|
|
|
#include <fcntl.h> // open
|
|
|
|
#include <unistd.h> // sysconf
|
2013-07-11 20:41:19 -07:00
|
|
|
#include <semaphore.h>
|
2011-08-26 17:05:37 -07:00
|
|
|
#ifdef __GLIBC__
|
|
|
|
#include <execinfo.h> // backtrace, backtrace_symbols
|
|
|
|
#endif // def __GLIBC__
|
|
|
|
#include <strings.h> // index
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "platform.h"
|
2013-09-27 09:08:45 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2013-03-29 12:34:49 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-07-11 20:41:19 -07:00
|
|
|
#include "mozilla/Atomics.h"
|
2013-03-29 12:34:49 -07:00
|
|
|
#include "ProfileEntry.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-04-03 15:59:17 -07:00
|
|
|
#include "TableTicker.h"
|
2013-05-28 05:03:38 -07:00
|
|
|
#include "UnwinderThread2.h"
|
2013-09-11 11:53:14 -07:00
|
|
|
#if defined(__ARM_EABI__) && defined(MOZ_WIDGET_GONK)
|
|
|
|
// Should also work on other Android and ARM Linux, but not tested there yet.
|
|
|
|
#define USE_EHABI_STACKWALK
|
|
|
|
#include "EHABIStackWalk.h"
|
|
|
|
#endif
|
2011-08-26 17:05:37 -07:00
|
|
|
|
2014-05-19 11:31:31 -07:00
|
|
|
// Memory profile
|
|
|
|
#include "nsMemoryReporterManager.h"
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2013-03-29 12:34:49 -07:00
|
|
|
#include <list>
|
2011-08-26 17:05:37 -07:00
|
|
|
|
2014-01-22 02:56:45 -08:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
|
|
|
|
2011-12-08 14:35:36 -08:00
|
|
|
#define SIGNAL_SAVE_PROFILE SIGUSR2
|
2011-08-26 17:05:37 -07:00
|
|
|
|
2011-12-15 12:44:13 -08:00
|
|
|
#if defined(__GLIBC__)
|
|
|
|
// glibc doesn't implement gettid(2).
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
pid_t gettid()
|
|
|
|
{
|
|
|
|
return (pid_t) syscall(SYS_gettid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-25 08:28:34 -07:00
|
|
|
/* static */ Thread::tid_t
|
|
|
|
Thread::GetCurrentId()
|
|
|
|
{
|
|
|
|
return gettid();
|
|
|
|
}
|
|
|
|
|
2013-04-16 11:53:03 -07:00
|
|
|
#if !defined(ANDROID)
|
|
|
|
// Keep track of when any of our threads calls fork(), so we can
|
|
|
|
// temporarily disable signal delivery during the fork() call. Not
|
|
|
|
// doing so appears to cause a kind of race, in which signals keep
|
|
|
|
// getting delivered to the thread doing fork(), which keeps causing
|
|
|
|
// it to fail and be restarted; hence forward progress is delayed a
|
|
|
|
// great deal. A side effect of this is to permanently disable
|
|
|
|
// sampling in the child process. See bug 837390.
|
|
|
|
|
|
|
|
// Unfortunately this is only doable on non-Android, since Bionic
|
|
|
|
// doesn't have pthread_atfork.
|
|
|
|
|
|
|
|
// This records the current state at the time we paused it.
|
|
|
|
static bool was_paused = false;
|
|
|
|
|
|
|
|
// In the parent, just before the fork, record the pausedness state,
|
|
|
|
// and then pause.
|
|
|
|
static void paf_prepare(void) {
|
2013-03-29 12:34:49 -07:00
|
|
|
if (Sampler::GetActiveSampler()) {
|
|
|
|
was_paused = Sampler::GetActiveSampler()->IsPaused();
|
|
|
|
Sampler::GetActiveSampler()->SetPaused(true);
|
2013-04-16 11:53:03 -07:00
|
|
|
} else {
|
|
|
|
was_paused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the parent, just after the fork, return pausedness to the
|
|
|
|
// pre-fork state.
|
|
|
|
static void paf_parent(void) {
|
2013-03-29 12:34:49 -07:00
|
|
|
if (Sampler::GetActiveSampler())
|
|
|
|
Sampler::GetActiveSampler()->SetPaused(was_paused);
|
2013-04-16 11:53:03 -07:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:32:40 -07:00
|
|
|
// Set up the fork handlers.
|
2013-04-16 11:53:03 -07:00
|
|
|
static void* setup_atfork() {
|
|
|
|
pthread_atfork(paf_prepare, paf_parent, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* !defined(ANDROID) */
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
struct SamplerRegistry {
|
|
|
|
static void AddActiveSampler(Sampler *sampler) {
|
|
|
|
ASSERT(!SamplerRegistry::sampler);
|
|
|
|
SamplerRegistry::sampler = sampler;
|
|
|
|
}
|
|
|
|
static void RemoveActiveSampler(Sampler *sampler) {
|
|
|
|
SamplerRegistry::sampler = NULL;
|
|
|
|
}
|
|
|
|
static Sampler *sampler;
|
|
|
|
};
|
|
|
|
|
|
|
|
Sampler *SamplerRegistry::sampler = NULL;
|
|
|
|
|
2013-07-11 20:41:19 -07:00
|
|
|
static mozilla::Atomic<ThreadProfile*> sCurrentThreadProfile;
|
|
|
|
static sem_t sSignalHandlingDone;
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
static void ProfilerSaveSignalHandler(int signal, siginfo_t* info, void* context) {
|
2013-03-29 12:34:49 -07:00
|
|
|
Sampler::GetActiveSampler()->RequestSave();
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
2013-09-25 08:28:34 -07:00
|
|
|
static void SetSampleContext(TickSample* sample, void* context)
|
|
|
|
{
|
|
|
|
// Extracting the sample from the context is extremely machine dependent.
|
|
|
|
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
|
|
|
|
mcontext_t& mcontext = ucontext->uc_mcontext;
|
|
|
|
#if V8_HOST_ARCH_IA32
|
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]);
|
|
|
|
#elif V8_HOST_ARCH_X64
|
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]);
|
|
|
|
#elif V8_HOST_ARCH_ARM
|
|
|
|
// An undefined macro evaluates to 0, so this applies to Android's Bionic also.
|
|
|
|
#if !defined(ANDROID) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
|
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]);
|
|
|
|
#ifdef ENABLE_ARM_LR_SAVING
|
|
|
|
sample->lr = reinterpret_cast<Address>(mcontext.gregs[R14]);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.arm_pc);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.arm_sp);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.arm_fp);
|
|
|
|
#ifdef ENABLE_ARM_LR_SAVING
|
|
|
|
sample->lr = reinterpret_cast<Address>(mcontext.arm_lr);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#elif V8_HOST_ARCH_MIPS
|
|
|
|
// Implement this on MIPS.
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#define V8_HOST_ARCH_ARM 1
|
|
|
|
#define SYS_gettid __NR_gettid
|
|
|
|
#define SYS_tgkill __NR_tgkill
|
|
|
|
#else
|
|
|
|
#define V8_HOST_ARCH_X64 1
|
|
|
|
#endif
|
|
|
|
static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
|
2013-08-13 13:44:46 -07:00
|
|
|
if (!Sampler::GetActiveSampler()) {
|
|
|
|
sem_post(&sSignalHandlingDone);
|
2011-08-26 17:05:37 -07:00
|
|
|
return;
|
2013-08-13 13:44:46 -07:00
|
|
|
}
|
2011-08-26 17:05:37 -07:00
|
|
|
|
|
|
|
TickSample sample_obj;
|
|
|
|
TickSample* sample = &sample_obj;
|
2012-03-02 11:11:47 -08:00
|
|
|
sample->context = context;
|
2011-08-26 17:05:37 -07:00
|
|
|
|
2012-01-10 15:02:00 -08:00
|
|
|
#ifdef ENABLE_SPS_LEAF_DATA
|
2011-08-26 17:05:37 -07:00
|
|
|
// If profiling, we extract the current pc and sp.
|
2013-03-29 12:34:49 -07:00
|
|
|
if (Sampler::GetActiveSampler()->IsProfiling()) {
|
2013-09-25 08:28:34 -07:00
|
|
|
SetSampleContext(sample, context);
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-03-29 12:34:49 -07:00
|
|
|
sample->threadProfile = sCurrentThreadProfile;
|
2012-01-10 15:02:00 -08:00
|
|
|
sample->timestamp = mozilla::TimeStamp::Now();
|
2014-05-19 11:31:31 -07:00
|
|
|
sample->rssMemory = sample->threadProfile->mRssMemory;
|
2012-01-10 15:02:00 -08:00
|
|
|
|
2013-03-29 12:34:49 -07:00
|
|
|
Sampler::GetActiveSampler()->Tick(sample);
|
|
|
|
|
|
|
|
sCurrentThreadProfile = NULL;
|
2013-07-11 20:41:19 -07:00
|
|
|
sem_post(&sSignalHandlingDone);
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
2014-05-19 11:31:31 -07:00
|
|
|
static void ProfilerSignalThread(ThreadProfile *profile,
|
|
|
|
bool isFirstProfiledThread)
|
|
|
|
{
|
|
|
|
if (isFirstProfiledThread && Sampler::GetActiveSampler()->ProfileMemory()) {
|
|
|
|
profile->mRssMemory = nsMemoryReporterManager::ResidentFast();
|
|
|
|
} else {
|
|
|
|
profile->mRssMemory = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 02:56:45 -08:00
|
|
|
// If the Nuwa process is enabled, we need to use the wrapper of tgkill() to
|
|
|
|
// perform the mapping of thread ID.
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
extern "C" MFBT_API int tgkill(pid_t tgid, pid_t tid, int signalno);
|
|
|
|
#else
|
2013-03-29 12:34:49 -07:00
|
|
|
int tgkill(pid_t tgid, pid_t tid, int signalno) {
|
|
|
|
return syscall(SYS_tgkill, tgid, tid, signalno);
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
2014-01-22 02:56:45 -08:00
|
|
|
#endif
|
2011-08-26 17:05:37 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
class PlatformData : public Malloced {
|
2011-08-26 17:05:37 -07:00
|
|
|
public:
|
2013-04-03 15:59:17 -07:00
|
|
|
PlatformData()
|
|
|
|
{}
|
|
|
|
};
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
/* static */ PlatformData*
|
|
|
|
Sampler::AllocPlatformData(int aThreadId)
|
|
|
|
{
|
|
|
|
return new PlatformData;
|
|
|
|
}
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
/* static */ void
|
|
|
|
Sampler::FreePlatformData(PlatformData* aData)
|
|
|
|
{
|
|
|
|
delete aData;
|
|
|
|
}
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
static void* SignalSender(void* arg) {
|
2013-09-11 10:57:43 -07:00
|
|
|
// Taken from platform_thread_posix.cc
|
|
|
|
prctl(PR_SET_NAME, "SamplerThread", 0, 0, 0);
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2014-01-22 02:56:45 -08:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
// If the Nuwa process is enabled, we need to mark and freeze the sampler
|
|
|
|
// thread in the Nuwa process and have this thread recreated in the spawned
|
|
|
|
// child.
|
|
|
|
if(IsNuwaProcess()) {
|
|
|
|
NuwaMarkCurrentThread(nullptr, nullptr);
|
|
|
|
// Freeze the thread here so the spawned child will get the correct tgid
|
|
|
|
// from the getpid() call below.
|
|
|
|
NuwaFreezeCurrentThread();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
int vm_tgid_ = getpid();
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
while (SamplerRegistry::sampler->IsActive()) {
|
|
|
|
SamplerRegistry::sampler->HandleSaveRequest();
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
if (!SamplerRegistry::sampler->IsPaused()) {
|
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
|
|
|
std::vector<ThreadInfo*> threads =
|
|
|
|
SamplerRegistry::sampler->GetRegisteredThreads();
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2014-05-19 11:31:31 -07:00
|
|
|
bool isFirstProfiledThread = true;
|
2013-04-03 15:59:17 -07:00
|
|
|
for (uint32_t i = 0; i < threads.size(); i++) {
|
|
|
|
ThreadInfo* info = threads[i];
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
// This will be null if we're not interested in profiling this thread.
|
|
|
|
if (!info->Profile())
|
|
|
|
continue;
|
2013-04-18 05:15:09 -07:00
|
|
|
|
2014-03-28 13:08:22 -07:00
|
|
|
PseudoStack::SleepState sleeping = info->Stack()->observeSleeping();
|
|
|
|
if (sleeping == PseudoStack::SLEEPING_AGAIN) {
|
|
|
|
info->Profile()->DuplicateLastSample();
|
|
|
|
//XXX: This causes flushes regardless of jank-only mode
|
|
|
|
info->Profile()->flush();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
// We use sCurrentThreadProfile the ThreadProfile for the
|
|
|
|
// thread we're profiling to the signal handler
|
|
|
|
sCurrentThreadProfile = info->Profile();
|
|
|
|
|
|
|
|
int threadId = info->ThreadId();
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2014-05-19 11:31:31 -07:00
|
|
|
// Profile from the signal sender for information which is not signal
|
|
|
|
// safe, and will have low variation between the emission of the signal
|
|
|
|
// and the signal handler catch.
|
|
|
|
ProfilerSignalThread(sCurrentThreadProfile, isFirstProfiledThread);
|
|
|
|
|
|
|
|
// Profile from the signal handler for information which is signal safe
|
|
|
|
// and needs to be precise too, such as the stack of the interrupted
|
|
|
|
// thread.
|
2013-04-03 15:59:17 -07:00
|
|
|
if (tgkill(vm_tgid_, threadId, SIGPROF) != 0) {
|
|
|
|
printf_stderr("profiler failed to signal tid=%d\n", threadId);
|
|
|
|
#ifdef DEBUG
|
|
|
|
abort();
|
2013-04-18 05:15:09 -07:00
|
|
|
#endif
|
2013-04-03 15:59:17 -07:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-18 05:15:09 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
// Wait for the signal handler to run before moving on to the next one
|
2013-07-11 20:41:19 -07:00
|
|
|
sem_wait(&sSignalHandlingDone);
|
2014-05-19 11:31:31 -07:00
|
|
|
isFirstProfiledThread = false;
|
2013-04-03 15:59:17 -07:00
|
|
|
}
|
|
|
|
}
|
2013-04-18 05:15:09 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
// Convert ms to us and subtract 100 us to compensate delays
|
|
|
|
// occuring during signal delivery.
|
|
|
|
// TODO measure and confirm this.
|
2013-03-26 09:34:27 -07:00
|
|
|
int interval = floor(SamplerRegistry::sampler->interval() * 1000 + 0.5) - 100;
|
|
|
|
if (interval <= 0) {
|
|
|
|
interval = 1;
|
|
|
|
}
|
|
|
|
OS::SleepMicro(interval);
|
2013-04-03 15:59:17 -07:00
|
|
|
}
|
2014-03-12 13:32:40 -07:00
|
|
|
return 0;
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
2013-03-26 09:34:27 -07:00
|
|
|
Sampler::Sampler(double interval, bool profiling, int entrySize)
|
2011-08-26 17:05:37 -07:00
|
|
|
: interval_(interval),
|
|
|
|
profiling_(profiling),
|
2012-06-13 16:24:48 -07:00
|
|
|
paused_(false),
|
2013-03-29 12:34:49 -07:00
|
|
|
active_(false),
|
|
|
|
entrySize_(entrySize) {
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Sampler::~Sampler() {
|
2013-04-03 15:59:17 -07:00
|
|
|
ASSERT(!signal_sender_launched_);
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sampler::Start() {
|
2013-03-10 15:00:23 -07:00
|
|
|
LOG("Sampler started");
|
2013-04-03 15:59:17 -07:00
|
|
|
|
2013-09-11 11:53:14 -07:00
|
|
|
#ifdef USE_EHABI_STACKWALK
|
|
|
|
mozilla::EHABIStackWalkInit();
|
|
|
|
#endif
|
2013-04-03 15:59:17 -07:00
|
|
|
SamplerRegistry::AddActiveSampler(this);
|
|
|
|
|
2013-07-11 20:41:19 -07:00
|
|
|
// Initialize signal handler communication
|
|
|
|
sCurrentThreadProfile = NULL;
|
|
|
|
if (sem_init(&sSignalHandlingDone, /* pshared: */ 0, /* value: */ 0) != 0) {
|
|
|
|
LOG("Error initializing semaphore");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
// Request profiling signals.
|
|
|
|
LOG("Request signal");
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_sigaction = ProfilerSignalHandler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
2013-04-03 15:59:17 -07:00
|
|
|
if (sigaction(SIGPROF, &sa, &old_sigprof_signal_handler_) != 0) {
|
2011-08-26 17:05:37 -07:00
|
|
|
LOG("Error installing signal");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request save profile signals
|
|
|
|
struct sigaction sa2;
|
|
|
|
sa2.sa_sigaction = ProfilerSaveSignalHandler;
|
|
|
|
sigemptyset(&sa2.sa_mask);
|
|
|
|
sa2.sa_flags = SA_RESTART | SA_SIGINFO;
|
2013-04-03 15:59:17 -07:00
|
|
|
if (sigaction(SIGNAL_SAVE_PROFILE, &sa2, &old_sigsave_signal_handler_) != 0) {
|
2011-08-26 17:05:37 -07:00
|
|
|
LOG("Error installing start signal");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG("Signal installed");
|
2013-04-03 15:59:17 -07:00
|
|
|
signal_handler_installed_ = true;
|
2011-08-26 17:05:37 -07:00
|
|
|
|
|
|
|
// Start a thread that sends SIGPROF signal to VM thread.
|
|
|
|
// Sending the signal ourselves instead of relying on itimer provides
|
|
|
|
// much better accuracy.
|
2011-10-31 10:25:04 -07:00
|
|
|
SetActive(true);
|
2011-08-26 17:05:37 -07:00
|
|
|
if (pthread_create(
|
2013-04-03 15:59:17 -07:00
|
|
|
&signal_sender_thread_, NULL, SignalSender, NULL) == 0) {
|
|
|
|
signal_sender_launched_ = true;
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
LOG("Profiler thread started");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sampler::Stop() {
|
2011-10-31 10:25:04 -07:00
|
|
|
SetActive(false);
|
2011-08-26 17:05:37 -07:00
|
|
|
|
|
|
|
// Wait for signal sender termination (it will exit after setting
|
|
|
|
// active_ to false).
|
2013-04-03 15:59:17 -07:00
|
|
|
if (signal_sender_launched_) {
|
|
|
|
pthread_join(signal_sender_thread_, NULL);
|
|
|
|
signal_sender_launched_ = false;
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
SamplerRegistry::RemoveActiveSampler(this);
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
// Restore old signal handler
|
2013-04-03 15:59:17 -07:00
|
|
|
if (signal_handler_installed_) {
|
|
|
|
sigaction(SIGNAL_SAVE_PROFILE, &old_sigsave_signal_handler_, 0);
|
|
|
|
sigaction(SIGPROF, &old_sigprof_signal_handler_, 0);
|
|
|
|
signal_handler_installed_ = false;
|
2013-03-29 12:34:49 -07:00
|
|
|
}
|
2013-03-29 12:34:49 -07:00
|
|
|
}
|
|
|
|
|
2013-05-28 05:03:38 -07:00
|
|
|
bool Sampler::RegisterCurrentThread(const char* aName,
|
|
|
|
PseudoStack* aPseudoStack,
|
|
|
|
bool aIsMainThread, void* stackTop)
|
2013-03-29 12:34:49 -07:00
|
|
|
{
|
2013-04-18 08:34:49 -07:00
|
|
|
if (!Sampler::sRegisteredThreadsMutex)
|
|
|
|
return false;
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
|
|
|
|
2013-09-24 12:23:36 -07:00
|
|
|
int id = gettid();
|
|
|
|
for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
|
|
|
|
ThreadInfo* info = sRegisteredThreads->at(i);
|
|
|
|
if (info->ThreadId() == id) {
|
|
|
|
// Thread already registered. This means the first unregister will be
|
|
|
|
// too early.
|
|
|
|
ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 08:28:34 -07:00
|
|
|
set_tls_stack_top(stackTop);
|
|
|
|
|
2013-09-24 12:23:36 -07:00
|
|
|
ThreadInfo* info = new ThreadInfo(aName, id,
|
2013-09-11 11:50:46 -07:00
|
|
|
aIsMainThread, aPseudoStack, stackTop);
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-05-16 13:31:50 -07:00
|
|
|
if (sActiveSampler) {
|
|
|
|
sActiveSampler->RegisterThread(info);
|
2013-03-29 12:34:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sRegisteredThreads->push_back(info);
|
2013-05-28 05:03:38 -07:00
|
|
|
|
|
|
|
uwt__register_thread_for_profiling(stackTop);
|
2013-03-29 12:34:49 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sampler::UnregisterCurrentThread()
|
|
|
|
{
|
2013-04-18 08:34:49 -07:00
|
|
|
if (!Sampler::sRegisteredThreadsMutex)
|
|
|
|
return;
|
|
|
|
|
2013-09-25 08:28:34 -07:00
|
|
|
tlsStackTop.set(nullptr);
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
2013-03-29 12:34:49 -07:00
|
|
|
|
|
|
|
int id = gettid();
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-03-29 12:34:49 -07:00
|
|
|
for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
|
|
|
|
ThreadInfo* info = sRegisteredThreads->at(i);
|
|
|
|
if (info->ThreadId() == id) {
|
|
|
|
delete info;
|
|
|
|
sRegisteredThreads->erase(sRegisteredThreads->begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-05-28 05:03:38 -07:00
|
|
|
|
|
|
|
uwt__unregister_thread_for_profiling();
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 08:28:04 -07:00
|
|
|
#ifdef ANDROID
|
2012-07-11 07:55:22 -07:00
|
|
|
static struct sigaction old_sigstart_signal_handler;
|
2013-09-11 11:01:16 -07:00
|
|
|
const int SIGSTART = SIGUSR2;
|
2012-03-02 11:11:47 -08:00
|
|
|
|
2013-10-24 17:09:33 -07:00
|
|
|
static void freeArray(const char** array, int size) {
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
free((void*) array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t readCSVArray(char* csvList, const char** buffer) {
|
|
|
|
uint32_t count;
|
|
|
|
char* savePtr;
|
|
|
|
int newlinePos = strlen(csvList) - 1;
|
|
|
|
if (csvList[newlinePos] == '\n') {
|
|
|
|
csvList[newlinePos] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
char* item = strtok_r(csvList, ",", &savePtr);
|
|
|
|
for (count = 0; item; item = strtok_r(NULL, ",", &savePtr)) {
|
|
|
|
int length = strlen(item) + 1; // Include \0
|
|
|
|
char* newBuf = (char*) malloc(sizeof(char) * length);
|
|
|
|
buffer[count] = newBuf;
|
|
|
|
strncpy(newBuf, item, length);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently support only the env variables
|
|
|
|
// reported in read_profiler_env
|
|
|
|
static void ReadProfilerVars(const char* fileName, const char** features,
|
|
|
|
uint32_t* featureCount, const char** threadNames, uint32_t* threadCount) {
|
|
|
|
FILE* file = fopen(fileName, "r");
|
|
|
|
const int bufferSize = 1024;
|
|
|
|
char line[bufferSize];
|
|
|
|
char* feature;
|
|
|
|
char* value;
|
|
|
|
char* savePtr;
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
while (fgets(line, bufferSize, file) != NULL) {
|
|
|
|
feature = strtok_r(line, "=", &savePtr);
|
|
|
|
value = strtok_r(NULL, "", &savePtr);
|
|
|
|
|
|
|
|
if (strncmp(feature, PROFILER_MODE, bufferSize) == 0) {
|
|
|
|
set_profiler_mode(value);
|
|
|
|
} else if (strncmp(feature, PROFILER_INTERVAL, bufferSize) == 0) {
|
|
|
|
set_profiler_interval(value);
|
|
|
|
} else if (strncmp(feature, PROFILER_ENTRIES, bufferSize) == 0) {
|
|
|
|
set_profiler_entries(value);
|
|
|
|
} else if (strncmp(feature, PROFILER_STACK, bufferSize) == 0) {
|
|
|
|
set_profiler_scan(value);
|
|
|
|
} else if (strncmp(feature, PROFILER_FEATURES, bufferSize) == 0) {
|
|
|
|
*featureCount = readCSVArray(value, features);
|
|
|
|
} else if (strncmp(feature, "threads", bufferSize) == 0) {
|
|
|
|
*threadCount = readCSVArray(value, threadNames);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 11:01:16 -07:00
|
|
|
|
2013-10-24 17:09:33 -07:00
|
|
|
static void StartSignalHandler(int signal, siginfo_t* info, void* context) {
|
2013-09-11 11:01:16 -07:00
|
|
|
// XXX: Everything we do here is NOT async signal safe. We risk nasty things
|
|
|
|
// like deadlocks but we typically only do this once so it tends to be ok.
|
|
|
|
// See bug 909403
|
2013-10-24 17:09:33 -07:00
|
|
|
uint32_t featureCount = 0;
|
2013-09-11 11:01:16 -07:00
|
|
|
uint32_t threadCount = 0;
|
|
|
|
|
2013-10-24 17:09:33 -07:00
|
|
|
// Just allocate 10 features for now
|
|
|
|
// FIXME: these don't really point to const chars*
|
|
|
|
// So we free them later, but we don't want to change the const char**
|
|
|
|
// declaration in profiler_start. Annoying but ok for now.
|
|
|
|
const char* threadNames[10];
|
|
|
|
const char* features[10];
|
|
|
|
const char* profilerConfigFile = "/data/local/tmp/profiler.options";
|
|
|
|
|
|
|
|
ReadProfilerVars(profilerConfigFile, features, &featureCount, threadNames, &threadCount);
|
|
|
|
MOZ_ASSERT(featureCount < 10);
|
|
|
|
MOZ_ASSERT(threadCount < 10);
|
2013-09-11 11:01:16 -07:00
|
|
|
|
|
|
|
profiler_start(PROFILE_DEFAULT_ENTRY, 1,
|
2013-10-24 17:09:33 -07:00
|
|
|
features, featureCount,
|
|
|
|
threadNames, threadCount);
|
|
|
|
|
|
|
|
freeArray(threadNames, threadCount);
|
|
|
|
freeArray(features, featureCount);
|
2012-03-02 11:11:47 -08:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:32:40 -07:00
|
|
|
void OS::Startup()
|
2012-03-02 11:11:47 -08:00
|
|
|
{
|
2012-07-11 07:55:22 -07:00
|
|
|
LOG("Registering start signal");
|
2012-03-02 11:11:47 -08:00
|
|
|
struct sigaction sa;
|
2012-07-11 07:55:22 -07:00
|
|
|
sa.sa_sigaction = StartSignalHandler;
|
2012-03-02 11:11:47 -08:00
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
2012-07-11 07:55:22 -07:00
|
|
|
if (sigaction(SIGSTART, &sa, &old_sigstart_signal_handler) != 0) {
|
2012-03-02 11:11:47 -08:00
|
|
|
LOG("Error installing signal");
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 13:32:40 -07:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
void OS::Startup() {
|
|
|
|
// Set up the fork handlers.
|
|
|
|
setup_atfork();
|
|
|
|
}
|
|
|
|
|
2012-07-11 08:28:04 -07:00
|
|
|
#endif
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2014-03-12 13:32:40 -07:00
|
|
|
|
|
|
|
|
2013-09-25 08:28:34 -07:00
|
|
|
void TickSample::PopulateContext(void* aContext)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContext);
|
|
|
|
ucontext_t* pContext = reinterpret_cast<ucontext_t*>(aContext);
|
|
|
|
if (!getcontext(pContext)) {
|
|
|
|
context = pContext;
|
|
|
|
SetSampleContext(this, aContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 12:43:34 -08:00
|
|
|
// WARNING: Works with values up to 1 second
|
2013-03-26 09:34:27 -07:00
|
|
|
void OS::SleepMicro(int microseconds)
|
|
|
|
{
|
2014-03-07 12:43:34 -08:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = microseconds * 1000UL;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// in the case of interrupt we keep waiting
|
|
|
|
// nanosleep puts the remaining to back into ts
|
|
|
|
if (!nanosleep(&ts, &ts) || errno != EINTR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-03-26 09:34:27 -07:00
|
|
|
}
|
|
|
|
|