2011-11-04 21:07:50 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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-11-04 21:07:50 +00:00
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
#include <string>
|
2012-03-09 15:20:00 +00:00
|
|
|
#include <sstream>
|
2011-12-05 01:53:17 +00:00
|
|
|
#ifdef MOZ_INSTRUMENT_EVENT_LOOP
|
|
|
|
#include "EventTracer.h"
|
|
|
|
#endif
|
2011-12-07 19:48:15 +00:00
|
|
|
#include "sampler.h"
|
2011-11-04 21:07:50 +00:00
|
|
|
#include "nsProfiler.h"
|
2011-12-07 19:48:15 +00:00
|
|
|
#include "nsMemory.h"
|
2012-03-09 15:20:00 +00:00
|
|
|
#include "nsString.h"
|
2012-12-17 23:25:50 +00: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 21:57:20 +00:00
|
|
|
#include "jsapi.h"
|
2011-12-07 19:48:15 +00:00
|
|
|
|
|
|
|
using std::string;
|
2011-11-04 21:07:50 +00:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsProfiler, nsIProfiler)
|
|
|
|
|
|
|
|
nsProfiler::nsProfiler()
|
2012-12-17 23:25:50 +00:00
|
|
|
: mLockedForPrivateBrowsing(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsProfiler::~nsProfiler()
|
2011-11-04 21:07:50 +00:00
|
|
|
{
|
2012-12-17 23:25:50 +00: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 21:07:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 23:25:50 +00: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;
|
|
|
|
mozilla_sampler_lock();
|
|
|
|
}
|
|
|
|
} else if (strcmp(aTopic, "last-pb-context-exited") == 0) {
|
|
|
|
mLockedForPrivateBrowsing = false;
|
|
|
|
mozilla_sampler_unlock();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-11-04 21:07:50 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsProfiler::StartProfiler(uint32_t aEntries, uint32_t aInterval,
|
|
|
|
const char** aFeatures, uint32_t aFeatureCount)
|
2011-11-04 21:07:50 +00:00
|
|
|
{
|
2012-12-17 23:25:50 +00:00
|
|
|
if (mLockedForPrivateBrowsing) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-06-26 21:57:43 +00:00
|
|
|
SAMPLER_START(aEntries, aInterval, aFeatures, aFeatureCount);
|
2011-12-05 01:53:17 +00:00
|
|
|
#ifdef MOZ_INSTRUMENT_EVENT_LOOP
|
|
|
|
mozilla::InitEventTracing();
|
|
|
|
#endif
|
2011-11-04 21:07:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::StopProfiler()
|
|
|
|
{
|
2011-12-07 19:48:15 +00:00
|
|
|
SAMPLER_STOP();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:45:28 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::AddMarker(const char *aMarker)
|
|
|
|
{
|
|
|
|
SAMPLE_MARKER(aMarker);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::GetProfile(char **aProfile)
|
|
|
|
{
|
|
|
|
char *profile = SAMPLER_GET_PROFILE();
|
2011-12-04 19:09:00 +00:00
|
|
|
if (profile) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = strlen(profile);
|
2011-12-04 19:09:00 +00:00
|
|
|
char *profileStr = static_cast<char *>
|
|
|
|
(nsMemory::Clone(profile, (len + 1) * sizeof(char)));
|
|
|
|
profileStr[len] = '\0';
|
|
|
|
*aProfile = profileStr;
|
|
|
|
free(profile);
|
|
|
|
}
|
2011-12-07 19:48:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-09 15:20:00 +00:00
|
|
|
static void
|
2012-08-21 21:14:38 +00:00
|
|
|
AddSharedLibraryInfoToStream(std::ostream& aStream, const SharedLibrary& aLib)
|
2012-03-09 15:20:00 +00:00
|
|
|
{
|
|
|
|
aStream << "{";
|
|
|
|
aStream << "\"start\":" << aLib.GetStart();
|
|
|
|
aStream << ",\"end\":" << aLib.GetEnd();
|
2012-04-13 20:33:53 +00:00
|
|
|
aStream << ",\"offset\":" << aLib.GetOffset();
|
2012-03-09 15:20:00 +00:00
|
|
|
aStream << ",\"name\":\"" << aLib.GetName() << "\"";
|
2013-01-09 20:05:00 +00:00
|
|
|
const std::string &breakpadId = aLib.GetBreakpadId();
|
|
|
|
aStream << ",\"breakpadId\":\"" << breakpadId << "\"";
|
2012-03-12 11:07:05 +00:00
|
|
|
#ifdef XP_WIN
|
2013-01-09 20:05:00 +00: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;
|
|
|
|
stream << std::hex << pdbAgeStr;
|
|
|
|
unsigned pdbAge;
|
|
|
|
stream >> pdbAge;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::ostringstream oStream;
|
|
|
|
oStream << pdbSignature << std::hex << pdbAge;
|
|
|
|
MOZ_ASSERT(breakpadId == oStream.str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
aStream << ",\"pdbSignature\":\"" << pdbSignature << "\"";
|
|
|
|
aStream << ",\"pdbAge\":" << pdbAge;
|
|
|
|
aStream << ",\"pdbName\":\"" << aLib.GetName() << "\"";
|
2012-03-12 11:07:05 +00:00
|
|
|
#endif
|
2012-03-09 15:20:00 +00:00
|
|
|
aStream << "}";
|
|
|
|
}
|
|
|
|
|
2012-04-16 13:32:18 +00:00
|
|
|
std::string
|
2012-03-09 15:20:00 +00: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;
|
|
|
|
}
|
|
|
|
|
2012-02-02 21:57:20 +00:00
|
|
|
NS_IMETHODIMP nsProfiler::GetProfileData(JSContext* aCx, jsval* aResult)
|
|
|
|
{
|
|
|
|
JSObject *obj = SAMPLER_GET_PROFILE_DATA(aCx);
|
|
|
|
if (!obj)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aResult = OBJECT_TO_JSVAL(obj);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-07 19:48:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::IsActive(bool *aIsActive)
|
|
|
|
{
|
|
|
|
*aIsActive = SAMPLER_IS_ACTIVE();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsProfiler::GetResponsivenessTimes(uint32_t *aCount, double **aResult)
|
2011-12-07 19:48:15 +00:00
|
|
|
{
|
|
|
|
unsigned int len = 100;
|
2012-01-12 16:50:43 +00:00
|
|
|
const double* times = SAMPLER_GET_RESPONSIVENESS();
|
2011-12-07 19:48:15 +00:00
|
|
|
if (!times) {
|
|
|
|
*aCount = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2011-12-07 19:48:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-12 16:50:43 +00:00
|
|
|
double *fs = static_cast<double *>
|
|
|
|
(nsMemory::Clone(times, len * sizeof(double)));
|
2011-12-07 19:48:15 +00:00
|
|
|
|
|
|
|
*aCount = len;
|
|
|
|
*aResult = fs;
|
|
|
|
|
2011-11-04 21:07:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-12 18:33:32 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsProfiler::GetFeatures(uint32_t *aCount, char ***aFeatures)
|
2012-01-12 18:33:32 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = 0;
|
2012-01-12 18:33:32 +00:00
|
|
|
|
|
|
|
const char **features = SAMPLER_GET_FEATURES();
|
|
|
|
if (!features) {
|
|
|
|
*aCount = 0;
|
2012-07-30 14:20:58 +00:00
|
|
|
*aFeatures = nullptr;
|
2012-01-12 18:33:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-14 15:59:09 +00:00
|
|
|
while (features[len]) {
|
|
|
|
len++;
|
|
|
|
}
|
2012-01-12 18:33:32 +00:00
|
|
|
|
|
|
|
char **featureList = static_cast<char **>
|
|
|
|
(nsMemory::Alloc(len * sizeof(char*)));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t strLen = strlen(features[i]);
|
2012-01-12 18:33:32 +00:00
|
|
|
featureList[i] = static_cast<char *>
|
|
|
|
(nsMemory::Clone(features[i], (strLen + 1) * sizeof(char)));
|
|
|
|
}
|
|
|
|
|
|
|
|
*aFeatures = featureList;
|
|
|
|
*aCount = len;
|
|
|
|
return NS_OK;
|
|
|
|
}
|