2011-12-04 23:09:00 +04:00
|
|
|
// Copyright (c) 2006-2011 The Chromium Authors. All rights reserved.
|
2012-11-19 23:02:32 +04: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 23:09:00 +04:00
|
|
|
|
|
|
|
#include <windows.h>
|
2012-07-26 20:02:48 +04:00
|
|
|
#include <mmsystem.h>
|
2013-04-18 16:15:09 +04:00
|
|
|
#include <process.h>
|
2013-03-29 23:34:49 +04:00
|
|
|
#include "platform.h"
|
2013-04-04 02:59:17 +04:00
|
|
|
#include "TableTicker.h"
|
2014-06-07 01:53:42 +04:00
|
|
|
#include "ThreadResponsiveness.h"
|
2013-03-29 23:34:49 +04:00
|
|
|
#include "ProfileEntry.h"
|
2013-05-28 16:03:38 +04:00
|
|
|
#include "UnwinderThread2.h"
|
2013-04-18 16:15:09 +04:00
|
|
|
|
2014-05-19 22:31:31 +04:00
|
|
|
// Memory profile
|
|
|
|
#include "nsMemoryReporterManager.h"
|
|
|
|
|
2013-04-04 02:59:17 +04:00
|
|
|
class PlatformData : public Malloced {
|
2011-12-04 23:09:00 +04:00
|
|
|
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.
|
2013-04-04 02:59:17 +04:00
|
|
|
PlatformData(int aThreadId) : profiled_thread_(OpenThread(THREAD_GET_CONTEXT |
|
2011-12-04 23:09:00 +04:00
|
|
|
THREAD_SUSPEND_RESUME |
|
|
|
|
THREAD_QUERY_INFORMATION,
|
|
|
|
false,
|
2013-04-04 02:59:17 +04:00
|
|
|
aThreadId)) {}
|
2011-12-04 23:09:00 +04:00
|
|
|
|
|
|
|
~PlatformData() {
|
|
|
|
if (profiled_thread_ != NULL) {
|
|
|
|
CloseHandle(profiled_thread_);
|
|
|
|
profiled_thread_ = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE profiled_thread() { return profiled_thread_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HANDLE profiled_thread_;
|
|
|
|
};
|
|
|
|
|
2013-04-04 02:59:17 +04:00
|
|
|
/* static */ PlatformData*
|
|
|
|
Sampler::AllocPlatformData(int aThreadId)
|
|
|
|
{
|
|
|
|
return new PlatformData(aThreadId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
Sampler::FreePlatformData(PlatformData* aData)
|
|
|
|
{
|
|
|
|
delete aData;
|
|
|
|
}
|
|
|
|
|
2012-01-17 04:59:15 +04:00
|
|
|
uintptr_t
|
2013-04-04 02:59:17 +04:00
|
|
|
Sampler::GetThreadHandle(PlatformData* aData)
|
2012-01-17 04:59:15 +04:00
|
|
|
{
|
|
|
|
return (uintptr_t) aData->profiled_thread();
|
|
|
|
}
|
2011-12-04 23:09:00 +04:00
|
|
|
|
|
|
|
class SamplerThread : public Thread {
|
|
|
|
public:
|
2013-03-26 20:34:27 +04:00
|
|
|
SamplerThread(double interval, Sampler* sampler)
|
2013-03-29 23:34:49 +04:00
|
|
|
: Thread("SamplerThread")
|
|
|
|
, interval_(interval)
|
2013-03-26 20:34:27 +04:00
|
|
|
, sampler_(sampler)
|
|
|
|
{
|
|
|
|
interval_ = floor(interval + 0.5);
|
|
|
|
if (interval_ <= 0) {
|
|
|
|
interval_ = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-04 23:09:00 +04: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 20:02:48 +04: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 23:09:00 +04:00
|
|
|
while (sampler_->IsActive()) {
|
2014-03-01 00:16:38 +04:00
|
|
|
if (!sampler_->IsPaused()) {
|
2013-04-04 02:59:17 +04:00
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
|
|
|
std::vector<ThreadInfo*> threads =
|
|
|
|
sampler_->GetRegisteredThreads();
|
2014-05-19 22:31:31 +04:00
|
|
|
bool isFirstProfiledThread = true;
|
2013-04-04 02:59:17 +04:00
|
|
|
for (uint32_t i = 0; i < threads.size(); i++) {
|
|
|
|
ThreadInfo* info = threads[i];
|
|
|
|
|
|
|
|
// This will be null if we're not interested in profiling this thread.
|
2014-10-06 22:12:52 +04:00
|
|
|
if (!info->Profile() || info->IsPendingDelete())
|
2013-04-04 02:59:17 +04:00
|
|
|
continue;
|
|
|
|
|
2014-03-29 00:08:22 +04: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;
|
|
|
|
}
|
|
|
|
|
2014-06-07 01:53:42 +04:00
|
|
|
info->Profile()->GetThreadResponsiveness()->Update();
|
|
|
|
|
2013-04-04 02:59:17 +04:00
|
|
|
ThreadProfile* thread_profile = info->Profile();
|
|
|
|
|
2014-05-19 22:31:31 +04:00
|
|
|
SampleContext(sampler_, thread_profile, isFirstProfiledThread);
|
|
|
|
isFirstProfiledThread = false;
|
2013-04-04 02:59:17 +04:00
|
|
|
}
|
|
|
|
}
|
2011-12-04 23:09:00 +04:00
|
|
|
OS::Sleep(interval_);
|
|
|
|
}
|
2012-07-26 20:02:48 +04:00
|
|
|
|
|
|
|
// disable any timer resolution changes we've made
|
|
|
|
if (interval_ < 10)
|
|
|
|
::timeEndPeriod(interval_);
|
2011-12-04 23:09:00 +04:00
|
|
|
}
|
|
|
|
|
2014-05-19 22:31:31 +04:00
|
|
|
void SampleContext(Sampler* sampler, ThreadProfile* thread_profile,
|
|
|
|
bool isFirstProfiledThread)
|
|
|
|
{
|
2013-04-04 02:59:17 +04:00
|
|
|
uintptr_t thread = Sampler::GetThreadHandle(
|
|
|
|
thread_profile->GetPlatformData());
|
|
|
|
HANDLE profiled_thread = reinterpret_cast<HANDLE>(thread);
|
2011-12-04 23:09:00 +04:00
|
|
|
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 21:11:26 +04:00
|
|
|
// Grab the timestamp before pausing the thread, to avoid deadlocks.
|
|
|
|
sample->timestamp = mozilla::TimeStamp::Now();
|
2013-04-04 02:59:17 +04:00
|
|
|
sample->threadProfile = thread_profile;
|
2011-12-24 21:11:26 +04:00
|
|
|
|
2014-05-19 22:31:31 +04:00
|
|
|
if (isFirstProfiledThread && Sampler::GetActiveSampler()->ProfileMemory()) {
|
|
|
|
sample->rssMemory = nsMemoryReporterManager::ResidentFast();
|
|
|
|
} else {
|
|
|
|
sample->rssMemory = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:31:09 +04:00
|
|
|
// Unique Set Size is not supported on Windows.
|
|
|
|
sample->ussMemory = 0;
|
|
|
|
|
2011-12-04 23:09:00 +04:00
|
|
|
static const DWORD kSuspendFailed = static_cast<DWORD>(-1);
|
|
|
|
if (SuspendThread(profiled_thread) == kSuspendFailed)
|
|
|
|
return;
|
|
|
|
|
2015-01-13 04:58:33 +03:00
|
|
|
// CONTEXT_CONTROL is faster but we can't use it on 64-bit because it
|
|
|
|
// causes crashes in RtlVirtualUnwind (see bug 1120126).
|
|
|
|
#if V8_HOST_ARCH_X64
|
|
|
|
context.ContextFlags = CONTEXT_FULL;
|
|
|
|
#else
|
2012-12-08 09:15:21 +04:00
|
|
|
context.ContextFlags = CONTEXT_CONTROL;
|
2015-01-13 04:58:33 +03:00
|
|
|
#endif
|
2011-12-04 23:09:00 +04: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-08 09:15:21 +04:00
|
|
|
sample->context = &context;
|
2011-12-04 23:09:00 +04:00
|
|
|
sampler->Tick(sample);
|
|
|
|
}
|
|
|
|
ResumeThread(profiled_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sampler* sampler_;
|
2013-03-26 20:34:27 +04:00
|
|
|
int interval_; // units: ms
|
2011-12-04 23:09:00 +04:00
|
|
|
|
|
|
|
// Protects the process wide state below.
|
|
|
|
static SamplerThread* instance_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SamplerThread);
|
|
|
|
};
|
|
|
|
|
|
|
|
SamplerThread* SamplerThread::instance_ = NULL;
|
|
|
|
|
|
|
|
|
2013-03-26 20:34:27 +04:00
|
|
|
Sampler::Sampler(double interval, bool profiling, int entrySize)
|
2011-12-04 23:09:00 +04:00
|
|
|
: interval_(interval),
|
|
|
|
profiling_(profiling),
|
2012-05-19 01:03:10 +04:00
|
|
|
paused_(false),
|
2012-06-14 03:24:48 +04:00
|
|
|
active_(false),
|
2013-04-04 02:59:17 +04:00
|
|
|
entrySize_(entrySize) {
|
2011-12-04 23:09:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Sampler::~Sampler() {
|
|
|
|
ASSERT(!IsActive());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a Win32 thread object. The thread has an invalid thread
|
|
|
|
// handle until it is started.
|
|
|
|
Thread::Thread(const char* name)
|
|
|
|
: stack_size_(0) {
|
2013-04-04 02:59:17 +04:00
|
|
|
thread_ = kNoThread;
|
2011-12-04 23:09:00 +04:00
|
|
|
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() {
|
2013-04-04 02:59:17 +04:00
|
|
|
if (thread_ != kNoThread) CloseHandle(thread_);
|
2011-12-04 23:09:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2013-04-04 02:59:17 +04:00
|
|
|
thread_ = reinterpret_cast<HANDLE>(
|
2011-12-04 23:09:00 +04:00
|
|
|
_beginthreadex(NULL,
|
|
|
|
static_cast<unsigned>(stack_size_),
|
|
|
|
ThreadEntry,
|
|
|
|
this,
|
|
|
|
0,
|
2013-09-25 19:28:34 +04:00
|
|
|
(unsigned int*) &thread_id_));
|
2011-12-04 23:09:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for thread to terminate.
|
|
|
|
void Thread::Join() {
|
2013-09-25 19:28:34 +04:00
|
|
|
if (thread_id_ != GetCurrentId()) {
|
2013-04-04 02:59:17 +04:00
|
|
|
WaitForSingleObject(thread_, INFINITE);
|
2011-12-04 23:09:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 19:28:34 +04:00
|
|
|
/* static */ Thread::tid_t
|
|
|
|
Thread::GetCurrentId()
|
|
|
|
{
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
}
|
|
|
|
|
2014-03-13 00:32:40 +04:00
|
|
|
void OS::Startup() {
|
|
|
|
}
|
|
|
|
|
2011-12-04 23:09:00 +04:00
|
|
|
void OS::Sleep(int milliseconds) {
|
|
|
|
::Sleep(milliseconds);
|
|
|
|
}
|
2013-03-29 23:34:49 +04:00
|
|
|
|
2013-05-28 16:03:38 +04:00
|
|
|
bool Sampler::RegisterCurrentThread(const char* aName,
|
|
|
|
PseudoStack* aPseudoStack,
|
|
|
|
bool aIsMainThread, void* stackTop)
|
2013-03-29 23:34:49 +04:00
|
|
|
{
|
2013-04-18 19:34:49 +04:00
|
|
|
if (!Sampler::sRegisteredThreadsMutex)
|
|
|
|
return false;
|
|
|
|
|
2013-09-25 19:28:34 +04:00
|
|
|
|
2013-04-04 02:59:17 +04:00
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
2013-03-29 23:34:49 +04:00
|
|
|
|
2013-09-24 23:23:36 +04:00
|
|
|
int id = GetCurrentThreadId();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
|
|
|
|
ThreadInfo* info = sRegisteredThreads->at(i);
|
2014-10-06 22:12:52 +04:00
|
|
|
if (info->ThreadId() == id && !info->IsPendingDelete()) {
|
2013-09-24 23:23:36 +04:00
|
|
|
// Thread already registered. This means the first unregister will be
|
|
|
|
// too early.
|
|
|
|
ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_tls_stack_top(stackTop);
|
|
|
|
|
2014-12-11 17:41:00 +03:00
|
|
|
ThreadInfo* info = new StackOwningThreadInfo(aName, id,
|
2013-09-11 22:50:46 +04:00
|
|
|
aIsMainThread, aPseudoStack, stackTop);
|
2013-03-29 23:34:49 +04:00
|
|
|
|
2013-05-17 00:31:50 +04:00
|
|
|
if (sActiveSampler) {
|
|
|
|
sActiveSampler->RegisterThread(info);
|
2013-03-29 23:34:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sRegisteredThreads->push_back(info);
|
2013-05-28 16:03:38 +04:00
|
|
|
|
|
|
|
uwt__register_thread_for_profiling(stackTop);
|
2013-03-29 23:34:49 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sampler::UnregisterCurrentThread()
|
|
|
|
{
|
2013-04-18 19:34:49 +04:00
|
|
|
if (!Sampler::sRegisteredThreadsMutex)
|
|
|
|
return;
|
|
|
|
|
2013-09-25 19:28:34 +04:00
|
|
|
tlsStackTop.set(nullptr);
|
|
|
|
|
2013-04-04 02:59:17 +04:00
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
|
|
|
|
|
|
|
int id = GetCurrentThreadId();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
|
|
|
|
ThreadInfo* info = sRegisteredThreads->at(i);
|
2014-10-06 22:12:52 +04:00
|
|
|
if (info->ThreadId() == id && !info->IsPendingDelete()) {
|
|
|
|
if (profiler_is_active()) {
|
|
|
|
// We still want to show the results of this thread if you
|
|
|
|
// save the profile shortly after a thread is terminated.
|
|
|
|
// For now we will defer the delete to profile stop.
|
|
|
|
info->SetPendingDelete();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
delete info;
|
|
|
|
sRegisteredThreads->erase(sRegisteredThreads->begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
2013-04-04 02:59:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 19:28:34 +04:00
|
|
|
|
|
|
|
void TickSample::PopulateContext(void* aContext)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContext);
|
|
|
|
CONTEXT* pContext = reinterpret_cast<CONTEXT*>(aContext);
|
|
|
|
context = pContext;
|
|
|
|
RtlCaptureContext(pContext);
|
|
|
|
|
|
|
|
#if defined(SPS_PLAT_amd64_windows)
|
|
|
|
|
|
|
|
pc = reinterpret_cast<Address>(pContext->Rip);
|
|
|
|
sp = reinterpret_cast<Address>(pContext->Rsp);
|
|
|
|
fp = reinterpret_cast<Address>(pContext->Rbp);
|
|
|
|
|
|
|
|
#elif defined(SPS_PLAT_x86_windows)
|
|
|
|
|
|
|
|
pc = reinterpret_cast<Address>(pContext->Eip);
|
|
|
|
sp = reinterpret_cast<Address>(pContext->Esp);
|
|
|
|
fp = reinterpret_cast<Address>(pContext->Ebp);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|