111 lines
3.5 KiB
Rust
111 lines
3.5 KiB
Rust
//! Parser for Portage's `emerge.log`, used to record *measured* compile times.
|
|
//!
|
|
//! Portage writes lines of the form:
|
|
//!
|
|
//! ```text
|
|
//! 1650000000: >>> emerge (1 of 3) sys-fs/cryptsetup-2.7.5 to /
|
|
//! 1650000060: ::: completed emerge (1 of 3) sys-fs/cryptsetup-2.7.5 to /
|
|
//! ```
|
|
//!
|
|
//! We derive a duration per atom. Nothing here guesses: if no data exists, no
|
|
//! entry is recorded.
|
|
|
|
/// Remove the version suffix from `category/package-version`.
|
|
pub fn atom_from_cpv(cpv: &str) -> String {
|
|
// Find the last '-' followed by a digit, which starts the version.
|
|
let bytes = cpv.as_bytes();
|
|
let mut split = None;
|
|
for i in (0..bytes.len()).rev() {
|
|
if bytes[i] == b'-' && i + 1 < bytes.len() && bytes[i + 1].is_ascii_digit() {
|
|
split = Some(i);
|
|
break;
|
|
}
|
|
}
|
|
match split {
|
|
Some(i) => cpv[..i].to_string(),
|
|
None => cpv.to_string(),
|
|
}
|
|
}
|
|
|
|
fn epoch(line: &str) -> Option<u64> {
|
|
let colon = line.find(':')?;
|
|
line[..colon].trim().parse::<u64>().ok()
|
|
}
|
|
|
|
/// Extract (atom, seconds) pairs from an emerge.log.
|
|
pub fn parse_emerge_log(text: &str) -> Vec<(String, u64)> {
|
|
use std::collections::HashMap;
|
|
let mut starts: HashMap<String, u64> = HashMap::new();
|
|
let mut out: Vec<(String, u64)> = Vec::new();
|
|
|
|
for line in text.lines() {
|
|
if let Some(rest) = line.split(">>> emerge (").nth(1) {
|
|
// rest: "1 of 3) sys-fs/cryptsetup-2.7.5 to /"
|
|
if let Some(cpv) = extract_cpv(rest) {
|
|
if let Some(ts) = epoch(line) {
|
|
starts.insert(atom_from_cpv(&cpv), ts);
|
|
}
|
|
}
|
|
} else if let Some(rest) = line.split("::: completed emerge (").nth(1) {
|
|
if let Some(cpv) = extract_cpv(rest) {
|
|
if let Some(ts) = epoch(line) {
|
|
let atom = atom_from_cpv(&cpv);
|
|
if let Some(start) = starts.remove(&atom) {
|
|
if ts >= start {
|
|
out.push((atom, ts - start));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The CPV token follows ") " and precedes " to ".
|
|
fn extract_cpv(rest: &str) -> Option<String> {
|
|
let after = rest.split_once(')')?.1.trim_start();
|
|
let token = after.split_whitespace().next()?;
|
|
if token.is_empty() {
|
|
None
|
|
} else {
|
|
Some(token.to_string())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn strips_versions_with_hyphenated_names() {
|
|
assert_eq!(
|
|
atom_from_cpv("sys-fs/cryptsetup-2.7.5"),
|
|
"sys-fs/cryptsetup"
|
|
);
|
|
assert_eq!(atom_from_cpv("app-editors/vim-9.1.1234"), "app-editors/vim");
|
|
assert_eq!(
|
|
atom_from_cpv("dev-lang/python-exec-conf-2.4.6"),
|
|
"dev-lang/python-exec-conf"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn computes_durations() {
|
|
let log = "\
|
|
1650000000: >>> emerge (1 of 2) sys-fs/cryptsetup-2.7.5 to /
|
|
1650000042: ::: completed emerge (1 of 2) sys-fs/cryptsetup-2.7.5 to /
|
|
1650000042: >>> emerge (2 of 2) sys-fs/btrfs-progs-6.5 to /
|
|
1650000170: ::: completed emerge (2 of 2) sys-fs/btrfs-progs-6.5 to /
|
|
";
|
|
let parsed = parse_emerge_log(log);
|
|
assert!(parsed.contains(&("sys-fs/cryptsetup".to_string(), 42)));
|
|
assert!(parsed.contains(&("sys-fs/btrfs-progs".to_string(), 128)));
|
|
}
|
|
|
|
#[test]
|
|
fn ignores_incomplete_entries() {
|
|
let log = "1650000000: >>> emerge (1 of 1) sys-apps/foo-1.0 to /\n";
|
|
assert!(parse_emerge_log(log).is_empty());
|
|
}
|
|
}
|