use super::super::statistics::{ Collector, Ownership::{Active, Retained}, }; use super::*; impl Executor { fn stack_usage(&self) -> StackUsage { StackUsage { input_frames: self.inputs.len(), groups: self.state.group_depth(), box_frames: self.box_frames.len(), math_frames: self.math.len(), conditionals: self.conditionals.len(), scanner_depth: self.scanner_depth, expansion_depth: self.expansion_depth, } } pub(super) fn observe_resource_stacks(&mut self) { self.stack_peaks.include(&self.stack_usage()); } pub(super) fn check_box_capacity(&mut self, shipping: Option<&NativeBox>) -> Result<(), Error> { if self.material_charge <= self.limits.tokens { return Ok(()); } // Packing can move existing lists or replace an old register. Recount // actual owners at an execution boundary before rejecting the job. let usage = self.resource_usage(shipping, None); self.material_charge = usage .active_material .records() .saturating_add(usage.retained_material.records()); if self.material_charge > self.limits.tokens { return self.fail("native box node capacity exceeded"); } Ok(()) } fn resource_usage( &self, shipping: Option<&NativeBox>, host: Option, ) -> ResourceUsage { let mut c = Collector::default(); self.state.collect_statistics(&mut c); c.usage.steps = self.steps; c.usage.source_files = self.sources.len(); c.usage.source_payload_bytes = self.sources.iter().map(|s| s.data.len()).sum(); c.usage.box_nodes_created = self.box_nodes_created; c.usage.diagnostic_bytes_generated = self.diagnostic_bytes; c.usage.step_limit = self.limits.steps; c.usage.token_limit = self.limits.tokens; c.usage.input_depth_limit = self.limits.input_depth; c.usage.stacks = self.stack_usage(); c.usage.stack_peaks = self.stack_peaks.clone(); c.usage.stack_peaks.include(&c.usage.stacks); c.usage.host = host; if let Some(p) = &self.paragraph { c.paragraph_builder(p); } c.vertical(&self.vertical.nodes, Active); c.vertical(&self.split_discards, Active); if let Some(d) = &self.display { d.collect_statistics(&mut c); } for b in &self.box_frames { b.collect_statistics(&mut c); } for a in &self.alignments { a.collect_statistics(&mut c); } for m in &self.math { c.math_builder(m); } if let Some(f) = &self.output_frame { f.collect_statistics(&mut c); } if let Some(p) = &self.native_page { c.vertical(&p.nodes, Active); c.vertical(&p.contributions, Active); c.vertical(&p.discards, Active); } for b in &self.pending_shipouts { c.native_box(b, Active); } if let Some(b) = shipping { c.native_box(b, Active); } if self.after_assignment.is_some() { c.usage.token_list_cells += 1; } c.usage.token_list_cells += self.pdf_catalog_tokens.len() + self.pdf_outlines.iter().map(|entry| entry.attributes.len()).sum::(); for input in &self.inputs { match input { InputFrame::Source { mouth, .. } => { c.usage.mouth_payload_bytes += mouth.payload_bytes() } InputFrame::Tokens { tokens, .. } => c.usage.token_list_cells += tokens.len(), InputFrame::Macro { definition, arguments, .. } => { c.macro_body(definition); for a in arguments { c.shared_tokens(a); } } InputFrame::Argument { tokens, .. } | InputFrame::Hook { tokens, .. } => { c.shared_tokens(tokens) } InputFrame::Boundary => {} } } for e in self.events.iter() { if matches!(e, Event::Statistics(_)) { continue; } c.usage.retained_events += 1; match e { Event::Shipout(s) => { c.usage.retained_shipouts += 1; c.native_box(&s.box_node, Retained); } Event::VerticalMaterial(v) => c.vertical(v, Retained), Event::BuiltParagraph(p) => { c.text(&p.nodes, Retained); for b in &p.line_boxes { c.native_box(b, Retained); } for v in &p.line_adjustments { c.vertical(v, Retained); } for b in &p.diagnostic_lines { c.diagnostics(&b.nodes, Retained); } for paragraph in &p.lines { for line in ¶graph.lines { for item in line { c.usage.retained_material.horizontal_nodes += 1; if let crate::HorizontalItem::Box(b) = item { c.horizontal_box(b, Retained); } } } } } Event::BuiltMath(m) => { c.math(&m.noads, Retained); c.text(&m.horizontal, Retained); } Event::BuiltDisplay(d) => c.native_box(&d.box_, Retained), Event::DeferredOutput(o) => c.output(o), _ => {} } } c.usage } pub(super) fn statistics_before_shipout( &mut self, b: &NativeBox, files: &mut impl SourceProvider, ) -> Result, Error> { if self.read_parameter(Parameter::Tracingstats).width() <= 1 { return Ok(None); } self.reads.insert(Dependency::ResourceStatistics); let host = files .resource_usage() .map_err(|e| self.error(e, self.current))?; Ok(Some(self.resource_usage(Some(b), host))) } pub(super) fn statistics_after_shipout( &mut self, before: Option, files: &mut impl SourceProvider, ) -> Result<(), Error> { let Some(before) = before else { return Ok(()); }; let host = files .resource_usage() .map_err(|e| self.error(e, self.current))?; let after = self.resource_usage(None, host); let text = format!( "Route-D resources at shipout: active records {} -> {}; retained records {} -> {}; token cells {} -> {}; retained pages {}.\n", before.active_material.records(), after.active_material.records(), before.retained_material.records(), after.retained_material.records(), before.macro_token_cells + before.token_list_cells, after.macro_token_cells + after.token_list_cells, after.retained_shipouts ); self.emit_statistics( StatisticsPhase::Shipout { before, after }, WriteDestination::TerminalAndLog, text, files, ) } pub(super) fn statistics_end( &mut self, success: bool, files: &mut impl SourceProvider, ) -> Result<(), Error> { if self.read_parameter(Parameter::Tracingstats).width() <= 0 { return Ok(()); } self.reads.insert(Dependency::ResourceStatistics); let host = files .resource_usage() .map_err(|e| self.error(e, self.current))?; let usage = self.resource_usage(None, host); let mut text = format!( "Route-D resource usage at core execution end ({}):\n {} steps out of {}\n {} source files, {} source payload bytes, {} active mouth payload bytes\n {} command entries, {} control-name bytes\n {} macro bindings, {} unique macro bodies, {} macro token cells\n {} token-list cells, {} saved entries\n {} active material records, {} retained material records, {} retained pages\n {} font instances, {} font parameter cells, {} TFM resources / {} source bytes\n {} hyphenation exceptions\n stack peaks: {} input, {} group, {} box, {} math, {} conditional, {} scanner, {} expansion\n", if success { "success" } else { "failure" }, usage.steps, usage.step_limit, usage.source_files, usage.source_payload_bytes, usage.mouth_payload_bytes, usage.state.command_entries, usage.state.control_name_bytes, usage.state.macro_bindings, usage.unique_macro_bodies, usage.macro_token_cells, usage.token_list_cells, usage.state.save_entries, usage.active_material.records(), usage.retained_material.records(), usage.retained_shipouts, usage.state.font_instances, usage.state.font_parameter_cells, usage.state.tfm_resources, usage.state.tfm_resource_bytes, usage.state.hyphenation_exceptions, usage.stack_peaks.input_frames, usage.stack_peaks.groups, usage.stack_peaks.box_frames, usage.stack_peaks.math_frames, usage.stack_peaks.conditionals, usage.stack_peaks.scanner_depth, usage.stack_peaks.expansion_depth ); if let Some(h) = &usage.host { text.push_str(&format!(" host resources: {} reads / {} bytes; {} lookups / {} misses / {} errors; {} stream reads / {} bytes\n",h.resource_reads,h.resource_bytes_read,h.lookups,h.negative_lookups,h.failed_lookups,h.stream_read_calls,h.stream_bytes_read)); } else { text.push_str(" host resource telemetry unavailable\n"); } self.emit_statistics( StatisticsPhase::End { success, usage }, WriteDestination::Log, text, files, ) } fn emit_statistics( &mut self, phase: StatisticsPhase, destination: WriteDestination, text: String, files: &mut impl SourceProvider, ) -> Result<(), Error> { self.diagnostic_bytes = self.diagnostic_bytes.saturating_add(text.len()); if self.diagnostic_bytes > self.limits.tokens { return self.fail("native diagnostic output limit exceeded"); } files .observe_diagnostic(destination, &text) .map_err(|e| self.error(e, self.current))?; self.events .push(Event::Statistics(Box::new(StatisticsReport { phase, destination, text, span: self.current, }))); Ok(()) } }