2011-12-04 11:09:00 -08: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-12-04 11:09:00 -08:00
|
|
|
|
|
|
|
#include <windows.h>
|
2012-07-26 09:02:48 -07:00
|
|
|
#include <mmsystem.h>
|
2013-03-29 12:34:49 -07:00
|
|
|
#include "platform.h"
|
2013-03-29 15:18:38 -07:00
|
|
|
#include <process.h>
|
2013-03-28 20:52:20 -07:00
|
|
|
|
2011-12-04 11:09:00 -08:00
|
|
|
|
|
|
|
class Sampler::PlatformData : public Malloced {
|
|
|
|
public:
|
|
|
|
// Get a handle to the calling thread. This is the thread that we are
|
|
|
|
// going to profile. We need to make a copy of the handle because we are
|
|
|
|
// going to use it in the sampler thread. Using GetThreadHandle() will
|
|
|
|
// not work in this case. We're using OpenThread because DuplicateHandle
|
|
|
|
// for some reason doesn't work in Chrome's sandbox.
|
|
|
|
PlatformData() : profiled_thread_(OpenThread(THREAD_GET_CONTEXT |
|
|
|
|
THREAD_SUSPEND_RESUME |
|
|
|
|
THREAD_QUERY_INFORMATION,
|
|
|
|
false,
|
|
|
|
GetCurrentThreadId())) {}
|
|
|
|
|
|
|
|
~PlatformData() {
|
|
|
|
if (profiled_thread_ != NULL) {
|
|
|
|
CloseHandle(profiled_thread_);
|
|
|
|
profiled_thread_ = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE profiled_thread() { return profiled_thread_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HANDLE profiled_thread_;
|
|
|
|
};
|
|
|
|
|
2012-01-16 16:59:15 -08:00
|
|
|
uintptr_t
|
|
|
|
Sampler::GetThreadHandle(Sampler::PlatformData* aData)
|
|
|
|
{
|
|
|
|
return (uintptr_t) aData->profiled_thread();
|
|
|
|
}
|
2011-12-04 11:09:00 -08:00
|
|
|
|
|
|
|
class SamplerThread : public Thread {
|
|
|
|
public:
|
|
|
|
SamplerThread(int interval, Sampler* sampler)
|
2013-03-29 15:18:38 -07:00
|
|
|
: Thread("SamplerThread"),
|
|
|
|
interval_(interval),
|
|
|
|
sampler_(sampler) {}
|
2011-12-04 11:09:00 -08:00
|
|
|
|
|
|
|
static void StartSampler(Sampler* sampler) {
|
|
|
|
if (instance_ == NULL) {
|
|
|
|
instance_ = new SamplerThread(sampler->interval(), sampler);
|
|
|
|
instance_->Start();
|
|
|
|
} else {
|
|
|
|
ASSERT(instance_->interval_ == sampler->interval());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StopSampler() {
|
|
|
|
instance_->Join();
|
|
|
|
delete instance_;
|
|
|
|
instance_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement Thread::Run().
|
|
|
|
virtual void Run() {
|
2012-07-26 09:02:48 -07:00
|
|
|
|
|
|
|
// By default we'll not adjust the timer resolution which tends to be around
|
|
|
|
// 16ms. However, if the requested interval is sufficiently low we'll try to
|
|
|
|
// adjust the resolution to match.
|
|
|
|
if (interval_ < 10)
|
|
|
|
::timeBeginPeriod(interval_);
|
|
|
|
|
2011-12-04 11:09:00 -08:00
|
|
|
while (sampler_->IsActive()) {
|
2012-05-18 14:03:10 -07:00
|
|
|
if (!sampler_->IsPaused())
|
|
|
|
SampleContext(sampler_);
|
2011-12-04 11:09:00 -08:00
|
|
|
OS::Sleep(interval_);
|
|
|
|
}
|
2012-07-26 09:02:48 -07:00
|
|
|
|
|
|
|
// disable any timer resolution changes we've made
|
|
|
|
if (interval_ < 10)
|
|
|
|
::timeEndPeriod(interval_);
|
2011-12-04 11:09:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleContext(Sampler* sampler) {
|
|
|
|
HANDLE profiled_thread = sampler->platform_data()->profiled_thread();
|
|
|
|
if (profiled_thread == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Context used for sampling the register state of the profiled thread.
|
|
|
|
CONTEXT context;
|
|
|
|
memset(&context, 0, sizeof(context));
|
|
|
|
|
|
|
|
TickSample sample_obj;
|
|
|
|
TickSample* sample = &sample_obj;
|
|
|
|
|
2011-12-24 09:11:26 -08:00
|
|
|
// Grab the timestamp before pausing the thread, to avoid deadlocks.
|
|
|
|
sample->timestamp = mozilla::TimeStamp::Now();
|
|
|
|
|
2011-12-04 11:09:00 -08:00
|
|
|
static const DWORD kSuspendFailed = static_cast<DWORD>(-1);
|
|
|
|
if (SuspendThread(profiled_thread) == kSuspendFailed)
|
|
|
|
return;
|
|
|
|
|
2012-12-07 21:15:21 -08:00
|
|
|
context.ContextFlags = CONTEXT_CONTROL;
|
2011-12-04 11:09:00 -08:00
|
|
|
if (GetThreadContext(profiled_thread, &context) != 0) {
|
|
|
|
#if V8_HOST_ARCH_X64
|
|
|
|
sample->pc = reinterpret_cast<Address>(context.Rip);
|
|
|
|
sample->sp = reinterpret_cast<Address>(context.Rsp);
|
|
|
|
sample->fp = reinterpret_cast<Address>(context.Rbp);
|
|
|
|
#else
|
|
|
|
sample->pc = reinterpret_cast<Address>(context.Eip);
|
|
|
|
sample->sp = reinterpret_cast<Address>(context.Esp);
|
|
|
|
sample->fp = reinterpret_cast<Address>(context.Ebp);
|
|
|
|
#endif
|
2012-12-07 21:15:21 -08:00
|
|
|
sample->context = &context;
|
2011-12-04 11:09:00 -08:00
|
|
|
sampler->SampleStack(sample);
|
|
|
|
sampler->Tick(sample);
|
|
|
|
}
|
|
|
|
ResumeThread(profiled_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sampler* sampler_;
|
|
|
|
const int interval_;
|
|
|
|
|
|
|
|
// Protects the process wide state below.
|
|
|
|
static SamplerThread* instance_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SamplerThread);
|
|
|
|
};
|
|
|
|
|
|
|
|
SamplerThread* SamplerThread::instance_ = NULL;
|
|
|
|
|
|
|
|
|
2013-03-29 15:18:38 -07:00
|
|
|
Sampler::Sampler(int interval, bool profiling)
|
2011-12-04 11:09:00 -08:00
|
|
|
: interval_(interval),
|
|
|
|
profiling_(profiling),
|
2012-05-18 14:03:10 -07:00
|
|
|
paused_(false),
|
2012-06-13 16:24:48 -07:00
|
|
|
active_(false),
|
2011-12-04 11:09:00 -08:00
|
|
|
data_(new PlatformData) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Sampler::~Sampler() {
|
|
|
|
ASSERT(!IsActive());
|
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sampler::Start() {
|
|
|
|
ASSERT(!IsActive());
|
|
|
|
SetActive(true);
|
|
|
|
SamplerThread::StartSampler(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sampler::Stop() {
|
|
|
|
ASSERT(IsActive());
|
|
|
|
SetActive(false);
|
|
|
|
SamplerThread::StopSampler();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const HANDLE kNoThread = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
static unsigned int __stdcall ThreadEntry(void* arg) {
|
|
|
|
Thread* thread = reinterpret_cast<Thread*>(arg);
|
|
|
|
thread->Run();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Thread::PlatformData : public Malloced {
|
|
|
|
public:
|
|
|
|
explicit PlatformData(HANDLE thread) : thread_(thread) {}
|
|
|
|
HANDLE thread_;
|
|
|
|
unsigned thread_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize a Win32 thread object. The thread has an invalid thread
|
|
|
|
// handle until it is started.
|
|
|
|
Thread::Thread(const char* name)
|
|
|
|
: stack_size_(0) {
|
|
|
|
data_ = new PlatformData(kNoThread);
|
|
|
|
set_name(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Thread::set_name(const char* name) {
|
|
|
|
strncpy(name_, name, sizeof(name_));
|
|
|
|
name_[sizeof(name_) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close our own handle for the thread.
|
|
|
|
Thread::~Thread() {
|
|
|
|
if (data_->thread_ != kNoThread) CloseHandle(data_->thread_);
|
|
|
|
delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new thread. It is important to use _beginthreadex() instead of
|
|
|
|
// the Win32 function CreateThread(), because the CreateThread() does not
|
|
|
|
// initialize thread specific structures in the C runtime library.
|
|
|
|
void Thread::Start() {
|
|
|
|
data_->thread_ = reinterpret_cast<HANDLE>(
|
|
|
|
_beginthreadex(NULL,
|
|
|
|
static_cast<unsigned>(stack_size_),
|
|
|
|
ThreadEntry,
|
|
|
|
this,
|
|
|
|
0,
|
|
|
|
&data_->thread_id_));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for thread to terminate.
|
|
|
|
void Thread::Join() {
|
|
|
|
if (data_->thread_id_ != GetCurrentThreadId()) {
|
|
|
|
WaitForSingleObject(data_->thread_, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OS::Sleep(int milliseconds) {
|
|
|
|
::Sleep(milliseconds);
|
|
|
|
}
|