Skip to content

Commit 338d525

Browse files
committed
AgentSignal -> TimerSignal, move to timer/mod.rs
1 parent 986c769 commit 338d525

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

src/pyroscope.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,12 @@ use crate::{
1111
backends::{pprof::Pprof, Backend},
1212
error::Result,
1313
session::{Session, SessionManager, SessionSignal},
14-
timer::Timer,
14+
timer::{Timer, TimerSignal},
1515
};
1616

1717
const LOG_TAG: &str = "Pyroscope::Agent";
1818

1919

20-
/// A signal sent from the agent to the timer.
21-
///
22-
/// Either schedules another wake-up, or asks
23-
/// the timer thread to terminate.
24-
#[derive(Debug, Clone, Copy)]
25-
pub enum AgentSignal {
26-
// Thread termination was requested.
27-
Terminate,
28-
// When to take the next snapshot using the `Backend`.
29-
NextSnapshot(u64),
30-
}
31-
32-
impl std::fmt::Display for AgentSignal {
33-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
34-
match self {
35-
Self::Terminate => write!(f, "Terminate"),
36-
Self::NextSnapshot(when) => write!(f, "NextSnapshot({})", when),
37-
}
38-
}
39-
}
40-
4120
/// Pyroscope Agent Configuration. This is the configuration that is passed to the agent.
4221
/// # Example
4322
/// ```
@@ -275,7 +254,7 @@ impl PyroscopeAgentBuilder {
275254
pub struct PyroscopeAgent {
276255
timer: Timer,
277256
session_manager: SessionManager,
278-
tx: Option<Sender<AgentSignal>>,
257+
tx: Option<Sender<TimerSignal>>,
279258
handle: Option<JoinHandle<Result<()>>>,
280259
running: Arc<(Mutex<bool>, Condvar)>,
281260

@@ -371,7 +350,7 @@ impl PyroscopeAgent {
371350

372351
while let Ok(signal) = rx.recv() {
373352
match signal {
374-
AgentSignal::NextSnapshot(until) => {
353+
TimerSignal::NextSnapshot(until) => {
375354
log::trace!(target: LOG_TAG, "Sending session {}", until);
376355

377356
// Generate report from backend
@@ -384,7 +363,7 @@ impl PyroscopeAgent {
384363
report,
385364
)?))?
386365
}
387-
AgentSignal::Terminate => {
366+
TimerSignal::Terminate => {
388367
log::trace!(target: LOG_TAG, "Session Killed");
389368

390369
let (lock, cvar) = &*pair;
@@ -420,8 +399,8 @@ impl PyroscopeAgent {
420399
// get tx and send termination signal
421400
if let Some(sender) = self.tx.take() {
422401
// best effort
423-
let _ = sender.send(AgentSignal::NextSnapshot(0));
424-
sender.send(AgentSignal::Terminate)?;
402+
let _ = sender.send(TimerSignal::NextSnapshot(0));
403+
sender.send(TimerSignal::Terminate)?;
425404
} else {
426405
log::error!("PyroscopeAgent - Missing sender")
427406
}

src/timer/epoll.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// https://www.apache.org/licenses/LICENSE-2.0>. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7-
use crate::pyroscope::AgentSignal;
7+
use super::TimerSignal;
88
use crate::utils::check_err;
99
use crate::utils::get_time_range;
1010
use crate::PyroscopeError;
@@ -27,7 +27,7 @@ use std::{thread, thread::JoinHandle};
2727
#[derive(Debug)]
2828
pub struct Timer {
2929
/// A vector to store listeners (mpsc::Sender)
30-
txs: Arc<Mutex<Vec<Sender<AgentSignal>>>>,
30+
txs: Arc<Mutex<Vec<Sender<TimerSignal>>>>,
3131

3232
/// Thread handle
3333
pub handle: Option<JoinHandle<Result<()>>>,
@@ -61,7 +61,7 @@ impl Timer {
6161
Timer::epoll_wait(timer_fd, epoll_fd)?;
6262

6363
// Get the current time range
64-
let from = AgentSignal::NextSnapshot(get_time_range(0)?.from);
64+
let from = TimerSignal::NextSnapshot(get_time_range(0)?.from);
6565

6666
// Iterate through Senders
6767
txs.lock()?.iter().for_each(|tx| {
@@ -165,7 +165,7 @@ impl Timer {
165165
///
166166
/// Timer will dispatch an event with the timestamp of the current instant,
167167
/// every 10th second to all attached senders
168-
pub fn attach_listener(&mut self, tx: Sender<AgentSignal>) -> Result<()> {
168+
pub fn attach_listener(&mut self, tx: Sender<TimerSignal>) -> Result<()> {
169169
// Push Sender to a Vector of Sender(s)
170170
let txs = Arc::clone(&self.txs);
171171
txs.lock()?.push(tx);

src/timer/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@
44
// https://www.apache.org/licenses/LICENSE-2.0>. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7+
8+
/// A signal sent to the timer.
9+
///
10+
/// Either schedules another wake-up, or asks
11+
/// the timer thread to terminate.
12+
#[derive(Debug, Clone, Copy)]
13+
pub enum TimerSignal {
14+
// Thread termination was requested.
15+
Terminate,
16+
// When to take the next snapshot using the `Backend`.
17+
NextSnapshot(u64),
18+
}
19+
20+
impl std::fmt::Display for TimerSignal {
21+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
22+
match self {
23+
Self::Terminate => write!(f, "Terminate"),
24+
Self::NextSnapshot(when) => write!(f, "NextSnapshot({})", when),
25+
}
26+
}
27+
}
728
// Possibly: ios, netbsd, openbsd, freebsd
829
#[cfg(target_os = "macos")] pub mod kqueue;
930
#[cfg(target_os = "macos")] pub use kqueue::Timer;

tests/timer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pyroscope::{pyroscope::AgentSignal, timer::Timer};
1+
use pyroscope::timer::{TimerSignal, Timer};
22
use assert_matches::assert_matches;
33

44
#[test]
@@ -12,7 +12,7 @@ fn test_timer() {
1212

1313
// Wait for event (should arrive in 10s)
1414
let planned = rx.recv().unwrap();
15-
assert_matches!(planned, AgentSignal::NextSnapshot(planned) => {
15+
assert_matches!(planned, TimerSignal::NextSnapshot(planned) => {
1616
// Get current time
1717
let now = std::time::SystemTime::now()
1818
.duration_since(std::time::UNIX_EPOCH)

0 commit comments

Comments
 (0)