Files
texrra-engine/crates/texrra-tex-core/src/execution/statistics.rs
T

308 lines
11 KiB
Rust

//! Measured native storage and execution resources, not invented TeX arena words.
use super::*;
use std::{collections::BTreeSet, sync::Arc};
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MaterialUsage {
pub boxes: usize,
pub horizontal_nodes: usize,
pub vertical_nodes: usize,
pub math_nodes: usize,
pub geometry_items: usize,
pub diagnostic_nodes: usize,
pub ligature_work_items: usize,
}
impl MaterialUsage {
pub fn records(&self) -> usize {
self.boxes
+ self.horizontal_nodes
+ self.vertical_nodes
+ self.math_nodes
+ self.geometry_items
+ self.diagnostic_nodes
+ self.ligature_work_items
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct StateUsage {
pub command_entries: usize,
pub control_name_bytes: usize,
pub macro_bindings: usize,
pub token_registers: usize,
pub parameter_entries: usize,
pub register_entries: usize,
pub code_entries: usize,
pub save_entries: usize,
pub font_instances: usize,
pub font_parameter_cells: usize,
pub tfm_resources: usize,
/// Size of the actual TFM resource versions, not Rust heap allocation bytes.
pub tfm_resource_bytes: u64,
pub hyphenation_exceptions: usize,
pub hyphenation_trie_nodes: usize,
pub hyphenation_edges: usize,
pub pattern_weight_cells: usize,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct StackUsage {
pub input_frames: usize,
pub groups: usize,
pub box_frames: usize,
pub math_frames: usize,
pub conditionals: usize,
pub scanner_depth: usize,
pub expansion_depth: usize,
}
impl StackUsage {
pub(super) fn include(&mut self, other: &Self) {
self.input_frames = self.input_frames.max(other.input_frames);
self.groups = self.groups.max(other.groups);
self.box_frames = self.box_frames.max(other.box_frames);
self.math_frames = self.math_frames.max(other.math_frames);
self.conditionals = self.conditionals.max(other.conditionals);
self.scanner_depth = self.scanner_depth.max(other.scanner_depth);
self.expansion_depth = self.expansion_depth.max(other.expansion_depth);
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct HostResourceUsage {
pub lookups: usize,
pub negative_lookups: usize,
pub failed_lookups: usize,
pub resource_reads: usize,
pub resource_bytes_read: u64,
pub stream_read_calls: usize,
pub stream_bytes_read: u64,
pub read_errors: usize,
pub open_inputs: Option<usize>,
pub open_outputs: Option<usize>,
}
impl HostResourceUsage {
pub fn from_trace(trace: &crate::resources::ResourceTrace) -> Self {
Self {
lookups: trace.lookups.len(),
negative_lookups: trace
.lookups
.iter()
.filter(|v| v.resolved.is_none() && v.error.is_none())
.count(),
failed_lookups: trace.lookups.iter().filter(|v| v.error.is_some()).count(),
resource_reads: trace.reads.len(),
resource_bytes_read: trace.reads.iter().map(|v| v.bytes).sum(),
stream_read_calls: trace.stream_reads.len(),
stream_bytes_read: trace.stream_reads.iter().map(|v| v.bytes).sum(),
read_errors: trace.read_errors.len(),
open_inputs: None,
open_outputs: None,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ResourceUsage {
pub steps: usize,
pub source_files: usize,
pub source_payload_bytes: usize,
pub mouth_payload_bytes: usize,
pub unique_macro_bodies: usize,
pub macro_token_cells: usize,
pub token_list_cells: usize,
pub state: StateUsage,
pub active_material: MaterialUsage,
pub retained_material: MaterialUsage,
/// Runtime events excluding statistics reports themselves.
pub retained_events: usize,
pub retained_shipouts: usize,
pub box_nodes_created: usize,
pub diagnostic_bytes_generated: usize,
pub stacks: StackUsage,
pub stack_peaks: StackUsage,
pub step_limit: usize,
pub token_limit: usize,
pub input_depth_limit: usize,
/// None means this host does not supply resource telemetry; never fake zero.
pub host: Option<HostResourceUsage>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum StatisticsPhase {
Shipout {
before: ResourceUsage,
after: ResourceUsage,
},
End {
success: bool,
usage: ResourceUsage,
},
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct StatisticsReport {
pub phase: StatisticsPhase,
pub destination: WriteDestination,
pub text: String,
pub span: Span,
}
#[derive(Clone, Copy)]
pub(super) enum Ownership {
Active,
Retained,
}
#[derive(Default)]
pub(super) struct Collector {
pub usage: ResourceUsage,
macros: BTreeSet<usize>,
token_lists: BTreeSet<(usize, usize)>,
}
impl Collector {
pub fn macro_body(&mut self, m: &Arc<Macro>) {
if self.macros.insert(Arc::as_ptr(m) as usize) {
self.usage.unique_macro_bodies += 1;
self.usage.macro_token_cells += m.prefix.len()
+ m.parameters.iter().map(Vec::len).sum::<usize>()
+ m.replacement.len();
}
}
pub fn shared_tokens(&mut self, t: &Arc<[Token]>) {
if self.token_lists.insert((t.as_ptr() as usize, t.len())) {
self.usage.token_list_cells += t.len();
}
}
fn material(&mut self, o: Ownership) -> &mut MaterialUsage {
match o {
Ownership::Active => &mut self.usage.active_material,
Ownership::Retained => &mut self.usage.retained_material,
}
}
pub fn output(&mut self, n: &OutputNode) {
if let OutputOperation::Write { tokens, .. } = &n.operation {
self.usage.token_list_cells += tokens.len();
}
}
pub fn native_box(&mut self, b: &NativeBox, o: Ownership) {
self.material(o).boxes += 1;
match &b.content {
BoxContent::Horizontal(v) => self.text(v, o),
BoxContent::Vertical(v) => self.vertical(v, o),
}
self.diagnostics(&b.diagnostic, o);
}
pub fn horizontal_box(&mut self, b: &crate::HorizontalBox, o: Ownership) {
if let Some(b) = &b.native_box {
self.native_box(b, o);
}
if let Some(g) = &b.math_geometry {
self.material(o).geometry_items += g.items.len();
}
}
pub fn text(&mut self, nodes: &[TextNode], o: Ownership) {
self.material(o).horizontal_nodes += nodes.len();
for n in nodes {
match n {
TextNode::Glyph { glyph, .. }
| TextNode::Box(glyph)
| TextNode::MathChar(glyph) => self.horizontal_box(glyph, o),
TextNode::Discretionary {
pre_break,
post_break,
..
} => {
self.text(pre_break, o);
self.text(post_break, o);
}
TextNode::Mark(m) => self.shared_tokens(&m.tokens),
TextNode::Adjustment(value) => self.vertical(&value.nodes, o),
TextNode::Insertion(n) => self.vertical(&n.nodes, o),
TextNode::Output(v) => self.output(v),
_ => {}
}
}
}
pub fn vertical<'a>(
&mut self,
nodes: impl IntoIterator<Item = &'a NativeVerticalNode>,
o: Ownership,
) {
for n in nodes {
self.material(o).vertical_nodes += 1;
match n {
NativeVerticalNode::Box(b) | NativeVerticalNode::Line(b) => self.native_box(b, o),
NativeVerticalNode::Mark(m) => self.shared_tokens(&m.tokens),
NativeVerticalNode::Insertion(n) => self.vertical(&n.nodes, o),
NativeVerticalNode::Output(v) => self.output(v),
_ => {}
}
}
}
pub fn math(&mut self, nodes: &[MathNode], o: Ownership) {
self.material(o).math_nodes += nodes.len();
for n in nodes {
match n {
MathNode::Noad(n) => {
self.field(&n.nucleus, o);
if let Some(f) = &n.sub {
self.field(f, o);
}
if let Some(f) = &n.sup {
self.field(f, o);
}
}
MathNode::Fraction {
numerator,
denominator,
..
} => {
self.math(numerator, o);
self.math(denominator, o);
}
MathNode::Choice(branches) => {
for branch in branches.iter() {
self.math(branch, o);
}
}
MathNode::Discretionary(n) => self.text(std::slice::from_ref(n.as_ref()), o),
MathNode::Mark(m) => self.shared_tokens(&m.tokens),
MathNode::Adjustment(value) => self.vertical(&value.nodes, o),
MathNode::Insertion(n) => self.vertical(&n.nodes, o),
MathNode::Output(v) => self.output(v),
_ => {}
}
}
}
fn field(&mut self, f: &MathField, o: Ownership) {
match f {
MathField::List(v) => self.math(v, o),
MathField::Radical(_, nucleus) => self.field(nucleus, o),
MathField::Box(b) => self.native_box(b, o),
_ => {}
}
}
pub fn math_builder(&mut self, b: &super::math::MathBuilder) {
self.math(&b.nodes, Ownership::Active);
if let Some(fraction) = &b.numerator {
self.math(&fraction.numerator, Ownership::Active);
}
}
pub fn paragraph_builder(&mut self, b: &super::paragraph::Builder) {
self.text(&b.nodes, Ownership::Active);
self.usage.active_material.ligature_work_items += b.ligature_work_items();
}
pub fn diagnostics(&mut self, nodes: &[super::diagnostics::DiagnosticNode], o: Ownership) {
use super::diagnostics::DiagnosticNode as D;
self.material(o).diagnostic_nodes += nodes.len();
for n in nodes {
match n {
D::Box(b) => self.diagnostics(&b.nodes, o),
D::Discretionary { pre, post, .. } => {
self.diagnostics(pre, o);
self.diagnostics(post, o);
}
D::Mark(m) => self.shared_tokens(&m.tokens),
D::Adjustment { nodes, .. } | D::Insertion { nodes, .. } => {
self.diagnostics(nodes, o)
}
D::Output(v) => self.output(v),
_ => {}
}
}
}
}