| // Copyright (C) 2024 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| import {Trace} from './trace'; |
| import {App} from './app'; |
| import {RouteArgs} from './route_schema'; |
| |
| /** |
| * This interface defines the shape of the plugins's class constructor (i.e. the |
| * the constructor and all static members of the plugin's class. |
| * |
| * This class constructor is registered with the core. |
| * |
| * On trace load, the core will create a new class instance by calling new on |
| * this constructor and then call its onTraceLoad() function. |
| * @template T The type of the plugin instance. |
| */ |
| export interface PerfettoPluginStatic<T extends PerfettoPlugin> { |
| /** |
| * A unique identifier for the plugin. |
| * |
| * This ID is used to identify the plugin within the Perfetto UI. It is |
| * recommended to use a reverse domain name style (e.g., |
| * `dev.perfetto.MyPlugin`). |
| */ |
| readonly id: string; |
| |
| /** |
| * An optional human-readable description of the plugin. |
| * |
| * This description may be displayed in the UI to provide more information |
| * about the plugin's functionality. |
| */ |
| readonly description?: string; |
| |
| /** |
| * An optional list of other plugins that this plugin depends on. |
| * |
| * The Perfetto UI ensures that all dependencies are loaded before activating |
| * this plugin. |
| */ |
| readonly dependencies?: ReadonlyArray<PerfettoPluginStatic<PerfettoPlugin>>; |
| |
| /** |
| * Called when the plugin is activated, before a trace has been loaded. |
| * |
| * This method is suitable for registering commands, sidebar items, or pages |
| * that are not dependent on a loaded trace. |
| * |
| * @param app The {@link App} instance, providing access to app-wide |
| * functionality. |
| * @param args The initial route arguments when the app was loaded. |
| */ |
| onActivate?(app: App, args: RouteArgs): void; |
| |
| /** |
| * Returns a list of metric visualisations provided by this plugin. |
| * |
| * Metric visualisations are used to display data from trace metrics in a |
| * graphical format. |
| * |
| * @returns An array of {@link MetricVisualisation} objects. |
| */ |
| metricVisualisations?(): MetricVisualisation[]; |
| |
| /** |
| * The constructor for the plugin's trace-scoped instance. |
| * |
| * This constructor is called when a trace is loaded, creating a new instance |
| * of the plugin that is scoped to the loaded trace. |
| * |
| * @param trace The {@link Trace} instance, providing access to trace-scoped |
| * functionality. |
| */ |
| new (trace: Trace): T; |
| } |
| |
| /** |
| * This interface defines the shape of a plugin's trace-scoped instance, which |
| * is created from the class constructor above at trace load time. |
| */ |
| export interface PerfettoPlugin { |
| /** |
| * Called when a trace is loaded. |
| * |
| * This method is suitable for performing trace-specific initialization, such |
| * as querying trace data or registering trace-scoped UI elements. |
| * |
| * @param ctx The {@link Trace} instance, providing access to trace-scoped |
| * functionality. |
| * @param args Optional arguments passed to the plugin during trace loading. |
| */ |
| onTraceLoad?(ctx: Trace, args?: {[key: string]: unknown}): Promise<void>; |
| } |
| |
| /** |
| * Represents a metric visualisation provided by a plugin. |
| * |
| * Metric visualisations are used to display data from trace metrics in a |
| * graphical format, typically using Vega or Vega-Lite specifications. |
| */ |
| export interface MetricVisualisation { |
| /** |
| * The name of the metric (e.g., 'android_camera'). |
| * |
| * This name corresponds to a metric generated by the trace processor. |
| */ |
| metric: string; |
| |
| /** |
| * A Vega or Vega-Lite visualisation specification. |
| * |
| * The data from the metric, extracted by the `path` property, will be |
| * exposed as a datasource named "metric" within the Vega(-Lite) spec. |
| */ |
| spec: string; |
| |
| /** |
| * A path index into the metric data. |
| * |
| * This path specifies how to extract the relevant data from the metric's |
| * protobuf output. For example, if the metric returns: |
| * ```protobuf |
| * { |
| * foo { |
| * bar { |
| * baz: { name: "a" } |
| * baz: { name: "b" } |
| * baz: { name: "c" } |
| * } |
| * } |
| * } |
| * ``` |
| * This becomes the following JSON: |
| * ```json |
| * { "foo": { "bar": { "baz": [ |
| * {"name": "a"}, |
| * {"name": "b"}, |
| * {"name": "c"}, |
| * ]}}} |
| * ``` |
| * And given `path = ["foo", "bar", "baz"]`, the extracted data |
| * `[ {"name": "a"}, {"name": "b"}, {"name": "c"} ]` is passed to the |
| * Vega(-Lite) visualisation. |
| */ |
| path: string[]; |
| } |
| |
| /** |
| * Manages registered plugins and their instances. |
| * |
| * Use this to get instances of other plugins (for inter-plugin |
| * communication) or access metric visualizations. Note that plugin instances |
| * are only available after a trace has been loaded. |
| */ |
| export interface PluginManager { |
| /** |
| * Retrieves an instance of a registered plugin. |
| * |
| * This method can only be called after a trace has been loaded, as plugin |
| * instances are trace-scoped. |
| * |
| * @param plugin The static plugin class (constructor) of the plugin to |
| * retrieve. |
| * @returns The trace-scoped instance of the requested plugin. |
| * @template T The type of the plugin instance. |
| */ |
| getPlugin<T extends PerfettoPlugin>(plugin: PerfettoPluginStatic<T>): T; |
| |
| /** |
| * Returns a list of all metric visualisations provided by all registered |
| * plugins. |
| * |
| * @returns An array of {@link MetricVisualisation} objects. |
| */ |
| metricVisualisations(): MetricVisualisation[]; |
| } |