2011-08-27 00:05:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Benoit Girard <bgirard@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
#include <string>
|
2011-08-27 00:05:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#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"
|
2011-12-16 14:03:54 +00:00
|
|
|
#include "mozilla/StringBuilder.h"
|
2012-02-03 20:19:18 +00:00
|
|
|
#include "mozilla/StackWalk.h"
|
2012-02-02 21:57:20 +00:00
|
|
|
#include "JSObjectBuilder.h"
|
2011-08-27 00:05:37 +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
|
|
|
|
|
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
|
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
|
2011-12-04 19:09:00 +00:00
|
|
|
mozilla::tls::key pkey_stack;
|
|
|
|
mozilla::tls::key pkey_ticker;
|
2011-12-08 15:46:02 +00:00
|
|
|
// We need to track whether we've been initialized otherwise
|
|
|
|
// we end up using pkey_stack without initializing it.
|
|
|
|
// Because pkey_stack is totally opaque to us we can't reuse
|
|
|
|
// 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-11 16:03:45 +00:00
|
|
|
class Profile;
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
class ProfileEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProfileEntry()
|
|
|
|
: mTagData(NULL)
|
|
|
|
, mLeafAddress(0)
|
|
|
|
, mTagName(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
// aTagData must not need release (i.e. be a string from the text segment)
|
|
|
|
ProfileEntry(char aTagName, const char *aTagData)
|
|
|
|
: mTagData(aTagData)
|
|
|
|
, mLeafAddress(0)
|
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
// aTagData must not need release (i.e. be a string from the text segment)
|
2011-08-27 00:05:37 +00:00
|
|
|
ProfileEntry(char aTagName, const char *aTagData, Address aLeafAddress)
|
|
|
|
: mTagData(aTagData)
|
|
|
|
, mLeafAddress(aLeafAddress)
|
|
|
|
, 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)
|
|
|
|
, mLeafAddress(0)
|
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2012-01-10 23:02:00 +00:00
|
|
|
ProfileEntry(char aTagName, uintptr_t aTagOffset)
|
|
|
|
: mTagOffset(aTagOffset)
|
|
|
|
, mLeafAddress(0)
|
|
|
|
, mTagName(aTagName)
|
|
|
|
{ }
|
|
|
|
|
2012-03-11 16:03:45 +00:00
|
|
|
string TagToString(Profile *profile);
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-11 16:03:45 +00:00
|
|
|
friend class Profile;
|
2011-12-07 19:48:15 +00:00
|
|
|
union {
|
|
|
|
const char* mTagData;
|
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
|
|
|
Address mLeafAddress;
|
|
|
|
char mTagName;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PROFILE_MAX_ENTRY 100000
|
2012-03-11 16:03:45 +00:00
|
|
|
class Profile
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-03-11 16:03:45 +00:00
|
|
|
Profile(int aEntrySize)
|
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)
|
|
|
|
{
|
|
|
|
mEntries = new ProfileEntry[mEntrySize];
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:03:45 +00:00
|
|
|
~Profile()
|
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
|
|
|
}
|
|
|
|
|
2011-12-16 14:03:54 +00:00
|
|
|
void ToString(StringBuilder &profile)
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
2012-02-11 23:56:18 +00:00
|
|
|
int readPos = mReadPos;
|
|
|
|
while (readPos != mLastFlushPos) {
|
|
|
|
profile.Append(mEntries[readPos].TagToString(this).c_str());
|
|
|
|
readPos = (readPos + 1) % mEntrySize;
|
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;
|
|
|
|
|
|
|
|
int oldReadPos = mReadPos;
|
|
|
|
while (mReadPos != mLastFlushPos) {
|
|
|
|
ProfileEntry entry = mEntries[mReadPos];
|
|
|
|
mReadPos = (mReadPos + 1) % mEntrySize;
|
|
|
|
switch (entry.mTagName) {
|
|
|
|
case 's':
|
|
|
|
sample = b.CreateObject();
|
|
|
|
b.DefineProperty(sample, "name", entry.mTagData);
|
|
|
|
frames = b.CreateArray();
|
|
|
|
b.DefineProperty(sample, "frames", frames);
|
|
|
|
b.ArrayPush(samples, sample);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
case 'l':
|
|
|
|
{
|
|
|
|
if (sample) {
|
|
|
|
JSObject *frame = b.CreateObject();
|
|
|
|
b.DefineProperty(frame, "location", entry.mTagData);
|
|
|
|
b.ArrayPush(frames, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mReadPos = oldReadPos;
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
void WriteProfile(FILE* stream)
|
|
|
|
{
|
2012-02-11 23:56:18 +00:00
|
|
|
int readPos = mReadPos;
|
|
|
|
while (readPos != mLastFlushPos) {
|
|
|
|
string tag = mEntries[readPos].TagToString(this);
|
2011-12-16 14:12:46 +00:00
|
|
|
fwrite(tag.data(), 1, tag.length(), stream);
|
2012-02-11 23:56:18 +00:00
|
|
|
readPos = (readPos + 1) % mEntrySize;
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-11 16:03:45 +00:00
|
|
|
TableTicker(int aInterval, int aEntrySize, Stack *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-11 16:03:45 +00:00
|
|
|
, mProfile(aEntrySize)
|
2012-03-11 16:03:05 +00:00
|
|
|
, mStack(aStack)
|
2011-08-27 00:05:37 +00:00
|
|
|
, mSaveRequested(false)
|
|
|
|
{
|
2012-01-10 23:02:00 +00:00
|
|
|
mUseStackWalk = hasFeature(aFeatures, aFeatureCount, "stackwalk");
|
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-03-11 16:03:45 +00:00
|
|
|
mProfile.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-11 16:03:45 +00:00
|
|
|
Stack* GetStack()
|
2012-03-11 16:03:05 +00:00
|
|
|
{
|
|
|
|
return mStack;
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:03:45 +00:00
|
|
|
Profile* GetProfile()
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
2012-03-11 16:03:45 +00:00
|
|
|
return &mProfile;
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
2012-01-17 00:59:15 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Not implemented on platforms which do not support backtracing
|
2012-03-11 16:03:45 +00:00
|
|
|
void doBacktrace(Profile &aProfile, Address pc);
|
2012-01-17 00:59:15 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-11 16:03:45 +00:00
|
|
|
Profile mProfile;
|
|
|
|
Stack *mStack;
|
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;
|
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() {
|
2011-12-04 19:09:00 +00:00
|
|
|
TableTicker *t = mozilla::tls::get<TableTicker>(pkey_ticker);
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2011-12-04 19:09:00 +00:00
|
|
|
char buff[MAXPATHLEN];
|
2011-08-27 00:05:37 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#define FOLDER "/sdcard/"
|
2011-12-04 19:09:00 +00:00
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#define FOLDER "%TEMP%\\"
|
2011-08-27 00:05:37 +00:00
|
|
|
#else
|
|
|
|
#define FOLDER "/tmp/"
|
|
|
|
#endif
|
2011-12-04 19:09:00 +00:00
|
|
|
|
|
|
|
snprintf(buff, MAXPATHLEN, "%sprofile_%i_%i.txt", FOLDER, XRE_GetProcessType(), getpid());
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
// Expand %TEMP% on Windows
|
|
|
|
{
|
|
|
|
char tmp[MAXPATHLEN];
|
|
|
|
ExpandEnvironmentStringsA(buff, tmp, mozilla::ArrayLength(tmp));
|
|
|
|
strcpy(buff, tmp);
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
FILE* stream = ::fopen(buff, "w");
|
|
|
|
if (stream) {
|
2012-03-11 16:03:45 +00:00
|
|
|
t->GetProfile()->WriteProfile(stream);
|
2011-08-27 00:05:37 +00:00
|
|
|
::fclose(stream);
|
2011-12-07 19:48:15 +00:00
|
|
|
LOG("Saved to " FOLDER "profile_TYPE_PID.txt");
|
2011-08-27 00:05:37 +00:00
|
|
|
} else {
|
|
|
|
LOG("Fail to open profile log file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-03 20:19:18 +00:00
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
#ifdef USE_BACKTRACE
|
2012-03-11 16:03:45 +00:00
|
|
|
void TableTicker::doBacktrace(Profile &aProfile, Address pc)
|
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-01-31 15:10:05 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)", 0));
|
2012-01-10 23:02:00 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if( (intptr_t)array[i] == -1 ) break;
|
2011-12-20 01:33:00 +00:00
|
|
|
aProfile.addTag(ProfileEntry('l', (const char*)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;
|
|
|
|
size_t size;
|
|
|
|
size_t count;
|
|
|
|
} PCArray;
|
|
|
|
|
|
|
|
static
|
|
|
|
void StackWalkCallback(void* aPC, void* aClosure)
|
|
|
|
{
|
|
|
|
PCArray* array = static_cast<PCArray*>(aClosure);
|
|
|
|
if (array->count >= array->size) {
|
|
|
|
// too many frames, ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
array->array[array->count++] = aPC;
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:03:45 +00:00
|
|
|
void TableTicker::doBacktrace(Profile &aProfile, Address fp)
|
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];
|
|
|
|
PCArray array = {
|
|
|
|
pc_array,
|
|
|
|
mozilla::ArrayLength(pc_array),
|
|
|
|
0
|
|
|
|
};
|
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)
|
|
|
|
stackEnd = static_cast<char*>(pthread_get_stackaddr_np(pt)) - pthread_get_stacksize_np(pt);
|
|
|
|
nsresult rv = FramePointerStackWalk(StackWalkCallback, 1, &array, reinterpret_cast<void**>(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-01-31 15:10:05 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)", 0));
|
2012-01-17 00:59:15 +00:00
|
|
|
|
|
|
|
for (size_t i = array.count; i > 0; --i) {
|
|
|
|
aProfile.addTag(ProfileEntry('l', (const char*)array.array[i - 1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
static
|
2012-03-11 16:03:45 +00:00
|
|
|
void doSampleStackTrace(Stack *aStack, Profile &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.
|
2011-12-20 01:33:00 +00:00
|
|
|
for (int i = 0; i < aStack->mStackPointer; i++) {
|
2011-08-27 00:05:37 +00:00
|
|
|
if (i == 0) {
|
|
|
|
Address pc = 0;
|
|
|
|
if (sample) {
|
|
|
|
pc = sample->pc;
|
|
|
|
}
|
2011-12-20 01:33:00 +00:00
|
|
|
aProfile.addTag(ProfileEntry('s', aStack->mStack[i], pc));
|
2011-08-27 00:05:37 +00:00
|
|
|
} else {
|
2011-12-20 01:33:00 +00:00
|
|
|
aProfile.addTag(ProfileEntry('c', aStack->mStack[i]));
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
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-11 16:03:05 +00:00
|
|
|
for (int i = 0; mStack->getMarker(i) != NULL; i++) {
|
2012-03-11 16:03:45 +00:00
|
|
|
mProfile.addTag(ProfileEntry('m', mStack->getMarker(i)));
|
2011-12-20 01:33:00 +00:00
|
|
|
}
|
2012-03-11 16:03:05 +00:00
|
|
|
mStack->mQueueClearMarker = true;
|
2011-12-20 01:33:00 +00:00
|
|
|
|
2012-01-18 23:07:46 +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-11 16:03:45 +00:00
|
|
|
mProfile.erase();
|
2012-01-18 23:07:46 +00:00
|
|
|
}
|
|
|
|
sLastSampledEventGeneration = sCurrentEventGeneration;
|
|
|
|
|
2011-12-20 20:13:52 +00:00
|
|
|
bool recordSample = true;
|
|
|
|
if (mJankOnly) {
|
|
|
|
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-01-17 00:59:15 +00:00
|
|
|
#if defined(USE_BACKTRACE) || defined(USE_NS_STACKWALK)
|
2012-01-18 23:07:46 +00:00
|
|
|
if (mUseStackWalk) {
|
2012-03-11 16:03:45 +00:00
|
|
|
doBacktrace(mProfile, sample->fp);
|
2012-01-18 23:07:46 +00:00
|
|
|
} else {
|
2012-03-11 16:03:45 +00:00
|
|
|
doSampleStackTrace(mStack, mProfile, sample);
|
2011-12-20 20:13:52 +00:00
|
|
|
}
|
2012-01-18 23:07:46 +00:00
|
|
|
#else
|
2012-03-11 16:03:45 +00:00
|
|
|
doSampleStackTrace(mStack, mProfile, sample);
|
2012-01-18 23:07:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recordSample)
|
2012-03-11 16:03:45 +00:00
|
|
|
mProfile.flush();
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2011-12-20 20:13:52 +00:00
|
|
|
if (!mJankOnly && !sLastTracerEvent.IsNull() && sample) {
|
2011-12-24 17:11:26 +00:00
|
|
|
TimeDuration delta = sample->timestamp - sLastTracerEvent;
|
2012-03-11 16:03:45 +00:00
|
|
|
mProfile.addTag(ProfileEntry('r', delta.ToMilliseconds()));
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 16:03:45 +00:00
|
|
|
string ProfileEntry::TagToString(Profile *profile)
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
|
|
|
string tag = "";
|
|
|
|
if (mTagName == 'r') {
|
|
|
|
char buff[50];
|
|
|
|
snprintf(buff, 50, "%-40f", mTagFloat);
|
|
|
|
tag += string(1, mTagName) + string("-") + string(buff) + string("\n");
|
2011-12-20 01:33:00 +00:00
|
|
|
} else if (mTagName == 'l') {
|
|
|
|
char tagBuff[1024];
|
|
|
|
Address pc = mTagAddress;
|
2012-03-09 15:20:03 +00:00
|
|
|
snprintf(tagBuff, 1024, "l-%p\n", pc);
|
|
|
|
tag += string(tagBuff);
|
2011-12-07 19:48:15 +00:00
|
|
|
} else {
|
|
|
|
tag += string(1, mTagName) + string("-") + string(mTagData) + string("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ENABLE_SPS_LEAF_DATA
|
|
|
|
if (mLeafAddress) {
|
|
|
|
char tagBuff[1024];
|
|
|
|
unsigned long pc = (unsigned long)mLeafAddress;
|
2012-03-09 15:20:03 +00:00
|
|
|
snprintf(tagBuff, 1024, "l-%llu\n", pc);
|
|
|
|
tag += string(tagBuff);
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return tag;
|
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
|
|
|
|
#define PROFILE_DEFAULT_ENTRY 100000
|
2011-12-07 19:48:15 +00:00
|
|
|
#define PROFILE_DEFAULT_INTERVAL 10
|
2012-01-12 18:33:32 +00:00
|
|
|
#define PROFILE_DEFAULT_FEATURES NULL
|
|
|
|
#define PROFILE_DEFAULT_FEATURE_COUNT 0
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
void mozilla_sampler_init()
|
|
|
|
{
|
|
|
|
// TODO linux port: Use TLS with ifdefs
|
2011-12-04 19:09:00 +00:00
|
|
|
if (!mozilla::tls::create(&pkey_stack) ||
|
|
|
|
!mozilla::tls::create(&pkey_ticker)) {
|
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-11 16:03:45 +00:00
|
|
|
Stack *stack = new Stack();
|
2011-12-04 19:09:00 +00:00
|
|
|
mozilla::tls::set(pkey_stack, stack);
|
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-01-12 18:33:32 +00:00
|
|
|
mozilla_sampler_start(PROFILE_DEFAULT_ENTRY, PROFILE_DEFAULT_INTERVAL,
|
|
|
|
PROFILE_DEFAULT_FEATURES, PROFILE_DEFAULT_FEATURE_COUNT);
|
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()
|
|
|
|
{
|
2011-12-04 19:09:00 +00:00
|
|
|
TableTicker *t = mozilla::tls::get<TableTicker>(pkey_ticker);
|
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()
|
|
|
|
{
|
2011-12-04 19:09:00 +00:00
|
|
|
TableTicker *t = mozilla::tls::get<TableTicker>(pkey_ticker);
|
2011-12-07 19:48:15 +00:00
|
|
|
if (!t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-16 14:03:54 +00:00
|
|
|
StringBuilder profile;
|
2012-03-11 16:03:45 +00:00
|
|
|
t->GetProfile()->ToString(profile);
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2011-12-16 14:03:54 +00:00
|
|
|
char *rtn = (char*)malloc( (profile.Length()+1) * sizeof(char) );
|
|
|
|
strcpy(rtn, profile.Buffer());
|
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)
|
|
|
|
{
|
|
|
|
TableTicker *t = mozilla::tls::get<TableTicker>(pkey_ticker);
|
|
|
|
if (!t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-11 16:03:45 +00:00
|
|
|
return t->GetProfile()->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-01-18 22:50:30 +00:00
|
|
|
#if defined(MOZ_PROFILING) && (defined(USE_BACKTRACE) || defined(USE_NS_STACKWALK))
|
2012-01-10 23:02:00 +00:00
|
|
|
"stackwalk",
|
|
|
|
#endif
|
2012-01-29 06:56:41 +00:00
|
|
|
"jank",
|
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-03-11 16:03:45 +00:00
|
|
|
Stack *stack = mozilla::tls::get<Stack>(pkey_stack);
|
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);
|
2011-12-04 19:09:00 +00:00
|
|
|
mozilla::tls::set(pkey_ticker, t);
|
2011-08-27 00:05:37 +00:00
|
|
|
t->Start();
|
|
|
|
}
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
void mozilla_sampler_stop()
|
2011-08-27 00:05:37 +00:00
|
|
|
{
|
2011-12-04 19:09:00 +00:00
|
|
|
TableTicker *t = mozilla::tls::get<TableTicker>(pkey_ticker);
|
2011-08-27 00:05:37 +00:00
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
t->Stop();
|
2012-03-11 16:03:45 +00:00
|
|
|
mozilla::tls::set(pkey_ticker, (Stack*)NULL);
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mozilla_sampler_is_active()
|
|
|
|
{
|
2011-12-04 19:09:00 +00:00
|
|
|
TableTicker *t = mozilla::tls::get<TableTicker>(pkey_ticker);
|
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
|
|
|
}
|
|
|
|
|