From f9b0bf0b5fed09ead72259f60f7edf2f37a9720d Mon Sep 17 00:00:00 2001 From: David Venhoff Date: Fri, 12 Sep 2025 11:53:11 +0200 Subject: [PATCH] Correct for errors in the current tsc by using cyc --- pt-dump-decoder/src/lib.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pt-dump-decoder/src/lib.rs b/pt-dump-decoder/src/lib.rs index 3f719f7..5f8e0bf 100644 --- a/pt-dump-decoder/src/lib.rs +++ b/pt-dump-decoder/src/lib.rs @@ -49,10 +49,17 @@ pub fn analyze_dump( let f_bus = cpuid!(0x16).ecx as f64 * 1_000_000.0; let mut current_cbr = 0u8; + // Keeps track of the cycles since the last tsc for higher accuracy + let mut duration_since_last_tsc = Duration::ZERO; // Records the first tsc after the ptwrite (42) message - let mut first_tsc_main = None::; + let mut first_tsc_main = None::; // Records the first tsc after the ptwrite (43) message - let mut first_tsc_post_run = None::; + let mut first_tsc_post_run = None::; + let f_tsc = CpuId::new() + .get_tsc_info() + .unwrap() + .tsc_frequency() + .unwrap(); let mut seen_cbrs = HashMap::::new(); @@ -67,17 +74,22 @@ pub fn analyze_dump( } let f_core = f_bus * current_cbr as f64; let duration = Duration::from_secs_f64(cyc.value() as f64 / f_core); + duration_since_last_tsc += duration; segments.last_mut().unwrap().1 += duration; } Ok(Packet::Tsc(tsc)) => { + let tsc_seconds = tsc.tsc() as f64 / f_tsc as f64; + let corrected_tsc = tsc_seconds + duration_since_last_tsc.as_secs_f64(); + duration_since_last_tsc = Duration::ZERO; + if let [.., (Scope::Main, _)] = segments.as_slice() && first_tsc_main.is_none() { - first_tsc_main = Some(tsc.tsc()); + first_tsc_main = Some(corrected_tsc); } else if let [.., (Scope::PostRun, _)] = segments.as_slice() && first_tsc_post_run.is_none() { - first_tsc_post_run = Some(tsc.tsc()); + first_tsc_post_run = Some(corrected_tsc); } } Ok(Packet::Cbr(cbr)) => { @@ -106,12 +118,7 @@ pub fn analyze_dump( let duration_total = segments.iter().map(|(_, duration)| *duration).sum(); let main_duration = duration_in_mode(&segments, Scope::Main); - let f_tsc = CpuId::new() - .get_tsc_info() - .unwrap() - .tsc_frequency() - .unwrap(); - let total_seconds_tsc = (first_tsc_post_run.unwrap() - first_tsc_main.unwrap()) as f64 / f_tsc as f64; + let total_seconds_tsc = first_tsc_post_run.unwrap() - first_tsc_main.unwrap(); Ok(AnalyzeData { packets: total,