2011-11-04 14:07:50 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -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/. */
|
2011-11-04 14:07:50 -07:00
|
|
|
|
2011-12-07 11:48:15 -08:00
|
|
|
#include <string>
|
2012-03-09 07:20:00 -08:00
|
|
|
#include <sstream>
|
2011-12-04 17:53:17 -08:00
|
|
|
#ifdef MOZ_INSTRUMENT_EVENT_LOOP
|
|
|
|
#include "EventTracer.h"
|
|
|
|
#endif
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2011-11-04 14:07:50 -07:00
|
|
|
#include "nsProfiler.h"
|
2011-12-07 11:48:15 -08:00
|
|
|
#include "nsMemory.h"
|
2012-03-09 07:20:00 -08:00
|
|
|
#include "nsString.h"
|
2012-12-17 15:25:50 -08:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "shared-libraries.h"
|
2012-02-02 13:57:20 -08:00
|
|
|
#include "jsapi.h"
|
2011-12-07 11:48:15 -08:00
|
|
|
|
|
|
|
using std::string;
|
2011-11-04 14:07:50 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsProfiler, nsIProfiler)
|
|
|
|
|
|
|
|
nsProfiler::nsProfiler()
|
2012-12-17 15:25:50 -08:00
|
|
|
: mLockedForPrivateBrowsing(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsProfiler::~nsProfiler()
|
2011-11-04 14:07:50 -07:00
|
|
|
{
|
2012-12-17 15:25:50 -08:00
|
|
|
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, "chrome-document-global-created");
|
|
|
|
observerService->RemoveObserver(this, "last-pb-context-exited");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsProfiler::Init() {
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, "chrome-document-global-created", false);
|
|
|
|
observerService->AddObserver(this, "last-pb-context-exited", false);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2011-11-04 14:07:50 -07:00
|
|
|
}
|
|
|
|
|
2012-12-17 15:25:50 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (strcmp(aTopic, "chrome-document-global-created") == 0) {
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> requestor = do_QueryInterface(aSubject);
|
|
|
|
nsCOMPtr<nsIWebNavigation> parentWebNav = do_GetInterface(requestor);
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(parentWebNav);
|
|
|
|
if (loadContext && loadContext->UsePrivateBrowsing() && !mLockedForPrivateBrowsing) {
|
|
|
|
mLockedForPrivateBrowsing = true;
|
2013-03-15 21:47:02 -07:00
|
|
|
profiler_lock();
|
2012-12-17 15:25:50 -08:00
|
|
|
}
|
|
|
|
} else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
|
|
|
|
mLockedForPrivateBrowsing = false;
|
2013-03-15 21:47:02 -07:00
|
|
|
profiler_unlock();
|
2012-12-17 15:25:50 -08:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-11-04 14:07:50 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsProfiler::StartProfiler(uint32_t aEntries, uint32_t aInterval,
|
|
|
|
const char** aFeatures, uint32_t aFeatureCount)
|
2011-11-04 14:07:50 -07:00
|
|
|
{
|
2012-12-17 15:25:50 -08:00
|
|
|
if (mLockedForPrivateBrowsing) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2013-03-15 21:47:02 -07:00
|
|
|
profiler_start(aEntries, aInterval, aFeatures, aFeatureCount);
|
2011-12-04 17:53:17 -08:00
|
|
|
#ifdef MOZ_INSTRUMENT_EVENT_LOOP
|
2013-03-22 09:16:52 -07:00
|
|
|
bool printToConsole = false;
|
|
|
|
mozilla::InitEventTracing(printToConsole);
|
2011-12-04 17:53:17 -08:00
|
|
|
#endif
|
2011-11-04 14:07:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::StopProfiler()
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
profiler_stop();
|
2011-12-07 11:48:15 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-05 06:45:28 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::AddMarker(const char *aMarker)
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_MARKER(aMarker);
|
2012-09-05 06:45:28 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-07 11:48:15 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::GetProfile(char **aProfile)
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
char *profile = profiler_get_profile();
|
2011-12-04 11:09:00 -08:00
|
|
|
if (profile) {
|
2013-03-26 10:32:09 -07:00
|
|
|
size_t len = strlen(profile);
|
2011-12-04 11:09:00 -08:00
|
|
|
char *profileStr = static_cast<char *>
|
|
|
|
(nsMemory::Clone(profile, (len + 1) * sizeof(char)));
|
|
|
|
profileStr[len] = '\0';
|
|
|
|
*aProfile = profileStr;
|
|
|
|
free(profile);
|
|
|
|
}
|
2011-12-07 11:48:15 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-09 07:20:00 -08:00
|
|
|
static void
|
2012-08-21 14:14:38 -07:00
|
|
|
AddSharedLibraryInfoToStream(std::ostream& aStream, const SharedLibrary& aLib)
|
2012-03-09 07:20:00 -08:00
|
|
|
{
|
|
|
|
aStream << "{";
|
|
|
|
aStream << "\"start\":" << aLib.GetStart();
|
|
|
|
aStream << ",\"end\":" << aLib.GetEnd();
|
2012-04-13 13:33:53 -07:00
|
|
|
aStream << ",\"offset\":" << aLib.GetOffset();
|
2012-03-09 07:20:00 -08:00
|
|
|
aStream << ",\"name\":\"" << aLib.GetName() << "\"";
|
2013-01-09 12:05:00 -08:00
|
|
|
const std::string &breakpadId = aLib.GetBreakpadId();
|
|
|
|
aStream << ",\"breakpadId\":\"" << breakpadId << "\"";
|
2012-03-12 04:07:05 -07:00
|
|
|
#ifdef XP_WIN
|
2013-01-09 12:05:00 -08:00
|
|
|
// FIXME: remove this XP_WIN code when the profiler plugin has switched to
|
|
|
|
// using breakpadId.
|
|
|
|
std::string pdbSignature = breakpadId.substr(0, 32);
|
|
|
|
std::string pdbAgeStr = breakpadId.substr(32, breakpadId.size() - 1);
|
|
|
|
|
|
|
|
std::stringstream stream;
|
2013-01-14 14:45:46 -08:00
|
|
|
stream << pdbAgeStr;
|
|
|
|
|
2013-01-09 12:05:00 -08:00
|
|
|
unsigned pdbAge;
|
2013-01-14 14:45:46 -08:00
|
|
|
stream << std::hex;
|
2013-01-09 12:05:00 -08:00
|
|
|
stream >> pdbAge;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::ostringstream oStream;
|
2013-01-14 13:20:05 -08:00
|
|
|
oStream << pdbSignature << std::hex << std::uppercase << pdbAge;
|
2013-01-09 12:05:00 -08:00
|
|
|
MOZ_ASSERT(breakpadId == oStream.str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
aStream << ",\"pdbSignature\":\"" << pdbSignature << "\"";
|
|
|
|
aStream << ",\"pdbAge\":" << pdbAge;
|
|
|
|
aStream << ",\"pdbName\":\"" << aLib.GetName() << "\"";
|
2012-03-12 04:07:05 -07:00
|
|
|
#endif
|
2012-03-09 07:20:00 -08:00
|
|
|
aStream << "}";
|
|
|
|
}
|
|
|
|
|
2012-04-16 06:32:18 -07:00
|
|
|
std::string
|
2012-03-09 07:20:00 -08:00
|
|
|
GetSharedLibraryInfoString()
|
|
|
|
{
|
|
|
|
SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf();
|
|
|
|
if (info.GetSize() == 0)
|
|
|
|
return "[]";
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
os << "[";
|
|
|
|
AddSharedLibraryInfoToStream(os, info.GetEntry(0));
|
|
|
|
|
|
|
|
for (size_t i = 1; i < info.GetSize(); i++) {
|
|
|
|
os << ",";
|
|
|
|
AddSharedLibraryInfoToStream(os, info.GetEntry(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "]";
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::GetSharedLibraryInformation(nsAString& aOutString)
|
|
|
|
{
|
|
|
|
aOutString.Assign(NS_ConvertUTF8toUTF16(GetSharedLibraryInfoString().c_str()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-15 22:22:01 -07:00
|
|
|
NS_IMETHODIMP nsProfiler::GetProfileData(JSContext* aCx, JS::Value* aResult)
|
2012-02-02 13:57:20 -08:00
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
JSObject *obj = profiler_get_profile_jsobject(aCx);
|
2012-02-02 13:57:20 -08:00
|
|
|
if (!obj)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aResult = OBJECT_TO_JSVAL(obj);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-07 11:48:15 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::IsActive(bool *aIsActive)
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
*aIsActive = profiler_is_active();
|
2011-12-07 11:48:15 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsProfiler::GetResponsivenessTimes(uint32_t *aCount, double **aResult)
|
2011-12-07 11:48:15 -08:00
|
|
|
{
|
|
|
|
unsigned int len = 100;
|
2013-03-15 21:47:02 -07:00
|
|
|
const double* times = profiler_get_responsiveness();
|
2011-12-07 11:48:15 -08:00
|
|
|
if (!times) {
|
|
|
|
*aCount = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2011-12-07 11:48:15 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-12 08:50:43 -08:00
|
|
|
double *fs = static_cast<double *>
|
|
|
|
(nsMemory::Clone(times, len * sizeof(double)));
|
2011-12-07 11:48:15 -08:00
|
|
|
|
|
|
|
*aCount = len;
|
|
|
|
*aResult = fs;
|
|
|
|
|
2011-11-04 14:07:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-12 10:33:32 -08:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsProfiler::GetFeatures(uint32_t *aCount, char ***aFeatures)
|
2012-01-12 10:33:32 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = 0;
|
2012-01-12 10:33:32 -08:00
|
|
|
|
2013-03-15 21:47:02 -07:00
|
|
|
const char **features = profiler_get_features();
|
2012-01-12 10:33:32 -08:00
|
|
|
if (!features) {
|
|
|
|
*aCount = 0;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aFeatures = nullptr;
|
2012-01-12 10:33:32 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-14 07:59:09 -08:00
|
|
|
while (features[len]) {
|
|
|
|
len++;
|
|
|
|
}
|
2012-01-12 10:33:32 -08:00
|
|
|
|
|
|
|
char **featureList = static_cast<char **>
|
|
|
|
(nsMemory::Alloc(len * sizeof(char*)));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2013-03-26 10:32:09 -07:00
|
|
|
size_t strLen = strlen(features[i]);
|
2012-01-12 10:33:32 -08:00
|
|
|
featureList[i] = static_cast<char *>
|
|
|
|
(nsMemory::Clone(features[i], (strLen + 1) * sizeof(char)));
|
|
|
|
}
|
|
|
|
|
|
|
|
*aFeatures = featureList;
|
|
|
|
*aCount = len;
|
|
|
|
return NS_OK;
|
|
|
|
}
|