Skip to content

Commit

Permalink
chore(portability): support all platforms without 64 bit atomics (pro…
Browse files Browse the repository at this point in the history
…metheus#203)

Notably, RISC-V 32 for ESP32-C3 chips.

Signed-off-by: Léo Gillot-Lamure <[email protected]>
Signed-off-by: Max Inden <[email protected]>
Co-authored-by: Max Inden <[email protected]>
  • Loading branch information
navaati and mxinden authored Jul 2, 2024
1 parent bf196d7 commit 4b78df1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.22.3] - unreleased

### Added

- Support all platforms with 32 bit atomics lacking 64 bit atomics.
See [PR 203].

[PR 203]: https://github.com/prometheus/client_rust/pull/203

## [0.22.2]

### Added
Expand Down
12 changes: 6 additions & 6 deletions src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::encoding::{EncodeMetric, MetricEncoder};

use super::{MetricType, TypedMetric};
use std::marker::PhantomData;
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -40,15 +40,15 @@ use std::sync::Arc;
/// counter.inc();
/// let _value: f64 = counter.get();
/// ```
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
#[derive(Debug)]
pub struct Counter<N = u64, A = AtomicU64> {
value: Arc<A>,
phantom: PhantomData<N>,
}

/// Open Metrics [`Counter`] to measure discrete events.
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
#[cfg(not(target_has_atomic = "64"))]
#[derive(Debug)]
pub struct Counter<N = u32, A = AtomicU32> {
value: Arc<A>,
Expand Down Expand Up @@ -114,7 +114,7 @@ pub trait Atomic<N> {
fn get(&self) -> N;
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
impl Atomic<u64> for AtomicU64 {
fn inc(&self) -> u64 {
self.inc_by(1)
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Atomic<u32> for AtomicU32 {
}
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
impl Atomic<f64> for AtomicU64 {
fn inc(&self) -> f64 {
self.inc_by(1.0)
Expand Down Expand Up @@ -231,7 +231,7 @@ mod tests {
assert_eq!(1, counter.get());
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
#[test]
fn f64_stored_in_atomic_u64() {
fn prop(fs: Vec<f64>) {
Expand Down
8 changes: 4 additions & 4 deletions src/metrics/exemplar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use super::histogram::Histogram;
use super::{MetricType, TypedMetric};
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use std::collections::HashMap;
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
#[cfg(not(target_has_atomic = "64"))]
use std::sync::atomic::AtomicU32;
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::AtomicU64;
use std::sync::Arc;

Expand Down Expand Up @@ -65,7 +65,7 @@ pub struct Exemplar<S, V> {
/// }),
/// );
/// ```
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
#[derive(Debug)]
pub struct CounterWithExemplar<S, N = u64, A = AtomicU64> {
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
Expand All @@ -77,7 +77,7 @@ impl<S> TypedMetric for CounterWithExemplar<S> {

/// Open Metrics [`Counter`] with an [`Exemplar`] to both measure discrete
/// events and track references to data outside of the metric set.
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
#[cfg(not(target_has_atomic = "64"))]
#[derive(Debug)]
pub struct CounterWithExemplar<S, N = u32, A = AtomicU32> {
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
Expand Down
10 changes: 5 additions & 5 deletions src/metrics/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::encoding::{EncodeGaugeValue, EncodeMetric, MetricEncoder};
use super::{MetricType, TypedMetric};
use std::marker::PhantomData;
use std::sync::atomic::{AtomicI32, AtomicU32, Ordering};
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
use std::sync::atomic::{AtomicI64, AtomicU64};
use std::sync::Arc;

Expand Down Expand Up @@ -40,15 +40,15 @@ use std::sync::Arc;
/// gauge.set(42.0);
/// let _value: f64 = gauge.get();
/// ```
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
#[derive(Debug)]
pub struct Gauge<N = i64, A = AtomicI64> {
value: Arc<A>,
phantom: PhantomData<N>,
}

/// Open Metrics [`Gauge`] to record current measurements.
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
#[cfg(not(target_has_atomic = "64"))]
#[derive(Debug)]
pub struct Gauge<N = i32, A = AtomicI32> {
value: Arc<A>,
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Atomic<u32> for AtomicU32 {
}
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
impl Atomic<i64> for AtomicI64 {
fn inc(&self) -> i64 {
self.inc_by(1)
Expand All @@ -213,7 +213,7 @@ impl Atomic<i64> for AtomicI64 {
}
}

#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
#[cfg(target_has_atomic = "64")]
impl Atomic<f64> for AtomicU64 {
fn inc(&self) -> f64 {
self.inc_by(1.0)
Expand Down

0 comments on commit 4b78df1

Please sign in to comment.