• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
24 
25 #include <string>
26 #include <unordered_map>
27 #include <utility>
28 #include <vector>
29 
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 // Specifies whether the ftrace data should be "soft-dropped" until a given
100 // global timestamp, meaning we'll still populate the |ftrace_events| table
101 // and some other internal storage, but won't persist derived info such as
102 // slices. See also |DropFtraceDataBefore| above.
103 // Note: this might behave in surprising ways for traces using >1 tracefs
104 // instances, but those aren't seen in practice at the time of writing.
105 enum class SoftDropFtraceDataBefore {
106   // Drop until the earliest timestamp covered by all per-cpu event bundles.
107   // In other words, the maximum of all per-cpu "valid from" timestamps.
108   // Important for correct parsing of traces where the ftrace data is written
109   // into a central perfetto buffer in ring-buffer mode (as opposed to discard
110   // mode).
111   kAllPerCpuBuffersValid = 0,
112 
113   // Keep all events (though DropFtraceDataBefore still applies).
114   kNoDrop = 1
115 };
116 
117 // Enum which encodes which timestamp source (if any) should be used to drop
118 // track event data before this timestamp.
119 enum class DropTrackEventDataBefore {
120   // Retain all track events. This is the default approach.
121   kNoDrop = 0,
122 
123   // Drops track events before the timestamp specified by the
124   // TrackEventRangeOfInterest trace packet. No data is dropped if this packet
125   // is not present in the trace.
126   kTrackEventRangeOfInterest = 1,
127 };
128 
129 // Struct for configuring a TraceProcessor instance (see trace_processor.h).
130 struct PERFETTO_EXPORT_COMPONENT Config {
131   // Indicates the sortinng mode that trace processor should use on the passed
132   // trace packets. See the enum documentation for more details.
133   SortingMode sorting_mode = SortingMode::kDefaultHeuristics;
134 
135   // When set to false, this option makes the trace processor not include ftrace
136   // events in the raw table; this makes converting events back to the systrace
137   // text format impossible. On the other hand, it also saves ~50% of memory
138   // usage of trace processor. For reference, Studio intends to use this option.
139   //
140   // Note: "generic" ftrace events will be parsed into the raw table even if
141   // this flag is false and all other events which parse into the raw table are
142   // unaffected by this flag.
143   bool ingest_ftrace_in_raw_table = true;
144 
145   // Indicates the event which should be used as a marker to drop ftrace data in
146   // the trace before that event. See the enum documentation for more details.
147   DropFtraceDataBefore drop_ftrace_data_before =
148       DropFtraceDataBefore::kTracingStarted;
149 
150   // Specifies whether the ftrace data should be "soft-dropped" until a given
151   // global timestamp.
152   SoftDropFtraceDataBefore soft_drop_ftrace_data_before =
153       SoftDropFtraceDataBefore::kAllPerCpuBuffersValid;
154 
155   // Indicates the source of timestamp before which track events should be
156   // dropped. See the enum documentation for more details.
157   DropTrackEventDataBefore drop_track_event_data_before =
158       DropTrackEventDataBefore::kNoDrop;
159 
160   // Any built-in metric proto or sql files matching these paths are skipped
161   // during trace processor metric initialization.
162   std::vector<std::string> skip_builtin_metric_paths;
163 
164   // When set to true, the trace processor analyzes trace proto content, and
165   // exports the field path -> total size mapping into an SQL table.
166   //
167   // The analysis feature is hidden behind the flag so that the users who don't
168   // need this feature don't pay the performance costs.
169   //
170   // The flag has no impact on non-proto traces.
171   bool analyze_trace_proto_content = false;
172 
173   // When set to true, trace processor will be augmented with a bunch of helpful
174   // features for local development such as extra SQL fuctions.
175   //
176   // Note that the features behind this flag are subject to breakage without
177   // backward compability guarantees at any time.
178   bool enable_dev_features = false;
179 
180   // Sets developer-only flags to the provided values. Does not have any affect
181   // unless |enable_dev_features| = true.
182   std::unordered_map<std::string, std::string> dev_flags;
183 };
184 
185 // Represents a dynamically typed value returned by SQL.
186 struct PERFETTO_EXPORT_COMPONENT SqlValue {
187   // Represents the type of the value.
188   enum Type {
189     kNull = 0,
190     kLong,
191     kDouble,
192     kString,
193     kBytes,
194     kLastType = kBytes,
195   };
196 
197   SqlValue() = default;
198 
LongSqlValue199   static SqlValue Long(int64_t v) {
200     SqlValue value;
201     value.long_value = v;
202     value.type = Type::kLong;
203     return value;
204   }
205 
DoubleSqlValue206   static SqlValue Double(double v) {
207     SqlValue value;
208     value.double_value = v;
209     value.type = Type::kDouble;
210     return value;
211   }
212 
StringSqlValue213   static SqlValue String(const char* v) {
214     SqlValue value;
215     value.string_value = v;
216     value.type = Type::kString;
217     return value;
218   }
219 
BytesSqlValue220   static SqlValue Bytes(const void* v, size_t size) {
221     SqlValue value;
222     value.bytes_value = v;
223     value.bytes_count = size;
224     value.type = Type::kBytes;
225     return value;
226   }
227 
AsDoubleSqlValue228   double AsDouble() const {
229     PERFETTO_CHECK(type == kDouble);
230     return double_value;
231   }
AsLongSqlValue232   int64_t AsLong() const {
233     PERFETTO_CHECK(type == kLong);
234     return long_value;
235   }
AsStringSqlValue236   const char* AsString() const {
237     PERFETTO_CHECK(type == kString);
238     return string_value;
239   }
AsBytesSqlValue240   const void* AsBytes() const {
241     PERFETTO_CHECK(type == kBytes);
242     return bytes_value;
243   }
244 
is_nullSqlValue245   bool is_null() const { return type == Type::kNull; }
246 
247   // Up to 1 of these fields can be accessed depending on |type|.
248   union {
249     // This string will be owned by the iterator that returned it and is valid
250     // as long until the subsequent call to Next().
251     const char* string_value;
252     int64_t long_value;
253     double double_value;
254     const void* bytes_value;
255   };
256   // The size of bytes_value. Only valid when |type == kBytes|.
257   size_t bytes_count = 0;
258   Type type = kNull;
259 };
260 
261 // Data used to register a new SQL module.
262 struct SqlModule {
263   // Must be unique among modules, or can be used to override existing module if
264   // |allow_module_override| is set.
265   std::string name;
266 
267   // Pairs of strings used for |IMPORT| with the contents of SQL files being
268   // run. Strings should only contain alphanumeric characters and '.', where
269   // string before the first dot has to be module name.
270   //
271   // It is encouraged that import key should be the path to the SQL file being
272   // run, with slashes replaced by dots and without the SQL extension. For
273   // example, 'android/camera/jank.sql' would be imported by
274   // 'android.camera.jank'.
275   std::vector<std::pair<std::string, std::string>> files;
276 
277   // If true, SqlModule will override registered module with the same name. Can
278   // only be set if enable_dev_features is true, otherwise will throw an error.
279   bool allow_module_override = false;
280 };
281 
282 }  // namespace trace_processor
283 }  // namespace perfetto
284 
285 #endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_
286