Skip to content

Commit bc25dd1

Browse files
chitoku-kmxinden
andauthored
src/metrics/: Don't use AtomicU64 on unsupported platforms (#42)
Use `AtomicU32` on platforms that don't support `AtomicU64`. Co-authored-by: Max Inden <mail@max-inden.de>
1 parent 16aa166 commit bc25dd1

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ jobs:
144144
matrix:
145145
target:
146146
- armv7-unknown-linux-gnueabihf
147+
- mipsel-unknown-linux-gnu
148+
- powerpc-unknown-linux-gnu
147149
- powerpc64-unknown-linux-gnu
148150
steps:
149151
- uses: actions/checkout@v2

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
- Expose `Encoder` methods. See [PR 41].
1212

13+
### Changed
14+
15+
- Use `AtomicU32` on platforms that don't support `AtomicU64`. See [PR 42].
16+
1317
[PR 41]: https://github.com/prometheus/client_rust/pull/41
18+
[PR 42]: https://github.com/prometheus/client_rust/pull/42
1419

1520
## [0.15.0] - 2022-01-16
1621

src/metrics/counter.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
55
use super::{MetricType, TypedMetric};
66
use std::marker::PhantomData;
7-
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
7+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
8+
use std::sync::atomic::AtomicU64;
9+
use std::sync::atomic::{AtomicU32, Ordering};
810
use std::sync::Arc;
911

1012
/// Open Metrics [`Counter`] to measure discrete events.
@@ -36,11 +38,18 @@ use std::sync::Arc;
3638
/// counter.inc();
3739
/// let _value: f64 = counter.get();
3840
/// ```
41+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
3942
pub struct Counter<N = u64, A = AtomicU64> {
4043
value: Arc<A>,
4144
phantom: PhantomData<N>,
4245
}
4346

47+
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
48+
pub struct Counter<N = u32, A = AtomicU32> {
49+
value: Arc<A>,
50+
phantom: PhantomData<N>,
51+
}
52+
4453
impl<N, A> Clone for Counter<N, A> {
4554
fn clone(&self) -> Self {
4655
Self {
@@ -96,6 +105,7 @@ pub trait Atomic<N> {
96105
fn get(&self) -> N;
97106
}
98107

108+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
99109
impl Atomic<u64> for AtomicU64 {
100110
fn inc(&self) -> u64 {
101111
self.inc_by(1)
@@ -124,6 +134,7 @@ impl Atomic<u32> for AtomicU32 {
124134
}
125135
}
126136

137+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
127138
impl Atomic<f64> for AtomicU64 {
128139
fn inc(&self) -> f64 {
129140
self.inc_by(1.0)
@@ -165,6 +176,7 @@ mod tests {
165176
assert_eq!(1, counter.get());
166177
}
167178

179+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
168180
#[test]
169181
fn f64_stored_in_atomic_u64() {
170182
fn prop(fs: Vec<f64>) {

src/metrics/exemplar.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use super::counter::{self, Counter};
66
use super::histogram::Histogram;
77
use owning_ref::OwningRef;
88
use std::collections::HashMap;
9+
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
10+
use std::sync::atomic::AtomicU32;
11+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
912
use std::sync::atomic::AtomicU64;
1013
use std::sync::{Arc, RwLock, RwLockReadGuard};
1114

@@ -26,10 +29,16 @@ pub struct Exemplar<S, V> {
2629
/// counter_with_exemplar.inc_by(1, Some(vec![("user_id".to_string(), "42".to_string())]));
2730
/// let _value: (u64, _) = counter_with_exemplar.get();
2831
/// ```
32+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
2933
pub struct CounterWithExemplar<S, N = u64, A = AtomicU64> {
3034
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
3135
}
3236

37+
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
38+
pub struct CounterWithExemplar<S, N = u32, A = AtomicU32> {
39+
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
40+
}
41+
3342
impl<S, N, A> Clone for CounterWithExemplar<S, N, A> {
3443
fn clone(&self) -> Self {
3544
CounterWithExemplar {

src/metrics/gauge.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
55
use super::{MetricType, TypedMetric};
66
use std::marker::PhantomData;
7-
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
7+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
8+
use std::sync::atomic::AtomicU64;
9+
use std::sync::atomic::{AtomicU32, Ordering};
810
use std::sync::Arc;
911

1012
/// Open Metrics [`Gauge`] to record current measurements.
@@ -36,11 +38,18 @@ use std::sync::Arc;
3638
/// gauge.set(42.0);
3739
/// let _value: f64 = gauge.get();
3840
/// ```
41+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
3942
pub struct Gauge<N = u64, A = AtomicU64> {
4043
value: Arc<A>,
4144
phantom: PhantomData<N>,
4245
}
4346

47+
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
48+
pub struct Gauge<N = u32, A = AtomicU32> {
49+
value: Arc<A>,
50+
phantom: PhantomData<N>,
51+
}
52+
4453
impl<N, A> Clone for Gauge<N, A> {
4554
fn clone(&self) -> Self {
4655
Self {
@@ -113,6 +122,7 @@ pub trait Atomic<N> {
113122
fn get(&self) -> N;
114123
}
115124

125+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
116126
impl Atomic<u64> for AtomicU64 {
117127
fn inc(&self) -> u64 {
118128
self.inc_by(1)
@@ -165,6 +175,7 @@ impl Atomic<u32> for AtomicU32 {
165175
}
166176
}
167177

178+
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
168179
impl Atomic<f64> for AtomicU64 {
169180
fn inc(&self) -> f64 {
170181
self.inc_by(1.0)

0 commit comments

Comments
 (0)