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>
|
2013-09-27 16:08:45 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2013-03-25 21:57:28 +00:00
|
|
|
#include "SaveProfileTask.h"
|
|
|
|
#include "ProfileEntry.h"
|
2013-09-25 15:28:34 +00:00
|
|
|
#include "SyncProfile.h"
|
2011-08-27 00:05:37 +00:00
|
|
|
#include "platform.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prenv.h"
|
2013-09-23 17:29:27 +00:00
|
|
|
#include "prtime.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"
|
2013-03-25 21:57:28 +00:00
|
|
|
#include "TableTicker.h"
|
2013-03-21 09:17:23 +00:00
|
|
|
#include "nsXULAppAPI.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"
|
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"
|
2013-03-10 22:00:23 +00:00
|
|
|
#include "PlatformMacros.h"
|
2012-06-27 03:43:28 +00:00
|
|
|
|
2013-04-23 18:33:38 +00:00
|
|
|
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
|
2013-04-23 17:10:29 +00:00
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#endif
|
|
|
|
|
2012-08-06 22:58:47 +00:00
|
|
|
// JS
|
2013-08-27 02:05:20 +00:00
|
|
|
#include "js/OldDebugAPI.h"
|
2012-08-06 22:58:47 +00:00
|
|
|
|
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
|
|
|
|
|
2013-09-25 15:28:34 +00:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
typedef CONTEXT tickcontext_t;
|
|
|
|
#elif defined(LINUX)
|
|
|
|
#include <ucontext.h>
|
|
|
|
typedef ucontext_t tickcontext_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(LINUX) || defined(XP_MACOSX)
|
|
|
|
#include <sys/types.h>
|
|
|
|
pid_t gettid();
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 18:53:14 +00:00
|
|
|
#if defined(SPS_ARCH_arm) && defined(MOZ_WIDGET_GONK)
|
|
|
|
// Should also work on other Android and ARM Linux, but not tested there yet.
|
|
|
|
#define USE_EHABI_STACKWALK
|
|
|
|
#endif
|
|
|
|
#ifdef USE_EHABI_STACKWALK
|
|
|
|
#include "EHABIStackWalk.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
|
|
|
#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
|
|
|
|
|
2013-03-25 21:57:28 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// BEGIN SaveProfileTask et al
|
2011-08-27 00:05:37 +00:00
|
|
|
|
2012-04-16 13:32:18 +00:00
|
|
|
std::string GetSharedLibraryInfoString();
|
|
|
|
|
2011-08-27 00:05:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
template <typename Builder>
|
|
|
|
typename Builder::Object TableTicker::GetMetaJSCustomObject(Builder& b)
|
2012-06-27 03:43:28 +00:00
|
|
|
{
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject meta(b.context(), 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());
|
|
|
|
|
2013-09-12 20:31:40 +00:00
|
|
|
TimeDuration delta = TimeStamp::Now() - sStartTime;
|
2013-09-27 15:51:25 +00:00
|
|
|
b.DefineProperty(meta, "startTime", PR_Now()/1000.0 - delta.ToMilliseconds());
|
2013-09-12 20:31:40 +00:00
|
|
|
|
2012-06-27 03:43:28 +00:00
|
|
|
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);
|
2013-09-05 23:10:37 +00:00
|
|
|
JS::RootedObject profile(aCx, b.CreateObject());
|
2012-11-30 17:49:20 +00:00
|
|
|
BuildJSObject(b, profile);
|
2013-09-05 23:10:37 +00:00
|
|
|
return profile;
|
2012-11-30 17:49:20 +00:00
|
|
|
}
|
2012-03-12 14:58:40 +00:00
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
template <typename Builder>
|
2013-03-21 09:17:23 +00:00
|
|
|
struct SubprocessClosure {
|
2013-09-05 23:10:37 +00:00
|
|
|
SubprocessClosure(Builder *aBuilder, typename Builder::ArrayHandle aThreads)
|
|
|
|
: mBuilder(aBuilder), mThreads(aThreads)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Builder* mBuilder;
|
|
|
|
typename Builder::ArrayHandle mThreads;
|
2013-03-21 09:17:23 +00:00
|
|
|
};
|
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
template <typename Builder>
|
2013-03-21 09:17:23 +00:00
|
|
|
void SubProcessCallback(const char* aProfile, void* aClosure)
|
|
|
|
{
|
|
|
|
// Called by the observer to get their profile data included
|
|
|
|
// as a sub profile
|
2013-09-05 23:10:37 +00:00
|
|
|
SubprocessClosure<Builder>* closure = (SubprocessClosure<Builder>*)aClosure;
|
2013-03-21 09:17:23 +00:00
|
|
|
|
|
|
|
closure->mBuilder->ArrayPush(closure->mThreads, aProfile);
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:33:38 +00:00
|
|
|
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
|
2013-09-05 23:10:37 +00:00
|
|
|
template <typename Builder>
|
2013-04-23 17:10:29 +00:00
|
|
|
static
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::Object BuildJavaThreadJSObject(Builder& b)
|
2013-04-23 17:10:29 +00:00
|
|
|
{
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject javaThread(b.context(), b.CreateObject());
|
2013-04-23 17:10:29 +00:00
|
|
|
b.DefineProperty(javaThread, "name", "Java Main Thread");
|
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedArray samples(b.context(), b.CreateArray());
|
2013-04-23 17:10:29 +00:00
|
|
|
b.DefineProperty(javaThread, "samples", samples);
|
|
|
|
|
|
|
|
int sampleId = 0;
|
|
|
|
while (true) {
|
|
|
|
int frameId = 0;
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject sample(b.context());
|
|
|
|
typename Builder::RootedArray frames(b.context());
|
2013-04-23 17:10:29 +00:00
|
|
|
while (true) {
|
|
|
|
nsCString result;
|
|
|
|
bool hasFrame = AndroidBridge::Bridge()->GetFrameNameJavaProfiling(0, sampleId, frameId, result);
|
|
|
|
if (!hasFrame) {
|
|
|
|
if (frames) {
|
|
|
|
b.DefineProperty(sample, "frames", frames);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!sample) {
|
|
|
|
sample = b.CreateObject();
|
|
|
|
frames = b.CreateArray();
|
|
|
|
b.DefineProperty(sample, "frames", frames);
|
|
|
|
b.ArrayPush(samples, sample);
|
|
|
|
|
2013-11-12 18:41:01 +00:00
|
|
|
double sampleTime = GeckoJavaSampler::GetSampleTimeJavaProfiling(0, sampleId);
|
2013-04-23 17:10:29 +00:00
|
|
|
b.DefineProperty(sample, "time", sampleTime);
|
|
|
|
}
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject frame(b.context(), b.CreateObject());
|
2013-04-23 17:10:29 +00:00
|
|
|
b.DefineProperty(frame, "location", result.BeginReading());
|
|
|
|
b.ArrayPush(frames, frame);
|
|
|
|
frameId++;
|
|
|
|
}
|
|
|
|
if (frameId == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sampleId++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return javaThread;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
template <typename Builder>
|
|
|
|
void TableTicker::BuildJSObject(Builder& b, typename Builder::ObjectHandle profile)
|
2012-11-30 17:49:20 +00:00
|
|
|
{
|
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
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject meta(b.context(), 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
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedArray threads(b.context(), b.CreateArray());
|
2012-03-12 14:58:40 +00:00
|
|
|
b.DefineProperty(profile, "threads", threads);
|
|
|
|
|
2012-05-18 21:03:10 +00:00
|
|
|
SetPaused(true);
|
2013-03-29 19:34:49 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sRegisteredThreads->size(); i++) {
|
2013-04-03 22:59:17 +00:00
|
|
|
// Thread not being profiled, skip it
|
|
|
|
if (!sRegisteredThreads->at(i)->Profile())
|
|
|
|
continue;
|
|
|
|
|
2013-03-29 19:34:49 +00:00
|
|
|
MutexAutoLock lock(*sRegisteredThreads->at(i)->Profile()->GetMutex());
|
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject threadSamples(b.context(), b.CreateObject());
|
2013-03-29 19:34:49 +00:00
|
|
|
sRegisteredThreads->at(i)->Profile()->BuildJSObject(b, threadSamples);
|
|
|
|
b.ArrayPush(threads, threadSamples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:33:38 +00:00
|
|
|
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
|
2013-04-23 17:10:29 +00:00
|
|
|
if (ProfileJava()) {
|
2013-11-12 18:41:01 +00:00
|
|
|
GeckoJavaSampler::PauseJavaProfiling();
|
2013-04-23 17:10:29 +00:00
|
|
|
|
2013-09-05 23:10:37 +00:00
|
|
|
typename Builder::RootedObject javaThread(b.context(), BuildJavaThreadJSObject(b));
|
2013-04-23 17:10:29 +00:00
|
|
|
b.ArrayPush(threads, javaThread);
|
|
|
|
|
2013-11-12 18:41:01 +00:00
|
|
|
GeckoJavaSampler::UnpauseJavaProfiling();
|
2013-04-23 17:10:29 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-05-18 21:03:10 +00:00
|
|
|
SetPaused(false);
|
2013-03-21 09:17:23 +00:00
|
|
|
|
|
|
|
// Send a event asking any subprocesses (plugins) to
|
|
|
|
// give us their information
|
2013-09-05 23:10:37 +00:00
|
|
|
SubprocessClosure<Builder> closure(&b, threads);
|
2013-03-21 09:17:23 +00:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
2013-09-05 23:10:37 +00:00
|
|
|
nsRefPtr<ProfileSaveEvent> pse = new ProfileSaveEvent(SubProcessCallback<Builder>, &closure);
|
2013-03-21 09:17:23 +00:00
|
|
|
os->NotifyObservers(pse, "profiler-subprocess", nullptr);
|
|
|
|
}
|
2013-04-23 17:10:29 +00:00
|
|
|
}
|
2012-03-12 14:58:40 +00:00
|
|
|
|
2013-03-25 21:57:28 +00:00
|
|
|
// END SaveProfileTask et al
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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*)];
|
2013-03-26 17:32:09 +00:00
|
|
|
size_t len = sizeof(void*)/sizeof(char);
|
2012-12-11 19:10:56 +00:00
|
|
|
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,
|
2013-03-10 22:00:23 +00:00
|
|
|
PseudoStack *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()) {
|
2013-11-11 19:16:31 +00:00
|
|
|
// The JIT only allows the top-most entry to have a nullptr pc
|
2012-08-06 22:58:47 +00:00
|
|
|
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) {
|
2013-11-11 19:16:31 +00:00
|
|
|
lineno = JS_PCToLineNumber(nullptr, entry.script(), jspc);
|
2012-08-06 22:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-11 19:16:31 +00:00
|
|
|
lineno = JS_PCToLineNumber(nullptr, entry.script(), entry.pc());
|
2012-08-06 22:58:47 +00:00
|
|
|
}
|
|
|
|
} 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
|
|
|
|
2013-09-11 18:53:14 +00:00
|
|
|
#if defined(USE_NS_STACKWALK) || defined(USE_EHABI_STACKWALK)
|
2012-01-17 00:59:15 +00:00
|
|
|
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;
|
|
|
|
|
2013-09-11 18:51:11 +00:00
|
|
|
static void mergeNativeBacktrace(ThreadProfile &aProfile, const PCArray &array) {
|
|
|
|
aProfile.addTag(ProfileEntry('s', "(root)"));
|
|
|
|
|
|
|
|
PseudoStack* stack = aProfile.GetPseudoStack();
|
|
|
|
uint32_t 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
|
2013-11-11 19:16:31 +00:00
|
|
|
* address in which case we have a nullptr stack address in which case we assume
|
2013-09-11 18:51:11 +00:00
|
|
|
* that it follows immediatly the previous element.
|
|
|
|
*
|
|
|
|
* C Stack | Address -- Pseudo Stack | Address
|
|
|
|
* main() | 0x100 run_js() | 0x40
|
2013-11-11 19:16:31 +00:00
|
|
|
* start() | 0x80 jsCanvas() | nullptr
|
|
|
|
* timer() | 0x50 drawLine() | nullptr
|
2013-09-11 18:51:11 +00:00
|
|
|
* 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.
|
|
|
|
for (size_t i = array.count; i > 0; --i) {
|
|
|
|
while (pseudoStackPos < stack->stackSize()) {
|
|
|
|
volatile StackEntry& entry = stack->mStack[pseudoStackPos];
|
|
|
|
|
|
|
|
if (entry.stackAddress() < array.sp_array[i-1] && entry.stackAddress())
|
|
|
|
break;
|
|
|
|
|
|
|
|
addProfileEntry(entry, aProfile, stack, array.array[0]);
|
|
|
|
pseudoStackPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
aProfile.addTag(ProfileEntry('l', (void*)array.array[i-1]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NS_STACKWALK
|
2012-01-17 00:59:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-25 21:57:28 +00:00
|
|
|
void TableTicker::doNativeBacktrace(ThreadProfile &aProfile, TickSample* aSample)
|
2012-01-17 00:59:15 +00:00
|
|
|
{
|
2012-12-05 23:10:15 +00:00
|
|
|
#ifndef XP_MACOSX
|
2013-04-03 22:59:17 +00:00
|
|
|
uintptr_t thread = GetThreadHandle(aSample->threadProfile->GetPlatformData());
|
2012-12-05 23:10:15 +00:00
|
|
|
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
|
|
|
|
2013-03-26 17:32:09 +00:00
|
|
|
uint32_t maxFrames = uint32_t(array.size - array.count);
|
2012-02-03 20:19:18 +00:00
|
|
|
#ifdef XP_MACOSX
|
2013-04-03 22:59:17 +00:00
|
|
|
pthread_t pt = GetProfiledThread(aSample->threadProfile->GetPlatformData());
|
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
|
2013-10-15 19:06:20 +00:00
|
|
|
if (aSample->isSamplingCurrentThread) {
|
|
|
|
// In this case we want NS_StackWalk to know that it's walking the
|
|
|
|
// current thread's stack, so we pass 0 as the thread handle.
|
|
|
|
thread = 0;
|
|
|
|
}
|
2013-01-30 01:59:12 +00:00
|
|
|
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
|
2013-09-11 18:51:11 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mergeNativeBacktrace(aProfile, array);
|
2012-01-17 00:59:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 18:53:14 +00:00
|
|
|
#ifdef USE_EHABI_STACKWALK
|
|
|
|
void TableTicker::doNativeBacktrace(ThreadProfile &aProfile, TickSample* aSample)
|
|
|
|
{
|
|
|
|
void *pc_array[1000];
|
|
|
|
void *sp_array[1000];
|
|
|
|
PCArray array = {
|
|
|
|
pc_array,
|
|
|
|
sp_array,
|
|
|
|
mozilla::ArrayLength(pc_array),
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
ucontext_t *ucontext = reinterpret_cast<ucontext_t *>(aSample->context);
|
|
|
|
array.count = EHABIStackWalk(ucontext->uc_mcontext, aProfile.GetStackTop(),
|
|
|
|
sp_array, pc_array, array.size);
|
|
|
|
mergeNativeBacktrace(aProfile, array);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-12-20 01:33:00 +00:00
|
|
|
static
|
2013-03-10 22:00:23 +00:00
|
|
|
void doSampleStackTrace(PseudoStack *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
|
|
|
}
|
|
|
|
|
|
|
|
void TableTicker::Tick(TickSample* sample)
|
2013-04-01 19:45:03 +00:00
|
|
|
{
|
|
|
|
if (HasUnwinderThread()) {
|
|
|
|
UnwinderTick(sample);
|
|
|
|
} else {
|
|
|
|
InplaceTick(sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableTicker::InplaceTick(TickSample* sample)
|
2011-12-20 01:33:00 +00:00
|
|
|
{
|
2013-03-29 19:34:49 +00:00
|
|
|
ThreadProfile& currThreadProfile = *sample->threadProfile;
|
|
|
|
|
|
|
|
PseudoStack* stack = currThreadProfile.GetPseudoStack();
|
2011-12-20 20:13:52 +00:00
|
|
|
bool recordSample = true;
|
2013-10-08 14:05:25 +00:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
bool powerSample = false;
|
|
|
|
#endif
|
2013-09-25 15:28:34 +00:00
|
|
|
|
|
|
|
/* Don't process the PeudoStack's markers or honour jankOnly if we're
|
|
|
|
immediately sampling the current thread. */
|
|
|
|
if (!sample->isSamplingCurrentThread) {
|
|
|
|
// Marker(s) come before the sample
|
|
|
|
ProfilerMarkerLinkedList* pendingMarkersList = stack->getPendingMarkers();
|
|
|
|
while (pendingMarkersList && pendingMarkersList->peek()) {
|
|
|
|
ProfilerMarker* marker = pendingMarkersList->popHead();
|
|
|
|
stack->addStoredMarker(marker);
|
|
|
|
currThreadProfile.addTag(ProfileEntry('m', marker));
|
2012-03-12 14:56:33 +00:00
|
|
|
}
|
2013-09-25 15:28:34 +00:00
|
|
|
stack->updateGeneration(currThreadProfile.GetGenerationID());
|
|
|
|
|
2013-10-08 14:05:25 +00:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
if (mProfilePower) {
|
|
|
|
mIntelPowerGadget->TakeSample();
|
|
|
|
powerSample = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-25 15:28:34 +00:00
|
|
|
if (mJankOnly) {
|
|
|
|
// 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
|
|
|
|
currThreadProfile.erase();
|
|
|
|
}
|
|
|
|
sLastSampledEventGeneration = sCurrentEventGeneration;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-12-20 20:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 23:02:00 +00:00
|
|
|
}
|
2011-12-20 20:13:52 +00:00
|
|
|
|
2013-09-11 18:53:14 +00:00
|
|
|
#if defined(USE_NS_STACKWALK) || defined(USE_EHABI_STACKWALK)
|
2012-01-18 23:07:46 +00:00
|
|
|
if (mUseStackWalk) {
|
2013-03-29 19:34:49 +00:00
|
|
|
doNativeBacktrace(currThreadProfile, sample);
|
2012-01-18 23:07:46 +00:00
|
|
|
} else {
|
2013-03-29 19:34:49 +00:00
|
|
|
doSampleStackTrace(stack, currThreadProfile, mAddLeafAddresses ? sample : nullptr);
|
2011-12-20 20:13:52 +00:00
|
|
|
}
|
2012-01-18 23:07:46 +00:00
|
|
|
#else
|
2013-03-29 19:34:49 +00:00
|
|
|
doSampleStackTrace(stack, currThreadProfile, mAddLeafAddresses ? sample : nullptr);
|
2012-01-18 23:07:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recordSample)
|
2013-03-29 19:34:49 +00:00
|
|
|
currThreadProfile.flush();
|
2011-12-07 19:48:15 +00:00
|
|
|
|
2013-03-29 19:34:49 +00:00
|
|
|
if (!sLastTracerEvent.IsNull() && sample && currThreadProfile.IsMainThread()) {
|
2011-12-24 17:11:26 +00:00
|
|
|
TimeDuration delta = sample->timestamp - sLastTracerEvent;
|
2013-03-29 19:34:49 +00:00
|
|
|
currThreadProfile.addTag(ProfileEntry('r', delta.ToMilliseconds()));
|
2011-12-07 19:48:15 +00:00
|
|
|
}
|
2012-07-05 19:49:19 +00:00
|
|
|
|
|
|
|
if (sample) {
|
2013-04-23 17:10:29 +00:00
|
|
|
TimeDuration delta = sample->timestamp - sStartTime;
|
2013-03-29 19:34:49 +00:00
|
|
|
currThreadProfile.addTag(ProfileEntry('t', delta.ToMilliseconds()));
|
2012-07-05 19:49:19 +00:00
|
|
|
}
|
2012-09-05 15:45:17 +00:00
|
|
|
|
2013-10-08 14:05:25 +00:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
if (powerSample) {
|
|
|
|
currThreadProfile.addTag(ProfileEntry('p', mIntelPowerGadget->GetTotalPackagePowerInWatts()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-05 15:45:17 +00:00
|
|
|
if (sLastFrameNumber != sFrameNumber) {
|
2013-03-29 19:34:49 +00:00
|
|
|
currThreadProfile.addTag(ProfileEntry('f', sFrameNumber));
|
2012-09-05 15:45:17 +00:00
|
|
|
sLastFrameNumber = sFrameNumber;
|
|
|
|
}
|
2011-08-27 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 15:28:34 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
SyncProfile* NewSyncProfile()
|
|
|
|
{
|
|
|
|
PseudoStack* stack = tlsPseudoStack.get();
|
|
|
|
if (!stack) {
|
|
|
|
MOZ_ASSERT(stack);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Thread::tid_t tid = Thread::GetCurrentId();
|
|
|
|
|
|
|
|
SyncProfile* profile = new SyncProfile("SyncProfile",
|
|
|
|
GET_BACKTRACE_DEFAULT_ENTRY,
|
|
|
|
stack, tid, NS_IsMainThread());
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
SyncProfile* TableTicker::GetBacktrace()
|
|
|
|
{
|
|
|
|
SyncProfile* profile = NewSyncProfile();
|
|
|
|
|
|
|
|
TickSample sample;
|
|
|
|
sample.threadProfile = profile;
|
|
|
|
|
2013-10-23 04:56:50 +00:00
|
|
|
#if defined(HAVE_NATIVE_UNWIND)
|
2013-09-25 15:28:34 +00:00
|
|
|
#if defined(XP_WIN) || defined(LINUX)
|
|
|
|
tickcontext_t context;
|
|
|
|
sample.PopulateContext(&context);
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
sample.PopulateContext(nullptr);
|
2013-10-23 04:56:50 +00:00
|
|
|
#endif
|
2013-09-25 15:28:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
sample.isSamplingCurrentThread = true;
|
|
|
|
sample.timestamp = mozilla::TimeStamp::Now();
|
|
|
|
|
|
|
|
if (!HasUnwinderThread()) {
|
|
|
|
profile->BeginUnwind();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tick(&sample);
|
|
|
|
|
|
|
|
if (!HasUnwinderThread()) {
|
|
|
|
profile->EndUnwind();
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_callback(const ProfileEntry& entry, const char* tagStringData)
|
|
|
|
{
|
2013-03-25 21:57:28 +00:00
|
|
|
switch (entry.getTagName()) {
|
2012-11-19 23:13:28 +00:00
|
|
|
case 's':
|
|
|
|
case 'c':
|
|
|
|
printf_stderr(" %s\n", tagStringData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 22:00:23 +00:00
|
|
|
void mozilla_sampler_print_location1()
|
2012-11-19 23:13:28 +00:00
|
|
|
{
|
|
|
|
if (!stack_key_initialized)
|
2013-11-11 19:16:31 +00:00
|
|
|
profiler_init(nullptr);
|
2012-11-19 23:13:28 +00:00
|
|
|
|
2013-09-25 15:28:34 +00:00
|
|
|
SyncProfile* syncProfile = NewSyncProfile();
|
|
|
|
if (!syncProfile) {
|
2012-11-19 23:13:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-25 15:28:34 +00:00
|
|
|
syncProfile->BeginUnwind();
|
2013-11-11 19:16:31 +00:00
|
|
|
doSampleStackTrace(syncProfile->GetPseudoStack(), *syncProfile, nullptr);
|
2013-09-25 15:28:34 +00:00
|
|
|
syncProfile->EndUnwind();
|
2012-11-19 23:13:28 +00:00
|
|
|
|
|
|
|
printf_stderr("Backtrace:\n");
|
2013-09-25 15:28:34 +00:00
|
|
|
syncProfile->IterateTags(print_callback);
|
|
|
|
delete syncProfile;
|
2012-11-19 23:13:28 +00:00
|
|
|
}
|
2012-12-17 23:25:50 +00:00
|
|
|
|
|
|
|
|