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 <assert.h> 21 #include <math.h> 22 #include <stdarg.h> 23 #include <stdint.h> 24 #include <functional> 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 #include "perfetto/base/build_config.h" 30 #include "perfetto/base/export.h" 31 #include "perfetto/base/logging.h" 32 33 namespace perfetto { 34 namespace 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 try to sort the ingested data. 41 // Note that these options are only applicable to proto traces; other trace 42 // types (e.g. JSON, Fuchsia) use full sorts. 43 enum class SortingMode { 44 // This option allows trace processor to use built-in heuristics about how to 45 // sort the data. Generally, this option is correct for most embedders as 46 // trace processor reads information from the trace to make the best decision. 47 // 48 // The exact heuristics are implementation details but will ensure that all 49 // relevant tables are sorted by timestamp. 50 // 51 // This is the default mode. 52 kDefaultHeuristics = 0, 53 54 // This option forces trace processor to wait for all trace packets to be 55 // passed to it before doing a full sort of all the packets. This causes any 56 // heuristics trace processor would normally use to ingest partially sorted 57 // data to be skipped. 58 kForceFullSort = 1, 59 60 // This option is deprecated in v18; trace processor will ignore it and 61 // use |kDefaultHeuristics|. 62 // 63 // Rationale for deprecation: 64 // The new windowed sorting logic in trace processor uses a combination of 65 // flush and buffer-read lifecycle events inside the trace instead of 66 // using time-periods from the config. 67 // 68 // Recommended migration: 69 // Users of this option should switch to using |kDefaultHeuristics| which 70 // will act very similarly to the pre-v20 behaviour of this option. 71 // 72 // This option is scheduled to be removed in v21. 73 kForceFlushPeriodWindowedSort = 2 74 }; 75 76 // Enum which encodes which event (if any) should be used to drop ftrace data 77 // from before this timestamp of that event. 78 enum class DropFtraceDataBefore { 79 // Drops ftrace data before timestmap specified by the 80 // TracingServiceEvent::tracing_started packet. If this packet is not in the 81 // trace, no data is dropped. If preserve_ftrace_buffer (from the trace 82 // config) is set, no data is dropped. 83 // Note: this event was introduced in S+ so no data will be dropped on R- 84 // traces. 85 // This is the default approach. 86 kTracingStarted = 0, 87 88 // Retains all ftrace data regardless of timestamp and other events. 89 kNoDrop = 1, 90 91 // Drops ftrace data before timestmap specified by the 92 // TracingServiceEvent::all_data_sources_started. If this packet is not in the 93 // trace, no data is dropped. 94 // This option can be used in cases where R- traces are being considered and 95 // |kTracingStart| cannot be used because the event was not present. 96 kAllDataSourcesStarted = 2, 97 }; 98 99 // Enum which encodes which timestamp source (if any) should be used to drop 100 // track event data before this timestamp. 101 enum class DropTrackEventDataBefore { 102 // Retain all track events. This is the default approach. 103 kNoDrop = 0, 104 105 // Drops track events before the timestamp specified by the 106 // TrackEventRangeOfInterest trace packet. No data is dropped if this packet 107 // is not present in the trace. 108 kTrackEventRangeOfInterest = 1, 109 }; 110 111 // Struct for configuring a TraceProcessor instance (see trace_processor.h). 112 struct PERFETTO_EXPORT_COMPONENT Config { 113 // Indicates the sortinng mode that trace processor should use on the passed 114 // trace packets. See the enum documentation for more details. 115 SortingMode sorting_mode = SortingMode::kDefaultHeuristics; 116 117 // When set to false, this option makes the trace processor not include ftrace 118 // events in the raw table; this makes converting events back to the systrace 119 // text format impossible. On the other hand, it also saves ~50% of memory 120 // usage of trace processor. For reference, Studio intends to use this option. 121 // 122 // Note: "generic" ftrace events will be parsed into the raw table even if 123 // this flag is false and all other events which parse into the raw table are 124 // unaffected by this flag. 125 bool ingest_ftrace_in_raw_table = true; 126 127 // Indicates the event which should be used as a marker to drop ftrace data in 128 // the trace before that event. See the ennu documenetation for more details. 129 DropFtraceDataBefore drop_ftrace_data_before = 130 DropFtraceDataBefore::kTracingStarted; 131 132 // Indicates the source of timestamp before which track events should be 133 // dropped. See the enum documentation for more details. 134 DropTrackEventDataBefore drop_track_event_data_before = 135 DropTrackEventDataBefore::kNoDrop; 136 137 // Any built-in metric proto or sql files matching these paths are skipped 138 // during trace processor metric initialization. 139 std::vector<std::string> skip_builtin_metric_paths; 140 141 // When set to true, the trace processor analyzes trace proto content, and 142 // exports the field path -> total size mapping into an SQL table. 143 // 144 // The analysis feature is hidden behind the flag so that the users who don't 145 // need this feature don't pay the performance costs. 146 // 147 // The flag has no impact on non-proto traces. 148 bool analyze_trace_proto_content = false; 149 150 // When set to true, trace processor will be augmented with a bunch of helpful 151 // features for local development such as extra SQL fuctions. 152 bool enable_dev_features = false; 153 }; 154 155 // Represents a dynamically typed value returned by SQL. 156 struct PERFETTO_EXPORT_COMPONENT SqlValue { 157 // Represents the type of the value. 158 enum Type { 159 kNull = 0, 160 kLong, 161 kDouble, 162 kString, 163 kBytes, 164 kLastType = kBytes, 165 }; 166 167 SqlValue() = default; 168 LongSqlValue169 static SqlValue Long(int64_t v) { 170 SqlValue value; 171 value.long_value = v; 172 value.type = Type::kLong; 173 return value; 174 } 175 DoubleSqlValue176 static SqlValue Double(double v) { 177 SqlValue value; 178 value.double_value = v; 179 value.type = Type::kDouble; 180 return value; 181 } 182 StringSqlValue183 static SqlValue String(const char* v) { 184 SqlValue value; 185 value.string_value = v; 186 value.type = Type::kString; 187 return value; 188 } 189 BytesSqlValue190 static SqlValue Bytes(const void* v, size_t size) { 191 SqlValue value; 192 value.bytes_value = v; 193 value.bytes_count = size; 194 value.type = Type::kBytes; 195 return value; 196 } 197 AsDoubleSqlValue198 double AsDouble() const { 199 PERFETTO_CHECK(type == kDouble); 200 return double_value; 201 } AsLongSqlValue202 int64_t AsLong() const { 203 PERFETTO_CHECK(type == kLong); 204 return long_value; 205 } AsStringSqlValue206 const char* AsString() const { 207 PERFETTO_CHECK(type == kString); 208 return string_value; 209 } AsBytesSqlValue210 const void* AsBytes() const { 211 PERFETTO_CHECK(type == kBytes); 212 return bytes_value; 213 } 214 is_nullSqlValue215 bool is_null() const { return type == Type::kNull; } 216 217 // Up to 1 of these fields can be accessed depending on |type|. 218 union { 219 // This string will be owned by the iterator that returned it and is valid 220 // as long until the subsequent call to Next(). 221 const char* string_value; 222 int64_t long_value; 223 double double_value; 224 const void* bytes_value; 225 }; 226 // The size of bytes_value. Only valid when |type == kBytes|. 227 size_t bytes_count = 0; 228 Type type = kNull; 229 }; 230 231 // Data used to register a new SQL module. 232 struct SqlModule { 233 // Must be unique among modules, or can be used to override existing module if 234 // |allow_module_override| is set. 235 std::string name; 236 237 // Pairs of strings used for |IMPORT| with the contents of SQL files being 238 // run. Strings should only contain alphanumeric characters and '.', where 239 // string before the first dot has to be module name. 240 // 241 // It is encouraged that import key should be the path to the SQL file being 242 // run, with slashes replaced by dots and without the SQL extension. For 243 // example, 'android/camera/junk.sql' would be imported by 244 // 'android.camera.junk'. 245 std::vector<std::pair<std::string, std::string>> files; 246 247 // If true, SqlModule will override registered module with the same name. Can 248 // only be set if enable_dev_features is true, otherwise will throw an error. 249 bool allow_module_override; 250 }; 251 252 } // namespace trace_processor 253 } // namespace perfetto 254 255 #endif // INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_ 256