Skip to content

Commit 83de0ee

Browse files
committed
update kqueue, use Duration directly
1 parent fda4139 commit 83de0ee

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

src/pyroscope.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
Arc, Condvar, Mutex,
66
},
77
thread::JoinHandle,
8+
time::Duration,
89
};
910

1011
use crate::{
@@ -33,7 +34,7 @@ pub struct PyroscopeConfig {
3334
/// Sample rate used in Hz
3435
pub sample_rate: i32,
3536
/// How long to accumulate data for before sending it upstream
36-
pub accumulation_cycle: std::time::Duration,
37+
pub accumulation_cycle: Duration,
3738
// TODO
3839
// log_level
3940
// auth_token
@@ -54,7 +55,7 @@ impl PyroscopeConfig {
5455
application_name: application_name.as_ref().to_owned(),
5556
tags: HashMap::new(),
5657
sample_rate: 100i32,
57-
accumulation_cycle: std::time::Duration::from_secs(10),
58+
accumulation_cycle: Duration::from_secs(10),
5859
}
5960
}
6061

@@ -86,7 +87,7 @@ impl PyroscopeConfig {
8687
/// # Ok(())
8788
/// # }
8889
/// ```
89-
pub fn accumulation_cycle(self, accumulation_cycle: std::time::Duration) -> Self {
90+
pub fn accumulation_cycle(self, accumulation_cycle: Duration) -> Self {
9091
Self {
9192
accumulation_cycle,
9293
..self
@@ -198,7 +199,7 @@ impl PyroscopeAgentBuilder {
198199
/// # Ok(())
199200
/// # }
200201
/// ```
201-
pub fn accumulation_cycle(self, acc_cycle: impl Into<std::time::Duration>) -> Self {
202+
pub fn accumulation_cycle(self, acc_cycle: impl Into<Duration>) -> Self {
202203
Self {
203204
config: self.config.accumulation_cycle(acc_cycle.into()),
204205
..self

src/session.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
sync::mpsc::{sync_channel, Receiver, SyncSender},
3-
thread,
4-
thread::JoinHandle,
3+
thread::{self, JoinHandle},
4+
time::Duration,
55
};
66

77
use crate::pyroscope::PyroscopeConfig;
@@ -161,7 +161,7 @@ impl Session {
161161
("spyName", "pyroscope-rs"),
162162
])
163163
.body(self.report)
164-
.timeout(std::time::Duration::from_secs(10))
164+
.timeout(Duration::from_secs(10))
165165
.send()?;
166166

167167
Ok(())

src/timer/epoll.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ use std::sync::{
1414
mpsc::{channel, Sender},
1515
Arc, Mutex,
1616
};
17-
use std::{thread, thread::JoinHandle};
17+
use std::{
18+
time::Duration,
19+
thread::{self, JoinHandle},
20+
};
1821

1922
/// A thread that sends a notification every 10th second
2023
///
@@ -35,7 +38,7 @@ pub struct Timer {
3538

3639
impl Timer {
3740
/// Initialize Timer and run a thread to send events to attached listeners
38-
pub fn initialize(cycle: std::time::Duration) -> Result<Self> {
41+
pub fn initialize(cycle: Duration) -> Result<Self> {
3942
let txs = Arc::new(Mutex::new(Vec::new()));
4043

4144
// Add a dummy tx so the below thread does not terminate early
@@ -76,7 +79,7 @@ impl Timer {
7679
}
7780

7881
/// create and set a timer file descriptor
79-
fn set_timerfd(cycle: std::time::Duration) -> Result<libc::c_int> {
82+
fn set_timerfd(cycle: Duration) -> Result<libc::c_int> {
8083
// Set the timer to use the system time.
8184
let clockid: libc::clockid_t = libc::CLOCK_REALTIME;
8285
// Non-blocking file descriptor

src/timer/kqueue.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use std::sync::{
1212
mpsc::{channel, Receiver, Sender},
1313
Arc, Mutex,
1414
};
15-
use std::{thread, thread::JoinHandle};
15+
use std::{
16+
time::Duration,
17+
thread::{self, JoinHandle},
18+
};
1619

1720
/// A thread that sends a notification every 10th second
1821
///
@@ -33,7 +36,7 @@ pub struct Timer {
3336

3437
impl Timer {
3538
/// Initialize Timer and run a thread to send events to attached listeners
36-
pub fn initialize(self) -> Result<Self> {
39+
pub fn initialize(self, accumulation_cycle: Duration) -> Result<Self> {
3740
let txs = Arc::clone(&self.txs);
3841

3942
// Add Default tx
@@ -44,11 +47,11 @@ impl Timer {
4447

4548
let handle = Some(thread::spawn(move || {
4649
// Wait for initial expiration
47-
let initial_event = Timer::register_initial_expiration(kqueue)?;
50+
let initial_event = Timer::register_initial_expiration(kqueue, Duration::from_millis(0))?;
4851
Timer::wait_event(kqueue, [initial_event].as_mut_ptr())?;
4952

5053
// Register loop event
51-
let loop_event = Timer::register_loop_expiration(kqueue)?;
54+
let loop_event = Timer::register_loop_expiration(kqueue, accumulation_cycle)?;
5255

5356
// Loop 10s
5457
loop {
@@ -132,14 +135,14 @@ impl Timer {
132135
}
133136

134137
/// Register a loop expiration event
135-
fn register_loop_expiration(kqueue: i32) -> Result<libc::kevent> {
138+
fn register_loop_expiration(kqueue: i32, duration: Duration) -> Result<libc::kevent> {
136139
let loop_event = libc::kevent {
137140
ident: 1,
138141
filter: libc::EVFILT_TIMER,
139142
flags: libc::EV_ADD | libc::EV_ENABLE,
140143
fflags: 0,
141-
data: 10000,
142-
udata: 0 as *mut libc::c_void,
144+
data: duration.as_millis(),
145+
udata: std::ptr::null(),
143146
};
144147

145148
// add loop event

tests/timer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use assert_matches::assert_matches;
22
use pyroscope::timer::{Timer, TimerSignal};
3+
use std::time::Duration;
34

45
#[test]
56
fn test_timer() {
67
// Initialize Timer
7-
let mut timer = Timer::initialize(std::time::Duration::from_secs(10)).unwrap();
8+
let mut timer = Timer::initialize(Duration::from_secs(10)).unwrap();
89

910
// Attach a listener
1011
let (tx, rx) = std::sync::mpsc::channel();

0 commit comments

Comments
 (0)