Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/perfetto/trace_processor/trace_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ class PERFETTO_EXPORT_COMPONENT TraceProcessor : public TraceProcessorStorage {
// materialization of structured queries. On success, |out| is populated with
// the new instance and ownership is transferred to the caller.
virtual base::Status CreateSummarizer(std::unique_ptr<Summarizer>* out) = 0;

// EXPERIMENTAL: Converts a proto from binary format to textproto format.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhhhhh why do you need this? This seems totally unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sorry please don't add a function like this, this is totally not the right thing to do. You should be able to convert to text proto in JavaScript without going to trace processor. If you need to do it on c++ see how it's done for trace config and generalize. I've allowed ad-hoc changes on TP and not looked too closely but this is where I draw the line. I don't want to add random things in TP just because it's convenient for explore page.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add functionality to go from protojs to text proto without TP, but I don't get the strong reaction. So far, this is what we have been always doing. To get trace config, in recording page, in Data explorer. The idea of continuing with it was not so random.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No the reaction here is because you are basically just dumping a shortcut to do proto to prototext of any proto trace processor understands. That's not the job of trace processor and I think that should be reasonably clear

// `proto_type` is the fully qualified proto type name (e.g.,
// ".perfetto.protos.TraceSummarySpec").
// `proto_bytes` contains the binary proto data.
// The result is written to `output`.
virtual base::Status ProtoToText(const std::string& proto_type,
const uint8_t* proto_bytes,
size_t proto_size,
std::string* output) = 0;
};

} // namespace perfetto::trace_processor
Expand Down
20 changes: 20 additions & 0 deletions protos/perfetto/trace_processor/trace_processor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ syntax = "proto2";
package perfetto.protos;

import "protos/perfetto/common/descriptor.proto";
import "protos/perfetto/perfetto_sql/structured_query.proto";
import "protos/perfetto/trace_processor/metatrace_categories.proto";
import "protos/perfetto/trace_summary/file.proto";
import "protos/perfetto/trace_summary/v2_metric.proto";

// This file defines the schema for {,un}marshalling arguments and return values
// when interfacing to the trace processor binary interface.
Expand Down Expand Up @@ -114,6 +116,7 @@ message TraceProcessorRpc {
TPM_UPDATE_SUMMARIZER_SPEC = 17;
TPM_QUERY_SUMMARIZER = 18;
TPM_DESTROY_SUMMARIZER = 19;
TPM_PROTO_CONTENT = 20;
}

oneof type {
Expand Down Expand Up @@ -157,6 +160,8 @@ message TraceProcessorRpc {
QuerySummarizerArgs query_summarizer_args = 113;
// For TPM_DESTROY_SUMMARIZER.
DestroySummarizerArgs destroy_summarizer_args = 114;
// For TPM_PROTO_CONTENT.
ProtoContentArgs proto_content_args = 115;

// TraceProcessorMethod response args.
// For TPM_APPEND_TRACE_DATA.
Expand Down Expand Up @@ -185,6 +190,8 @@ message TraceProcessorRpc {
QuerySummarizerResult query_summarizer_result = 217;
// For TPM_DESTROY_SUMMARIZER.
DestroySummarizerResult destroy_summarizer_result = 218;
// For TPM_PROTO_CONTENT.
ProtoContentResult proto_content_result = 219;
}

// Previously: RawQueryArgs for TPM_QUERY_RAW_DEPRECATED
Expand Down Expand Up @@ -495,3 +502,16 @@ message DestroySummarizerArgs {
message DestroySummarizerResult {
optional string error = 1;
}

message ProtoContentArgs {
oneof proto {
TraceSummarySpec trace_summary_spec = 1;
PerfettoSqlStructuredQuery structured_query = 2;
TraceMetricV2Spec metric_spec = 3;
}
}

message ProtoContentResult {
optional string textproto = 1;
optional string error = 2;
}
Binary file modified python/perfetto/trace_processor/trace_processor.descriptor
Binary file not shown.
37 changes: 37 additions & 0 deletions src/trace_processor/rpc/rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,43 @@ void Rpc::ParseRpcRequest(const uint8_t* data, size_t len) {
resp.Send(rpc_response_fn_);
break;
}
case RpcProto::TPM_PROTO_CONTENT: {
Response resp(tx_seq_id_++, req_type);
protozero::ConstBytes args = req.proto_content_args();
protos::pbzero::ProtoContentArgs::Decoder decoder(args.data, args.size);

// Determine which proto type was provided and get its bytes
std::string proto_type;
protozero::ConstBytes proto_bytes;

if (decoder.has_trace_summary_spec()) {
proto_type = ".perfetto.protos.TraceSummarySpec";
proto_bytes = decoder.trace_summary_spec();
} else if (decoder.has_structured_query()) {
proto_type = ".perfetto.protos.PerfettoSqlStructuredQuery";
proto_bytes = decoder.structured_query();
} else if (decoder.has_metric_spec()) {
proto_type = ".perfetto.protos.TraceMetricV2Spec";
proto_bytes = decoder.metric_spec();
} else {
auto* result = resp->set_proto_content_result();
result->set_error("No proto provided in ProtoContentArgs");
resp.Send(rpc_response_fn_);
break;
}

auto* result = resp->set_proto_content_result();
std::string textproto;
base::Status status = trace_processor_->ProtoToText(
proto_type, proto_bytes.data, proto_bytes.size, &textproto);
if (!status.ok()) {
result->set_error(status.message());
} else {
result->set_textproto(textproto);
}
resp.Send(rpc_response_fn_);
break;
}
default: {
// This can legitimately happen if the client is newer. We reply with a
// generic "unknown request" response, so the client can do feature
Expand Down
24 changes: 24 additions & 0 deletions src/trace_processor/trace_processor_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,30 @@ void TraceProcessorImpl::EnableMetatrace(MetatraceConfig config) {
// | Experimental |
// =================================================================

base::Status TraceProcessorImpl::ProtoToText(const std::string& proto_type,
const uint8_t* proto_bytes,
size_t proto_size,
std::string* output) {
// Ensure the descriptor is loaded for this proto type
auto opt_idx = metrics_descriptor_pool_.FindDescriptorIdx(proto_type);
if (!opt_idx) {
// Try loading the trace summary descriptor which contains most types we
// need
metrics_descriptor_pool_.AddFromFileDescriptorSet(
kTraceSummaryDescriptor.data(), kTraceSummaryDescriptor.size());
opt_idx = metrics_descriptor_pool_.FindDescriptorIdx(proto_type);
if (!opt_idx) {
return base::ErrStatus("Unknown proto type: %s", proto_type.c_str());
}
}

*output = protozero_to_text::ProtozeroToText(
metrics_descriptor_pool_, proto_type,
protozero::ConstBytes{proto_bytes, proto_size},
protozero_to_text::kIncludeNewLines);
return base::OkStatus();
}

namespace {

class StringInterner {
Expand Down
11 changes: 8 additions & 3 deletions src/trace_processor/trace_processor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,17 @@ class TraceProcessorImpl : public TraceProcessor,

std::vector<uint8_t> GetMetricDescriptors() override;

// ===================
// | Summarizer |
// ===================
// ============================
// | Experimental methods |
// ============================

base::Status CreateSummarizer(std::unique_ptr<Summarizer>* out) override;

base::Status ProtoToText(const std::string& proto_type,
const uint8_t* proto_bytes,
size_t proto_size,
std::string* output) override;

private:
// Needed for iterators to be able to access the context.
friend class IteratorImpl;
Expand Down
38 changes: 38 additions & 0 deletions ui/src/assets/explore_page/node_info/metrics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Metrics

**Purpose:** Define trace-based metrics from your query results. This node packages your data into a `TraceMetricV2TemplateSpec` proto (metric bundle) that can be exported and used in trace analysis pipelines. The selected value column becomes the metric value, and all other columns become dimensions.

**How to use:**

1. **Connect an input:** This node requires a source of data (e.g., from a Table Source or after filtering/aggregating data)

2. **Set Metric ID Prefix:** Give your metric a unique identifier prefix (e.g., `cpu_metrics`, `memory_usage`). The metric will be named `<prefix>_<column_name>`.

3. **Select a Value Column:** Choose a numeric column (int, double, etc.) that contains the metric value you want to track. Then configure:
- **Unit:** Select the appropriate unit for the metric values (Count, Time, Bytes, Percentage, etc.). Use "Custom" for units not in the predefined list.
- **Polarity:** Indicate whether higher or lower values are "better"
- Higher is Better: e.g., throughput, cache hit rate
- Lower is Better: e.g., latency, error count
- Not Applicable: for metrics where direction doesn't apply

4. **Configure Dimension Uniqueness:** Specify whether dimension combinations are unique
- Unique: Each combination of dimension values appears at most once
- Not Unique: The same dimension combination may appear multiple times

**Dimensions:**
All columns in your input **except** the value column automatically become dimensions. Use a Modify Columns node before this one to control which columns are included as dimensions.

**Export:**
Click the "Export" button to generate a textproto representation of your metric template specification. This can be saved and used in trace analysis pipelines.

**Example workflow:**
1. Start with a Table Source (e.g., `slice` table)
2. Add Aggregation to compute `SUM(dur)` grouped by `process_name`
3. Add Metrics node:
- Metric ID Prefix: `slice_stats`
- Value column: `sum_dur` with Unit: Time (nanoseconds), Polarity: Not Applicable
4. Export the metric spec

This creates a metric `slice_stats_sum_dur` with `process_name` as a dimension.

**Output:** The node passes through input columns unchanged. The metric template specification is generated separately via the Export button.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ import {
CounterToIntervalsNode,
CounterToIntervalsNodeState,
} from './nodes/counter_to_intervals_node';
import {
MetricsNode,
MetricsNodeState,
MetricsSerializedState,
} from './nodes/metrics_node';
import {Icons} from '../../../base/semantic_icons';
import {NodeType} from '../query_node';

Expand Down Expand Up @@ -556,4 +561,20 @@ export function registerCoreNodes() {
sqlModules,
}),
});

nodeRegistry.register('metrics', {
name: 'Metrics',
description:
'Define a trace-based metric with value column and dimensions.',
icon: 'analytics',
type: 'modification',
nodeType: NodeType.kMetrics,
factory: (state) => new MetricsNode(state as MetricsNodeState),
deserialize: (state, _trace, sqlModules) =>
new MetricsNode({
...MetricsNode.deserializeState(state as MetricsSerializedState),
sqlModules,
}),
postDeserializeLate: (node) => (node as MetricsNode).onPrevNodesUpdated(),
});
}
Loading
Loading