| #!/usr/bin/env fuchsia-vendored-python | |
| # Copyright 2024 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """ | |
| Returns CPU breakdown in a JSON file. | |
| """ | |
| import argparse | |
| import json | |
| from collections.abc import Sequence | |
| from typing import cast | |
| from trace_processing import hardware_configs, trace_importing, trace_model | |
| from trace_processing.metrics import agg_cpu_breakdown, cpu | |
| # Default cut-off for the percentage CPU. Any process that has CPU below this | |
| # won't be listed in the results. User can pass in a cutoff. | |
| DEFAULT_PERCENT_CUTOFF = 0.0 | |
| def main(argv: Sequence[str] | None = None) -> None: | |
| parser = argparse.ArgumentParser( | |
| description="Breaks down CPU usage by threads that use the highest" | |
| " percentage of the CPU over the runtime of the trace.", | |
| ) | |
| """ | |
| Takes in a trace file (.fxt or .fxt.zst) and outputs | |
| a free-form JSON with breakdown metrics to `output_path`. | |
| """ | |
| subparsers = parser.add_subparsers( | |
| title="Analysis Options", | |
| description="Ways to break down the data provided in the trace", | |
| required=True, | |
| ) | |
| cpu_subparser = subparsers.add_parser( | |
| "by_cpu", | |
| help="Analyzes the trace file data and reports the threads that" | |
| " take the most CPU time over the duration of the trace, organized" | |
| " by CPU.", | |
| ) | |
| cpu_subparser.add_argument( | |
| "path_to_trace", | |
| type=str, | |
| help="Path to trace file (.fxt or .fxt.zst)", | |
| ) | |
| cpu_subparser.add_argument( | |
| "output_path", type=str, help="Output path for CPU breakdown, as .json" | |
| ) | |
| cpu_subparser.add_argument( | |
| "--percent_cutoff", | |
| type=float, | |
| default=DEFAULT_PERCENT_CUTOFF, | |
| help="percent cutoff (CPU usage less than this percent will not be logged)", | |
| ) | |
| parser.add_argument( | |
| "--group_processes", | |
| type=bool, | |
| default=False, | |
| help="Saves CPU breakdown by processes only (not thread-level granularity)", | |
| ) | |
| agg_subparser = subparsers.add_parser( | |
| "by_freq", | |
| help="Analyzes the trace file data and reports the threads that" | |
| " take the most CPU time over the duration of the trace, aggregated" | |
| " over each unique CPU frequency present in the hardware profile.", | |
| ) | |
| agg_subparser.add_argument( | |
| "path_to_trace", | |
| type=str, | |
| help="Path to trace file (.fxt or .fxt.zst)", | |
| ) | |
| agg_subparser.add_argument( | |
| "output_path", type=str, help="Output path for CPU breakdown, as .json" | |
| ) | |
| agg_subparser.add_argument( | |
| "--hardware_profile", | |
| type=str, | |
| default="vim3", | |
| help="The hardware configuration profile to use for CPU frequencies", | |
| ) | |
| agg_subparser.add_argument( | |
| "--percent_cutoff", | |
| type=float, | |
| default=DEFAULT_PERCENT_CUTOFF, | |
| help="percent cutoff (CPU usage less than this percent will not be logged)", | |
| ) | |
| cpu_subparser.set_defaults( | |
| func=lambda args: RunCpuBreakdown(args, args.path_to_trace) | |
| ) | |
| agg_subparser.set_defaults( | |
| func=lambda args: RunAggBreakdown(args, args.path_to_trace) | |
| ) | |
| args: argparse.Namespace = parser.parse_args(argv) | |
| if not args.path_to_trace.endswith((".fxt", ".fxt.zst")): | |
| parser.error("Trace file must be in .fxt or .fxt.zst format") | |
| args.func(args) | |
| def _create_model(trace_path: str) -> trace_model.Model: | |
| processor = cpu.CpuMetricsProcessor() | |
| return trace_importing.create_model_from_trace_file_path( | |
| trace_path, | |
| patterns=processor.event_patterns, | |
| categories=processor.category_names, | |
| without_json_conversion=True, | |
| ) | |
| # Gets the CPU breakdown and outputs it as a freeform JSON file. | |
| def RunCpuBreakdown(args: argparse.Namespace, trace_path: str) -> None: | |
| model = _create_model(trace_path) | |
| processor = cpu.CpuMetricsProcessor(percent_cutoff=args.percent_cutoff) | |
| # TODO(https://github.com/python/mypy/issues/18176): eliminate this cast | |
| # and "untyped_breakdown" local field; the type-checker ought understand | |
| # that a ThreadBreakdown is suitable to use where a metrics.JSON is | |
| # needed. | |
| _, untyped_breakdown = processor.process_freeform_metrics(model) | |
| thread_breakdown = cast(cpu.ThreadBreakdown, untyped_breakdown) | |
| if args.group_processes: | |
| process_breakdown = cpu.group_by_process_name(thread_breakdown) | |
| breakdown = process_breakdown | |
| else: | |
| breakdown = thread_breakdown | |
| with open(args.output_path, "w") as json_file: | |
| json.dump(breakdown, json_file, indent=4) | |
| # Gets the CPU breakdown and aggregates it over the CPU frequencies, | |
| # outputting it as a freeform JSON file. | |
| def RunAggBreakdown(args: argparse.Namespace, trace_path: str) -> None: | |
| model = _create_model(trace_path) | |
| (breakdown, total_time) = cpu.process_metrics_and_get_total_time(model) | |
| hardware_profile = hardware_configs.configs[args.hardware_profile] | |
| agg_breakdown = agg_cpu_breakdown.AggCpuBreakdownMetricsProcessor( | |
| hardware_profile, total_time, args.percent_cutoff | |
| ).aggregate_metrics(breakdown) | |
| with open(args.output_path, "w") as json_file: | |
| json.dump(agg_breakdown, json_file) | |
| if __name__ == "__main__": | |
| main() |