1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_ 18 #define INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_ 19 20 #include <cassert> 21 #include <cstdarg> 22 #include <cstddef> 23 #include <cstdint> 24 25 #include <optional> 26 #include <string> 27 #include <unordered_map> 28 #include <utility> 29 #include <vector> 30 31 #include "perfetto/base/export.h" 32 #include "perfetto/base/logging.h" 33 34 namespace perfetto::trace_processor { 35 36 // All metrics protos are in this directory. When loading metric extensions, the 37 // protos are mounted onto a virtual path inside this directory. 38 constexpr char kMetricProtoRoot[] = "protos/perfetto/metrics/"; 39 40 // Enum which encodes how trace processor should parse the ingested data. 41 enum class ParsingMode { 42 // This option causes trace processor to tokenize the raw trace bytes, sort 43 // the events into timestamp order and parse the events into tables. 44 // 45 // This is the default mode. 46 kDefault = 0, 47 48 // This option causes trace processor to skip the sorting and parsing 49 // steps of ingesting a trace, only retaining any information which could be 50 // gathered during tokenization of the trace files. 51 // 52 // Note the exact information available with this option is left intentionally 53 // undefined as it relies heavily on implementation details of trace 54 // processor. It is mainly intended for use by the Perfetto UI which 55 // integrates very closely with trace processor. General users should use 56 // `kDefault` unless they know what they are doing. 57 kTokenizeOnly = 1, 58 59 // This option causes trace processor to skip the parsing step of ingesting 60 // a trace. 61 // 62 // Note this option does not offer any visible benefits over `kTokenizeOnly` 63 // but has the downside of being slower. It mainly exists for use by 64 // developers debugging performance of trace processor. 65 kTokenizeAndSort = 2, 66 }; 67 68 // Enum which encodes how trace processor should try to sort the ingested data. 69 enum class SortingMode { 70 // This option allows trace processor to use built-in heuristics about how to 71 // sort the data. Generally, this option is correct for most embedders as 72 // trace processor reads information from the trace to make the best decision. 73 // 74 // The exact heuristics are implementation details but will ensure that all 75 // relevant tables are sorted by timestamp. 76 // 77 // This is the default mode. 78 kDefaultHeuristics = 0, 79 80 // This option forces trace processor to wait for all events to be passed to 81 // it before doing a full sort of all the events. This causes any 82 // heuristics trace processor would normally use to ingest partially sorted 83 // data to be skipped. 84 kForceFullSort = 1, 85 }; 86 87 // Enum which encodes which event (if any) should be used to drop ftrace data 88 // from before this timestamp of that event. 89 enum class DropFtraceDataBefore { 90 // Drops ftrace data before timestmap specified by the 91 // TracingServiceEvent::tracing_started packet. If this packet is not in the 92 // trace, no data is dropped. If preserve_ftrace_buffer (from the trace 93 // config) is set, no data is dropped. 94 // Note: this event was introduced in S+ so no data will be dropped on R- 95 // traces. 96 // This is the default approach. 97 kTracingStarted = 0, 98 99 // Retains all ftrace data regardless of timestamp and other events. 100 kNoDrop = 1, 101 102 // Drops ftrace data before timestmap specified by the 103 // TracingServiceEvent::all_data_sources_started. If this packet is not in the 104 // trace, no data is dropped. 105 // This option can be used in cases where R- traces are being considered and 106 // |kTracingStart| cannot be used because the event was not present. 107 kAllDataSourcesStarted = 2 108 }; 109 110 // Specifies whether the ftrace data should be "soft-dropped" until a given 111 // global timestamp, meaning we'll still populate the |ftrace_events| table 112 // and some other internal storage, but won't persist derived info such as 113 // slices. See also |DropFtraceDataBefore| above. 114 // Note: this might behave in surprising ways for traces using >1 tracefs 115 // instances, but those aren't seen in practice at the time of writing. 116 enum class SoftDropFtraceDataBefore { 117 // Drop until the earliest timestamp covered by all per-cpu event bundles. 118 // In other words, the maximum of all per-cpu "valid from" timestamps. 119 // Important for correct parsing of traces where the ftrace data is written 120 // into a central perfetto buffer in ring-buffer mode (as opposed to discard 121 // mode). 122 kAllPerCpuBuffersValid = 0, 123 124 // Keep all events (though DropFtraceDataBefore still applies). 125 kNoDrop = 1 126 }; 127 128 // Enum which encodes which timestamp source (if any) should be used to drop 129 // track event data before this timestamp. 130 enum class DropTrackEventDataBefore { 131 // Retain all track events. This is the default approach. 132 kNoDrop = 0, 133 134 // Drops track events before the timestamp specified by the 135 // TrackEventRangeOfInterest trace packet. No data is dropped if this packet 136 // is not present in the trace. 137 kTrackEventRangeOfInterest = 1, 138 }; 139 140 // Struct for configuring a TraceProcessor instance (see trace_processor.h). 141 struct PERFETTO_EXPORT_COMPONENT Config { 142 // Indicates the parsing mode trace processor should use to extract 143 // information from the raw trace bytes. See the enum documentation for more 144 // details. 145 ParsingMode parsing_mode = ParsingMode::kDefault; 146 147 // Indicates the sortinng mode that trace processor should use on the 148 // passed trace packets. See the enum documentation for more details. 149 SortingMode sorting_mode = SortingMode::kDefaultHeuristics; 150 151 // When set to false, this option makes the trace processor not include ftrace 152 // events in the ftrace_event table; this makes converting events back to the 153 // systrace text format impossible. On the other hand, it also saves ~50% of 154 // memory usage of trace processor. For reference, Studio intends to use this 155 // option. 156 // 157 // Note: "generic" ftrace events will be parsed into the ftrace_event table 158 // even if this flag is false. 159 // 160 // Note: this option should really be named 161 // `ingest_ftrace_in_ftrace_event_table` as the use of the `raw` table is 162 // deprecated. 163 bool ingest_ftrace_in_raw_table = true; 164 165 // Indicates the event which should be used as a marker to drop ftrace data in 166 // the trace before that event. See the enum documentation for more details. 167 DropFtraceDataBefore drop_ftrace_data_before = 168 DropFtraceDataBefore::kTracingStarted; 169 170 // Specifies whether the ftrace data should be "soft-dropped" until a given 171 // global timestamp. 172 SoftDropFtraceDataBefore soft_drop_ftrace_data_before = 173 SoftDropFtraceDataBefore::kAllPerCpuBuffersValid; 174 175 // Indicates the source of timestamp before which track events should be 176 // dropped. See the enum documentation for more details. 177 DropTrackEventDataBefore drop_track_event_data_before = 178 DropTrackEventDataBefore::kNoDrop; 179 180 // Any built-in metric proto or sql files matching these paths are skipped 181 // during trace processor metric initialization. 182 std::vector<std::string> skip_builtin_metric_paths; 183 184 // When set to true, the trace processor analyzes trace proto content, and 185 // exports the field path -> total size mapping into an SQL table. 186 // 187 // The analysis feature is hidden behind the flag so that the users who don't 188 // need this feature don't pay the performance costs. 189 // 190 // The flag has no impact on non-proto traces. 191 bool analyze_trace_proto_content = false; 192 193 // When set to true, trace processor will be augmented with a bunch of helpful 194 // features for local development such as extra SQL fuctions. 195 // 196 // Note that the features behind this flag are subject to breakage without 197 // backward compability guarantees at any time. 198 bool enable_dev_features = false; 199 200 // Sets developer-only flags to the provided values. Does not have any affect 201 // unless |enable_dev_features| = true. 202 std::unordered_map<std::string, std::string> dev_flags; 203 204 // When set to true, trace processor will perform additional runtime checks 205 // to catch additional classes of SQL errors. 206 bool enable_extra_checks = false; 207 }; 208 209 // Represents a dynamically typed value returned by SQL. 210 struct PERFETTO_EXPORT_COMPONENT SqlValue { 211 // Represents the type of the value. 212 enum Type { 213 kNull = 0, 214 kLong, 215 kDouble, 216 kString, 217 kBytes, 218 kLastType = kBytes, 219 }; 220 221 SqlValue() = default; 222 LongSqlValue223 static SqlValue Long(int64_t v) { 224 SqlValue value; 225 value.long_value = v; 226 value.type = Type::kLong; 227 return value; 228 } 229 DoubleSqlValue230 static SqlValue Double(double v) { 231 SqlValue value; 232 value.double_value = v; 233 value.type = Type::kDouble; 234 return value; 235 } 236 StringSqlValue237 static SqlValue String(const char* v) { 238 SqlValue value; 239 value.string_value = v; 240 value.type = Type::kString; 241 return value; 242 } 243 BytesSqlValue244 static SqlValue Bytes(const void* v, size_t size) { 245 SqlValue value; 246 value.bytes_value = v; 247 value.bytes_count = size; 248 value.type = Type::kBytes; 249 return value; 250 } 251 AsDoubleSqlValue252 double AsDouble() const { 253 PERFETTO_CHECK(type == kDouble); 254 return double_value; 255 } AsLongSqlValue256 int64_t AsLong() const { 257 PERFETTO_CHECK(type == kLong); 258 return long_value; 259 } AsStringSqlValue260 const char* AsString() const { 261 PERFETTO_CHECK(type == kString); 262 return string_value; 263 } AsBytesSqlValue264 const void* AsBytes() const { 265 PERFETTO_CHECK(type == kBytes); 266 return bytes_value; 267 } 268 is_nullSqlValue269 bool is_null() const { return type == Type::kNull; } 270 271 // Up to 1 of these fields can be accessed depending on |type|. 272 union { 273 // This string will be owned by the iterator that returned it and is valid 274 // as long until the subsequent call to Next(). 275 const char* string_value; 276 int64_t long_value; 277 double double_value; 278 const void* bytes_value; 279 }; 280 // The size of bytes_value. Only valid when |type == kBytes|. 281 size_t bytes_count = 0; 282 Type type = kNull; 283 }; 284 285 // Data used to register a new SQL package. 286 struct SqlPackage { 287 // Must be unique among package, or can be used to override existing package 288 // if |allow_override| is set. 289 std::string name; 290 291 // Pairs of strings mapping from the name of the module used by `INCLUDE 292 // PERFETTO MODULE` statements to the contents of SQL files being executed. 293 // Module names should only contain alphanumeric characters and '.', where 294 // string before the first dot must be the package name. 295 // 296 // It is encouraged that include key should be the path to the SQL file being 297 // run, with slashes replaced by dots and without the SQL extension. For 298 // example, 'android/camera/jank.sql' would be included by 299 // 'android.camera.jank'. This conforms to user expectations of how modules 300 // behave in other languages (e.g. Java, Python etc). 301 std::vector<std::pair<std::string, std::string>> modules; 302 303 // If true, will allow overriding a package which already exists with `name. 304 // Can only be set if enable_dev_features (in the TraceProcessorConfig object 305 // when creating TraceProcessor) is true. Otherwise, this option will throw an 306 // error. 307 bool allow_override = false; 308 }; 309 310 // Struct which defines how the trace should be summarized by 311 // `TraceProcessor::Summarize`. 312 struct TraceSummaryComputationSpec { 313 // The set of metric ids which should be computed and returned in the 314 // `TraceSummary` proto. 315 std::vector<std::string> v2_metric_ids; 316 317 // The id of the query (which must exist in the `query` field of one of the 318 // TraceSummary specs) which will be used to populate the `metadata` field of 319 // the TraceSummary proto. This query *must* output exactly two string columns 320 // `key` and `value` which will be used to populate the `metadata` field of 321 // the output proto. 322 std::optional<std::string> metadata_query_id; 323 }; 324 325 // A struct which defines the how the TraceSummary output proto should be 326 // formatted. 327 struct TraceSummaryOutputSpec { 328 // The file format of the output returned from the trace summary functions. 329 enum class Format : uint8_t { 330 // Indicates that the ouput is `TraceSummary` encoded as a binary protobuf. 331 kBinaryProto, 332 // Indicates that the ouput is `TraceSummary` encoded as a text protobuf. 333 kTextProto, 334 }; 335 Format format; 336 }; 337 338 // A struct wrapping the bytes of a `TraceSummarySpec` instance. 339 struct TraceSummarySpecBytes { 340 // The pointer to the contents of `TraceSummarySpec` 341 const uint8_t* ptr; 342 343 // The number of bytes of the `TraceSummarySpec. 344 size_t size; 345 346 // The format of the data located at the pointer above. 347 enum class Format : uint8_t { 348 // Indicates that the spec is `TraceSummarySpec` encoded as a binary 349 // protobuf. 350 kBinaryProto, 351 // Indicates that the spec is `TraceSummarySpec` encoded as a text 352 // protobuf. 353 kTextProto, 354 }; 355 Format format; 356 }; 357 358 // A struct wrapping the bytes of a `StructuredQuery` instance. 359 struct StructuredQueryBytes { 360 // The pointer to the contents of `StructuredQuery` 361 const uint8_t* ptr; 362 363 // The number of bytes of the `StructuredQuery. 364 size_t size; 365 366 // The format of the data located at the pointer above. 367 enum class Format : uint8_t { 368 // Indicates that the spec is `StructuredQuery` encoded as a binary 369 // protobuf. 370 kBinaryProto, 371 // Indicates that the spec is `StructuredQuery` encoded as a text 372 // protobuf. 373 kTextProto, 374 }; 375 Format format; 376 }; 377 378 // Experimental. Not considered part of Trace Processor API and shouldn't be 379 // used. 380 struct AnalyzedStructuredQuery { 381 std::string sql; 382 std::string textproto; 383 384 // Modules referenced by sql 385 std::vector<std::string> modules; 386 // Preambles referenced by sql 387 std::vector<std::string> preambles; 388 }; 389 390 // Deprecated. Please use `RegisterSqlPackage` and `SqlPackage` instead. 391 struct SqlModule { 392 std::string name; 393 std::vector<std::pair<std::string, std::string>> files; 394 bool allow_module_override = false; 395 }; 396 397 } // namespace perfetto::trace_processor 398 399 #endif // INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_ 400