diff --git a/crates/cost/src/node/cost_terms.rs b/crates/cost/src/node/cost_terms.rs index 47fce9463..14e0aab26 100644 --- a/crates/cost/src/node/cost_terms.rs +++ b/crates/cost/src/node/cost_terms.rs @@ -54,6 +54,13 @@ pub enum CostConstant { ExactDistinctOperationsPerInsertedValue, ExactDistinctOperationsPerInsertedPair, SketchUpdateOperationsPerCounter, + CounterMatrixUpdateOperationsPerCounter, + UnivmonUpdateOperationsPerCounter, + CmsHeapOperationsPerEstimate, + CmsHeapOperationsPerEstimatedCounter, + CountSketchHeapOperationsPerEstimate, + CountSketchHeapOperationsPerEstimatedCounter, + SketchAllocationOperationsPerCounter, ExactMergeOperationsPerState, ExtremumMergeOperationsPerState, CounterMergeOperationsPerState, @@ -63,9 +70,14 @@ pub enum CostConstant { KllMergeOperationsPerMergedByte, MergeOperationsPerOutputGroup, SketchMergeOperationsPerByte, + CounterMatrixMergeOperationsPerState, + CounterMatrixMergeOperationsPerCounter, + HeapMergeOperationsPerResidentCounter, KllEstimateOperationsPerByte, KllEstimateOperationsPerUnsortedItem, SketchEstimateOperationsPerGroup, + HeapReadOperationsPerGroup, + HeapReadOperationsPerResidentCounter, OutputRowOperationsPerGroup, SketchChainEmitOperationsPerState, ExactChainEmitOperationsPerState, @@ -180,6 +192,40 @@ impl CostConstant { CostConstant::KllMergeOperationsPerOutputGroup => { built_in::KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP } + CostConstant::CounterMatrixUpdateOperationsPerCounter => { + built_in::COUNTER_MATRIX_UPDATE_OPERATIONS_PER_COUNTER + } + CostConstant::UnivmonUpdateOperationsPerCounter => { + built_in::UNIVMON_UPDATE_OPERATIONS_PER_COUNTER + } + CostConstant::CmsHeapOperationsPerEstimate => { + built_in::CMS_HEAP_OPERATIONS_PER_ESTIMATE + } + CostConstant::CmsHeapOperationsPerEstimatedCounter => { + built_in::CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER + } + CostConstant::CountSketchHeapOperationsPerEstimate => { + built_in::COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATE + } + CostConstant::CountSketchHeapOperationsPerEstimatedCounter => { + built_in::COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER + } + CostConstant::SketchAllocationOperationsPerCounter => { + built_in::SKETCH_ALLOCATION_OPERATIONS_PER_COUNTER + } + CostConstant::CounterMatrixMergeOperationsPerState => { + built_in::COUNTER_MATRIX_MERGE_OPERATIONS_PER_STATE + } + CostConstant::CounterMatrixMergeOperationsPerCounter => { + built_in::COUNTER_MATRIX_MERGE_OPERATIONS_PER_COUNTER + } + CostConstant::HeapMergeOperationsPerResidentCounter => { + built_in::HEAP_MERGE_OPERATIONS_PER_RESIDENT_COUNTER + } + CostConstant::HeapReadOperationsPerGroup => built_in::HEAP_READ_OPERATIONS_PER_GROUP, + CostConstant::HeapReadOperationsPerResidentCounter => { + built_in::HEAP_READ_OPERATIONS_PER_RESIDENT_COUNTER + } } } } diff --git a/crates/cost/src/node/cost_terms/tests.rs b/crates/cost/src/node/cost_terms/tests.rs index 6a063f0e2..932956f9b 100644 --- a/crates/cost/src/node/cost_terms/tests.rs +++ b/crates/cost/src/node/cost_terms/tests.rs @@ -50,7 +50,7 @@ fn every_per_unit_constant_has_a_cost_constant() { .iter() .map(|constant| format!("{constant:?}")) .collect(); - assert_eq!(defined.len(), 39, "{defined:?}"); + assert_eq!(defined.len(), 51, "{defined:?}"); assert_eq!( defined.difference(&variants).collect::>(), Vec::<&String>::new(), @@ -103,7 +103,7 @@ fn cost_constant_json_names_are_the_full_snake_case_list() { } /// Every constant's JSON name, in declaration order. -const JSON_NAMES: [&str; 39] = [ +const JSON_NAMES: [&str; 51] = [ "time_range_operations_per_row", "operations_per_group_per_raw_batch", "chain_operations_per_arriving_row", @@ -127,6 +127,13 @@ const JSON_NAMES: [&str; 39] = [ "exact_distinct_operations_per_inserted_value", "exact_distinct_operations_per_inserted_pair", "sketch_update_operations_per_counter", + "counter_matrix_update_operations_per_counter", + "univmon_update_operations_per_counter", + "cms_heap_operations_per_estimate", + "cms_heap_operations_per_estimated_counter", + "count_sketch_heap_operations_per_estimate", + "count_sketch_heap_operations_per_estimated_counter", + "sketch_allocation_operations_per_counter", "exact_merge_operations_per_state", "extremum_merge_operations_per_state", "counter_merge_operations_per_state", @@ -136,9 +143,14 @@ const JSON_NAMES: [&str; 39] = [ "kll_merge_operations_per_merged_byte", "merge_operations_per_output_group", "sketch_merge_operations_per_byte", + "counter_matrix_merge_operations_per_state", + "counter_matrix_merge_operations_per_counter", + "heap_merge_operations_per_resident_counter", "kll_estimate_operations_per_byte", "kll_estimate_operations_per_unsorted_item", "sketch_estimate_operations_per_group", + "heap_read_operations_per_group", + "heap_read_operations_per_resident_counter", "output_row_operations_per_group", "sketch_chain_emit_operations_per_state", "exact_chain_emit_operations_per_state", diff --git a/crates/cost/src/node/operations_per_unit.rs b/crates/cost/src/node/operations_per_unit.rs index b2f271da9..2ea08e2e6 100644 --- a/crates/cost/src/node/operations_per_unit.rs +++ b/crates/cost/src/node/operations_per_unit.rs @@ -1,9 +1,10 @@ //! The CPU operations the formulas charge per unit of work: per row a time //! range keeps, per row a hash aggregate groups, per row folded into a //! summary state, per row and label column a grouping carries, per group -//! and raw batch a grouping visits, per state merged and per byte of a KLL -//! state merged or read, per output group of a merge, per group read out -//! and per output row built. +//! and raw batch a grouping visits, per counter a sketch state allocates, +//! per state merged and per byte of a KLL state or counter of a sketch +//! merged or read, per heap item re-estimated, per output group of a +//! merge, per group read out and per output row built. //! //! One CPU operation is one nanosecond of the reference executor's work on //! one core: each count below is the executor's measured time for that @@ -414,14 +415,95 @@ pub(crate) fn exact_distinct_value_terms_per_row( ) } -/// One counter update of any other sketch, per counter its row updates -/// ([`counters_updated_per_row`]), above +/// One counter update of a Hydra or HyperLogLog sketch, or of a sample, per +/// counter its row updates ([`counters_updated_per_row`]), above /// `EXACT_UPDATE_OPERATIONS_PER_ROW`, the executor's cost of handing a row -/// to its group's state. Not measured on the executor: `asap_sketchlib`'s -/// own Count-Min Sketch of depth 3 with a top-k heap updates in about -/// 72 ns, 18 ns per counter. +/// to its group's state. Not measured on the executor; taken from +/// `asap_sketchlib`'s own Count-Min Sketch of depth 3 with a top-k heap, +/// which updates in about 72 ns, 18 ns per counter. The counter-matrix sketches (Count-Min, Count +/// Sketch, their heap variants, UnivMon) have measured constants below. pub(crate) const SKETCH_UPDATE_OPERATIONS_PER_COUNTER: f64 = 18.0; +/// The constants below, for the counter-matrix sketches (Count-Min and +/// Count Sketch, with and without a top-k heap, and UnivMon), are fitted on +/// the executor's per-group builds, readouts and merges: 235 builds of 1 to +/// 1,000 states of 10 to 100,000 rows each, keyed by 10 to 10,000 distinct +/// items; Count-Min with heap at widths 272–2,719 and depths 5–12, Count +/// Sketch with heap at widths 300–120,000 and depths 47–155, UnivMon at +/// 624–56,817 columns of 5 rows (one layer), heaps of 10 and 50 items, and +/// plain Count-Min (its kernel alone; the executor builds it through no +/// plan) at widths 272–2,719. Each time is the node's own thread CPU, median +/// of 3 runs on one pinned core whose sibling was idle, after spinning the +/// core so its clock was up. The form of each term is the work the kernel +/// does; the constants minimize the squared relative error, with the +/// executor's hand-off (`EXACT_UPDATE_OPERATIONS_PER_ROW`), group visits +/// and output rows fixed. Leaving out each (states, rows, items, heap) +/// setting in turn, the builds are predicted within 0.313 RMS log error, +/// each (width, depth) shape 0.319; the old charges (18 per counter +/// updated, nothing per state) were 3.1 RMS log under, up to 800x; +/// measured ÷ charged is now 0.55–4.94. 17 ingestion-time builds, held +/// out, measure 0.59–2.42x their charge (old: 4–246x). The largest errors +/// are states of 2.8 MB or more, whose allocation the allocator serves +/// either from memory it keeps or by faulting in fresh pages: one +/// 120,000 x 47 Count Sketch measures 4.9x its charge, 100 states of +/// 30,000 x 81 0.55x. +/// +/// Per counter a Count-Min or Count Sketch row updates (one per row of +/// depth), above `EXACT_UPDATE_OPERATIONS_PER_ROW` (fitted 19.2 on the +/// plain Count-Min kernel, which Count Sketch shares). Example: depth 5, +/// 29 + 19 x 5 = 124 per row. +pub(crate) const COUNTER_MATRIX_UPDATE_OPERATIONS_PER_COUNTER: f64 = 19.0; + +/// Per counter a UnivMon row updates ([`counters_updated_per_row`]: its +/// rows and its heap, per layer it reaches), above +/// `EXACT_UPDATE_OPERATIONS_PER_ROW` (fitted 95.0): each counter's hash is +/// computed apart. Example: 5 rows, one layer, 29 + 95 x 6 = 599 per row, +/// measured 623–665 ns at 100,000 rows in one state. +pub(crate) const UNIVMON_UPDATE_OPERATIONS_PER_COUNTER: f64 = 95.0; + +/// Per estimate a Count-Min Sketch with heap computes per row, beyond the +/// counters it reads (`CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER`): the +/// executor's weighted-frequency kernel (`update_key`) calls +/// `rescore_heap` after each update, which re-estimates every item the heap +/// holds, then estimates the row's own item to offer it to the heap, so a +/// row costs one estimate per resident plus one ([`heap_residents_per_update`]), +/// each hashing the item's key and reading one counter per row of depth +/// (fitted 153 per estimate and 33.2 per counter read). Example: depth 10, +/// heap of 10 full: 29 + 11 x (150 + 33 x 10) = 5,309 per row, measured 5.0–5.1 +/// µs at 1,000 and 10,000 rows per state, against 227 charged before. +/// `asap_sketchlib`'s own heap update estimates only the row's item; once +/// the kernel uses it, this constant, `CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER` +/// and the two Count Sketch ones must be measured again. +pub(crate) const CMS_HEAP_OPERATIONS_PER_ESTIMATE: f64 = 150.0; + +/// Per counter a Count-Min Sketch with heap reads per estimate (see +/// `CMS_HEAP_OPERATIONS_PER_ESTIMATE`). +pub(crate) const CMS_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER: f64 = 33.0; + +/// Per estimate a Count Sketch with heap computes per row, as +/// `CMS_HEAP_OPERATIONS_PER_ESTIMATE` (the same kernel re-estimates every +/// resident per row, and the same re-measurement applies), beyond the counters it reads +/// (`COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER`; fitted 58.1 and +/// 36.3: a Count Sketch estimate is the median of its rows). Example: +/// depth 81, heap of 10 full: 11 x (58 + 36 x 81) = 32,714 per row, +/// measured 33–40 µs at 1,000 and 10,000 rows per state, against 1,505 +/// charged before. +pub(crate) const COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATE: f64 = 58.0; + +/// Per counter a Count Sketch with heap reads per estimate (see +/// `COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATE`). +pub(crate) const COUNT_SKETCH_HEAP_OPERATIONS_PER_ESTIMATED_COUNTER: f64 = 36.0; + +/// Per counter a counter-matrix sketch's state allocates and zeroes when +/// it is built ([`sketch_state_counters`]), once per state: by a +/// `SummaryAgg` for each group, by a tumbling-window chain for each window's +/// state (fitted 2.1, one constant for every family). Example: a +/// 7,500 x 81 Count Sketch (607,500 counters) costs 1.28 ms per state +/// before its first row; 100 such states of 100 rows measure 3.6 ms each +/// more than 300 x 81 ones, charged 1.22 ms more (the allocator's regime +/// for states this large, see above). +pub(crate) const SKETCH_ALLOCATION_OPERATIONS_PER_COUNTER: f64 = 2.1; + /// One exact accumulator state merged into its group's (sum, count): /// 128–265 ns per state over 360,000 states. pub(crate) const EXACT_MERGE_OPERATIONS_PER_STATE: f64 = 230.0; @@ -486,13 +568,35 @@ pub(crate) const KLL_MERGE_OPERATIONS_PER_MERGED_BYTE: f64 = 0.43; /// finalize or an estimate also reads each state into a new value column. pub(crate) const MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 1_100.0; -/// Per byte of any other sketch state merged, above +/// Per byte of a Hydra or HyperLogLog state merged, above /// `EXACT_MERGE_OPERATIONS_PER_STATE`: merging adds every counter of the /// input state. Not measured on the executor: `asap_sketchlib` merges a /// Count-Min Sketch of 16 KB in about 6 µs and a HyperLogLog of 16 KB in /// about 13 µs, 0.4–0.8 ns per byte. pub(crate) const SKETCH_MERGE_OPERATIONS_PER_BYTE: f64 = 1.0; +/// One counter-matrix sketch state merged into its group's, beyond its +/// counters (`COUNTER_MATRIX_MERGE_OPERATIONS_PER_COUNTER`) and a heap's +/// residents (`HEAP_MERGE_OPERATIONS_PER_RESIDENT_COUNTER`). Fitted with +/// them on 126 executor merges of 10 to 1,000 Count-Min with heap, Count +/// Sketch with heap and UnivMon states into one (2,302, 2.86 and 52.6): +/// leaving out each setting 0.196 RMS log error, each shape 0.208, each +/// family 0.239; measured ÷ charged 0.80–2.75, where the old charge (230 +/// plus 1 per byte) was 0.34–3.49 (0.812 RMS log). +pub(crate) const COUNTER_MATRIX_MERGE_OPERATIONS_PER_STATE: f64 = 2_300.0; + +/// Per counter of a counter-matrix sketch state merged: the executor copies +/// the group's state and adds the input's counters to it. Example: a +/// 7,500 x 81 Count Sketch with a heap of 10 merges in 2,300 + 2.9 x +/// 607,500 + 53 x 10 x 81 +/// = 1.81 ms, measured 1.93 (100 states into one). +pub(crate) const COUNTER_MATRIX_MERGE_OPERATIONS_PER_COUNTER: f64 = 2.9; + +/// Per counter a heap's residents are re-estimated from when a state with +/// a heap is merged: each resident of the input state's heap is estimated +/// over every row of depth (see `COUNTER_MATRIX_MERGE_OPERATIONS_PER_STATE`). +pub(crate) const HEAP_MERGE_OPERATIONS_PER_RESIDENT_COUNTER: f64 = 53.0; + /// Per byte of the merged KLL state one group's answer is read from, /// before its output row: the quantile sorts the items the state holds, /// whose levels a merge leaves sorted. The bytes are those the planner @@ -525,6 +629,27 @@ pub(crate) const KLL_ESTIMATE_OPERATIONS_PER_UNSORTED_ITEM: f64 = 40.0; /// Count-Min Sketch top-k query 0.5–8 µs. pub(crate) const SKETCH_ESTIMATE_OPERATIONS_PER_GROUP: f64 = 1_000.0; +/// One group's top-k read from a Count-Min or Count Sketch state with a +/// heap, before its output row, beyond the counters it reads +/// (`HEAP_READ_OPERATIONS_PER_RESIDENT_COUNTER`): the executor estimates +/// each item the heap holds and sorts them. Fitted on 158 executor +/// readouts of 1 to 1,000 states (3,951 per group and 55.0 per counter +/// read, beside a constant 19 µs per execution, which is not charged): +/// leaving out each setting 0.263 RMS log error, each shape 0.268; +/// measured ÷ charged 0.66–2.14 over 10 to 1,000 states (up to 5.5 over one +/// state, where that constant dominates), against 2.1–89 for the 1,000 +/// charged before. Charged with [`heap_residents`], which counts a state's +/// distinct items, the range is 0.66–2.65: at its 26 points of states of +/// 10 rows of 10 keys the bench cycles the keys, so every state holds all +/// 10, where 10 uniform draws hold 6.3. Example: depth 81, heap of 10 full: 4,000 + 55 x 10 x +/// 81 + the output row 2,500 = 51 µs per group, measured 36 (width 300) +/// and 71 (width 7,500) over 100 states. +pub(crate) const HEAP_READ_OPERATIONS_PER_GROUP: f64 = 4_000.0; + +/// Per counter a heap's top-k read reads: one per row of depth for each +/// resident (see `HEAP_READ_OPERATIONS_PER_GROUP`). +pub(crate) const HEAP_READ_OPERATIONS_PER_RESIDENT_COUNTER: f64 = 55.0; + /// One group's output row built from its value, with the group's labels /// and series identity, by a hash aggregate, a finalize or an estimate (a /// top-k estimate emits `k` rows per group and is charged one): a @@ -563,8 +688,8 @@ pub(crate) const KLL_MERGE_OPERATIONS_PER_OUTPUT_GROUP: f64 = 19_000.0; /// The operations one input row costs folded into a state of `summary_type` /// with `parameters`, which folds `rows_per_state` rows (`None`: unknown) /// of a column taking `distinct_values` values (`None`: every row's -/// differs). Only a KMV's depends on them -/// ([`kmv_compared_hashes_per_row`]). +/// differs). Only a KMV's ([`kmv_compared_hashes_per_row`]) and a heap +/// sketch's ([`heap_residents_per_update`]) depend on them. pub(crate) fn update_terms_per_row( summary_type: &FieldDataType, parameters: Option<&SketchParams>, @@ -590,21 +715,57 @@ pub(crate) fn update_terms_per_row( kmv_compared_hashes_per_row(*k, rows_per_state, distinct_values), ) } + (_, Some(SketchParams::Cms { depth, .. } | SketchParams::CountSketch { depth, .. })) => { + NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0).plus( + CostConstant::CounterMatrixUpdateOperationsPerCounter, + f64::from(*depth), + ) + } ( _, - Some( - parameters @ (SketchParams::Hydra(_) - | SketchParams::Cms { .. } - | SketchParams::CountSketch { .. } - | SketchParams::CmsWithHeap { .. } - | SketchParams::CountSketchWithHeap { .. } - | SketchParams::UnivMon { .. } - | SketchParams::Hll { .. }), - ), - ) => NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0).plus( - CostConstant::SketchUpdateOperationsPerCounter, - counters_updated_per_row(parameters), - ), + Some(SketchParams::CmsWithHeap { + depth, heap_size, .. + }), + ) => { + let estimates = + heap_residents_per_update(*heap_size, rows_per_state, distinct_values) + 1.0; + NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0) + .plus(CostConstant::CmsHeapOperationsPerEstimate, estimates) + .plus( + CostConstant::CmsHeapOperationsPerEstimatedCounter, + estimates * f64::from(*depth), + ) + } + ( + _, + Some(SketchParams::CountSketchWithHeap { + depth, heap_size, .. + }), + ) => { + let estimates = + heap_residents_per_update(*heap_size, rows_per_state, distinct_values) + 1.0; + NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0) + .plus( + CostConstant::CountSketchHeapOperationsPerEstimate, + estimates, + ) + .plus( + CostConstant::CountSketchHeapOperationsPerEstimatedCounter, + estimates * f64::from(*depth), + ) + } + (_, Some(parameters @ SketchParams::UnivMon { .. })) => { + NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0).plus( + CostConstant::UnivmonUpdateOperationsPerCounter, + counters_updated_per_row(parameters), + ) + } + (_, Some(parameters @ (SketchParams::Hydra(_) | SketchParams::Hll { .. }))) => { + NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0).plus( + CostConstant::SketchUpdateOperationsPerCounter, + counters_updated_per_row(parameters), + ) + } (_, None) => NodeCostTerms::of(CostConstant::ExactUpdateOperationsPerRow, 1.0) .plus(CostConstant::SketchUpdateOperationsPerCounter, 1.0), } @@ -651,18 +812,17 @@ pub(crate) fn kmv_compared_hashes_per_row( } /// The counters one input row updates in a sketch with `parameters`: one -/// per row of depth for Count-Min and Count Sketch, plus one heap update for -/// their top-k variants; for UnivMon, the same per layer it updates -/// (`univmon_expected_layers`); one for every other sketch. Example: a -/// Count-Min Sketch of depth 3 with a heap updates 4. +/// per row of depth for Count-Min and Count Sketch (a top-k heap's work is +/// charged apart, [`update_terms_per_row`]); for UnivMon, its rows and its +/// heap per layer it updates (`univmon_expected_layers`); one for every +/// other sketch. Example: a Count-Min Sketch of depth 3 updates 3. pub(crate) fn counters_updated_per_row(parameters: &SketchParams) -> f64 { match parameters { SketchParams::Hydra(hydra) => counters_updated_per_row(&hydra.per_subpopulation()), - SketchParams::Cms { depth, .. } | SketchParams::CountSketch { depth, .. } => { - f64::from(*depth) - } - SketchParams::CmsWithHeap { depth, .. } - | SketchParams::CountSketchWithHeap { depth, .. } => f64::from(*depth) + 1.0, + SketchParams::Cms { depth, .. } + | SketchParams::CountSketch { depth, .. } + | SketchParams::CmsWithHeap { depth, .. } + | SketchParams::CountSketchWithHeap { depth, .. } => f64::from(*depth), SketchParams::UnivMon { sketch_rows, layers, @@ -675,6 +835,94 @@ pub(crate) fn counters_updated_per_row(parameters: &SketchParams) -> f64 { } } +/// The counters a state of a counter-matrix sketch with `parameters` +/// allocates and zeroes when it is built: `width x depth` for Count-Min and +/// Count Sketch, with or without a heap, and `columns x rows` per layer for +/// UnivMon; none for any other sketch, whose state grows with its rows +/// (their update charges it). Example: a 7,500 x 81 Count Sketch with heap +/// has 607,500; a UnivMon of 5 rows of 2,367 columns in 1 layer 11,835. +fn sketch_state_counters(parameters: &SketchParams) -> f64 { + match parameters { + SketchParams::Cms { width, depth } + | SketchParams::CountSketch { width, depth } + | SketchParams::CmsWithHeap { width, depth, .. } + | SketchParams::CountSketchWithHeap { width, depth, .. } => { + f64::from(*width) * f64::from(*depth) + } + SketchParams::UnivMon { + sketch_rows, + sketch_cols, + layers, + .. + } => f64::from(*sketch_rows) * f64::from(*sketch_cols) * f64::from(*layers), + SketchParams::Hydra(_) + | SketchParams::Kll { .. } + | SketchParams::DDSketch { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. } => 0.0, + } +} + +/// The operations allocating and zeroing one state of a sketch with +/// `parameters` costs ([`sketch_state_counters`] at +/// `SKETCH_ALLOCATION_OPERATIONS_PER_COUNTER`), paid once per state built. +/// Example: a 7,500 x 81 Count Sketch costs 1,275,750. +pub(crate) fn state_allocation_terms(parameters: Option<&SketchParams>) -> NodeCostTerms { + parameters.map_or_else(NodeCostTerms::default, |parameters| { + NodeCostTerms::of( + CostConstant::SketchAllocationOperationsPerCounter, + sketch_state_counters(parameters), + ) + }) +} + +/// The expected distinct items among `rows` uniform draws of +/// `distinct_items` (`None`: every row's differs). +fn distinct_items_seen(rows: f64, distinct_items: Option) -> f64 { + distinct_items.map_or(rows, |items| { + expected_distinct_values(rows, items.max(1) as f64) + }) +} + +/// The items a top-k heap of `heap_size` holds when an update arrives, +/// averaged over a state's `rows_per_state` rows (`None`: a full heap), +/// each one of `distinct_items` items (`None`: every row's differs): the +/// distinct items seen so far, at most `heap_size`. Averaged over 16 equal +/// slices of the rows. Example: a heap of 10 over 10 rows of 10 items +/// holds 3.7 on average, over 100 rows of 100 items 9.5. +fn heap_residents_per_update( + heap_size: u32, + rows_per_state: Option, + distinct_items: Option, +) -> f64 { + const SLICES: u32 = 16; + let heap = f64::from(heap_size); + let Some(rows) = rows_per_state else { + return heap; + }; + (0..SLICES) + .map(|slice| { + let row = (f64::from(slice) + 0.5) * rows as f64 / f64::from(SLICES); + distinct_items_seen(row, distinct_items).min(heap) + }) + .sum::() + / f64::from(SLICES) +} + +/// The items a top-k heap of `heap_size` holds once its state has folded +/// the rows `input_rows` says (`None`, or rows unknown: a full heap): the +/// distinct items among them, at most `heap_size`, as +/// [`heap_residents_per_update`] counts them. Example: a heap of 10 over 4 +/// rows of distinct items holds 4; over 1,000 rows of 5 items, 5. +fn heap_residents(heap_size: u32, input_rows: Option) -> f64 { + let heap = f64::from(heap_size); + input_rows + .and_then(|rows| Some((rows.rows_per_state?, rows.distinct_values))) + .map_or(heap, |(rows, distinct_items)| { + distinct_items_seen(rows as f64, distinct_items).min(heap) + }) +} + /// Layers a UnivMon row updates in expectation. `asap_sketchlib` updates /// layers `0..=b`, with bottom layer `b` the index of the first zero among /// hash bits `1..layers`, capped at `layers - 1`; so it reaches layer `j` @@ -696,7 +944,9 @@ fn univmon_expected_layers(layers: u8) -> f64 { /// hashes of the values it folded, so the states draw theirs from the same /// smallest hashes of the column's values and share some: of `V` values, a /// state retaining `h` hashes of the `D` it folded draws them from the -/// `V h / D` smallest. Example: a KLL state of 872 bytes merged into one of +/// `V h / D` smallest. A counter-matrix sketch (Count-Min, Count Sketch, +/// UnivMon) merges per counter, and a heap variant also re-estimates its +/// heap's residents ([`heap_residents`]). Example: a KLL state of 872 bytes merged into one of /// 33,512 costs 360 + 0.1 × 872 + 0.43 × 33,512 = 14,857. Five 1-minute /// KMV states at k = 4,323 of 12,000 rows each of 1,000,000 users retain /// 4,323 hashes each, drawn from the 362,000 smallest: 3.40 ms per state, @@ -739,13 +989,7 @@ pub(crate) fn merge_terms_per_state( // are unknown (a state never folds more). let folded_values = input_rows .and_then(|rows| rows.rows_per_state) - .map(|rows| { - let rows = rows as f64; - values.map_or(rows, |values| { - let values = values.max(1) as f64; - expected_distinct_values(rows, values) - }) - }) + .map(|rows| distinct_items_seen(rows as f64, values)) .or(values.map(|values| values.max(1) as f64)); let retained_hashes = folded_values.map_or(f64::from(*k), |folded| folded.min(f64::from(*k))); @@ -763,17 +1007,35 @@ pub(crate) fn merge_terms_per_state( .times(retained_hashes) } StateFamily::Sketch( - SketchParams::Hydra(_) - | SketchParams::Cms { .. } + parameters @ (SketchParams::Cms { .. } | SketchParams::CountSketch { .. } - | SketchParams::CmsWithHeap { .. } - | SketchParams::CountSketchWithHeap { .. } - | SketchParams::UnivMon { .. } - | SketchParams::Hll { .. }, - ) => NodeCostTerms::of(CostConstant::ExactMergeOperationsPerState, 1.0).plus( - CostConstant::SketchMergeOperationsPerByte, - state_bytes as f64, + | SketchParams::UnivMon { .. }), + ) => NodeCostTerms::of(CostConstant::CounterMatrixMergeOperationsPerState, 1.0).plus( + CostConstant::CounterMatrixMergeOperationsPerCounter, + sketch_state_counters(parameters), ), + StateFamily::Sketch( + parameters @ (SketchParams::CmsWithHeap { + depth, heap_size, .. + } + | SketchParams::CountSketchWithHeap { + depth, heap_size, .. + }), + ) => NodeCostTerms::of(CostConstant::CounterMatrixMergeOperationsPerState, 1.0) + .plus( + CostConstant::CounterMatrixMergeOperationsPerCounter, + sketch_state_counters(parameters), + ) + .plus( + CostConstant::HeapMergeOperationsPerResidentCounter, + heap_residents(*heap_size, input_rows) * f64::from(*depth), + ), + StateFamily::Sketch(SketchParams::Hydra(_) | SketchParams::Hll { .. }) => { + NodeCostTerms::of(CostConstant::ExactMergeOperationsPerState, 1.0).plus( + CostConstant::SketchMergeOperationsPerByte, + state_bytes as f64, + ) + } } } @@ -842,23 +1104,28 @@ pub(crate) fn chain_emit_terms_per_state(summary_type: &FieldDataType) -> NodeCo } /// How the summary state an estimate reads was last written, which a -/// KLL's read depends on. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// KLL's and a top-k heap's read depend on. +#[derive(Debug, Clone, Copy, PartialEq)] pub(crate) enum StateLastWrittenBy { - /// A merge, which leaves a KLL's levels sorted. - Merge, - /// Updates of `rows_per_state` rows each (`None` when unknown), whose - /// newest items a KLL sorts when read. - Updates { rows_per_state: Option }, + /// A merge of states folding the rows `input_rows` says (`None` when + /// unknown), which leaves a KLL's levels sorted and whose distinct + /// items a heap holds. + Merge { input_rows: Option }, + /// Updates of the rows `input_rows` says each state folds (`None` when + /// unknown), whose newest items a KLL sorts when read and whose + /// distinct items a heap holds. + Updates { input_rows: Option }, } /// The operations reading one group's answer from a state of `family` /// holding `state_bytes`, last written by `written_by`, costs, its output /// row included. A KLL state last written by a merge is read per byte; one /// built by updates per unsorted item, at most its `k` (every one when its -/// rows are unknown). Example: a DDSketch quantile costs 1,000 + 2,500 = -/// 3,500 per group; a KLL at k = 547 of 4,844 bytes 6,860 after a merge -/// and, built from 300 rows, 2,500 + 40 × 300 = 14,500 after updates. +/// rows are unknown). A Count-Min or Count Sketch heap's top-k estimates +/// each item its heap holds, over every row of depth. Example: a DDSketch +/// quantile costs 1,000 + 2,500 = 3,500 per group; a KLL at k = 547 of +/// 4,844 bytes 6,860 after a merge and, built from 300 rows, 2,500 + 40 × +/// 300 = 14,500 after updates. pub(crate) fn estimate_terms_per_group( family: StateFamily<'_>, state_bytes: u64, @@ -867,11 +1134,12 @@ pub(crate) fn estimate_terms_per_group( let output_row = NodeCostTerms::of(CostConstant::OutputRowOperationsPerGroup, 1.0); match family { StateFamily::Sketch(SketchParams::Kll { k }) => match written_by { - StateLastWrittenBy::Merge => output_row.plus( + StateLastWrittenBy::Merge { .. } => output_row.plus( CostConstant::KllEstimateOperationsPerByte, state_bytes as f64, ), - StateLastWrittenBy::Updates { rows_per_state } => { + StateLastWrittenBy::Updates { input_rows } => { + let rows_per_state = input_rows.and_then(|rows| rows.rows_per_state); let k = u64::from(*k); let unsorted_items = rows_per_state.map_or(k, |rows| rows.min(k)); output_row.plus( @@ -880,13 +1148,28 @@ pub(crate) fn estimate_terms_per_group( ) } }, + StateFamily::Sketch( + SketchParams::CmsWithHeap { + depth, heap_size, .. + } + | SketchParams::CountSketchWithHeap { + depth, heap_size, .. + }, + ) => { + let (StateLastWrittenBy::Merge { input_rows } + | StateLastWrittenBy::Updates { input_rows }) = written_by; + output_row + .plus(CostConstant::HeapReadOperationsPerGroup, 1.0) + .plus( + CostConstant::HeapReadOperationsPerResidentCounter, + heap_residents(*heap_size, input_rows) * f64::from(*depth), + ) + } StateFamily::Sketch( SketchParams::DDSketch { .. } | SketchParams::Hydra(_) | SketchParams::Cms { .. } | SketchParams::CountSketch { .. } - | SketchParams::CmsWithHeap { .. } - | SketchParams::CountSketchWithHeap { .. } | SketchParams::UnivMon { .. } | SketchParams::Hll { .. } | SketchParams::Kmv { .. }, diff --git a/crates/cost/src/node/state_bytes/mod.rs b/crates/cost/src/node/state_bytes/mod.rs index 4efa09ff4..c55668553 100644 --- a/crates/cost/src/node/state_bytes/mod.rs +++ b/crates/cost/src/node/state_bytes/mod.rs @@ -115,7 +115,7 @@ pub fn summary_state_bytes( /// a KLL's, a DDSketch's and a KMV's do): the input rows it folds, the /// sign of their values and, for a DDSketch, their magnitudes; and the /// distinct values of the column it folds, which a KMV build's and merge's -/// CPU depend on. +/// CPU and the items a top-k heap holds depend on. /// The rows are whatever its producer emits (raw data samples, rates, sums), /// not a summary sample. #[derive(Debug, Clone, Copy, PartialEq)] @@ -133,13 +133,16 @@ pub struct StateInputRows { /// `DDSKETCH_ASSUMED_MAGNITUDE_RANGE`. E.g. `Some(ValueMagnitudeRange { /// min_abs: 0.001, max_abs: 10000.0 })` for a latency in seconds. pub value_magnitude_range: Option, - /// The distinct values the folded column takes in one state, each - /// folded row a draw from them: the fewer of the column's declared - /// distinct values and the sources' bound per group; `None` when - /// neither is declared, which takes every row's value as new. E.g. - /// `Some(1_000_000)` for `user_id` over 1,000,000 users: KMV states of - /// 12,000 rows each then fold about 11,929 distinct users, and states - /// merged together share some of their retained hashes. + /// The distinct values the folded column takes in one state (a keyed + /// summary's item column, else its weight column), each folded row a + /// draw from them: the fewer of the column's declared distinct values + /// and the sources' bound per group; `None` when neither is declared, + /// which takes every row's value as new. E.g. `Some(1_000_000)` for + /// `user_id` over 1,000,000 users: KMV states of 12,000 rows each then + /// fold about 11,929 distinct users, and states merged together share + /// some of their retained hashes. `Some(100)` for a top-k heap keyed by + /// a `url` declaring 100 values: over 1,000 rows it holds at most 100 + /// items. pub distinct_values: Option, } diff --git a/crates/cost/src/node/usage/mod.rs b/crates/cost/src/node/usage/mod.rs index 04a1ce866..2a67c1468 100644 --- a/crates/cost/src/node/usage/mod.rs +++ b/crates/cost/src/node/usage/mod.rs @@ -25,8 +25,8 @@ use crate::node::analytical::{ use crate::node::cost_terms::{CostConstant, NodeCostTerms}; use crate::node::operations_per_unit::{ chain_emit_terms_per_state, estimate_terms_per_group, hash_aggregate_terms_per_row, - merge_output_terms, merge_terms_per_state, pass_through_merge_terms, update_terms_per_row, - StateLastWrittenBy, + merge_output_terms, merge_terms_per_state, pass_through_merge_terms, state_allocation_terms, + update_terms_per_row, StateLastWrittenBy, }; use crate::node::operator_statistics::{ EdgeStatistics, OperatorStatistics, PartitionStatistics, UnaryEdgeStatistics, @@ -240,13 +240,15 @@ pub struct ProducerOutput<'a> { } impl ProducerOutput<'_> { - /// The source column `weight` reads, when this producer passes that - /// column on unchanged from the sources: the column whose values a - /// summary built over this producer folds. E.g. `value` for a DDSketch - /// over a scan of `latency` or over a filter on that scan; `None` for a - /// computed weight such as a rate. - pub(crate) fn unchanged_column(&self, weight: &SummaryInputExpr) -> Option<&str> { - let SummaryInputExpr::Column(column) = weight else { + /// The source column `expr` reads, when `expr` is a single column this + /// producer passes on unchanged from the sources: the column whose + /// values a summary built over this producer folds, its weight or a + /// keyed summary's item. E.g. `value` for a DDSketch over a scan of + /// `latency` or over a filter on that scan, `url` for a top-k heap keyed + /// by `url`; `None` for a computed weight such as a rate, or a tuple + /// or entity-identity item. + pub(crate) fn unchanged_column(&self, expr: &SummaryInputExpr) -> Option<&str> { + let SummaryInputExpr::Column(column) = expr else { return None; }; let index = resolve_column_ref(column, self.output_schema).ok()?; @@ -635,13 +637,15 @@ pub(crate) fn node_usage_and_cost_terms<'a>( Some(total.saturating_add(data.sources.get(*source)?.max_distinct_per_evaluation?)) }) }; - // The values the column a summary built here folds takes in one state: - // the fewer of its declared distinct values and the sources' bound per - // output row; `None` when neither is declared. + // The values the column a summary built here folds takes in one state + // (a keyed summary's item column, e.g. the `url` a top-k heap counts; + // else its weight column): the fewer of its declared distinct values + // and the sources' bound per output row; `None` when neither is + // declared. let folded_distinct_values = |update: &SummaryUpdate| -> Option { - let column = producers - .first() - .and_then(|producer| producer.unchanged_column(&update.weight)); + let column = producers.first().and_then(|producer| { + producer.unchanged_column(update.item.as_ref().unwrap_or(&update.weight)) + }); [declared_distinct_values(column), distinct_items_per_group()] .into_iter() .flatten() @@ -1122,15 +1126,25 @@ pub(crate) fn node_usage_and_cost_terms<'a>( .ok_or(AnalyticalCostError::Overflow)?, }; let group_visits = visits(group_count); + // Each group's state allocated and zeroed (a counter-matrix + // sketch's counters), before its first row. + let allocation_terms = state_allocation_terms(node.parameters.as_ref()); + let allocation_ops = allocation_terms.operations(); + let allocation_detail = if allocation_ops > 0.0 { + format!(", {allocation_ops} ops to allocate each state") + } else { + String::new() + }; let terms = update_terms .times(input.rows as f64) - .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits); + .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits) + .plus_terms(&allocation_terms.times(group_count as f64)); ( out, Ok(charged(terms, out.bytes, 0)), format!( "build {} into {group_count} states: {} rows x {update_ops} ops, \ - {group_visits:.0} group visits", + {group_visits:.0} group visits{allocation_detail}", summary_type_name(family), input.rows ), @@ -1186,10 +1200,10 @@ pub(crate) fn node_usage_and_cost_terms<'a>( ) .plus_terms(&carried_label_terms_per_row(&node.output_schema, reduction)); let update_ops = update_terms.operations(); - // The states it completes in one execution: at ingestion time - // one second's share of each group's window (a 10 s window: a - // tenth of a state per group); at query time every retained - // window's. + // The states it completes in one execution, each allocated + // when its window opens: at ingestion time one second's share + // of each group's window (a 10 s window: a tenth of a state per + // group); at query time every retained window's. let emitted_states = match ingestion { true => { group_count as f64 * ONE_SECOND_MS as f64 / *tumbling_window_width_ms as f64 @@ -1219,7 +1233,11 @@ pub(crate) fn node_usage_and_cost_terms<'a>( .times(input.rows as f64) .plus(range_constant, input.rows as f64) .plus(CostConstant::OperationsPerGroupPerRawBatch, group_visits) - .plus_terms(&chain_emit_terms_per_state(family).times(emitted_states)), + .plus_terms( + &chain_emit_terms_per_state(family) + .plus_terms(&state_allocation_terms(node.parameters.as_ref())) + .times(emitted_states), + ), out.bytes, 0, )), @@ -1323,12 +1341,12 @@ pub(crate) fn node_usage_and_cost_terms<'a>( Some(producer) if matches!(producer.payload, Payload::ASAP(ASAPOp::SummaryMerge { .. })) => { - StateLastWrittenBy::Merge + StateLastWrittenBy::Merge { + input_rows: producer.state_input_rows, + } } producer => StateLastWrittenBy::Updates { - rows_per_state: producer - .and_then(|producer| producer.state_input_rows) - .and_then(|rows| rows.rows_per_state), + input_rows: producer.and_then(|producer| producer.state_input_rows), }, }; let per_group = match query { diff --git a/crates/cost/src/node/usage/tests.rs b/crates/cost/src/node/usage/tests.rs index 199c47adb..3568904d7 100644 --- a/crates/cost/src/node/usage/tests.rs +++ b/crates/cost/src/node/usage/tests.rs @@ -3649,3 +3649,326 @@ fn aggregate_needs_every_measure() { usage.work_ms_per_execution ); } + +/// What one counter-matrix sketch (Count-Min or Count Sketch with heap, +/// UnivMon) measured on the executor did: `groups` states of +/// `rows_per_state` rows each, every row one of `items` distinct items, as +/// [`counter_sketch_charges_ms`] charges them. +struct CounterSketchShape { + parameters: SketchParams, + groups: u64, + rows_per_state: u64, + items: u64, +} + +/// What a [`CounterSketchShape`] is charged, in ms per execution: the +/// query-time build (a `SummaryAgg` keyed by `key`, one state per `g`, over +/// finalized rows, not raw ones), its readout (a top-10 or an L2), a merge +/// of every state into one, and the same build at ingestion time (a +/// tumbling-window chain of 60 s windows, each the shape's rows). +struct CounterSketchCharges { + build_ms: f64, + readout_ms: f64, + merge_ms: f64, + ingestion_build_ms: f64, +} + +fn counter_sketch_charges_ms(shape: &CounterSketchShape) -> CounterSketchCharges { + use asap_types::ir::schema::DataType; + use asap_types::summary_type::{SketchAlgorithm, SummaryInputExpr}; + let settings = CostInputs::default(); + let data: DataWorkload = serde_json::from_value(serde_json::json!({ + "arrival": {"ingesting": {"rows_per_second": 1000.0}}, + "sources": {"t": {"distinct_values_per_label": {"g": shape.groups, "key": shape.items}}} + })) + .unwrap(); + let keyed = |weight| SummaryUpdate { + item: Some(SummaryInputExpr::Column(ColumnRef::Named("key".into()))), + weight, + }; + let value = || SummaryInputExpr::Column(ColumnRef::Named("value".into())); + let top_10 = SketchStatistic::TopK { k: 10 }; + let (algorithm, update, statistic) = match shape.parameters { + SketchParams::UnivMon { .. } => ( + SketchAlgorithm::UnivMon, + keyed(SummaryInputExpr::Constant(1.0)), + SketchStatistic::FrequencyL2, + ), + SketchParams::CmsWithHeap { .. } => (SketchAlgorithm::CmsWithHeap, keyed(value()), top_10), + SketchParams::CountSketchWithHeap { .. } => { + (SketchAlgorithm::CountSketchWithHeap, keyed(value()), top_10) + } + SketchParams::Cms { .. } + | SketchParams::CountSketch { .. } + | SketchParams::Hydra(_) + | SketchParams::Kll { .. } + | SketchParams::DDSketch { .. } + | SketchParams::Hll { .. } + | SketchParams::Kmv { .. } => panic!("a counter-matrix sketch measured on the executor"), + }; + let rows_schema = Schema::new(vec![ + Field::new("g", FieldDataType::Plain(DataType::Utf8), false), + Field::new("key", FieldDataType::Plain(DataType::Utf8), false), + Field::new("value", FieldDataType::Plain(DataType::Float64), false), + ]); + let rows_node = PhysicalASAPDAGNode { + output_schema: rows_schema.clone(), + ..node(Payload::NonASAP(NonASAPOp::TimeRange { + range: Duration::from_secs(60), + kind: TimeRangeKind::Range, + child: 0, + })) + }; + let rows = |count: u64| ProducerOutput { + payload: &rows_node.payload, + output_schema: &rows_node.output_schema, + output: EdgeStatistics { + rows: count, + bytes: count * 48, + }, + sources: BTreeSet::from(["t"]), + family: StateFamily::None, + unchanged_columns: ["g", "key", "value"].into(), + tumbling_windows: 1, + chain_tumbling_window_width_ms: None, + raw_rows_span_ms: None, + state_input_rows: None, + }; + let family = FieldDataType::Sketch(algorithm); + let states_schema = Schema::new(vec![ + Field::new("g", FieldDataType::Plain(DataType::Utf8), false), + Field::new("state", family.clone(), false), + ]); + let build = PhysicalASAPDAGNode { + parameters: Some(shape.parameters.clone()), + output_schema: states_schema.clone(), + ..node(Payload::ASAP(ASAPOp::SummaryAgg { + child: 0, + summary_type: family.clone(), + input: update.clone(), + reduction: Reduction::by(vec![0]), + filter: None, + })) + }; + let all_rows = shape.groups * shape.rows_per_state; + let build_usage = + node_resource_usage_and_cost(&build, &[rows(all_rows)], (1, 1.0), None, &data, &settings) + .unwrap(); + assert_eq!(build_usage.output.rows, shape.groups); + let readout = node(Payload::ASAP(ASAPOp::SummaryEstimate { + summary_input: 0, + query: statistic, + })); + let readout_usage = node_resource_usage_and_cost( + &readout, + &[produced(&build, &build_usage)], + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap(); + let merge = PhysicalASAPDAGNode { + parameters: Some(shape.parameters.clone()), + ..node(Payload::ASAP(ASAPOp::SummaryMerge { + children: vec![0], + group_by: Some(vec![]), + })) + }; + let merge_usage = node_resource_usage_and_cost( + &merge, + &[produced(&build, &build_usage)], + (1, 1.0), + None, + &data, + &settings, + ) + .unwrap(); + assert_eq!(merge_usage.output.rows, 1); + // At ingestion time, one second of a 60 s window's rows. + let mut chain = PhysicalASAPDAGNode { + parameters: Some(shape.parameters.clone()), + output_schema: states_schema, + ..node(Payload::ASAP(ASAPOp::SummaryTumblingWindowChain { + child: 0, + summary_type: family, + input: update, + reduction: Reduction::by(vec![0]), + filter: None, + tumbling_window_width_ms: 60_000, + })) + }; + chain.output_state = ExecutionDataState::INGESTION_ROWS; + chain.lifetime = Lifetime::AcrossEvaluations(StorageMedium::Memory); + chain.retention_ms = Some(60_000); + let chain_usage = node_resource_usage_and_cost( + &chain, + &[rows(all_rows.div_ceil(60))], + (1, 1.0), + Some(60_000), + &data, + &settings, + ) + .unwrap(); + CounterSketchCharges { + build_ms: build_usage.work_ms_per_execution, + readout_ms: readout_usage.work_ms_per_execution, + merge_ms: merge_usage.work_ms_per_execution, + ingestion_build_ms: chain_usage.cpu_ops_per_second * 60.0 * 1e-6, + } +} + +/// A Count Sketch with heap built at query time costs more the wider it +/// is, as the executor measures it: the top-10 by `url` over 100 rows of +/// 100 URLs into one state at depth 81 measures 3.08 ms at width 300 and +/// 6.35 ms at width 30,000, which allocates and zeroes 2.4 M counters more +/// (the executor's per-group keyed build, thread CPU, median of 3 runs on +/// one pinned core). Charged per row alone, both cost 0.15 ms. The ratio +/// of the charges is within the build fit's leave-one-setting-out error +/// (0.313 RMS log, a factor of 1.37) of the measured ratio. +#[test] +fn a_wider_query_time_count_sketch_costs_more_as_the_executor_measures() { + let charged = |width| { + counter_sketch_charges_ms(&CounterSketchShape { + parameters: SketchParams::CountSketchWithHeap { + width, + depth: 81, + heap_size: 10, + }, + groups: 1, + rows_per_state: 100, + items: 100, + }) + .build_ms + }; + let (narrow, wide) = (charged(300), charged(30_000)); + let measured_ratio = 6.351 / 3.076; + assert!( + wide > narrow && (1.0 / 1.37..=1.37).contains(&(wide / narrow / measured_ratio)), + "width 300 charged {narrow:.3} ms (measured 3.076), width 30,000 {wide:.3} ms (measured 6.351)" + ); +} + +/// Counter-matrix sketches measured on the executor, at query and at +/// ingestion time, are charged within the fit's range of measured ÷ +/// charged over its whole grid (235 builds, 158 readouts, 126 merges, 17 +/// ingestion builds; see `operations_per_unit`), rounded outwards: builds +/// 0.54–4.94, readouts of 10 states or more 0.65–2.66, merges 0.79–2.76, +/// ingestion builds 0.61–2.37. +/// Charged per row alone, without a state's counters, its heap's +/// re-estimates or its size, the builds were 12–800x under. +#[test] +fn counter_matrix_sketches_are_charged_their_measured_time() { + let cms = |width, depth| SketchParams::CmsWithHeap { + width, + depth, + heap_size: 10, + }; + let count_sketch = |width, depth| SketchParams::CountSketchWithHeap { + width, + depth, + heap_size: 10, + }; + let univmon = |sketch_cols| SketchParams::UnivMon { + heap_size: 1, + sketch_rows: 5, + sketch_cols, + layers: 1, + }; + let shape = |parameters, groups, rows_per_state, items| CounterSketchShape { + parameters, + groups, + rows_per_state, + items, + }; + // (term, shape, measured ms) + let points = [ + ("build", shape(count_sketch(300, 81), 1, 100, 100), 3.076), + ("build", shape(count_sketch(30_000, 81), 1, 100, 100), 6.351), + ( + "build", + shape(count_sketch(7_500, 81), 100, 100, 100), + 670.96, + ), + ("build", shape(cms(272, 5), 100, 100, 100), 32.13), + ("build", shape(cms(2_719, 12), 1_000, 10, 10), 150.92), + ("build", shape(univmon(624), 1, 100_000, 10_000), 62.29), + ("build", shape(univmon(56_817), 100, 1_000, 1_000), 208.99), + ("readout", shape(cms(1_360, 12), 1_000, 10, 10), 15.53), + ( + "readout", + shape(count_sketch(7_500, 81), 100, 100, 100), + 7.10, + ), + ("merge", shape(cms(2_719, 12), 1_000, 10, 10), 91.28), + ( + "merge", + shape(count_sketch(7_500, 81), 100, 100, 100), + 192.73, + ), + ("merge", shape(univmon(56_817), 100, 100, 100), 97.16), + ( + "ingestion build", + shape(cms(2_719, 12), 100, 100, 100), + 73.09, + ), + ("ingestion build", shape(univmon(624), 1_000, 10, 10), 9.99), + ( + "ingestion build", + shape(count_sketch(300, 81), 100, 100, 100), + 416.44, + ), + ]; + for (term, shape, measured) in points { + let charges = counter_sketch_charges_ms(&shape); + let (charged, range) = match term { + "build" => (charges.build_ms, 0.54..=4.94), + "readout" => (charges.readout_ms, 0.65..=2.66), + "merge" => (charges.merge_ms, 0.79..=2.76), + _ => (charges.ingestion_build_ms, 0.61..=2.37), + }; + assert!( + range.contains(&(measured / charged)), + "{term} of {:?}, {} states of {} rows of {} items: charged {charged:.3} ms, measured {measured}", + shape.parameters, + shape.groups, + shape.rows_per_state, + shape.items + ); + } +} + +/// A heap's top-k read and its merge estimate the items the heap holds, +/// which are the distinct items its state folded, at most its size: a heap +/// of 10 over 1,000 rows of 5 URLs holds 5, as its updates count them, not +/// 10. Its read is charged 55 operations per counter of depth less for each +/// item it does not hold, its merge 53. +#[test] +fn a_heap_over_few_distinct_items_is_read_and_merged_for_the_items_it_holds() { + let charges = |items| { + counter_sketch_charges_ms(&CounterSketchShape { + parameters: SketchParams::CmsWithHeap { + width: 272, + depth: 10, + heap_size: 10, + }, + groups: 10, + rows_per_state: 1_000, + items, + }) + }; + let (few, many) = (charges(5), charges(10_000)); + let fewer_residents_ms = |per_counter: f64| 10.0 * 5.0 * per_counter * 10.0 * 1e-6; + assert!( + (many.readout_ms - few.readout_ms - fewer_residents_ms(55.0)).abs() < 1e-9, + "readout {} ms over 5 items, {} over 10,000", + few.readout_ms, + many.readout_ms + ); + assert!( + (many.merge_ms - few.merge_ms - fewer_residents_ms(53.0)).abs() < 1e-9, + "merge {} ms over 5 items, {} over 10,000", + few.merge_ms, + many.merge_ms + ); +} diff --git a/crates/devtools/tests/sketch_plan_snapshots/manifest.json b/crates/devtools/tests/sketch_plan_snapshots/manifest.json index a8f59d22a..f6f57e28a 100644 --- a/crates/devtools/tests/sketch_plan_snapshots/manifest.json +++ b/crates/devtools/tests/sketch_plan_snapshots/manifest.json @@ -46,7 +46,7 @@ }, { "id": "topk-rate-guarantees-ignored-mvp", - "covers": "topk of a rate with guarantees ignored selects a CMS heap", + "covers": "topk of a rate with guarantees ignored: the exact top-k costs less than a query-time CMS heap", "workload": "crates/devtools/tests/sketch_plan_snapshots/workloads/topk-rate.json", "deployment": "mvp", "accuracy_guarantees": "ignored" diff --git a/crates/devtools/tests/sketch_plan_snapshots/topk-rate-guarantees-ignored-mvp.json b/crates/devtools/tests/sketch_plan_snapshots/topk-rate-guarantees-ignored-mvp.json index 249e5523d..1a272c837 100644 --- a/crates/devtools/tests/sketch_plan_snapshots/topk-rate-guarantees-ignored-mvp.json +++ b/crates/devtools/tests/sketch_plan_snapshots/topk-rate-guarantees-ignored-mvp.json @@ -25,15 +25,13 @@ "accepted": [ { "accepted_metric": null, - "sketches": [ - "CmsWithHeap" - ], - "summary": "no accuracy target (CmsWithHeap)" + "sketches": [], + "summary": "no accuracy target" } ], "cost": { - "cost_per_second": 3.34907479468e-7, - "cpu_core_seconds_per_second": 0.0252173675, + "cost_per_second": 3.64389360906e-7, + "cpu_core_seconds_per_second": 0.027839267, "per_node": { "0": { "cost_per_second": 6.52152e-9, @@ -81,40 +79,31 @@ "timing": "query_time" }, "5": { - "cost_per_second": 1.54061966081e-9, + "cost_per_second": 3.02475975973e-8, "cost_source": "analytical", - "cpu_core_seconds_per_second": 0.000137017, - "detail": "build CmsWithHeap into 1 states: 10000 rows x 137 ops, 1 group visits; x 0.1000/s; holds 3254 bytes for 1.370 ms", + "cpu_core_seconds_per_second": 0.00269, + "detail": "hash aggregate 10000 rows into 10000 groups: 20 ops per row, 10000 group visits; x 0.1000/s; holds 400000 bytes for 26.900 ms", "multiplier": 1, - "operator": "SummaryAgg", + "operator": "Aggregate", "timing": "query_time" }, "6": { - "cost_per_second": 3.93540004831e-12, + "cost_per_second": 7.78844522855e-10, "cost_source": "analytical", - "cpu_core_seconds_per_second": 3.5e-7, - "detail": "estimate 1 groups x 5; x 0.1000/s; holds 120 bytes for 0.003 ms", + "cpu_core_seconds_per_second": 0.000069267, + "detail": "hash aggregate 10000 rows into 1 groups: 69 ops per row, 1 group visits; x 0.1000/s; holds 80016 bytes for 0.693 ms", "multiplier": 1, - "operator": "SummaryEstimate", - "timing": "query_time" - }, - "7": { - "cost_per_second": 5.6220000138e-15, - "cost_source": "analytical", - "cpu_core_seconds_per_second": 5e-10, - "detail": "operator: pass 5 rows; x 0.1000/s; holds 24 bytes for 0.000 ms", - "multiplier": 1, - "operator": "Project", + "operator": "Aggregate", "timing": "query_time" } }, "source": "analytical-cost-v2 (illustrative statistics)", "usage": { - "average_memory_bytes": 44656704.4934, - "cpu_ops_per_second": 24737367.5, + "average_memory_bytes": 44657785.59, + "cpu_ops_per_second": 27359267.0, "disk_bytes": 0, "peak_cpu_cores": 2, - "peak_memory_bytes": 46083590, + "peak_memory_bytes": 46560208, "scanned_bytes_per_second": 4800000.0 } }, @@ -388,31 +377,6 @@ }, { "consumer": 6, - "data_state": { - "primitive": "SummaryState", - "timing": "query_time" - }, - "grouping": "NotApplicable", - "intermediate_schema": { - "closed": true, - "fields": [ - { - "dtype": { - "Sketch": "CmsWithHeap" - }, - "name": "state", - "nullable": false - } - ], - "time_index": null, - "unique_keys": [] - }, - "producer": 5, - "role": "Input", - "window": "NotApplicable" - }, - { - "consumer": 7, "data_state": { "primitive": "Raw", "timing": "query_time" @@ -432,7 +396,7 @@ "dtype": { "Plain": "float64" }, - "name": "topk", + "name": "sum", "nullable": false } ], @@ -443,7 +407,7 @@ ] ] }, - "producer": 6, + "producer": 5, "role": "Input", "window": "NotApplicable" } @@ -820,61 +784,6 @@ "lifetime": { "OneEvaluation": "Memory" }, - "output_schema": { - "closed": true, - "fields": [ - { - "dtype": { - "Sketch": "CmsWithHeap" - }, - "name": "state", - "nullable": false - } - ], - "time_index": null, - "unique_keys": [] - }, - "output_state": { - "primitive": "SummaryState", - "timing": "query_time" - }, - "parameters": { - "CmsWithHeap": { - "depth": 5, - "heap_size": 10, - "width": 28 - } - }, - "payload": { - "ASAP": { - "SummaryAgg": { - "child": 4, - "filter": null, - "input": { - "item": { - "Column": { - "Named": "job" - } - }, - "weight": { - "Column": "SampleValue" - } - }, - "reduction": { - "Reduce": [] - }, - "summary_type": { - "Sketch": "CmsWithHeap" - } - } - } - } - }, - { - "id": 6, - "lifetime": { - "OneEvaluation": "Memory" - }, "output_schema": { "closed": true, "fields": [ @@ -889,7 +798,7 @@ "dtype": { "Plain": "float64" }, - "name": "topk", + "name": "sum", "nullable": false } ], @@ -905,21 +814,34 @@ "timing": "query_time" }, "payload": { - "ASAP": { - "SummaryEstimate": { - "query": { - "TopK": { - "k": 5 + "NonASAP": { + "Aggregate": { + "child": 4, + "filters": [], + "having": null, + "measures": [ + { + "col": null, + "kind": "sum" } - }, - "summary_input": 5 + ], + "output_names": [ + "" + ], + "reduction": { + "Reduce": [ + 2 + ] + } } } } }, { - "id": 7, - "lifetime": "None", + "id": 6, + "lifetime": { + "OneEvaluation": "Memory" + }, "output_schema": { "closed": true, "fields": [ @@ -951,29 +873,29 @@ }, "payload": { "NonASAP": { - "Project": { - "child": 6, - "cols": [ - { - "alias": "topk_5", - "expr": { - "Column": 1 - } - }, + "Aggregate": { + "child": 5, + "filters": [], + "having": null, + "measures": [ { - "alias": "job", - "expr": { - "Column": 0 - } + "k": 5, + "kind": "top_k" } - ] + ], + "output_names": [ + "" + ], + "reduction": { + "Reduce": [] + } } } } } ], "roots": [ - 7 + 6 ], "scalar_roots": [] }