use serde_json::Value;
use critpath_core::Millis;
use crate::error::ParseError;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum TimingTarget {
Library,
BuildScriptCompile,
BuildScriptRun,
Named {
name: String,
kind: String,
},
Other(String),
}
impl TimingTarget {
#[must_use]
pub fn label(&self) -> &str {
match self {
Self::Library => "lib",
Self::BuildScriptCompile => "build-script",
Self::BuildScriptRun => "build-script (run)",
Self::Named { name, .. } => name,
Self::Other(text) => text,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TimingUnit {
pub index: usize,
pub position: usize,
pub package: String,
pub version: String,
pub target: TimingTarget,
pub features: Vec<String>,
pub start: Millis,
pub duration: Millis,
pub metadata_at: Option<Millis>,
pub unblocked: Vec<usize>,
pub unblocked_rmeta: Vec<usize>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct TimingReport {
pub units: Vec<TimingUnit>,
pub fresh_units: usize,
pub dirty_units: usize,
pub total_units: usize,
pub jobs: Option<u32>,
pub cpus: Option<u32>,
pub max_concurrency: Option<u32>,
pub profile: Option<String>,
pub rustc_version: Option<String>,
pub host: Option<String>,
pub target: Option<String>,
pub reported_total: Option<Millis>,
pub targets: Option<String>,
pub build_start: Option<String>,
}
impl TimingReport {
#[must_use]
pub fn compilation_span(&self) -> Millis {
let first = self.units.iter().map(|u| u.start).min();
let last = self
.units
.iter()
.map(|u| u.start.saturating_add(u.duration))
.max();
match (first, last) {
(Some(first), Some(last)) => last.saturating_sub(first),
_ => Millis::ZERO,
}
}
#[must_use]
pub fn excluded_prefix(&self) -> Millis {
match self.reported_total {
Some(total) => total.saturating_sub(self.compilation_span()),
None => Millis::ZERO,
}
}
}
pub fn parse(text: &str) -> Result<TimingReport, ParseError> {
crate::error::reject_wide_text(text, "timing report")?;
let raw = array_literal(text, "UNIT_DATA")?.ok_or(ParseError::NoUnitData)?;
let summary = summary_table(text)?;
let fresh_units = summary.number("Fresh units")?;
let dirty_units = summary.number("Dirty units")?;
let total_units = summary
.number("Total units")
.unwrap_or_else(|_| fresh_units.saturating_add(dirty_units));
if fresh_units > 0 {
return Err(ParseError::FreshUnits {
fresh: fresh_units,
total: total_units,
});
}
let value: Value = serde_json::from_str(&raw).map_err(|e| ParseError::MalformedArray {
name: "UNIT_DATA".to_owned(),
detail: e.to_string(),
})?;
let entries = value.as_array().ok_or_else(|| ParseError::MalformedArray {
name: "UNIT_DATA".to_owned(),
detail: "the literal is not an array".to_owned(),
})?;
let mut units = Vec::with_capacity(entries.len());
for (position, entry) in entries.iter().enumerate() {
units.push(unit(entry, position)?);
}
let (max_concurrency, jobs, cpus) = concurrency_row(&summary);
let (rustc_version, host, target) = compiler_row(&summary);
Ok(TimingReport {
units,
fresh_units,
dirty_units,
total_units,
jobs,
cpus,
max_concurrency,
profile: summary.text("Profile"),
rustc_version,
host,
target,
reported_total: summary.text("Total time").and_then(|v| seconds_prefix(&v)),
targets: summary.text("Targets"),
build_start: summary.text("Build start"),
})
}
fn unit(entry: &Value, position: usize) -> Result<TimingUnit, ParseError> {
let bad = |field: &'static str, expected: &'static str| ParseError::BadUnitField {
entry: position,
field,
expected,
};
let index = entry
.get("i")
.and_then(Value::as_u64)
.ok_or_else(|| bad("i", "a unit index"))? as usize;
let package = entry
.get("name")
.and_then(Value::as_str)
.ok_or_else(|| bad("name", "a package name"))?
.to_owned();
let version = entry
.get("version")
.and_then(Value::as_str)
.unwrap_or_default()
.to_owned();
let display = entry
.get("target")
.and_then(Value::as_str)
.ok_or_else(|| bad("target", "a target display string"))?;
let start_seconds = entry
.get("start")
.and_then(Value::as_f64)
.ok_or_else(|| bad("start", "a number of seconds"))?;
let duration_seconds = entry
.get("duration")
.and_then(Value::as_f64)
.ok_or_else(|| bad("duration", "a number of seconds"))?;
let start = millis_from_seconds(start_seconds, position)?;
let duration = millis_from_seconds(duration_seconds, position)?;
let mut features: Vec<String> = entry
.get("features")
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect()
})
.unwrap_or_default();
features.sort();
features.dedup();
let frontend_end = entry
.get("sections")
.and_then(Value::as_array)
.and_then(|sections| {
sections.iter().find_map(|section| {
let pair = section.as_array()?;
if pair.first().and_then(Value::as_str)? != "frontend" {
return None;
}
pair.get(1)?.get("end")?.as_f64()
})
});
let metadata_at = match frontend_end {
Some(seconds) => Some(millis_from_seconds(seconds, position)?.max(Millis::ZERO)),
None => None,
};
let metadata_at = metadata_at.map(|at| if at > duration { duration } else { at });
Ok(TimingUnit {
index,
position,
package,
version,
target: target_of(display),
features,
start,
duration,
metadata_at,
unblocked: indices(entry.get("unblocked_units")),
unblocked_rmeta: indices(entry.get("unblocked_rmeta_units")),
})
}
fn indices(value: Option<&Value>) -> Vec<usize> {
value
.and_then(Value::as_array)
.map(|items| {
items
.iter()
.filter_map(Value::as_u64)
.map(|i| i as usize)
.collect()
})
.unwrap_or_default()
}
fn millis_from_seconds(seconds: f64, entry: usize) -> Result<Millis, ParseError> {
const LIMIT: f64 = 1e15;
if !seconds.is_finite() || !(0.0..=LIMIT).contains(&seconds) {
return Err(ParseError::DurationOutOfRange { entry, seconds });
}
Ok(Millis::new((seconds * 1000.0).round() as u64))
}
fn target_of(display: &str) -> TimingTarget {
match display {
"" => return TimingTarget::Library,
" build-script" => return TimingTarget::BuildScriptCompile,
" build-script (run)" => return TimingTarget::BuildScriptRun,
_ => {}
}
let trimmed = display.trim();
if let Some((name, rest)) = trimmed.split_once(' ') {
let kind = rest.trim().trim_matches('"');
if !name.is_empty() && !kind.is_empty() && !kind.contains(char::is_whitespace) {
return TimingTarget::Named {
name: name.to_owned(),
kind: kind.to_owned(),
};
}
}
TimingTarget::Other(trimmed.to_owned())
}
fn concurrency_row(summary: &Summary) -> (Option<u32>, Option<u32>, Option<u32>) {
let Some(value) = summary.text("Max concurrency") else {
return (None, None, None);
};
let peak = value.split_whitespace().next().and_then(|n| n.parse().ok());
let field = |name: &str| {
value
.split(|c: char| c.is_whitespace() || c == '(' || c == ')')
.find_map(|part| part.strip_prefix(name)?.parse().ok())
};
(peak, field("jobs="), field("ncpu="))
}
fn compiler_row(summary: &Summary) -> (Option<String>, Option<String>, Option<String>) {
let Some(raw) = summary.raw("rustc") else {
return (None, None, None);
};
let mut version = None;
let mut host = None;
let mut target = None;
for part in raw.split("<br>") {
let text = strip_tags(part);
if let Some(rest) = text.strip_prefix("Host:") {
host = Some(rest.trim().to_owned());
} else if let Some(rest) = text.strip_prefix("Target:") {
target = Some(rest.trim().to_owned());
} else if !text.is_empty() && version.is_none() {
version = Some(text);
}
}
(version, host, target)
}
fn seconds_prefix(value: &str) -> Option<Millis> {
let digits: String = value
.chars()
.take_while(|c| c.is_ascii_digit() || *c == '.')
.collect();
let seconds: f64 = digits.parse().ok()?;
millis_from_seconds(seconds, 0).ok()
}
struct Summary {
rows: Vec<(String, String)>,
}
impl Summary {
fn raw(&self, label: &str) -> Option<&str> {
self.rows
.iter()
.find(|(name, _)| name == label)
.map(|(_, value)| value.as_str())
}
fn text(&self, label: &str) -> Option<String> {
self.raw(label).map(strip_tags)
}
fn number(&self, label: &'static str) -> Result<usize, ParseError> {
let value = self
.text(label)
.ok_or(ParseError::MissingSummaryRow { row: label })?;
value.trim().parse().map_err(|_| ParseError::BadSummaryRow {
row: label,
value,
expected: "a count",
})
}
}
fn summary_table(text: &str) -> Result<Summary, ParseError> {
let start = text.find("<table").ok_or(ParseError::NoSummaryTable)?;
let rest = text.get(start..).ok_or(ParseError::NoSummaryTable)?;
let end = rest.find("</table>").ok_or(ParseError::NoSummaryTable)?;
let table = rest.get(..end).ok_or(ParseError::NoSummaryTable)?;
let mut cells = Vec::new();
let mut remaining = table;
while let Some(open) = remaining.find("<td>") {
let after = match remaining.get(open.saturating_add(4)..) {
Some(after) => after,
None => break,
};
let Some(close) = after.find("</td>") else {
break;
};
if let Some(cell) = after.get(..close) {
cells.push(cell);
}
remaining = match after.get(close.saturating_add(5)..) {
Some(rest) => rest,
None => break,
};
}
let mut rows = Vec::new();
let mut pairs = cells.chunks_exact(2);
for pair in &mut pairs {
let (Some(label), Some(value)) = (pair.first(), pair.get(1)) else {
continue;
};
let label = strip_tags(label);
let label = label.trim().trim_end_matches(':').to_owned();
rows.push((label, (*value).to_owned()));
}
if rows.is_empty() {
return Err(ParseError::NoSummaryTable);
}
Ok(Summary { rows })
}
fn strip_tags(html: &str) -> String {
let mut out = String::with_capacity(html.len());
let mut depth = 0usize;
for c in html.chars() {
match c {
'<' => depth = depth.saturating_add(1),
'>' => depth = depth.saturating_sub(1),
_ if depth == 0 => out.push(c),
_ => {}
}
}
decode_entities(out.trim())
}
fn decode_entities(text: &str) -> String {
if !text.contains('&') {
return text.to_owned();
}
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find('&') {
if let Some(head) = rest.get(..at) {
out.push_str(head);
}
let tail = rest.get(at..).unwrap_or("");
let replacement = [
("&", "&"),
("<", "<"),
(">", ">"),
(""", "\""),
("'", "'"),
]
.into_iter()
.find(|(entity, _)| tail.starts_with(entity));
match replacement {
Some((entity, decoded)) => {
out.push_str(decoded);
rest = tail.get(entity.len()..).unwrap_or("");
}
None => {
out.push('&');
rest = tail.get(1..).unwrap_or("");
}
}
}
out.push_str(rest);
out
}
fn array_literal(text: &str, name: &str) -> Result<Option<String>, ParseError> {
let needle = format!("const {name} = ");
let Some(at) = text.find(&needle) else {
return Ok(None);
};
let rest = text
.get(at.saturating_add(needle.len())..)
.unwrap_or_default();
let mut depth = 0usize;
let mut in_string = false;
let mut escaped = false;
for (offset, c) in rest.char_indices() {
if in_string {
if escaped {
escaped = false;
} else if c == '\\' {
escaped = true;
} else if c == '"' {
in_string = false;
}
continue;
}
match c {
'"' => in_string = true,
'[' | '{' => depth = depth.saturating_add(1),
']' | '}' => {
depth = depth.saturating_sub(1);
if depth == 0 {
let end = offset.saturating_add(c.len_utf8());
return Ok(rest.get(..end).map(str::to_owned));
}
}
_ => {}
}
}
Err(ParseError::TruncatedArray {
name: name.to_owned(),
})
}