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 <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-11-30 17:49:20 +00:00
|
|
|
#include "JSCustomObjectBuilder.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-09-20 17:36:50 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "mozilla/Services.h"
|
2012-06-27 03:43:28 +00:00
|
|
|
|
2012-08-06 22:58:47 +00:00
|
|
|
// JS
|
|
|
|
#include "jsdbgapi.h"
|
|
|
|
|
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
|
|
|
|
|
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-09-05 15:45:17 +00:00
|
|
|
int sFrameNumber = 0;
|
|
|
|
int sLastFrameNumber = 0;
|
2011-12-07 19:48:15 +00:00
|
|
|
|
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-08-06 22:58:47 +00:00
|
|
|
ProfileEntry(char aTagName, int aTagLine)
|
|
|
|
: mTagLine(aTagLine)
|
|
|
|
, 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
|
|
|
|
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;
|
2012-08-06 22:58:47 +00:00
|
|
|
int mTagLine;
|
2011-12-07 19:48:15 +00:00
|
|
|
};
|
2011-08-27 00:05:37 +00:00
|
|
|
char mTagName;
|
|
|
|
};
|
|
|
|
|
2012-11-19 23:13:28 +00:00
|
|
|
typedef void (*IterateTagsCallback)(const ProfileEntry& entry, const char* tagStringData);
|
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
#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-11-19 23:13:28 +00:00
|
|
|
void IterateTags(IterateTagsCallback aCallback)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
|
|
|
|
int readPos = mReadPos;
|
|
|
|
while (readPos != mLastFlushPos) {
|
|
|
|
// Number of tag consumed
|
|
|
|
int incBy = 1;
|
|
|
|
const ProfileEntry& entry = mEntries[readPos];
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback(entry, tagStringData);
|
|
|
|
|
|
|
|
readPos = (readPos + incBy) % mEntrySize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
void ToStreamAsJSON(std::ostream& stream)
|
|
|
|
{
|
|
|
|
JSCustomObjectBuilder b;
|
|
|
|
JSCustomObject *profile = b.CreateObject();
|
|
|
|
BuildJSObject(b, profile);
|
|
|
|
b.Serialize(profile, stream);
|
|
|
|
b.DeleteObject(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSCustomObject *ToJSObject(JSContext *aCx)
|
2012-02-02 21:57:20 +00:00
|
|
|
{
|
|
|
|
JSObjectBuilder b(aCx);
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject *profile = b.CreateObject();
|
|
|
|
BuildJSObject(b, profile);
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
2012-02-02 21:57:20 +00:00
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
void BuildJSObject(JSAObjectBuilder& b, JSCustomObject* profile) {
|
|
|
|
JSCustomArray *samples = b.CreateArray();
|
2012-02-02 21:57:20 +00:00
|
|
|
b.DefineProperty(profile, "samples", samples);
|
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject *sample = nullptr;
|
|
|
|
JSCustomArray *frames = nullptr;
|
|
|
|
JSCustomArray *marker = nullptr;
|
2012-02-02 21:57:20 +00:00
|
|
|
|
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);
|
2012-10-15 14:33:20 +00:00
|
|
|
// Created lazily
|
|
|
|
marker = NULL;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
if (!marker) {
|
|
|
|
marker = b.CreateArray();
|
|
|
|
b.DefineProperty(sample, "marker", marker);
|
|
|
|
}
|
|
|
|
b.ArrayPush(marker, tagStringData);
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 21:57:20 +00:00
|
|
|
break;
|
2012-06-20 23:22:02 +00:00
|
|
|
case 'r':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
b.DefineProperty(sample, "responsiveness", entry.mTagFloat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-09-05 15:45:17 +00:00
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
b.DefineProperty(sample, "frameNumber", entry.mTagLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject *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-08-06 22:58:47 +00:00
|
|
|
readAheadPos = (readPos + incBy) % mEntrySize;
|
|
|
|
if (readAheadPos != mLastFlushPos &&
|
|
|
|
mEntries[readAheadPos].mTagName == 'n') {
|
|
|
|
b.DefineProperty(frame, "line",
|
|
|
|
mEntries[readAheadPos].mTagLine);
|
|
|
|
incBy++;
|
|
|
|
}
|
2012-06-27 03:25:14 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-12 22:15:17 +00:00
|
|
|
mAddLeafAddresses = hasFeature(aFeatures, aFeatureCount, "leaf");
|
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-11-30 17:49:20 +00:00
|
|
|
void ToStreamAsJSON(std::ostream& stream);
|
2012-03-12 14:58:40 +00:00
|
|
|
JSObject *ToJSObject(JSContext *aCx);
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject *GetMetaJSCustomObject(JSAObjectBuilder& 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
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
void BuildJSObject(JSAObjectBuilder& b, JSCustomObject* profile);
|
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-12-12 22:15:17 +00:00
|
|
|
bool mAddLeafAddresses;
|
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);
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString profile = NS_ConvertUTF16toUTF8(buf, len);
|
2012-07-11 19:36:04 +00:00
|
|
|
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();
|
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-09-24 22:38:07 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2012-07-24 17:29:59 +00:00
|
|
|
nsCString tmpPath;
|
|
|
|
tmpPath.AppendPrintf("/sdcard/profile_%i_%i.txt", XRE_GetProcessType(), getpid());
|
|
|
|
#else
|
2012-07-10 07:26:57 +00:00
|
|
|
nsCOMPtr<nsIFile> tmpFile;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString tmpPath;
|
2012-07-10 07:26:57 +00:00
|
|
|
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-11-30 17:49:20 +00:00
|
|
|
// Create a JSContext to run a JSCustomObjectBuilder :(
|
2012-07-11 19:36:04 +00:00
|
|
|
// 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.
|
|
|
|
if (stream.is_open()) {
|
2012-08-22 01:42:53 +00:00
|
|
|
JSAutoCompartment autoComp(cx, obj);
|
|
|
|
JSObject* profileObj = mozilla_sampler_get_profile_data(cx);
|
|
|
|
jsval val = OBJECT_TO_JSVAL(profileObj);
|
|
|
|
JS_Stringify(cx, &val, nullptr, JSVAL_NULL, WriteCallback, &stream);
|
2012-07-11 19:36:04 +00:00
|
|
|
stream.close();
|
|
|
|
LOGF("Saved to %s", tmpPath.get());
|
|
|
|
} else {
|
|
|
|
LOG("Fail to open profile log file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JS_EndRequest(cx);
|
|
|
|
JS_DestroyContext(cx);
|
|
|
|
|
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-11-30 17:49:20 +00:00
|
|
|
JSCustomObject* TableTicker::GetMetaJSCustomObject(JSAObjectBuilder& b)
|
2012-06-27 03:43:28 +00:00
|
|
|
{
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject *meta = b.CreateObject();
|
2012-06-27 03:43:28 +00:00
|
|
|
|
|
|
|
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)) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString string;
|
2012-06-27 03:43:28 +00:00
|
|
|
|
|
|
|
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) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString string;
|
2012-06-27 03:43:28 +00:00
|
|
|
|
|
|
|
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) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString string;
|
2012-06-27 03:43:28 +00:00
|
|
|
|
|
|
|
res = appInfo->GetName(string);
|
|
|
|
if (!NS_FAILED(res))
|
|
|
|
b.DefineProperty(meta, "product", string.Data());
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
void TableTicker::ToStreamAsJSON(std::ostream& stream)
|
|
|
|
{
|
|
|
|
JSCustomObjectBuilder b;
|
|
|
|
JSCustomObject* profile = b.CreateObject();
|
|
|
|
BuildJSObject(b, profile);
|
|
|
|
b.Serialize(profile, stream);
|
|
|
|
b.DeleteObject(profile);
|
|
|
|
}
|
|
|
|
|
2012-03-12 14:58:40 +00:00
|
|
|
JSObject* TableTicker::ToJSObject(JSContext *aCx)
|
|
|
|
{
|
|
|
|
JSObjectBuilder b(aCx);
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject* profile = b.CreateObject();
|
|
|
|
BuildJSObject(b, profile);
|
|
|
|
JSObject* jsProfile = b.GetJSObject(profile);
|
2012-03-12 14:58:40 +00:00
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
return jsProfile;
|
|
|
|
}
|
2012-03-12 14:58:40 +00:00
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
void TableTicker::BuildJSObject(JSAObjectBuilder& b, JSCustomObject* profile)
|
|
|
|
{
|
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-11-30 17:49:20 +00:00
|
|
|
JSCustomObject *meta = GetMetaJSCustomObject(b);
|
2012-06-27 03:43:28 +00:00
|
|
|
b.DefineProperty(profile, "meta", meta);
|
2012-03-12 14:58:40 +00:00
|
|
|
|
|
|
|
// Lists the samples for each ThreadProfile
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomArray *threads = b.CreateArray();
|
2012-03-12 14:58:40 +00:00
|
|
|
b.DefineProperty(profile, "threads", threads);
|
|
|
|
|
|
|
|
// For now we only have one thread
|
2012-05-18 21:03:10 +00:00
|
|
|
SetPaused(true);
|
2012-11-30 17:49:20 +00:00
|
|
|
JSCustomObject* threadSamples = b.CreateObject();
|
|
|
|
GetPrimaryThreadProfile()->BuildJSObject(b, threadSamples);
|
2012-03-12 14:58:40 +00:00
|
|
|
b.ArrayPush(threads, threadSamples);
|
2012-05-18 21:03:10 +00:00
|
|
|
SetPaused(false);
|
2012-03-12 14:58:40 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 19:10:56 +00:00
|
|
|
static
|
|
|
|
void addDynamicTag(ThreadProfile &aProfile, char aTagName, const char *aStr)
|
|
|
|
{
|
|
|
|
aProfile.addTag(ProfileEntry(aTagName, ""));
|
|
|
|
// Add one to store the null termination
|
|
|
|
size_t strLen = strlen(aStr) + 1;
|
|
|
|
for (size_t j = 0; j < strLen;) {
|
|
|
|
// Store as many characters in the void* as the platform allows
|
|
|
|
char text[sizeof(void*)];
|
|
|
|
int len = sizeof(void*)/sizeof(char);
|
|
|
|
if (j+len >= strLen) {
|
|
|
|
len = strLen - j;
|
|
|
|
}
|
|
|
|
memcpy(text, &aStr[j], len);
|
|
|
|
j += sizeof(void*)/sizeof(char);
|
|
|
|
// Cast to *((void**) to pass the text data to a void*
|
|
|
|
aProfile.addTag(ProfileEntry('d', *((void**)(&text[0]))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 23:04:58 +00:00
|
|
|
static
|
2012-08-06 22:58:47 +00:00
|
|
|
void addProfileEntry(volatile StackEntry &entry, ThreadProfile &aProfile,
|
|
|
|
ProfileStack *stack, void *lastpc)
|
2012-06-27 23:04:58 +00:00
|
|
|
{
|
2012-08-06 22:58:47 +00:00
|
|
|
int lineno = -1;
|
|
|
|
|
2012-06-27 23:04:58 +00:00
|
|
|
// First entry has tagName 's' (start)
|
|
|
|
// Check for magic pointer bit 1 to indicate copy
|
2012-08-06 18:35:56 +00:00
|
|
|
const char* sampleLabel = entry.label();
|
|
|
|
if (entry.isCopyLabel()) {
|
2012-06-27 23:04:58 +00:00
|
|
|
// Store the string using 1 or more 'd' (dynamic) tags
|
|
|
|
// that will happen to the preceding tag
|
|
|
|
|
2012-12-11 19:10:56 +00:00
|
|
|
addDynamicTag(aProfile, 'c', sampleLabel);
|
2012-08-06 22:58:47 +00:00
|
|
|
if (entry.js()) {
|
|
|
|
if (!entry.pc()) {
|
|
|
|
// The JIT only allows the top-most entry to have a NULL pc
|
|
|
|
MOZ_ASSERT(&entry == &stack->mStack[stack->stackSize() - 1]);
|
|
|
|
// If stack-walking was disabled, then that's just unfortunate
|
|
|
|
if (lastpc) {
|
|
|
|
jsbytecode *jspc = js::ProfilingGetPC(stack->mRuntime, entry.script(),
|
|
|
|
lastpc);
|
|
|
|
if (jspc) {
|
|
|
|
lineno = JS_PCToLineNumber(NULL, entry.script(), jspc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lineno = JS_PCToLineNumber(NULL, entry.script(), entry.pc());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lineno = entry.line();
|
|
|
|
}
|
2012-06-27 23:04:58 +00:00
|
|
|
} else {
|
|
|
|
aProfile.addTag(ProfileEntry('c', sampleLabel));
|
2012-08-06 22:58:47 +00:00
|
|
|
lineno = entry.line();
|
|
|
|
}
|
|
|
|
if (lineno != -1) {
|
|
|
|
aProfile.addTag(ProfileEntry('n', lineno));
|
2012-06-27 23:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
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);
|
2012-12-21 05:31:57 +00:00
|
|
|
MOZ_ASSERT(array->count < array->size);
|
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-12-05 23:10:15 +00:00
|
|
|
void TableTicker::doBacktrace(ThreadProfile &aProfile, TickSample* aSample)
|
2012-01-17 00:59:15 +00:00
|
|
|
{
|
2012-12-05 23:10:15 +00:00
|
|
|
#ifndef XP_MACOSX
|
|
|
|
uintptr_t thread = GetThreadHandle(platform_data());
|
|
|
|
MOZ_ASSERT(thread);
|
|
|
|
#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-12-21 05:31:57 +00:00
|
|
|
uint32_t maxFrames = array.size - array.count;
|
2012-02-03 20:19:18 +00:00
|
|
|
#ifdef XP_MACOSX
|
2012-12-05 23:10:15 +00:00
|
|
|
pthread_t pt = GetProfiledThread(platform_data());
|
2012-02-15 05:17:34 +00:00
|
|
|
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-11-21 21:24:39 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aSample->fp >= aSample->sp && aSample->fp <= stackEnd)
|
2012-12-21 05:31:57 +00:00
|
|
|
rv = FramePointerStackWalk(StackWalkCallback, /* skipFrames */ 0,
|
|
|
|
maxFrames, &array,
|
|
|
|
reinterpret_cast<void**>(aSample->fp), stackEnd);
|
2012-02-03 20:19:18 +00:00
|
|
|
#else
|
2013-01-30 01:59:12 +00:00
|
|
|
void *platformData = nullptr;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
platformData = aSample->context;
|
|
|
|
#endif // XP_WIN
|
|
|
|
|
2012-12-21 05:31:57 +00:00
|
|
|
nsresult rv = NS_StackWalk(StackWalkCallback, /* skipFrames */ 0, maxFrames,
|
|
|
|
&array, thread, platformData);
|
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-08-06 18:35:56 +00:00
|
|
|
while (pseudoStackPos < stack->stackSize()) {
|
2012-06-27 23:04:58 +00:00
|
|
|
volatile StackEntry& entry = stack->mStack[pseudoStackPos];
|
|
|
|
|
2012-08-06 18:35:56 +00:00
|
|
|
if (entry.stackAddress() < array.sp_array[i-1] && entry.stackAddress())
|
2012-06-27 23:04:58 +00:00
|
|
|
break;
|
|
|
|
|
2012-08-06 22:58:47 +00:00
|
|
|
addProfileEntry(entry, aProfile, stack, array.array[0]);
|
2012-06-27 23:04:58 +00:00
|
|
|
pseudoStackPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
aProfile.addTag(ProfileEntry('l', (void*)array.array[i-1]));
|
2012-01-17 00:59:15 +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-08-06 18:35:56 +00:00
|
|
|
for (uint32_t i = 0; i < aStack->stackSize(); i++) {
|
2012-08-06 22:58:47 +00:00
|
|
|
addProfileEntry(aStack->mStack[i], aProfile, aStack, nullptr);
|
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++) {
|
2012-12-11 19:10:56 +00:00
|
|
|
addDynamicTag(mPrimaryThreadProfile, '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-11-22 19:04:56 +00:00
|
|
|
#if defined(USE_BACKTRACE) || defined(USE_NS_STACKWALK)
|
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-12-12 22:15:17 +00:00
|
|
|
doSampleStackTrace(stack, mPrimaryThreadProfile, mAddLeafAddresses ? sample : nullptr);
|
2011-12-20 20:13:52 +00:00
|
|
|
}
|
2012-01-18 23:07:46 +00:00
|
|
|
#else
|
2012-12-12 22:15:17 +00:00
|
|
|
doSampleStackTrace(stack, mPrimaryThreadProfile, mAddLeafAddresses ? sample : nullptr);
|
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()));
|
|
|
|
}
|
2012-09-05 15:45:17 +00:00
|
|
|
|
|
|
|
if (sLastFrameNumber != sFrameNumber) {
|
|
|
|
mPrimaryThreadProfile.addTag(ProfileEntry('f', sFrameNumber));
|
|
|
|
sLastFrameNumber = sFrameNumber;
|
|
|
|
}
|
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-10-06 08:37:45 +00:00
|
|
|
if (stack_key_initialized)
|
|
|
|
return;
|
|
|
|
|
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-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
|
|
|
|
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-12-12 23:01:31 +00:00
|
|
|
const char* features[] = {"js", "leaf"
|
2012-10-14 16:08:11 +00:00
|
|
|
#if defined(XP_WIN) || defined(XP_MACOSX)
|
|
|
|
, "stackwalk"
|
|
|
|
#endif
|
|
|
|
};
|
2012-01-12 18:33:32 +00:00
|
|
|
mozilla_sampler_start(PROFILE_DEFAULT_ENTRY, PROFILE_DEFAULT_INTERVAL,
|
2012-10-14 16:08:11 +00:00
|
|
|
features, sizeof(features)/sizeof(const char*));
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 17:49:20 +00:00
|
|
|
void mozilla_sampler_shutdown()
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
2012-11-30 17:49:20 +00:00
|
|
|
TableTicker *t = tlsTicker.get();
|
|
|
|
if (t) {
|
|
|
|
const char *val = PR_GetEnv("MOZ_PROFILER_SHUTDOWN");
|
|
|
|
if (val) {
|
|
|
|
std::ofstream stream;
|
|
|
|
stream.open(val);
|
|
|
|
if (stream.is_open()) {
|
|
|
|
t->ToStreamAsJSON(stream);
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
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-11-22 19:04:56 +00:00
|
|
|
#if defined(MOZ_PROFILING) && (defined(USE_BACKTRACE) || defined(USE_NS_STACKWALK))
|
2012-01-10 23:02:00 +00:00
|
|
|
"stackwalk",
|
2012-12-12 22:15:17 +00:00
|
|
|
#endif
|
|
|
|
#if defined(ENABLE_SPS_LEAF_DATA)
|
|
|
|
"leaf",
|
2012-01-10 23:02:00 +00:00
|
|
|
#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-09-11 15:33:53 +00:00
|
|
|
TableTicker *t = new TableTicker(aInterval ? aInterval : PROFILE_DEFAULT_INTERVAL,
|
|
|
|
aProfileEntries ? aProfileEntries : PROFILE_DEFAULT_ENTRY,
|
|
|
|
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();
|
2012-09-20 17:36:50 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os)
|
|
|
|
os->NotifyObservers(nullptr, "profiler-started", nullptr);
|
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();
|
2012-09-20 17:36:50 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os)
|
|
|
|
os->NotifyObservers(nullptr, "profiler-stopped", nullptr);
|
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
|
|
|
}
|
|
|
|
|
2012-09-05 15:45:17 +00:00
|
|
|
void mozilla_sampler_frame_number(int frameNumber)
|
|
|
|
{
|
|
|
|
sFrameNumber = frameNumber;
|
|
|
|
}
|
2012-11-19 23:13:28 +00:00
|
|
|
|
|
|
|
void print_callback(const ProfileEntry& entry, const char* tagStringData) {
|
|
|
|
switch (entry.mTagName) {
|
|
|
|
case 's':
|
|
|
|
case 'c':
|
|
|
|
printf_stderr(" %s\n", tagStringData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_print_location()
|
|
|
|
{
|
|
|
|
if (!stack_key_initialized)
|
|
|
|
mozilla_sampler_init();
|
|
|
|
|
|
|
|
ProfileStack *stack = tlsStack.get();
|
|
|
|
if (!stack) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-11 21:13:15 +00:00
|
|
|
ThreadProfile threadProfile(1000, stack);
|
2012-11-19 23:13:28 +00:00
|
|
|
doSampleStackTrace(stack, threadProfile, NULL);
|
|
|
|
|
|
|
|
threadProfile.flush();
|
|
|
|
|
|
|
|
printf_stderr("Backtrace:\n");
|
|
|
|
threadProfile.IterateTags(print_callback);
|
|
|
|
}
|
2012-12-17 23:25:50 +00:00
|
|
|
|
|
|
|
void mozilla_sampler_lock()
|
|
|
|
{
|
|
|
|
mozilla_sampler_stop();
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os)
|
|
|
|
os->NotifyObservers(nullptr, "profiler-locked", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_unlock()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os)
|
|
|
|
os->NotifyObservers(nullptr, "profiler-unlocked", nullptr);
|
|
|
|
}
|