2011-08-27 00:05:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
#include <string>
|
2011-08-27 00:05:37 +00:00
|
|
|
#include <stdio.h>
|
2012-03-23 19:09:27 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2011-08-27 00:05:37 +00:00
|
|
|
#include "sps_sampler.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prenv.h"
|
2011-12-15 12:31:41 +00:00
|
|
|
#include "shared-libraries.h"
|
2012-02-03 20:19:18 +00:00
|
|
|
#include "mozilla/StackWalk.h"
|
2012-07-11 19:36:04 +00:00
|
|
|
|
|
|
|
// JSON
|
2012-02-02 21:57:20 +00:00
|
|
|
#include "JSObjectBuilder.h"
|
2012-07-11 19:36:04 +00:00
|
|
|
#include "nsIJSRuntimeService.h"
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-06-27 03:43:28 +00:00
|
|
|
// Meta
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsXPCOMCID.h"
|
|
|
|
#include "nsIHttpProtocolHandler.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIXULRuntime.h"
|
|
|
|
#include "nsIXULAppInfo.h"
|
2012-07-10 07:26:57 +00:00
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2012-06-27 03:43:28 +00:00
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
// we eventually want to make this runtime switchable
|
2012-02-03 20:19:18 +00:00
|
|
|
#if defined(MOZ_PROFILING) && (defined(XP_UNIX) && !defined(XP_MACOSX))
|
2012-01-13 01:36:55 +00:00
|
|
|
#ifndef ANDROID
|
|
|
|
#define USE_BACKTRACE
|
|
|
|
#endif
|
2012-01-10 23:02:00 +00:00
|
|
|
#endif
|
2011-12-20 01:33:00 +00:00
|
|
|
#ifdef USE_BACKTRACE
|
2012-01-13 01:36:55 +00:00
|
|
|
#include <execinfo.h>
|
2011-12-20 01:33:00 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-03 20:19:18 +00:00
|
|
|
#if defined(MOZ_PROFILING) && (defined(XP_MACOSX) || defined(XP_WIN))
|
2012-01-17 00:59:15 +00:00
|
|
|
#define USE_NS_STACKWALK
|
|
|
|
#endif
|
|
|
|
#ifdef USE_NS_STACKWALK
|
|
|
|
#include "nsStackWalk.h"
|
|
|
|
#endif
|
|
|
|
|
2012-03-02 19:11:47 +00:00
|
|
|
#if defined(MOZ_PROFILING) && defined(ANDROID)
|
|
|
|
#define USE_LIBUNWIND
|
|
|
|
#include <libunwind.h>
|
|
|
|
#include "android-signal-defs.h"
|
|
|
|
#endif
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
using std::string;
|
2011-12-16 14:03:54 +00:00
|
|
|
using namespace mozilla;
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2011-12-04 19:09:00 +00:00
|
|
|
#ifdef XP_WIN
|
2012-01-29 06:56:41 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#define getpid GetCurrentProcessId
|
2011-12-04 19:09:00 +00:00
|
|
|
#else
|
2012-01-29 06:56:41 +00:00
|
|
|
#include <unistd.h>
|
2011-12-04 19:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAXPATHLEN
|
2012-01-29 06:56:41 +00:00
|
|
|
#ifdef PATH_MAX
|
|
|
|
#define MAXPATHLEN PATH_MAX
|
|
|
|
#elif defined(MAX_PATH)
|
|
|
|
#define MAXPATHLEN MAX_PATH
|
|
|
|
#elif defined(_MAX_PATH)
|
|
|
|
#define MAXPATHLEN _MAX_PATH
|
|
|
|
#elif defined(CCHMAXPATH)
|
|
|
|
#define MAXPATHLEN CCHMAXPATH
|
|
|
|
#else
|
|
|
|
#define MAXPATHLEN 1024
|
|
|
|
#endif
|
2011-12-04 19:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if _MSC_VER
|
2012-01-29 06:56:41 +00:00
|
|
|
#define snprintf _snprintf
|
2011-12-04 19:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
2012-06-27 03:25:14 +00:00
|
|
|
static const int DYNAMIC_MAX_STRING = 512;
|
2011-12-20 01:33:00 +00:00
|
|
|
|
2012-05-22 06:43:55 +00:00
|
|
|
mozilla::ThreadLocal<ProfileStack *> tlsStack;
|
|
|
|
mozilla::ThreadLocal<TableTicker *> tlsTicker;
|
2011-12-08 15:46:02 +00:00
|
|
|
// We need to track whether we've been initialized otherwise
|
2012-05-16 21:20:06 +00:00
|
|
|
// we end up using tlsStack without initializing it.
|
|
|
|
// Because tlsStack is totally opaque to us we can't reuse
|
2011-12-08 15:46:02 +00:00
|
|
|
// it as the flag itself.
|
|
|
|
bool stack_key_initialized;
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
TimeStamp sLastTracerEvent;
|
|
|
|
|
2012-03-12 14:58:33 +00:00
|
|
|
class ThreadProfile;
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
class ProfileEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProfileEntry()
|
|
|
|
: mTagData(NULL)
|
|
|
|
, mTagName(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
// aTagData must not need release (i.e. be a string from the text segment)
|
|
|
|
ProfileEntry(char aTagName, const char *aTagData)
|
|
|
|
: mTagData(aTagData)
|
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2012-05-08 19:38:11 +00:00
|
|
|
ProfileEntry(char aTagName, void *aTagPtr)
|
|
|
|
: mTagPtr(aTagPtr)
|
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2012-01-12 16:50:43 +00:00
|
|
|
ProfileEntry(char aTagName, double aTagFloat)
|
2011-12-07 19:48:15 +00:00
|
|
|
: mTagFloat(aTagFloat)
|
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2012-01-10 23:02:00 +00:00
|
|
|
ProfileEntry(char aTagName, uintptr_t aTagOffset)
|
|
|
|
: mTagOffset(aTagOffset)
|
2012-04-13 18:56:51 +00:00
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
ProfileEntry(char aTagName, Address aTagAddress)
|
|
|
|
: mTagAddress(aTagAddress)
|
2012-01-10 23:02:00 +00:00
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2012-03-23 19:09:27 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& stream, const ProfileEntry& entry);
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-12 14:58:33 +00:00
|
|
|
friend class ThreadProfile;
|
2011-12-07 19:48:15 +00:00
|
|
|
union {
|
|
|
|
const char* mTagData;
|
2012-06-27 03:25:14 +00:00
|
|
|
char mTagChars[sizeof(void*)];
|
2012-05-08 19:38:11 +00:00
|
|
|
void* mTagPtr;
|
2012-01-12 16:50:43 +00:00
|
|
|
double mTagFloat;
|
2011-12-20 01:33:00 +00:00
|
|
|
Address mTagAddress;
|
2012-01-10 23:02:00 +00:00
|
|
|
uintptr_t mTagOffset;
|
2011-12-07 19:48:15 +00:00
|
|
|
};
|
2011-08-27 00:05:37 +00:00
|
|
|
char mTagName;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PROFILE_MAX_ENTRY 100000
|
2012-03-12 14:58:33 +00:00
|
|
|
class ThreadProfile
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-03-12 14:58:37 +00:00
|
|
|
ThreadProfile(int aEntrySize, ProfileStack *aStack)
|
2011-08-27 00:05:37 +00:00
|
|
|
: mWritePos(0)
|
2012-01-18 23:07:46 +00:00
|
|
|
, mLastFlushPos(0)
|
2011-08-27 00:05:37 +00:00
|
|
|
, mReadPos(0)
|
|
|
|
, mEntrySize(aEntrySize)
|
2012-03-12 14:58:37 +00:00
|
|
|
, mStack(aStack)
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
|
|
|
mEntries = new ProfileEntry[mEntrySize];
|
|
|
|
}
|
|
|
|
|
2012-03-12 14:58:33 +00:00
|
|
|
~ThreadProfile()
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
|
|
|
delete[] mEntries;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addTag(ProfileEntry aTag)
|
|
|
|
{
|
|
|
|
// Called from signal, call only reentrant functions
|
|
|
|
mEntries[mWritePos] = aTag;
|
|
|
|
mWritePos = (mWritePos + 1) % mEntrySize;
|
|
|
|
if (mWritePos == mReadPos) {
|
|
|
|
// Keep one slot open
|
|
|
|
mEntries[mReadPos] = ProfileEntry();
|
|
|
|
mReadPos = (mReadPos + 1) % mEntrySize;
|
|
|
|
}
|
2012-01-18 23:07:46 +00:00
|
|
|
// we also need to move the flush pos to ensure we
|
|
|
|
// do not pass it
|
|
|
|
if (mWritePos == mLastFlushPos) {
|
|
|
|
mLastFlushPos = (mLastFlushPos + 1) % mEntrySize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush the new entries
|
|
|
|
void flush()
|
|
|
|
{
|
|
|
|
mLastFlushPos = mWritePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// discards all of the entries since the last flush()
|
|
|
|
// NOTE: that if mWritePos happens to wrap around past
|
|
|
|
// mLastFlushPos we actually only discard mWritePos - mLastFlushPos entries
|
|
|
|
//
|
|
|
|
// r = mReadPos
|
|
|
|
// w = mWritePos
|
|
|
|
// f = mLastFlushPos
|
|
|
|
//
|
|
|
|
// r f w
|
|
|
|
// |-----------------------------|
|
|
|
|
// | abcdefghijklmnopq | -> 'abcdefghijklmnopq'
|
|
|
|
// |-----------------------------|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// mWritePos and mReadPos have passed mLastFlushPos
|
|
|
|
// f
|
|
|
|
// w r
|
|
|
|
// |-----------------------------|
|
|
|
|
// |ABCDEFGHIJKLMNOPQRSqrstuvwxyz|
|
|
|
|
// |-----------------------------|
|
|
|
|
// w
|
|
|
|
// r
|
|
|
|
// |-----------------------------|
|
|
|
|
// |ABCDEFGHIJKLMNOPQRSqrstuvwxyz| -> ''
|
|
|
|
// |-----------------------------|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// mWritePos will end up the same as mReadPos
|
|
|
|
// r
|
|
|
|
// w f
|
|
|
|
// |-----------------------------|
|
|
|
|
// |ABCDEFGHIJKLMklmnopqrstuvwxyz|
|
|
|
|
// |-----------------------------|
|
|
|
|
// r
|
|
|
|
// w
|
|
|
|
// |-----------------------------|
|
|
|
|
// |ABCDEFGHIJKLMklmnopqrstuvwxyz| -> ''
|
|
|
|
// |-----------------------------|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// mWritePos has moved past mReadPos
|
|
|
|
// w r f
|
|
|
|
// |-----------------------------|
|
|
|
|
// |ABCDEFdefghijklmnopqrstuvwxyz|
|
|
|
|
// |-----------------------------|
|
|
|
|
// r w
|
|
|
|
// |-----------------------------|
|
|
|
|
// |ABCDEFdefghijklmnopqrstuvwxyz| -> 'defghijkl'
|
|
|
|
// |-----------------------------|
|
|
|
|
|
|
|
|
void erase()
|
|
|
|
{
|
|
|
|
mWritePos = mLastFlushPos;
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 03:25:14 +00:00
|
|
|
char* processDynamicTag(int readPos, int* tagsConsumed, char* tagBuff)
|
|
|
|
{
|
|
|
|
int readAheadPos = (readPos + 1) % mEntrySize;
|
|
|
|
int tagBuffPos = 0;
|
|
|
|
|
|
|
|
// Read the string stored in mTagData until the null character is seen
|
|
|
|
bool seenNullByte = false;
|
|
|
|
while (readAheadPos != mLastFlushPos && !seenNullByte) {
|
|
|
|
(*tagsConsumed)++;
|
|
|
|
ProfileEntry readAheadEntry = mEntries[readAheadPos];
|
|
|
|
for (size_t pos = 0; pos < sizeof(void*); pos++) {
|
|
|
|
tagBuff[tagBuffPos] = readAheadEntry.mTagChars[pos];
|
|
|
|
if (tagBuff[tagBuffPos] == '\0' || tagBuffPos == DYNAMIC_MAX_STRING-2) {
|
|
|
|
seenNullByte = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tagBuffPos++;
|
|
|
|
}
|
|
|
|
if (!seenNullByte)
|
|
|
|
readAheadPos = (readAheadPos + 1) % mEntrySize;
|
|
|
|
}
|
|
|
|
return tagBuff;
|
|
|
|
}
|
|
|
|
|
2012-03-23 19:09:27 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& stream, const ThreadProfile& profile);
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2012-02-02 21:57:20 +00:00
|
|
|
JSObject *ToJSObject(JSContext *aCx)
|
|
|
|
{
|
|
|
|
JSObjectBuilder b(aCx);
|
|
|
|
|
|
|
|
JSObject *profile = b.CreateObject();
|
|
|
|
JSObject *samples = b.CreateArray();
|
|
|
|
b.DefineProperty(profile, "samples", samples);
|
|
|
|
|
|
|
|
JSObject *sample = NULL;
|
|
|
|
JSObject *frames = NULL;
|
|
|
|
|
2012-06-26 21:57:43 +00:00
|
|
|
int readPos = mReadPos;
|
|
|
|
while (readPos != mLastFlushPos) {
|
2012-06-27 03:25:14 +00:00
|
|
|
// Number of tag consumed
|
|
|
|
int incBy = 1;
|
2012-06-26 21:57:43 +00:00
|
|
|
ProfileEntry entry = mEntries[readPos];
|
2012-06-27 03:25:14 +00:00
|
|
|
|
|
|
|
// Read ahead to the next tag, if it's a 'd' tag process it now
|
|
|
|
const char* tagStringData = entry.mTagData;
|
|
|
|
int readAheadPos = (readPos + 1) % mEntrySize;
|
|
|
|
char tagBuff[DYNAMIC_MAX_STRING];
|
|
|
|
// Make sure the string is always null terminated if it fills up DYNAMIC_MAX_STRING-2
|
|
|
|
tagBuff[DYNAMIC_MAX_STRING-1] = '\0';
|
|
|
|
|
|
|
|
if (readAheadPos != mLastFlushPos && mEntries[readAheadPos].mTagName == 'd') {
|
|
|
|
tagStringData = processDynamicTag(readPos, &incBy, tagBuff);
|
|
|
|
}
|
|
|
|
|
2012-02-02 21:57:20 +00:00
|
|
|
switch (entry.mTagName) {
|
|
|
|
case 's':
|
|
|
|
sample = b.CreateObject();
|
2012-06-27 03:25:14 +00:00
|
|
|
b.DefineProperty(sample, "name", tagStringData);
|
2012-02-02 21:57:20 +00:00
|
|
|
frames = b.CreateArray();
|
|
|
|
b.DefineProperty(sample, "frames", frames);
|
|
|
|
b.ArrayPush(samples, sample);
|
|
|
|
break;
|
2012-06-20 23:22:02 +00:00
|
|
|
case 'r':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
b.DefineProperty(sample, "responsiveness", entry.mTagFloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-07-05 19:49:19 +00:00
|
|
|
case 't':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
b.DefineProperty(sample, "time", entry.mTagFloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-02-02 21:57:20 +00:00
|
|
|
case 'c':
|
|
|
|
case 'l':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
JSObject *frame = b.CreateObject();
|
2012-06-27 03:25:14 +00:00
|
|
|
if (entry.mTagName == 'l') {
|
|
|
|
// Bug 753041
|
|
|
|
// We need a double cast here to tell GCC that we don't want to sign
|
|
|
|
// extend 32-bit addresses starting with 0xFXXXXXX.
|
|
|
|
unsigned long long pc = (unsigned long long)(uintptr_t)entry.mTagPtr;
|
|
|
|
snprintf(tagBuff, DYNAMIC_MAX_STRING, "%#llx", pc);
|
|
|
|
b.DefineProperty(frame, "location", tagBuff);
|
|
|
|
} else {
|
|
|
|
b.DefineProperty(frame, "location", tagStringData);
|
|
|
|
}
|
2012-02-02 21:57:20 +00:00
|
|
|
b.ArrayPush(frames, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-27 03:25:14 +00:00
|
|
|
readPos = (readPos + incBy) % mEntrySize;
|
2012-02-02 21:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2012-03-12 14:58:37 +00:00
|
|
|
ProfileStack* GetStack()
|
|
|
|
{
|
|
|
|
return mStack;
|
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
private:
|
|
|
|
// Circular buffer 'Keep One Slot Open' implementation
|
|
|
|
// for simplicity
|
|
|
|
ProfileEntry *mEntries;
|
|
|
|
int mWritePos; // points to the next entry we will write to
|
2012-01-18 23:07:46 +00:00
|
|
|
int mLastFlushPos; // points to the next entry since the last flush()
|
2011-08-27 00:05:37 +00:00
|
|
|
int mReadPos; // points to the next entry we will read to
|
|
|
|
int mEntrySize;
|
2012-03-12 14:58:37 +00:00
|
|
|
ProfileStack *mStack;
|
2011-08-27 00:05:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SaveProfileTask;
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
static bool
|
|
|
|
hasFeature(const char** aFeatures, uint32_t aFeatureCount, const char* aFeature) {
|
|
|
|
for(size_t i = 0; i < aFeatureCount; i++) {
|
|
|
|
if (strcmp(aFeatures[i], aFeature) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
class TableTicker: public Sampler {
|
|
|
|
public:
|
2012-03-12 14:58:33 +00:00
|
|
|
TableTicker(int aInterval, int aEntrySize, ProfileStack *aStack,
|
2012-01-12 18:33:32 +00:00
|
|
|
const char** aFeatures, uint32_t aFeatureCount)
|
2011-08-27 00:05:37 +00:00
|
|
|
: Sampler(aInterval, true)
|
2012-03-12 14:58:37 +00:00
|
|
|
, mPrimaryThreadProfile(aEntrySize, aStack)
|
2012-07-05 19:49:19 +00:00
|
|
|
, mStartTime(TimeStamp::Now())
|
2011-08-27 00:05:37 +00:00
|
|
|
, mSaveRequested(false)
|
|
|
|
{
|
2012-01-10 23:02:00 +00:00
|
|
|
mUseStackWalk = hasFeature(aFeatures, aFeatureCount, "stackwalk");
|
2012-03-02 19:11:47 +00:00
|
|
|
|
2012-01-18 23:07:46 +00:00
|
|
|
//XXX: It's probably worth splitting the jank profiler out from the regular profiler at some point
|
2011-12-20 20:13:52 +00:00
|
|
|
mJankOnly = hasFeature(aFeatures, aFeatureCount, "jank");
|
2012-06-21 00:58:55 +00:00
|
|
|
mProfileJS = hasFeature(aFeatures, aFeatureCount, "js");
|
2012-03-12 14:58:33 +00:00
|
|
|
mPrimaryThreadProfile.addTag(ProfileEntry('m', "Start"));
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~TableTicker() { if (IsActive()) Stop(); }
|
|
|
|
|
|
|
|
virtual void SampleStack(TickSample* sample) {}
|
|
|
|
|
|
|
|
// Called within a signal. This function must be reentrant
|
|
|
|
virtual void Tick(TickSample* sample);
|
|
|
|
|
|
|
|
// Called within a signal. This function must be reentrant
|
|
|
|
virtual void RequestSave()
|
|
|
|
{
|
|
|
|
mSaveRequested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void HandleSaveRequest();
|
|
|
|
|
2012-03-12 14:58:33 +00:00
|
|
|
ThreadProfile* GetPrimaryThreadProfile()
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
2012-03-12 14:58:33 +00:00
|
|
|
return &mPrimaryThreadProfile;
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
2012-01-17 00:59:15 +00:00
|
|
|
|
2012-03-12 14:58:40 +00:00
|
|
|
JSObject *ToJSObject(JSContext *aCx);
|
2012-06-27 03:43:28 +00:00
|
|
|
JSObject *GetMetaJSObject(JSObjectBuilder& b);
|
2012-03-12 14:58:40 +00:00
|
|
|
|
2012-06-21 00:58:55 +00:00
|
|
|
const bool ProfileJS() { return mProfileJS; }
|
|
|
|
|
2012-01-17 00:59:15 +00:00
|
|
|
private:
|
|
|
|
// Not implemented on platforms which do not support backtracing
|
2012-03-22 16:36:45 +00:00
|
|
|
void doBacktrace(ThreadProfile &aProfile, TickSample* aSample);
|
2012-01-17 00:59:15 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-12 14:58:33 +00:00
|
|
|
// This represent the application's main thread (SAMPLER_INIT)
|
|
|
|
ThreadProfile mPrimaryThreadProfile;
|
2012-07-05 19:49:19 +00:00
|
|
|
TimeStamp mStartTime;
|
2011-08-27 00:05:37 +00:00
|
|
|
bool mSaveRequested;
|
2012-01-10 23:02:00 +00:00
|
|
|
bool mUseStackWalk;
|
2011-12-20 20:13:52 +00:00
|
|
|
bool mJankOnly;
|
2012-06-21 00:58:55 +00:00
|
|
|
bool mProfileJS;
|
2011-08-27 00:05:37 +00:00
|
|
|
};
|
|
|
|
|
2012-04-16 13:32:18 +00:00
|
|
|
std::string GetSharedLibraryInfoString();
|
|
|
|
|
2012-07-11 19:36:04 +00:00
|
|
|
static JSBool
|
|
|
|
WriteCallback(const jschar *buf, uint32_t len, void *data)
|
|
|
|
{
|
|
|
|
std::ofstream& stream = *static_cast<std::ofstream*>(data);
|
|
|
|
nsCAutoString profile = NS_ConvertUTF16toUTF8(buf, len);
|
|
|
|
stream << profile.Data();
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
/**
|
|
|
|
* This is an event used to save the profile on the main thread
|
|
|
|
* to be sure that it is not being modified while saving.
|
|
|
|
*/
|
|
|
|
class SaveProfileTask : public nsRunnable {
|
|
|
|
public:
|
|
|
|
SaveProfileTask() {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2012-05-16 21:20:06 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-05-18 21:03:10 +00:00
|
|
|
// Pause the profiler during saving.
|
|
|
|
// This will prevent us from recording sampling
|
|
|
|
// regarding profile saving. This will also
|
|
|
|
// prevent bugs caused by the circular buffer not
|
|
|
|
// being thread safe. Bug 750989.
|
|
|
|
t->SetPaused(true);
|
2012-07-10 07:26:57 +00:00
|
|
|
|
2012-07-11 19:36:04 +00:00
|
|
|
// Get file path
|
2012-07-24 17:29:59 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
nsCString tmpPath;
|
|
|
|
tmpPath.AppendPrintf("/sdcard/profile_%i_%i.txt", XRE_GetProcessType(), getpid());
|
|
|
|
#else
|
2012-07-10 07:26:57 +00:00
|
|
|
nsCOMPtr<nsIFile> tmpFile;
|
|
|
|
nsCAutoString tmpPath;
|
|
|
|
if (NS_FAILED(NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tmpFile)))) {
|
|
|
|
LOG("Failed to find temporary directory.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
tmpPath.AppendPrintf("profile_%i_%i.txt", XRE_GetProcessType(), getpid());
|
|
|
|
|
|
|
|
nsresult rv = tmpFile->AppendNative(tmpPath);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = tmpFile->GetNativePath(tmpPath);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2012-07-24 17:29:59 +00:00
|
|
|
#endif
|
2012-07-10 07:26:57 +00:00
|
|
|
|
2012-07-11 19:36:04 +00:00
|
|
|
// Create a JSContext to run a JSObjectBuilder :(
|
|
|
|
// Based on XPCShellEnvironment
|
|
|
|
JSRuntime *rt;
|
|
|
|
JSContext *cx;
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> rtsvc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
|
|
|
|
if (!rtsvc || NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) {
|
|
|
|
LOG("failed to get RuntimeService");
|
|
|
|
return NS_ERROR_FAILURE;;
|
|
|
|
}
|
|
|
|
|
|
|
|
cx = JS_NewContext(rt, 8192);
|
|
|
|
if (!cx) {
|
|
|
|
LOG("Failed to get context");
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
2012-07-11 19:36:04 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
static JSClass c = {
|
|
|
|
"global", JSCLASS_GLOBAL_FLAGS,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub
|
|
|
|
};
|
|
|
|
JSObject *obj = JS_NewGlobalObject(cx, &c, NULL);
|
|
|
|
|
|
|
|
std::ofstream stream;
|
|
|
|
stream.open(tmpPath.get());
|
|
|
|
// Pause the profiler during saving.
|
|
|
|
// This will prevent us from recording sampling
|
|
|
|
// regarding profile saving. This will also
|
|
|
|
// prevent bugs caused by the circular buffer not
|
|
|
|
// being thread safe. Bug 750989.
|
|
|
|
t->SetPaused(true);
|
|
|
|
if (stream.is_open()) {
|
|
|
|
JSAutoEnterCompartment autoComp;
|
|
|
|
if (autoComp.enter(cx, obj)) {
|
|
|
|
JSObject* profileObj = mozilla_sampler_get_profile_data(cx);
|
|
|
|
jsval val = OBJECT_TO_JSVAL(profileObj);
|
2012-07-30 14:20:58 +00:00
|
|
|
JS_Stringify(cx, &val, nullptr, JSVAL_NULL, WriteCallback, &stream);
|
2012-07-11 19:36:04 +00:00
|
|
|
} else {
|
|
|
|
LOG("Failed to enter compartment");
|
|
|
|
}
|
|
|
|
stream.close();
|
|
|
|
LOGF("Saved to %s", tmpPath.get());
|
|
|
|
} else {
|
|
|
|
LOG("Fail to open profile log file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_EndRequest(cx);
|
|
|
|
JS_DestroyContext(cx);
|
|
|
|
|
2012-05-18 21:03:10 +00:00
|
|
|
t->SetPaused(false);
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TableTicker::HandleSaveRequest()
|
|
|
|
{
|
|
|
|
if (!mSaveRequested)
|
|
|
|
return;
|
|
|
|
mSaveRequested = false;
|
|
|
|
|
|
|
|
// TODO: Use use the ipc/chromium Tasks here to support processes
|
|
|
|
// without XPCOM.
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new SaveProfileTask();
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
2012-06-27 03:43:28 +00:00
|
|
|
JSObject* TableTicker::GetMetaJSObject(JSObjectBuilder& b)
|
|
|
|
{
|
|
|
|
JSObject *meta = b.CreateObject();
|
|
|
|
|
|
|
|
b.DefineProperty(meta, "version", 2);
|
|
|
|
b.DefineProperty(meta, "interval", interval());
|
|
|
|
b.DefineProperty(meta, "stackwalk", mUseStackWalk);
|
|
|
|
b.DefineProperty(meta, "jank", mJankOnly);
|
|
|
|
b.DefineProperty(meta, "processType", XRE_GetProcessType());
|
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIHttpProtocolHandler> http = do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "http", &res);
|
|
|
|
if (!NS_FAILED(res)) {
|
|
|
|
nsCAutoString string;
|
|
|
|
|
|
|
|
res = http->GetPlatform(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "platform", string.Data());
|
|
|
|
|
|
|
|
res = http->GetOscpu(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "oscpu", string.Data());
|
|
|
|
|
|
|
|
res = http->GetMisc(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "misc", string.Data());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXULRuntime> runtime = do_GetService("@mozilla.org/xre/runtime;1");
|
|
|
|
if (runtime) {
|
|
|
|
nsCAutoString string;
|
|
|
|
|
|
|
|
res = runtime->GetXPCOMABI(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "abi", string.Data());
|
|
|
|
|
|
|
|
res = runtime->GetWidgetToolkit(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "toolkit", string.Data());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXULAppInfo> appInfo = do_GetService("@mozilla.org/xre/app-info;1");
|
|
|
|
if (appInfo) {
|
|
|
|
nsCAutoString string;
|
|
|
|
|
|
|
|
res = appInfo->GetName(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "product", string.Data());
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
2012-03-12 14:58:40 +00:00
|
|
|
JSObject* TableTicker::ToJSObject(JSContext *aCx)
|
|
|
|
{
|
|
|
|
JSObjectBuilder b(aCx);
|
|
|
|
|
|
|
|
JSObject *profile = b.CreateObject();
|
|
|
|
|
2012-07-11 19:36:04 +00:00
|
|
|
// Put shared library info
|
|
|
|
b.DefineProperty(profile, "libs", GetSharedLibraryInfoString().c_str());
|
|
|
|
|
2012-03-12 14:58:40 +00:00
|
|
|
// Put meta data
|
2012-06-27 03:43:28 +00:00
|
|
|
JSObject *meta = GetMetaJSObject(b);
|
|
|
|
b.DefineProperty(profile, "meta", meta);
|
2012-03-12 14:58:40 +00:00
|
|
|
|
|
|
|
// Lists the samples for each ThreadProfile
|
|
|
|
JSObject *threads = b.CreateArray();
|
|
|
|
b.DefineProperty(profile, "threads", threads);
|
|
|
|
|
|
|
|
// For now we only have one thread
|
2012-05-18 21:03:10 +00:00
|
|
|
SetPaused(true);
|
2012-03-12 14:58:40 +00:00
|
|
|
JSObject* threadSamples = GetPrimaryThreadProfile()->ToJSObject(aCx);
|
|
|
|
b.ArrayPush(threads, threadSamples);
|
2012-05-18 21:03:10 +00:00
|
|
|
SetPaused(false);
|
2012-03-12 14:58:40 +00:00
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2012-06-27 23:04:58 +00:00
|
|
|
static
|
|
|
|
void addProfileEntry(ProfileStack *aStack, ThreadProfile &aProfile, int i)
|
|
|
|
{
|
|
|
|
// First entry has tagName 's' (start)
|
|
|
|
// Check for magic pointer bit 1 to indicate copy
|
|
|
|
const char* sampleLabel = aStack->mStack[i].mLabel;
|
|
|
|
if (aStack->mStack[i].isCopyLabel()) {
|
|
|
|
// Store the string using 1 or more 'd' (dynamic) tags
|
|
|
|
// that will happen to the preceding tag
|
|
|
|
|
|
|
|
aProfile.addTag(ProfileEntry('c', ""));
|
|
|
|
// Add one to store the null termination
|
|
|
|
size_t strLen = strlen(sampleLabel) + 1;
|
|
|
|
for (size_t j = 0; j < strLen;) {
|
|
|
|
// Store as many characters in the void* as the platform allows
|
|
|
|
char text[sizeof(void*)];
|
|
|
|
for (size_t pos = 0; pos < sizeof(void*) && j+pos < strLen; pos++) {
|
|
|
|
text[pos] = sampleLabel[j+pos];
|
|
|
|
}
|
|
|
|
j += sizeof(void*)/sizeof(char);
|
|
|
|
// Cast to *((void**) to pass the text data to a void*
|
|
|
|
aProfile.addTag(ProfileEntry('d', *((void**)(&text[0]))));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
aProfile.addTag(ProfileEntry('c', sampleLabel));
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 20:19:18 +00:00
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
#ifdef USE_BACKTRACE
|
2012-03-02 19:11:47 +00:00
|
|
|
void TableTicker::doBacktrace(ThreadProfile &aProfile, TickSample* aSample)
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
2011-12-20 01:33:00 +00:00
|
|
|
void *array[100];
|
|
|
|
int count = backtrace (array, 100);
|
|
|
|
|
2012-04-16 22:32:11 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)"));
|
2012-01-10 23:02:00 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if( (intptr_t)array[i] == -1 ) break;
|
2012-05-08 19:38:11 +00:00
|
|
|
aProfile.addTag(ProfileEntry('l', (void*)array[i]));
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
2011-12-20 01:33:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-02-03 20:19:18 +00:00
|
|
|
|
2012-01-17 00:59:15 +00:00
|
|
|
#ifdef USE_NS_STACKWALK
|
|
|
|
typedef struct {
|
|
|
|
void** array;
|
2012-06-27 23:04:58 +00:00
|
|
|
void** sp_array;
|
2012-01-17 00:59:15 +00:00
|
|
|
size_t size;
|
|
|
|
size_t count;
|
|
|
|
} PCArray;
|
|
|
|
|
|
|
|
static
|
2012-06-27 20:08:21 +00:00
|
|
|
void StackWalkCallback(void* aPC, void* aSP, void* aClosure)
|
2012-01-17 00:59:15 +00:00
|
|
|
{
|
|
|
|
PCArray* array = static_cast<PCArray*>(aClosure);
|
|
|
|
if (array->count >= array->size) {
|
|
|
|
// too many frames, ignore
|
|
|
|
return;
|
|
|
|
}
|
2012-06-27 23:04:58 +00:00
|
|
|
array->sp_array[array->count] = aSP;
|
|
|
|
array->array[array->count] = aPC;
|
|
|
|
array->count++;
|
2012-01-17 00:59:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 19:11:47 +00:00
|
|
|
void TableTicker::doBacktrace(ThreadProfile &aProfile, TickSample* aSample)
|
2012-01-17 00:59:15 +00:00
|
|
|
{
|
2012-02-03 20:19:18 +00:00
|
|
|
#ifndef XP_MACOSX
|
2012-01-17 00:59:15 +00:00
|
|
|
uintptr_t thread = GetThreadHandle(platform_data());
|
|
|
|
MOZ_ASSERT(thread);
|
2012-02-03 20:19:18 +00:00
|
|
|
#endif
|
2012-01-17 00:59:15 +00:00
|
|
|
void* pc_array[1000];
|
2012-06-27 23:04:58 +00:00
|
|
|
void* sp_array[1000];
|
2012-01-17 00:59:15 +00:00
|
|
|
PCArray array = {
|
|
|
|
pc_array,
|
2012-06-27 23:04:58 +00:00
|
|
|
sp_array,
|
2012-01-17 00:59:15 +00:00
|
|
|
mozilla::ArrayLength(pc_array),
|
|
|
|
0
|
|
|
|
};
|
2012-03-22 22:31:56 +00:00
|
|
|
|
|
|
|
// Start with the current function.
|
2012-06-27 20:08:21 +00:00
|
|
|
StackWalkCallback(aSample->pc, aSample->sp, &array);
|
2012-03-22 22:31:56 +00:00
|
|
|
|
2012-02-03 20:19:18 +00:00
|
|
|
#ifdef XP_MACOSX
|
2012-02-15 05:17:34 +00:00
|
|
|
pthread_t pt = GetProfiledThread(platform_data());
|
|
|
|
void *stackEnd = reinterpret_cast<void*>(-1);
|
|
|
|
if (pt)
|
2012-03-12 14:57:36 +00:00
|
|
|
stackEnd = static_cast<char*>(pthread_get_stackaddr_np(pt));
|
2012-03-22 22:30:37 +00:00
|
|
|
nsresult rv = FramePointerStackWalk(StackWalkCallback, 0, &array, reinterpret_cast<void**>(aSample->fp), stackEnd);
|
2012-02-03 20:19:18 +00:00
|
|
|
#else
|
2012-01-17 00:59:15 +00:00
|
|
|
nsresult rv = NS_StackWalk(StackWalkCallback, 0, &array, thread);
|
2012-02-03 20:19:18 +00:00
|
|
|
#endif
|
2012-01-17 00:59:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-04-18 16:55:48 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)"));
|
2012-01-17 00:59:15 +00:00
|
|
|
|
2012-06-27 23:04:58 +00:00
|
|
|
ProfileStack* stack = aProfile.GetStack();
|
|
|
|
int pseudoStackPos = 0;
|
|
|
|
|
|
|
|
/* We have two stacks, the native C stack we extracted from unwinding,
|
|
|
|
* and the pseudostack we managed during execution. We want to consolidate
|
|
|
|
* the two in order. We do so by merging using the approximate stack address
|
|
|
|
* when each entry was push. When pushing JS entry we may not now the stack
|
|
|
|
* address in which case we have a NULL stack address in which case we assume
|
|
|
|
* that it follows immediatly the previous element.
|
|
|
|
*
|
|
|
|
* C Stack | Address -- Pseudo Stack | Address
|
|
|
|
* main() | 0x100 run_js() | 0x40
|
|
|
|
* start() | 0x80 jsCanvas() | NULL
|
|
|
|
* timer() | 0x50 drawLine() | NULL
|
|
|
|
* azure() | 0x10
|
|
|
|
*
|
|
|
|
* Merged: main(), start(), timer(), run_js(), jsCanvas(), drawLine(), azure()
|
|
|
|
*/
|
|
|
|
// i is the index in C stack starting at main and decreasing
|
|
|
|
// pseudoStackPos is the position in the Pseudo stack starting
|
|
|
|
// at the first frame (run_js in the example) and increasing.
|
2012-01-17 00:59:15 +00:00
|
|
|
for (size_t i = array.count; i > 0; --i) {
|
2012-06-27 23:04:58 +00:00
|
|
|
while (pseudoStackPos < stack->mStackPointer) {
|
|
|
|
volatile StackEntry& entry = stack->mStack[pseudoStackPos];
|
|
|
|
|
|
|
|
if (entry.mStackAddress < array.sp_array[i-1] && entry.mStackAddress)
|
|
|
|
break;
|
|
|
|
|
|
|
|
addProfileEntry(stack, aProfile, pseudoStackPos);
|
|
|
|
pseudoStackPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
aProfile.addTag(ProfileEntry('l', (void*)array.array[i-1]));
|
2012-01-17 00:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-02 19:11:47 +00:00
|
|
|
#if defined(USE_LIBUNWIND) && defined(ANDROID)
|
|
|
|
void TableTicker::doBacktrace(ThreadProfile &aProfile, TickSample* aSample)
|
|
|
|
{
|
|
|
|
void* pc_array[1000];
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
unw_cursor_t cursor; unw_context_t uc;
|
|
|
|
unw_word_t ip;
|
|
|
|
unw_getcontext(&uc);
|
|
|
|
|
|
|
|
// Dirty hack: replace the registers with values from the signal handler
|
|
|
|
// We do this in order to avoid the overhead of walking up to reach the
|
|
|
|
// signal handler frame, and the possibility that libunwind fails to
|
|
|
|
// handle it correctly.
|
|
|
|
unw_tdep_context_t *unw_ctx = reinterpret_cast<unw_tdep_context_t*> (&uc);
|
|
|
|
mcontext_t& mcontext = reinterpret_cast<ucontext_t*> (aSample->context)->uc_mcontext;
|
|
|
|
#define REPLACE_REG(num) unw_ctx->regs[num] = mcontext.gregs[R##num]
|
|
|
|
REPLACE_REG(0);
|
|
|
|
REPLACE_REG(1);
|
|
|
|
REPLACE_REG(2);
|
|
|
|
REPLACE_REG(3);
|
|
|
|
REPLACE_REG(4);
|
|
|
|
REPLACE_REG(5);
|
|
|
|
REPLACE_REG(6);
|
|
|
|
REPLACE_REG(7);
|
|
|
|
REPLACE_REG(8);
|
|
|
|
REPLACE_REG(9);
|
|
|
|
REPLACE_REG(10);
|
|
|
|
REPLACE_REG(11);
|
|
|
|
REPLACE_REG(12);
|
|
|
|
REPLACE_REG(13);
|
|
|
|
REPLACE_REG(14);
|
|
|
|
REPLACE_REG(15);
|
|
|
|
#undef REPLACE_REG
|
|
|
|
unw_init_local(&cursor, &uc);
|
|
|
|
while (count < ArrayLength(pc_array) &&
|
|
|
|
unw_step(&cursor) > 0) {
|
|
|
|
unw_get_reg(&cursor, UNW_REG_IP, &ip);
|
|
|
|
pc_array[count++] = reinterpret_cast<void*> (ip);
|
|
|
|
}
|
|
|
|
|
2012-04-13 18:56:51 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)"));
|
2012-03-02 19:11:47 +00:00
|
|
|
for (size_t i = count; i > 0; --i) {
|
2012-05-08 19:38:11 +00:00
|
|
|
aProfile.addTag(ProfileEntry('l', reinterpret_cast<void*>(pc_array[i - 1])));
|
2012-03-02 19:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
static
|
2012-03-12 14:58:33 +00:00
|
|
|
void doSampleStackTrace(ProfileStack *aStack, ThreadProfile &aProfile, TickSample *sample)
|
2011-12-20 01:33:00 +00:00
|
|
|
{
|
2011-08-27 00:05:37 +00:00
|
|
|
// Sample
|
|
|
|
// 's' tag denotes the start of a sample block
|
|
|
|
// followed by 0 or more 'c' tags.
|
2012-06-27 03:25:14 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)"));
|
2012-06-21 00:58:55 +00:00
|
|
|
for (mozilla::sig_safe_t i = 0;
|
|
|
|
i < aStack->mStackPointer && i < mozilla::ArrayLength(aStack->mStack);
|
|
|
|
i++) {
|
2012-06-27 23:04:58 +00:00
|
|
|
addProfileEntry(aStack, aProfile, i);
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
2012-04-13 18:56:51 +00:00
|
|
|
#ifdef ENABLE_SPS_LEAF_DATA
|
|
|
|
if (sample) {
|
2012-05-08 19:38:11 +00:00
|
|
|
aProfile.addTag(ProfileEntry('l', (void*)sample->pc));
|
2012-07-04 15:25:15 +00:00
|
|
|
#ifdef ENABLE_ARM_LR_SAVING
|
|
|
|
aProfile.addTag(ProfileEntry('L', (void*)sample->lr));
|
|
|
|
#endif
|
2012-04-13 18:56:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-12-20 01:33:00 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 23:07:46 +00:00
|
|
|
/* used to keep track of the last event that we sampled during */
|
|
|
|
unsigned int sLastSampledEventGeneration = 0;
|
|
|
|
|
|
|
|
/* a counter that's incremented everytime we get responsiveness event
|
|
|
|
* note: it might also be worth tracking everytime we go around
|
|
|
|
* the event loop */
|
|
|
|
unsigned int sCurrentEventGeneration = 0;
|
|
|
|
/* we don't need to worry about overflow because we only treat the
|
|
|
|
* case of them being the same as special. i.e. we only run into
|
|
|
|
* a problem if 2^32 events happen between samples that we need
|
|
|
|
* to know are associated with different events */
|
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
void TableTicker::Tick(TickSample* sample)
|
|
|
|
{
|
|
|
|
// Marker(s) come before the sample
|
2012-03-12 14:58:37 +00:00
|
|
|
ProfileStack* stack = mPrimaryThreadProfile.GetStack();
|
|
|
|
for (int i = 0; stack->getMarker(i) != NULL; i++) {
|
|
|
|
mPrimaryThreadProfile.addTag(ProfileEntry('m', stack->getMarker(i)));
|
2011-12-20 01:33:00 +00:00
|
|
|
}
|
2012-03-12 14:58:37 +00:00
|
|
|
stack->mQueueClearMarker = true;
|
2011-12-20 01:33:00 +00:00
|
|
|
|
2011-12-20 20:13:52 +00:00
|
|
|
bool recordSample = true;
|
|
|
|
if (mJankOnly) {
|
2012-03-12 14:56:33 +00:00
|
|
|
// if we are on a different event we can discard any temporary samples
|
|
|
|
// we've kept around
|
|
|
|
if (sLastSampledEventGeneration != sCurrentEventGeneration) {
|
|
|
|
// XXX: we also probably want to add an entry to the profile to help
|
|
|
|
// distinguish which samples are part of the same event. That, or record
|
|
|
|
// the event generation in each sample
|
2012-03-12 14:58:33 +00:00
|
|
|
mPrimaryThreadProfile.erase();
|
2012-03-12 14:56:33 +00:00
|
|
|
}
|
|
|
|
sLastSampledEventGeneration = sCurrentEventGeneration;
|
|
|
|
|
2011-12-20 20:13:52 +00:00
|
|
|
recordSample = false;
|
|
|
|
// only record the events when we have a we haven't seen a tracer event for 100ms
|
|
|
|
if (!sLastTracerEvent.IsNull()) {
|
|
|
|
TimeDuration delta = sample->timestamp - sLastTracerEvent;
|
|
|
|
if (delta.ToMilliseconds() > 100.0) {
|
|
|
|
recordSample = true;
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 23:02:00 +00:00
|
|
|
}
|
2011-12-20 20:13:52 +00:00
|
|
|
|
2012-03-02 19:11:47 +00:00
|
|
|
#if defined(USE_BACKTRACE) || defined(USE_NS_STACKWALK) || defined(USE_LIBUNWIND)
|
2012-01-18 23:07:46 +00:00
|
|
|
if (mUseStackWalk) {
|
2012-03-02 19:11:47 +00:00
|
|
|
doBacktrace(mPrimaryThreadProfile, sample);
|
2012-01-18 23:07:46 +00:00
|
|
|
} else {
|
2012-03-12 14:58:37 +00:00
|
|
|
doSampleStackTrace(stack, mPrimaryThreadProfile, sample);
|
2011-12-20 20:13:52 +00:00
|
|
|
}
|
2012-01-18 23:07:46 +00:00
|
|
|
#else
|
2012-03-12 14:58:37 +00:00
|
|
|
doSampleStackTrace(stack, mPrimaryThreadProfile, sample);
|
2012-01-18 23:07:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recordSample)
|
2012-03-12 14:58:33 +00:00
|
|
|
mPrimaryThreadProfile.flush();
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2012-07-05 19:47:20 +00:00
|
|
|
if (!sLastTracerEvent.IsNull() && sample) {
|
2011-12-24 17:11:26 +00:00
|
|
|
TimeDuration delta = sample->timestamp - sLastTracerEvent;
|
2012-03-12 14:58:33 +00:00
|
|
|
mPrimaryThreadProfile.addTag(ProfileEntry('r', delta.ToMilliseconds()));
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
2012-07-05 19:49:19 +00:00
|
|
|
|
|
|
|
if (sample) {
|
|
|
|
TimeDuration delta = sample->timestamp - mStartTime;
|
|
|
|
mPrimaryThreadProfile.addTag(ProfileEntry('t', delta.ToMilliseconds()));
|
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
2012-03-23 19:09:27 +00:00
|
|
|
std::ostream& operator<<(std::ostream& stream, const ThreadProfile& profile)
|
|
|
|
{
|
|
|
|
int readPos = profile.mReadPos;
|
|
|
|
while (readPos != profile.mLastFlushPos) {
|
|
|
|
stream << profile.mEntries[readPos];
|
|
|
|
readPos = (readPos + 1) % profile.mEntrySize;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& stream, const ProfileEntry& entry)
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
2012-07-06 20:40:10 +00:00
|
|
|
if (entry.mTagName == 'r' || entry.mTagName == 't') {
|
2012-03-23 19:09:27 +00:00
|
|
|
stream << entry.mTagName << "-" << std::fixed << entry.mTagFloat << "\n";
|
2012-07-04 15:25:15 +00:00
|
|
|
} else if (entry.mTagName == 'l' || entry.mTagName == 'L') {
|
2012-03-28 23:01:13 +00:00
|
|
|
// Bug 739800 - Force l-tag addresses to have a "0x" prefix on all platforms
|
|
|
|
// Additionally, stringstream seemed to be ignoring formatter flags.
|
|
|
|
char tagBuff[1024];
|
2012-05-08 19:38:11 +00:00
|
|
|
unsigned long long pc = (unsigned long long)(uintptr_t)entry.mTagPtr;
|
2012-07-04 15:25:15 +00:00
|
|
|
snprintf(tagBuff, 1024, "%c-%#llx\n", entry.mTagName, pc);
|
2012-03-28 23:01:13 +00:00
|
|
|
stream << tagBuff;
|
2012-06-27 03:25:14 +00:00
|
|
|
} else if (entry.mTagName == 'd') {
|
|
|
|
// TODO implement 'd' tag for text profile
|
2011-12-07 19:48:15 +00:00
|
|
|
} else {
|
2012-03-23 19:09:27 +00:00
|
|
|
stream << entry.mTagName << "-" << entry.mTagData << "\n";
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
2012-03-23 19:09:27 +00:00
|
|
|
return stream;
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
void mozilla_sampler_init()
|
|
|
|
{
|
2012-05-16 21:20:06 +00:00
|
|
|
if (!tlsStack.init() || !tlsTicker.init()) {
|
2011-08-27 00:05:37 +00:00
|
|
|
LOG("Failed to init.");
|
|
|
|
return;
|
|
|
|
}
|
2011-12-08 15:46:02 +00:00
|
|
|
stack_key_initialized = true;
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-03-12 14:58:33 +00:00
|
|
|
ProfileStack *stack = new ProfileStack();
|
2012-05-16 21:20:06 +00:00
|
|
|
tlsStack.set(stack);
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2012-03-02 19:11:47 +00:00
|
|
|
#if defined(USE_LIBUNWIND) && defined(ANDROID)
|
|
|
|
// Only try debug_frame and exidx unwinding
|
|
|
|
putenv("UNW_ARM_UNWIND_METHOD=5");
|
2012-07-11 14:55:22 +00:00
|
|
|
#endif
|
2012-03-02 19:11:47 +00:00
|
|
|
|
2012-07-11 14:55:22 +00:00
|
|
|
// Allow the profiler to be started using signals
|
|
|
|
OS::RegisterStartHandler();
|
2012-03-02 19:11:47 +00:00
|
|
|
|
2012-07-11 14:55:22 +00:00
|
|
|
#if defined(USE_LIBUNWIND) && defined(__arm__) && defined(MOZ_CRASHREPORTER)
|
|
|
|
// On ARM, libunwind defines a signal handler for segmentation faults.
|
|
|
|
// If SPS is enabled now, the crash reporter will override that signal
|
|
|
|
// handler, and libunwind will likely break.
|
2012-03-02 19:11:47 +00:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
// We can't open pref so we use an environment variable
|
|
|
|
// to know if we should trigger the profiler on startup
|
|
|
|
// NOTE: Default
|
|
|
|
const char *val = PR_GetEnv("MOZ_PROFILER_STARTUP");
|
2011-12-01 22:40:33 +00:00
|
|
|
if (!val || !*val) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-11 02:05:38 +00:00
|
|
|
const char* features = "js";
|
2012-01-12 18:33:32 +00:00
|
|
|
mozilla_sampler_start(PROFILE_DEFAULT_ENTRY, PROFILE_DEFAULT_INTERVAL,
|
2012-07-11 02:05:38 +00:00
|
|
|
&features, 1);
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_deinit()
|
|
|
|
{
|
|
|
|
mozilla_sampler_stop();
|
|
|
|
// We can't delete the Stack because we can be between a
|
|
|
|
// sampler call_enter/call_exit point.
|
|
|
|
// TODO Need to find a safe time to delete Stack
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
void mozilla_sampler_save()
|
|
|
|
{
|
2012-05-16 21:20:06 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
2011-12-07 19:48:15 +00:00
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->RequestSave();
|
|
|
|
// We're on the main thread already so we don't
|
|
|
|
// have to wait to handle the save request.
|
|
|
|
t->HandleSaveRequest();
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
char* mozilla_sampler_get_profile()
|
|
|
|
{
|
2012-05-16 21:20:06 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
2011-12-07 19:48:15 +00:00
|
|
|
if (!t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-23 19:09:27 +00:00
|
|
|
std::stringstream profile;
|
2012-05-18 21:03:10 +00:00
|
|
|
t->SetPaused(true);
|
2012-03-23 19:09:27 +00:00
|
|
|
profile << *(t->GetPrimaryThreadProfile());
|
2012-05-18 21:03:10 +00:00
|
|
|
t->SetPaused(false);
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2012-03-23 19:09:27 +00:00
|
|
|
std::string profileString = profile.str();
|
|
|
|
char *rtn = (char*)malloc( (profileString.length() + 1) * sizeof(char) );
|
|
|
|
strcpy(rtn, profileString.c_str());
|
2011-12-07 19:48:15 +00:00
|
|
|
return rtn;
|
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-02-02 21:57:20 +00:00
|
|
|
JSObject *mozilla_sampler_get_profile_data(JSContext *aCx)
|
|
|
|
{
|
2012-05-16 21:20:06 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
2012-02-02 21:57:20 +00:00
|
|
|
if (!t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-12 14:58:40 +00:00
|
|
|
return t->ToJSObject(aCx);
|
2012-02-02 21:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
const char** mozilla_sampler_get_features()
|
|
|
|
{
|
2012-01-10 23:02:00 +00:00
|
|
|
static const char* features[] = {
|
2012-03-02 19:11:47 +00:00
|
|
|
#if defined(MOZ_PROFILING) && (defined(USE_BACKTRACE) || defined(USE_NS_STACKWALK) || defined(USE_LIBUNWIND))
|
2012-01-10 23:02:00 +00:00
|
|
|
"stackwalk",
|
|
|
|
#endif
|
2012-01-29 06:56:41 +00:00
|
|
|
"jank",
|
2012-06-21 00:58:55 +00:00
|
|
|
"js",
|
2012-01-10 23:02:00 +00:00
|
|
|
NULL
|
|
|
|
};
|
2012-01-12 18:33:32 +00:00
|
|
|
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
// Values are only honored on the first start
|
2012-01-12 18:33:32 +00:00
|
|
|
void mozilla_sampler_start(int aProfileEntries, int aInterval,
|
|
|
|
const char** aFeatures, uint32_t aFeatureCount)
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
2012-06-26 21:57:43 +00:00
|
|
|
if (!stack_key_initialized)
|
|
|
|
mozilla_sampler_init();
|
|
|
|
|
2012-05-16 21:20:06 +00:00
|
|
|
ProfileStack *stack = tlsStack.get();
|
2011-12-07 19:48:15 +00:00
|
|
|
if (!stack) {
|
|
|
|
ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla_sampler_stop();
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
TableTicker *t = new TableTicker(aInterval, aProfileEntries, stack,
|
|
|
|
aFeatures, aFeatureCount);
|
2012-05-16 21:20:06 +00:00
|
|
|
tlsTicker.set(t);
|
2011-08-27 00:05:37 +00:00
|
|
|
t->Start();
|
2012-06-21 00:58:55 +00:00
|
|
|
if (t->ProfileJS())
|
2012-07-09 21:24:23 +00:00
|
|
|
stack->enableJSSampling();
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
void mozilla_sampler_stop()
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
2012-06-26 21:57:43 +00:00
|
|
|
if (!stack_key_initialized)
|
|
|
|
mozilla_sampler_init();
|
|
|
|
|
2012-05-16 21:20:06 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
2011-08-27 00:05:37 +00:00
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-09 21:24:23 +00:00
|
|
|
bool disableJS = t->ProfileJS();
|
2012-06-21 00:58:55 +00:00
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
t->Stop();
|
2012-05-31 23:38:49 +00:00
|
|
|
delete t;
|
2012-05-16 21:20:06 +00:00
|
|
|
tlsTicker.set(NULL);
|
2012-06-21 00:58:55 +00:00
|
|
|
ProfileStack *stack = tlsStack.get();
|
|
|
|
ASSERT(stack != NULL);
|
|
|
|
|
2012-07-09 21:24:23 +00:00
|
|
|
if (disableJS)
|
|
|
|
stack->disableJSSampling();
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mozilla_sampler_is_active()
|
|
|
|
{
|
2012-06-26 21:57:43 +00:00
|
|
|
if (!stack_key_initialized)
|
|
|
|
mozilla_sampler_init();
|
|
|
|
|
2012-05-16 21:20:06 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
2011-12-07 19:48:15 +00:00
|
|
|
if (!t) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t->IsActive();
|
|
|
|
}
|
|
|
|
|
2012-01-12 16:50:43 +00:00
|
|
|
double sResponsivenessTimes[100];
|
|
|
|
double sCurrResponsiveness = 0.f;
|
2011-12-07 19:48:15 +00:00
|
|
|
unsigned int sResponsivenessLoc = 0;
|
|
|
|
void mozilla_sampler_responsiveness(TimeStamp aTime)
|
|
|
|
{
|
|
|
|
if (!sLastTracerEvent.IsNull()) {
|
|
|
|
if (sResponsivenessLoc == 100) {
|
|
|
|
for(size_t i = 0; i < 100-1; i++) {
|
|
|
|
sResponsivenessTimes[i] = sResponsivenessTimes[i+1];
|
|
|
|
}
|
|
|
|
sResponsivenessLoc--;
|
|
|
|
}
|
|
|
|
TimeDuration delta = aTime - sLastTracerEvent;
|
|
|
|
sResponsivenessTimes[sResponsivenessLoc++] = delta.ToMilliseconds();
|
|
|
|
}
|
2012-01-18 23:07:46 +00:00
|
|
|
sCurrentEventGeneration++;
|
2011-12-07 19:48:15 +00:00
|
|
|
|
|
|
|
sLastTracerEvent = aTime;
|
|
|
|
}
|
|
|
|
|
2012-01-12 16:50:43 +00:00
|
|
|
const double* mozilla_sampler_get_responsiveness()
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
|
|
|
return sResponsivenessTimes;
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|