2011-04-27 11:07:02 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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-04-27 11:07:02 -07:00
|
|
|
|
|
|
|
#include "base/histogram.h"
|
2011-12-09 12:15:53 -08:00
|
|
|
#include "base/pickle.h"
|
2011-04-27 11:07:02 -07:00
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "mozilla/ModuleUtils.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsITelemetry.h"
|
2011-12-09 12:15:53 -08:00
|
|
|
#include "nsIFile.h"
|
2011-06-20 14:47:58 -07:00
|
|
|
#include "Telemetry.h"
|
2011-06-20 14:48:03 -07:00
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsBaseHashtable.h"
|
2011-05-21 23:23:20 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2011-12-06 12:12:55 -08:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2011-12-09 12:15:53 -08:00
|
|
|
#include "mozilla/FileUtils.h"
|
2012-03-21 10:26:48 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-04-27 11:07:02 -07:00
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
namespace {
|
|
|
|
|
2011-04-27 11:07:02 -07:00
|
|
|
using namespace base;
|
2011-06-20 14:47:58 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-02-13 11:47:40 -08:00
|
|
|
template<class EntryType>
|
|
|
|
class AutoHashtable : public nsTHashtable<EntryType>
|
|
|
|
{
|
|
|
|
public:
|
2012-05-16 04:39:21 -07:00
|
|
|
AutoHashtable(uint32_t initSize = PL_DHASH_MIN_SIZE);
|
2012-02-13 11:47:40 -08:00
|
|
|
~AutoHashtable();
|
|
|
|
typedef bool (*ReflectEntryFunc)(EntryType *entry, JSContext *cx, JSObject *obj);
|
|
|
|
bool ReflectHashtable(ReflectEntryFunc entryFunc, JSContext *cx, JSObject *obj);
|
|
|
|
private:
|
|
|
|
struct EnumeratorArgs {
|
|
|
|
JSContext *cx;
|
|
|
|
JSObject *obj;
|
|
|
|
ReflectEntryFunc entryFunc;
|
|
|
|
};
|
|
|
|
static PLDHashOperator ReflectEntryStub(EntryType *entry, void *arg);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class EntryType>
|
2012-05-16 04:39:21 -07:00
|
|
|
AutoHashtable<EntryType>::AutoHashtable(uint32_t initSize)
|
2012-02-13 11:47:40 -08:00
|
|
|
{
|
|
|
|
this->Init(initSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class EntryType>
|
|
|
|
AutoHashtable<EntryType>::~AutoHashtable()
|
|
|
|
{
|
|
|
|
this->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename EntryType>
|
|
|
|
PLDHashOperator
|
|
|
|
AutoHashtable<EntryType>::ReflectEntryStub(EntryType *entry, void *arg)
|
|
|
|
{
|
|
|
|
EnumeratorArgs *args = static_cast<EnumeratorArgs *>(arg);
|
|
|
|
if (!args->entryFunc(entry, args->cx, args->obj)) {
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reflect the individual entries of table into JS, usually by defining
|
|
|
|
* some property and value of obj. entryFunc is called for each entry.
|
|
|
|
*/
|
|
|
|
template<typename EntryType>
|
|
|
|
bool
|
|
|
|
AutoHashtable<EntryType>::ReflectHashtable(ReflectEntryFunc entryFunc,
|
|
|
|
JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
EnumeratorArgs args = { cx, obj, entryFunc };
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t num = this->EnumerateEntries(ReflectEntryStub, static_cast<void*>(&args));
|
2012-02-13 11:47:40 -08:00
|
|
|
return num == this->Count();
|
|
|
|
}
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
class TelemetryImpl : public nsITelemetry
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSITELEMETRY
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
public:
|
2011-06-20 14:48:03 -07:00
|
|
|
TelemetryImpl();
|
|
|
|
~TelemetryImpl();
|
2011-06-28 16:57:44 -07:00
|
|
|
|
|
|
|
static bool CanRecord();
|
|
|
|
static already_AddRefed<nsITelemetry> CreateTelemetryInstance();
|
|
|
|
static void ShutdownTelemetry();
|
2012-03-21 10:26:48 -07:00
|
|
|
static void RecordSlowStatement(const nsACString &sql, const nsACString &dbName,
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t delay, bool isDynamicString);
|
2012-03-14 19:57:04 -07:00
|
|
|
#if defined(MOZ_ENABLE_PROFILER_SPS)
|
2012-05-16 04:39:21 -07:00
|
|
|
static void RecordChromeHang(uint32_t duration,
|
2012-03-12 04:07:05 -07:00
|
|
|
const Telemetry::HangStack &callStack,
|
|
|
|
SharedLibraryInfo &moduleMap);
|
2012-03-14 19:57:04 -07:00
|
|
|
#endif
|
2012-01-06 14:13:02 -08:00
|
|
|
static nsresult GetHistogramEnumId(const char *name, Telemetry::ID *id);
|
2011-12-06 12:12:55 -08:00
|
|
|
struct StmtStats {
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t hitCount;
|
|
|
|
uint32_t totalTime;
|
2012-03-21 10:26:48 -07:00
|
|
|
bool isDynamicSql;
|
|
|
|
bool isTrackedDb;
|
|
|
|
bool isAggregate;
|
2011-12-06 12:12:55 -08:00
|
|
|
};
|
|
|
|
typedef nsBaseHashtableET<nsCStringHashKey, StmtStats> SlowSQLEntryType;
|
2012-03-12 04:07:05 -07:00
|
|
|
struct HangReport {
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t duration;
|
2012-03-12 04:07:05 -07:00
|
|
|
Telemetry::HangStack callStack;
|
2012-03-14 19:57:04 -07:00
|
|
|
#if defined(MOZ_ENABLE_PROFILER_SPS)
|
2012-03-12 04:07:05 -07:00
|
|
|
SharedLibraryInfo moduleMap;
|
2012-03-14 19:57:04 -07:00
|
|
|
#endif
|
2012-03-12 04:07:05 -07:00
|
|
|
};
|
2011-06-20 14:47:58 -07:00
|
|
|
|
|
|
|
private:
|
2012-05-16 04:39:21 -07:00
|
|
|
static void StoreSlowSQL(const nsACString &offender, uint32_t delay,
|
2012-03-21 10:26:48 -07:00
|
|
|
bool isDynamicSql, bool isTrackedDB, bool isAggregate);
|
|
|
|
|
|
|
|
static bool ReflectPublicSql(SlowSQLEntryType *entry, JSContext *cx,
|
|
|
|
JSObject *obj);
|
|
|
|
static bool ReflectPrivateSql(SlowSQLEntryType *entry, JSContext *cx,
|
|
|
|
JSObject *obj);
|
|
|
|
static bool ReflectSql(SlowSQLEntryType *entry, JSContext *cx, JSObject *obj);
|
|
|
|
|
|
|
|
bool AddSQLInfo(JSContext *cx, JSObject *rootObj, bool mainThread,
|
|
|
|
bool includePrivateStrings);
|
|
|
|
bool GetSQLStats(JSContext *cx, jsval *ret, bool includePrivateSql);
|
2011-12-06 12:12:55 -08:00
|
|
|
|
2012-01-06 11:39:29 -08:00
|
|
|
// Like GetHistogramById, but returns the underlying C++ object, not the JS one.
|
|
|
|
nsresult GetHistogramByName(const nsACString &name, Histogram **ret);
|
2012-01-06 14:13:02 -08:00
|
|
|
bool ShouldReflectHistogram(Histogram *h);
|
|
|
|
void IdentifyCorruptHistograms(StatisticsRecorder::Histograms &hs);
|
|
|
|
typedef StatisticsRecorder::Histograms::iterator HistogramIterator;
|
2012-01-20 13:56:48 -08:00
|
|
|
|
|
|
|
struct AddonHistogramInfo {
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t min;
|
|
|
|
uint32_t max;
|
|
|
|
uint32_t bucketCount;
|
|
|
|
uint32_t histogramType;
|
2012-01-20 13:56:48 -08:00
|
|
|
Histogram *h;
|
|
|
|
};
|
|
|
|
typedef nsBaseHashtableET<nsCStringHashKey, AddonHistogramInfo> AddonHistogramEntryType;
|
|
|
|
typedef AutoHashtable<AddonHistogramEntryType> AddonHistogramMapType;
|
|
|
|
typedef nsBaseHashtableET<nsCStringHashKey, AddonHistogramMapType *> AddonEntryType;
|
|
|
|
typedef AutoHashtable<AddonEntryType> AddonMapType;
|
|
|
|
static bool AddonHistogramReflector(AddonHistogramEntryType *entry,
|
|
|
|
JSContext *cx, JSObject *obj);
|
|
|
|
static bool AddonReflector(AddonEntryType *entry, JSContext *cx, JSObject *obj);
|
2012-03-02 06:59:38 -08:00
|
|
|
static bool CreateHistogramForAddon(const nsACString &name,
|
|
|
|
AddonHistogramInfo &info);
|
2012-01-20 13:56:48 -08:00
|
|
|
AddonMapType mAddonMap;
|
|
|
|
|
2012-01-06 14:13:02 -08:00
|
|
|
// This is used for speedy string->Telemetry::ID conversions
|
2011-06-20 14:48:03 -07:00
|
|
|
typedef nsBaseHashtableET<nsCharPtrHashKey, Telemetry::ID> CharPtrEntryType;
|
2012-02-13 11:47:40 -08:00
|
|
|
typedef AutoHashtable<CharPtrEntryType> HistogramMapType;
|
2011-06-20 14:48:03 -07:00
|
|
|
HistogramMapType mHistogramMap;
|
2011-06-28 16:57:44 -07:00
|
|
|
bool mCanRecord;
|
|
|
|
static TelemetryImpl *sTelemetry;
|
2012-02-13 11:47:40 -08:00
|
|
|
AutoHashtable<SlowSQLEntryType> mSlowSQLOnMainThread;
|
|
|
|
AutoHashtable<SlowSQLEntryType> mSlowSQLOnOtherThread;
|
|
|
|
// This gets marked immutable in debug builds, so we can't use
|
|
|
|
// AutoHashtable here.
|
2011-12-06 12:12:55 -08:00
|
|
|
nsTHashtable<nsCStringHashKey> mTrackedDBs;
|
|
|
|
Mutex mHashMutex;
|
2012-03-12 04:07:05 -07:00
|
|
|
nsTArray<HangReport> mHangReports;
|
|
|
|
Mutex mHangReportsMutex;
|
2011-04-27 11:07:02 -07:00
|
|
|
};
|
|
|
|
|
2011-06-28 16:57:44 -07:00
|
|
|
TelemetryImpl* TelemetryImpl::sTelemetry = NULL;
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
// A initializer to initialize histogram collection
|
|
|
|
StatisticsRecorder gStatisticsRecorder;
|
2011-04-27 11:07:02 -07:00
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
// Hardcoded probes
|
|
|
|
struct TelemetryHistogram {
|
|
|
|
const char *id;
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t min;
|
|
|
|
uint32_t max;
|
|
|
|
uint32_t bucketCount;
|
|
|
|
uint32_t histogramType;
|
2012-01-06 11:40:45 -08:00
|
|
|
const char *comment;
|
2011-06-20 14:47:58 -07:00
|
|
|
};
|
|
|
|
|
2011-12-16 11:30:44 -08:00
|
|
|
// Perform the checks at the beginning of HistogramGet at compile time, so
|
|
|
|
// that if people add incorrect histogram definitions, they get compiler
|
|
|
|
// errors.
|
|
|
|
#define HISTOGRAM(id, min, max, bucket_count, histogram_type, b) \
|
2012-04-02 17:21:24 -07:00
|
|
|
MOZ_STATIC_ASSERT(nsITelemetry::HISTOGRAM_ ## histogram_type == nsITelemetry::HISTOGRAM_BOOLEAN || \
|
|
|
|
nsITelemetry::HISTOGRAM_ ## histogram_type == nsITelemetry::HISTOGRAM_FLAG || \
|
|
|
|
(min < max && bucket_count > 2 && min >= 1), \
|
|
|
|
"Incorrect histogram definitions were found");
|
2011-12-16 11:30:44 -08:00
|
|
|
|
|
|
|
#include "TelemetryHistograms.h"
|
|
|
|
|
|
|
|
#undef HISTOGRAM
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
const TelemetryHistogram gHistograms[] = {
|
2012-01-06 11:40:45 -08:00
|
|
|
#define HISTOGRAM(id, min, max, bucket_count, histogram_type, comment) \
|
2012-01-09 09:47:35 -08:00
|
|
|
{ NS_STRINGIFY(id), min, max, bucket_count, \
|
2012-01-06 11:40:45 -08:00
|
|
|
nsITelemetry::HISTOGRAM_ ## histogram_type, comment },
|
2011-06-20 14:47:58 -07:00
|
|
|
|
|
|
|
#include "TelemetryHistograms.h"
|
|
|
|
|
|
|
|
#undef HISTOGRAM
|
|
|
|
};
|
2012-01-06 14:13:02 -08:00
|
|
|
bool gCorruptHistograms[Telemetry::HistogramCount];
|
2011-06-20 14:47:58 -07:00
|
|
|
|
2012-01-06 11:40:04 -08:00
|
|
|
bool
|
2012-05-16 04:39:21 -07:00
|
|
|
TelemetryHistogramType(Histogram *h, uint32_t *result)
|
2012-01-06 11:40:04 -08:00
|
|
|
{
|
|
|
|
switch (h->histogram_type()) {
|
|
|
|
case Histogram::HISTOGRAM:
|
|
|
|
*result = nsITelemetry::HISTOGRAM_EXPONENTIAL;
|
|
|
|
break;
|
|
|
|
case Histogram::LINEAR_HISTOGRAM:
|
|
|
|
*result = nsITelemetry::HISTOGRAM_LINEAR;
|
|
|
|
break;
|
|
|
|
case Histogram::BOOLEAN_HISTOGRAM:
|
|
|
|
*result = nsITelemetry::HISTOGRAM_BOOLEAN;
|
|
|
|
break;
|
2012-03-02 06:59:38 -08:00
|
|
|
case Histogram::FLAG_HISTOGRAM:
|
|
|
|
*result = nsITelemetry::HISTOGRAM_FLAG;
|
2012-04-26 01:09:30 -07:00
|
|
|
break;
|
2012-01-06 11:40:04 -08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
nsresult
|
2012-05-16 04:39:21 -07:00
|
|
|
HistogramGet(const char *name, uint32_t min, uint32_t max, uint32_t bucketCount,
|
|
|
|
uint32_t histogramType, Histogram **result)
|
2011-06-20 14:47:58 -07:00
|
|
|
{
|
2012-03-02 06:59:38 -08:00
|
|
|
if (histogramType != nsITelemetry::HISTOGRAM_BOOLEAN
|
|
|
|
&& histogramType != nsITelemetry::HISTOGRAM_FLAG) {
|
2011-06-20 14:47:58 -07:00
|
|
|
// Sanity checks for histogram parameters.
|
|
|
|
if (min >= max)
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
if (bucketCount <= 2)
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
if (min < 1)
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (histogramType) {
|
|
|
|
case nsITelemetry::HISTOGRAM_EXPONENTIAL:
|
2011-06-30 14:58:00 -07:00
|
|
|
*result = Histogram::FactoryGet(name, min, max, bucketCount, Histogram::kUmaTargetedHistogramFlag);
|
2011-06-20 14:47:58 -07:00
|
|
|
break;
|
|
|
|
case nsITelemetry::HISTOGRAM_LINEAR:
|
2011-06-30 14:58:00 -07:00
|
|
|
*result = LinearHistogram::FactoryGet(name, min, max, bucketCount, Histogram::kUmaTargetedHistogramFlag);
|
2011-06-20 14:47:58 -07:00
|
|
|
break;
|
|
|
|
case nsITelemetry::HISTOGRAM_BOOLEAN:
|
2011-06-30 14:58:00 -07:00
|
|
|
*result = BooleanHistogram::FactoryGet(name, Histogram::kUmaTargetedHistogramFlag);
|
2011-06-20 14:47:58 -07:00
|
|
|
break;
|
2012-03-02 06:59:38 -08:00
|
|
|
case nsITelemetry::HISTOGRAM_FLAG:
|
|
|
|
*result = FlagHistogram::FactoryGet(name, Histogram::kUmaTargetedHistogramFlag);
|
|
|
|
break;
|
2011-06-20 14:47:58 -07:00
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// O(1) histogram lookup by numeric id
|
|
|
|
nsresult
|
|
|
|
GetHistogramByEnumId(Telemetry::ID id, Histogram **ret)
|
|
|
|
{
|
|
|
|
static Histogram* knownHistograms[Telemetry::HistogramCount] = {0};
|
|
|
|
Histogram *h = knownHistograms[id];
|
|
|
|
if (h) {
|
|
|
|
*ret = h;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TelemetryHistogram &p = gHistograms[id];
|
2011-06-20 14:48:00 -07:00
|
|
|
nsresult rv = HistogramGet(p.id, p.min, p.max, p.bucketCount, p.histogramType, &h);
|
2011-06-20 14:47:58 -07:00
|
|
|
if (NS_FAILED(rv))
|
2011-08-05 06:53:48 -07:00
|
|
|
return rv;
|
2011-06-20 14:47:58 -07:00
|
|
|
|
|
|
|
*ret = knownHistograms[id] = h;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
bool
|
2011-04-27 11:07:02 -07:00
|
|
|
FillRanges(JSContext *cx, JSObject *array, Histogram *h)
|
|
|
|
{
|
2011-06-20 14:47:58 -07:00
|
|
|
for (size_t i = 0; i < h->bucket_count(); i++) {
|
2011-04-27 11:07:02 -07:00
|
|
|
if (!JS_DefineElement(cx, array, i, INT_TO_JSVAL(h->ranges(i)), NULL, NULL, JSPROP_ENUMERATE))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:13:02 -08:00
|
|
|
enum reflectStatus {
|
|
|
|
REFLECT_OK,
|
|
|
|
REFLECT_CORRUPT,
|
|
|
|
REFLECT_FAILURE
|
|
|
|
};
|
|
|
|
|
|
|
|
enum reflectStatus
|
2011-12-13 09:03:30 -08:00
|
|
|
ReflectHistogramAndSamples(JSContext *cx, JSObject *obj, Histogram *h,
|
|
|
|
const Histogram::SampleSet &ss)
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
2012-01-06 14:13:02 -08:00
|
|
|
// We don't want to reflect corrupt histograms.
|
|
|
|
if (h->FindCorruption(ss) != Histogram::NO_INCONSISTENCIES) {
|
|
|
|
return REFLECT_CORRUPT;
|
|
|
|
}
|
|
|
|
|
2012-03-01 20:36:59 -08:00
|
|
|
if (!(JS_DefineProperty(cx, obj, "min", INT_TO_JSVAL(h->declared_min()), NULL, NULL, JSPROP_ENUMERATE)
|
|
|
|
&& JS_DefineProperty(cx, obj, "max", INT_TO_JSVAL(h->declared_max()), NULL, NULL, JSPROP_ENUMERATE)
|
|
|
|
&& JS_DefineProperty(cx, obj, "histogram_type", INT_TO_JSVAL(h->histogram_type()), NULL, NULL, JSPROP_ENUMERATE)
|
2012-02-15 14:01:53 -08:00
|
|
|
&& JS_DefineProperty(cx, obj, "sum", DOUBLE_TO_JSVAL(ss.sum()), NULL, NULL, JSPROP_ENUMERATE))) {
|
|
|
|
return REFLECT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t count = h->bucket_count();
|
|
|
|
JSObject *rarray = JS_NewArrayObject(cx, count, nsnull);
|
|
|
|
if (!rarray) {
|
|
|
|
return REFLECT_FAILURE;
|
|
|
|
}
|
|
|
|
JS::AutoObjectRooter aroot(cx, rarray);
|
|
|
|
if (!(FillRanges(cx, rarray, h)
|
|
|
|
&& JS_DefineProperty(cx, obj, "ranges", OBJECT_TO_JSVAL(rarray),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE))) {
|
|
|
|
return REFLECT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *counts_array = JS_NewArrayObject(cx, count, NULL);
|
|
|
|
if (!counts_array) {
|
|
|
|
return REFLECT_FAILURE;
|
|
|
|
}
|
|
|
|
JS::AutoObjectRooter croot(cx, counts_array);
|
|
|
|
if (!JS_DefineProperty(cx, obj, "counts", OBJECT_TO_JSVAL(counts_array),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
2012-01-06 14:13:02 -08:00
|
|
|
return REFLECT_FAILURE;
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
2011-06-20 14:47:58 -07:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2012-02-15 14:01:53 -08:00
|
|
|
if (!JS_DefineElement(cx, counts_array, i, INT_TO_JSVAL(ss.counts(i)),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE)) {
|
2012-01-06 14:13:02 -08:00
|
|
|
return REFLECT_FAILURE;
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
|
|
|
}
|
2012-02-15 14:01:53 -08:00
|
|
|
|
2012-01-06 14:13:02 -08:00
|
|
|
return REFLECT_OK;
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
|
|
|
|
2011-12-13 09:03:30 -08:00
|
|
|
enum reflectStatus
|
|
|
|
ReflectHistogramSnapshot(JSContext *cx, JSObject *obj, Histogram *h)
|
|
|
|
{
|
|
|
|
Histogram::SampleSet ss;
|
|
|
|
h->SnapshotSample(&ss);
|
|
|
|
return ReflectHistogramAndSamples(cx, obj, h, ss);
|
|
|
|
}
|
|
|
|
|
2012-05-08 07:58:20 -07:00
|
|
|
bool
|
|
|
|
IsEmpty(const Histogram *h)
|
|
|
|
{
|
|
|
|
Histogram::SampleSet ss;
|
|
|
|
h->SnapshotSample(&ss);
|
|
|
|
|
|
|
|
return ss.counts(0) == 0 && ss.sum() == 0;
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
JSHistogram_Add(JSContext *cx, unsigned argc, jsval *vp)
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
2011-06-28 16:54:33 -07:00
|
|
|
if (!argc) {
|
|
|
|
JS_ReportError(cx, "Expected one argument");
|
2011-04-27 11:07:02 -07:00
|
|
|
return JS_FALSE;
|
2011-06-28 16:54:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
jsval v = JS_ARGV(cx, vp)[0];
|
|
|
|
|
|
|
|
if (!(JSVAL_IS_NUMBER(v) || JSVAL_IS_BOOLEAN(v))) {
|
|
|
|
JS_ReportError(cx, "Not a number");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-06-06 00:22:24 -07:00
|
|
|
int32_t value;
|
2011-06-28 16:54:33 -07:00
|
|
|
if (!JS_ValueToECMAInt32(cx, v, &value)) {
|
2011-04-27 11:07:02 -07:00
|
|
|
return JS_FALSE;
|
2011-06-28 16:54:33 -07:00
|
|
|
}
|
|
|
|
|
2011-06-28 16:57:44 -07:00
|
|
|
if (TelemetryImpl::CanRecord()) {
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
Histogram *h = static_cast<Histogram*>(JS_GetPrivate(obj));
|
2011-06-28 16:57:44 -07:00
|
|
|
if (h->histogram_type() == Histogram::BOOLEAN_HISTOGRAM)
|
|
|
|
h->Add(!!value);
|
|
|
|
else
|
|
|
|
h->Add(value);
|
|
|
|
}
|
2011-04-27 11:07:02 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
JSHistogram_Snapshot(JSContext *cx, unsigned argc, jsval *vp)
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
Histogram *h = static_cast<Histogram*>(JS_GetPrivate(obj));
|
2012-02-15 14:01:53 -08:00
|
|
|
JSObject *snapshot = JS_NewObject(cx, nsnull, nsnull, nsnull);
|
2011-04-27 11:07:02 -07:00
|
|
|
if (!snapshot)
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-25 21:57:58 -07:00
|
|
|
return JS_FALSE;
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter sroot(cx, snapshot);
|
2012-01-06 14:13:02 -08:00
|
|
|
|
|
|
|
switch (ReflectHistogramSnapshot(cx, snapshot, h)) {
|
|
|
|
case REFLECT_FAILURE:
|
|
|
|
return JS_FALSE;
|
|
|
|
case REFLECT_CORRUPT:
|
|
|
|
JS_ReportError(cx, "Histogram is corrupt");
|
|
|
|
return JS_FALSE;
|
|
|
|
case REFLECT_OK:
|
|
|
|
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(snapshot));
|
|
|
|
return JS_TRUE;
|
|
|
|
default:
|
|
|
|
MOZ_NOT_REACHED("unhandled reflection status");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
|
|
|
|
2012-05-08 12:39:24 -07:00
|
|
|
JSBool
|
|
|
|
JSHistogram_Clear(JSContext *cx, unsigned argc, jsval *vp)
|
|
|
|
{
|
|
|
|
JSObject *obj = JS_THIS_OBJECT(cx, vp);
|
|
|
|
if (!obj) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Histogram *h = static_cast<Histogram*>(JS_GetPrivate(obj));
|
|
|
|
h->Clear();
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
nsresult
|
2011-04-27 11:07:02 -07:00
|
|
|
WrapAndReturnHistogram(Histogram *h, JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
static JSClass JSHistogram_class = {
|
|
|
|
"JSHistogram", /* name */
|
|
|
|
JSCLASS_HAS_PRIVATE, /* flags */
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub
|
2011-04-27 11:07:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSObject *obj = JS_NewObject(cx, &JSHistogram_class, NULL, NULL);
|
|
|
|
if (!obj)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter root(cx, obj);
|
2012-05-08 12:39:24 -07:00
|
|
|
if (!(JS_DefineFunction(cx, obj, "add", JSHistogram_Add, 1, 0)
|
|
|
|
&& JS_DefineFunction(cx, obj, "snapshot", JSHistogram_Snapshot, 0, 0)
|
|
|
|
&& JS_DefineFunction(cx, obj, "clear", JSHistogram_Clear, 0, 0))) {
|
2012-02-15 14:01:53 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-04-27 11:07:02 -07:00
|
|
|
*ret = OBJECT_TO_JSVAL(obj);
|
2012-02-05 12:07:23 -08:00
|
|
|
JS_SetPrivate(obj, h);
|
2012-02-15 14:01:53 -08:00
|
|
|
return NS_OK;
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
2011-06-20 14:47:58 -07:00
|
|
|
|
2011-06-28 16:57:44 -07:00
|
|
|
TelemetryImpl::TelemetryImpl():
|
2012-02-13 11:47:40 -08:00
|
|
|
mHistogramMap(Telemetry::HistogramCount),
|
2011-12-06 12:12:55 -08:00
|
|
|
mCanRecord(XRE_GetProcessType() == GeckoProcessType_Default),
|
2012-03-12 04:07:05 -07:00
|
|
|
mHashMutex("Telemetry::mHashMutex"),
|
|
|
|
mHangReportsMutex("Telemetry::mHangReportsMutex")
|
2011-06-28 16:57:44 -07:00
|
|
|
{
|
2011-12-06 12:12:55 -08:00
|
|
|
// A whitelist to prevent Telemetry reporting on Addon & Thunderbird DBs
|
|
|
|
const char *trackedDBs[] = {
|
|
|
|
"addons.sqlite", "chromeappsstore.sqlite", "content-prefs.sqlite",
|
|
|
|
"cookies.sqlite", "downloads.sqlite", "extensions.sqlite",
|
|
|
|
"formhistory.sqlite", "index.sqlite", "permissions.sqlite", "places.sqlite",
|
|
|
|
"search.sqlite", "signons.sqlite", "urlclassifier3.sqlite",
|
|
|
|
"webappsstore.sqlite"
|
|
|
|
};
|
|
|
|
|
|
|
|
mTrackedDBs.Init();
|
2012-01-09 10:01:52 -08:00
|
|
|
for (size_t i = 0; i < ArrayLength(trackedDBs); i++)
|
2011-12-06 12:12:55 -08:00
|
|
|
mTrackedDBs.PutEntry(nsDependentCString(trackedDBs[i]));
|
|
|
|
|
2012-01-10 12:31:34 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Mark immutable to prevent asserts on simultaneous access from multiple threads
|
|
|
|
mTrackedDBs.MarkImmutable();
|
|
|
|
#endif
|
2011-06-20 14:48:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TelemetryImpl::~TelemetryImpl() {
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:07:02 -07:00
|
|
|
NS_IMETHODIMP
|
2012-05-16 04:39:21 -07:00
|
|
|
TelemetryImpl::NewHistogram(const nsACString &name, uint32_t min, uint32_t max, uint32_t bucketCount, uint32_t histogramType, JSContext *cx, jsval *ret)
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
2011-05-19 19:33:05 -07:00
|
|
|
Histogram *h;
|
2011-06-20 14:47:58 -07:00
|
|
|
nsresult rv = HistogramGet(PromiseFlatCString(name).get(), min, max, bucketCount, histogramType, &h);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-06-30 14:58:00 -07:00
|
|
|
h->ClearFlags(Histogram::kUmaTargetedHistogramFlag);
|
2011-04-27 11:07:02 -07:00
|
|
|
return WrapAndReturnHistogram(h, cx, ret);
|
|
|
|
}
|
|
|
|
|
2012-02-13 11:47:40 -08:00
|
|
|
bool
|
2012-03-21 10:26:48 -07:00
|
|
|
TelemetryImpl::ReflectSql(SlowSQLEntryType *entry, JSContext *cx, JSObject *obj)
|
2011-12-06 12:12:55 -08:00
|
|
|
{
|
2011-12-14 12:04:25 -08:00
|
|
|
const nsACString &sql = entry->GetKey();
|
2011-12-06 12:12:55 -08:00
|
|
|
jsval hitCount = UINT_TO_JSVAL(entry->mData.hitCount);
|
|
|
|
jsval totalTime = UINT_TO_JSVAL(entry->mData.totalTime);
|
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
JSObject *arrayObj = JS_NewArrayObject(cx, 0, nsnull);
|
2012-02-15 14:01:53 -08:00
|
|
|
if (!arrayObj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JS::AutoObjectRooter root(cx, arrayObj);
|
|
|
|
return (JS_SetElement(cx, arrayObj, 0, &hitCount)
|
2012-02-13 11:47:40 -08:00
|
|
|
&& JS_SetElement(cx, arrayObj, 1, &totalTime)
|
|
|
|
&& JS_DefineProperty(cx, obj,
|
|
|
|
sql.BeginReading(),
|
|
|
|
OBJECT_TO_JSVAL(arrayObj),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE));
|
2011-12-06 12:12:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-03-21 10:26:48 -07:00
|
|
|
TelemetryImpl::ReflectPublicSql(SlowSQLEntryType *entry, JSContext *cx,
|
|
|
|
JSObject *obj)
|
|
|
|
{
|
|
|
|
bool isPrivateSql = entry->mData.isDynamicSql || (!entry->mData.isTrackedDb);
|
|
|
|
if (!isPrivateSql || entry->mData.isAggregate)
|
|
|
|
return ReflectSql(entry, cx, obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelemetryImpl::ReflectPrivateSql(SlowSQLEntryType *entry, JSContext *cx,
|
|
|
|
JSObject *obj)
|
|
|
|
{
|
|
|
|
if (!entry->mData.isAggregate)
|
|
|
|
return ReflectSql(entry, cx, obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelemetryImpl::AddSQLInfo(JSContext *cx, JSObject *rootObj, bool mainThread,
|
|
|
|
bool includePrivateStrings)
|
2011-12-06 12:12:55 -08:00
|
|
|
{
|
|
|
|
JSObject *statsObj = JS_NewObject(cx, NULL, NULL, NULL);
|
2011-12-14 12:04:25 -08:00
|
|
|
if (!statsObj)
|
2011-12-06 12:12:55 -08:00
|
|
|
return false;
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter root(cx, statsObj);
|
2011-12-06 12:12:55 -08:00
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
AutoHashtable<SlowSQLEntryType> &sqlMap =
|
|
|
|
(mainThread ? mSlowSQLOnMainThread : mSlowSQLOnOtherThread);
|
|
|
|
AutoHashtable<SlowSQLEntryType>::ReflectEntryFunc reflectFunction =
|
|
|
|
(includePrivateStrings ? ReflectPrivateSql : ReflectPublicSql);
|
|
|
|
if(!sqlMap.ReflectHashtable(reflectFunction, cx, statsObj)) {
|
2012-02-15 14:01:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_DefineProperty(cx, rootObj,
|
|
|
|
mainThread ? "mainThread" : "otherThreads",
|
|
|
|
OBJECT_TO_JSVAL(statsObj),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
2011-12-06 12:12:55 -08:00
|
|
|
}
|
|
|
|
|
2012-01-06 11:39:29 -08:00
|
|
|
nsresult
|
2012-01-09 12:42:34 -08:00
|
|
|
TelemetryImpl::GetHistogramEnumId(const char *name, Telemetry::ID *id)
|
2012-01-06 11:39:29 -08:00
|
|
|
{
|
2012-01-09 12:42:34 -08:00
|
|
|
if (!sTelemetry) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-01-06 11:39:29 -08:00
|
|
|
// Cache names
|
|
|
|
// Note the histogram names are statically allocated
|
2012-01-09 12:42:34 -08:00
|
|
|
TelemetryImpl::HistogramMapType *map = &sTelemetry->mHistogramMap;
|
|
|
|
if (!map->Count()) {
|
2012-05-16 04:39:21 -07:00
|
|
|
for (uint32_t i = 0; i < Telemetry::HistogramCount; i++) {
|
2012-01-09 12:42:34 -08:00
|
|
|
CharPtrEntryType *entry = map->PutEntry(gHistograms[i].id);
|
2012-01-06 11:39:29 -08:00
|
|
|
if (NS_UNLIKELY(!entry)) {
|
2012-01-09 12:42:34 -08:00
|
|
|
map->Clear();
|
2012-01-06 11:39:29 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
entry->mData = (Telemetry::ID) i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 12:42:34 -08:00
|
|
|
CharPtrEntryType *entry = map->GetEntry(name);
|
|
|
|
if (!entry) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
*id = entry->mData;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
TelemetryImpl::GetHistogramByName(const nsACString &name, Histogram **ret)
|
|
|
|
{
|
|
|
|
Telemetry::ID id;
|
|
|
|
nsresult rv = GetHistogramEnumId(PromiseFlatCString(name).get(), &id);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-01-06 11:39:29 -08:00
|
|
|
|
2012-01-09 12:42:34 -08:00
|
|
|
rv = GetHistogramByEnumId(id, ret);
|
2012-01-06 11:39:29 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-06 11:40:04 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::HistogramFrom(const nsACString &name, const nsACString &existing_name,
|
|
|
|
JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
Histogram *existing;
|
|
|
|
nsresult rv = GetHistogramByName(existing_name, &existing);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t histogramType;
|
2012-01-06 11:40:04 -08:00
|
|
|
bool success = TelemetryHistogramType(existing, &histogramType);
|
|
|
|
if (!success)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
Histogram *clone;
|
|
|
|
rv = HistogramGet(PromiseFlatCString(name).get(), existing->declared_min(),
|
|
|
|
existing->declared_max(), existing->bucket_count(),
|
|
|
|
histogramType, &clone);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
Histogram::SampleSet ss;
|
|
|
|
existing->SnapshotSample(&ss);
|
|
|
|
clone->AddSampleSet(ss);
|
|
|
|
return WrapAndReturnHistogram(clone, cx, ret);
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:13:02 -08:00
|
|
|
void
|
|
|
|
TelemetryImpl::IdentifyCorruptHistograms(StatisticsRecorder::Histograms &hs)
|
|
|
|
{
|
|
|
|
for (HistogramIterator it = hs.begin(); it != hs.end(); ++it) {
|
|
|
|
Histogram *h = *it;
|
|
|
|
|
|
|
|
Telemetry::ID id;
|
|
|
|
nsresult rv = GetHistogramEnumId(h->histogram_name().c_str(), &id);
|
|
|
|
// This histogram isn't a static histogram, just ignore it.
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gCorruptHistograms[id]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Histogram::SampleSet ss;
|
|
|
|
h->SnapshotSample(&ss);
|
|
|
|
Histogram::Inconsistencies check = h->FindCorruption(ss);
|
|
|
|
bool corrupt = (check != Histogram::NO_INCONSISTENCIES);
|
|
|
|
|
|
|
|
if (corrupt) {
|
|
|
|
Telemetry::ID corruptID = Telemetry::HistogramCount;
|
|
|
|
if (check & Histogram::RANGE_CHECKSUM_ERROR) {
|
|
|
|
corruptID = Telemetry::RANGE_CHECKSUM_ERRORS;
|
|
|
|
} else if (check & Histogram::BUCKET_ORDER_ERROR) {
|
|
|
|
corruptID = Telemetry::BUCKET_ORDER_ERRORS;
|
|
|
|
} else if (check & Histogram::COUNT_HIGH_ERROR) {
|
|
|
|
corruptID = Telemetry::TOTAL_COUNT_HIGH_ERRORS;
|
|
|
|
} else if (check & Histogram::COUNT_LOW_ERROR) {
|
|
|
|
corruptID = Telemetry::TOTAL_COUNT_LOW_ERRORS;
|
|
|
|
}
|
|
|
|
Telemetry::Accumulate(corruptID, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gCorruptHistograms[id] = corrupt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelemetryImpl::ShouldReflectHistogram(Histogram *h)
|
|
|
|
{
|
|
|
|
const char *name = h->histogram_name().c_str();
|
|
|
|
Telemetry::ID id;
|
|
|
|
nsresult rv = GetHistogramEnumId(name, &id);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// GetHistogramEnumId generally should not fail. But a lookup
|
|
|
|
// failure shouldn't prevent us from reflecting histograms into JS.
|
|
|
|
//
|
|
|
|
// However, these two histograms are created by Histogram itself for
|
|
|
|
// tracking corruption. We have our own histograms for that, so
|
|
|
|
// ignore these two.
|
|
|
|
if (strcmp(name, "Histogram.InconsistentCountHigh") == 0
|
|
|
|
|| strcmp(name, "Histogram.InconsistentCountLow") == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return !gCorruptHistograms[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:56:48 -08:00
|
|
|
// Compute the name to pass into Histogram for the addon histogram
|
|
|
|
// 'name' from the addon 'id'. We can't use 'name' directly because it
|
|
|
|
// might conflict with other histograms in other addons or even with our
|
|
|
|
// own.
|
|
|
|
void
|
|
|
|
AddonHistogramName(const nsACString &id, const nsACString &name,
|
|
|
|
nsACString &ret)
|
|
|
|
{
|
|
|
|
ret.Append(id);
|
|
|
|
ret.Append(NS_LITERAL_CSTRING(":"));
|
|
|
|
ret.Append(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::RegisterAddonHistogram(const nsACString &id,
|
|
|
|
const nsACString &name,
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t min, uint32_t max,
|
|
|
|
uint32_t bucketCount,
|
|
|
|
uint32_t histogramType)
|
2012-01-20 13:56:48 -08:00
|
|
|
{
|
|
|
|
AddonEntryType *addonEntry = mAddonMap.GetEntry(id);
|
|
|
|
if (!addonEntry) {
|
|
|
|
addonEntry = mAddonMap.PutEntry(id);
|
|
|
|
if (NS_UNLIKELY(!addonEntry)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
addonEntry->mData = new AddonHistogramMapType();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonHistogramMapType *histogramMap = addonEntry->mData;
|
|
|
|
AddonHistogramEntryType *histogramEntry = histogramMap->GetEntry(name);
|
|
|
|
// Can't re-register the same histogram.
|
|
|
|
if (histogramEntry) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
histogramEntry = histogramMap->PutEntry(name);
|
|
|
|
if (NS_UNLIKELY(!histogramEntry)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonHistogramInfo &info = histogramEntry->mData;
|
|
|
|
info.min = min;
|
|
|
|
info.max = max;
|
|
|
|
info.bucketCount = bucketCount;
|
|
|
|
info.histogramType = histogramType;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetAddonHistogram(const nsACString &id, const nsACString &name,
|
|
|
|
JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
AddonEntryType *addonEntry = mAddonMap.GetEntry(id);
|
|
|
|
// The given id has not been registered.
|
|
|
|
if (!addonEntry) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonHistogramMapType *histogramMap = addonEntry->mData;
|
|
|
|
AddonHistogramEntryType *histogramEntry = histogramMap->GetEntry(name);
|
|
|
|
// The given histogram name has not been registered.
|
|
|
|
if (!histogramEntry) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddonHistogramInfo &info = histogramEntry->mData;
|
2012-03-02 06:59:38 -08:00
|
|
|
if (!info.h) {
|
2012-01-20 13:56:48 -08:00
|
|
|
nsCAutoString actualName;
|
|
|
|
AddonHistogramName(id, name, actualName);
|
2012-03-02 06:59:38 -08:00
|
|
|
if (!CreateHistogramForAddon(actualName, info)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-01-20 13:56:48 -08:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 06:59:38 -08:00
|
|
|
return WrapAndReturnHistogram(info.h, cx, ret);
|
2012-01-20 13:56:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::UnregisterAddonHistograms(const nsACString &id)
|
|
|
|
{
|
|
|
|
AddonEntryType *addonEntry = mAddonMap.GetEntry(id);
|
|
|
|
if (addonEntry) {
|
|
|
|
// Histogram's destructor is private, so this is the best we can do.
|
|
|
|
// The histograms the addon created *will* stick around, but they
|
|
|
|
// will be deleted if and when the addon registers histograms with
|
|
|
|
// the same names.
|
|
|
|
delete addonEntry->mData;
|
|
|
|
mAddonMap.RemoveEntry(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:07:02 -07:00
|
|
|
NS_IMETHODIMP
|
2011-06-20 14:47:58 -07:00
|
|
|
TelemetryImpl::GetHistogramSnapshots(JSContext *cx, jsval *ret)
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
|
|
|
JSObject *root_obj = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!root_obj)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
*ret = OBJECT_TO_JSVAL(root_obj);
|
|
|
|
|
2012-03-02 06:59:38 -08:00
|
|
|
// Ensure that all the HISTOGRAM_FLAG histograms have been created, so
|
|
|
|
// that their values are snapshotted.
|
|
|
|
for (size_t i = 0; i < Telemetry::HistogramCount; ++i) {
|
|
|
|
if (gHistograms[i].histogramType == nsITelemetry::HISTOGRAM_FLAG) {
|
|
|
|
Histogram *h;
|
|
|
|
DebugOnly<nsresult> rv = GetHistogramByEnumId(Telemetry::ID(i), &h);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-06 14:13:02 -08:00
|
|
|
StatisticsRecorder::Histograms hs;
|
|
|
|
StatisticsRecorder::GetHistograms(&hs);
|
|
|
|
|
|
|
|
// We identify corrupt histograms first, rather than interspersing it
|
|
|
|
// in the loop below, to ensure that our corruption statistics don't
|
|
|
|
// depend on histogram enumeration order.
|
|
|
|
//
|
|
|
|
// Of course, we hope that all of these corruption-statistics
|
|
|
|
// histograms are not themselves corrupt...
|
|
|
|
IdentifyCorruptHistograms(hs);
|
|
|
|
|
|
|
|
// OK, now we can actually reflect things.
|
|
|
|
for (HistogramIterator it = hs.begin(); it != hs.end(); ++it) {
|
2011-04-27 11:07:02 -07:00
|
|
|
Histogram *h = *it;
|
2012-05-08 07:58:20 -07:00
|
|
|
if (!ShouldReflectHistogram(h) || IsEmpty(h)) {
|
2012-01-06 14:13:02 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:07:02 -07:00
|
|
|
JSObject *hobj = JS_NewObject(cx, NULL, NULL, NULL);
|
2012-01-06 14:13:02 -08:00
|
|
|
if (!hobj) {
|
2011-04-27 11:07:02 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter root(cx, hobj);
|
2012-01-06 14:13:02 -08:00
|
|
|
switch (ReflectHistogramSnapshot(cx, hobj, h)) {
|
|
|
|
case REFLECT_CORRUPT:
|
|
|
|
// We can still hit this case even if ShouldReflectHistograms
|
|
|
|
// returns true. The histogram lies outside of our control
|
|
|
|
// somehow; just skip it.
|
|
|
|
continue;
|
|
|
|
case REFLECT_FAILURE:
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
case REFLECT_OK:
|
|
|
|
if (!JS_DefineProperty(cx, root_obj, h->histogram_name().c_str(),
|
|
|
|
OBJECT_TO_JSVAL(hobj), NULL, NULL, JSPROP_ENUMERATE)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
2011-12-14 12:04:25 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-02 06:59:38 -08:00
|
|
|
bool
|
|
|
|
TelemetryImpl::CreateHistogramForAddon(const nsACString &name,
|
|
|
|
AddonHistogramInfo &info)
|
|
|
|
{
|
|
|
|
Histogram *h;
|
|
|
|
nsresult rv = HistogramGet(PromiseFlatCString(name).get(),
|
|
|
|
info.min, info.max, info.bucketCount,
|
|
|
|
info.histogramType, &h);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Don't let this histogram be reported via the normal means
|
|
|
|
// (e.g. Telemetry.registeredHistograms); we'll make it available in
|
|
|
|
// other ways.
|
|
|
|
h->ClearFlags(Histogram::kUmaTargetedHistogramFlag);
|
|
|
|
info.h = h;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:56:48 -08:00
|
|
|
bool
|
|
|
|
TelemetryImpl::AddonHistogramReflector(AddonHistogramEntryType *entry,
|
|
|
|
JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2012-03-02 06:59:38 -08:00
|
|
|
AddonHistogramInfo &info = entry->mData;
|
|
|
|
|
2012-01-20 13:56:48 -08:00
|
|
|
// Never even accessed the histogram.
|
2012-03-02 06:59:38 -08:00
|
|
|
if (!info.h) {
|
|
|
|
// Have to force creation of HISTOGRAM_FLAG histograms.
|
|
|
|
if (info.histogramType != nsITelemetry::HISTOGRAM_FLAG)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!CreateHistogramForAddon(entry->GetKey(), info)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-20 13:56:48 -08:00
|
|
|
}
|
|
|
|
|
2012-05-08 07:58:20 -07:00
|
|
|
if (IsEmpty(info.h)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:56:48 -08:00
|
|
|
JSObject *snapshot = JS_NewObject(cx, NULL, NULL, NULL);
|
2012-02-15 14:01:53 -08:00
|
|
|
if (!snapshot) {
|
|
|
|
// Just consider this to be skippable.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
JS::AutoObjectRooter r(cx, snapshot);
|
2012-03-02 06:59:38 -08:00
|
|
|
switch (ReflectHistogramSnapshot(cx, snapshot, info.h)) {
|
2012-01-20 13:56:48 -08:00
|
|
|
case REFLECT_FAILURE:
|
|
|
|
case REFLECT_CORRUPT:
|
|
|
|
return false;
|
|
|
|
case REFLECT_OK:
|
|
|
|
const nsACString &histogramName = entry->GetKey();
|
|
|
|
if (!JS_DefineProperty(cx, obj,
|
|
|
|
PromiseFlatCString(histogramName).get(),
|
|
|
|
OBJECT_TO_JSVAL(snapshot), NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelemetryImpl::AddonReflector(AddonEntryType *entry,
|
|
|
|
JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
const nsACString &addonId = entry->GetKey();
|
|
|
|
JSObject *subobj = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!subobj) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter r(cx, subobj);
|
2012-01-20 13:56:48 -08:00
|
|
|
|
|
|
|
AddonHistogramMapType *map = entry->mData;
|
|
|
|
if (!(map->ReflectHashtable(AddonHistogramReflector, cx, subobj)
|
|
|
|
&& JS_DefineProperty(cx, obj,
|
|
|
|
PromiseFlatCString(addonId).get(),
|
|
|
|
OBJECT_TO_JSVAL(subobj), NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetAddonHistogramSnapshots(JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
*ret = JSVAL_VOID;
|
|
|
|
JSObject *obj = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter r(cx, obj);
|
2012-01-20 13:56:48 -08:00
|
|
|
|
|
|
|
if (!mAddonMap.ReflectHashtable(AddonReflector, cx, obj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*ret = OBJECT_TO_JSVAL(obj);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
bool
|
|
|
|
TelemetryImpl::GetSQLStats(JSContext *cx, jsval *ret, bool includePrivateSql)
|
2011-12-14 12:04:25 -08:00
|
|
|
{
|
|
|
|
JSObject *root_obj = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!root_obj)
|
2012-03-21 10:26:48 -07:00
|
|
|
return false;
|
2011-12-14 12:04:25 -08:00
|
|
|
*ret = OBJECT_TO_JSVAL(root_obj);
|
2011-12-06 12:12:55 -08:00
|
|
|
|
|
|
|
MutexAutoLock hashMutex(mHashMutex);
|
|
|
|
// Add info about slow SQL queries on the main thread
|
2012-03-21 10:26:48 -07:00
|
|
|
if (!AddSQLInfo(cx, root_obj, true, includePrivateSql))
|
|
|
|
return false;
|
2011-12-06 12:12:55 -08:00
|
|
|
// Add info about slow SQL queries on other threads
|
2012-03-21 10:26:48 -07:00
|
|
|
if (!AddSQLInfo(cx, root_obj, false, includePrivateSql))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-06 12:12:55 -08:00
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetSlowSQL(JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
if (GetSQLStats(cx, ret, false))
|
|
|
|
return NS_OK;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetDebugSlowSQL(JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
bool revealPrivateSql =
|
|
|
|
Preferences::GetBool("toolkit.telemetry.debugSlowSql", false);
|
|
|
|
if (GetSQLStats(cx, ret, revealPrivateSql))
|
|
|
|
return NS_OK;
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
|
|
|
|
2012-03-12 04:07:05 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetChromeHangs(JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
MutexAutoLock hangReportMutex(mHangReportsMutex);
|
|
|
|
JSObject *reportArray = JS_NewArrayObject(cx, 0, nsnull);
|
|
|
|
if (!reportArray) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
*ret = OBJECT_TO_JSVAL(reportArray);
|
|
|
|
|
|
|
|
// Each hang report is an object in the 'chromeHangs' array
|
|
|
|
for (size_t i = 0; i < mHangReports.Length(); ++i) {
|
|
|
|
JSObject *reportObj = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!reportObj) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
jsval reportObjVal = OBJECT_TO_JSVAL(reportObj);
|
|
|
|
if (!JS_SetElement(cx, reportArray, i, &reportObjVal)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the hang duration (expressed in seconds)
|
|
|
|
JSBool ok = JS_DefineProperty(cx, reportObj, "duration",
|
|
|
|
INT_TO_JSVAL(mHangReports[i].duration),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
|
|
|
if (!ok) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represent call stack PCs as strings
|
|
|
|
// (JS can't represent all 64-bit integer values)
|
|
|
|
JSObject *pcArray = JS_NewArrayObject(cx, 0, nsnull);
|
|
|
|
if (!pcArray) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
ok = JS_DefineProperty(cx, reportObj, "stack", OBJECT_TO_JSVAL(pcArray),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
|
|
|
if (!ok) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-05-16 04:39:21 -07:00
|
|
|
const uint32_t pcCount = mHangReports[i].callStack.Length();
|
2012-03-12 04:07:05 -07:00
|
|
|
for (size_t pcIndex = 0; pcIndex < pcCount; ++pcIndex) {
|
|
|
|
nsCAutoString pcString;
|
|
|
|
pcString.AppendPrintf("0x%p", mHangReports[i].callStack[pcIndex]);
|
|
|
|
JSString *str = JS_NewStringCopyZ(cx, pcString.get());
|
|
|
|
if (!str) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
jsval v = STRING_TO_JSVAL(str);
|
|
|
|
if (!JS_SetElement(cx, pcArray, pcIndex, &v)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record memory map info
|
|
|
|
JSObject *moduleArray = JS_NewArrayObject(cx, 0, nsnull);
|
|
|
|
if (!moduleArray) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
ok = JS_DefineProperty(cx, reportObj, "memoryMap",
|
|
|
|
OBJECT_TO_JSVAL(moduleArray),
|
|
|
|
NULL, NULL, JSPROP_ENUMERATE);
|
|
|
|
if (!ok) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:57:04 -07:00
|
|
|
#if defined(MOZ_ENABLE_PROFILER_SPS)
|
2012-05-16 04:39:21 -07:00
|
|
|
const uint32_t moduleCount = mHangReports[i].moduleMap.GetSize();
|
2012-03-12 04:07:05 -07:00
|
|
|
for (size_t moduleIndex = 0; moduleIndex < moduleCount; ++moduleIndex) {
|
|
|
|
// Current module
|
|
|
|
const SharedLibrary &module =
|
|
|
|
mHangReports[i].moduleMap.GetEntry(moduleIndex);
|
|
|
|
|
|
|
|
JSObject *moduleInfoArray = JS_NewArrayObject(cx, 0, nsnull);
|
|
|
|
if (!moduleInfoArray) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
jsval val = OBJECT_TO_JSVAL(moduleInfoArray);
|
|
|
|
if (!JS_SetElement(cx, moduleArray, moduleIndex, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start address
|
|
|
|
nsCAutoString addressString;
|
|
|
|
addressString.AppendPrintf("0x%p", module.GetStart());
|
|
|
|
JSString *str = JS_NewStringCopyZ(cx, addressString.get());
|
|
|
|
if (!str) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
val = STRING_TO_JSVAL(str);
|
|
|
|
if (!JS_SetElement(cx, moduleInfoArray, 0, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module name
|
|
|
|
str = JS_NewStringCopyZ(cx, module.GetName());
|
|
|
|
if (!str) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
val = STRING_TO_JSVAL(str);
|
|
|
|
if (!JS_SetElement(cx, moduleInfoArray, 1, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module size in memory
|
|
|
|
val = INT_TO_JSVAL(int32_t(module.GetEnd() - module.GetStart()));
|
|
|
|
if (!JS_SetElement(cx, moduleInfoArray, 2, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "PDB Age" identifier
|
|
|
|
val = INT_TO_JSVAL(0);
|
|
|
|
#if defined(MOZ_PROFILING) && defined(XP_WIN)
|
|
|
|
val = INT_TO_JSVAL(module.GetPdbAge());
|
|
|
|
#endif
|
|
|
|
if (!JS_SetElement(cx, moduleInfoArray, 3, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "PDB Signature" GUID
|
|
|
|
char guidString[NSID_LENGTH] = { 0 };
|
|
|
|
#if defined(MOZ_PROFILING) && defined(XP_WIN)
|
|
|
|
module.GetPdbSignature().ToProvidedString(guidString);
|
|
|
|
#endif
|
|
|
|
str = JS_NewStringCopyZ(cx, guidString);
|
|
|
|
if (!str) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
val = STRING_TO_JSVAL(str);
|
|
|
|
if (!JS_SetElement(cx, moduleInfoArray, 4, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-03-19 16:05:25 -07:00
|
|
|
|
|
|
|
// Name of associated PDB file
|
|
|
|
const char *pdbName = "";
|
|
|
|
#if defined(MOZ_PROFILING) && defined(XP_WIN)
|
|
|
|
pdbName = module.GetPdbName();
|
|
|
|
#endif
|
|
|
|
str = JS_NewStringCopyZ(cx, pdbName);
|
|
|
|
if (!str) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
val = STRING_TO_JSVAL(str);
|
|
|
|
if (!JS_SetElement(cx, moduleInfoArray, 5, &val)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-03-12 04:07:05 -07:00
|
|
|
}
|
2012-03-14 19:57:04 -07:00
|
|
|
#endif
|
2012-03-12 04:07:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-06 11:40:45 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetRegisteredHistograms(JSContext *cx, jsval *ret)
|
|
|
|
{
|
|
|
|
size_t count = ArrayLength(gHistograms);
|
|
|
|
JSObject *info = JS_NewObject(cx, NULL, NULL, NULL);
|
|
|
|
if (!info)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-02-15 14:01:53 -08:00
|
|
|
JS::AutoObjectRooter root(cx, info);
|
2012-01-06 11:40:45 -08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
JSString *comment = JS_InternString(cx, gHistograms[i].comment);
|
|
|
|
|
|
|
|
if (!(comment
|
|
|
|
&& JS_DefineProperty(cx, info, gHistograms[i].id,
|
|
|
|
STRING_TO_JSVAL(comment), NULL, NULL,
|
|
|
|
JSPROP_ENUMERATE))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = OBJECT_TO_JSVAL(info);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-16 12:33:18 -08:00
|
|
|
NS_IMETHODIMP
|
2011-12-03 01:13:14 -08:00
|
|
|
TelemetryImpl::GetHistogramById(const nsACString &name, JSContext *cx, jsval *ret)
|
2011-11-16 12:33:18 -08:00
|
|
|
{
|
2011-06-20 14:47:58 -07:00
|
|
|
Histogram *h;
|
2012-01-06 11:39:29 -08:00
|
|
|
nsresult rv = GetHistogramByName(name, &h);
|
2011-06-20 14:47:58 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return WrapAndReturnHistogram(h, cx, ret);
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
|
|
|
|
2011-06-28 16:57:44 -07:00
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
TelemetryImpl::GetCanRecord(bool *ret) {
|
2011-06-28 16:57:44 -07:00
|
|
|
*ret = mCanRecord;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
TelemetryImpl::SetCanRecord(bool canRecord) {
|
2011-06-28 16:57:44 -07:00
|
|
|
mCanRecord = !!canRecord;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
TelemetryImpl::CanRecord() {
|
|
|
|
return !sTelemetry || sTelemetry->mCanRecord;
|
|
|
|
}
|
2011-06-20 14:47:58 -07:00
|
|
|
|
2012-02-01 20:42:20 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TelemetryImpl::GetCanSend(bool *ret) {
|
|
|
|
#if defined(MOZILLA_OFFICIAL) && defined(MOZ_TELEMETRY_REPORTING)
|
|
|
|
*ret = true;
|
|
|
|
#else
|
|
|
|
*ret = false;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
already_AddRefed<nsITelemetry>
|
2011-06-28 16:57:44 -07:00
|
|
|
TelemetryImpl::CreateTelemetryInstance()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(sTelemetry == NULL, "CreateTelemetryInstance may only be called once, via GetService()");
|
|
|
|
sTelemetry = new TelemetryImpl();
|
|
|
|
// AddRef for the local reference
|
|
|
|
NS_ADDREF(sTelemetry);
|
|
|
|
// AddRef for the caller
|
|
|
|
NS_ADDREF(sTelemetry);
|
|
|
|
return sTelemetry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TelemetryImpl::ShutdownTelemetry()
|
2011-04-27 11:07:02 -07:00
|
|
|
{
|
2011-06-28 16:57:44 -07:00
|
|
|
NS_IF_RELEASE(sTelemetry);
|
2011-04-27 11:07:02 -07:00
|
|
|
}
|
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
void
|
2012-05-16 04:39:21 -07:00
|
|
|
TelemetryImpl::StoreSlowSQL(const nsACString &sql, uint32_t delay,
|
2012-03-21 10:26:48 -07:00
|
|
|
bool isDynamicSql, bool isTrackedDB, bool isAggregate)
|
2011-12-06 12:12:55 -08:00
|
|
|
{
|
2012-02-13 11:47:40 -08:00
|
|
|
AutoHashtable<SlowSQLEntryType> *slowSQLMap = NULL;
|
2011-12-06 12:12:55 -08:00
|
|
|
if (NS_IsMainThread())
|
|
|
|
slowSQLMap = &(sTelemetry->mSlowSQLOnMainThread);
|
|
|
|
else
|
|
|
|
slowSQLMap = &(sTelemetry->mSlowSQLOnOtherThread);
|
|
|
|
|
|
|
|
MutexAutoLock hashMutex(sTelemetry->mHashMutex);
|
2012-03-21 10:26:48 -07:00
|
|
|
|
|
|
|
SlowSQLEntryType *entry = slowSQLMap->GetEntry(sql);
|
2011-12-06 12:12:55 -08:00
|
|
|
if (!entry) {
|
2012-03-21 10:26:48 -07:00
|
|
|
entry = slowSQLMap->PutEntry(sql);
|
2011-12-06 12:12:55 -08:00
|
|
|
if (NS_UNLIKELY(!entry))
|
|
|
|
return;
|
2012-03-21 10:26:48 -07:00
|
|
|
entry->mData.isDynamicSql = isDynamicSql;
|
|
|
|
entry->mData.isTrackedDb = isTrackedDB;
|
|
|
|
entry->mData.isAggregate = isAggregate;
|
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
entry->mData.hitCount = 0;
|
|
|
|
entry->mData.totalTime = 0;
|
|
|
|
}
|
2012-03-21 10:26:48 -07:00
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
entry->mData.hitCount++;
|
|
|
|
entry->mData.totalTime += delay;
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:26:48 -07:00
|
|
|
void
|
|
|
|
TelemetryImpl::RecordSlowStatement(const nsACString &sql, const nsACString &dbName,
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t delay, bool isDynamicString)
|
2012-03-21 10:26:48 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(sTelemetry);
|
|
|
|
if (!sTelemetry->mCanRecord)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool isTrackedDb = sTelemetry->mTrackedDBs.Contains(dbName);
|
|
|
|
bool isPrivate = (!isTrackedDb) || isDynamicString;
|
|
|
|
if (isPrivate) {
|
|
|
|
// Report aggregate DB-level statistics to Telemetry for potentially
|
|
|
|
// sensitive SQL strings
|
|
|
|
nsCAutoString aggregate;
|
|
|
|
aggregate.AppendPrintf("Untracked SQL for %s", dbName.BeginReading());
|
|
|
|
StoreSlowSQL(aggregate, delay, isDynamicString, isTrackedDb, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record original SQL string
|
|
|
|
nsCAutoString fullSql(sql);
|
|
|
|
if (!isTrackedDb)
|
|
|
|
fullSql.AppendPrintf(" -- Untracked DB %s", dbName.BeginReading());
|
|
|
|
StoreSlowSQL(fullSql, delay, isDynamicString, isTrackedDb, false);
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:57:04 -07:00
|
|
|
#if defined(MOZ_ENABLE_PROFILER_SPS)
|
2012-03-12 04:07:05 -07:00
|
|
|
void
|
2012-05-16 04:39:21 -07:00
|
|
|
TelemetryImpl::RecordChromeHang(uint32_t duration,
|
2012-03-12 04:07:05 -07:00
|
|
|
const Telemetry::HangStack &callStack,
|
|
|
|
SharedLibraryInfo &moduleMap)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sTelemetry);
|
|
|
|
if (!sTelemetry->mCanRecord) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock hangReportMutex(sTelemetry->mHangReportsMutex);
|
|
|
|
|
|
|
|
// Only report the modules which changed since the first hang report
|
|
|
|
if (sTelemetry->mHangReports.Length()) {
|
|
|
|
SharedLibraryInfo &firstModuleMap =
|
|
|
|
sTelemetry->mHangReports[0].moduleMap;
|
|
|
|
for (size_t i = 0; i < moduleMap.GetSize(); ++i) {
|
|
|
|
if (firstModuleMap.Contains(moduleMap.GetEntry(i))) {
|
|
|
|
moduleMap.RemoveEntries(i, i + 1);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HangReport newReport = { duration, callStack, moduleMap };
|
|
|
|
sTelemetry->mHangReports.AppendElement(newReport);
|
|
|
|
}
|
2012-03-14 19:57:04 -07:00
|
|
|
#endif
|
2012-03-12 04:07:05 -07:00
|
|
|
|
2011-06-28 16:57:44 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(TelemetryImpl, nsITelemetry)
|
|
|
|
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsITelemetry, TelemetryImpl::CreateTelemetryInstance)
|
2011-04-27 11:07:02 -07:00
|
|
|
|
|
|
|
#define NS_TELEMETRY_CID \
|
2011-05-19 19:33:05 -07:00
|
|
|
{0xaea477f2, 0xb3a2, 0x469c, {0xaa, 0x29, 0x0a, 0x82, 0xd1, 0x32, 0xb8, 0x29}}
|
2011-04-27 11:07:02 -07:00
|
|
|
NS_DEFINE_NAMED_CID(NS_TELEMETRY_CID);
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
const Module::CIDEntry kTelemetryCIDs[] = {
|
|
|
|
{ &kNS_TELEMETRY_CID, false, NULL, nsITelemetryConstructor },
|
2011-04-27 11:07:02 -07:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
const Module::ContractIDEntry kTelemetryContracts[] = {
|
2011-04-27 11:07:02 -07:00
|
|
|
{ "@mozilla.org/base/telemetry;1", &kNS_TELEMETRY_CID },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
const Module kTelemetryModule = {
|
|
|
|
Module::kVersion,
|
2011-04-27 11:07:02 -07:00
|
|
|
kTelemetryCIDs,
|
|
|
|
kTelemetryContracts,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2011-06-28 16:57:44 -07:00
|
|
|
TelemetryImpl::ShutdownTelemetry
|
2011-04-27 11:07:02 -07:00
|
|
|
};
|
|
|
|
|
2011-05-19 19:33:05 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace Telemetry {
|
|
|
|
|
|
|
|
void
|
2012-05-16 04:39:21 -07:00
|
|
|
Accumulate(ID aHistogram, uint32_t aSample)
|
2011-06-20 14:47:58 -07:00
|
|
|
{
|
2011-06-28 16:57:44 -07:00
|
|
|
if (!TelemetryImpl::CanRecord()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-20 14:47:58 -07:00
|
|
|
Histogram *h;
|
|
|
|
nsresult rv = GetHistogramByEnumId(aHistogram, &h);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
h->Add(aSample);
|
|
|
|
}
|
|
|
|
|
2011-10-10 10:04:57 -07:00
|
|
|
void
|
|
|
|
AccumulateTimeDelta(ID aHistogram, TimeStamp start, TimeStamp end)
|
|
|
|
{
|
|
|
|
Accumulate(aHistogram,
|
2012-05-16 04:39:21 -07:00
|
|
|
static_cast<uint32_t>((end - start).ToMilliseconds()));
|
2011-10-10 10:04:57 -07:00
|
|
|
}
|
|
|
|
|
2012-01-18 09:08:18 -08:00
|
|
|
bool
|
|
|
|
CanRecord()
|
|
|
|
{
|
|
|
|
return TelemetryImpl::CanRecord();
|
|
|
|
}
|
|
|
|
|
2011-08-05 06:53:48 -07:00
|
|
|
base::Histogram*
|
|
|
|
GetHistogramById(ID id)
|
|
|
|
{
|
|
|
|
Histogram *h = NULL;
|
|
|
|
GetHistogramByEnumId(id, &h);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2011-12-06 12:12:55 -08:00
|
|
|
void
|
|
|
|
RecordSlowSQLStatement(const nsACString &statement,
|
|
|
|
const nsACString &dbName,
|
2012-05-16 04:39:21 -07:00
|
|
|
uint32_t delay,
|
2012-03-21 10:26:48 -07:00
|
|
|
bool isDynamicString)
|
2011-12-06 12:12:55 -08:00
|
|
|
{
|
2012-03-21 10:26:48 -07:00
|
|
|
TelemetryImpl::RecordSlowStatement(statement, dbName, delay, isDynamicString);
|
2011-12-06 12:12:55 -08:00
|
|
|
}
|
|
|
|
|
2012-01-10 12:29:58 -08:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
// Make the service manager hold a long-lived reference to the service
|
|
|
|
nsCOMPtr<nsITelemetry> telemetryService =
|
|
|
|
do_GetService("@mozilla.org/base/telemetry;1");
|
|
|
|
MOZ_ASSERT(telemetryService);
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:57:04 -07:00
|
|
|
#if defined(MOZ_ENABLE_PROFILER_SPS)
|
2012-05-16 04:39:21 -07:00
|
|
|
void RecordChromeHang(uint32_t duration,
|
2012-03-12 04:07:05 -07:00
|
|
|
const Telemetry::HangStack &callStack,
|
|
|
|
SharedLibraryInfo &moduleMap)
|
|
|
|
{
|
|
|
|
TelemetryImpl::RecordChromeHang(duration, callStack, moduleMap);
|
|
|
|
}
|
2012-03-14 19:57:04 -07:00
|
|
|
#endif
|
2012-03-12 04:07:05 -07:00
|
|
|
|
2011-06-20 14:47:58 -07:00
|
|
|
} // namespace Telemetry
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2011-04-27 11:07:02 -07:00
|
|
|
NSMODULE_DEFN(nsTelemetryModule) = &kTelemetryModule;
|
2011-05-21 23:23:20 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The XRE_TelemetryAdd function is to be used by embedding applications
|
|
|
|
* that can't use mozilla::Telemetry::Accumulate() directly.
|
|
|
|
*/
|
|
|
|
void
|
2012-05-16 04:39:21 -07:00
|
|
|
XRE_TelemetryAccumulate(int aID, uint32_t aSample)
|
2011-05-21 23:23:20 -07:00
|
|
|
{
|
|
|
|
mozilla::Telemetry::Accumulate((mozilla::Telemetry::ID) aID, aSample);
|
|
|
|
}
|