From fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:14:29 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- .../glean/bindings/private/CustomDistribution.cpp | 8 +- .../glean/bindings/private/DistributionData.h | 6 +- .../glean/bindings/private/MemoryDistribution.cpp | 8 +- .../components/glean/bindings/private/Object.cpp | 80 ++++++++++++++++++ toolkit/components/glean/bindings/private/Object.h | 94 ++++++++++++++++++++++ .../components/glean/bindings/private/Timespan.cpp | 1 + .../glean/bindings/private/TimingDistribution.cpp | 29 ++++--- 7 files changed, 207 insertions(+), 19 deletions(-) create mode 100644 toolkit/components/glean/bindings/private/Object.cpp create mode 100644 toolkit/components/glean/bindings/private/Object.h (limited to 'toolkit/components/glean/bindings/private') diff --git a/toolkit/components/glean/bindings/private/CustomDistribution.cpp b/toolkit/components/glean/bindings/private/CustomDistribution.cpp index 2f0226cb58..a5a821a558 100644 --- a/toolkit/components/glean/bindings/private/CustomDistribution.cpp +++ b/toolkit/components/glean/bindings/private/CustomDistribution.cpp @@ -59,9 +59,10 @@ CustomDistributionMetric::TestGetValue(const nsACString& aPingName) const { nsTArray buckets; nsTArray counts; uint64_t sum; - fog_custom_distribution_test_get_value(mId, &aPingName, &sum, &buckets, - &counts); - return Some(DistributionData(buckets, counts, sum)); + uint64_t count; + fog_custom_distribution_test_get_value(mId, &aPingName, &sum, &count, + &buckets, &counts); + return Some(DistributionData(buckets, counts, sum, count)); } } // namespace impl @@ -92,6 +93,7 @@ void GleanCustomDistribution::TestGetValue( dom::GleanDistributionData ret; ret.mSum = optresult.ref().sum; + ret.mCount = optresult.ref().count; auto& data = optresult.ref().values; for (const auto& entry : data) { dom::binding_detail::RecordEntry bucket; diff --git a/toolkit/components/glean/bindings/private/DistributionData.h b/toolkit/components/glean/bindings/private/DistributionData.h index 6ff995f222..fb9bba720e 100644 --- a/toolkit/components/glean/bindings/private/DistributionData.h +++ b/toolkit/components/glean/bindings/private/DistributionData.h @@ -12,6 +12,7 @@ namespace mozilla::glean { struct DistributionData final { uint64_t sum; + uint64_t count; nsTHashMap values; /** @@ -19,8 +20,9 @@ struct DistributionData final { * as returned by `fog_*_distribution_test_get_value`. */ DistributionData(const nsTArray& aBuckets, - const nsTArray& aCounts, uint64_t aSum) - : sum(aSum) { + const nsTArray& aCounts, uint64_t aSum, + uint64_t aCount) + : sum(aSum), count(aCount) { for (size_t i = 0; i < aBuckets.Length(); ++i) { this->values.InsertOrUpdate(aBuckets[i], aCounts[i]); } diff --git a/toolkit/components/glean/bindings/private/MemoryDistribution.cpp b/toolkit/components/glean/bindings/private/MemoryDistribution.cpp index a580c5df3c..64f3bf241c 100644 --- a/toolkit/components/glean/bindings/private/MemoryDistribution.cpp +++ b/toolkit/components/glean/bindings/private/MemoryDistribution.cpp @@ -44,9 +44,10 @@ MemoryDistributionMetric::TestGetValue(const nsACString& aPingName) const { nsTArray buckets; nsTArray counts; uint64_t sum; - fog_memory_distribution_test_get_value(mId, &aPingName, &sum, &buckets, - &counts); - return Some(DistributionData(buckets, counts, sum)); + uint64_t count; + fog_memory_distribution_test_get_value(mId, &aPingName, &sum, &count, + &buckets, &counts); + return Some(DistributionData(buckets, counts, sum, count)); } } // namespace impl @@ -76,6 +77,7 @@ void GleanMemoryDistribution::TestGetValue( dom::GleanDistributionData ret; ret.mSum = optresult.ref().sum; + ret.mCount = optresult.ref().count; auto& data = optresult.ref().values; for (const auto& entry : data) { dom::binding_detail::RecordEntry bucket; diff --git a/toolkit/components/glean/bindings/private/Object.cpp b/toolkit/components/glean/bindings/private/Object.cpp new file mode 100644 index 0000000000..817b14dc0f --- /dev/null +++ b/toolkit/components/glean/bindings/private/Object.cpp @@ -0,0 +1,80 @@ +/* -*- 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/glean/bindings/Object.h" + +#include "Common.h" +#include "mozilla/dom/GleanMetricsBinding.h" +#include "mozilla/dom/ToJSValue.h" +#include "mozilla/Logging.h" +#include "jsapi.h" +#include "js/JSON.h" +#include "nsContentUtils.h" + +using namespace mozilla::dom; + +namespace mozilla::glean { + +/* virtual */ +JSObject* GleanObject::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return dom::GleanObject_Binding::Wrap(aCx, this, aGivenProto); +} + +void GleanObject::Set(JSContext* aCx, JS::Handle aObj) { + // We take in an `object`. Cannot be `null`! + // But at this point the type system doesn't know that. + JS::Rooted value(aCx); + value.setObjectOrNull(aObj); + + nsAutoString serializedValue; + bool res = nsContentUtils::StringifyJSON(aCx, value, serializedValue, + UndefinedIsNullStringLiteral); + if (!res) { + // JS_Stringify throws an exception, e.g. on cyclic objects. + // We don't want this rethrown. + JS_ClearPendingException(aCx); + + LogToBrowserConsole(nsIScriptError::warningFlag, + u"passed in object cannot be serialized"_ns); + return; + } + + NS_ConvertUTF16toUTF8 payload(serializedValue); + mObject.SetStr(payload); +} + +void GleanObject::TestGetValue(JSContext* aCx, const nsACString& aPingName, + JS::MutableHandle aResult, + ErrorResult& aRv) { + aResult.set(nullptr); + + auto result = mObject.TestGetValue(aPingName); + if (result.isErr()) { + aRv.ThrowDataError(result.unwrapErr()); + return; + } + auto optresult = result.unwrap(); + if (optresult.isNothing()) { + return; + } + + const NS_ConvertUTF8toUTF16 str(optresult.ref()); + JS::Rooted json(aCx); + bool res = JS_ParseJSON(aCx, str.get(), str.Length(), &json); + if (!res) { + aRv.ThrowDataError("couldn't parse stored object"); + return; + } + if (!json.isObject()) { + aRv.ThrowDataError("stored data does not represent a valid object"); + return; + } + + aResult.set(&json.toObject()); +} + +} // namespace mozilla::glean diff --git a/toolkit/components/glean/bindings/private/Object.h b/toolkit/components/glean/bindings/private/Object.h new file mode 100644 index 0000000000..4c81f8b096 --- /dev/null +++ b/toolkit/components/glean/bindings/private/Object.h @@ -0,0 +1,94 @@ +/* -*- 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/. */ + +#ifndef mozilla_glean_GleanObject_h +#define mozilla_glean_GleanObject_h + +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/glean/bindings/GleanMetric.h" +#include "mozilla/glean/fog_ffi_generated.h" +#include "mozilla/ResultVariant.h" +#include "nsString.h" +#include "nsTArray.h" + +namespace mozilla::glean { + +// forward declaration +class GleanObject; + +namespace impl { + +template +class ObjectMetric { + friend class mozilla::glean::GleanObject; + + public: + constexpr explicit ObjectMetric(uint32_t id) : mId(id) {} + + private: + const uint32_t mId; + + /* TODO(bug 1881023): Turn this into the public C++ API */ + /** + * **Test-only API** + * + * Gets the currently stored object as a JSON-encoded string. + * + * This function will attempt to await the last parent-process task (if any) + * writing to the the metric's storage engine before returning a value. + * This function will not wait for data from child processes. + * + * This doesn't clear the stored value. + * Parent process only. Panics in child processes. + * + * @param aPingName The (optional) name of the ping to retrieve the metric + * for. Defaults to the first value in `send_in_pings`. + * + * @return value of the stored metric, or Nothing() if there is no value. + */ + Result, nsCString> TestGetValue( + const nsACString& aPingName) const { + nsCString err; + if (fog_object_test_get_error(mId, &err)) { + return Err(err); + } + if (!fog_object_test_has_value(mId, &aPingName)) { + return Maybe(); + } + nsCString ret; + fog_object_test_get_value(mId, &aPingName, &ret); + return Some(ret); + } + + void SetStr(const nsACString& aValue) const { + fog_object_set_string(mId, &aValue); + } +}; + +} // namespace impl + +class GleanObject final : public GleanMetric { + public: + explicit GleanObject(uint32_t aId, nsISupports* aParent) + : GleanMetric(aParent), mObject(aId) {} + + virtual JSObject* WrapObject( + JSContext* aCx, JS::Handle aGivenProto) override final; + + void Set(JSContext* aCx, JS::Handle aObj); + + void TestGetValue(JSContext* aCx, const nsACString& aPingName, + JS::MutableHandle aResult, ErrorResult& aRv); + + virtual ~GleanObject() = default; + + private: + const impl::ObjectMetric mObject; +}; + +} // namespace mozilla::glean + +#endif /* mozilla_glean_GleanObject.h */ diff --git a/toolkit/components/glean/bindings/private/Timespan.cpp b/toolkit/components/glean/bindings/private/Timespan.cpp index 13e57317fa..2ab1f0dbba 100644 --- a/toolkit/components/glean/bindings/private/Timespan.cpp +++ b/toolkit/components/glean/bindings/private/Timespan.cpp @@ -36,6 +36,7 @@ class ScalarIDHashKey : public PLDHashEntryHdr { return static_cast::type>(*aKey); } enum { ALLOW_MEMMOVE = true }; + static_assert(std::is_trivially_copyable_v); private: const ScalarID mValue; diff --git a/toolkit/components/glean/bindings/private/TimingDistribution.cpp b/toolkit/components/glean/bindings/private/TimingDistribution.cpp index f7a78165ae..036db5f9db 100644 --- a/toolkit/components/glean/bindings/private/TimingDistribution.cpp +++ b/toolkit/components/glean/bindings/private/TimingDistribution.cpp @@ -21,7 +21,10 @@ namespace mozilla::glean { using MetricId = uint32_t; // Same type as in api/src/private/mod.rs -using MetricTimerTuple = std::tuple; +struct MetricTimerTuple { + MetricId mMetricId; + TimerId mTimerId; +}; class MetricTimerTupleHashKey : public PLDHashEntryHdr { public: using KeyType = const MetricTimerTuple&; @@ -34,16 +37,17 @@ class MetricTimerTupleHashKey : public PLDHashEntryHdr { KeyType GetKey() const { return mValue; } bool KeyEquals(KeyTypePointer aKey) const { - return std::get<0>(*aKey) == std::get<0>(mValue) && - std::get<1>(*aKey) == std::get<1>(mValue); + return aKey->mMetricId == mValue.mMetricId && + aKey->mTimerId == mValue.mTimerId; } static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } static PLDHashNumber HashKey(KeyTypePointer aKey) { // Chosen because this is how nsIntegralHashKey does it. - return HashGeneric(std::get<0>(*aKey), std::get<1>(*aKey)); + return HashGeneric(aKey->mMetricId, aKey->mTimerId); } enum { ALLOW_MEMMOVE = true }; + static_assert(std::is_trivially_copyable_v); private: const MetricTimerTuple mValue; @@ -103,7 +107,7 @@ extern "C" NS_EXPORT void GIFFT_TimingDistributionStart( auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId); if (mirrorId) { mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) { - auto tuple = std::make_tuple(aMetricId, aTimerId); + auto tuple = mozilla::glean::MetricTimerTuple{aMetricId, aTimerId}; // It should be all but impossible for anyone to have already inserted // this timer for this metric given the monotonicity of timer ids. (void)NS_WARN_IF(lock.ref()->Remove(tuple)); @@ -118,7 +122,8 @@ extern "C" NS_EXPORT void GIFFT_TimingDistributionStopAndAccumulate( auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId); if (mirrorId) { mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) { - auto optStart = lock.ref()->Extract(std::make_tuple(aMetricId, aTimerId)); + auto tuple = mozilla::glean::MetricTimerTuple{aMetricId, aTimerId}; + auto optStart = lock.ref()->Extract(tuple); // The timer might not be in the map to be removed if it's already been // cancelled or stop_and_accumulate'd. if (!NS_WARN_IF(!optStart)) { @@ -145,8 +150,8 @@ extern "C" NS_EXPORT void GIFFT_TimingDistributionCancel( mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) { // The timer might not be in the map to be removed if it's already been // cancelled or stop_and_accumulate'd. - (void)NS_WARN_IF( - !lock.ref()->Remove(std::make_tuple(aMetricId, aTimerId))); + auto tuple = mozilla::glean::MetricTimerTuple{aMetricId, aTimerId}; + (void)NS_WARN_IF(!lock.ref()->Remove(tuple)); }); } } @@ -187,9 +192,10 @@ TimingDistributionMetric::TestGetValue(const nsACString& aPingName) const { nsTArray buckets; nsTArray counts; uint64_t sum; - fog_timing_distribution_test_get_value(mId, &aPingName, &sum, &buckets, - &counts); - return Some(DistributionData(buckets, counts, sum)); + uint64_t count; + fog_timing_distribution_test_get_value(mId, &aPingName, &sum, &count, + &buckets, &counts); + return Some(DistributionData(buckets, counts, sum, count)); } } // namespace impl @@ -225,6 +231,7 @@ void GleanTimingDistribution::TestGetValue( dom::GleanDistributionData ret; ret.mSum = optresult.ref().sum; + ret.mCount = optresult.ref().count; auto& data = optresult.ref().values; for (const auto& entry : data) { dom::binding_detail::RecordEntry bucket; -- cgit v1.2.3