Skip to content

*: use AtomicU64 in value #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ rust:
env:
- RUSTFLAGS=-Dwarnings

script:
- cargo test

matrix:
include:
- rust: nightly
env:
- FEATURE="--features dev"
- RUSTFLAGS=-Dwarnings

script:
- cargo test $FEATURE
script:
- cargo test --features="dev nightly"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why two cargo test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to test nigltly feature too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so why not test "dev nightly" directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default feature uses Mutex<f64>, the nightly uses AtomicF64.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include = [
[features]
default = []
dev = ["clippy"]
nightly = []

[[bench]]
name = "benches"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test:

dev: format
cargo build --features dev
cargo test --features dev -- --nocapture
cargo test --features="nightly dev" -- --nocapture

bench: format
cargo bench --features dev -- --nocapture
Expand Down
113 changes: 113 additions & 0 deletions src/atomicf64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2014 The Prometheus Authors
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

#[cfg(not(feature = "nightly"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put these into a submodule and then reexport it like:

#[cfg(feature = "nightly")]
mod imp {
...
}

#[cfg(not(feature = "nightly"))]
mod imp {
...
}

pub use imp::AtomicF64;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

pub use self::rwlock::RwlockF64 as AtomicF64;
#[cfg(feature = "nightly")]
pub use self::atomic::AtomicF64;

#[cfg(not(feature = "nightly"))]
mod rwlock {
use std::sync::RwLock;

pub struct RwlockF64 {
inner: RwLock<f64>,
}

impl RwlockF64 {
pub fn new(val: f64) -> RwlockF64 {
RwlockF64 { inner: RwLock::new(val) }
}

#[inline]
pub fn set(&self, val: f64) {
*self.inner.write().unwrap() = val;
}

#[inline]
pub fn get(&self) -> f64 {
*self.inner.read().unwrap()
}

#[inline]
pub fn inc_by(&self, delta: f64) {
*self.inner.write().unwrap() += delta;
}
}
}

#[cfg(feature = "nightly")]
mod atomic {
use std::sync::atomic::{AtomicU64, Ordering};
use std::mem::transmute;

pub struct AtomicF64 {
inner: AtomicU64,
}

impl AtomicF64 {
pub fn new(val: f64) -> AtomicF64 {
AtomicF64 { inner: AtomicU64::new(f64_to_u64(val)) }
}

#[inline]
pub fn get(&self) -> f64 {
u64_to_f64(self.inner.load(Ordering::Relaxed))
}

#[inline]
pub fn set(&self, val: f64) {
self.inner.store(f64_to_u64(val), Ordering::Release)
}

#[inline]
pub fn inc_by(&self, delta: f64) {
loop {
let current = self.inner.load(Ordering::Acquire);
let new = u64_to_f64(current) + delta;
let swapped = self.inner
.compare_and_swap(current, f64_to_u64(new), Ordering::Release);
if swapped == current {
return;
}
}
}
}

fn u64_to_f64(val: u64) -> f64 {
unsafe { transmute(val) }
}

fn f64_to_u64(val: f64) -> u64 {
unsafe { transmute(val) }
}

}

#[cfg(test)]
mod test {
use std::f64::consts::PI;
use std::f64::{self, EPSILON};

use super::*;

#[test]
fn test_atomicf64() {
let table: Vec<f64> = vec![0.0, 1.0, PI, f64::MIN, f64::MAX];

for f in table {
assert!((f - AtomicF64::new(f).get()).abs() < EPSILON);
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#![cfg_attr(feature="dev", feature(plugin))]
#![cfg_attr(feature="dev", plugin(clippy))]
#![cfg_attr(feature="nightly", feature(integer_atomics))]

#[macro_use]
extern crate quick_error;
Expand Down Expand Up @@ -41,6 +42,7 @@ mod registry;
mod vec;
mod histogram;
mod push;
mod atomicf64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is atomicf64?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, my fault, now it's here, please review.


// Mods

Expand Down
24 changes: 11 additions & 13 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::RwLock;

use protobuf::RepeatedField;

use proto::{LabelPair, Metric, Counter, Gauge, Untyped, MetricFamily, MetricType};
use desc::Desc;
use errors::{Result, Error};
use atomicf64::AtomicF64;

/// `ValueType` is an enumeration of metric types that represent a simple value
/// for `Counter`, `Gauge`, and `Untyped`.
Expand All @@ -45,8 +44,7 @@ impl ValueType {
/// `Counter`, `Gauge`, and `Untyped`.
pub struct Value {
pub desc: Desc,
// TODO: like prometheus client go, use atomic u64.
pub val: RwLock<f64>,
pub val: AtomicF64,
pub val_type: ValueType,
pub label_pairs: Vec<LabelPair>,
}
Expand All @@ -66,20 +64,25 @@ impl Value {

Ok(Value {
desc: desc,
val: RwLock::new(val),
val: AtomicF64::new(val),
val_type: value_type,
label_pairs: label_pairs,
})
}

#[inline]
pub fn get(&self) -> f64 {
self.val.get()
}

#[inline]
pub fn set(&self, val: f64) {
*self.val.write().unwrap() = val;
self.val.set(val);
}

#[inline]
pub fn get(&self) -> f64 {
*self.val.read().unwrap()
pub fn inc_by(&self, val: f64) {
self.val.inc_by(val);
}

#[inline]
Expand All @@ -92,11 +95,6 @@ impl Value {
self.inc_by(-1.0);
}

#[inline]
pub fn inc_by(&self, val: f64) {
*self.val.write().unwrap() += val;
}

#[inline]
pub fn dec_by(&self, val: f64) {
self.inc_by(val * -1.0)
Expand Down