2013-03-25 14:57:28 -07: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/. */
|
|
|
|
|
2013-05-27 05:29:24 -07:00
|
|
|
#include <ostream>
|
2013-03-25 14:57:28 -07:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-06-14 11:01:02 -07:00
|
|
|
#include "IOInterposer.h"
|
2013-09-12 07:47:37 -07:00
|
|
|
#include "NSPRInterposer.h"
|
2013-06-14 11:01:02 -07:00
|
|
|
#include "ProfilerIOInterposeObserver.h"
|
2013-03-25 14:57:28 -07:00
|
|
|
#include "platform.h"
|
|
|
|
#include "PlatformMacros.h"
|
|
|
|
#include "prenv.h"
|
2013-09-12 07:47:37 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2013-03-25 14:57:28 -07:00
|
|
|
#include "mozilla/ThreadLocal.h"
|
|
|
|
#include "PseudoStack.h"
|
|
|
|
#include "TableTicker.h"
|
|
|
|
#include "UnwinderThread2.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "mozilla/Services.h"
|
2013-03-29 12:34:49 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2013-03-25 14:57:28 -07:00
|
|
|
|
2013-04-23 11:33:38 -07:00
|
|
|
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
|
2013-04-23 10:10:29 -07:00
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#endif
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
mozilla::ThreadLocal<PseudoStack *> tlsPseudoStack;
|
|
|
|
mozilla::ThreadLocal<TableTicker *> tlsTicker;
|
|
|
|
// We need to track whether we've been initialized otherwise
|
|
|
|
// we end up using tlsStack without initializing it.
|
|
|
|
// Because tlsStack is totally opaque to us we can't reuse
|
|
|
|
// it as the flag itself.
|
|
|
|
bool stack_key_initialized;
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
TimeStamp sLastTracerEvent; // is raced on
|
2013-04-23 10:10:29 -07:00
|
|
|
TimeStamp sStartTime;
|
2013-04-03 15:59:17 -07:00
|
|
|
int sFrameNumber = 0;
|
|
|
|
int sLastFrameNumber = 0;
|
2013-04-18 08:34:49 -07:00
|
|
|
int sInitCount = 0; // Each init must have a matched shutdown.
|
2013-04-03 15:59:17 -07:00
|
|
|
static bool sIsProfiling = false; // is raced on
|
2013-03-25 14:57:28 -07: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
|
2013-03-29 12:34:49 -07:00
|
|
|
* note: it might also be worth trackplaing everytime we go around
|
2013-03-25 14:57:28 -07:00
|
|
|
* 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 */
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
std::vector<ThreadInfo*>* Sampler::sRegisteredThreads = nullptr;
|
|
|
|
mozilla::Mutex* Sampler::sRegisteredThreadsMutex = nullptr;
|
2013-03-29 12:34:49 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
TableTicker* Sampler::sActiveSampler;
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
static mozilla::StaticAutoPtr<mozilla::ProfilerIOInterposeObserver>
|
|
|
|
sInterposeObserver;
|
2013-06-14 11:01:02 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
void Sampler::Startup() {
|
|
|
|
sRegisteredThreads = new std::vector<ThreadInfo*>();
|
|
|
|
sRegisteredThreadsMutex = new mozilla::Mutex("sRegisteredThreads mutex");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sampler::Shutdown() {
|
|
|
|
while (sRegisteredThreads->size() > 0) {
|
|
|
|
delete sRegisteredThreads->back();
|
|
|
|
sRegisteredThreads->pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete sRegisteredThreadsMutex;
|
|
|
|
delete sRegisteredThreads;
|
2013-04-18 08:34:49 -07:00
|
|
|
|
|
|
|
// UnregisterThread can be called after shutdown in XPCShell. Thus
|
|
|
|
// we need to point to null to ignore such a call after shutdown.
|
|
|
|
sRegisteredThreadsMutex = nullptr;
|
|
|
|
sRegisteredThreads = nullptr;
|
2013-04-03 15:59:17 -07:00
|
|
|
}
|
2013-03-29 12:34:49 -07:00
|
|
|
|
|
|
|
ThreadInfo::~ThreadInfo() {
|
|
|
|
free(mName);
|
|
|
|
|
|
|
|
if (mProfile)
|
|
|
|
delete mProfile;
|
2013-04-03 15:59:17 -07:00
|
|
|
|
|
|
|
Sampler::FreePlatformData(mPlatformData);
|
2013-03-29 12:34:49 -07:00
|
|
|
}
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
bool sps_version2()
|
|
|
|
{
|
|
|
|
static int version = 0; // Raced on, potentially
|
|
|
|
|
|
|
|
if (version == 0) {
|
|
|
|
bool allow2 = false; // Is v2 allowable on this platform?
|
|
|
|
# if defined(SPS_PLAT_amd64_linux) || defined(SPS_PLAT_arm_android) \
|
|
|
|
|| defined(SPS_PLAT_x86_linux)
|
|
|
|
allow2 = true;
|
|
|
|
# elif defined(SPS_PLAT_amd64_darwin) || defined(SPS_PLAT_x86_darwin) \
|
|
|
|
|| defined(SPS_PLAT_x86_windows) || defined(SPS_PLAT_x86_android) \
|
|
|
|
|| defined(SPS_PLAT_amd64_windows)
|
|
|
|
allow2 = false;
|
|
|
|
# else
|
|
|
|
# error "Unknown platform"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
bool req2 = PR_GetEnv("MOZ_PROFILER_NEW") != NULL; // Has v2 been requested?
|
|
|
|
|
|
|
|
bool elfhackd = false;
|
|
|
|
# if defined(USE_ELF_HACK)
|
|
|
|
bool elfhackd = true;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if (req2 && allow2) {
|
|
|
|
version = 2;
|
|
|
|
LOG("------------------- MOZ_PROFILER_NEW set -------------------");
|
|
|
|
} else if (req2 && !allow2) {
|
|
|
|
version = 1;
|
|
|
|
LOG("--------------- MOZ_PROFILER_NEW requested, ----------------");
|
|
|
|
LOG("---------- but is not available on this platform -----------");
|
|
|
|
} else if (req2 && elfhackd) {
|
|
|
|
version = 1;
|
|
|
|
LOG("--------------- MOZ_PROFILER_NEW requested, ----------------");
|
|
|
|
LOG("--- but this build was not done with --disable-elf-hack ----");
|
|
|
|
} else {
|
|
|
|
version = 1;
|
|
|
|
LOG("----------------- MOZ_PROFILER_NEW not set -----------------");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return version == 2;
|
|
|
|
}
|
|
|
|
|
2013-04-06 01:45:30 -07:00
|
|
|
#if !defined(ANDROID)
|
|
|
|
/* Has MOZ_PROFILER_VERBOSE been set? */
|
|
|
|
bool moz_profiler_verbose()
|
|
|
|
{
|
|
|
|
/* 0 = not checked, 1 = unset, 2 = set */
|
|
|
|
static int status = 0; // Raced on, potentially
|
|
|
|
|
|
|
|
if (status == 0) {
|
|
|
|
if (PR_GetEnv("MOZ_PROFILER_VERBOSE") != NULL)
|
|
|
|
status = 2;
|
|
|
|
else
|
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status == 2;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
static inline const char* name_UnwMode(UnwMode m)
|
|
|
|
{
|
|
|
|
switch (m) {
|
|
|
|
case UnwINVALID: return "invalid";
|
|
|
|
case UnwNATIVE: return "native";
|
|
|
|
case UnwPSEUDO: return "pseudo";
|
|
|
|
case UnwCOMBINED: return "combined";
|
|
|
|
default: return "??name_UnwMode??";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read env vars at startup, so as to set sUnwindMode and sInterval.
|
2013-04-06 01:49:11 -07:00
|
|
|
void read_profiler_env_vars()
|
2013-03-25 14:57:28 -07:00
|
|
|
{
|
|
|
|
bool nativeAvail = false;
|
|
|
|
# if defined(HAVE_NATIVE_UNWIND)
|
|
|
|
nativeAvail = true;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
MOZ_ASSERT(sUnwindMode == UnwINVALID);
|
|
|
|
MOZ_ASSERT(sUnwindInterval == 0);
|
2013-08-05 09:03:22 -07:00
|
|
|
MOZ_ASSERT(sProfileEntries == 0);
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
/* Set defaults */
|
|
|
|
sUnwindMode = nativeAvail ? UnwCOMBINED : UnwPSEUDO;
|
|
|
|
sUnwindInterval = 0; /* We'll have to look elsewhere */
|
2013-08-05 09:03:22 -07:00
|
|
|
sProfileEntries = 0;
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
const char* strM = PR_GetEnv("MOZ_PROFILER_MODE");
|
|
|
|
const char* strI = PR_GetEnv("MOZ_PROFILER_INTERVAL");
|
2013-08-05 09:03:22 -07:00
|
|
|
const char* strE = PR_GetEnv("MOZ_PROFILER_ENTRIES");
|
2013-04-06 01:49:11 -07:00
|
|
|
const char* strF = PR_GetEnv("MOZ_PROFILER_STACK_SCAN");
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
if (strM) {
|
|
|
|
if (0 == strcmp(strM, "pseudo"))
|
|
|
|
sUnwindMode = UnwPSEUDO;
|
|
|
|
else if (0 == strcmp(strM, "native") && nativeAvail)
|
|
|
|
sUnwindMode = UnwNATIVE;
|
|
|
|
else if (0 == strcmp(strM, "combined") && nativeAvail)
|
|
|
|
sUnwindMode = UnwCOMBINED;
|
|
|
|
else goto usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strI) {
|
|
|
|
errno = 0;
|
|
|
|
long int n = strtol(strI, (char**)NULL, 10);
|
|
|
|
if (errno == 0 && n >= 1 && n <= 1000) {
|
|
|
|
sUnwindInterval = n;
|
|
|
|
}
|
|
|
|
else goto usage;
|
|
|
|
}
|
|
|
|
|
2013-08-05 09:03:22 -07:00
|
|
|
if (strE) {
|
|
|
|
errno = 0;
|
|
|
|
long int n = strtol(strE, (char**)NULL, 10);
|
|
|
|
if (errno == 0 && n > 0) {
|
|
|
|
sProfileEntries = n;
|
|
|
|
}
|
|
|
|
else goto usage;
|
|
|
|
}
|
|
|
|
|
2013-04-06 01:49:11 -07:00
|
|
|
if (strF) {
|
|
|
|
errno = 0;
|
|
|
|
long int n = strtol(strF, (char**)NULL, 10);
|
|
|
|
if (errno == 0 && n >= 0 && n <= 100) {
|
|
|
|
sUnwindStackScan = n;
|
|
|
|
}
|
|
|
|
else goto usage;
|
|
|
|
}
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
usage:
|
|
|
|
LOG( "SPS: ");
|
|
|
|
LOG( "SPS: Environment variable usage:");
|
|
|
|
LOG( "SPS: ");
|
|
|
|
LOG( "SPS: MOZ_PROFILER_MODE=native for native unwind only");
|
|
|
|
LOG( "SPS: MOZ_PROFILER_MODE=pseudo for pseudo unwind only");
|
|
|
|
LOG( "SPS: MOZ_PROFILER_MODE=combined for combined native & pseudo unwind");
|
|
|
|
LOG( "SPS: If unset, default is 'combined' on native-capable");
|
|
|
|
LOG( "SPS: platforms, 'pseudo' on others.");
|
|
|
|
LOG( "SPS: ");
|
|
|
|
LOG( "SPS: MOZ_PROFILER_INTERVAL=<number> (milliseconds, 1 to 1000)");
|
|
|
|
LOG( "SPS: If unset, platform default is used.");
|
|
|
|
LOG( "SPS: ");
|
2013-08-05 09:03:22 -07:00
|
|
|
LOG( "SPS: MOZ_PROFILER_ENTRIES=<number> (count, minimum of 1)");
|
|
|
|
LOG( "SPS: If unset, platform default is used.");
|
|
|
|
LOG( "SPS: ");
|
2013-04-06 01:45:30 -07:00
|
|
|
LOG( "SPS: MOZ_PROFILER_VERBOSE");
|
|
|
|
LOG( "SPS: If set to any value, increases verbosity (recommended).");
|
|
|
|
LOG( "SPS: ");
|
2013-04-06 01:49:11 -07:00
|
|
|
LOG( "SPS: MOZ_PROFILER_STACK_SCAN=<number> (default is zero)");
|
|
|
|
LOG( "SPS: The number of dubious (stack-scanned) frames allowed");
|
|
|
|
LOG( "SPS: ");
|
2013-04-06 01:45:30 -07:00
|
|
|
LOG( "SPS: MOZ_PROFILER_NEW");
|
|
|
|
LOG( "SPS: Needs to be set to use Breakpad-based unwinding.");
|
|
|
|
LOG( "SPS: ");
|
2013-03-25 14:57:28 -07:00
|
|
|
LOGF("SPS: This platform %s native unwinding.",
|
|
|
|
nativeAvail ? "supports" : "does not support");
|
|
|
|
LOG( "SPS: ");
|
|
|
|
/* Re-set defaults */
|
|
|
|
sUnwindMode = nativeAvail ? UnwCOMBINED : UnwPSEUDO;
|
|
|
|
sUnwindInterval = 0; /* We'll have to look elsewhere */
|
2013-08-05 09:03:22 -07:00
|
|
|
sProfileEntries = 0;
|
2013-04-06 01:49:11 -07:00
|
|
|
sUnwindStackScan = 0;
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
out:
|
|
|
|
LOG( "SPS:");
|
|
|
|
LOGF("SPS: Unwind mode = %s", name_UnwMode(sUnwindMode));
|
|
|
|
LOGF("SPS: Sampling interval = %d ms (zero means \"platform default\")",
|
|
|
|
(int)sUnwindInterval);
|
2013-08-05 09:03:22 -07:00
|
|
|
LOGF("SPS: Entry store size = %d (zero means \"platform default\")",
|
|
|
|
(int)sProfileEntries);
|
2013-04-06 01:49:11 -07:00
|
|
|
LOGF("SPS: UnwindStackScan = %d (max dubious frames per unwind).",
|
|
|
|
(int)sUnwindStackScan);
|
2013-03-25 14:57:28 -07:00
|
|
|
LOG( "SPS: Use env var MOZ_PROFILER_MODE=help for further information.");
|
|
|
|
LOG( "SPS:");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// BEGIN externally visible functions
|
|
|
|
|
2013-05-28 05:03:38 -07:00
|
|
|
void mozilla_sampler_init(void* stackTop)
|
2013-03-25 14:57:28 -07:00
|
|
|
{
|
2013-04-18 08:34:49 -07:00
|
|
|
sInitCount++;
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
if (stack_key_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG("BEGIN mozilla_sampler_init");
|
|
|
|
if (!tlsPseudoStack.init() || !tlsTicker.init()) {
|
|
|
|
LOG("Failed to init.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stack_key_initialized = true;
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
Sampler::Startup();
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
PseudoStack *stack = new PseudoStack();
|
|
|
|
tlsPseudoStack.set(stack);
|
|
|
|
|
2013-05-28 05:03:38 -07:00
|
|
|
Sampler::RegisterCurrentThread("Gecko", stack, true, stackTop);
|
2013-04-03 15:59:17 -07:00
|
|
|
|
2013-04-01 12:45:03 -07:00
|
|
|
// Read mode settings from MOZ_PROFILER_MODE and interval
|
|
|
|
// settings from MOZ_PROFILER_INTERVAL and stack-scan threshhold
|
|
|
|
// from MOZ_PROFILER_STACK_SCAN.
|
|
|
|
read_profiler_env_vars();
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
// Allow the profiler to be started using signals
|
|
|
|
OS::RegisterStartHandler();
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
// Initialize I/O interposing
|
|
|
|
mozilla::IOInterposer::Init();
|
|
|
|
// Initialize NSPR I/O Interposing
|
|
|
|
mozilla::InitNSPRIOInterposing();
|
2013-06-14 11:01:02 -07:00
|
|
|
|
2013-03-25 14:57:28 -07: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");
|
|
|
|
if (!val || !*val) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* features[] = {"js"
|
|
|
|
, "leaf"
|
2013-09-11 11:53:14 -07:00
|
|
|
#if defined(XP_WIN) || defined(XP_MACOSX) || (defined(SPS_ARCH_arm) && defined(linux))
|
2013-03-25 14:57:28 -07:00
|
|
|
, "stackwalk"
|
2013-07-09 13:34:44 -07:00
|
|
|
#endif
|
|
|
|
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
|
|
|
|
, "java"
|
2013-03-25 14:57:28 -07:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
profiler_start(PROFILE_DEFAULT_ENTRY, PROFILE_DEFAULT_INTERVAL,
|
2013-05-16 13:31:50 -07:00
|
|
|
features, sizeof(features)/sizeof(const char*),
|
|
|
|
// TODO Add env variable to select threads
|
|
|
|
NULL, 0);
|
2013-03-25 14:57:28 -07:00
|
|
|
LOG("END mozilla_sampler_init");
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_shutdown()
|
|
|
|
{
|
2013-04-18 08:34:49 -07:00
|
|
|
sInitCount--;
|
|
|
|
|
|
|
|
if (sInitCount > 0)
|
|
|
|
return;
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
// Save the profile on shutdown if requested.
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
profiler_stop();
|
2013-04-03 15:59:17 -07:00
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
// Unregister IO interpose observer
|
|
|
|
mozilla::IOInterposer::Unregister(mozilla::IOInterposeObserver::OpAll,
|
|
|
|
sInterposeObserver);
|
|
|
|
// mozilla_sampler_shutdown is only called at shutdown, and late-write checks
|
|
|
|
// might need the IO interposer, so we don't clear it. Don't worry it's
|
|
|
|
// designed not to report leaks.
|
|
|
|
// mozilla::IOInterposer::Clear();
|
|
|
|
mozilla::ClearNSPRIOInterposing();
|
2013-06-14 11:01:02 -07:00
|
|
|
sInterposeObserver = nullptr;
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
Sampler::Shutdown();
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_save()
|
|
|
|
{
|
|
|
|
TableTicker *t = tlsTicker.get();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
char* mozilla_sampler_get_profile()
|
|
|
|
{
|
|
|
|
TableTicker *t = tlsTicker.get();
|
|
|
|
if (!t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-21 02:17:23 -07:00
|
|
|
std::stringstream stream;
|
|
|
|
t->ToStreamAsJSON(stream);
|
|
|
|
char* profile = strdup(stream.str().c_str());
|
|
|
|
return profile;
|
2013-03-25 14:57:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *mozilla_sampler_get_profile_data(JSContext *aCx)
|
|
|
|
{
|
|
|
|
TableTicker *t = tlsTicker.get();
|
|
|
|
if (!t) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t->ToJSObject(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char** mozilla_sampler_get_features()
|
|
|
|
{
|
|
|
|
static const char* features[] = {
|
|
|
|
#if defined(MOZ_PROFILING) && defined(HAVE_NATIVE_UNWIND)
|
2013-04-01 12:45:03 -07:00
|
|
|
// Walk the C++ stack.
|
2013-03-25 14:57:28 -07:00
|
|
|
"stackwalk",
|
|
|
|
#endif
|
|
|
|
#if defined(ENABLE_SPS_LEAF_DATA)
|
2013-04-01 12:45:03 -07:00
|
|
|
// Include the C++ leaf node if not stackwalking. DevTools
|
|
|
|
// profiler doesn't want the native addresses.
|
2013-03-25 14:57:28 -07:00
|
|
|
"leaf",
|
|
|
|
#endif
|
2013-04-01 12:45:03 -07:00
|
|
|
#if !defined(SPS_OS_windows)
|
|
|
|
// Use a seperate thread of walking the stack.
|
|
|
|
"unwinder",
|
|
|
|
#endif
|
2013-04-23 10:10:29 -07:00
|
|
|
"java",
|
2013-04-01 12:45:03 -07:00
|
|
|
// Only record samples during periods of bad responsiveness
|
2013-03-25 14:57:28 -07:00
|
|
|
"jank",
|
2013-04-01 12:45:03 -07:00
|
|
|
// Tell the JS engine to emmit pseudostack entries in the
|
|
|
|
// pro/epilogue.
|
2013-03-25 14:57:28 -07:00
|
|
|
"js",
|
2013-04-01 12:45:03 -07:00
|
|
|
// Profile the registered secondary threads.
|
2013-04-03 15:59:17 -07:00
|
|
|
"threads",
|
2013-06-14 09:42:10 -07:00
|
|
|
// Do not include user-identifiable information
|
|
|
|
"privacy",
|
2013-06-14 11:01:02 -07:00
|
|
|
// Add main thread I/O to the profile
|
|
|
|
"mainthreadio",
|
2013-03-25 14:57:28 -07:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Values are only honored on the first start
|
2013-03-26 09:34:27 -07:00
|
|
|
void mozilla_sampler_start(int aProfileEntries, double aInterval,
|
2013-05-16 13:31:50 -07:00
|
|
|
const char** aFeatures, uint32_t aFeatureCount,
|
|
|
|
const char** aThreadNameFilters, uint32_t aFilterCount)
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
{
|
|
|
|
if (!stack_key_initialized)
|
2013-05-28 05:03:38 -07:00
|
|
|
profiler_init(NULL);
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
/* If the sampling interval was set using env vars, use that
|
|
|
|
in preference to anything else. */
|
|
|
|
if (sUnwindInterval > 0)
|
|
|
|
aInterval = sUnwindInterval;
|
|
|
|
|
2013-08-05 09:03:22 -07:00
|
|
|
/* If the entry count was set using env vars, use that, too: */
|
|
|
|
if (sProfileEntries > 0)
|
|
|
|
aProfileEntries = sProfileEntries;
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
// Reset the current state if the profiler is running
|
|
|
|
profiler_stop();
|
|
|
|
|
|
|
|
TableTicker* t;
|
2013-04-01 12:45:03 -07:00
|
|
|
t = new TableTicker(aInterval ? aInterval : PROFILE_DEFAULT_INTERVAL,
|
|
|
|
aProfileEntries ? aProfileEntries : PROFILE_DEFAULT_ENTRY,
|
2013-05-16 13:31:50 -07:00
|
|
|
aFeatures, aFeatureCount,
|
|
|
|
aThreadNameFilters, aFilterCount);
|
2013-04-01 12:45:03 -07:00
|
|
|
if (t->HasUnwinderThread()) {
|
|
|
|
// Create the unwinder thread. ATM there is only one.
|
|
|
|
uwt__init();
|
2013-03-25 14:57:28 -07:00
|
|
|
}
|
2013-04-01 12:45:03 -07:00
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
tlsTicker.set(t);
|
|
|
|
t->Start();
|
2013-06-14 09:42:10 -07:00
|
|
|
if (t->ProfileJS() || t->InPrivacyMode()) {
|
2013-04-03 15:59:17 -07:00
|
|
|
mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
|
|
|
|
std::vector<ThreadInfo*> threads = t->GetRegisteredThreads();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < threads.size(); i++) {
|
|
|
|
ThreadInfo* info = threads[i];
|
|
|
|
ThreadProfile* thread_profile = info->Profile();
|
|
|
|
if (!thread_profile) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-14 09:42:10 -07:00
|
|
|
if (t->ProfileJS()) {
|
|
|
|
thread_profile->GetPseudoStack()->enableJSSampling();
|
|
|
|
}
|
|
|
|
if (t->InPrivacyMode()) {
|
|
|
|
thread_profile->GetPseudoStack()->mPrivacyMode = true;
|
|
|
|
}
|
2013-04-03 15:59:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:33:38 -07:00
|
|
|
#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
|
2013-04-23 10:10:29 -07:00
|
|
|
if (t->ProfileJava()) {
|
|
|
|
int javaInterval = aInterval;
|
|
|
|
// Java sampling doesn't accuratly keep up with 1ms sampling
|
|
|
|
if (javaInterval < 10) {
|
|
|
|
aInterval = 10;
|
|
|
|
}
|
|
|
|
mozilla::AndroidBridge::Bridge()->StartJavaProfiling(javaInterval, 1000);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-14 11:01:02 -07:00
|
|
|
if (t->AddMainThreadIO()) {
|
2013-09-12 07:47:37 -07:00
|
|
|
if (!sInterposeObserver) {
|
|
|
|
// Lazily create IO interposer observer
|
|
|
|
sInterposeObserver = new mozilla::ProfilerIOInterposeObserver();
|
|
|
|
}
|
|
|
|
mozilla::IOInterposer::Register(mozilla::IOInterposeObserver::OpAll,
|
|
|
|
sInterposeObserver);
|
2013-06-14 11:01:02 -07:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
sIsProfiling = true;
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os)
|
|
|
|
os->NotifyObservers(nullptr, "profiler-started", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_stop()
|
|
|
|
{
|
|
|
|
if (!stack_key_initialized)
|
2013-05-28 05:03:38 -07:00
|
|
|
profiler_init(NULL);
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
TableTicker *t = tlsTicker.get();
|
|
|
|
if (!t) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool disableJS = t->ProfileJS();
|
2013-04-01 12:45:03 -07:00
|
|
|
bool unwinderThreader = t->HasUnwinderThread();
|
|
|
|
|
|
|
|
// Shut down and reap the unwinder thread. We have to do this
|
|
|
|
// before stopping the sampler, so as to guarantee that the unwinder
|
|
|
|
// thread doesn't try to access memory that the subsequent call to
|
|
|
|
// mozilla_sampler_stop causes to be freed.
|
|
|
|
if (unwinderThreader) {
|
|
|
|
uwt__stop();
|
|
|
|
}
|
2013-03-25 14:57:28 -07:00
|
|
|
|
|
|
|
t->Stop();
|
|
|
|
delete t;
|
|
|
|
tlsTicker.set(NULL);
|
|
|
|
|
2013-06-14 09:42:10 -07:00
|
|
|
if (disableJS) {
|
|
|
|
PseudoStack *stack = tlsPseudoStack.get();
|
|
|
|
ASSERT(stack != NULL);
|
2013-03-25 14:57:28 -07:00
|
|
|
stack->disableJSSampling();
|
2013-06-14 09:42:10 -07:00
|
|
|
}
|
2013-03-25 14:57:28 -07:00
|
|
|
|
2013-04-01 12:45:03 -07:00
|
|
|
if (unwinderThreader) {
|
|
|
|
uwt__deinit();
|
|
|
|
}
|
|
|
|
|
2013-09-12 07:47:37 -07:00
|
|
|
mozilla::IOInterposer::Unregister(mozilla::IOInterposeObserver::OpAll,
|
|
|
|
sInterposeObserver);
|
2013-06-14 11:01:02 -07:00
|
|
|
|
2013-04-03 15:59:17 -07:00
|
|
|
sIsProfiling = false;
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os)
|
|
|
|
os->NotifyObservers(nullptr, "profiler-stopped", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mozilla_sampler_is_active()
|
|
|
|
{
|
2013-04-03 15:59:17 -07:00
|
|
|
return sIsProfiling;
|
2013-03-25 14:57:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static double sResponsivenessTimes[100];
|
|
|
|
static unsigned int sResponsivenessLoc = 0;
|
|
|
|
void mozilla_sampler_responsiveness(const 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();
|
|
|
|
}
|
|
|
|
sCurrentEventGeneration++;
|
|
|
|
|
|
|
|
sLastTracerEvent = aTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
const double* mozilla_sampler_get_responsiveness()
|
|
|
|
{
|
|
|
|
return sResponsivenessTimes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_frame_number(int frameNumber)
|
|
|
|
{
|
|
|
|
sFrameNumber = frameNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_print_location2()
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_lock()
|
|
|
|
{
|
|
|
|
profiler_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);
|
|
|
|
}
|
|
|
|
|
2013-05-28 05:03:38 -07:00
|
|
|
bool mozilla_sampler_register_thread(const char* aName, void* stackTop)
|
2013-03-29 12:34:49 -07:00
|
|
|
{
|
2013-07-16 06:56:43 -07:00
|
|
|
#ifndef MOZ_WIDGET_GONK
|
2013-03-29 12:34:49 -07:00
|
|
|
PseudoStack* stack = new PseudoStack();
|
|
|
|
tlsPseudoStack.set(stack);
|
|
|
|
|
2013-05-28 05:03:38 -07:00
|
|
|
return Sampler::RegisterCurrentThread(aName, stack, false, stackTop);
|
2013-07-16 06:56:43 -07:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2013-03-29 12:34:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void mozilla_sampler_unregister_thread()
|
|
|
|
{
|
2013-07-16 06:56:43 -07:00
|
|
|
#ifndef MOZ_WIDGET_GONK
|
2013-03-29 12:34:49 -07:00
|
|
|
Sampler::UnregisterCurrentThread();
|
2013-05-16 08:38:23 -07:00
|
|
|
|
|
|
|
PseudoStack *stack = tlsPseudoStack.get();
|
|
|
|
if (!stack) {
|
|
|
|
ASSERT(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete stack;
|
|
|
|
tlsPseudoStack.set(nullptr);
|
2013-07-16 06:56:43 -07:00
|
|
|
#endif
|
2013-03-29 12:34:49 -07:00
|
|
|
}
|
|
|
|
|
2013-04-23 10:10:29 -07:00
|
|
|
double mozilla_sampler_time()
|
|
|
|
{
|
2013-07-09 13:34:44 -07:00
|
|
|
if (!mozilla_sampler_is_active()) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
2013-04-23 10:10:29 -07:00
|
|
|
TimeDuration delta = TimeStamp::Now() - sStartTime;
|
|
|
|
return delta.ToMilliseconds();
|
|
|
|
}
|
|
|
|
|
2013-03-25 14:57:28 -07:00
|
|
|
// END externally visible functions
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-03-29 12:34:49 -07:00
|
|
|
|