Bug 1751448 Expand one metric type's GIFFT to Rust r=janerik,bas

Differential Revision: https://phabricator.services.mozilla.com/D136664
This commit is contained in:
Chris H-C 2022-02-16 19:30:14 +00:00
Родитель 530503798f
Коммит 08d9d265ac
2 изменённых файлов: 69 добавлений и 25 удалений

Просмотреть файл

@ -24,7 +24,7 @@ pub enum TimingDistributionMetric {
Parent {
/// The metric's ID.
///
/// **TEST-ONLY** - Do not use unless gated with `#[cfg(test)]`.
/// No longer test-only, is also used for GIFFT.
id: MetricId,
inner: glean::private::TimingDistributionMetric,
},
@ -95,7 +95,17 @@ impl TimingDistribution for TimingDistributionMetric {
/// A unique [`TimerId`] for the new timer.
fn start(&self) -> TimerId {
match self {
TimingDistributionMetric::Parent { inner, .. } => inner.start(),
TimingDistributionMetric::Parent { id, inner } => {
let timer_id = inner.start();
extern "C" {
fn GIFFT_TimingDistributionStart(metric_id: u32, timer_id: u64);
}
// SAFETY: using only primitives, no return value.
unsafe {
GIFFT_TimingDistributionStart(id.0, timer_id);
}
timer_id
}
TimingDistributionMetric::Child(c) => {
// There is no glean-core on this process to give us a TimerId,
// so we'll have to make our own and do our own bookkeeping.
@ -129,7 +139,17 @@ impl TimingDistribution for TimingDistributionMetric {
/// same timespan metric.
fn stop_and_accumulate(&self, id: TimerId) {
match self {
TimingDistributionMetric::Parent { inner, .. } => {
TimingDistributionMetric::Parent {
id: metric_id,
inner,
} => {
extern "C" {
fn GIFFT_TimingDistributionStopAndAccumulate(metric_id: u32, timer_id: u64);
}
// SAFETY: using only primitives, no return value.
unsafe {
GIFFT_TimingDistributionStopAndAccumulate(metric_id.0, id);
}
inner.stop_and_accumulate(id);
}
TimingDistributionMetric::Child(c) => {
@ -179,7 +199,17 @@ impl TimingDistribution for TimingDistributionMetric {
/// same timing distribution metric.
fn cancel(&self, id: TimerId) {
match self {
TimingDistributionMetric::Parent { inner, .. } => {
TimingDistributionMetric::Parent {
id: metric_id,
inner,
} => {
extern "C" {
fn GIFFT_TimingDistributionCancel(metric_id: u32, timer_id: u64);
}
// SAFETY: using only primitives, no return value.
unsafe {
GIFFT_TimingDistributionCancel(metric_id.0, id);
}
inner.cancel(id);
}
TimingDistributionMetric::Child(c) => {

Просмотреть файл

@ -18,39 +18,53 @@
#include "nsString.h"
#include "js/PropertyAndElement.h" // JS_DefineProperty
// Called from within FOG's Rust impl.
extern "C" NS_EXPORT void GIFFT_TimingDistributionStart(
uint32_t aMetricId, mozilla::glean::TimerId aTimerId) {
auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
if (mirrorId) {
auto lock = mozilla::glean::GetTimerIdToStartsLock();
(void)NS_WARN_IF(lock.ref()->Remove(aTimerId));
lock.ref()->InsertOrUpdate(aTimerId, mozilla::TimeStamp::Now());
}
}
// Called from within FOG's Rust impl.
extern "C" NS_EXPORT void GIFFT_TimingDistributionStopAndAccumulate(
uint32_t aMetricId, mozilla::glean::TimerId aTimerId) {
auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
if (mirrorId) {
auto lock = mozilla::glean::GetTimerIdToStartsLock();
auto optStart = lock.ref()->Extract(aTimerId);
if (!NS_WARN_IF(!optStart)) {
AccumulateTimeDelta(mirrorId.extract(), optStart.extract());
}
}
}
// Called from within FOG's Rust impl.
extern "C" NS_EXPORT void GIFFT_TimingDistributionCancel(
uint32_t aMetricId, mozilla::glean::TimerId aTimerId) {
auto mirrorId = mozilla::glean::HistogramIdForMetric(aMetricId);
if (mirrorId) {
auto lock = mozilla::glean::GetTimerIdToStartsLock();
(void)NS_WARN_IF(!lock.ref()->Remove(aTimerId));
}
}
namespace mozilla::glean {
namespace impl {
TimerId TimingDistributionMetric::Start() const {
TimerId id = fog_timing_distribution_start(mId);
auto mirrorId = HistogramIdForMetric(mId);
if (mirrorId) {
auto lock = GetTimerIdToStartsLock();
(void)NS_WARN_IF(lock.ref()->Remove(id));
lock.ref()->InsertOrUpdate(id, TimeStamp::Now());
}
return id;
return fog_timing_distribution_start(mId);
}
void TimingDistributionMetric::StopAndAccumulate(const TimerId&& aId) const {
auto mirrorId = HistogramIdForMetric(mId);
if (mirrorId) {
auto lock = GetTimerIdToStartsLock();
auto optStart = lock.ref()->Extract(aId);
if (!NS_WARN_IF(!optStart)) {
AccumulateTimeDelta(mirrorId.extract(), optStart.extract());
}
}
fog_timing_distribution_stop_and_accumulate(mId, aId);
}
void TimingDistributionMetric::Cancel(const TimerId&& aId) const {
auto mirrorId = HistogramIdForMetric(mId);
if (mirrorId) {
auto lock = GetTimerIdToStartsLock();
(void)NS_WARN_IF(!lock.ref()->Remove(aId));
}
fog_timing_distribution_cancel(mId, aId);
}