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 
27 #include "perfetto/base/export.h"
28 #include "perfetto/base/logging.h"
29 
30 namespace perfetto {
31 namespace trace_processor {
32 
33 // Various places in trace processor assume a max number of CPUs to keep code
34 // simpler (e.g. use arrays instead of vectors).
35 constexpr size_t kMaxCpus = 128;
36 
37 // Enum which encodes how trace processor should try to sort the ingested data.
38 enum class SortingMode {
39   // This option allows trace processor to use built-in heuristics about how to
40   // sort the data. Generally, this option is correct for most embedders as
41   // trace processor reads information from the trace (e.g. TraceConfig) to make
42   // the best decision.
43   //
44   // The exact heuristics are implementation details but will ensure that all
45   // relevant tables are sorted by timestamp.
46   //
47   // This is the default mode.
48   kDefaultHeuristics = 0,
49 
50   // This option forces trace processor to wait for all trace packets to be
51   // passed to it before doing a full sort of all the packets. This causes any
52   // heuristics trace processor would normally use to ingest partially sorted
53   // data to be skipped.
54   kForceFullSort = 1,
55 
56   // This option forces trace processor to use the |flush_period_ms| specified
57   // in the TraceConfig to perform a windowed sort of the data. The window size
58   // is not guaranteed to be exactly |flush_period_ms| but will be of the same
59   // order of magnitude; the exact value is an implementation detail and should
60   // not be relied upon.
61   //
62   // If a |flush_period_ms| is not specified in the TraceConfig, this mode will
63   // act the same as |SortingMode::kDefaultHeuristics|.
64   kForceFlushPeriodWindowedSort = 2
65 };
66 
67 // Enum which encodes which event (if any) should be used to drop ftrace data
68 // from before this timestamp of that event.
69 enum class DropFtraceDataBefore {
70   // Drops ftrace data before timestmap specified by the
71   // TracingServiceEvent::tracing_started packet. If this packet is not in the
72   // trace, no data is dropped.
73   // Note: this event was introduced in S+ so no data will be dropped on R-
74   // traces.
75   // This is the default approach.
76   kTracingStarted = 0,
77 
78   // Retains all ftrace data regardless of timestamp and other events.
79   kNoDrop = 1,
80 
81   // Drops ftrace data before timestmap specified by the
82   // TracingServiceEvent::all_data_sources_started. If this packet is not in the
83   // trace, no data is dropped.
84   // This option can be used in cases where R- traces are being considered and
85   // |kTracingStart| cannot be used because the event was not present.
86   kAllDataSourcesStarted = 2,
87 };
88 
89 // Struct for configuring a TraceProcessor instance (see trace_processor.h).
90 struct PERFETTO_EXPORT Config {
91   // Indicates the sortinng mode that trace processor should use on the passed
92   // trace packets. See the enum documentation for more details.
93   SortingMode sorting_mode = SortingMode::kDefaultHeuristics;
94 
95   // When set to false, this option makes the trace processor not include ftrace
96   // events in the raw table; this makes converting events back to the systrace
97   // text format impossible. On the other hand, it also saves ~50% of memory
98   // usage of trace processor. For reference, Studio intends to use this option.
99   //
100   // Note: "generic" ftrace events will be parsed into the raw table even if
101   // this flag is false and all other events which parse into the raw table are
102   // unaffected by this flag.
103   bool ingest_ftrace_in_raw_table = true;
104 
105   // Indicates the event which should be used as a marker to drop ftrace data in
106   // the trace before that event. See the ennu documenetation for more details.
107   DropFtraceDataBefore drop_ftrace_data_before =
108       DropFtraceDataBefore::kTracingStarted;
109 };
110 
111 // Represents a dynamically typed value returned by SQL.
112 struct PERFETTO_EXPORT SqlValue {
113   // Represents the type of the value.
114   enum Type {
115     kNull = 0,
116     kLong,
117     kDouble,
118     kString,
119     kBytes,
120   };
121 
122   SqlValue() = default;
123 
LongSqlValue124   static SqlValue Long(int64_t v) {
125     SqlValue value;
126     value.long_value = v;
127     value.type = Type::kLong;
128     return value;
129   }
130 
DoubleSqlValue131   static SqlValue Double(double v) {
132     SqlValue value;
133     value.double_value = v;
134     value.type = Type::kDouble;
135     return value;
136   }
137 
StringSqlValue138   static SqlValue String(const char* v) {
139     SqlValue value;
140     value.string_value = v;
141     value.type = Type::kString;
142     return value;
143   }
144 
BytesSqlValue145   static SqlValue Bytes(const void* v, size_t size) {
146     SqlValue value;
147     value.bytes_value = v;
148     value.bytes_count = size;
149     value.type = Type::kBytes;
150     return value;
151   }
152 
AsDoubleSqlValue153   double AsDouble() const {
154     PERFETTO_CHECK(type == kDouble);
155     return double_value;
156   }
AsLongSqlValue157   int64_t AsLong() const {
158     PERFETTO_CHECK(type == kLong);
159     return long_value;
160   }
AsStringSqlValue161   const char* AsString() const {
162     PERFETTO_CHECK(type == kString);
163     return string_value;
164   }
AsBytesSqlValue165   const void* AsBytes() const {
166     PERFETTO_CHECK(type == kBytes);
167     return bytes_value;
168   }
169 
is_nullSqlValue170   bool is_null() const { return type == Type::kNull; }
171 
172   // Up to 1 of these fields can be accessed depending on |type|.
173   union {
174     // This string will be owned by the iterator that returned it and is valid
175     // as long until the subsequent call to Next().
176     const char* string_value;
177     int64_t long_value;
178     double double_value;
179     const void* bytes_value;
180   };
181   // The size of bytes_value. Only valid when |type == kBytes|.
182   size_t bytes_count = 0;
183   Type type = kNull;
184 };
185 
186 }  // namespace trace_processor
187 }  // namespace perfetto
188 
189 #endif  // INCLUDE_PERFETTO_TRACE_PROCESSOR_BASIC_TYPES_H_
190