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
|
2011-12-07 11:48:15 -08:00
|
|
|
#include "sampler.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 "shared-libraries.h"
|
|
|
|
#include "nsString.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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-25 16:23:25 -07:00
|
|
|
nsProfiler::StartProfiler(PRUint32 aInterval, PRUint32 aEntries,
|
2012-01-12 10:33:32 -08:00
|
|
|
const char** aFeatures, PRUint32 aFeatureCount)
|
2011-11-04 14:07:50 -07:00
|
|
|
{
|
2012-06-25 16:23:25 -07:00
|
|
|
SAMPLER_START(aInterval, aEntries, aFeatures, aFeatureCount);
|
2011-12-04 17:53:17 -08:00
|
|
|
#ifdef MOZ_INSTRUMENT_EVENT_LOOP
|
|
|
|
mozilla::InitEventTracing();
|
|
|
|
#endif
|
2011-11-04 14:07:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::StopProfiler()
|
|
|
|
{
|
2011-12-07 11:48:15 -08:00
|
|
|
SAMPLER_STOP();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::GetProfile(char **aProfile)
|
|
|
|
{
|
|
|
|
char *profile = SAMPLER_GET_PROFILE();
|
2011-12-04 11:09:00 -08:00
|
|
|
if (profile) {
|
|
|
|
PRUint32 len = strlen(profile);
|
|
|
|
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
|
|
|
|
AddSharedLibraryInfoToStream(std::ostream& aStream, SharedLibrary& aLib)
|
|
|
|
{
|
|
|
|
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() << "\"";
|
2012-03-12 04:07:05 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
aStream << ",\"pdbSignature\":\"" << aLib.GetPdbSignature().ToString() << "\"";
|
|
|
|
aStream << ",\"pdbAge\":" << aLib.GetPdbAge();
|
2012-03-26 19:02:46 -07:00
|
|
|
aStream << ",\"pdbName\":\"" << aLib.GetPdbName() << "\"";
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-02 13:57:20 -08: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 11:48:15 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsProfiler::IsActive(bool *aIsActive)
|
|
|
|
{
|
|
|
|
*aIsActive = SAMPLER_IS_ACTIVE();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-01-12 08:50:43 -08:00
|
|
|
nsProfiler::GetResponsivenessTimes(PRUint32 *aCount, double **aResult)
|
2011-12-07 11:48:15 -08:00
|
|
|
{
|
|
|
|
unsigned int len = 100;
|
2012-01-12 08:50:43 -08:00
|
|
|
const double* times = SAMPLER_GET_RESPONSIVENESS();
|
2011-12-07 11:48:15 -08:00
|
|
|
if (!times) {
|
|
|
|
*aCount = 0;
|
|
|
|
*aResult = nsnull;
|
|
|
|
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
|
|
|
|
nsProfiler::GetFeatures(PRUint32 *aCount, char ***aFeatures)
|
|
|
|
{
|
|
|
|
PRUint32 len = 0;
|
|
|
|
|
|
|
|
const char **features = SAMPLER_GET_FEATURES();
|
|
|
|
if (!features) {
|
|
|
|
*aCount = 0;
|
|
|
|
*aFeatures = nsnull;
|
|
|
|
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++) {
|
|
|
|
PRUint32 strLen = strlen(features[i]);
|
|
|
|
featureList[i] = static_cast<char *>
|
|
|
|
(nsMemory::Clone(features[i], (strLen + 1) * sizeof(char)));
|
|
|
|
}
|
|
|
|
|
|
|
|
*aFeatures = featureList;
|
|
|
|
*aCount = len;
|
|
|
|
return NS_OK;
|
|
|
|
}
|