Correct for errors in the current tsc by using cyc

This commit is contained in:
David Venhoff 2025-09-12 11:53:11 +02:00
parent 2732383465
commit f9b0bf0b5f

View File

@ -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::<u64>;
let mut first_tsc_main = None::<f64>;
// Records the first tsc after the ptwrite (43) message
let mut first_tsc_post_run = None::<u64>;
let mut first_tsc_post_run = None::<f64>;
let f_tsc = CpuId::new()
.get_tsc_info()
.unwrap()
.tsc_frequency()
.unwrap();
let mut seen_cbrs = HashMap::<u8, u64>::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,