• 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 <stdint.h>
24 #include <functional>
25 #include <string>
26 #include <vector>
27 
28 #include "perfetto/base/build_config.h"
29 #include "perfetto/base/export.h"
30 #include "perfetto/base/logging.h"
31 
32 namespace perfetto {
33 namespace trace_processor {
34 
35 // All metrics protos are in this directory. When loading metric extensions, the
36 // protos are mounted onto a virtual path inside this directory.
37 constexpr char kMetricProtoRoot[] = "protos/perfetto/metrics/";
38 
39 // Enum which encodes how trace processor should try to sort the ingested data.
40 // Note that these options are only applicable to proto traces; other trace
41 // types (e.g. JSON, Fuchsia) use full sorts.
42 enum class SortingMode {
43   // This option allows trace processor to use built-in heuristics about how to
44   // sort the data. Generally, this option is correct for most embedders as
45   // trace processor reads information from the trace to make the best decision.
46   //
47   // The exact heuristics are implementation details but will ensure that all
48   // relevant tables are sorted by timestamp.
49   //
50   // This is the default mode.
51   kDefaultHeuristics = 0,
52 
53   // This option forces trace processor to wait for all trace packets to be
54   // passed to it before doing a full sort of all the packets. This causes any
55   // heuristics trace processor would normally use to ingest partially sorted
56   // data to be skipped.
57   kForceFullSort = 1,
58 
59   // This option is deprecated in v18; trace processor will ignore it and
60   // use |kDefaultHeuristics|.
61   //
62   // Rationale for deprecation:
63   // The new windowed sorting logic in trace processor uses a combination of
64   // flush and buffer-read lifecycle events inside the trace instead of
65   // using time-periods from the config.
66   //
67   // Recommended migration:
68   // Users of this option should switch to using |kDefaultHeuristics| which
69   // will act very similarly to the pre-v20 behaviour of this option.
70   //
71   // This option is scheduled to be removed in v21.
72   kForceFlushPeriodWindowedSort = 2
73 };
74 
75 // Enum which encodes which event (if any) should be used to drop ftrace data
76 // from before this timestamp of that event.
77 enum class DropFtraceDataBefore {
78   // Drops ftrace data before timestmap specified by the
79   // TracingServiceEvent::tracing_started packet. If this packet is not in the
80   // trace, no data is dropped.
81   // Note: this event was introduced in S+ so no data will be dropped on R-
82   // traces.
83   // This is the default approach.
84   kTracingStarted = 0,
85 
86   // Retains all ftrace data regardless of timestamp and other events.
87   kNoDrop = 1,
88 
89   // Drops ftrace data before timestmap specified by the
90   // TracingServiceEvent::all_data_sources_started. If this packet is not in the
91   // trace, no data is dropped.
92   // This option can be used in cases where R- traces are being considered and
93   // |kTracingStart| cannot be used because the event was not present.
94   kAllDataSourcesStarted = 2,
95 };
96 
97 // Struct for configuring a TraceProcessor instance (see trace_processor.h).
98 struct PERFETTO_EXPORT Config {
99   // Indicates the sortinng mode that trace processor should use on the passed
100   // trace packets. See the enum documentation for more details.
101   SortingMode sorting_mode = SortingMode::kDefaultHeuristics;
102 
103   // When set to false, this option makes the trace processor not include ftrace
104   // events in the raw table; this makes converting events back to the systrace
105   // text format impossible. On the other hand, it also saves ~50% of memory
106   // usage of trace processor. For reference, Studio intends to use this option.
107   //
108   // Note: "generic" ftrace events will be parsed into the raw table even if
109   // this flag is false and all other events which parse into the raw table are
110   // unaffected by this flag.
111   bool ingest_ftrace_in_raw_table = true;
112 
113   // Indicates the event which should be used as a marker to drop ftrace data in
114   // the trace before that event. See the ennu documenetation for more details.
115   DropFtraceDataBefore drop_ftrace_data_before =
116       DropFtraceDataBefore::kTracingStarted;
117 
118   // Any built-in metric proto or sql files matching these paths are skipped
119   // during trace processor metric initialization.
120   std::vector<std::string> skip_builtin_metric_paths;
121 };
122 
123 // Represents a dynamically typed value returned by SQL.
124 struct PERFETTO_EXPORT SqlValue {
125   // Represents the type of the value.
126   enum Type {
127     kNull = 0,
128     kLong,
129     kDouble,
130     kString,
131     kBytes,
132   };
133 
134   SqlValue() = default;
135 
LongSqlValue136   static SqlValue Long(int64_t v) {
137     SqlValue value;
138     value.long_value = v;
139     value.type = Type::kLong;
140     return value;
141   }
142 
DoubleSqlValue143   static SqlValue Double(double v) {
144     SqlValue value;
145     value.double_value = v;
146     value.type = Type::kDouble;
147     return value;
148   }
149 
StringSqlValue150   static SqlValue String(const char* v) {
151     SqlValue value;
152     value.string_value = v;
153     value.type = Type::kString;
154     return value;
155   }
156 
BytesSqlValue157   static SqlValue Bytes(const void* v, size_t size) {
158     SqlValue value;
159     value.bytes_value = v;
160     value.bytes_count = size;
161     value.type = Type::kBytes;
162     return value;
163   }
164 
AsDoubleSqlValue165   double AsDouble() const {
166     PERFETTO_CHECK(type == kDouble);
167     return double_value;
168   }
AsLongSqlValue169   int64_t AsLong() const {
170     PERFETTO_CHECK(type == kLong);
171     return long_value;
172   }
AsStringSqlValue173   const char* AsString() const {
174     PERFETTO_CHECK(type == kString);
175     return string_value;
176   }
AsBytesSqlValue177   const void* AsBytes() const {
178     PERFETTO_CHECK(type == kBytes);
179     return bytes_value;
180   }
181 
is_nullSqlValue182   bool is_null() const { return type == Type::kNull; }
183 
184   // Up to 1 of these fields can be accessed depending on |type|.
185   union {
186     // This string will be owned by the iterator that returned it and is valid
187     // as long until the subsequent call to Next().
188     const char* string_value;
189     int64_t long_value;
190     double double_value;
191     const void* bytes_value;
192   };
193   // The size of bytes_value. Only valid when |type == kBytes|.
194   size_t bytes_count = 0;
195   Type type = kNull;
196 };
197 
198 }  // namespace trace_processor
199 }  // namespace perfetto
200 
201 #endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_
202