parse_sort_by

Function parse_sort_by 

Source
pub fn parse_sort_by(spec: &str) -> Result<Vec<SortKey>>
Expand description

Parse a --sort-by CLI value into a list of SortKeys. Spec format: metric1[:dir1],metric2[:dir2],... where each metric is a name from CTPROF_METRICS or CTPROF_DERIVED_METRICS and dir is asc or desc (case-insensitive — :DESC, :Asc, :asc all work). Direction defaults to desc (largest delta first — operator “show me the largest changes” default).

Whitespace around the metric name and around the direction is trimmed independently, so wait_sum : desc and wait_sum:desc produce identical SortKey values.

Each parsed SortKey stores the matched registry name as &'static str (not a copy of the user’s input), so downstream equality with CtprofMetricDef::name or DerivedMetricDef::name is a content-equality check (str::eq) over the same registry-owned bytes — no per-key allocation outlives this call. The two registries are disjoint, so a name resolves unambiguously to one or the other.

Sorts groups by their aggregated metric values under whatever --group-by axis is in effect. The same spec works under every grouping (pcomm / cgroup / comm / comm-exact) — group rank reflects the per-group aggregate (sum, max, etc. per the metric’s AggRule) of the named metric, OR the per-group derived value for entries from CTPROF_DERIVED_METRICS.

Examples:

  • "wait_sum" → one key, descending.
  • "wait_sum:asc" → one key, ascending.
  • "wait_sum:desc,run_time_ns:desc" → two keys, both descending; lexicographic.
  • "avg_wait_ns:desc" → one key referencing a derived metric, descending.
  • "" → empty Vec (caller falls back to default sort).

Errors:

  • Unknown metric name (not in CTPROF_METRICS AND not in CTPROF_DERIVED_METRICS).
  • Categorical metric name (one whose AggRule is AggRule::Mode / AggRule::ModeChar / AggRule::ModeBool — string- / char- / bool-valued, no scalar to sort by). The default sort already places mode rows last under the delta_pct ladder; sorting BY a mode metric would silently degrade to alphabetical group order.
  • Duplicate metric name across two entries (e.g. --sort-by wait_sum,wait_sum). The second key would never contribute to the lex ordering, so it’s an operator typo rather than a meaningful spec.
  • Direction string other than asc / desc.
  • Empty token between commas (e.g. "a,,b").