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>
|
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#else
|
|
|
|
#define __android_log_print(a, ...)
|
|
|
|
#endif
|
|
|
|
// 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
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
#include <execinfo.h> // backtrace, backtrace_symbols
|
|
|
|
#endif // def __GLIBC__
|
|
|
|
#include <strings.h> // index
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "platform.h"
|
2012-03-02 11:11:47 -08:00
|
|
|
#include "sps_sampler.h"
|
2011-08-26 17:05:37 -07:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
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
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
static Sampler* sActiveSampler = NULL;
|
|
|
|
|
|
|
|
|
2012-11-08 08:45:17 -08:00
|
|
|
#ifdef ANDROID
|
2012-03-02 11:11:47 -08:00
|
|
|
#include "android-signal-defs.h"
|
2011-08-26 17:05:37 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void ProfilerSaveSignalHandler(int signal, siginfo_t* info, void* context) {
|
|
|
|
sActiveSampler->RequestSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
#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) {
|
|
|
|
if (!sActiveSampler)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (sActiveSampler->IsProfiling()) {
|
|
|
|
// 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.
|
2012-11-08 08:45:17 -08:00
|
|
|
#if !defined(ANDROID) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
|
2011-08-26 17:05:37 -07:00
|
|
|
sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]);
|
|
|
|
sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]);
|
|
|
|
sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]);
|
2012-07-04 08:25:15 -07:00
|
|
|
#ifdef ENABLE_ARM_LR_SAVING
|
|
|
|
sample->lr = reinterpret_cast<Address>(mcontext.gregs[R14]);
|
|
|
|
#endif
|
2011-08-26 17:05:37 -07:00
|
|
|
#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);
|
2012-07-04 08:25:15 -07:00
|
|
|
#ifdef ENABLE_ARM_LR_SAVING
|
|
|
|
sample->lr = reinterpret_cast<Address>(mcontext.arm_lr);
|
|
|
|
#endif
|
2011-08-26 17:05:37 -07:00
|
|
|
#endif
|
|
|
|
#elif V8_HOST_ARCH_MIPS
|
|
|
|
// Implement this on MIPS.
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-10 15:02:00 -08:00
|
|
|
sample->timestamp = mozilla::TimeStamp::Now();
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
sActiveSampler->Tick(sample);
|
|
|
|
}
|
|
|
|
|
2011-12-08 14:35:36 -08:00
|
|
|
#ifndef XP_MACOSX
|
2011-08-26 17:05:37 -07:00
|
|
|
void tgkill(pid_t tgid, pid_t tid, int signalno) {
|
|
|
|
syscall(SYS_tgkill, tgid, tid, signalno);
|
|
|
|
}
|
2011-12-08 14:35:36 -08:00
|
|
|
#endif
|
2011-08-26 17:05:37 -07:00
|
|
|
|
2011-10-28 13:43:02 -07:00
|
|
|
class Sampler::PlatformData : public Malloced {
|
2011-08-26 17:05:37 -07:00
|
|
|
public:
|
|
|
|
explicit PlatformData(Sampler* sampler)
|
|
|
|
: sampler_(sampler),
|
|
|
|
signal_handler_installed_(false),
|
|
|
|
vm_tgid_(getpid()),
|
2011-12-08 14:35:36 -08:00
|
|
|
#ifndef XP_MACOSX
|
2011-08-26 17:05:37 -07:00
|
|
|
vm_tid_(gettid()),
|
2011-12-08 14:35:36 -08:00
|
|
|
#endif
|
|
|
|
signal_sender_launched_(false)
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
, signal_receiver_(pthread_self())
|
|
|
|
#endif
|
|
|
|
{
|
2011-08-26 17:05:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SignalSender() {
|
|
|
|
while (sampler_->IsActive()) {
|
|
|
|
sampler_->HandleSaveRequest();
|
|
|
|
|
2012-05-18 14:03:10 -07:00
|
|
|
if (!sampler_->IsPaused()) {
|
2011-12-08 14:35:36 -08:00
|
|
|
#ifdef XP_MACOSX
|
2012-05-18 14:03:10 -07:00
|
|
|
pthread_kill(signal_receiver_, SIGPROF);
|
2011-12-08 14:35:36 -08:00
|
|
|
#else
|
2012-05-18 14:03:10 -07:00
|
|
|
// Glibc doesn't provide a wrapper for tgkill(2).
|
|
|
|
tgkill(vm_tgid_, vm_tid_, SIGPROF);
|
2011-12-08 14:35:36 -08:00
|
|
|
#endif
|
2012-05-18 14:03:10 -07:00
|
|
|
}
|
|
|
|
|
2011-08-26 17:05:37 -07:00
|
|
|
// Convert ms to us and subtract 100 us to compensate delays
|
|
|
|
// occuring during signal delivery.
|
|
|
|
// TODO measure and confirm this.
|
|
|
|
const useconds_t interval = sampler_->interval_ * 1000 - 100;
|
|
|
|
//int result = usleep(interval);
|
|
|
|
usleep(interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Sampler* sampler_;
|
|
|
|
bool signal_handler_installed_;
|
|
|
|
struct sigaction old_sigprof_signal_handler_;
|
|
|
|
struct sigaction old_sigsave_signal_handler_;
|
|
|
|
pid_t vm_tgid_;
|
|
|
|
pid_t vm_tid_;
|
|
|
|
bool signal_sender_launched_;
|
|
|
|
pthread_t signal_sender_thread_;
|
2011-12-08 14:35:36 -08:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
pthread_t signal_receiver_;
|
|
|
|
#endif
|
2011-08-26 17:05:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void* SenderEntry(void* arg) {
|
|
|
|
Sampler::PlatformData* data =
|
|
|
|
reinterpret_cast<Sampler::PlatformData*>(arg);
|
|
|
|
data->SignalSender();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Sampler::Sampler(int interval, bool profiling)
|
|
|
|
: interval_(interval),
|
|
|
|
profiling_(profiling),
|
2012-06-13 16:24:48 -07:00
|
|
|
paused_(false),
|
|
|
|
active_(false) {
|
2011-08-26 17:05:37 -07:00
|
|
|
data_ = new PlatformData(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sampler::~Sampler() {
|
|
|
|
ASSERT(!data_->signal_sender_launched_);
|
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sampler::Start() {
|
|
|
|
LOG("Sampler Started");
|
|
|
|
if (sActiveSampler != NULL) return;
|
|
|
|
|
|
|
|
// Request profiling signals.
|
|
|
|
LOG("Request signal");
|
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_sigaction = ProfilerSignalHandler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
|
|
|
if (sigaction(SIGPROF, &sa, &data_->old_sigprof_signal_handler_) != 0) {
|
|
|
|
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;
|
|
|
|
if (sigaction(SIGNAL_SAVE_PROFILE, &sa2, &data_->old_sigsave_signal_handler_) != 0) {
|
|
|
|
LOG("Error installing start signal");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG("Signal installed");
|
|
|
|
data_->signal_handler_installed_ = true;
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
&data_->signal_sender_thread_, NULL, SenderEntry, data_) == 0) {
|
|
|
|
data_->signal_sender_launched_ = true;
|
|
|
|
}
|
|
|
|
LOG("Profiler thread started");
|
|
|
|
|
|
|
|
// Set this sampler as the active sampler.
|
|
|
|
sActiveSampler = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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).
|
|
|
|
if (data_->signal_sender_launched_) {
|
|
|
|
pthread_join(data_->signal_sender_thread_, NULL);
|
|
|
|
data_->signal_sender_launched_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore old signal handler
|
|
|
|
if (data_->signal_handler_installed_) {
|
2012-07-11 07:55:22 -07:00
|
|
|
sigaction(SIGNAL_SAVE_PROFILE, &data_->old_sigsave_signal_handler_, 0);
|
2011-08-26 17:05:37 -07:00
|
|
|
sigaction(SIGPROF, &data_->old_sigprof_signal_handler_, 0);
|
|
|
|
data_->signal_handler_installed_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This sampler is no longer the active sampler.
|
|
|
|
sActiveSampler = NULL;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
const int SIGSTART = SIGUSR1;
|
2012-03-02 11:11:47 -08:00
|
|
|
|
2012-07-11 07:55:22 -07:00
|
|
|
static void StartSignalHandler(int signal, siginfo_t* info, void* context) {
|
2012-03-02 11:11:47 -08:00
|
|
|
mozilla_sampler_start(PROFILE_DEFAULT_ENTRY, PROFILE_DEFAULT_INTERVAL,
|
|
|
|
PROFILE_DEFAULT_FEATURES, PROFILE_DEFAULT_FEATURE_COUNT);
|
|
|
|
}
|
|
|
|
|
2012-07-11 07:55:22 -07:00
|
|
|
void OS::RegisterStartHandler()
|
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");
|
|
|
|
}
|
|
|
|
}
|
2012-07-11 08:28:04 -07:00
|
|
|
#endif
|