Unverified Commit 12b596a3 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] use Timestamp type, allow spans less than 1µs (#75479)

### What?

The minimum span size of 1µs was a bit limiting. This changes it to 0.01µs.

A trace viewer update is also needed: https://github.com/vercel/turbo-trace-viewer/pull/11
parent 64f34a0e
......@@ -18,6 +18,7 @@ mod span_graph_ref;
mod span_ref;
mod store;
mod store_container;
mod timestamp;
mod u64_empty_string;
mod u64_string;
mod viewer;
......
......@@ -19,6 +19,7 @@ mod span_graph_ref;
mod span_ref;
mod store;
mod store_container;
mod timestamp;
mod u64_empty_string;
mod u64_string;
mod viewer;
......
......@@ -6,7 +6,7 @@ use rustc_demangle::demangle;
use rustc_hash::{FxHashMap, FxHashSet};
use super::TraceFormat;
use crate::{span::SpanIndex, store_container::StoreContainer, FxIndexMap};
use crate::{span::SpanIndex, store_container::StoreContainer, timestamp::Timestamp, FxIndexMap};
#[derive(Debug, Clone, Copy)]
struct TraceNode {
......@@ -87,7 +87,7 @@ struct TraceData {
pub struct HeaptrackFormat {
store: Arc<StoreContainer>,
version: u32,
last_timestamp: u64,
last_timestamp: Timestamp,
strings: Vec<String>,
traces: Vec<TraceData>,
ip_parent_map: FxHashMap<(usize, SpanIndex), usize>,
......@@ -109,7 +109,7 @@ impl HeaptrackFormat {
Self {
store,
version: 0,
last_timestamp: 0,
last_timestamp: Timestamp::ZERO,
strings: vec!["".to_string()],
traces: vec![TraceData {
span_index: SpanIndex::new(usize::MAX).unwrap(),
......@@ -411,7 +411,7 @@ impl TraceFormat for HeaptrackFormat {
b'c' => {
// timestamp
let timestamp = read_hex(&mut line)?;
self.last_timestamp = timestamp;
self.last_timestamp = Timestamp::from_micros(timestamp);
}
b'a' => {
// allocation info
......
......@@ -4,7 +4,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize;
use super::TraceFormat;
use crate::{span::SpanIndex, store_container::StoreContainer, FxIndexMap};
use crate::{span::SpanIndex, store_container::StoreContainer, timestamp::Timestamp, FxIndexMap};
pub struct NextJsFormat {
store: Arc<StoreContainer>,
......@@ -51,6 +51,8 @@ impl TraceFormat for NextJsFormat {
start_time: _,
trace_id: _,
} = span;
let timestamp = Timestamp::from_micros(timestamp);
let duration = Timestamp::from_micros(duration);
let (parent, queue_parent) = if let Some(parent) = parent_id {
if let Some(parent) = self.id_mapping.get(&parent) {
(Some(*parent), None)
......
......@@ -14,6 +14,7 @@ use super::TraceFormat;
use crate::{
span::SpanIndex,
store_container::{StoreContainer, StoreWriteGuard},
timestamp::Timestamp,
FxIndexMap,
};
......@@ -42,20 +43,20 @@ impl InternalRow<'_> {
enum InternalRowType<'a> {
Start {
new_id: u64,
ts: u64,
ts: Timestamp,
name: Cow<'a, str>,
target: Cow<'a, str>,
values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
},
End {
ts: u64,
ts: Timestamp,
},
SelfTime {
start: u64,
end: u64,
start: Timestamp,
end: Timestamp,
},
Event {
ts: u64,
ts: Timestamp,
values: Vec<(Cow<'a, str>, TraceValue<'a>)>,
},
Record {
......@@ -135,7 +136,7 @@ pub struct TurbopackFormat {
outdated_spans: FxHashSet<SpanIndex>,
thread_stacks: FxHashMap<u64, Vec<u64>>,
thread_allocation_counters: FxHashMap<u64, AllocationInfo>,
self_time_started: FxHashMap<(u64, u64), u64>,
self_time_started: FxHashMap<(u64, u64), Timestamp>,
}
impl TurbopackFormat {
......@@ -161,6 +162,7 @@ impl TurbopackFormat {
target,
values,
} => {
let ts = Timestamp::from_micros(ts);
self.process_internal_row(
store,
InternalRow {
......@@ -185,6 +187,7 @@ impl TurbopackFormat {
);
}
TraceRow::End { ts, id } => {
let ts = Timestamp::from_micros(ts);
self.process_internal_row(
store,
InternalRow {
......@@ -194,6 +197,7 @@ impl TurbopackFormat {
);
}
TraceRow::Enter { ts, id, thread_id } => {
let ts = Timestamp::from_micros(ts);
let stack = self.thread_stacks.entry(thread_id).or_default();
if let Some(&parent) = stack.last() {
if let Some(parent_start) = self.self_time_started.remove(&(parent, thread_id))
......@@ -218,6 +222,7 @@ impl TurbopackFormat {
self.self_time_started.insert((id, thread_id), ts);
}
TraceRow::Exit { ts, id, thread_id } => {
let ts = Timestamp::from_micros(ts);
let stack = self.thread_stacks.entry(thread_id).or_default();
if let Some(pos) = stack.iter().rev().position(|&x| x == id) {
let stack_index = stack.len() - pos - 1;
......@@ -238,6 +243,7 @@ impl TurbopackFormat {
}
}
TraceRow::Event { ts, parent, values } => {
let ts = Timestamp::from_micros(ts);
self.process_internal_row(
store,
InternalRow {
......@@ -404,10 +410,12 @@ impl TurbopackFormat {
}
InternalRowType::Event { ts, values } => {
let mut values = values.into_iter().collect::<FxIndexMap<_, _>>();
let duration = values
.swap_remove("duration")
.and_then(|v| v.as_u64())
.unwrap_or(0);
let duration = Timestamp::from_micros(
values
.swap_remove("duration")
.and_then(|v| v.as_u64())
.unwrap_or(0),
);
let name = values
.swap_remove("name")
.and_then(|v| v.as_str().map(|s| s.to_string()))
......
......@@ -3,6 +3,8 @@ use std::{
mem::take,
};
use crate::timestamp::Timestamp;
const SPLIT_COUNT: usize = 128;
/// Start balancing the tree when there are N times more items on one side. Must be at least 3.
const BALANCE_THRESHOLD: usize = 3;
......@@ -14,15 +16,15 @@ pub struct SelfTimeTree<T> {
}
struct SelfTimeEntry<T> {
start: u64,
end: u64,
start: Timestamp,
end: Timestamp,
item: T,
}
struct SelfTimeChildren<T> {
/// Entries < split_point
left: SelfTimeTree<T>,
split_point: u64,
split_point: Timestamp,
/// Entries >= split_point
right: SelfTimeTree<T>,
/// Number of entries in the SelfTimeTree::entries list that overlap the split point
......@@ -48,7 +50,7 @@ impl<T> SelfTimeTree<T> {
self.count
}
pub fn insert(&mut self, start: u64, end: u64, item: T) {
pub fn insert(&mut self, start: Timestamp, end: Timestamp, item: T) {
self.count += 1;
self.entries.push(SelfTimeEntry { start, end, item });
self.check_for_split();
......@@ -187,8 +189,8 @@ impl<T> SelfTimeTree<T> {
}
}
pub fn lookup_range_count(&self, start: u64, end: u64) -> u64 {
let mut total_count = 0;
pub fn lookup_range_count(&self, start: Timestamp, end: Timestamp) -> Timestamp {
let mut total_count = Timestamp::ZERO;
for entry in &self.entries {
if entry.start < end && entry.end > start {
let start = max(entry.start, start);
......@@ -208,11 +210,21 @@ impl<T> SelfTimeTree<T> {
total_count
}
pub fn for_each_in_range(&self, start: u64, end: u64, mut f: impl FnMut(u64, u64, &T)) {
pub fn for_each_in_range(
&self,
start: Timestamp,
end: Timestamp,
mut f: impl FnMut(Timestamp, Timestamp, &T),
) {
self.for_each_in_range_ref(start, end, &mut f);
}
fn for_each_in_range_ref(&self, start: u64, end: u64, f: &mut impl FnMut(u64, u64, &T)) {
fn for_each_in_range_ref(
&self,
start: Timestamp,
end: Timestamp,
f: &mut impl FnMut(Timestamp, Timestamp, &T),
) {
for entry in &self.entries {
if entry.start < end && entry.end > start {
f(entry.start, entry.end, &entry.item);
......@@ -276,11 +288,14 @@ mod tests {
let mut tree = SelfTimeTree::new();
let count = 10000;
for i in 0..count {
tree.insert(i, i + 1, i);
tree.insert(Timestamp::from_micros(i), Timestamp::from_micros(i + 1), i);
assert_eq!(tree.count, (i + 1) as usize);
assert_balanced(&tree);
}
assert_eq!(tree.lookup_range_count(0, count), count);
assert_eq!(
tree.lookup_range_count(Timestamp::ZERO, Timestamp::from_micros(count)),
Timestamp::from_micros(count)
);
print_tree(&tree, 0);
assert_balanced(&tree);
}
......@@ -294,13 +309,16 @@ mod tests {
for c in 0..10 {
for d in 0..10 {
let i = d * 1000 + c * 100 + b * 10 + a;
tree.insert(i, i + 1, i);
tree.insert(Timestamp::from_micros(i), Timestamp::from_micros(i + 1), i);
assert_balanced(&tree);
}
}
}
}
assert_eq!(tree.lookup_range_count(0, count), count);
assert_eq!(
tree.lookup_range_count(Timestamp::ZERO, Timestamp::from_micros(count)),
Timestamp::from_micros(count)
);
print_tree(&tree, 0);
assert_balanced(&tree);
}
......@@ -310,11 +328,18 @@ mod tests {
let mut tree = SelfTimeTree::new();
let count = 10000;
for i in 0..count {
tree.insert(i, i + 100, i);
tree.insert(
Timestamp::from_micros(i),
Timestamp::from_micros(i + 100),
i,
);
assert_eq!(tree.count, (i + 1) as usize);
assert_balanced(&tree);
}
assert_eq!(tree.lookup_range_count(0, count + 100), count * 100);
assert_eq!(
tree.lookup_range_count(Timestamp::ZERO, Timestamp::from_micros(count + 100)),
Timestamp::from_micros(count * 100)
);
print_tree(&tree, 0);
assert_balanced(&tree);
}
......@@ -324,10 +349,17 @@ mod tests {
let mut tree = SelfTimeTree::new();
let count = 10000;
for i in 0..count {
tree.insert(i, i + 500, i);
tree.insert(
Timestamp::from_micros(i),
Timestamp::from_micros(i + 500),
i,
);
assert_eq!(tree.count, (i + 1) as usize);
}
assert_eq!(tree.lookup_range_count(0, count + 500), count * 500);
assert_eq!(
tree.lookup_range_count(Timestamp::ZERO, Timestamp::from_micros(count + 500)),
Timestamp::from_micros(count * 500)
);
print_tree(&tree, 0);
assert_balanced(&tree);
}
......
......@@ -11,6 +11,7 @@ use tungstenite::{accept, Message};
use crate::{
store::SpanId,
store_container::StoreContainer,
timestamp::Timestamp,
u64_string,
viewer::{Update, ViewLineUpdate, ViewMode, Viewer},
};
......@@ -32,10 +33,10 @@ pub enum ServerToClientMessage {
#[serde(with = "u64_string")]
id: SpanId,
is_graph: bool,
start: u64,
end: u64,
duration: u64,
cpu: u64,
start: Timestamp,
end: Timestamp,
duration: Timestamp,
cpu: Timestamp,
allocations: u64,
deallocations: u64,
allocation_count: u64,
......@@ -74,8 +75,8 @@ pub enum ClientToServerMessage {
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SpanViewEvent {
pub start: u64,
pub duration: u64,
pub start: Timestamp,
pub duration: Timestamp,
pub name: String,
pub id: Option<SpanId>,
}
......@@ -312,10 +313,10 @@ fn handle_connection(
ServerToClientMessage::QueryResult {
id,
is_graph: false,
start: 0,
end: 0,
duration: 0,
cpu: 0,
start: Timestamp::ZERO,
end: Timestamp::ZERO,
duration: Timestamp::ZERO,
cpu: Timestamp::ZERO,
allocations: 0,
deallocations: 0,
allocation_count: 0,
......
......@@ -5,13 +5,15 @@ use std::{
use rustc_hash::FxHashMap;
use crate::timestamp::Timestamp;
pub type SpanIndex = NonZeroUsize;
pub struct Span {
// These values won't change after creation:
pub parent: Option<SpanIndex>,
pub depth: u32,
pub start: u64,
pub start: Timestamp,
pub category: String,
pub name: String,
pub args: Vec<(String, String)>,
......@@ -46,16 +48,16 @@ pub struct SpanTimeData {
pub ignore_self_time: bool,
// This might change during writing:
pub self_end: u64,
pub self_end: Timestamp,
// These values are computed automatically:
pub self_time: u64,
pub self_time: Timestamp,
// These values are computed when accessed (and maybe deleted during writing):
pub end: OnceLock<u64>,
pub total_time: OnceLock<u64>,
pub corrected_self_time: OnceLock<u64>,
pub corrected_total_time: OnceLock<u64>,
pub end: OnceLock<Timestamp>,
pub total_time: OnceLock<Timestamp>,
pub corrected_self_time: OnceLock<Timestamp>,
pub corrected_total_time: OnceLock<Timestamp>,
}
#[derive(Default)]
......@@ -99,7 +101,7 @@ impl Span {
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum SpanEvent {
SelfTime { start: u64, end: u64 },
SelfTime { start: Timestamp, end: Timestamp },
Child { index: SpanIndex },
}
......@@ -108,7 +110,7 @@ pub enum SpanGraphEvent {
// TODO(sokra) use events instead of children for visualizing span graphs
#[allow(dead_code)]
SelfTime {
duration: u64,
duration: Timestamp,
},
Child {
child: Arc<SpanGraph>,
......@@ -123,19 +125,19 @@ pub struct SpanGraph {
// These values are computed when accessed:
pub max_depth: OnceLock<u32>,
pub events: OnceLock<Vec<SpanGraphEvent>>,
pub self_time: OnceLock<u64>,
pub self_time: OnceLock<Timestamp>,
pub self_allocations: OnceLock<u64>,
pub self_deallocations: OnceLock<u64>,
pub self_persistent_allocations: OnceLock<u64>,
pub self_allocation_count: OnceLock<u64>,
pub total_time: OnceLock<u64>,
pub total_time: OnceLock<Timestamp>,
pub total_allocations: OnceLock<u64>,
pub total_deallocations: OnceLock<u64>,
pub total_persistent_allocations: OnceLock<u64>,
pub total_allocation_count: OnceLock<u64>,
pub total_span_count: OnceLock<u64>,
pub corrected_self_time: OnceLock<u64>,
pub corrected_total_time: OnceLock<u64>,
pub corrected_self_time: OnceLock<Timestamp>,
pub corrected_total_time: OnceLock<Timestamp>,
pub bottom_up: OnceLock<Vec<Arc<SpanBottomUp>>>,
}
......@@ -148,8 +150,8 @@ pub struct SpanBottomUp {
// These values are computed when accessed:
pub max_depth: OnceLock<u32>,
pub events: OnceLock<Vec<SpanGraphEvent>>,
pub self_time: OnceLock<u64>,
pub corrected_self_time: OnceLock<u64>,
pub self_time: OnceLock<Timestamp>,
pub corrected_self_time: OnceLock<Timestamp>,
pub self_allocations: OnceLock<u64>,
pub self_deallocations: OnceLock<u64>,
pub self_persistent_allocations: OnceLock<u64>,
......
......@@ -9,6 +9,7 @@ use crate::{
span_graph_ref::{event_map_to_list, SpanGraphEventRef, SpanGraphRef},
span_ref::SpanRef,
store::{SpanId, Store},
timestamp::Timestamp,
FxIndexMap,
};
......@@ -128,14 +129,14 @@ impl<'a> SpanBottomUpRef<'a> {
})
}
pub fn corrected_self_time(&self) -> u64 {
pub fn corrected_self_time(&self) -> Timestamp {
*self
.bottom_up
.corrected_self_time
.get_or_init(|| self.spans().map(|span| span.corrected_self_time()).sum())
}
pub fn self_time(&self) -> u64 {
pub fn self_time(&self) -> Timestamp {
*self
.bottom_up
.self_time
......
......@@ -12,6 +12,7 @@ use crate::{
span_bottom_up_ref::SpanBottomUpRef,
span_ref::SpanRef,
store::{SpanId, Store},
timestamp::Timestamp,
FxIndexMap,
};
......@@ -175,7 +176,7 @@ impl<'a> SpanGraphRef<'a> {
})
}
pub fn self_time(&self) -> u64 {
pub fn self_time(&self) -> Timestamp {
*self.graph.self_time.get_or_init(|| {
self.recursive_spans()
.map(|span| span.self_time())
......@@ -184,7 +185,7 @@ impl<'a> SpanGraphRef<'a> {
})
}
pub fn total_time(&self) -> u64 {
pub fn total_time(&self) -> Timestamp {
*self.graph.total_time.get_or_init(|| {
self.children()
.map(|graph| graph.total_time())
......@@ -284,19 +285,19 @@ impl<'a> SpanGraphRef<'a> {
})
}
pub fn corrected_self_time(&self) -> u64 {
pub fn corrected_self_time(&self) -> Timestamp {
*self.graph.corrected_self_time.get_or_init(|| {
self.recursive_spans_par()
.map(|span| span.corrected_self_time())
.sum::<u64>()
.sum::<Timestamp>()
})
}
pub fn corrected_total_time(&self) -> u64 {
pub fn corrected_total_time(&self) -> Timestamp {
*self.graph.corrected_total_time.get_or_init(|| {
self.children_par()
.map(|graph| graph.corrected_total_time())
.sum::<u64>()
.sum::<Timestamp>()
+ self.corrected_self_time()
})
}
......@@ -353,19 +354,19 @@ impl Debug for SpanGraphRef<'_> {
#[allow(dead_code)]
#[derive(Clone)]
pub enum SpanGraphEventRef<'a> {
SelfTime { duration: u64 },
SelfTime { duration: Timestamp },
Child { graph: SpanGraphRef<'a> },
}
impl SpanGraphEventRef<'_> {
pub fn corrected_total_time(&self) -> u64 {
pub fn corrected_total_time(&self) -> Timestamp {
match self {
SpanGraphEventRef::SelfTime { duration } => *duration,
SpanGraphEventRef::Child { graph } => graph.corrected_total_time(),
}
}
pub fn total_time(&self) -> u64 {
pub fn total_time(&self) -> Timestamp {
match self {
SpanGraphEventRef::SelfTime { duration } => *duration,
SpanGraphEventRef::Child { graph } => graph.total_time(),
......
......@@ -14,6 +14,7 @@ use crate::{
span_bottom_up_ref::SpanBottomUpRef,
span_graph_ref::{event_map_to_list, SpanGraphEventRef, SpanGraphRef},
store::{SpanId, Store},
timestamp::Timestamp,
FxIndexMap,
};
......@@ -41,7 +42,7 @@ impl<'a> SpanRef<'a> {
})
}
pub fn start(&self) -> u64 {
pub fn start(&self) -> Timestamp {
self.span.start
}
......@@ -57,7 +58,7 @@ impl<'a> SpanRef<'a> {
self.span.names()
}
pub fn end(&self) -> u64 {
pub fn end(&self) -> Timestamp {
let time_data = self.time_data();
*time_data.end.get_or_init(|| {
max(
......@@ -137,7 +138,7 @@ impl<'a> SpanRef<'a> {
self.span.args.iter().map(|(k, v)| (k.as_str(), v.as_str()))
}
pub fn self_time(&self) -> u64 {
pub fn self_time(&self) -> Timestamp {
self.time_data().self_time
}
......@@ -207,7 +208,7 @@ impl<'a> SpanRef<'a> {
})
}
pub fn total_time(&self) -> u64 {
pub fn total_time(&self) -> Timestamp {
*self.time_data().total_time.get_or_init(|| {
self.children()
.map(|child| child.total_time())
......@@ -267,7 +268,7 @@ impl<'a> SpanRef<'a> {
})
}
pub fn corrected_self_time(&self) -> u64 {
pub fn corrected_self_time(&self) -> Timestamp {
let store = self.store;
*self.time_data().corrected_self_time.get_or_init(|| {
let mut self_time = self
......@@ -276,31 +277,31 @@ impl<'a> SpanRef<'a> {
.par_iter()
.filter_map(|event| {
if let SpanEvent::SelfTime { start, end } = event {
let duration = end - start;
if duration != 0 {
let duration = *end - *start;
if !duration.is_zero() {
store.set_max_self_time_lookup(*end);
let concurrent_time = store
.self_time_tree
.as_ref()
.map_or(duration, |tree| tree.lookup_range_count(*start, *end));
return Some(duration * duration / concurrent_time);
return Some(duration * *duration / *concurrent_time);
}
}
None
})
.sum();
if self.children().next().is_none() {
self_time = max(self_time, 1);
self_time = max(self_time, Timestamp::from_value(1));
}
self_time
})
}
pub fn corrected_total_time(&self) -> u64 {
pub fn corrected_total_time(&self) -> Timestamp {
*self.time_data().corrected_total_time.get_or_init(|| {
self.children_par()
.map(|child| child.corrected_total_time())
.sum::<u64>()
.sum::<Timestamp>()
+ self.corrected_self_time()
})
}
......@@ -478,6 +479,6 @@ impl Debug for SpanRef<'_> {
#[allow(dead_code)]
#[derive(Copy, Clone)]
pub enum SpanEventRef<'a> {
SelfTime { start: u64, end: u64 },
SelfTime { start: Timestamp, end: Timestamp },
Child { span: SpanRef<'a> },
}
......@@ -12,6 +12,7 @@ use crate::{
self_time_tree::SelfTimeTree,
span::{Span, SpanEvent, SpanIndex},
span_ref::SpanRef,
timestamp::Timestamp,
};
pub type SpanId = NonZeroUsize;
......@@ -28,7 +29,7 @@ fn new_root_span() -> Span {
Span {
parent: None,
depth: 0,
start: u64::MAX,
start: Timestamp::MAX,
category: "".into(),
name: "(root)".into(),
args: vec![],
......@@ -80,7 +81,7 @@ impl Store {
pub fn add_span(
&mut self,
parent: Option<SpanIndex>,
start: u64,
start: Timestamp,
category: String,
name: String,
args: Vec<(String, String)>,
......@@ -137,7 +138,8 @@ impl Store {
outdated_spans.insert(span_index);
}
pub fn set_max_self_time_lookup(&self, time: u64) {
pub fn set_max_self_time_lookup(&self, time: Timestamp) {
let time = *time;
let mut old = self
.max_self_time_lookup_time
.load(std::sync::atomic::Ordering::Relaxed);
......@@ -156,13 +158,13 @@ impl Store {
fn insert_self_time(
&mut self,
start: u64,
end: u64,
start: Timestamp,
end: Timestamp,
span_index: SpanIndex,
outdated_spans: &mut FxHashSet<SpanIndex>,
) {
if let Some(tree) = self.self_time_tree.as_mut() {
if *self.max_self_time_lookup_time.get_mut() >= start {
if Timestamp::from_value(*self.max_self_time_lookup_time.get_mut()) >= start {
tree.for_each_in_range(start, end, |_, _, span| {
outdated_spans.insert(*span);
});
......@@ -174,8 +176,8 @@ impl Store {
pub fn add_self_time(
&mut self,
span_index: SpanIndex,
start: u64,
end: u64,
start: Timestamp,
end: Timestamp,
outdated_spans: &mut FxHashSet<SpanIndex>,
) {
let span = &mut self.spans[span_index.get()];
......@@ -193,8 +195,8 @@ impl Store {
pub fn set_total_time(
&mut self,
span_index: SpanIndex,
start_time: u64,
total_time: u64,
start_time: Timestamp,
total_time: Timestamp,
outdated_spans: &mut FxHashSet<SpanIndex>,
) {
let span = SpanRef {
......@@ -208,7 +210,7 @@ impl Store {
.collect::<Vec<_>>();
children.sort();
let self_end = start_time + total_time;
let mut self_time = 0;
let mut self_time = Timestamp::ZERO;
let mut current = start_time;
let mut events = Vec::new();
for (start, end, index) in children {
......
use std::{
fmt::{Debug, Display, Formatter},
iter::Sum,
ops::{Add, AddAssign, Deref, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
};
use serde::{Deserialize, Serialize};
const DUR_VALUE_MICROSECOND: u64 = 100;
#[derive(Clone, Copy, Default, Serialize, Deserialize)]
pub struct Timestamp(u64);
impl Timestamp {
pub const MAX: Self = Self(u64::MAX);
pub const ZERO: Self = Self(0);
}
impl Timestamp {
pub fn from_micros(micros: u64) -> Self {
Self(micros * DUR_VALUE_MICROSECOND)
}
pub fn is_zero(&self) -> bool {
self.0 == 0
}
pub fn from_value(value: u64) -> Self {
Self(value)
}
pub fn saturating_sub(self, rhs: Self) -> Self {
Self(self.0.saturating_sub(rhs.0))
}
}
impl Debug for Timestamp {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{:.2}μs", self.0 as f64 / DUR_VALUE_MICROSECOND as f64)
}
}
impl Display for Timestamp {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "{:.2}μs", self.0 as f64 / DUR_VALUE_MICROSECOND as f64)
}
}
impl Add for Timestamp {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Timestamp {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl Sub for Timestamp {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self(self.0 - rhs.0)
}
}
impl SubAssign for Timestamp {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl Div<u64> for Timestamp {
type Output = Self;
fn div(self, rhs: u64) -> Self {
Self(self.0 / rhs)
}
}
impl DivAssign<u64> for Timestamp {
fn div_assign(&mut self, rhs: u64) {
self.0 /= rhs;
}
}
impl Div for Timestamp {
type Output = u64;
fn div(self, rhs: Self) -> u64 {
self.0 / rhs.0
}
}
impl Mul<u64> for Timestamp {
type Output = Self;
fn mul(self, rhs: u64) -> Self {
Self(self.0 * rhs)
}
}
impl MulAssign<u64> for Timestamp {
fn mul_assign(&mut self, rhs: u64) {
self.0 *= rhs;
}
}
impl Sum for Timestamp {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Timestamp(0), |a, b| a + b)
}
}
impl PartialEq for Timestamp {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for Timestamp {}
impl Ord for Timestamp {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl PartialOrd for Timestamp {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Deref for Timestamp {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
......@@ -12,6 +12,7 @@ use crate::{
span_graph_ref::{SpanGraphEventRef, SpanGraphRef},
span_ref::SpanRef,
store::{SpanId, Store},
timestamp::Timestamp,
u64_empty_string,
};
......@@ -55,8 +56,8 @@ impl ValueMode {
fn value_from_span(&self, span: &SpanRef<'_>) -> u64 {
match self {
ValueMode::Duration => span.corrected_total_time(),
ValueMode::Cpu => span.total_time(),
ValueMode::Duration => *span.corrected_total_time(),
ValueMode::Cpu => *span.total_time(),
ValueMode::Allocations => span.total_allocations(),
ValueMode::Deallocations => span.total_deallocations(),
ValueMode::PersistentAllocations => span.total_persistent_allocations(),
......@@ -77,8 +78,8 @@ impl ValueMode {
fn value_from_graph(&self, graph: &SpanGraphRef<'_>) -> u64 {
match self {
ValueMode::Duration => graph.corrected_total_time(),
ValueMode::Cpu => graph.total_time(),
ValueMode::Duration => *graph.corrected_total_time(),
ValueMode::Cpu => *graph.total_time(),
ValueMode::Allocations => graph.total_allocations(),
ValueMode::Deallocations => graph.total_deallocations(),
ValueMode::PersistentAllocations => graph.total_persistent_allocations(),
......@@ -99,8 +100,8 @@ impl ValueMode {
fn value_from_graph_event(&self, event: &SpanGraphEventRef<'_>) -> u64 {
match self {
ValueMode::Duration => event.corrected_total_time(),
ValueMode::Cpu => event.total_time(),
ValueMode::Duration => *event.corrected_total_time(),
ValueMode::Cpu => *event.total_time(),
ValueMode::Allocations => event.total_allocations(),
ValueMode::Deallocations => event.total_deallocations(),
ValueMode::PersistentAllocations => event.total_persistent_allocations(),
......@@ -121,8 +122,8 @@ impl ValueMode {
fn value_from_bottom_up(&self, bottom_up: &SpanBottomUpRef<'_>) -> u64 {
match self {
ValueMode::Duration => bottom_up.corrected_self_time(),
ValueMode::Cpu => bottom_up.self_time(),
ValueMode::Duration => *bottom_up.corrected_self_time(),
ValueMode::Cpu => *bottom_up.self_time(),
ValueMode::Allocations => bottom_up.self_allocations(),
ValueMode::Deallocations => bottom_up.self_deallocations(),
ValueMode::PersistentAllocations => bottom_up.self_persistent_allocations(),
......@@ -145,8 +146,8 @@ impl ValueMode {
fn value_from_bottom_up_span(&self, bottom_up_span: &SpanRef<'_>) -> u64 {
match self {
ValueMode::Duration => bottom_up_span.corrected_self_time(),
ValueMode::Cpu => bottom_up_span.self_time(),
ValueMode::Duration => *bottom_up_span.corrected_self_time(),
ValueMode::Cpu => *bottom_up_span.self_time(),
ValueMode::Allocations => bottom_up_span.self_allocations(),
ValueMode::Deallocations => bottom_up_span.self_deallocations(),
ValueMode::PersistentAllocations => bottom_up_span.self_persistent_allocations(),
......@@ -171,11 +172,11 @@ impl ValueMode {
/// this is unfortunately int division but itll have to do.
///
/// cases where count per time is very low is probably not important
fn value_over_time(value: u64, time: u64) -> u64 {
if time == 0 {
fn value_over_time(value: u64, time: Timestamp) -> u64 {
if *time == 0 {
0
} else {
value / time
value / *time
}
}
......@@ -447,7 +448,7 @@ impl Viewer {
let offset = root_spans
.iter()
.min_by_key(|span| span.start())
.map_or(0, |span| span.start());
.map_or(Timestamp::ZERO, |span| span.start());
root_spans.par_iter().for_each(|span| {
span.max_depth();
QueueItem::Span(*span).value(value_mode);
......@@ -455,7 +456,7 @@ impl Viewer {
for span in root_spans {
if matches!(value_mode, ValueMode::Duration) {
// Move current to start if needed.
current = max(current, span.start() - offset);
current = max(current, *(span.start() - offset));
}
if add_child_item(
&mut children,
......@@ -976,7 +977,7 @@ impl Viewer {
if let Some(parent) = span.parent() {
let parent_start = parent.start();
let parent_duration = parent.end() - parent_start;
if parent_duration > 0 {
if !parent_duration.is_zero() {
start_in_parent = ((span.start() - parent_start) * 10000
/ parent_duration)
as u32;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment