From d8bbc7858622b6d9c278469aab701ca0b609cddf Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 15 May 2024 05:35:49 +0200 Subject: Merging upstream version 126.0. Signed-off-by: Daniel Baumann --- .../components/glean/bindings/private/Boolean.cpp | 2 +- .../components/glean/bindings/private/Counter.cpp | 2 +- .../glean/bindings/private/CustomDistribution.cpp | 23 ++++++++++++++++++++++ .../glean/bindings/private/CustomDistribution.h | 17 ++++++++++++++++ .../glean/bindings/private/DistributionData.h | 22 +++++++++++++++++++++ .../components/glean/bindings/private/Labeled.cpp | 2 +- .../components/glean/bindings/private/Labeled.h | 2 +- toolkit/components/glean/bindings/private/Ping.cpp | 4 ++-- .../components/glean/bindings/private/Timespan.cpp | 6 +++--- .../glean/bindings/private/TimingDistribution.cpp | 20 +++++++++++++++---- 10 files changed, 87 insertions(+), 13 deletions(-) (limited to 'toolkit/components/glean/bindings') diff --git a/toolkit/components/glean/bindings/private/Boolean.cpp b/toolkit/components/glean/bindings/private/Boolean.cpp index 8300990b49..168179f569 100644 --- a/toolkit/components/glean/bindings/private/Boolean.cpp +++ b/toolkit/components/glean/bindings/private/Boolean.cpp @@ -21,7 +21,7 @@ void BooleanMetric::Set(bool aValue) const { if (scalarId) { Telemetry::ScalarSet(scalarId.extract(), aValue); } else if (IsSubmetricId(mId)) { - GetLabeledMirrorLock().apply([&](auto& lock) { + GetLabeledMirrorLock().apply([&](const auto& lock) { auto tuple = lock.ref()->MaybeGet(mId); if (tuple) { Telemetry::ScalarSet(std::get<0>(tuple.ref()), std::get<1>(tuple.ref()), diff --git a/toolkit/components/glean/bindings/private/Counter.cpp b/toolkit/components/glean/bindings/private/Counter.cpp index f7f70f29eb..4eeeb3de7a 100644 --- a/toolkit/components/glean/bindings/private/Counter.cpp +++ b/toolkit/components/glean/bindings/private/Counter.cpp @@ -22,7 +22,7 @@ void CounterMetric::Add(int32_t aAmount) const { if (scalarId) { Telemetry::ScalarAdd(scalarId.extract(), aAmount); } else if (IsSubmetricId(mId)) { - GetLabeledMirrorLock().apply([&](auto& lock) { + GetLabeledMirrorLock().apply([&](const auto& lock) { auto tuple = lock.ref()->MaybeGet(mId); if (tuple && aAmount > 0) { Telemetry::ScalarAdd(std::get<0>(tuple.ref()), diff --git a/toolkit/components/glean/bindings/private/CustomDistribution.cpp b/toolkit/components/glean/bindings/private/CustomDistribution.cpp index a5a821a558..88e96236db 100644 --- a/toolkit/components/glean/bindings/private/CustomDistribution.cpp +++ b/toolkit/components/glean/bindings/private/CustomDistribution.cpp @@ -33,6 +33,15 @@ void CustomDistributionMetric::AccumulateSamples( fog_custom_distribution_accumulate_samples(mId, &aSamples); } +void CustomDistributionMetric::AccumulateSingleSample(uint64_t aSample) const { + auto hgramId = HistogramIdForMetric(mId); + if (hgramId) { + auto id = hgramId.extract(); + Telemetry::Accumulate(id, aSample); + } + fog_custom_distribution_accumulate_single_sample(mId, aSample); +} + void CustomDistributionMetric::AccumulateSamplesSigned( const nsTArray& aSamples) const { auto hgramId = HistogramIdForMetric(mId); @@ -47,6 +56,16 @@ void CustomDistributionMetric::AccumulateSamplesSigned( fog_custom_distribution_accumulate_samples_signed(mId, &aSamples); } +void CustomDistributionMetric::AccumulateSingleSampleSigned( + int64_t aSample) const { + auto hgramId = HistogramIdForMetric(mId); + if (hgramId) { + auto id = hgramId.extract(); + Telemetry::Accumulate(id, aSample); + } + fog_custom_distribution_accumulate_single_sample_signed(mId, aSample); +} + Result, nsCString> CustomDistributionMetric::TestGetValue(const nsACString& aPingName) const { nsCString err; @@ -78,6 +97,10 @@ void GleanCustomDistribution::AccumulateSamples( mCustomDist.AccumulateSamplesSigned(aSamples); } +void GleanCustomDistribution::AccumulateSingleSample(const int64_t aSample) { + mCustomDist.AccumulateSingleSampleSigned(aSample); +} + void GleanCustomDistribution::TestGetValue( const nsACString& aPingName, dom::Nullable& aRetval, ErrorResult& aRv) { diff --git a/toolkit/components/glean/bindings/private/CustomDistribution.h b/toolkit/components/glean/bindings/private/CustomDistribution.h index 8227b024ad..8074a0542e 100644 --- a/toolkit/components/glean/bindings/private/CustomDistribution.h +++ b/toolkit/components/glean/bindings/private/CustomDistribution.h @@ -34,6 +34,13 @@ class CustomDistributionMetric { */ void AccumulateSamples(const nsTArray& aSamples) const; + /** + * Accumulates the provided sample in the metric. + * + * @param aSamples The sample to be recorded by the metric. + */ + void AccumulateSingleSample(uint64_t aSample) const; + /** * Accumulates the provided samples in the metric. * @@ -45,6 +52,14 @@ class CustomDistributionMetric { */ void AccumulateSamplesSigned(const nsTArray& aSamples) const; + /** + * Accumulates the provided sample in the metric. + * + * @param aSamples The signed integer sample to be recorded by the + * metric. + */ + void AccumulateSingleSampleSigned(int64_t aSample) const; + /** * **Test-only API** * @@ -80,6 +95,8 @@ class GleanCustomDistribution final : public GleanMetric { void AccumulateSamples(const dom::Sequence& aSamples); + void AccumulateSingleSample(const int64_t aSample); + void TestGetValue(const nsACString& aPingName, dom::Nullable& aRetval, ErrorResult& aRv); diff --git a/toolkit/components/glean/bindings/private/DistributionData.h b/toolkit/components/glean/bindings/private/DistributionData.h index fb9bba720e..782fe17c98 100644 --- a/toolkit/components/glean/bindings/private/DistributionData.h +++ b/toolkit/components/glean/bindings/private/DistributionData.h @@ -27,6 +27,28 @@ struct DistributionData final { this->values.InsertOrUpdate(aBuckets[i], aCounts[i]); } } + + friend std::ostream& operator<<(std::ostream& aStream, + const DistributionData& aDist) { + aStream << "DistributionData("; + aStream << "sum=" << aDist.sum << ", "; + aStream << "count=" << aDist.count << ", "; + aStream << "values={"; + bool first = true; + for (const auto& entry : aDist.values) { + if (!first) { + aStream << ", "; + } + first = false; + + const uint64_t bucket = entry.GetKey(); + const uint64_t count = entry.GetData(); + aStream << bucket << "=" << count; + } + aStream << "}"; + aStream << ")"; + return aStream; + } }; } // namespace mozilla::glean diff --git a/toolkit/components/glean/bindings/private/Labeled.cpp b/toolkit/components/glean/bindings/private/Labeled.cpp index 23527708e0..1af9b870ae 100644 --- a/toolkit/components/glean/bindings/private/Labeled.cpp +++ b/toolkit/components/glean/bindings/private/Labeled.cpp @@ -31,7 +31,7 @@ already_AddRefed GleanLabeled::NamedGetter(const nsAString& aName, auto mirrorId = ScalarIdForMetric(mId); if (mirrorId) { - GetLabeledMirrorLock().apply([&](auto& lock) { + GetLabeledMirrorLock().apply([&](const auto& lock) { auto tuple = std::make_tuple( mirrorId.extract(), nsString(aName)); lock.ref()->InsertOrUpdate(submetricId, std::move(tuple)); diff --git a/toolkit/components/glean/bindings/private/Labeled.h b/toolkit/components/glean/bindings/private/Labeled.h index 65e31bd2bd..0e3aafba05 100644 --- a/toolkit/components/glean/bindings/private/Labeled.h +++ b/toolkit/components/glean/bindings/private/Labeled.h @@ -60,7 +60,7 @@ class Labeled { static inline void UpdateLabeledMirror(Telemetry::ScalarID aMirrorId, uint32_t aSubmetricId, const nsACString& aLabel) { - GetLabeledMirrorLock().apply([&](auto& lock) { + GetLabeledMirrorLock().apply([&](const auto& lock) { auto tuple = std::make_tuple( std::move(aMirrorId), NS_ConvertUTF8toUTF16(aLabel)); lock.ref()->InsertOrUpdate(aSubmetricId, std::move(tuple)); diff --git a/toolkit/components/glean/bindings/private/Ping.cpp b/toolkit/components/glean/bindings/private/Ping.cpp index 19f4fb5f77..8dbb316128 100644 --- a/toolkit/components/glean/bindings/private/Ping.cpp +++ b/toolkit/components/glean/bindings/private/Ping.cpp @@ -42,7 +42,7 @@ void Ping::Submit(const nsACString& aReason) const { { auto callback = Maybe(); GetCallbackMapLock().apply( - [&](auto& lock) { callback = lock.ref()->Extract(mId); }); + [&](const auto& lock) { callback = lock.ref()->Extract(mId); }); // Calling the callback outside of the lock allows it to register a new // callback itself. if (callback) { @@ -55,7 +55,7 @@ void Ping::Submit(const nsACString& aReason) const { void Ping::TestBeforeNextSubmit(PingTestCallback&& aCallback) const { { GetCallbackMapLock().apply( - [&](auto& lock) { lock.ref()->InsertOrUpdate(mId, aCallback); }); + [&](const auto& lock) { lock.ref()->InsertOrUpdate(mId, aCallback); }); } } diff --git a/toolkit/components/glean/bindings/private/Timespan.cpp b/toolkit/components/glean/bindings/private/Timespan.cpp index 2ab1f0dbba..7f154152eb 100644 --- a/toolkit/components/glean/bindings/private/Timespan.cpp +++ b/toolkit/components/glean/bindings/private/Timespan.cpp @@ -93,7 +93,7 @@ void TimespanMetric::Start() const { auto optScalarId = ScalarIdForMetric(mId); if (optScalarId) { auto scalarId = optScalarId.extract(); - GetTimesToStartsLock().apply([&](auto& lock) { + GetTimesToStartsLock().apply([&](const auto& lock) { (void)NS_WARN_IF(lock.ref()->Remove(scalarId)); lock.ref()->InsertOrUpdate(scalarId, TimeStamp::Now()); }); @@ -105,7 +105,7 @@ void TimespanMetric::Stop() const { auto optScalarId = ScalarIdForMetric(mId); if (optScalarId) { auto scalarId = optScalarId.extract(); - GetTimesToStartsLock().apply([&](auto& lock) { + GetTimesToStartsLock().apply([&](const auto& lock) { auto optStart = lock.ref()->Extract(scalarId); if (!NS_WARN_IF(!optStart)) { double delta = (TimeStamp::Now() - optStart.extract()).ToMilliseconds(); @@ -127,7 +127,7 @@ void TimespanMetric::Cancel() const { if (optScalarId) { auto scalarId = optScalarId.extract(); GetTimesToStartsLock().apply( - [&](auto& lock) { lock.ref()->Remove(scalarId); }); + [&](const auto& lock) { lock.ref()->Remove(scalarId); }); } fog_timespan_cancel(mId); } diff --git a/toolkit/components/glean/bindings/private/TimingDistribution.cpp b/toolkit/components/glean/bindings/private/TimingDistribution.cpp index 036db5f9db..7273d3fd2f 100644 --- a/toolkit/components/glean/bindings/private/TimingDistribution.cpp +++ b/toolkit/components/glean/bindings/private/TimingDistribution.cpp @@ -106,7 +106,7 @@ extern "C" NS_EXPORT void GIFFT_TimingDistributionStart( uint32_t aMetricId, mozilla::glean::TimerId aTimerId) { auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId); if (mirrorId) { - mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) { + mozilla::glean::GetTimerIdToStartsLock().apply([&](const auto& lock) { 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. @@ -121,7 +121,7 @@ extern "C" NS_EXPORT void GIFFT_TimingDistributionStopAndAccumulate( uint32_t aMetricId, mozilla::glean::TimerId aTimerId) { auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId); if (mirrorId) { - mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) { + mozilla::glean::GetTimerIdToStartsLock().apply([&](const auto& lock) { 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 @@ -147,7 +147,7 @@ extern "C" NS_EXPORT void GIFFT_TimingDistributionCancel( uint32_t aMetricId, mozilla::glean::TimerId aTimerId) { auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId); if (mirrorId) { - mozilla::glean::GetTimerIdToStartsLock().apply([&](auto& lock) { + mozilla::glean::GetTimerIdToStartsLock().apply([&](const auto& lock) { // The timer might not be in the map to be removed if it's already been // cancelled or stop_and_accumulate'd. auto tuple = mozilla::glean::MetricTimerTuple{aMetricId, aTimerId}; @@ -172,8 +172,20 @@ void TimingDistributionMetric::StopAndAccumulate(const TimerId&& aId) const { // type. void TimingDistributionMetric::AccumulateRawDuration( const TimeDuration& aDuration) const { + // `* 1000.0` is an acceptable overflow risk as durations are unlikely to be + // on the order of (-)10^282 years. + double durationNs = aDuration.ToMicroseconds() * 1000.0; + double roundedDurationNs = std::round(durationNs); + if (MOZ_UNLIKELY( + roundedDurationNs < + static_cast(std::numeric_limits::min()) || + roundedDurationNs > + static_cast(std::numeric_limits::max()))) { + // TODO(bug 1691073): Instrument this error. + return; + } fog_timing_distribution_accumulate_raw_nanos( - mId, uint64_t(aDuration.ToMicroseconds() * 1000.00)); + mId, static_cast(roundedDurationNs)); } void TimingDistributionMetric::Cancel(const TimerId&& aId) const { -- cgit v1.2.3