1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/telemetry/Stopwatch.h"
#include "TelemetryHistogram.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/TimeStamp.h"
#include "nsHashKeys.h"
#include "nsContentUtils.h"
#include "nsPrintfCString.h"
#include "nsQueryObject.h"
#include "nsRefPtrHashtable.h"
#include "nsString.h"
#include "xpcpublic.h"
using mozilla::dom::AutoJSAPI;
static inline nsQueryObject<nsISupports> do_QueryReflector(
JSObject* aReflector) {
// None of the types we query to are implemented by Window or Location.
nsCOMPtr<nsISupports> reflector = xpc::ReflectorToISupportsStatic(aReflector);
return do_QueryObject(reflector);
}
static inline nsQueryObject<nsISupports> do_QueryReflector(
const JS::Value& aReflector) {
return do_QueryReflector(&aReflector.toObject());
}
static void LogError(JSContext* aCx, const nsCString& aMessage) {
// This is a bit of a hack to report an error with the current JS caller's
// location. We create an AutoJSAPI object bound to the current caller
// global, report a JS error, and then let AutoJSAPI's destructor report the
// error.
//
// Unfortunately, there isn't currently a more straightforward way to do
// this from C++.
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
AutoJSAPI jsapi;
if (jsapi.Init(global)) {
JS_ReportErrorUTF8(jsapi.cx(), "%s", aMessage.get());
}
}
namespace mozilla::telemetry {
class Timer final {
public:
NS_INLINE_DECL_REFCOUNTING(Timer)
Timer() = default;
void Start(bool aInSeconds) {
mStartTime = TimeStamp::Now();
mInSeconds = aInSeconds;
}
bool Started() { return !mStartTime.IsNull(); }
uint32_t Elapsed() {
auto delta = TimeStamp::Now() - mStartTime;
return mInSeconds ? delta.ToSeconds() : delta.ToMilliseconds();
}
bool& InSeconds() { return mInSeconds; }
private:
~Timer() = default;
TimeStamp mStartTime{};
bool mInSeconds;
};
#define TIMER_KEYS_IID \
{ \
0xef707178, 0x1544, 0x46e2, { \
0xa3, 0xf5, 0x98, 0x38, 0xba, 0x60, 0xfd, 0x8f \
} \
}
class TimerKeys final : public nsISupports {
public:
NS_DECL_ISUPPORTS
NS_DECLARE_STATIC_IID_ACCESSOR(TIMER_KEYS_IID)
Timer* Get(const nsAString& aKey, bool aCreate = true);
already_AddRefed<Timer> GetAndDelete(const nsAString& aKey) {
RefPtr<Timer> timer;
mTimers.Remove(aKey, getter_AddRefs(timer));
return timer.forget();
}
bool Delete(const nsAString& aKey) { return mTimers.Remove(aKey); }
private:
~TimerKeys() = default;
nsRefPtrHashtable<nsStringHashKey, Timer> mTimers;
};
NS_DEFINE_STATIC_IID_ACCESSOR(TimerKeys, TIMER_KEYS_IID)
NS_IMPL_ISUPPORTS(TimerKeys, TimerKeys)
Timer* TimerKeys::Get(const nsAString& aKey, bool aCreate) {
if (aCreate) {
RefPtr<Timer>& timer = mTimers.GetOrInsert(aKey);
if (!timer) {
timer = new Timer();
}
return timer;
}
return mTimers.GetWeak(aKey);
}
class Timers final {
public:
Timers();
static Timers& Singleton();
NS_INLINE_DECL_REFCOUNTING(Timers)
JSObject* Get(JSContext* aCx, const nsAString& aHistogram,
bool aCreate = true);
TimerKeys* Get(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, bool aCreate = true);
Timer* Get(JSContext* aCx, const nsAString& aHistogram, JS::HandleObject aObj,
const nsAString& aKey, bool aCreate = true);
already_AddRefed<Timer> GetAndDelete(JSContext* aCx,
const nsAString& aHistogram,
JS::HandleObject aObj,
const nsAString& aKey);
bool Delete(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey);
int32_t TimeElapsed(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey,
bool aCanceledOkay = false, bool aDelete = false);
bool Start(JSContext* aCx, const nsAString& aHistogram, JS::HandleObject aObj,
const nsAString& aKey, bool aInSeconds = false);
int32_t Finish(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey,
bool aCanceledOkay = false);
bool& SuppressErrors() { return mSuppressErrors; }
private:
~Timers() = default;
JS::PersistentRooted<JSObject*> mTimers;
bool mSuppressErrors = false;
static StaticRefPtr<Timers> sSingleton;
};
StaticRefPtr<Timers> Timers::sSingleton;
/* static */ Timers& Timers::Singleton() {
if (!sSingleton) {
sSingleton = new Timers();
ClearOnShutdown(&sSingleton);
}
return *sSingleton;
}
Timers::Timers() : mTimers(dom::RootingCx()) {
AutoJSAPI jsapi;
MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
mTimers = JS::NewMapObject(jsapi.cx());
MOZ_RELEASE_ASSERT(mTimers);
}
JSObject* Timers::Get(JSContext* aCx, const nsAString& aHistogram,
bool aCreate) {
JSAutoRealm ar(aCx, mTimers);
JS::RootedValue histogram(aCx);
JS::RootedValue objs(aCx);
if (!xpc::NonVoidStringToJsval(aCx, aHistogram, &histogram) ||
!JS::MapGet(aCx, mTimers, histogram, &objs)) {
return nullptr;
}
if (!objs.isObject()) {
if (aCreate) {
objs = JS::ObjectOrNullValue(JS::NewWeakMapObject(aCx));
}
if (!objs.isObject() || !JS::MapSet(aCx, mTimers, histogram, objs)) {
return nullptr;
}
}
return &objs.toObject();
}
TimerKeys* Timers::Get(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, bool aCreate) {
JSAutoRealm ar(aCx, mTimers);
JS::RootedObject objs(aCx, Get(aCx, aHistogram, aCreate));
if (!objs) {
return nullptr;
}
// If no object is passed, use mTimers as a stand-in for a null object
// (which cannot be used as a weak map key).
JS::RootedObject obj(aCx, aObj ? aObj : mTimers);
if (!JS_WrapObject(aCx, &obj)) {
return nullptr;
}
RefPtr<TimerKeys> keys;
JS::RootedValue keysObj(aCx);
if (!JS::GetWeakMapEntry(aCx, objs, obj, &keysObj)) {
return nullptr;
}
if (!keysObj.isObject()) {
if (aCreate) {
keys = new TimerKeys();
Unused << nsContentUtils::WrapNative(aCx, keys, &keysObj);
}
if (!keysObj.isObject() || !JS::SetWeakMapEntry(aCx, objs, obj, keysObj)) {
return nullptr;
}
}
keys = do_QueryReflector(keysObj);
return keys;
}
Timer* Timers::Get(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey, bool aCreate) {
if (RefPtr<TimerKeys> keys = Get(aCx, aHistogram, aObj, aCreate)) {
return keys->Get(aKey, aCreate);
}
return nullptr;
}
already_AddRefed<Timer> Timers::GetAndDelete(JSContext* aCx,
const nsAString& aHistogram,
JS::HandleObject aObj,
const nsAString& aKey) {
if (RefPtr<TimerKeys> keys = Get(aCx, aHistogram, aObj, false)) {
return keys->GetAndDelete(aKey);
}
return nullptr;
}
bool Timers::Delete(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey) {
if (RefPtr<TimerKeys> keys = Get(aCx, aHistogram, aObj, false)) {
return keys->Delete(aKey);
}
return false;
}
int32_t Timers::TimeElapsed(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey,
bool aCanceledOkay, bool aDelete) {
RefPtr<Timer> timer;
if (aDelete) {
timer = GetAndDelete(aCx, aHistogram, aObj, aKey);
} else {
timer = Get(aCx, aHistogram, aObj, aKey, false);
}
if (!timer) {
if (!aCanceledOkay && !mSuppressErrors) {
LogError(aCx, nsPrintfCString(
"TelemetryStopwatch: requesting elapsed time for "
"nonexisting stopwatch. Histogram: \"%s\", key: \"%s\"",
NS_ConvertUTF16toUTF8(aHistogram).get(),
NS_ConvertUTF16toUTF8(aKey).get()));
}
return -1;
}
return timer->Elapsed();
}
bool Timers::Start(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey,
bool aInSeconds) {
if (RefPtr<Timer> timer = Get(aCx, aHistogram, aObj, aKey)) {
if (timer->Started()) {
if (!mSuppressErrors) {
LogError(aCx,
nsPrintfCString(
"TelemetryStopwatch: key \"%s\" was already initialized",
NS_ConvertUTF16toUTF8(aHistogram).get()));
}
Delete(aCx, aHistogram, aObj, aKey);
} else {
timer->Start(aInSeconds);
return true;
}
}
return false;
}
int32_t Timers::Finish(JSContext* aCx, const nsAString& aHistogram,
JS::HandleObject aObj, const nsAString& aKey,
bool aCanceledOkay) {
int32_t delta = TimeElapsed(aCx, aHistogram, aObj, aKey, aCanceledOkay, true);
if (delta == -1) {
return delta;
}
NS_ConvertUTF16toUTF8 histogram(aHistogram);
nsresult rv;
if (!aKey.IsVoid()) {
NS_ConvertUTF16toUTF8 key(aKey);
rv = TelemetryHistogram::Accumulate(histogram.get(), key, delta);
} else {
rv = TelemetryHistogram::Accumulate(histogram.get(), delta);
}
if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE && !mSuppressErrors) {
LogError(aCx, nsPrintfCString(
"TelemetryStopwatch: failed to update the Histogram "
"\"%s\", using key: \"%s\"",
NS_ConvertUTF16toUTF8(aHistogram).get(),
NS_ConvertUTF16toUTF8(aKey).get()));
}
return NS_SUCCEEDED(rv) ? delta : -1;
}
/* static */
bool Stopwatch::Start(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram, JS::Handle<JSObject*> aObj,
const dom::TelemetryStopwatchOptions& aOptions) {
return StartKeyed(aGlobal, aHistogram, VoidString(), aObj, aOptions);
}
/* static */
bool Stopwatch::StartKeyed(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram, const nsAString& aKey,
JS::Handle<JSObject*> aObj,
const dom::TelemetryStopwatchOptions& aOptions) {
return Timers::Singleton().Start(aGlobal.Context(), aHistogram, aObj, aKey,
aOptions.mInSeconds);
}
/* static */
bool Stopwatch::Running(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram,
JS::Handle<JSObject*> aObj) {
return RunningKeyed(aGlobal, aHistogram, VoidString(), aObj);
}
/* static */
bool Stopwatch::RunningKeyed(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram, const nsAString& aKey,
JS::Handle<JSObject*> aObj) {
return TimeElapsedKeyed(aGlobal, aHistogram, aKey, aObj, true) != -1;
}
/* static */
int32_t Stopwatch::TimeElapsed(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram,
JS::Handle<JSObject*> aObj, bool aCanceledOkay) {
return TimeElapsedKeyed(aGlobal, aHistogram, VoidString(), aObj,
aCanceledOkay);
}
/* static */
int32_t Stopwatch::TimeElapsedKeyed(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram,
const nsAString& aKey,
JS::Handle<JSObject*> aObj,
bool aCanceledOkay) {
return Timers::Singleton().TimeElapsed(aGlobal.Context(), aHistogram, aObj,
aKey, aCanceledOkay);
}
/* static */
bool Stopwatch::Finish(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram, JS::Handle<JSObject*> aObj,
bool aCanceledOkay) {
return FinishKeyed(aGlobal, aHistogram, VoidString(), aObj, aCanceledOkay);
}
/* static */
bool Stopwatch::FinishKeyed(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram, const nsAString& aKey,
JS::Handle<JSObject*> aObj, bool aCanceledOkay) {
return Timers::Singleton().Finish(aGlobal.Context(), aHistogram, aObj, aKey,
aCanceledOkay) != -1;
}
/* static */
bool Stopwatch::Cancel(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram,
JS::Handle<JSObject*> aObj) {
return CancelKeyed(aGlobal, aHistogram, VoidString(), aObj);
}
/* static */
bool Stopwatch::CancelKeyed(const dom::GlobalObject& aGlobal,
const nsAString& aHistogram, const nsAString& aKey,
JS::Handle<JSObject*> aObj) {
return Timers::Singleton().Delete(aGlobal.Context(), aHistogram, aObj, aKey);
}
/* static */
void Stopwatch::SetTestModeEnabled(const dom::GlobalObject& aGlobal,
bool aTesting) {
Timers::Singleton().SuppressErrors() = aTesting;
}
} // namespace mozilla::telemetry
|