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 "shared-libraries.h"
|
|
|
|
#include "nsString.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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-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() << "\"";
|
2012-03-12 11:07:05 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
aStream << ",\"pdbSignature\":\"" << aLib.GetPdbSignature().ToString() << "\"";
|
|
|
|
aStream << ",\"pdbAge\":" << aLib.GetPdbAge();
|
2012-03-27 02:02:46 +00:00
|
|
|
aStream << ",\"pdbName\":\"" << aLib.GetPdbName() << "\"";
|
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;
|
|
|
|
}
|