Fix equality/ordering of Sample
Only use sample_id within a process. Otherwise order by start_ns.
@@ -113,7 +113,7 @@ pub struct Sample {
/// A private ordering to apply to the events based on creation order.
/// Disambiguates in case 2 samples might be created from different threads
/// with the same start_ns for purposes of ordering.
- sample_id: usize,
+ pub(crate) sample_id: usize,
/// The name of the event to be shown.
pub name: StrCow,
/// List of categories the event applies to.
@@ -192,7 +192,7 @@ impl Sample {
impl PartialEq for Sample {
fn eq(&self, other: &Sample) -> bool {
- self.sample_id == other.sample_id
+ self.pid == other.pid && self.sample_id == other.sample_id
}
}
@@ -206,13 +206,17 @@ impl PartialOrd for Sample {
impl Ord for Sample {
fn cmp(&self, other: &Sample) -> cmp::Ordering {
+ if self.pid == other.pid {
self.sample_id.cmp(&other.sample_id)
+ } else {
+ self.start_ns.cmp(&other.start_ns)
+ }
}
}
impl Hash for Sample {
fn hash<H: Hasher>(&self, state: &mut H) {
- self.sample_id.hash(state);
+ (self.pid, self.sample_id).hash(state);
}
}
@@ -786,6 +790,25 @@ mod tests {
assert_eq!(snapshot[5].name, "c");
}
+ #[test]
+ fn test_cross_process_samples() {
+ let mut samples = vec![
+ Sample::new_instant("local pid", &[], None),
+ Sample::new_instant("remote pid", &[], None)];
+ samples[0].pid = 1;
+ samples[0].start_ns = 10;
+ samples[0].end_ns = 10;
+
+ samples[1].pid = 2;
+ samples[1].start_ns = 5;
+ samples[1].end_ns = 5;
+
+ samples.sort();
+
+ assert_eq!(samples[0].name, "remote pid");
+ assert_eq!(samples[1].name, "local pid");
+ }
+
#[cfg(feature = "benchmarks")]
#[bench]
fn bench_trace_instant_disabled(b: &mut Bencher) {
