Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tmplt committed Nov 26, 2021
1 parent 703934c commit be50af2
Showing 1 changed file with 182 additions and 84 deletions.
266 changes: 182 additions & 84 deletions itm/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ where
current_baseline: DateTime,
}

#[cfg_attr(test, derive(Clone, Debug))]
struct Gts {
pub lower: Option<u64>,
pub upper: Option<u64>,
}
impl Gts {
const GTS2_SHIFT: usize = 26; // see (Appendix D4.2.5).
const GTS2_SHIFT: u32 = 26; // see (Appendix D4.2.5).

pub fn replace_lower(&mut self, new: u64) {
self.lower = match self.lower {
Expand All @@ -127,7 +128,12 @@ impl Gts {

pub fn merge(&self) -> Option<u64> {
if let (Some(lower), Some(upper)) = (self.lower, self.upper) {
Some((upper << Self::GTS2_SHIFT) | lower)
Some(
upper
.checked_shl(Self::GTS2_SHIFT)
.expect("GTS merge overflow")
| lower,
)
} else {
None
}
Expand Down Expand Up @@ -321,6 +327,7 @@ fn calc_offset(ts: u64, prescaler: Option<LocalTimestampOptions>, freq: u32) ->
let ticks = ts * prescale;
let seconds = (1_f64 / freq as f64) * ticks as f64;

// NOTE(ceil) ...
chrono::Duration::nanoseconds((seconds * 1e9).ceil() as i64)
}

Expand All @@ -330,6 +337,15 @@ mod timestamp_utils {

#[test]
fn gts() {
let mut gts = Gts {
lower: Some(1), // bit 1
upper: Some(1), // bit 26
};
assert_eq!(gts.merge(), Some(67108865));

gts.replace_lower(127);
assert_eq!(gts.merge(), Some(67108991));

let gts = Gts {
lower: None,
upper: None,
Expand Down Expand Up @@ -413,8 +429,8 @@ mod timestamps {
TimestampDataRelation::Sync,
),
(TracePacket::LocalTimestamp2 { ts }, Some(gts)) => (
false,
calc_offset(gts.merge().unwrap_or(0) + (ts as u64), None, FREQ),
true,
calc_offset(gts.merge().unwrap() + (ts as u64), None, FREQ),
TimestampDataRelation::Sync,
),
_ => panic!("???"),
Expand All @@ -434,8 +450,26 @@ mod timestamps {
}
}

fn is_sorted_increasing(timestamps: &[Timestamp]) -> bool {
let mut it = timestamps.iter();
let mut prev = None;
while let Some(curr) = it.next() {
if prev.is_none() {
continue;
}

if curr < prev.unwrap() {
return false;
}

prev = Some(curr);
}

true
}

#[test]
fn test1() {
fn check_timestamps() {
unsafe {
BASELINE = Some(chrono::DateTime::<chrono::Utc>::from_utc(
chrono::NaiveDateTime::from_timestamp(0, 0),
Expand Down Expand Up @@ -556,25 +590,7 @@ mod timestamps {
]
};

// ensure timestamps are in increasing order
// TODO(unstable) replace with slice::is_sorted
assert!(|| -> bool {
let mut it = timestamps.iter();
let mut prev = None;
while let Some(curr) = it.next() {
if prev.is_none() {
continue;
}

if curr < prev.unwrap() {
return false;
}

prev = Some(curr);
}

true
}());
assert!(is_sorted_increasing(&timestamps));

let mut decoder = Decoder::new(stream.clone(), DecoderOptions { ignore_eof: false });
let mut it = decoder.timestamps(TimestampsConfiguration {
Expand Down Expand Up @@ -627,64 +643,146 @@ mod timestamps {
}
}

// #[test]
// fn test2() {
// unsafe {
// BASELINE = Some(chrono::DateTime::<chrono::Utc>::from_utc(
// chrono::NaiveDateTime::from_timestamp(0, 0),
// chrono::Utc,
// ));
// }

// #[rustfmt::skip]
// let stream: &[u8] = &[
// // GTS1
// 0b1001_0100,
// 0b1000_0000,
// 0b1010_0000,
// 0b1000_0100,
// 0b0000_0000,

// // GTS2 (64-bit)
// 0b1011_0100,
// 0b1011_1101,
// 0b1111_0100,
// 0b1001_0001,
// 0b1000_0001,
// 0b1000_0001,
// 0b0000_0001,

// // LTS2
// 0b0110_0000,

// // GTS1 (compressed)
// 0b1001_0100,
// 0b0111_1111,

// // LTS2
// 0b0110_0000,
// ];

// let timestamps = {
// let mut offset_sum = chrono::Duration::nanoseconds(0);
// let mut decoder = Decoder::new(stream.clone(), DecoderOptions { ignore_eof: false });
// let mut it = decoder.singles();
// let mut gts = None;
// [
// {
// let gts1 = it.nth(0).unwrap().unwrap();
// let gts2 = it.nth(0).unwrap().unwrap();
// let lts2 = it.nth(0).unwrap().unwrap();
// gts = Gts::from_two(gts1, gts2);

// outer_calc_offset(lts2, Some(Gts::from_two(gts1, gts2)), &mut offset_sum)
// },
// {
// let gts1 = it.nth(0).unwrap().unwrap();
// let lts2 = it.nth(0).unwrap().unwrap();
// outer_calc_offset(lts2, Some(Gts::from_one(gts1)), &mut offset_sum)
// },
// ]
// };
// }
#[test]
fn gts_compression() {
unsafe {
BASELINE = Some(chrono::DateTime::<chrono::Utc>::from_utc(
chrono::NaiveDateTime::from_timestamp(0, 0),
chrono::Utc,
));
}

#[rustfmt::skip]
let stream: &[u8] = &[
// LTS2
0b0110_0000,

// GTS1 (bit 1 set)
0b1001_0100,
0b1000_0001,
0b1000_0000,
0b1000_0000,
0b0000_0000,

// GTS2 (64-bit, bit 26 set)
0b1011_0100,
0b1000_0001,
0b1000_0000,
0b1000_0000,
0b1000_0000,
0b1000_0000,
0b0000_0000,

// LTS2
0b0110_0000,

// GTS1 (compressed)
0b1001_0100,
0b1111_1111,
0b0000_0000,

// LTS2
0b0110_0000,
];

let timestamps = {
let mut offset_sum = chrono::Duration::nanoseconds(0);
let mut decoder = Decoder::new(stream.clone(), DecoderOptions { ignore_eof: false });
let mut it = decoder.singles();
let mut gts: Option<Gts> = None;
[
(
{
let lts2 = it.nth(0).unwrap().unwrap();

outer_calc_offset(lts2, None, &mut offset_sum)
},
chrono::Duration::nanoseconds(375),
),
(
{
let gts1 = it.nth(0).unwrap().unwrap();
let gts2 = it.nth(0).unwrap().unwrap();
let lts2 = it.nth(0).unwrap().unwrap();
gts = Some(Gts::from_two(gts1, gts2).to_owned());

outer_calc_offset(lts2, gts.clone(), &mut offset_sum)
},
chrono::Duration::nanoseconds(4194304438),
),
(
{
if let TracePacket::GlobalTimestamp1 { ts, .. } =
it.nth(0).unwrap().unwrap()
{
gts.as_mut().unwrap().replace_lower(ts);
gts.as_ref().unwrap().merge();
} else {
unreachable!();
}
let lts2 = it.nth(0).unwrap().unwrap();

outer_calc_offset(lts2, gts, &mut offset_sum)
},
chrono::Duration::nanoseconds(4194312313),
),
]
};

assert!(is_sorted_increasing(
&timestamps
.iter()
.map(|(ts, _since)| ts.clone())
.collect::<Vec<Timestamp>>()
));

for (ts, since) in timestamps.iter() {
assert_eq!(
unsafe { BASELINE.unwrap() }
.checked_add_signed(since.clone())
.unwrap(),
ts.ts
);
}

let mut decoder = Decoder::new(stream.clone(), DecoderOptions { ignore_eof: false });
let mut it = decoder.timestamps(TimestampsConfiguration {
clock_frequency: FREQ,
lts_prescaler: LocalTimestampOptions::Enabled,
baseline: unsafe { BASELINE.unwrap() },
expect_malformed: false,
});

for (i, set) in [
TimestampedTracePackets {
packets: [].into(),
malformed_packets: [].into(),
timestamp: timestamps[0].0.clone(),
consumed_packets: 1,
},
TimestampedTracePackets {
packets: [].into(),
malformed_packets: [].into(),
timestamp: timestamps[1].0.clone(),
consumed_packets: 3,
},
TimestampedTracePackets {
packets: [].into(),
malformed_packets: [].into(),
timestamp: timestamps[2].0.clone(),
consumed_packets: 2,
},
]
.iter()
.enumerate()
{
let ttp = it.next().unwrap().unwrap();
let since = ttp
.timestamp
.ts
.signed_duration_since(unsafe { BASELINE.unwrap() });
assert_eq!(dbg!(since), dbg!(timestamps[i].1));
assert_eq!(ttp, *set);
}
}
}

0 comments on commit be50af2

Please sign in to comment.